Skip to content

fix(session): escalate exhausted context reduction to compaction - #835

Open
agenteinstinct1-max wants to merge 10 commits into
dev-vibecodedfrom
fix/exhausted-context-compaction-830
Open

agenteinstinct1-max wants to merge 10 commits into
dev-vibecodedfrom
fix/exhausted-context-compaction-830

Conversation

@agenteinstinct1-max

@agenteinstinct1-max agenteinstinct1-max commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Issue for this PR

Closes #841. Refs #830, which remains open. #841 tracks the scoped exhausted escalation, bounded re-entry and automatic blank-summary history defects. It does not replace or shrink #830's broader acceptance.

Type of change

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

What does this PR do?

Pass the prepared exhausted reduction outcome to core compaction and schedule compaction before ordinary v1 dispatch when automatic compaction is enabled. Bound no-progress re-entry so it reaches dispatch or terminal failure. Publish completion only for a nonblank persisted summary and log reason-specific failures.

For a blank automatic summary, persist summary: false before continuation so a normally finished assistant message cannot hide original history through filterCompacted(). Use honest failure continuation text, including the distinction between media placeholders in a replay copy and original attachments that may remain in history. Preserve nonblank behavior and the existing error-result path.

V4 reviewed candidate: d247e74, tree ef389d40696268675683e9ba55e2233ac34b7074, parent f90b548, integrating base 32c33f4. Candidate objects and all three V4 post-image blobs were independently checked against the unmodified author patch. Publication and exact-head checks are tracked separately; these local identity checks are not a CI result.

The same independent Arena reviewer reviewed all eight changed files and returned CLEAN for scoped V4, resolving the recursion, blank-summary and unfinished-test-fake blockers. The reviewer verified the attached patches, not the supplied commit bundle. Kilo's overflow-budget-reset warning remains bounded and nonblocking: a provider step can permit a threshold summary, an overflow summary and a post-overflow threshold summary. This is not a strict once-per-entire-prompt summary budget. Review: https://arena.ai/agent/01a1c817-7dc7-77ed-8953-3ac7fb8a614a .

No timeout, assertion, protection, workflow or CI gate is relaxed.

How did you verify your code works?

On the unmodified V4 candidate, full opencode compaction and context-reduction files: 62 pass, 1 existing disabled-v2-projector skip, 210 expectations. Related real-prompt subset: 3 pass. The shared fake now persists whitespace or summary text and a normal finish, so the overflow test checks actual completion eligibility and original user text/image retention.

Causal controls are separate from candidate results. Removing only summary: false fails first at the direct summary-flag assertion (line 995, true instead of false). A separately labelled diagnostic that also removes only that first assertion reaches the original user text/image retention failure at line 1011. Removing only the flag fix in the real prompt regression also loses protected original history. These controls do not count as passing candidate tests.

Published exact head: d247e74, tree ef389d40696268675683e9ba55e2233ac34b7074, on base 32c33f4. Normal work-branch fast-forward only; no direct integration-branch push.

Current exact-head status checked November 11 at 05:16 BRT: typecheck and both standards jobs pass. Both Linux and Windows unit CI are RED. Linux failed in packages/core/test/lib/effect.test.ts:57: the cancelled-effect test captured the abandoned-finalizer marker but did not find finalizer boom in the captured stderr line. The preceding thrown-error/message assertions passed. Full core result: 1547 pass, 8 skip, 1 fail, 4403 expectations. The failure ended the Turbo run, so the full opencode prompt/TUI suites, generated-client check and HttpAPI gates are not qualified by that run. Linux job: https://github.com/mdev34-lab/silvercode/actions/runs/42980679036/job/130390102729 . Windows job: https://github.com/mdev34-lab/silvercode/actions/runs/42980679036/job/130390103215 .

Windows completed with a different failure: workspace sync state > sync history sends the local sequence fence and replays returned events in workspace context in packages/opencode/test/control-plane/workspace.test.ts:1388, interrupted after 22959.16ms. The owned-deadline.ts:333 stack frame rethrows the interruption; it does not identify its origin. Opencode finished with 5074 pass, 61 skip, 1 todo, 1 fail, 21894 expectations across 346 files (1738.23s). Windows core passed 1530 tests with 27 skips; TUI passed 938 tests with 9 skips. These passing packages do not clear either failed OS gate. The Windows job did not hit its 40-minute limit.

