Repository navigation
fix(sessions): preserve event append order on tied timestamps (#8051) - #8124
Open
mohitdixit02 wants to merge 2 commits into
Open
mohitdixit02 wants to merge 2 commits into
mohitdixit02 wants to merge 2 commits into
Conversation
its-amann
reviewed
Nov 13, 2026
| def _setup_database_schema(connection: Connection, metadata: MetaData) -> None: | ||
| """Ensures tables and indexes declared in metadata exist.""" | ||
| metadata.create_all(bind=connection) | ||
| _ensure_schema_columns_exist(connection) |
Contributor
There was a problem hiding this comment.
this also runs on the V0 path (prepare_tables calls _setup_database_schema with BaseV0.metadata), so a legacy db gets an ALTER TABLE for a column V0 never reads, and a db user without ALTER rights now fails at startup. gating it on the metadata keeps it V1 only:
Suggested change
| _ensure_schema_columns_exist(connection) | |
| if "event_index" in metadata.tables["events"].c: | |
| _ensure_schema_columns_exist(connection) |
fixed this plus a test in this commit, you can just cherry pick it instead of redoing it: its-amann@8f7ceeb
Author
|
Thanks for catching this edge case with the V0 legacy schema and providing the test! I've cherry-picked your commit (4c14fbc) onto this branch. All unit tests ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to Issue or Description of Change
Problem:
When multiple session events share the same timestamp (for instance, events created within the same microsecond or during batch processing),
DatabaseSessionServiceorders events byStorageEvent.timestamp.desc(), StorageEvent.id.desc(). Because event IDs are randomly generated UUIDs, sorting byid.desc()reorders events arbitrarily rather than preserving their chronological append order. On databases such as PostgreSQL and MySQL, replaying or reloading the session scrambles the event sequence and causes out-of-order execution.Solution:
Introduce a monotonically increasing
event_index: Mapped[int]per session inStorageEvent(V1 schema) and calculate the next index sequentially duringappend_event()under the existing session lock. Updateget_session()and_session_matches_storage_revision()to order bytimestamp.desc(), event_index.desc(), id.desc(), strictly preserving FIFO append order across all database backends while maintaining backward-compatibility with existing tables via automatic schema migration.Before & After Example
Testing Plan
Unit Tests:
I have added or updated unit tests for my change.
All unit tests pass locally.
Updated
test_get_session_orders_tied_timestamps_by_idto assert FIFO append order.Added
test_database_session_service_multiple_events_tied_timestamp_orderverifying 8 events sharing identical timestamps reload in exact append order.Added
test_database_session_service_migration_adds_event_index_columnensuring existing tables withoutevent_indexautomatically migrate and order tied events properly.Local test execution results:
pytest tests/unittests/sessions/test_session_service.py: 509 passed, 1 xfailedpytest tests/unittests/sessions/migration/: 61 passed (19 schema + 42 migration)ruff check src/google/adk/sessions tests/unittests/sessions: All checks passed