Root cause: compaction only ran after successful assistant response, never before calling the LLM API. Internal runs skipped it entirely, and the fire-and-forget void swallowed errors with race conditions. Layer 1 - Pre-flight compaction in transformContext: Runs before EVERY LLM call (including internal runs). Prunes tool results and drops oldest messages when utilization exceeds 80%. Pure in-memory, no disk writes. Layer 2 - Fix post-hoc compaction: Store compaction promise instead of voiding it, await at _run() start to prevent race conditions. Emit compaction_start before the actual compaction. Remove useless catch-rethrow block. Layer 3 - Context overflow recovery: Catch context overflow 400 errors → auto-compact → retry (up to 2 attempts). Runs before classifyError/auth rotation so overflow errors are handled by compaction, not profile switching. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> |
||
|---|---|---|
| .. | ||
| src | ||
| package.json | ||
| tsconfig.json | ||
| tsup.config.ts | ||