adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

SqliteMemoryService and FirestoreMemoryService: add_events_to_memory keeps turns a rewind took back

Open AndresDvst opened this issue 2 days ago • 3 comments

🔴 Required Information

Describe the Bug: Split out of #8137 as suggested there; that issue now covers only InMemoryMemoryService.

SqliteMemoryService.add_events_to_memory and FirestoreMemoryService.add_events_to_memory ignore rewind events. When the events passed as a delta include the event Runner.rewind_async appends, the turns that rewind took back stay in memory and search_memory keeps returning them.

add_session_to_memory on the same services drops them, because it runs _apply_rewinds(session.events) (#7570). The delta path never does, and since the rewind event has no content, it is skipped when the events are written.

Steps to Reproduce:

  1. Check out main (60cef52f) and install it.
  2. Run the script under "Minimal Reproduction Code" (SQLite only, no credentials needed).
  3. It stores turns from invocations a and b with add_events_to_memory, then sends a rewind before b in two ways: as a delta with add_events_to_memory(events=[rewind]), and with add_session_to_memory(session). Each time it counts the matches for baker, a word that only appears in b's turn.

Expected Behavior: Both counts are 0: after the rewind before b, the turn from b is no longer searchable, as with add_session_to_memory.

Observed Behavior:

add_events_to_memory(rewind): 1
add_session_to_memory(session): 0

It also happens within a single delta: add_events_to_memory(events=[a, b, rewind_before_b]) still stores b.

FirestoreMemoryService behaves the same way. With the mocked client from test_firestore_memory_service.py, sending the rewind event through add_events_to_memory issues no delete for b's document, while add_session_to_memory deletes it.

Environment Details:

  • ADK Library Version (pip show google-adk): main at 60cef52f (google.adk.__version__ 2.15.1)
  • Desktop OS: Linux
  • Python Version (python -V): 3.13.5

Model Information:

  • Are you using LiteLLM: N/A (no model involved)
  • Which model is being used: N/A

🟡 Optional Information

Regression: No. add_session_to_memory handles rewinds since v2.14.0 (#7570); add_events_to_memory never did.

Additional Context:

  • Neither backend stores the invocation_id: the SQLite memory_events table and the Firestore documents hold no invocation id. So a delta that holds only the rewind event cannot locate the rows of the rewound invocation and the ones after it.
  • Applying _apply_rewinds to the incoming delta would fix the single-delta case. The cross-call case would need the invocation id stored (a SQLite schema change), plus the handling of contentless invocations being discussed in #8137.
  • Workaround: call add_session_to_memory(session) after a rewind instead of sending only the new events.
  • MongoDB may have the same delta-path gap (#8215 only changes add_session_to_memory); I haven't tested it.

Minimal Reproduction Code:

import asyncio
import os
import tempfile

from google.adk.events import Event, EventActions
from google.adk.memory import SqliteMemoryService
from google.adk.sessions import Session
from google.genai import types

SCOPE = dict(app_name='app', user_id='user', session_id='session')


def turn(invocation_id, text):
  return Event(
      invocation_id=invocation_id, author='user',
      content=types.Content(role='user', parts=[types.Part(text=text)]),
  )


async def count_baker(add_rewind):
  with tempfile.TemporaryDirectory() as tmp:
    service = SqliteMemoryService(os.path.join(tmp, 'memory.db'))
    events = [turn('a', 'trip to paris'), turn('b', 'address is baker street')]
    await service.add_events_to_memory(**SCOPE, events=events)
    rewind = Event(
        author='user', actions=EventActions(rewind_before_invocation_id='b')
    )
    await add_rewind(service, events, rewind)
    result = await service.search_memory(
        app_name='app', user_id='user', query='baker'
    )
    await service.close()
    return len(result.memories)


async def main():
  print('add_events_to_memory(rewind):', await count_baker(
      lambda s, events, rw: s.add_events_to_memory(**SCOPE, events=[rw])))
  print('add_session_to_memory(session):', await count_baker(
      lambda s, events, rw: s.add_session_to_memory(Session(
          id='session', app_name='app', user_id='user',
          events=[*events, rw]))))


asyncio.run(main())

How often has this issue occurred?:

  • Always (100%)

AndresDvst avatar Dec 03 '26 17:12 AndresDvst

May I work on this issue (#8501: SqliteMemoryService and FirestoreMemoryService: addeventstomemory keeps turns a rewind took back) and, if I can reproduce it, submit a focused fix with regression coverage? Please let me know if someone is already handling it or if you would prefer a different approach. I would use Codex assistance and disclose that in any PR.

chelsealong avatar Dec 03 '26 17:12 chelsealong

Thanks for offering! I'm the reporter and I'm already working on a fix, starting from the regression tests behind the report.

The single-call case only needs _apply_rewinds on the incoming events. The cross-call case needs the invocation id stored with each memory entry so a later rewind can find the turns it took back, which for SQLite means a schema change. I'd appreciate a maintainer's view on that part; I'll open a PR linked to this issue.

AndresDvst avatar Dec 03 '26 17:12 AndresDvst

Hello @AndresDvst,

Thank you for raising this issue and for your contribution. We will review it and take the necessary action. We appreciate your patience in the meantime.

llalitkumarrr avatar Dec 04 '26 06:12 llalitkumarrr