Skip to content

feat(bedrock): end unread agent completion spans on GC and keep error text out of span status - #816

Open
nik13 wants to merge 3 commits into
devfrom
feat/bedrock-agent-stream-finalization
Open

nik13 wants to merge 3 commits into
devfrom
feat/bedrock-agent-stream-finalization

Conversation

@nik13

@nik13 nik13 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

  1. Ends unread or partly read Bedrock Agent completion spans on garbage collection. In @traceai/bedrock, observeAgentCompletion registers a FinalizationRegistry token whose holdings carry only { span, ended, read, steps } (no reference to the stream, wrapper, iterator, trace builder, or callbacks) and disarms the registration inside finalize. An InvokeAgent / InvokeInlineAgent completion stream dropped before it ends, without return(), ends any open step spans first, then ends the AGENT span best-effort on GC with status UNSET and fi.stream.consumed=false, plus fi.stream.partial=true when some events were read; no timer runs and nothing is read ahead.
  2. Keeps error text out of span status messages. Both code-interpreter execution errors (codeInterpreterInvocationOutput.executionError) and agent failures (failureTrace.failureReason) place their text in output.value (where hideOutputs applies), setting span status to ERROR with no status message and setting a stable error.type (CodeInterpreterExecutionError, or the numeric/string failureCode, falling back to AgentFailure).
  3. Updates typescript/packages/traceai_bedrock/CHANGELOG.md with an [Unreleased] entry covering both changes.

Why?

An InvokeAgent completion stream returned to a caller that did not finish iterating it left its AGENT span (and any open step spans) open indefinitely. In addition, placing executionError and failureReason directly in span status.message bypassed hideOutputs masking, and numeric failureCode values from the AWS SDK fell back to AgentFailure.

How was it tested?

  • New src/__tests__/agent-stream-finalization.test.ts: verifies FinalizationRegistry registration with minimal { ended, read, span, steps } holdings, GC finalization closing open step spans before ending the root span as UNSET (fi.stream.consumed=false, plus fi.stream.partial=true when partly iterated) without timers, unregistration when consumed/closed/errored, and a real --expose-gc collection check.

  • Updated src/__tests__/agent-runtime.test.ts: verifies code-interpreter executionError, failureTrace failureReason + numeric/string failureCode, and agent stream errors under visible outputs and hideOutputs: true.

  • Full @traceai/bedrock build and Jest test suite: 12/12 suites, 111/111 tests pass with 0 new failures.

  • Unit tests added / updated (jest)

  • Integration tests pass (or N/A — no gateway behavior changed)

  • Build / typecheck pass

  • Package CHANGELOG.md updated

Checklist

  • Commit messages follow Conventional Commits
  • No TODOs or commented-out code left in
  • No real API keys or secrets in the diff
  • If prose was added or changed: checked against docs/VOCABULARY.md

Notes for reviewers

Targets dev.

nik13 added 3 commits December 1, 2026 01:40
…interpreter errors out of span status

An InvokeAgent completion stream that the caller does not iterate left its
AGENT span open. A FinalizationRegistry, matching the model stream path,
ends it best-effort on GC as UNSET with fi.stream.consumed=false. The
holdings carry only span state, and finalize disarms the registration.

Code-interpreter execution errors set status ERROR with
error.type=CodeInterpreterExecutionError and no status message; the error
text stays in output.value, where the hide settings apply.
Move failureTrace failureReason text into output.value on agent_failure
spans so hideOutputs applies, leaving span status as ERROR without a
message and keeping failureCode in error.type.
…C, and update changelog

Format finite numeric failureCode values in error.type on agent_failure
spans, close open step spans before ending the AGENT span on GC (adding
fi.stream.partial=true when events were read), add stream error and
real-GC tests, and update CHANGELOG.md.

This branch has not been deployed

No deployments
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