Skip to content

fix(sessions): preserve append order for tied timestamps in SqliteSessionService - #7287

Closed
chelsealong wants to merge 1 commit into
google:mainfrom
chelsealong:fix-7286-sqlite-session-tied-timestamp-order
Closed

chelsealong wants to merge 1 commit into
google:mainfrom
chelsealong:fix-7286-sqlite-session-tied-timestamp-order

Conversation

@chelsealong

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:

SqliteSessionService.get_session reloads a session's events ordered by
ORDER BY timestamp DESC, id DESC. Event ids are random
(Event.new_id()), so when two events share a timestamp they come back in
an order unrelated to how they were appended — merely "stable" (the same
order on every read), not the actual append order.

Equal timestamps are common: Python 3.12 on Windows ticks every 15.6 ms by
default, and fast function nodes emit several events per tick on any OS.
For a resumable graph Workflow, this breaks resume: ReplayManager
builds the replay sequence barrier from the reloaded events, so a swapped
pair gives the barrier a completion order the workflow can never follow,
and resume fails after the 15s barrier timeout with RuntimeError: Replay divergence detected: ....

Solution:

Break timestamp ties on SQLite's implicit rowid instead of id.
rowid reflects actual insertion order for this table (it is a normal
rowid table — no WITHOUT ROWID clause, and the declared primary key is
non-integer, so SQLite maintains the implicit monotonically increasing
rowid), so ordering on it preserves append order for tied timestamps.

Scope:

The issue also reports the same class of bug in DatabaseSessionService
(order_by(timestamp.desc(), id.desc()), used both in get_session and
in the markerless-session staleness check). Fixing that correctly needs a
portable insertion-order column across both SQLite and Postgres (there is
no equivalent of SQLite's rowid available generically through
SQLAlchemy across dialects), which means a schema/migration change well
beyond a minimal fix. I'm leaving that as a follow-up rather than bundling
a larger, riskier schema change into this PR.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added test_sqlite_session_service_preserves_append_order_on_tied_timestamps
to tests/unittests/sessions/test_session_service.py, asserting that
SqliteSessionService.get_session returns tied-timestamp events in the
order they were appended (both forward and reverse append order).

Also split the pre-existing test_get_session_orders_tied_timestamps_by_id
(which asserted id-order for both DatabaseSessionService and
SqliteSessionService) so it now documents only the still-open
DatabaseSessionService behavior; SqliteSessionService is covered by the
new append-order test instead.

$ git stash push -- src/google/adk/sessions/sqlite_session_service.py
$ pytest tests/unittests/sessions/test_session_service.py -k preserves_append_order_on_tied_timestamps -q
FAILED ...[True] - AssertionError: assert ['event_a', 'event_m', 'event_z'] == ['event_z', 'event_m', 'event_a']

$ git stash pop
$ pytest tests/unittests/sessions/test_session_service.py -k "orders_tied_timestamps_by_id or preserves_append_order_on_tied_timestamps" -q
4 passed in 3.42s

$ pytest tests/unittests/sessions -q
501 passed, 2 xfailed

$ pytest tests/unittests -n auto -q
16176 passed, 82 skipped, 26 xfailed, 2 xpassed

pre-commit run --files src/google/adk/sessions/sqlite_session_service.py tests/unittests/sessions/test_session_service.py passes (isort, pyink,
ruff, addlicense, compliance checks, codespell). The one hook failure it
reports (check-new-py-prefix on src/google/adk/memory/_sqlite_memory_service.py)
is pre-existing on main/unmodified HEAD and unrelated to this change.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have added tests that prove my fix is effective.
  • New and existing unit tests pass locally with my changes.

AI assistance disclosure

This change (analysis, fix, and test) was developed with the assistance of
Claude Code (Anthropic).

…sionService

SqliteSessionService.get_session broke timestamp ties with `ORDER BY
id DESC`. Event ids are random, so tied events come back in an order
unrelated to how they were appended, reshuffling a replayed
conversation and, for a resumable Workflow, handing ReplayManager a
completion order it can never reproduce.

Break ties on `rowid` instead, which reflects actual insertion order.

Fixes google#7286 for SqliteSessionService. DatabaseSessionService has the
same class of bug but needs a portable insertion-order column across
SQLite and Postgres, which is a larger schema/migration change left
for a follow-up.
copybara-service Bot pushed a commit that referenced this pull request Oct 2, 2026
Merge #7287

Break timestamp ties on SQLite rowid instead of random event id in
SqliteSessionService.get_session so events sharing a timestamp reload in
the order they were appended. DatabaseSessionService remains a separate
follow-up requiring a cross-dialect migration.

Part of #7286

PiperOrigin-RevId: 992076400
@adk-bot

adk-bot commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator

Thank you @chelsealong for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit f33343f.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Oct 2, 2026
@adk-bot adk-bot closed this Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Events with equal timestamps reload out of append order (SqliteSessionService, DatabaseSessionService), breaking Workflow replay

3 participants