Repository navigation
fix(runtime): size the output cap from the folded request transcript - #1797
Merged
vastsa merged 3 commits intoOct 22, 2026
Merged
Conversation
Long sessions accumulate one system-transcript snapshot per instruction change. Unless a model declares mid-conversation system support, every adapter folds them into a single leading message, but the output-cap estimate charged each superseded snapshot again. The clamp collapsed to 1 and the provider floor (16) went on the wire while the request was far from the window, so reasoning models were cut off and the turn ended as EMPTY_MODEL_RESPONSE without compaction ever triggering. The parent and subagent stream paths now fold the transcript the way the adapter will before clamping, and hand that same transcript to pi-ai so its own streamSimple clamp agrees. Context calibration and the compaction budget keep their existing basis.
Long sessions retain superseded system snapshots even though adapters send only the current instructions. Measuring the folded request keeps the configured output budget from collapsing to the provider floor, while model-declared mid-conversation support preserves its existing transcript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Long sessions can end with

EMPTY_MODEL_RESPONSEeven though the contextring still shows plenty of room and compaction never triggers. The request
actually goes out with
max_output_tokens: 16, so a reasoning model is cutoff while still thinking, the silent-turn re-run is cut off the same way,
and the turn fails.
Screenshot of the failure.
The model responds normally, but its output is truncated after 16 tokens, so the app wrongly reports an empty response — while 57% of the context window is still free.
Root cause
Every instruction change appends a system-transcript snapshot. Unless the
bound model declares
supportsMidConvoSystemMessages, which defaults to off foropenai-responses/openai-completions(Google and Bedrock always fold),the adapter folds those snapshots into one leading message holding only the
current instructions and tools.
clampOutputToContextestimated the unfolded transcript, charging everysuperseded snapshot again (sections + tool deltas). With a large
contextsection, a few dozen snapshots are enough to push the estimate past the
window: the clamp returns 1 and the Responses floor (16) goes on the wire.
pi-ai's own
streamSimpleclamp sees the same unfolded context.contextBudget()excludes system messages, so compaction does not trigger,and the condition persists across "continue" and branches because the
snapshots stay in the transcript.
Introduced with chronological model system state (d8fd08d). #1477 / #1499
deduplicated identical rows; distinct snapshots remain (a real session had
26 rows / 9 distinct payloads).
Fix
The parent and subagent stream paths fold the transcript with
resolveTranscriptexactly as the adapter will (requestTranscriptinoutput-cap.ts), clamp against that, and hand the same folded transcript topi-ai so its
streamSimpleclamp agrees. Models that declaremid-conversation system support keep the unfolded transcript (unchanged).
Context calibration, the compaction budget, and request headers keep their
existing basis. The provider minimum (16) and recovery paths are untouched.
Affected specs / E2E
docs/spec/03-runtime/02-agent-runtime.md— output-cap paragraphdocs/spec/06-delivery/04-e2e-test-plan.md—E2E-OUTPUT-CAP-716Validation
output-cap-request.test.ts: loopback Responses gateway, restoredsession with 20 large instruction updates, plus subagent path. Old code
sends
max_output_tokens: 16; fixed code sends the configured 8192 withone folded instruction message.
output-cap.test.ts: folded vs. mid-conversation unit cases.pnpm --filter agent-runtime typecheck: pass.Full agent-runtime suite: pass except 2 Windows
EPERM symlinkfailuresin
native-pi-session.test.ts(environment, unrelated).pnpm test:e2eon c82490c (base 8f4285f),Windows, local debug host-core: 23/23 passed, 2 skipped
(E2E-008/009 need a live API key, not run to avoid paid calls).
Risk
Request payloads are unchanged for adapters that already fold. Only the
computed output budget changes, upward and toward the configured value.