Patch set for opencode that improves compaction (context window management) for long-running agentic sessions.
Base: opencode v1.1.65
Developed from xxfork, an earlier experimental fork where the core compaction algorithms were prototyped. This patch set is a clean rebase of those changes onto upstream opencode.
- Anchor-based compaction — three-segment split (prelude/interlude/temporal) instead of summarizing everything
- Watermark calibration — uses real API token counts to correct estimation drift
- Improved filterCompacted — marker-based matching prevents splice errors on repeated compaction
- Tool message sanitization + consecutive role merging for Anthropic API compatibility
- LLM debug dump, usage tracking, snapshot capture (instrumentation)
- Linux x86-64
- bun >= 1.3.8
- git
./build.shFrom a local clone (faster, no GitHub access needed):
./build.sh --local /path/to/opencodeClean rebuild:
./build.sh --cleanBinary output: build/opencode/packages/opencode/dist/opencode-linux-x64/bin/opencode
| # | Description |
|---|---|
| 0001 | B-class: flag env vars, watermark calibration, budget constraint, systemEstimate |
| 0002 | C-class: LLM debug dump, snapshot capture, maxOutputTokensOverride |
| 0003 | A-class: limit.input merge, OUTPUT_TOKEN_MAX 64K, anchor-based compaction, filterCompacted rewrite |
| 0004 | Fix: restore LLM dump enhancement after rebase |
| 0005 | Refactor: move watermark.json to .opencode/ephemeral/ |
| 0006 | Fix: calldump-role-violation only writes when COMPACTION_DEBUG_DIR is set |
| Variable | Description |
|---|---|
OPENCODE_COMPACTION_DEBUG_DIR |
Compaction debug output directory |
OPENCODE_LLM_DEBUG_DIR |
LLM request/response dump directory |
OPENCODE_USAGE_LOG |
Usage JSONL log path (default: .opencode/ephemeral/usage.jsonl) |
OPENCODE_SNAPSHOT_DIR |
Message snapshot output directory |
OPENCODE_SESSION_TOKEN_BUDGET |
Effective token budget per session |
OPENCODE_COMPACTION_RECALL_PROMPT |
Custom compaction recall prompt |
OPENCODE_COMPACTION_CONTINUE_PROMPT |
Custom compaction continue prompt |
conjure-demo/ is a self-contained project that uses the patched binary to analyze its own patches. It includes:
AGENT.md— instructions for the analysis taskconjure.sh— launcher script.opencode/plugins/conjure.ts— lightweight plugin (situational motif + compaction stats)/bootstrapcommand to start the analysis
./build.sh
cd conjure-demo
./conjure.sh
# then type: /bootstrapThe conjure.ts plugin injects a <context> tag into each LLM call with current time, session stats, and context usage percentage. After compaction, it logs the context reduction details.
Tested with opencode zen mode + Kimi (free tier). See conjure-demo/test/ for sample output.
Same as upstream opencode (MIT).