Bounded source inspection found no explicit owned-deadline timer for this test, but did not identify the interrupted step, fiber or interruptor or reproduce the Windows sequence. In-body interruption is a hypothesis, not a proven exclusion of wider host or harness involvement. No shared cause with the separate #836 scrollback paint failure is established.

Separate local exact-head core runs passed (plain: 1553 pass, 3 skip; CI environment: 1547 pass, 9 skip). They did not reproduce the remote stderr failure and do not clear it. A bounded scratch capture also did not reproduce it; exact CI test order and the failed captured stderr contents remain unavailable. No cause or fix is claimed. The local full prompt run hit its cap without a result. No blind rerun, assertion relaxation or merge exemption is requested. Required exact-head full-suite and CI gates remain open.

#830 still needs focused core failure-branch coverage (no head, oversized summary prompt, provider failure, empty summary), real replay behavior, stop/error behavioral tests, the exact v1 auto=false exhausted-trigger test and manual blank-compaction handling. Source inspection and existing disabled-overflow coverage do not replace these tests. This PR must not close #830.

Screenshots / recordings

Not applicable: no UI change.

Checklist

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

@github-actions

Copy link
Copy Markdown

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

Comment thread packages/opencode/src/session/prompt.ts Outdated
.pipe(Effect.ignore)
// Reduction stops at protected content. When it exhausted its eligible steps the
// prepared request is still over target, so escalate to durable compaction before
// dispatching it. This is a threshold compaction and must follow compaction.auto.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

WARNING: Possible non-progress loop when exhausted compaction is attempted but does not succeed

This branch escalates to compaction.create(...) and immediately return "continue" whenever reduction.report.outcome === "exhausted" and auto-compaction is enabled, but it does not check whether compaction actually completed or changed the request/history. If compaction is skipped/fails (e.g. no summarizable head, summary too large, provider failure) while the reduction remains exhausted, the loop may re-enter this same path repeatedly with the same prepared state.

Consider handling a non-successful compaction result explicitly (e.g. proceed with the reduced request, surface the failure, or add a guard to avoid retrying the same exhausted escalation indefinitely).

@kilo-code-bot

kilo-code-bot Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (4 files)
  • packages/opencode/src/session/compaction.ts
  • packages/opencode/test/session/compaction.test.ts
  • packages/opencode/test/session/prompt.test.ts
  • packages/tui/test/cli/tui/diff-viewer.test.tsx
Previous Review Summaries (5 snapshots, latest commit 0718d82)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 0718d82)

Status: 1 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
packages/core/src/session/runner/llm.ts 477 compactions once-per-turn bound bypassed on overflow recovery; compactIfNeeded can run twice per turn
Files Reviewed (4 files)
  • packages/core/src/session/runner/llm.ts - 1 issue
  • packages/core/test/session-runner.test.ts - No issues
  • packages/tui/src/routes/session/index.tsx - No issues
  • packages/tui/test/cli/tui/activity-padding.test.tsx - No issues

Fix these issues in Kilo Cloud

Previous review (commit 4f50095)

Status: 6 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 4
SUGGESTION 2
Issue Details (click to expand)

WARNING

File Line Issue
packages/tui/src/context/local.tsx 915 Memory leak: attempts Map never cleaned up
packages/tui/src/context/local.tsx 959 Failed push permanently blocks re-send for same selection
packages/opencode/src/session/processor.ts 461 loadPriorSteps only catches InvalidCursorError, other errors crash processor
packages/opencode/src/session/processor.ts 580 Potential race: ctx.toolcalls[value.id]?.partID may be undefined

SUGGESTION

File Line Issue
packages/opencode/src/session/processor.ts 581 doomLoopAskedSteps uses step index which resets per message
packages/opencode/src/session/compaction.ts 559 Summary extraction assumes text parts exist; non-text compaction skipped silently
Files Reviewed (8 files)
  • packages/opencode/src/session/compaction.ts - 1 issue (inline comment posted)
  • packages/opencode/src/session/processor.ts - 3 issues (not in PR diff; changes from merged base branch)
  • packages/tui/src/context/local.tsx - 2 issues (not in PR diff; changes from merged base branch)
  • packages/opencode/test/session/compaction.test.ts - No issues
  • packages/opencode/test/session/processor-effect.test.ts - Not reviewed (test file)
  • packages/opencode/test/session/prompt.test.ts - No issues
  • packages/tui/test/cli/tui/dialog-agent-effort.test.tsx - Not reviewed (test file)
  • packages/tui/test/context/local.test.ts - Not reviewed (test file)

