Skip to content

fix(a2a): keep streamed artifacts when a Task snapshot arrives mid-stream - #7230

Closed
chelsealong wants to merge 1 commit into
google:mainfrom
chelsealong:fix-a2a-normalizer-snapshot-drops-artifacts-6680
Closed

chelsealong wants to merge 1 commit into
google:mainfrom
chelsealong:fix-a2a-normalizer-snapshot-drops-artifacts-6680

Conversation

@chelsealong

Copy link
Copy Markdown
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

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

Problem:

On the RemoteA2aAgent delegation path (orchestrator → remote specialist), streaming fails client-side with:

append=True for nonexistent artifact_id='<uuid>' in task '<uuid>'.
The artifact must be created (append=False) before appending parts to it.

Re-diagnosed per @surajksharma07's request on #6680 rather than reworking #6686. The cause is in the client-side stream normalizer (_compat.make_stream_normalizer), not in the append/partial flag the server sends:

if kind == "task":
  # A full task state is already passed; use it as the aggregate.
  state["task"] = payload

A running-Task snapshot is only as complete as its sender made it, and servers/proxies emit them with status and no artifacts. When one arrives after the first append=False chunk of an artifact has been folded into the aggregate, the aggregate is replaced wholesale and that artifact is gone; the next append=True chunk then raises inside append_artifact_to_task. Reproduced deterministically through the real normalizer with a2a-sdk 1.1.5:

  ok     A: chunk1(append=F), chunk2(append=T)
  ok     B: snapshot, chunk1, chunk2
  CRASH  C: chunk1, SNAPSHOT, chunk2
         InvalidAgentResponseError: append=True for nonexistent artifact_id=...

Sequence C uses exactly the flags the server sends today. This is a distinct root cause from what #6686 assumed. #6686 forced append=True on the final chunk, which conflicts with the base_llm contract ("Final chunk: partial=False (aggregated content from entire turn)") and with test_convert_event_to_a2a_events_final_chunk_replaces, and would have appended the full accumulated text on top of the already-streamed deltas in the common single-agent case. It masked the crash on the delegation path only because it changed which chunk the client saw first. I am closing #6686 in favour of this.

Solution:

Merge the snapshot into the running aggregate instead of replacing it: artifacts the running task already holds and the snapshot lacks are carried over; an artifact the snapshot does carry wins for that id, so an authoritative snapshot still takes precedence. from_adk_event.py is untouched; the append/partial semantics stay as pinned on main.

Testing Plan

Unit Tests:

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

Two new tests in tests/unittests/a2a/test_compat.py:

  • test_stream_normalizer_task_snapshot_keeps_streamed_artifacts — the exact chunk / snapshot / chunk sequence. Fails on main with the reported append=True for nonexistent artifact_id error, passes with the fix.
  • test_stream_normalizer_task_snapshot_with_artifacts_is_authoritative — a snapshot that carries an artifact wins for that id; only missing ones are carried over.
$ pytest tests/unittests/a2a -q
725 passed, 49 skipped in 10.05s

With the fix reverted and the tests kept: 2 failed (both new tests, with the nonexistent artifact_id message).

Manual End-to-End (E2E) Tests:

The reproduction above is a 30-line script driving make_stream_normalizer() directly with StreamResponse protos; happy to attach it. @virktapvir, if you can re-run your deployed Agent Engine delegation repro against this branch without #6686 applied, that would confirm the diagnosis on the real path — the sequence I reproduced is the one your trace implies (the proxy emits a Task state between artifact events).

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Built with an AI coding agent (Claude Code); diagnosis, reproduction and tests verified by running them as shown above.

…ream

RemoteA2aAgent's client-side stream normalizer (make_stream_normalizer)
replaced its running Task aggregate wholesale whenever a full Task item
arrived. A running-Task snapshot carries status and, from many servers and
proxies, no artifacts; when one lands after the first append=False chunk of
an artifact has been folded in, that artifact is gone from the aggregate,
and the next append=True chunk fails inside append_artifact_to_task with
"append=True for nonexistent artifact_id" (google#6680, the delegation-path crash).

The append/partial flag on the chunks is not the cause: the same sequence
crashes with the flags the server actually sends, and the final chunk of a
stream carries the accumulated content, so it must keep replacing
(append=False) as pinned by test_convert_event_to_a2a_events_final_chunk_replaces.

Merge the snapshot instead: artifacts the running task already holds and the
snapshot lacks are carried over; an artifact the snapshot does carry wins for
that id. Two regression tests: the exact chunk / snapshot / chunk sequence
that raised, and the authoritative-snapshot case.

Fixes google#6680
copybara-service Bot pushed a commit that referenced this pull request Oct 4, 2026
A running-Task snapshot mid-stream may carry status without artifacts.
Carry over previously folded-in artifacts into the aggregate state so
subsequent append chunks can resolve their target artifact, while
returning the snapshot as received to the caller.

Merge #7230

Fixes #6680

PiperOrigin-RevId: 993339769
@adk-bot

adk-bot commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator

Thank you @chelsealong for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 71c4fd7.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Oct 4, 2026
@adk-bot adk-bot closed this Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

3 participants