fix(sessions): Order database sessions deterministically#6276
Open
Sehlani042 wants to merge 6 commits into
Open
fix(sessions): Order database sessions deterministically#6276Sehlani042 wants to merge 6 commits into
Sehlani042 wants to merge 6 commits into
Conversation
DatabaseSessionService.list_sessions previously returned rows without an ORDER BY, so callers using sessions[-1] could receive an older session depending on database heap order. Order rows by create_time with stable primary-key tie breakers so the newest created session is last. Fixes google#6272
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Collaborator
|
Hi @Sehlani042 , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share. |
Collaborator
|
Hi @wyf7107 , can you please review this. |
… into codex/fix-adk-list-sessions-order
This was referenced Jul 20, 2026
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.
Summary
DatabaseSessionService.list_sessions()byupdate_time ASCwith stableuser_id/idtie-breakersWhy
list_sessions()previously executed without anORDER BY, so consumers usingsessions[-1]for the most recently active session could receive an arbitrary row depending on database heap order.The first revision used
create_time, which made the result deterministic but did not match the reporter’s clarified contract: orphaned sessions can be newly created while having stale activity. The ordering therefore followsupdate_time, which maps to the returnedSession.last_update_time.Fixes #6272
Testing
uv run pytest tests/unittests/sessions/test_session_service.py::test_database_session_service_list_sessions_orders_by_update_time_then_id -qfailed because the stale orphan sorted last undercreate_timeuv run pytest tests/unittests/sessions/test_session_service.py -q(135 passed)uv run --extra dev pyink --check src/google/adk/sessions/database_session_service.py tests/unittests/sessions/test_session_service.pyuv run --extra dev ruff check src/google/adk/sessions/database_session_service.py tests/unittests/sessions/test_session_service.pygit diff --check