Skip to content

fix(sessions): preserve artifact custom_metadata when rewinding - #8510

Open
llalitkumarrr wants to merge 1 commit into
google:mainfrom
llalitkumarrr:fix/8483-rewind-preserve-artifact-custom-metadata
Open

llalitkumarrr wants to merge 1 commit into
google:mainfrom
llalitkumarrr:fix/8483-rewind-preserve-artifact-custom-metadata

Conversation

@llalitkumarrr

Copy link
Copy Markdown
Collaborator

When a rewind restores an artifact to the version it had at the rewind point, compute_artifact_delta_for_rewind loaded that version's data with load_artifact and saved it as a new version, but never carried over the version's custom_metadata. The restored version therefore always had empty metadata, even though the original version still held it.

load_artifact returns only the payload, so the rewind now also reads the version's ArtifactVersion with get_artifact_version and passes its custom_metadata to save_artifact. Tombstones written for artifacts that did not exist at the rewind point keep empty metadata, and if the version metadata cannot be found the data is still restored as before.

Link to Issue or Description of Change

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

2. Or, if no issue exists, describe the change:

Problem:
Runner.rewind_async() restores an artifact to the version it had at the rewind point by loading that version with load_artifact and saving it as a new version. load_artifact returns only the payload and compute_artifact_delta_for_rewind never passed custom_metadata to save_artifact so the restored version always had custom_metadata == {} even though the original version still held it.

Solution:
In compute_artifact_delta_for_rewind after load_artifact succeeds for the rewind point version also call get_artifact_version(..., version=vt) and pass its custom_metadata to save_artifact. The change is confined to src/google/adk/sessions/_rewind_utils.py.

Testing Plan

Unit Tests:

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

New tests:

  • tests/unittests/sessions/test_rewind_utils.py

    • test_compute_artifact_delta_restores_custom_metadata (parametrized): {"unit": "km"} → {"unit": "mi"} → rewind restores {"unit": "km"} and no metadata → {"unit": "mi"} → rewind restores {}.
    • test_compute_artifact_delta_tombstone_has_no_custom_metadata: an artifact created after the rewind point is tombstoned with {}.
    • test_compute_artifact_delta_restores_data_when_version_metadata_missing: when get_artifact_version returns None, data is still restored and custom_metadata=None is passed.
    • The existing AsyncMock based test now sets get_artifact_version.return_value = None so no mock object leaks into save_artifact.
  • tests/unittests/runners/test_runner_rewind.py

Manual End-to-End (E2E) Tests:
The script below reproduces the issue without an LLM or API key. It saves route.csv twice with different custom_metadata, appends the matching session events, rewinds the second invocation through Runner.rewind_async and compares the new current version with version 0.

import asyncio

from google.adk import Agent, Runner
from google.adk.artifacts import InMemoryArtifactService
from google.adk.events import Event, EventActions
from google.adk.sessions import InMemorySessionService
from google.genai import types

APP, USER, SESSION, FILE = "walking_club", "club_member", "route_notes", "route.csv"


async def save_turn(sessions, artifacts, session, inv_id, text, meta):
  await sessions.append_event(session, Event(invocation_id=inv_id, author="user"))
  version = await artifacts.save_artifact(
      app_name=APP, user_id=USER, session_id=SESSION, filename=FILE,
      artifact=types.Part.from_bytes(data=text.encode(), mime_type="text/csv"),
      custom_metadata=meta,
  )
  await sessions.append_event(session, Event(
      invocation_id=inv_id, author="route_assistant",
      actions=EventActions(artifact_delta={FILE: version}),
  ))


async def main():
  artifacts, sessions = InMemoryArtifactService(), InMemorySessionService()
  runner = Runner(agent=Agent(name="route_assistant", model="gemini-2.5-flash"),
                  app_name=APP, artifact_service=artifacts, session_service=sessions)
  session = await sessions.create_session(app_name=APP, user_id=USER, session_id=SESSION)

  await save_turn(sessions, artifacts, session, "inv-1", "route,length\nRiverside,5\n", {"unit": "km"})
  await save_turn(sessions, artifacts, session, "inv-2", "route,length\nLakeside,8\n", {"unit": "mi"})

  await runner.rewind_async(user_id=USER, session_id=SESSION, rewind_before_invocation_id="inv-2")

  ids = dict(app_name=APP, user_id=USER, session_id=SESSION, filename=FILE)
  current = await artifacts.get_artifact_version(**ids)
  data = await artifacts.load_artifact(**ids)
  print("current version :", current.version)
  print("current data    :", data.inline_data.data.decode().strip().replace("\n", " | "))
  print("current metadata:", current.custom_metadata)


asyncio.run(main())


…sion

When a rewind restores an artifact to the version it had at the rewind
point, compute_artifact_delta_for_rewind loaded that version's data with
load_artifact and saved it as a new version, but never carried over the
version's custom_metadata. The restored version therefore always had
empty metadata, even though the original version still held it.

load_artifact returns only the payload, so the rewind now also reads the
version's ArtifactVersion with get_artifact_version and passes its
custom_metadata to save_artifact. Tombstones written for artifacts that
did not exist at the rewind point keep empty metadata, and if the version
metadata cannot be found the data is still restored as before.

Closes google#8483
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.

[Bug] compute_artifact_delta_for_rewind restores file data without custom_metadata

1 participant