[PR #8810] feat(opencode): add context overflow prevention with configurable thresholds #12878

Open
opened 2026-02-16 18:17:46 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/8810

State: open
Merged: No


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
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/8810 **State:** open **Merged:** No --- 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 ```json { "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
yindo added the pull-request label 2026-02-16 18:17:46 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12878