Skip to content

Fix: restore event round trip in VertexAiSessionService - #565

Open
AmaadMartin wants to merge 7 commits into
google:mainfrom
AmaadMartin:fix/vertex-ai-session-event-round-trip
Open

Fix: restore event round trip in VertexAiSessionService#565
AmaadMartin wants to merge 7 commits into
google:mainfrom
AmaadMartin:fix/vertex-ai-session-event-round-trip

Conversation

@AmaadMartin

Copy link
Copy Markdown
Collaborator

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):

  • N/A

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

Problem:

Four independent defects in VertexAiSessionService break the appendEvent() -> getSession() round trip on the legacy (non-rawEvent) read path and make the write path unsafe. adk-python already handles all four correctly.

  1. groundingMetadata is written but never restored. appendEvent copies it into config.eventMetadata, but the legacy branch of _fromApiEvent never reads it back, so a replayed event always comes back with groundingMetadata === undefined.
  2. Write/read naming asymmetry on the agent-transfer action. appendEvent copied event.actions verbatim, so the wire payload carried ADK's transferToAgent, while the read path looks for transferAgent (the name the API's EventActions declares, and the camelCase form of the transfer_agent field adk-python writes). The two names never met, so an agent transfer written by adk-js was silently dropped when the session was replayed through the legacy path.
  3. The rawEvent fallback caught everything. The append was wrapped in an undiscriminated try/catch that retried on any throw. A transient failure (5xx, 429, timeout, socket reset) that may already have persisted the event triggered a second appendEvent, duplicating it in the session, and callers saw the failure of the retry rather than of the first attempt. It also rebuilt the request, so the retry could carry a different inv-${Date.now()} invocation id than the first attempt.
  4. partMetadata was not stripped before append. partMetadata is a Gemini Developer API-only Part field that adk-js populates for streamed/partial parts; the Agent Engine Sessions API rejects it with 400 INVALID_ARGUMENT (Unknown name "part_metadata" at 'event.content.parts[0]').

Solution:

All four fixes live in core/src/sessions/vertex_ai_session_service.ts; the service is @experimental, so the wire-format corrections are in scope. No new dependency, no new export, no signature change.

  • _fromApiEvent now restores groundingMetadata from eventMetadata — a direct property read, since the two @google/genai copies in the tree declare a structurally compatible GroundingMetadata, so no cast is needed.
  • appendEvent emits the transfer action as actions.transferAgent via a small toApiEventActions mapper; every other action field, including requestedToolConfirmations, keeps its name. The read path maps transferAgent back to Event.actions.transferToAgent and still accepts a legacy transferToAgent key: the released write path copied event.actions onto the request verbatim and the SDK converter forwards that object unchanged, so already-stored sessions can carry the ADK key and dropping the fallback would silently lose their transfers.
  • The rawEvent retry is now gated on the service rejecting the payload itself (HTTP 400), which is what an API that does not know rawEvent returns. This is a deliberate behaviour change: transient failures now propagate unchanged instead of being quietly re-appended. The error is matched structurally on status rather than with instanceof ApiError, because core and @google-cloud/vertexai resolve separate @google/genai copies and instanceof is false at runtime. The retry also reuses the already-built request, so both attempts share the same name, author, invocationId and timestamp and differ only in rawEvent.
  • appendEvent strips partMetadata from every Part in both config.content and config.rawEvent.content, building a copy so the caller's event is never mutated (partialCopy shares content by reference, so stripping in place would corrupt the caller's event).

Compatibility: new writes emit actions.transferAgent and omit partMetadata, aligning adk-js with adk-python and with the API's declared schema. Old data keeps working because the read path accepts both action key names.

Testing Plan

Unit Tests:

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

