Runner evaluates plugin replacement event persistence using the original event

Open
#7,184 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start with the provided repro using Runner.run_async, Runner.run_live, on_event_callback, and InMemorySessionService to observe the replacement-event persistence behavior. Review the prepared regression coverage for async, SSE, live media, and before-run paths; done means effective replacement events and their state deltas receive the same persistence and filtering treatment as in-place mutations.

Written by the indexing model from the issue text.

Description

Runner persists plugin replacement events according to the original event's partial/media flags

Problem

on_event_callback can mutate an Event or return a replacement. On main 5bc9e8c9, equivalent transformations behave differently: turning a partial event into a final event in place persists the final event and state delta, while returning a replacement delivers the final event to the caller but loses both the event and state delta from the session.

The same stale-input check affects live media filtering: replacing inline audio with text still excludes the resulting text, while replacing text with inline audio stores data the live persistence filter would normally exclude.

Expected

Persistence decisions should inspect the post-callback event that Runner delivers to the caller. Mutation and replacement should have the same persistence effects for equivalent output events.

Reproducer

Run the following from an ADK development checkout with PYTHONPATH=src python repro.py. No model or network is used.

import asyncio,json
from google.adk.agents import BaseAgent
from google.adk.events import Event,EventActions
from google.adk.plugins.base_plugin import BasePlugin
from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService
from google.adk.live import LiveRequestQueue
from google.genai import types

async def probe(mode,style):
    class Agent(BaseAgent):
        async def _run_async_impl(self,ctx):
            yield Event(author=self.name,partial=True,
                        content=types.Content(role='model',parts=[types.Part(text='draft')]))
        _run_live_impl=_run_async_impl
    class Finalize(BasePlugin):
        async def on_event_callback(self,*,invocation_context,event):
            if not event.partial: return None
            if style=='in_place':
                event.partial=False
                event.content=types.Content(role='model',parts=[types.Part(text='final')])
                event.actions=EventActions(state_delta={'finalized':True})
                return None
            return Event(author=event.author,partial=False,
                         content=types.Content(role='model',parts=[types.Part(text='final')]),
                         actions=EventActions(state_delta={'finalized':True}))
    service=InMemorySessionService()
    runner=Runner(app_name='scout',agent=Agent(name='agent'),session_service=service,
                  plugins=[Finalize(name='finalize')])
    session=await service.create_session(app_name='scout',user_id='u')
    if mode=='live':
        gen=runner.run_live(user_id='u',session_id=session.id,live_request_queue=LiveRequestQueue())
    else:
        gen=runner.run_async(user_id='u',session_id=session.id,new_message=types.Content(role='user',parts=[types.Part(text='hello')]))
    output=[e async for e in gen]
    stored=await service.get_session(app_name='scout',user_id='u',session_id=session.id)
    await runner.close()
    return {'mode':mode,'style':style,'yielded_final':any(e.content and any(p.text=='final' for p in e.content.parts or []) for e in output),
            'stored_final':any(e.content and any(p.text=='final' for p in e.content.parts or []) for e in stored.events),'state':stored.state}

async def main():
    results=[await probe(m,s) for m in ['async','live'] for s in ['in_place','replacement']]
    print(json.dumps(results,indent=2))
if __name__=='__main__':asyncio.run(main())

On unmodified main, all four cases report yielded_final: true. Only in-place cases report stored_final: true and state { "finalized": true }; replacement cases report stored_final: false and empty state.

Diagnosis and verification

Persistence eligibility is currently evaluated against the pre-callback event rather than the effective event returned from on_event_callback. Once a replacement is accepted, persistence and live filtering decisions governing that output should use the effective event.

A focused fix is prepared locally that aligns persistence and live filtering decisions with the effective event. It includes 20 regression cases, including SSE and live media filtering on normal and before-run early-exit paths. Baseline: 7 fail, 13 pass. Patched: all 20 pass; related suites: 259 pass, 1 skip, 3 expected failures. Pre-commit passes. Python 3.11.9/macOS arm64; full supported-Python tox is pending.

I have a focused local fix and regression tests prepared and would be happy to send the PR if this direction is appropriate. I will link a draft pending maintainer confirmation of the persistence semantics and completion of validation.

Related but distinct: #3990 concerns which Event is persisted after callbacks; this report concerns which Event determines persistence eligibility. #5161 proposes a separate post-persistence/pre-yield hook; this report stays within the existing on_event_callback contract. No matching open issue/PR found in my search; please flag any overlapping work.

Dominant language
Python
Stars
21.6k
Forks
4k
Avg merge
13h 49m
Merged PRs (30d)
10

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from google/adk-python

All issues in google/adk-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.