Repository navigation
fix(session): keep in-memory Get from returning events before After - #2089
Merged
Merged
Conversation
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
requested changes
Nov 4, 2026
wolo-lab
left a comment
Contributor
There was a problem hiding this comment.
Two changes are needed before merge: the new comment overstates parity with the database backends, and one test row passes without the fix.
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.
Contributor
Author
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)
This was referenced Nov 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Linked issue
Problem:
The in-memory
GetappliesAfterwith a binary search that assumes events are sorted by timestamp. They're kept in append order, so it can return events from beforeAfterand drop ones at or after it.Solution:
Test each event against
Afterinstead of searching for a cut point. WithNumRecentEventsunset, this returns every event at or afterAfter, which is what the Go doc says and what the database and Vertex AI backends do. WithNumRecentEventsset, 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 beforeAfter. This PR keeps such events; a code comment says so.Behavior change
The in-memory
Getno longer returns events timestamped beforeAfter, and it no longer drops events at or afterAfterthat 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:
Added
TestInMemoryService_Get_AfterWithOutOfOrderTimestamps, with an After-only row, aNumRecentEvents: 6row, and aNumRecentEvents: 5row. 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 tosessiontestsuite, because a new suite case would need a new vertexai replay recording.With your source change reverted and your tests kept, which test fails?
With the change,
go test -race -count=1 ./session/...passes,go build ./...succeeds, andgolangci-lint runreports 0 issues, withgolangci-lint fmt --diffandgo mod tidy -diffboth empty.Manual End-to-End (E2E) Tests:
N/A, covered by the unit test.
Checklist