multica/packages/core
yushen 9c52dc0124 fix(agent): 3-layer defense against context window overflow
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>
2026-02-12 14:52:19 +08:00
..
src fix(agent): 3-layer defense against context window overflow 2026-02-12 14:52:19 +08:00
package.json refactor: restructure to monorepo architecture 2026-02-10 18:00:23 +08:00
tsconfig.json refactor: restructure to monorepo architecture 2026-02-10 18:00:23 +08:00
tsup.config.ts refactor: restructure to monorepo architecture 2026-02-10 18:00:23 +08:00