Skip to content

fix(core): fit compaction output limit to the request actually sent - #55613

Merged
rekram1-node merged 2 commits into
devfrom
compaction-max-tokens
Oct 15, 2026
Merged

rekram1-node merged 2 commits into
devfrom
compaction-max-tokens

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

No issue filed; reported in Slack with a repro script. Related: #54797 (same provider error shape on normal turns).

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Compaction picked the summary's output limit in prepare() assuming the request would be shrunk to budget: max_tokens = window − budget. deliver() then sends a bigger request when it can't shrink the conversation (e.g. a single exchange larger than the window), but kept that same output limit. Input + requested output then exceeded the window even by our own estimate, and providers that enforce input + max_tokens <= window reject it with context_length_exceeded. Shrink retries only reduce input, so compaction failed with "cannot be reduced further".

Wire capture from the repro (opencode/cat-jam-free, configured window 262,144, real 1,048,576):

max_tokens=26214 → 400 "request has 1029271 input tokens and asks for 26214 output tokens (1055485 tokens total)"

The fix lowers the output limit right before each summary request is sent, to the room that request leaves in the window, never below 1,024 and never above the existing limit (including a limit set by a plugin hook). Normal compactions are unchanged; only requests sent above the target get a smaller limit. This matches what pi does at its transport layer.

It does not fix token estimates that run low (#54797), or a single message too large for the real window.

How did you verify your code works?

  • New test: an unshrinkable 250k-token (estimated) message on a 200k window is sent with maxTokens: 1024 (20,000 before the fix).
  • bun run check passes; session-compaction, session-compact, session-runner, plugin/session-compact, session-model-request-hooks tests pass.
  • Ran the Slack repro script against this branch through a logging proxy: the compaction request now sends max_tokens=1024, is accepted, and both cases reply OK (2 clean runs).

Screenshots / recordings

N/A

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #54797

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No blocking problems. The output limit is only ever lowered, so normal compactions and limits set by hooks are untouched. Retries shrunk after a rejection get more room again, still no more than the original limit. I checked it by running the new test against the base: the summary request is sent with maxTokens: 20000 there and 1024 with this change. Compaction-related tests and typecheck pass in packages/core.

Public API is unchanged; the change is internal to compaction.

One concern that doesn't block the merge, noted inline: fitOutput repeats the logic of outputLimit in model-request.ts but leaves out its estimate margin.

Related: #54797

Comment thread packages/core/src/session/compaction.ts Outdated
@rekram1-node
rekram1-node merged commit ae8d59f into dev Oct 15, 2026
11 checks passed
@rekram1-node
rekram1-node deleted the compaction-max-tokens branch October 15, 2026 06:57
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