Skip to content

fix(sessions): read SQLite session history and state from one snapshot - #8415

Open
WeiLiuu wants to merge 1 commit into
google:mainfrom
WeiLiuu:fix/sqlite-session-read-snapshot
Open

WeiLiuu wants to merge 1 commit into
google:mainfrom
WeiLiuu:fix/sqlite-session-read-snapshot

Conversation

@WeiLiuu

@WeiLiuu WeiLiuu commented Nov 27, 2026

Copy link
Copy Markdown

Link to Issue or Description of Change

With a file-backed SQLite session, a tool can update session, app and user state together, yet a concurrent get_session() can return the old event history and session state alongside the new app and user values. list_sessions() can return the same mix. Each SELECT sees the latest commit independently.

Start a read transaction before fetching the session rows and release it after the last state query. Event parsing and list construction happen after the transaction ends.

Testing Plan

Unit Tests:

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

Both regression cases failed before the fix and passed afterward. The full suite passed on Python 3.11, 3.12 and 3.14 on the first run. On 3.13 one in-memory streaming telemetry test failed on the first run. It passed in isolation on both main and this branch, and the full 3.13 suite passed on rerun. All 14 pre-commit hooks passed.

python -m pytest -q tests/unittests/sessions/test_session_service.py -k reads_one_revision

Manual End-to-End (E2E) Tests:

On a WAL database, start reading a session with three counters set to zero. Between the history read and the app-state read, run a scripted agent on a second connection. Its tool increments all three counters. Before this change the reader saw (0, 1, 1) and no events. It now sees (0, 0, 0). A fresh read sees (1, 1, 1) and four events, and the next turn receives the original tool result. The same check passed with streaming on and off.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

The regression uses WAL so the writer can commit at a known point while the reader is still active. With the default rollback journal, the writer waited for the active reader. Both read methods returned the old counters, and a fresh read returned the updated counters and event.

A concurrent append can commit after get_session reads the session row
but before it reads the events or the app and user state. The returned
session then combines values from different revisions. list_sessions
reads scoped state in separate queries as well.

Hold a read transaction across those queries and release it before
building the returned sessions. Exercise both reads with an append that
commits on a second connection between the session and scoped reads.
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.

1 participant