Incremental in-memory ingestion keeps recalling rewound turns
Describe the bug
InMemoryMemoryService.add_events_to_memory ignores rewind events because they have no content. A caller ingesting session deltas therefore keeps returning facts from turns that Runner.rewind_async has undone. Re-ingesting the full session through add_session_to_memory correctly removes them, so the two ingestion APIs disagree.
Reproduction
import asyncio
from google.adk.events import Event, EventActions
from google.adk.memory import InMemoryMemoryService
from google.genai import types
async def main():
service = InMemoryMemoryService()
scope = dict(app_name='app', user_id='user', session_id='session')
await service.add_events_to_memory(**scope, events=[Event(
invocation_id='old', author='user',
content=types.Content(parts=[types.Part(text='address is baker street')]),
)])
await service.add_events_to_memory(**scope, events=[Event(
author='user', actions=EventActions(rewind_before_invocation_id='old'),
)])
result = await service.search_memory(app_name='app', user_id='user', query='baker')
print(len(result.memories))
asyncio.run(main())
Actual: 1; expected: 0.
Environment and proposed fix
Reproduced on main at 12aab4aaa, Python 3.12.10, Windows. Also reproduced through a real InMemoryRunner: ingest its session, rewind the second invocation, then ingest only the newly appended rewind event. The old address remains searchable.
Apply the existing rewind-resolution helper when combining incoming deltas with stored events. Invocation boundaries without text must remain available so rewinding a contentless or previously deleted invocation also removes later turns. Deleted memory payloads must remain deleted; only the minimal invocation anchor should be retained. Keep other sessions isolated.
Prepared with AI assistance; no model provider or external credentials are needed.
Hi, I'd like to work on this unless the reporter plans to send a PR. My plan: in InMemoryMemoryService.add_events_to_memory, resolve the incoming delta together with the stored events through _apply_rewinds, keep a minimal content-free anchor per invocation so rewinding a contentless or deleted invocation still drops the later turns, and keep deleted payloads deleted. I'd add regression tests to tests/unittests/memory/test_in_memory_memory_service.py, including the reproduction above.
_sqlite_memory_service.py and firestore_memory_service.py seem to have the same gap in add_events_to_memory. Should I keep this PR to the in-memory service and open a separate issue for those?
@1aifanatic. Reproduced this on 2.14.1: after a rewind delta, add_events_to_memory still returns the rewound turns, while add_session_to_memory drops them.
For now, a possible workaround is to call add_session_to_memory(session) after a rewind instead of sending only the new rewind event, since that path already runs _apply_rewinds. Could you check whether that works for your setup?
On the fix: #8138 and #8141 both take the same _apply_rewinds approach, so it would be good to settle on one. Please finish the full test run on #8138 before marking it ready, and maybe take a look at how #8141 handles anchors and events with no id.
@AndresDvst I’d keep this issue to the in-memory service. The SQLite/Firestore gap is worth its own issue, so feel free to open one.
Answering the anchors / events-with-no-id part for #8141 (the full side-by-side with #8138 is in the "Relationship to #8138" section of its description):
- Anchors: contentless events and deleted payloads are stored as content-stripped invocation-boundary anchors — unsearchable, but their position and
invocation_idlet a later rewind delta locate the cut. Anchors are deduplicated by id and do not reserve their id: a payload delivered later under an anchored id is still stored. Pinned bytest_add_events_to_memory_keeps_anchor_for_contentless_invocation,test_add_events_to_memory_deleted_payload_keeps_only_anchor, andtest_add_events_to_memory_anchor_does_not_reserve_event_id. - Events with no id: each one is stored (payload if it carries content, anchor otherwise). The old path silently dropped later id-less events once one had been stored — an id-less event carries no identity to dedupe on, so re-delivering one does duplicate it; that trade-off and the suffix-delta assumption are listed in the PR's known limitations.
- Validation status: the Testing Plan on the PR documents the full
toxpy311–py314 matrix and a CPURunnerE2E; I re-rantests/unittests/memory/on the current head (1159745) today — 237 passed.
Thanks @Bensonluo. Both PRs resolve the stored bucket plus the delta through _apply_rewinds, and both merge cleanly on main (2.14.1, where add_events_to_memory still drops contentless events). The real difference is scope. #8141 only keeps anchors on the delta path, so after add_session_to_memory or delete_memory, a later rewind delta targeting an invocation with no stored content (or whose only event was deleted) finds no boundary and leaves the later turns in place. #8138 covers that by keeping anchors on those two paths as well.
Small note: Event.model_post_init always assigns an id, so event.id is never None for a real Event. The id-less branch in #8141 (and the “id-less” framing of the issue repro) doesn’t change any real behaviour.
Since you’ve offered to defer, I’d suggest landing one PR: either #8141 picks up the add_session_to_memory/delete_memory anchors from #8138, or @1aifanatic finishes the full run on #8138 and marks it ready. @1aifanatic, which would you prefer? Also, did the add_session_to_memory workaround hold up on your side?
Hey thanks for contributing to this @1aifanatic and @Bensonluo! An internal team will be taking over from here and your work will be credited.