π The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
Checkpoint: File edits
rUv9a71156eb6f9
diff --git a/v3/implementation/adrs/ADR-051-infinite-context-compaction-bridge.md b/v3/implementation/adrs/ADR-051-infinite-context-compaction-bridge.mdindex f002b1632d..f5a806fc79 100644--- a/v3/implementation/adrs/ADR-051-infinite-context-compaction-bridge.md+++ b/v3/implementation/adrs/ADR-051-infinite-context-compaction-bridge.md@@ -80,28 +80,92 @@ bridge retrieves and injects the most relevant archived context.8. **Aggressive Pruning**: SDK patch truncates old conversation text automaticallyon every query, keeping context lean so compaction rarely or never fires-## SDK Compaction Mechanics (Discovered via Deep Review)--The Claude Code SDK (`cli.js`) processes PreCompact hooks with three exit code behaviors:--| Exit Code | SDK Behavior |-|-----------|-------------|-| **0** | stdout is appended as **custom compact instructions** (guides preservation) |-| **2** | **Blocks compaction entirely** (hook can prevent compaction) |-| Other | stderr shown to user, compaction continues normally |--The `_H0` function (line 4700) executes all PreCompact hooks, collecting-`newCustomInstructions` from exit code 0 hooks. The `NJ1` function (line 1769)-performs actual compaction, using these instructions to guide the summary.--This enables our **Smart Compaction Gate**:-- **Default mode**: Exit code 0 with custom instructions listing archived files,- tools, decisions, and recent turns -- Claude's compaction summary preserves- the most important details-- **Block mode** (Context Autopilot, enabled by default): Exit code 2 on auto-trigger- to prevent compaction; manual `/compact` is allowed and resets autopilot state-- **Legacy block mode** (`CLAUDE_FLOW_BLOCK_COMPACTION=true`): Exit code 2 on auto-trigger- via environment variable (superseded by Context Autopilot)+## SDK Compaction Mechanics (Decompiled from cli.js v2.0.76)++### Full Compaction Pipeline++The Claude Code SDK has two compaction mechanisms, decompiled from `cli.js`:++```+Every query (ew function):+β+ββ Vd() β MICRO-COMPACT (runs every query, invisible to user)+β β Targets: Read, Bash, Grep, Glob, WebSearch, WebFetch, Edit, Write+β β Keeps last 3 tool results intact (Ly5=3)+β β Replaces older results with "[Old tool result content cleared]"+β β Only activates when: tokens > warningThreshold AND savings >= 20K+β β Hardcoded thresholds: Ny5=40000, qy5=20000, Ly5=3+β β DOES NOT prune text content (user/assistant messages)+β β+β ββ OUR PATCH: _aggressiveTextPrune() inserted after Vd()+β Truncates old text blocks to 80 chars + "[earlier context pruned]"+β Keeps last 4 turns intact, starts at 20K tokens+β Configurable via: CLAUDE_TEXT_PRUNE_KEEP, _THRESHOLD, _MAX_CHARS+β+ββ CT2() β AUTO-COMPACT (only when above threshold)+β β Gate 1: DISABLE_COMPACT env β skip entirely+β β Gate 2: autoCompactEnabled setting β skip if false+β β Gate 3: Sy5() β skip if tokens < threshold+β β Threshold: zT2() = min(maxTokens Γ PCT_OVERRIDE/100, maxTokens - 13000)+β β Default: 93% of context window (~187K tokens)+β β Override: CLAUDE_AUTOCOMPACT_PCT_OVERRIDE env var+β β+β β First tries TJ1() β session-memory compact (no LLM, instant)+β β Falls back to NJ1() β full LLM compaction (slow, "Compacting..." UI)+β ββ NJ1 calls _H0 (executePreCompactHooks) before compacting+β+ββ NO OTHER PRUNING MECHANISM EXISTS in Claude Code+```++### Key SDK Functions (Decompiled)++```javascript+// Hd() β Is auto-compact enabled?+function Hd() {+ if (process.env.DISABLE_COMPACT) return false;+ return localSettings.autoCompactEnabled; // default: true+}++// zT2() β Auto-compact threshold (tokens)+function zT2() {+ let max = effectiveMaxTokens(); // ~200K+ let threshold = max - 13000; // ~187K (93%)+ let override = process.env.CLAUDE_AUTOCOMPACT_PCT_OVERRIDE;+ if (override) {+ let pct = parseFloat(override);+ if (pct > 0 && pct <= 100)+ threshold = Math.min(Math.floor(max * pct / 100), threshold);+ }+ return threshold;+}++// T9A() β Context health check+function T9A(tokens) {+ let threshold = zT2();+ let ref = Hd() ? threshold : effectiveMaxTokens();+ return {+ isAboveWarningThreshold: tokens >= ref - 20000, // micro-compact activates+ isAboveErrorThreshold: tokens >= ref - 20000,+ isAboveAutoCompactThreshold: Hd() && tokens >= threshold // full compact fires+ };+}+```++### PreCompact Exit Code 2: NOT IMPLEMENTED++**Critical finding**: The SDK documentation (line 4140) states "Exit code 2 - block+compaction" for PreCompact hooks. However, this is **not implemented** in v2.0.76.++- `_H0` (executePreCompactHooks) uses `executeHooksOutsideREPL` (`NM0`)+- `NM0` returns `{command, succeeded: status===0, output}` β no blocking field+- Exit code 2 is treated as a failed hook (succeeded: false), not a blocking signal+- Compare: REPL-based hooks (PreToolUse, Stop) DO handle exit code 2 via the+ streaming executor (`ms`) which yields `{blockingError, outcome: "blocking"}`+- The `NJ1` compaction function ALWAYS proceeds after collecting hook outputs++**Consequence**: Compaction cannot be blocked via hooks. Our system uses+archive+restore (lossless compaction) and SDK patching (aggressive pruning)+instead of blocking.## Architecture