Files
oh-my-opencode/src/hooks/anthropic-context-window-limit-recovery/AGENTS.md
T
YeonGyu-Kim 9f6d0d2281 docs(agents-md): refresh hierarchical knowledge base for v4.1.2
- Bump root AGENTS.md header: 2026-05-14 → 2026-05-15, commit 5ffbe0e2453a740636, release v4.1.1 → v4.1.2
- Update file counts: 2034 (1337+697) → 2041 (1340+701), LOC ~292k → ~294k
- Fix STRUCTURE: openclaw lives at src/openclaw/ (not src/features/); list more accurate feature modules in the parenthetical
- Clarify interactive_bash gate: tmux binary on PATH via isInteractiveBashEnabled() (not 'tmux enabled')
- Fix docs/reference/features.md hook counts: Tool Guard 14→16, Total base 52→54, total with team-mode 59→61
- Bump 'Generated' date on all 43 subdir AGENTS.md files to 2026-05-15
- Preserve promptAsync injection cautions verbatim (per request)
2026-05-15 13:48:11 +09:00

2.1 KiB
Raw Blame History

src/hooks/anthropic-context-window-limit-recovery/ — Multi-Strategy Context Recovery

Generated: 2026-05-15

OVERVIEW

31 files (~2232 LOC). Most complex hook. Recovers from context window limit errors via multiple strategies applied in sequence.

RECOVERY STRATEGIES (in priority order)

Strategy File Mechanism
Empty content recovery empty-content-recovery.ts Handle empty/null content blocks in messages
Deduplication deduplication-recovery.ts Remove duplicate tool results from context
Target-token truncation target-token-truncation.ts Truncate largest tool outputs to fit target ratio
Aggressive truncation aggressive-truncation-strategy.ts Last-resort truncation with minimal output preservation
Summarize retry summarize-retry-strategy.ts Compaction + summarization then retry

KEY FILES

File Purpose
recovery-hook.ts Main hook entry — session.error handler, strategy orchestration
executor.ts Execute recovery strategies in sequence
parser.ts Parse Anthropic token limit error messages
state.ts AutoCompactState — per-session retry/truncation tracking
types.ts ParsedTokenLimitError, RetryState, TruncateState, config constants
storage.ts Persist tool results for later truncation
tool-result-storage.ts Store/retrieve individual tool call results
message-builder.ts Build retry messages after recovery

RETRY CONFIG

  • Max attempts: 2
  • Initial delay: 2s, backoff ×2, max 30s
  • Max truncation attempts: 20
  • Target token ratio: 0.5 (truncate to 50% of limit)
  • Chars per token estimate: 4

PRUNING SYSTEM

pruning-*.ts files handle intelligent output pruning:

  • pruning-deduplication.ts — Remove duplicate content across tool results
  • pruning-tool-output-truncation.ts — Truncate oversized tool outputs
  • pruning-types.ts — Pruning-specific type definitions

SDK VARIANTS

empty-content-recovery-sdk.ts and tool-result-storage-sdk.ts provide SDK-based implementations for OpenCode client interactions.