Added to core/test/sessions/vertex_ai_session_service_test.ts, reusing the existing mock Sessions fixture and the createEvent / createEventActions / createSession factories (the append mock is typed with the SDK's request-parameter type, so the captured payload is checked by the compiler rather than cast):

  • sends transferToAgent as transferAgent and keeps every other action — asserts the entire captured config.actions, which pins both the rename and the absence of a transferToAgent key, and guards requestedToolConfirmations, which the SDK type omits so the compiler cannot catch its loss.
  • strips partMetadata from content and rawEvent without mutating the event, handles content without parts, handles an event without content.
  • retries without rawEvent when the API rejects it with 400 — records what each attempt actually carried and asserts the retry reuses the same request object (which is what keeps the invocation id and timestamp identical; the previous code minted a fresh one).
  • rethrows a server error / a network error without re-appending — the regression guard for the duplicate append, asserting exactly one call.
  • restores groundingMetadata from eventMetadata, restores transferToAgent from actions.transferAgent, restores transferToAgent from a legacy transferToAgent key.

Commands run locally on the pushed commit:

  • npx vitest run --project unit:core core/test/sessions/vertex_ai_session_service_test.ts — 61 passed.
  • Coverage of the changed file (--coverage.include='core/src/sessions/vertex_ai_session_service.ts'): every new line and branch is executed; the only uncovered branches in the file are pre-existing ones this change does not touch.
  • npm run build, npm run lint, npm run format:check — all pass.

Manual End-to-End (E2E) Tests:

The Agent Engine Sessions backend cannot be exercised in CI, so this needs a real Vertex AI project with an Agent Engine. With projectId / location / agentEngineId pointed at a live reasoning engine:

  1. createSession, then appendEvent with an event that carries actions.transferToAgent, a groundingMetadata payload, and a part with partMetadata set.
  2. Confirm the append succeeds — before this change the partMetadata part fails with 400 INVALID_ARGUMENT (Unknown name "part_metadata").
  3. getSession and confirm the event comes back with actions.transferToAgent and groundingMetadata intact, and with the part's text preserved (partMetadata is intentionally not persisted, matching adk-python).

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.

Additional context

The service is marked @experimental, so the wire-format corrections (emitting actions.transferAgent, omitting partMetadata) are in scope. The read path stays backward compatible with sessions written by earlier adk-js versions.

Amaad Martin added 7 commits July 31, 2026 15:06
The Agent Engine Sessions legacy (rawEvent-less) read path lost data and
the write path was unsafe:

- groundingMetadata was written to eventMetadata but never restored, so
  replayed events always came back without it.
- appendEvent sent ADK's `transferToAgent` while the API (and the read
  path) use `transferAgent`, so agent transfers were silently dropped.
- the rawEvent fallback retried on any error, so a transient 5xx that may
  already have persisted the event appended it a second time and masked
  the original failure. It now retries only on 400/INVALID_ARGUMENT, and
  reuses the same request params so both attempts share an invocation id.
- partMetadata (a Gemini Developer API-only Part field) was sent as-is and
  is rejected by the Sessions API with 400 INVALID_ARGUMENT; it is now
  stripped from a copy of the content, leaving the caller's event intact.

The read path accepts a legacy `transferToAgent` key so sessions written
by earlier adk-js versions keep their transfers.
Adds unit tests for the four round-trip fixes: the transferAgent wire
name (and that the other action fields are untouched), partMetadata
stripping without mutating the caller's event, the narrowed rawEvent
fallback (retries on 400/INVALID_ARGUMENT, rethrows 5xx and network
errors exactly once), and restoring groundingMetadata plus both the new
and legacy transfer keys on read.

Also adds append -> legacy-read round-trip tests, which rebuild the
API's SessionEvent from the captured request without rawEvent to prove
the fixes end to end, and types the append mock so the captured request
is checked against the SDK's parameter type.
- Drop the local ApiEventActions extension: the SDK's EventActions is a
  fine return type, since TypeScript does not excess-property-check the
  spread that carries requestedToolConfirmations. The JSDoc now records
  that the field rides along.
- Match only the error shape the SDK can actually produce. The Sessions
  client is HTTP-only and reports failures as ApiError { status }, so the
  gRPC INVALID_ARGUMENT and numeric `code` checks were unreachable.
- Drop tests that could not fail: assertions comparing the reused request
  object with itself, a read of an absent field with no branch behind it,
  a transfer-key precedence case no writer can produce, and two round
  trips already pinned by exact wire-level assertions.
Second review pass: keep a single append -> legacy-read guard for the
transfer action (the defect that a one-sided test cannot catch) and drop
the separate round-trip suite, assert content without parts by value
rather than by reference, and drop the now-meaningless `Base` suffix on
the SDK EventActions alias.
Third review pass: fold the transferAgent rename assertion into the test
that pins the whole actions payload, fold the non-mutation assertion into
the partMetadata stripping test, inline the single-use legacy-event
builder, and trim the fallback docblock to the rationale that is not
already in the code.

Keeps the legacy `transferToAgent` read fallback: the released write path
copies `event.actions` verbatim and the SDK converter forwards that object
unchanged, so stored sessions can carry the ADK key, and dropping the
fallback would silently lose their transfers.
Fourth review pass: both halves of the transfer round trip are already
pinned to the literal wire key -- the append test asserts the whole
actions payload and the read test feeds `transferAgent` through
`_fromApiEvent` -- so the composition of the two could not fail on its
own.
Rebasing onto main picked up google#567, which added a structural ApiError
status check to getSession in this same file. Two things went stale:

- The isInvalidArgumentError docblock restated google#567's explanation of why
  the match is structural rather than `instanceof`. Point at that comment
  instead of repeating it.
- The append tests hand-rolled `{name: 'ApiError', status}` literals for a
  class the file now imports and that the sibling getSession tests
  construct directly. Throw the real ApiError, so the tests exercise the
  shape the SDK actually produces.

No behaviour change.
@AmaadMartin
AmaadMartin force-pushed the fix/vertex-ai-session-event-round-trip branch from 92e5111 to 58b2a6e Compare July 31, 2026 22:21
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.

1 participant