Skip to content

fix(session): escalate exhausted context reduction to automatic compaction #830

Open

Description

@agenteinstinct1-max

Problem

With automatic compaction enabled, deterministic context reduction can exhaust its eligible content without triggering durable compaction. The runner then continues toward the ordinary provider request. Exhaustion currently records a warning rather than determining escalation.

This is a source-level behavior gap, not a claim that a provider hard limit was exceeded or that a runtime regression has been reproduced.

Current mechanism

At c54ff0ef5a1ba02f84bd96cd1e7c5454f4ef18ba:

  • context-reduction.ts:80-84,180-203 defaults to a target of 0.8 of the usable window. Once the eligible reduction steps are exhausted, a request still above that target produces outcome: "exhausted". Protected recent content stays protected.
  • runner/llm.ts:290-305 calls compactIfNeeded before publishing the context report. It does not pass the exhausted outcome to that check. A false result leads to logging exhaustion and continuing toward the ordinary request.
  • compaction.ts:233-243 independently checks whether the estimated lowered request exceeds context - max(output, buffer). With auto enabled, an exhausted reduction can therefore coexist with a skipped compaction.
  • compaction.ts:179-222 also returns false for no summarizable head, an oversized summary prompt, a failed summary stream, or an empty summary. The caller cannot distinguish these cases from a request that needed no compaction.

The reducer and compactor measure different serialized representations. A regression test must capture both estimates instead of assuming that one displayed counter proves the compaction condition.

Related history

#320 already recorded that the exhausted outcome only logs while the contract called for escalation. #341 aligned the documentation with separate, config-driven compaction; it did not implement exhausted-to-compaction behavior. This follow-up concerns behavior with automatic compaction enabled, not another label or documentation-only change. Explicit compaction.auto=false must remain respected.

Acceptance

  • Through the real runner path, cover auto=true with protected or irreducible content above the reduction target but below the current independent compaction gate. Exhaustion should trigger a durable compaction attempt rather than only a warning.
  • Preserve auto=false. Do not override disabled automatic compaction.
  • Preserve protected content and durable summary/continuation semantics. Do not solve this by dropping protected recent turns, raising a limit, weakening protection, or changing only UI text.
  • Cover no summarizable head, summary prompt too large, provider failure, and empty summary. Make the decision or failure observable, avoid unbounded compaction loops, and do not report compaction as completed when no summary was saved.
  • The existing runner test escalates to compaction instead of reducing protected content crosses the higher compaction gate. Add coverage for the exhausted-only interval rather than treating that existing test as proof of this path.

Sources

Additional execution path: opencode v1 loop

The issue must cover the opencode session loop used by the current HTTP API and TUI, not only the core v2 runner. A core-only escalation change would not establish the behavior of this path.

Source at c54ff0e:

  • server/routes/instance/httpapi/handlers/session.ts:54,353-377 resolves SessionPrompt.Service and forwards normal/async prompts to it.
  • session/context-reduction.ts:93-103 maps v1 history into the core reduction model and returns its report and projected history. It does not perform compaction.
  • session/prompt.ts:1794-1828 reduces the current request, publishes SessionEvent.Context.Prepared, lowers the result and calls the processor. The reduction report's exhausted outcome does not schedule compaction or block dispatch here.
  • session/prompt.ts:1633-1639 checks overflow using lastFinished.tokens, not the newly prepared reduction report.
  • session/processor.ts:552-595 receives provider usage at step-finish and sets needsCompaction using that usage. processor.ts:727-736 also handles a provider context-overflow error, honoring disabled auto compaction. The result is handled by prompt.ts:1868-1882 to schedule the next compaction task.
  • session/overflow.ts:28-34 uses the provider token total (or input+output+cache fields), respects auto=false and compares against its own usable-window calculation. That is a separate quantity and timing from the prepared-request estimate displayed by the TUI.
  • tui/context/sync.tsx:391-395 stores the prepared report, and the shared context-usage helper renders its tokens and utilization. The report is the most recent published preparation measurement, not a live provider tokenizer or proof of which compaction branch ran.

Consequences established by source:

  1. In this path a current reduction estimate above its reported usable window can still proceed while prior provider usage remains below the compaction check. The explanation is not limited to a gap between an 80% reduction target and a 100% compaction target.
  2. Disabled auto compaction, stale or different provider usage, model selection/limits and request representation are distinct conditions that regression coverage must discriminate.
  3. Existing v1 test loop stops provider overflow instead of auto-compacting when disabled covers auto=false and must remain valid. Add a v1 integration regression for auto=true, exhausted prepared context and dispatch/escalation order; a core v2 runner test alone is insufficient.
  4. Verify whether a compaction task was never scheduled, scheduled but failed, or completed and then immediately re-exhausted. Do not infer this from a single rendered frame.

Sources:
https://github.com/mdev34-lab/silvercode/blob/c54ff0ef5a1ba02f84bd96cd1e7c5454f4ef18ba/packages/opencode/src/session/prompt.ts
https://github.com/mdev34-lab/silvercode/blob/c54ff0ef5a1ba02f84bd96cd1e7c5454f4ef18ba/packages/opencode/src/session/processor.ts
https://github.com/mdev34-lab/silvercode/blob/c54ff0ef5a1ba02f84bd96cd1e7c5454f4ef18ba/packages/opencode/src/session/context-reduction.ts
https://github.com/mdev34-lab/silvercode/blob/c54ff0ef5a1ba02f84bd96cd1e7c5454f4ef18ba/packages/opencode/src/session/overflow.ts
https://github.com/mdev34-lab/silvercode/blob/c54ff0ef5a1ba02f84bd96cd1e7c5454f4ef18ba/packages/opencode/src/server/routes/instance/httpapi/handlers/session.ts

Activity

  1. added
    priority: high
    on Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions