Preserve structured tool outputs during truncation#1500
Conversation
Read-time context truncation and row-size enforcement now compact tool outputs without blindly replacing structured objects with strings. This keeps tool-specific toModelOutput handlers on their expected replay contracts, while still falling back to compact markers when a large object or array cannot be bounded structurally. Harden Think's workspace read replay path so legacy raw-string outputs and unknown object shapes no longer crash or fall through to multimodal rehydration. Add regressions for truncated read replay, legacy raw-string replay, structured object truncation, and primitive-heavy row-size enforcement. Co-authored-by: Cursor <cursoragent@cursor.com>
🦋 Changeset detectedLatest commit: 92ca4f6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| return marker; | ||
| } | ||
|
|
||
| return [truncateString("", maxChars, originalLength)]; |
There was a problem hiding this comment.
🟡 compactArrayMarker fallback produces an empty string because truncateString("", ...) always returns ""
When the primary array marker string doesn't fit within maxChars, the fallback calls truncateString("", maxChars, originalLength). Because "".length (0) is always <= maxChars (for any non-negative value), truncateString returns "" immediately at tool-output-truncation.ts:151, bypassing the truncation suffix logic entirely. The result is [""] — an array containing an empty string with no truncation context.
This is reachable when an array is a nested child of a larger structure, where childMaxChars (tool-output-truncation.ts:198-203) assigns a budget of 80 chars. The primary marker string ("Array output omitted because...") JSON-serializes to ~105+ chars, exceeding 80, so the fallback triggers. Compare with compactObjectMarker (tool-output-truncation.ts:177-180) which has a proper minimal fallback with __truncated and __truncatedChars fields.
| return [truncateString("", maxChars, originalLength)]; | |
| return [truncateString(truncatedSuffix(originalLength), Math.max(0, maxChars - 4), originalLength)]; |
Was this helpful? React with 👍 or 👎 to provide feedback.
Use a structured marker when an array is too large to preserve within its budget, and fall back to a non-empty truncation suffix for extremely small budgets. This avoids returning arrays like [\"\"] for nested array outputs while keeping the row-size fallback bounded. Add a regression covering nested arrays with small child budgets. Co-authored-by: Cursor <cursoragent@cursor.com>
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
Summary
toModelOutputhandlers keep seeing compatible replay data.readreplay path so legacy raw-string outputs and unknown object shapes replay safely instead of stalling inference.Details
This fixes the failure mode from #1498 where
truncateOlderMessages()stringified oldertool-readoutputs, then Think'sread.toModelOutputattempted object membership checks on a string duringconvertToModelMessages().The new shared truncation helper keeps strings as strings, objects as objects, and arrays as arrays while recursively truncating large nested values. If an object or array remains too large because its size is spread across many keys or primitive values, it falls back to a compact truncation marker rather than returning an oversized row.
Test plan
npm run test:workers -w agents -- --run src/tests/experimental/memory/utils/compaction.test.tsnpm run test -w agentsnpm run test:workers -w @cloudflare/think -- src/tests/think-session.test.tsnpm run test -w @cloudflare/thinknpm run checkMade with Cursor