Skip to content

fix(sessions): Order database sessions deterministically#6276

Open
Sehlani042 wants to merge 2 commits into
google:mainfrom
Sehlani042:codex/fix-adk-list-sessions-order
Open

fix(sessions): Order database sessions deterministically#6276
Sehlani042 wants to merge 2 commits into
google:mainfrom
Sehlani042:codex/fix-adk-list-sessions-order

Conversation

@Sehlani042

Copy link
Copy Markdown

Summary

  • order DatabaseSessionService.list_sessions() by create_time ASC with stable user_id / id tie-breakers
  • add a regression test where database insertion order differs from session creation time order

Why

list_sessions() previously executed without an ORDER BY, so consumers using sessions[-1] for the newest session could receive an older row depending on database heap order.

Fixes #6272

Testing

  • RED before fix: uv run pytest tests/unittests/sessions/test_session_service.py::test_database_session_service_list_sessions_orders_by_create_time_then_id -q failed with unordered ids
  • GREEN after fix: uv run pytest tests/unittests/sessions/test_session_service.py::test_database_session_service_list_sessions_orders_by_create_time_then_id -q
  • uv run pytest tests/unittests/sessions/test_session_service.py::test_create_and_list_sessions tests/unittests/sessions/test_session_service.py::test_list_sessions_all_users tests/unittests/sessions/test_session_service.py::test_database_session_service_list_sessions_orders_by_create_time_then_id -q
  • uv run pytest tests/unittests/sessions/test_session_service.py -q
  • uv run --extra dev pyink --check src/google/adk/sessions/database_session_service.py tests/unittests/sessions/test_session_service.py
  • uv run --extra dev ruff check src/google/adk/sessions/database_session_service.py tests/unittests/sessions/test_session_service.py
  • git diff --check HEAD~1..HEAD

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
@google-cla

google-cla Bot commented Jul 3, 2026

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DatabaseSessionService.list_sessions() returns unordered results — sessions[-1] is unreliable for "latest session" logic

1 participant