Skip to content

fix(session): keep in-memory Get from returning events before After - #2089

Merged
wolo-lab merged 3 commits into
google:mainfrom
adityasingh2400:fix/inmemory-after-filter
Nov 5, 2026
Merged

wolo-lab merged 3 commits into
google:mainfrom
adityasingh2400:fix/inmemory-after-filter

Conversation

@adityasingh2400

@adityasingh2400 adityasingh2400 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Linked issue

Problem:
The in-memory Get applies After with a binary search that assumes events are sorted by timestamp. They're kept in append order, so it can return events from before After and drop ones at or after it.

Solution:
Test each event against After instead of searching for a cut point. With NumRecentEvents unset, this returns every event at or after After, which is what the Go doc says and what the database and Vertex AI backends do. With NumRecentEvents set, in-memory still trims to the last N events in append order before filtering, unchanged by this PR and as adk-python's in-memory service does, while those backends filter first and then take the newest N.

adk-python's in-memory filter (in_memory_session_service.py#L220-L227) scans back from the newest event to the first one before After. It therefore drops a qualifying event only when that event was appended ahead of one timestamped before After. This PR keeps such events; a code comment says so.

Behavior change

The in-memory Get no longer returns events timestamped before After, and it no longer drops events at or after After that the old search skipped, such as e40 in the test's six-event window. Results are unchanged when events were appended in timestamp order.

Testing Plan

Unit Tests:

  • All unit tests pass locally.

Added TestInMemoryService_Get_AfterWithOutOfOrderTimestamps, with an After-only row, a NumRecentEvents: 6 row, and a NumRecentEvents: 5 row. The 5-event row pins the existing trim-then-filter order: it passes on main by design and fails if the filter runs before the trim. I didn't add it to sessiontestsuite, because a new suite case would need a new vertexai replay recording.

With your source change reverted and your tests kept, which test fails?

after_only:             Get() event IDs = [e40 e20 e30 e50 e60 e70], want [e40 e30 e50 e60 e70]
with_num_recent_events: Get() event IDs = [e30 e50 e60 e70], want [e40 e30 e50 e60 e70]

With the change, go test -race -count=1 ./session/... passes, go build ./... succeeds, and golangci-lint run reports 0 issues, with golangci-lint fmt --diff and go mod tidy -diff both empty.

Manual End-to-End (E2E) Tests:

N/A, covered by the unit test.

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.

The in-memory Get found the first event to keep with sort.Search, which
assumes the events are sorted by timestamp. They are kept in append
order, and an event carries the time it was created, or whatever time
the agent or tool that built it gave it, rather than the time it was
appended. When those orders differ, the binary search can land on an
earlier event and return events older than GetRequest.After, contrary
to its documented contract ("returns events with timestamp >= the
given time").

Test each event instead. This keeps every event at or after After, as
the database backend does in SQL and as adk-python's database service
does (database_session_service.py:768-774). It diverges from
adk-python's in-memory service (in_memory_session_service.py:220-227),
which scans back from the end and keeps only the trailing run of events
at or after the cutoff; neither returns an event from before it.

@wolo-lab wolo-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two changes are needed before merge: the new comment overstates parity with the database backends, and one test row passes without the fix.

Comment thread session/inmemory.go Resolved
Comment thread session/inmemory_test.go Resolved
Use a six-event window, which fails on main, add a five-event row that pins trimming before filtering, and narrow the comment's parity claim to the After-only case.
@adityasingh2400

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @wolo-lab. Both points are addressed in 55a2504, and the description is updated to match. go test -race -count=1 ./session/... passes, and golangci-lint reports 0 issues, with fmt --diff and go mod tidy -diff empty.

@wolo-lab wolo-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks

@wolo-lab
wolo-lab merged commit 10395f0 into google:main Nov 5, 2026
14 checks passed
wolo-lab added a commit that referenced this pull request Nov 16, 2026
, #1919, #2189, #2285) (#2346)

* fix(session/vertexai): drop temp: keys from the initial state on Create (#2100)

(cherry picked from commit 0c109f7)

* fix(session): keep in-memory Get from returning events before After (#2089)

(cherry picked from commit 10395f0)

* fix(session/database): lock app and user state rows in Create (#1930)

(cherry picked from commit 2f3b2c2)

* fix(session/database): delete a session's events explicitly in Delete (#1774)

(cherry picked from commit 7af0f5f)

* fix(session): define List order as last update time ascending (#1919)

(cherry picked from commit 8be7e25)

* fix(session): skip nil parts in IsFinalResponse classification (#2189)

(cherry picked from commit 75ae920)

* fix(session): count a final error event as a final response (#2285)

(cherry picked from commit d9ecb34)
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.

session: in-memory Get with After can return events older than After

2 participants