opencode icon indicating copy to clipboard operation
opencode copied to clipboard

feat(opencode): add context overflow prevention with configurable thresholds

Open Zena-park opened this issue 5 days ago • 1 comments

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

Zena-park avatar Jan 16 '26 05:01 Zena-park