Skip to content

Conversation

@Zena-park
Copy link

@Zena-park Zena-park commented Jan 16, 2026

Fixes #8089, #8293, #6286, #6068
Closes #8140

What does this PR do?

Problem

Auto-compaction was not triggering before context limits were exceeded, causing context_length_exceeded API errors. Users had no control over when compaction triggered or ability to limit context usage for cost optimization.

Solution

#8140 - Configurable context limit and auto-compaction threshold

  • Add compaction.threshold config option (0.5-0.99, default 0.9)
  • Add compaction.maxContext config option to override model's context limit
  • Implement shouldCompact() function in compaction.ts with threshold logic

#8089, #8293, #6286, #6068 - Prevent context_length_exceeded errors

  • Pre-check in prompt.ts: estimate tokens before API call, trigger compaction if threshold exceeded
  • Post-check in processor.ts: monitor context after tool execution
  • Model-adaptive tool output limits in truncation.ts (5% of context, min 10KB, max 2MB)
  • Update bash.ts, tool.ts, registry.ts to use dynamic limits based on model size

Config Example

{
  "compaction": {
    "threshold": 0.8,
    "maxContext": 100000
  }
}

How did you verify your code works?

  • ✅ 756 tests passing (+8 from baseline)
  • ✅ Added 22 new tests (truncation: 14, compaction: 8)
  • ✅ Type check passing
  • ✅ Verified pre-check triggers compaction before API calls
  • ✅ Verified post-check monitors context after tool execution

…esholds

## Issues Addressed

  ### anomalyco#8140 - Configurable context limit and auto-compaction threshold
  Fixed by:
  - Add `compaction.threshold` config option in config.ts (0.5-0.99, default 0.9)
  - Add `compaction.maxContext` config option in config.ts to override model's context limit
  - Implement `shouldCompact()` function in compaction.ts with threshold logic

  ### anomalyco#8089 - Auto-compaction not preventing context_length_exceeded errors
  Fixed by:
  - Pre-check in prompt.ts: estimate tokens before API call, trigger compaction if threshold exceeded
  - Post-check in processor.ts: monitor context after tool execution
  - Add `getMaxBytes()` and `getMaxMetadata()` in truncation.ts for model-adaptive tool output limits
  - Update bash.ts, tool.ts, registry.ts to use dynamic limits based on model size

  ## Config Example

  ```json
  {
    "compaction": {
      "threshold": 0.8,
      "maxContext": 100000
    }
  }

 ## Test Plan

  - 756 tests passing (+8 from baseline)
  - Added 22 new tests (truncation: 14, compaction: 8)
  - Type check passing
@github-actions
Copy link
Contributor

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

No duplicate PRs found

Zena-park and others added 2 commits January 19, 2026 10:49
Resolved merge conflicts in:
- packages/opencode/src/session/prompt.ts
- packages/opencode/src/tool/registry.ts

Changes:
- Adopted new ToolRegistry.tools() signature from upstream
- Preserved context overflow functionality by passing fullModel parameter
- Integrated ApplyPatchTool from upstream

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant