Skip to content

fix(sessions): Order database sessions deterministically#6276

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

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

Conversation

@Sehlani042

@Sehlani042 Sehlani042 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • order DatabaseSessionService.list_sessions() by update_time ASC with stable user_id / id tie-breakers
  • add a regression where a newly created but stale orphan session must sort before older-but-active sessions

Why

list_sessions() previously executed without an ORDER BY, so consumers using sessions[-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 follows update_time, which maps to the returned Session.last_update_time.

Fixes #6272

Testing

  • RED before follow-up: uv run pytest tests/unittests/sessions/test_session_service.py::test_database_session_service_list_sessions_orders_by_update_time_then_id -q failed because the stale orphan sorted last under create_time
  • GREEN after follow-up: the same focused regression passed
  • uv 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.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

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.

@rohityan rohityan added the services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc label Jul 6, 2026
@rohityan
rohityan requested a review from wyf7107 July 8, 2026 04:14
@rohityan rohityan added the needs review [Status] The PR/issue is awaiting review from the maintainer label Jul 8, 2026
@rohityan

rohityan commented Jul 8, 2026

Copy link
Copy Markdown
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.

@rohityan

rohityan commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Hi @wyf7107 , can you please review this.

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

Labels

needs review [Status] The PR/issue is awaiting review from the maintainer services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

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

2 participants