Skip to content

Recover a failed or stalled trace output and count the spans it drops - #655

Open
sabasiddique1 wants to merge 7 commits into
GenAI-Security-Project:integrationfrom
sabasiddique1:refimpl/otel-output-breaker-282
Open

sabasiddique1 wants to merge 7 commits into
GenAI-Security-Project:integrationfrom
sabasiddique1:refimpl/otel-output-breaker-282

Conversation

@sabasiddique1

@sabasiddique1 sabasiddique1 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What changed

A trace output that failed once stayed off until the Guardian restarted, a hung write was never marked as failed, and dropped spans printed nothing an operator sees. isolated() now keeps a breaker for each output: a failed, thrown or unanswered export opens it, and batches are dropped and counted while it is open. The next batch after a backoff (1 s, doubling to 60 s) tests the output again, and the Guardian prints each output's dropped count on stderr. One span is limited to 32 KiB of text, so a stalled output cannot grow memory without bound.

Scope

Item by item against #282:

  1. Breaker in place of dead. Done. The output is never tested during shutdown or forceFlush, and every timer is unref'd.
  2. One write in flight. Done, with one limit. An output with an export unanswered past the timeout is handed nothing more. A flush or shutdown that starts before that first timeout still hands over what is queued, up to four more batches, because the processor starts them together. The file output has one append in flight either way, since it chains its writes. Strict one-at-a-time needs a waiting list in the wrapper.
  3. Replay buffer (optional). Left out, to keep the change small. A dropped span is not sent later, and the READMEs say so.
  4. Dropped count per output. Done. It counts spans ended minus spans delivered, and that includes spans the processor's full queue would drop silently. It is printed at the first drop, again if a full queue caused that drop and a write or send then fails or times out, at most once a minute while drops go on, when the output recovers, and at shutdown if that output dropped any.
  5. Bytes per span. Done: SPAN_BYTE_LIMIT is 32 KiB of text for one span, and only request values are cut to fit. The ids, the decision and any failure event stay whole, and arrays keep their length, so the policy and provenance columns still line up. If those alone pass 32 KiB, the span goes over, with every request value empty.
  6. READMEs and tests. Done, in both READMEs. Four older tests now assert the shutdown count line, and one of them is renamed. Two tests check that the Guardian README quotes the four outage lines and the shutdown line. One sentence under docs/spec/ changes too, see Specification changes.

The issue's acceptance tests are in packages/guardian/test/otel-trace-exporter.test.ts: the hung output (with the limit in item 2), recovery and no duplicates, run on both Bun versions. The hung-output test keeps memory growth under 256 MB over 1500 largest-size requests. It uses ASCII, as the issue's 0.5 MB span implies.

Three things change for an operator. For a failed output the stderr line is now failed: <reason>. Dropping spans until it recovers, where it was disabled after failure. A failed shutdown of one output prints could not shut down: <reason>. and drops nothing. onError is now called for each failure that starts an outage or opens a breaker, and for each failed shutdown, where it was called at most once for each output.

This conflicts with my PR #656 for #283 in otel-trace-exporter.ts, the Guardian README and the exporter test file. DanPazlev suggested this one lands first, and I'll rebase #656 on it once it merges.

Credit

The scope and the acceptance tests are rocklambros's, from #282. The findings behind them came from the review of PR #244 by rocklambros and bar-capsule, collected in #274.

Questions for the reviewer

@artmaro, two questions:

  1. This is now 402 changed source lines, against the guide of about 400. The review fixes added 13, most of them comment lines. The byte limit, item 5, is about 90 of those lines and can still go in its own PR. Do you want that?
  2. Do you want strict one write at a time for item 2? It needs a waiting list in the wrapper.

Which issue does this implement

Closes #282
Refs #274

  • This is an editorial correction (typo, grammar, link, formatting) with no change in
    meaning, so it needs no issue

Base branch

  • integration, because this touches the specification, schemas, a reference
    implementation, an adapter, tests, or CI
  • main, because every changed path is on the documentation lane allowlist in
    CONTRIBUTING.md

Type of change

  • Specification change (schema, hooks, events, AgBOM)
  • Reference implementation or adapter
  • Documentation
  • Tooling or CI
  • Governance (licensing, security policy, contributor docs)

Specification changes

  • I opened a Discussion before this PR
  • Schema changes validate against the JSON Schema spec
  • I described the impact on downstream implementers below

No normative text changes. One sentence in docs/spec/trace/extend_opentelemetry.md changes, where it describes what the AGT Guardian does when an output fails.

Breaking for implementers? No.

I tested this

  • I synced my branch with the base branch before opening this
  • uv run pytest -v passes on my machine
  • uv run mkdocs build --strict passes on my machine

How I tested

From reference-implementations/agt, on Bun 1.3.11 and again on Bun 1.3.14:

bun run typecheck
bun test
  • Typecheck is clean.
  • Bun 1.3.11: 1582 pass, 4 skip, 0 fail.
  • Bun 1.3.14: 1585 pass, 1 skip, 0 fail.
  • The exporter test file, which holds the timer tests, passed three runs in a row on each version.

From the repo root, uv run pytest -v gives 9376 passed, 7 skipped and 1 xfailed, and uv run mkdocs build --strict passes. I broke the behavior each new test covers, and each went red. I ran this on macOS only.

The Go recording is not changed here. As rocklambros asked, the branch keeps the integration copy of ts-cases.json, and a lead records it again after the Guardian pull requests merge. Until then TestRecordingIsCurrent fails in agt-go, because the packages/guardian/src tree id changed. go test ./... passes.

Checklist

  • Commits are signed off with git commit -s (required by the DCO)
  • Prose follows STYLE.md
  • No secrets, tokens, or internal URLs in the diff

Security

  • This change has no security impact

It bounds the memory a stalled trace output can hold. A Guardian that runs out of memory fails open under the default posture.

@github-actions

Copy link
Copy Markdown

This pull request's description says it closes #282, which is on the project roadmap. When it merges, GitHub closes that issue and the roadmap counts it as delivered. Please confirm the change delivers the whole issue before merging.

@rocklambros rocklambros left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Saba, thank you for this. I reviewed commit e784196.

The breaker works well. A second reviewer ran a real Guardian with one output that never answers. The decisions and the latency were the same as with export off, and the file output still got every span. Seventeen injected defects each failed a test, and the exporter tests passed 97 of 97 six times under heavy load.

Two things are needed before merge.

  1. This pull request conflicts with integration in reference-implementations/agt-go/agtbridge/testdata/ts-cases.json. #597 changed that file. A lead records that file again after the Guardian pull requests merge, so please keep the integration copy:

    1. git fetch upstream (upstream is GenAI-Security-Project/agent-control-standard)
    2. git rebase upstream/integration
    3. git checkout upstream/integration -- reference-implementations/agt-go/agtbridge/testdata/ts-cases.json
    4. git add reference-implementations/agt-go/agtbridge/testdata/ts-cases.json and git rebase --continue
    5. git push --force-with-lease
  2. The span limit can cut a value that the Guardian writes, and nothing marks the cut. In packages/guardian/src/otel-trace-exporter.ts:816, the decision attributes (attributes) are in the second argument of fitSpanBytes(), so they share the cut with the request's step attributes. The session id and request id come from the request and are never cut. So a request with long ids makes acs.chain_hash shorter. I added this test next to "holds a span from a largest-size request" and ran it:

    exporter.end(
      exporter.start(largestRequest("é".repeat(4096), "\u{1F600}".repeat(2048),
        { sessionId: "€".repeat(4096), requestId: "€".repeat(4096) }), "steps/toolCallRequest", TRUSTED),
      decisionResponse(1, { ...DENY, chain_hash: "a".repeat(64) }),
    );

    acs.chain_hash on the decision event has 63 characters, and the response sent 64. A trace reader then sees a wrong chain hash.

    Fix: replace line 816 with these two lines, so only request values are cut:

    const decided = attributes;
    const [held = {}] = fitSpanBytes([stepSpan.claimed, failure ?? {}, decided], [stepSpan.held]);

    Please add the test above with expect(event?.attributes?.["acs.chain_hash"]).toBe("a".repeat(64)). I tried this fix: bun run typecheck exits 0, and the exporter tests give 98 passed, 0 failed, with the new test included.

What I ran, on integration with this pull request merged in (I took the integration copy of ts-cases.json), from reference-implementations/agt, on Bun 1.4.4:

  1. bun run typecheck. It exits 0.
  2. bun test. Result: 1391 passed, 1 skipped, 0 failed.
  3. bun test packages/guardian/test/otel-trace-exporter.test.ts three times. Each run: 97 passed, 0 failed.
  4. The test above.

What I did not check: the Go suite, Windows, and a real OTLP collector.

Three follow-ups. They can go in this pull request or a later one:

  1. When the queue fills first, the real cause is never printed. The queue-full drop sets failing = true without opening the breaker (otel-trace-exporter.ts:526). A later timeout then prints nothing, so the operator sees "queue is full" and not "no answer within 3000 ms".
  2. A healthy output whose shutdown() or forceFlush() rejects prints "Dropping spans until it recovers, 0 dropped so far" while the Guardian closes (otel-trace-exporter.ts:500 and :507). Please print a separate line for this case.
  3. STYLE.md allows four sentences in a paragraph. The changed paragraph at reference-implementations/agt/README.md:299 has six, and two new paragraphs in packages/guardian/README.md have five.

When you push, I will review the new commit.

@DanPazlev

Copy link
Copy Markdown
Collaborator

Similar to that of the 656 pull, where I think I see the same ts-cases.json conflict against integration, are we able to rebase it onto integration? This also conflicts with #656 in the trace exporter source aspect, its test, and the README as well I think.

I suggest this lands first because it adds the dropped-span count, which #656's changes must preserve. Once this is in, #656 rebases on it.

@acs-board-reconciler acs-board-reconciler Bot moved this from Needs triage to In progress in ACS Project Tracker Dec 3, 2026
Refs GenAI-Security-Project#282

Signed-off-by: Saba Siddique <sabasiddiqdev@gmail.com>
Refs GenAI-Security-Project#282

Signed-off-by: Saba Siddique <sabasiddiqdev@gmail.com>
Refs GenAI-Security-Project#282

Signed-off-by: Saba Siddique <sabasiddiqdev@gmail.com>
Refs GenAI-Security-Project#282

Signed-off-by: Saba Siddique <sabasiddiqdev@gmail.com>
Refs GenAI-Security-Project#282

Signed-off-by: Saba Siddique <sabasiddiqdev@gmail.com>
@sabasiddique1
sabasiddique1 force-pushed the refimpl/otel-output-breaker-282 branch from e784196 to c1b842d Compare December 4, 2026 19:24
@sabasiddique1
sabasiddique1 requested a review from e2hln as a code owner December 4, 2026 19:24
@sabasiddique1

Copy link
Copy Markdown
Contributor Author

Rebased on integration and kept its ts-cases.json as you asked, so agt-go goes red until a lead re-records it. 6320d64 is your chain-hash fix and test, with one change: decision values are still clipped to 4096 characters first. Without that, a 100 000-character acs.reasoning empties every request value, which the second new test shows. Your three follow-ups are in 3e2d261, e943413 and 028efe9, and c1b842d corrects the README on which output a flush or shutdown skips.

Two things I left out. There's no test for the flush line, because the SDK never calls the exporter's forceFlush. And a span whose ids, decision and failure event alone pass 32 KiB still goes over, with every request value empty (the README says so now). Do you want a hard bound for that case?

@DanPazlev agreed, this one lands first. I'll rebase #656 on it after.

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

Status: In progress

Development

Successfully merging this pull request may close these issues.

[RefImpl] Recover a failed or stalled trace output and count the spans it drops

3 participants