Note: Three issues are in files (packages/opencode/src/session/processor.ts and packages/tui/src/context/local.tsx) that were modified by merge commits bringing in base branch changes. These files do not appear in the PR diff (base to head) because the changes were already in the base branch. Inline comments could not be posted for them; they are documented here for the author's attention.

Fix these issues in Kilo Cloud

Previous review (commit cc8ebad)

This review did not finish. The model reached its output limit before it
could write the review — a reasoning model can spend the whole budget thinking.
Re-run the review, or lower the model's thinking effort, and it should get
further. Any inline comments below are from an earlier review.

Previous review (commit cc8ebad)

Status: No Issues Found | Recommendation: Merge

Overview

Severity Count
CRITICAL 0
WARNING 0
SUGGESTION 0

Notes

The previous WARNING finding ("Exhausted-reduction escalation can re-enter indefinitely if compaction.create(...) fails or makes no progress") is resolved: this PR adds MAX_EXHAUSTED_COMPACTIONS = 1 and an exhaustedCompactions counter so the exhausted escalation runs at most once per prompt turn, after which the loop logs a warning and dispatches without further compaction.

Files Reviewed (2 files)
  • packages/opencode/src/session/prompt.ts - 0 issues
  • packages/opencode/test/session/prompt.test.ts - 0 issues

Previous review (commit 5726716)

Status: 1 Issue Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 1
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
packages/opencode/src/session/prompt.ts 1816 Exhausted-reduction escalation can re-enter indefinitely if compaction.create(...) fails or makes no progress
Files Reviewed (6 files)
  • packages/core/src/session/compaction.ts - no issues found
  • packages/core/src/session/context-reduction.ts - no issues found
  • packages/core/src/session/runner/llm.ts - no issues found
  • packages/core/test/session-runner.test.ts - no issues found
  • packages/opencode/src/session/prompt.ts - 1 issue
  • packages/opencode/test/session/prompt.test.ts - no issues found

Fix these issues in Kilo Cloud


Reviewed by free · Input: 118.4K · Output: 26.1K · Cached: 247.2K

const summary = (yield* MessageV2.parts(processor.message.id).pipe(
Effect.provideService(Database.Service, database),
))
.filter((part): part is SessionV1.TextPart => part.type === "text")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: Summary extraction assumes text parts exist; non-text compaction skipped silently

The summary extraction (lines 556-563) filters for type === "text" parts. If compaction produces only tool calls or other non-text parts (e.g., a compaction that only runs tools), summary will be empty and the Compacted event is skipped with a warning. This may be intentional, but consider whether a compaction with only tool activity should still publish an event (perhaps with empty summary) for consistency.

@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

if (!(defect instanceof TurnTransitionError)) return yield* Effect.die(defect)
yield* Effect.yieldNow
if (defect.transition._tag === "ContinueAfterOverflowCompaction")
return yield* runAfterOverflowCompaction(sessionID, undefined, defect.transition.step)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

WARNING: compactions once-per-turn bound is bypassed on overflow recovery

runAfterOverflowCompaction accepts a compactions counter (llm.ts:456) that gates compactIfNeeded to compactions === 0, enforcing "escalate at most once per turn". This call site omits compactions, so it defaults to 0: after an overflow compaction already ran via compactAfterOverflow (llm.ts:386), runTurnAttempt re-enters with compactions === 0 and compactIfNeeded can compact a second time in the same turn, over-summarizing history and contradicting the stated once-per-turn guarantee.

Propagate the counter here, e.g. pass compactions + 1 since an overflow compaction just occurred.

Suggested change
return yield* runAfterOverflowCompaction(sessionID, undefined, defect.transition.step)
return yield* runAfterOverflowCompaction(sessionID, undefined, defect.transition.step, compactions + 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant