Skip to content

fix: keep state seeded at creation when rewinding a session - #7772

Closed
lisry-1 wants to merge 1 commit into
google:mainfrom
lisry-1:fix/rewind-preserves-seeded-state
Closed

lisry-1 wants to merge 1 commit into
google:mainfrom
lisry-1:fix/rewind-preserves-seeded-state

Conversation

@lisry-1

@lisry-1 lisry-1 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Problem

Runner.rewind_async() — and _rewind_utils.rewind_session() beneath it — deletes
every session-state key that was seeded by create_session(). With
InMemorySessionService:

service = InMemorySessionService()
session = await service.create_session(
    app_name="app", user_id="u1", session_id="s1",
    state={"cart": "apple"},
)
await service.append_event(session, Event(invocation_id="inv1", author="user",
                                          actions=EventActions(state_delta={"step": 1})))
await service.append_event(session, Event(invocation_id="inv2", author="user"))

await _rewind_utils.rewind_session(
    session_service=service, session=session, rewind_before_invocation_id="inv2",
)
print(session.state)

Before the fix:

before rewind: {'cart': 'apple', 'step': 1}
rewind delta:  {'cart': None}          # <- cart was never touched by an event
after rewind:  {'cart': None, 'step': 1}

cart was created with the session, is unrelated to inv2, and is gone after
the rewind. The value is unrecoverable: no event delta records it, so nothing
can replay it, and the None is written into the session's state bucket by the
rewind event like any other delta.

The same happens with any state bucket key seeded at creation time — including
initial_state.json seeding in adk web, state passed to
Runner.run_async's session bootstrap, and state seeded by an
InMemorySessionService/DatabaseSessionService caller.

Root cause

compute_state_delta_for_rewind() rebuilds the state at the rewind point by
replaying the state_delta of the events it keeps, and then treated "absent
from that replay"
as "added after the rewind point":

for key in current_state:
    if key not in state_at_rewind_point:
        rewind_state_delta[key] = None      # "These keys were added after the
                                            #  rewind point and need to be removed."

That inference does not hold. create_session() writes initial state straight
into the session's state bucket without recording an event, so a key that
predates every event is absent from the replay and was deleted.

Fix

A key can be added after the rewind point only if one of the discarded
events introduces it — every state write reaches the session through an event
delta, so a key that none of the discarded events mentions cannot have been
created by them. The deletion branch now requires that:

introduced_after_rewind = set()
for event in session.events[rewind_event_index:]:
    if event.actions and event.actions.state_delta:
        introduced_after_rewind.update(event.actions.state_delta)

for key in current_state:
    if key.startswith(_NON_SESSION_STATE_PREFIXES):
        continue
    if key not in state_at_rewind_point and key in introduced_after_rewind:
        rewind_state_delta[key] = None

Restoration of keys that changed after the rewind point (the first loop) is
untouched, so rewind still reverts every key an event wrote.

This also removes the deletion of keys that a rewind event itself had
restored earlier, which had the same shape: a restored key is not in
state_at_rewind_point for a later rewind to an earlier point, so a second
rewind used to delete it.

Known limitation (unchanged by this PR)

If a key is seeded at creation, then overwritten by a rewound event, the value
it had at the rewind point is not recoverable from the event history, and the
key is still removed. Recording the initial state as a delta on a synthetic
first event would fix that too; it is a larger, storage-visible change and is
deliberately left out of this bug fix.

Tests

tests/unittests/sessions/test_rewind_utils.py:

  • test_rewind_keeps_state_seeded_at_session_creation — end-to-end through
    rewind_session(): a seeded key, and a seeded user: key, survive a rewind;
    the key written by the kept invocation is still restored. Fails before the
    fix
    ({'cart': None}).
  • test_compute_state_delta_still_clears_keys_a_rewound_event_adds — the
    negative case: a key introduced by a discarded event is still deleted, so the
    fix does not simply stop deleting keys. Fails before the fix (it also
    reported the seeded key for deletion).
$ pytest tests/unittests/sessions
1 failed, 704 passed       # the failure is test_load_dialect_impl_spanner,
                           # pre-existing here: google-cloud-sqlalchemy-spanner
                           # is not installed in this environment
$ pytest tests/unittests/sessions/test_rewind_utils.py tests/unittests/apps/test_compaction_rewind_e2e.py
16 passed

Checklist

  • Bug has a reproduction that fails without the change
  • Change is limited to compute_state_delta_for_rewind
  • Regression tests added for both the fixed and the preserved behaviour
  • No behaviour change for app: / user: / temp: keys

@google-cla

google-cla Bot commented Oct 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.

`compute_state_delta_for_rewind` rebuilt the state at the rewind point by
replaying the deltas of the events it keeps, then deleted every key of the
current state that the replay did not produce. State seeded by
`create_session` never appears in an event delta -- it is written straight
into the session's state bucket -- so it was taken to be "added after the
rewind point" and set to None. Rewinding a session therefore erased the
state the session had been created with, which `rewind_async` cannot restore
because no delta records it.

A key now becomes a deletion only when one of the rewound events introduces
it, since an event delta is the only way a key can be added after the rewind
point.
@lisry-1
lisry-1 force-pushed the fix/rewind-preserves-seeded-state branch from ecae737 to 15d085f Compare October 28, 2026 07:19
@GWeale GWeale self-assigned this Nov 2, 2026
@GWeale
GWeale self-requested a review November 2, 2026 16:53
@GWeale

GWeale commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

thanks for putting together the fix and the tests, @lisry-1. #7118 landed the same change in dfd534a, where rewinding now only clears keys written by an undone event. state passed to create_session survives now, and both of your new tests pass on main. it's on main and not in a release yet, so i'm going to close this one out. if you see this happening again, please open a new issue.

@GWeale GWeale closed this Nov 2, 2026
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.

3 participants