Skip to content

fix(runtime): size the output cap from the folded request transcript - #1797

Merged
vastsa merged 3 commits into
vastsa:mainfrom
spacker-343-forks:fix/output-cap-collapsed-system
Oct 22, 2026
Merged

vastsa merged 3 commits into
vastsa:mainfrom
spacker-343-forks:fix/output-cap-collapsed-system

Conversation

@spacker-343

@spacker-343 spacker-343 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Long sessions can end with EMPTY_MODEL_RESPONSE even though the context
ring still shows plenty of room and compaction never triggers. The request
actually goes out with max_output_tokens: 16, so a reasoning model is cut
off 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.
image

Root cause

Every instruction change appends a system-transcript snapshot. Unless the
bound model declares supportsMidConvoSystemMessages, which defaults to off for
openai-responses / openai-completions (Google and Bedrock always fold),
the adapter folds those snapshots into one leading message holding only the
current instructions and tools.

clampOutputToContext estimated the unfolded transcript, charging every
superseded snapshot again (sections + tool deltas). With a large context
section, 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 streamSimple clamp 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
resolveTranscript exactly as the adapter will (requestTranscript in
output-cap.ts), clamp against that, and hand the same folded transcript to
pi-ai so its streamSimple clamp agrees. Models that declare
mid-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 paragraph
  • docs/spec/06-delivery/04-e2e-test-plan.md — E2E-OUTPUT-CAP-716

Validation

  • New output-cap-request.test.ts: loopback Responses gateway, restored
    session with 20 large instruction updates, plus subagent path. Old code
    sends max_output_tokens: 16; fixed code sends the configured 8192 with
    one folded instruction message.
  • output-cap.test.ts: folded vs. mid-conversation unit cases.
  • pnpm --filter agent-runtime typecheck: pass.
  • Targeted agent-runtime tests (5 files, 402 tests): pass.
    Full agent-runtime suite: pass except 2 Windows EPERM symlink failures
    in native-pi-session.test.ts (environment, unrelated).
  • Task-candidate E2E: pnpm test:e2e on 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.

spacker-343 and others added 3 commits October 21, 2026 17:03
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.
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.

2 participants