Skip to content

fix(memory): keep turns outside a truncated session in InMemoryMemoryService - #8387

Open
chaitanya0305 wants to merge 3 commits into
google:mainfrom
chaitanya0305:fix/in-memory-memory-truncated-session
Open

chaitanya0305 wants to merge 3 commits into
google:mainfrom
chaitanya0305:fix/in-memory-memory-truncated-session

Conversation

@chaitanya0305

Copy link
Copy Markdown

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

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

Problem:
InMemoryMemoryService.add_session_to_memory replaced everything stored for a session with the events of the Session it was given. A session loaded with GetSessionConfig(num_recent_events=N) only holds its latest events, so each call dropped the memories of every older turn, and num_recent_events=0 dropped all of them. This happens with a RunConfig that sets get_session_config plus an after_agent_callback that calls ctx.add_session_to_memory(). SqliteMemoryService keeps those entries (#7571).

Solution:
Stored events whose ids are not in session.events at all are kept. Events that are in session.events are replaced from _apply_rewinds(session.events) as before, so turns a rewind took back are still removed and repeated adds do not duplicate. This matches SqliteMemoryService. Memories removed with delete_memory stay removed, because delete_memory drops them from the stored list and records their ids, and re-adds skip those ids.

Testing Plan

Unit Tests:

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

New tests in tests/unittests/memory/test_in_memory_memory_service.py. The windowed cases run on both InMemoryMemoryService and SqliteMemoryService through one fixture, so the two services are checked against each other:

  • a later turn added from a truncated session keeps the earlier turn,
  • a session loaded with num_recent_events=0 removes nothing,
  • repeated full adds store each event once,
  • a rewind inside the window drops its turn and keeps older turns,
  • (InMemory only, since SqliteMemoryService has no delete_memory) a deleted memory stays deleted when a truncated session is added again, with the deleted event inside and outside the window.
$ pytest tests/unittests/memory/test_in_memory_memory_service.py
73 passed

$ pytest -n 2 tests/unittests/memory/
239 passed

Without the change in in_memory_memory_service.py, 4 of the new tests fail (the [in_memory] windowed, num_recent_events=0 and rewind cases, and the delete case); their [sqlite] variants pass both ways.

Manual End-to-End (E2E) Tests:

The reproduction script from #8353 (two turns, then add_session_to_memory with a session loaded with num_recent_events=2):

# before
InMemory search 'Paris': []
Sqlite search 'Paris': ['Noted Paris', 'My favourite city is Paris']

# after
InMemory search 'Paris': ['My favourite city is Paris', 'Noted Paris']
Sqlite search 'Paris': ['Noted Paris', 'My favourite city is Paris']

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

#8141 changes add_events_to_memory in the same file; this PR only changes add_session_to_memory, so the code changes do not overlap. Both PRs append tests at the end of test_in_memory_memory_service.py, so whichever lands second may need a trivial rebase there.

…Service

add_session_to_memory replaced everything stored for the session with the
events of the session it was given. A session loaded with
GetSessionConfig(num_recent_events=...) only holds its latest events, so
each call dropped the memories of every older turn, for example when an
after_agent_callback calls ctx.add_session_to_memory() under a RunConfig
that sets get_session_config.

Stored events that are not in session.events at all are now kept, as in
SqliteMemoryService. Events that are in it are still replaced, so turns a
rewind took back are still removed.
…lite

Runs the windowed add_session_to_memory cases on both InMemoryMemoryService
and SqliteMemoryService: a later turn added from a truncated session, a
session loaded with num_recent_events=0, repeated full adds, and a rewind
inside the window. Also checks that a memory removed with delete_memory
stays removed when a truncated session is added again.
@google-cla

google-cla Bot commented Nov 27, 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.

…ne session

Events added with add_events_to_memory and later windowed
add_session_to_memory calls share the same per-session entry. The test
checks, on InMemory and Sqlite, that a windowed add keeps the directly
added event and older turns, and that adding the same events again
through either method stores each of them once.
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.

InMemoryMemoryService.add_session_to_memory deletes older turns when the session is loaded with num_recent_events

1 participant