Skip to content

feat(opencode): add double-buffer context management#15130

Open
marklubin wants to merge 2 commits intoanomalyco:devfrom
marklubin:feat/double-buffer-context
Open

feat(opencode): add double-buffer context management#15130
marklubin wants to merge 2 commits intoanomalyco:devfrom
marklubin:feat/double-buffer-context

Conversation

@marklubin
Copy link

Issue for this PR

Refs #8140 #11314 #2945 #4317 #12479

Type of change

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

What does this PR do?

Implements double-buffer context management — a two-phase compaction strategy that produces higher-quality summaries by running summarization early (at ~50% capacity) while the model still has full attention, rather than at the capacity cliff.

How it works:

  1. Checkpoint phase (~50%): A background compaction generates a summary and stores it in per-session state. The user's workflow is uninterrupted.
  2. Concurrent phase (50-75%): Normal operation continues. The pre-computed checkpoint summary is ready.
  3. Swap phase (~75%): Instead of stop-the-world compaction, uses the pre-computed summary. Falls back to standard compaction if no checkpoint is available.

Changes:

Community asks addressed:

Existing single-pass compaction remains as the fallback when no checkpoint is available.

Algorithm description: https://marklubin.me/posts/hopping-context-windows/

How did you verify your code works?

  • Unit tests for state machine transitions (double-buffer.test.ts)
  • All 109 session tests pass
  • Full test suite: 1157/1158 pass (1 pre-existing unrelated failure)

Checklist

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

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

Found related PRs! These are related to the compaction and context management work but address different aspects:

Related PRs (Not Duplicates):

  1. PR fix(opencode): skip overflow check on compaction summaries #15129 - fix(opencode): skip overflow check on compaction summaries

  2. PR feat(compaction): configurable threshold and model (global + per-model) #11931 - feat(compaction): configurable threshold and model (global + per-model)

    • Related to configurable compaction thresholds mentioned as one of the community asks
  3. PR feat(session): add custom compaction thresholds along with prevention of continuous compaction #10123 - feat(session): add custom compaction thresholds along with prevention of continuous compaction

  4. PR fix(opencode): prevent context overflow during compaction #11453 - fix(opencode): prevent context overflow during compaction

    • Related to overflow prevention during compaction
  5. PR fix: recover from 413 Request Entity Too Large via auto-compaction #14707 - fix: recover from 413 Request Entity Too Large via auto-compaction

    • Related to automatic compaction strategies
  6. PR feat(opencode): add context overflow prevention with configurable thresholds #8810 - feat(opencode): add context overflow prevention with configurable thresholds

    • Addresses similar threshold configuration needs

These are all related work in the compaction/context management space but none are duplicates of PR #15130. This PR is the first to implement the specific "double-buffer" two-phase compaction strategy with checkpoint phases.

Implements a two-phase compaction strategy that checkpoints at ~50%
capacity and swaps at ~75%, producing higher-quality summaries by
summarizing while the model still has full attention rather than at
the capacity cliff.

Changes:
- New `double-buffer.ts`: state machine tracking normal → checkpoint →
  concurrent → swap phases per session
- `compaction.ts`: background checkpoint (fire-and-forget), swap check,
  and summary retrieval exports
- `processor.ts`: skip overflow on compaction summaries (fixes anomalyco#13946)
- `prompt.ts`: swap check before standard overflow, background checkpoint
  trigger after successful processing
- `config.ts`: configurable `checkpointThreshold` and `swapThreshold`
  in compaction schema
- Unit tests for state machine transitions

The double-buffer is additive — existing single-pass compaction remains
as the fallback when no checkpoint is available.

Algorithm: https://marklubin.me/posts/hopping-context-windows/

Refs anomalyco#8140 anomalyco#11314 anomalyco#2945 anomalyco#4317 anomalyco#12479
Replace `require("./double-buffer")` with `await import("./double-buffer")`
in getCheckpointSummary and completeSwap to avoid issues with top-level
await in transitive dependencies. Update callers in prompt.ts accordingly.
@marklubin marklubin force-pushed the feat/double-buffer-context branch from dda8503 to d181d83 Compare February 26, 2026 00:41
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.

Bug: opencode run exits after compaction when compaction model's token usage exceeds overflow threshold

1 participant