Skip to content

fix(tangle): truncate oversized tool output instead of discarding a paid turn - #312

Open
drewstone wants to merge 1 commit into
mainfrom
fix/tool-output-truncate-not-throw
Open

fix(tangle): truncate oversized tool output instead of discarding a paid turn#312
drewstone wants to merge 1 commit into
mainfrom
fix/tool-output-truncate-not-throw

Conversation

@drewstone

Copy link
Copy Markdown
Contributor

Follow-up to #311. That PR removed the instance; this one removes the class.

#311 left a 4× mismatch

It widened tool output from the 16 KiB metadata bound to the 1 MiB content bound. But the producer still serializes each tool value up to 4 MiB, and the consumer bound is 1 MiB across the whole record, not per value. Measured against the shipped 1.1.9:

0.9 MiB x1                    ACCEPTED
2 MiB x1 (producer allows 4)  REJECTED — Tangle prompt result exceeded its JSON bound
0.6 MiB x2 = 1.2 MiB total    REJECTED — Tangle prompt result exceeded its JSON bound

256× became 4×, and the multi-value case is worse than the single-value case.

The mismatch was never the real defect

This validator runs inside the terminal result read — after the live stream has drained and after the usage receipt has been credited. A throw here cannot prevent the work or the charge. It can only destroy a finished, fully paid turn, which a supervisor then reports as a child that did nothing at all.

That failure mode cost one Discovery Lab 143 of 199 children across 16 pursuits, and its six-stage sourcing graph blocked in 24 of 24 invocations.

Any bound that throws on this path is one large page away from doing it again. Widening it is not a fix; it is a bigger margin before the same outcome.

Change

Oversized tool output is replaced, not refused. Each pass cuts the widest toolInvocations[].result by the record's measured overflow and leaves a marker naming both byte counts:

[truncated by the Tangle provider: kept 1046528 of 4194304 bytes to stay inside the 1048576-byte record content bound]

The caller still receives the turn, its response text, its usage, and every tool call it made. What it loses is the tail of an oversized result, marked where it was cut, so a reader can tell truncation from a tool that genuinely returned little and can choose to re-fetch.

Only tool output is truncatable. Every other field is identity, accounting, or control material where a silently shortened value would be worse than a refusal — so a record whose overflow is elsewhere still refuses, as does one breaking the node, depth, or array limits, which trimming a string cannot satisfy.

Tests

One added, fails on 1.1.9. It pins: a 2 MiB single result survives; the producer's own 4 MiB per-value maximum survives; several results that each fit but together do not converge rather than the first being destroyed; the marker carries both byte counts; every tool call is still present; and an oversized response still throws.

Provider suite 264 pass, typecheck clean. 1.1.9 → 1.1.10.

🤖 Generated with Claude Code

…aid turn

1.1.9 widened the bound on tool output from the 16 KiB metadata limit to the
1 MiB content limit. That removed the instance and left the class: the producer
still serializes each tool value up to 4 MiB, and the consumer bound is 1 MiB
across the WHOLE record, so a single 2 MiB fetch still died, and so did two
0.6 MiB fetches together. A 256x mismatch became a 4x one.

The mismatch is not the real defect. This validator runs inside the terminal
result read, after the live stream has drained and after the usage receipt has
been credited, so a throw here cannot prevent the work or the charge. It can
only destroy a finished, fully paid turn, which a supervisor then reports as a
child that did nothing at all. Any bound that throws on this path is one large
page away from doing that again.

So oversized tool output is now replaced rather than refused. Each pass cuts the
widest `toolInvocations[].result` by the record's measured overflow and leaves a
marker naming both byte counts, so a reader can tell a truncated result from a
tool that genuinely returned little. The caller still receives the turn, its
response text, its usage, and every tool call it made.

Only tool output is truncatable. Every other field is identity, accounting, or
control material where a silently shortened value would be worse than a refusal,
so a record whose overflow is elsewhere still refuses — as does one that breaks
the node, depth, or array limits, which trimming a string cannot satisfy.

Tests: one added, failing on 1.1.9. It pins a 2 MiB single result, the
producer's own 4 MiB per-value maximum, several results that each fit but
together do not, the marker's byte counts, that every tool call survives, and
that an oversized `response` still refuses.

Provider suite 264 pass, typecheck clean.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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