11 improvements across 4 priority tiers, all integrated into production code paths with 416 new tests across 33 test files. Each wave passed a brutal honesty audit with all findings resolved. Wave 0-2 (P0/P1): Middleware chain, microcompact, tool concurrency, retry engine, session durability, prompt cache latch, startup fast paths, hook security hardening. Wave 3 (P2): 4-tier context compaction pipeline, plugin architecture with local/GitHub/npm sources. Wave 4 (P3): QE Quality Daemon — priority queue, git watcher (with Linux polling fallback), coverage delta analyzer, CI monitor, test suggester, nightly consolidation, notification service with SSRF guard, persistent SQLite-backed memory, CLI commands, MCP startup integration. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
23 KiB
Claude Code Internals Research & AQE Platform Analysis
Date: 2026-04-01 Source: https://github.com/stuinfla/claude-code-internals (reverse-engineered docs) Analysis Method: Six Thinking Hats Platform Version: AQE v3.8.14
Table of Contents
- Claude Code Architecture Overview
- Core Architecture & Tools
- Agent Intelligence & Interface
- Interface & Infrastructure
- Connectivity & Plugins
- Unreleased Features
- AQE Platform Current State
- Six Thinking Hats Analysis
- Improvement Recommendations
1. Claude Code Architecture Overview
Claude Code is a TypeScript application built on Bun, React/Ink (terminal UI), and the Anthropic API. Internal codename: "tengu". Organized into six architectural layers:
- Boot -- Startup, settings, migrations, session wiring
- UI Shell -- Ink-rendered terminal interface (custom React reconciler targeting terminal DOM)
- State -- Immutable
AppState(React) plus global singleton state - Query Engine -- Conversation lifecycle, system prompt assembly, API streaming
- Tools -- Capability registry (30+ built-in tools plus MCP)
- Services -- Anthropic API client, MCP connections, context compaction
2. Core Architecture & Tools
2.1 Boot Sequence
Three-Layer Entrypoint:
- Layer 1: CLI Entrypoint (
cli.tsx) -- Zero-cost fast paths.--versionreturns without loading any modules - Layer 2: Main Function (
main.tsx) -- Commander argument parsing, initialization, migrations (version 11) - Layer 3: Setup + REPL (
setup.ts+replLauncher.tsx) -- 16+ ordered steps, then Ink rendering
Parallel Prefetch Side-Effects (during ~135ms module evaluation):
startMdmRawRead()-- MDM policy subprocesses viaplutil/reg query(20-40ms)startKeychainPrefetch()-- macOS keychain reads (~65ms saved)preconnectAnthropicApi()-- TCP+TLS warm-up (~150ms saved)startDeferredPrefetches()-- user/git context, tips, model capabilities
Cold Start Timeline:
t=0ms $ claude
t=1ms profileCheckpoint, MDM/keychain fire
t=136ms Module eval complete
t=140ms Commander.parse()
t=160ms preconnectAnthropicApi()
t=165ms captureHooksConfigSnapshot()
t=190ms FIRST RENDER
t=191ms startDeferredPrefetches()
8 Fast Paths (detection order, gated by feature flags):
--version-- zero imports--dump-system-prompt-- minimal modules- Chrome/Computer-Use MCP -- standalone servers
--daemon-worker-- only worker registry- Bridge/Remote Control
daemonsubcommand- Background sessions -- no interactive UI
- Fallthrough to full CLI
Bare Mode (--bare / CLAUDE_CODE_SIMPLE): Strips all non-essential startup for CI/SDK.
2.2 Query Engine
Four-Layer Architecture:
QueryEngine.submitMessage()-- Validates prompts, constructs system messaging, delegatesquery()->queryLoop()-- Async generator loop until tool-calling ceasesqueryModel/callModel-- Anthropic API via streaming, wrapped inwithRetry()- Stop hooks & token budget -- Post-turn hook execution and continuation decision
7 Continuation Reasons:
| Reason | Meaning |
|---|---|
max_output_tokens_escalate |
First 8k cap hit; retry at 64k |
max_output_tokens_recovery |
Model hit output limit; inject recovery nudge (up to 3x) |
reactive_compact_retry |
Prompt-too-long -> compact -> retry |
collapse_drain_retry |
Prompt-too-long -> drain stages -> retry |
stop_hook_blocking |
Stop hook returned blocking error; re-query |
token_budget_continuation |
Budget indicates more work needed |
| (tool follow-up) | Model returned tool_use blocks |
Token Budget Auto-Continue:
const COMPLETION_THRESHOLD = 0.9 // 90% used = done
const DIMINISHING_THRESHOLD = 500 // <500 new tokens = no progress
Streaming and Retry:
StreamingToolExecutorfires tools while stream remains open (parallel execution)- Tombstone messages handle mid-stream fallback
withRetry()-- Up to 10 retries with exponential backoff (base * 2^attempt, max 32s, +25% jitter)- 529 overloaded: Only foreground retries; background bails
- After 3 consecutive 529s, throws
FallbackTriggeredErrorswitching to fallback model - OAuth 401: Token refresh before retry
- Context overflow 400: Parses token counts, computes new
maxTokensOverride - Persistent mode: Retries indefinitely with 30-min cap, heartbeat every 30s
2.3 State Management
Architecture: 35 Lines, No Dependencies:
- Custom
createStore<T>withgetState,setState(updater),subscribe AppState(400+ fields,DeepImmutable<{...}>)- React integration via
useSyncExternalStore
Two-Layer State:
- Layer 1: Global Singleton (~60 fields: sessionId, cwd, token counters, FPS, hook registry)
- Layer 2: React State (90+ fields: settings, models, tool permissions, tasks, MCP, plugins)
Prompt Cache Latch Fields:
afkModeHeaderLatched, fastModeHeaderLatched keep API headers stable to avoid busting 50-70K token prompt cache.
2.4 Context Management Pipeline (Priority Order)
applyToolResultBudget()-- Caps individual tool result byte sizesnipCompact(HISTORY_SNIP) -- Removes provably unneeded messages without summarizationmicrocompact-- Merges consecutive tool-result/user pairs into condensed summariescontextCollapse(CONTEXT_COLLAPSE) -- Read-time projection over REPL historyautoCompact-- Full summarization via forked agent
2.5 4-Tier Context Compaction System
Tier 1 - Microcompact (zero API calls):
- Clears cached tool-result content from in-memory message arrays
- Keeps last 5 results, replaces old with
'[Old tool result content cleared]' - Time-based: 60-min threshold
- Token estimation: padded heuristic
ceil(chars / 3)(~3 chars/token vs standard 4 chars/token); images = flat 2,000 tokens
Tier 2 - Session Memory Compact (no API calls):
- Uses continuously-updated session memory files
- Min 10,000 tokens recent context, max 40,000 tokens
- Preserves tool_use/tool_result pairs
Tier 3 - Full LLM Compact (one API call):
- Forks sub-agent for structured 9-section summary:
- Primary Request and Intent
- Key Technical Concepts
- Files and Code Sections
- Errors and Fixes
- Problem Solving
- All User Messages (verbatim -- captures intent drift)
- Pending Tasks
- Current Work
- Optional Next Step
- 20k token reservation (p99.99 = 17,387 tokens)
Tier 4 - Reactive Compact:
- Triggered by 413 prompt-too-long errors
- Peels API rounds from oldest entries
5 Operational States:
| State | Remaining Tokens | Behavior |
|---|---|---|
| Normal | >20k | No action |
| Warning | <=20k | Yellow indicator |
| Error | <=20k | Red indicator |
| Auto-Compact | <=13k | Auto-trigger compaction |
| Blocking | <=3k | Prevents new input |
Post-compact file restoration: Up to 5 previously-read files (50k token budget, 5k per file) plus skills (25k budget, 5k per skill).
2.6 Tool System
Tool Interface:
type Tool<Input, Output, P> = {
name: string
aliases?: string[]
inputSchema: ZodSchema
maxResultSizeChars: number
call(args, context, canUseTool, parentMessage, onProgress?): Promise<ToolResult<Output>>
checkPermissions(input, context): Promise<PermissionResult>
isConcurrencySafe(input): boolean // Default: false (conservative)
isReadOnly(input): boolean
isDestructive?(input): boolean
}
Tool Concurrency Partitioning:
- Consecutive tools with
isConcurrencySafe === truebatch for parallel execution - Non-safe tools break batches and run alone
- Max concurrency:
CLAUDE_CODE_MAX_TOOL_USE_CONCURRENCY(default 10)
StreamingToolExecutor:
- States:
queued->executing->completed->yielded - Tools execute concurrently but emit results in model's requested order
- Sibling abort: Only Bash errors cascade; Read/WebFetch independent
2.7 Bash Tool Security
23 Security Validators (each returns allow, passthrough, or ask):
- Incomplete commands, jq system functions, obfuscated flags
- Shell metacharacters (
;,&&,||,|) - Dangerous variables (
$BASH_ENV,$ENV,$IFS) - Process substitution, I/O redirects, brace expansion
- Control characters, unicode whitespace
- zsh-specific dangerous commands (
zmodload,sysopen,ztcp) - 50-subcommand cap defaults to
ask
2.8 MCP System
8 Transport Types:
- Public:
stdio,sse,http,ws(with mTLS) - IDE:
sse-ide,ws-ide - Internal:
sdk,in-process
7-Level Configuration Cascade:
- Enterprise (
managed-mcp.json) - Dynamic (CLI flag)
- Claude.ai connector API
- Project (
.mcp.json) - Local
- User (
~/.claude/settings.json) - Managed/plugin
8-Phase Connection Lifecycle: Config -> batched connection (stdio: 3 concurrent, remote: 20) -> transport -> connect (30s timeout) -> auth handling -> capability negotiation -> tool/resource/prompt fetch -> live notifications (exponential backoff reconnect, 1s -> 30s, max 5)
3. Agent Intelligence & Interface
3.1 Skills System
6-Stage Lifecycle: Discovery -> Load -> Parse -> Substitute -> Execute -> Inject
4 Skill Sources (Priority):
- Managed/Policy (Enterprise, lockable)
- User (
~/.claude/skills/) - Project (
.claude/skills/) - Bundled (compiled into CLI)
Execution Modes:
- Inline (default): Prompt injected in same context
- Forked (
context: fork): Isolated sub-agent viarunAgent()
3.2 Agent System
3 Agent Type Definitions:
BuiltInAgentDefinition: Dynamic system prompts, cannot be overriddenCustomAgentDefinition: From.claude/agents/*.mdor settingsPluginAgentDefinition: Admin-trusted for MCP policy
Sync vs Async Agents:
- Sync: Build prompt -> optional worktree ->
await runAgent()-> return - Async: Register with own AbortController -> return
async_launched-> notify on completion
Fork Path (Experimental):
- Clone parent's full assistant message
- Build byte-identical placeholder blocks
- Enable prompt cache sharing across N parallel children
- 60-70% cost reduction for parallel agent spawning
3.3 Coordinator Mode
Activated via CLAUDE_CODE_COORDINATOR_MODE=1. Only 4 tools: Agent, TaskStop, SendMessage, SyntheticOutput.
4-Phase Workflow:
- Research (Workers, parallel) -- Investigate codebase
- Synthesis (Coordinator) -- Transform findings into specifics
- Implementation (Workers) -- Make changes, test, commit
- Verification (Workers) -- Prove code works independently
3.4 Teams & Swarm
File-based Architecture:
~/.claude/teams/<team>/config.json
~/.claude/teams/<team>/permissions/
~/.claude/tasks/<team>/0001.json
3 Spawn Backends:
- tmux -- Socket:
claude-swarm-${process.pid} - iterm2 -- Via
it2CLI - in-process -- Non-interactive, isolated via
AsyncLocalStorage
Mailbox Messaging: File-based at ~/.claude/teams/<team>/inbox/<agent-name>/
Message types: plain text, shutdown_request, idle notification, permission_request/response, mode_set_request
3.5 Memory System
3 Layers:
- Auto Memory -- Persistent facts at
~/.claude/projects/<slug>/memory/ - Session Memory -- In-session notes at
~/.claude/session-memory/<uuid>.md - Team Memory -- Server-synced via API, scoped to GitHub repo
Auto Memory Extraction Pipeline:
- Query ends
- Gate check (feature flag + cursor delta)
- Scan memdir (frontmatter headers only)
- Fork agent (shared cache, max 5 turns, skip transcript)
- Write + notify, advance cursor
AutoDream Consolidation (3 gates: time >= 24h, sessions >= 5, no lock): 4 phases: Orient, Gather, Consolidate, Prune and index
Recall - findRelevantMemories: Two-step: scan memory files -> Sonnet sideQuery (max 256 tokens, selects up to 5 relevant files)
4. Interface & Infrastructure
4.1 Permission System
7-Phase Decision Pipeline:
- Deny rule checks
- Ask rule evaluation
- Tool-specific validation
- User interaction requirements
- Safety-protected paths
- Mode-based transformations
- Auto mode classification
5 Permission Modes:
| Mode | Behavior |
|---|---|
| default | Ask user for tools not in allow-list |
| plan | Read-only blocking writes |
| acceptEdits | Auto-approves file mods in working directory |
| bypassPermissions | Skips prompts except deny/safety/interaction |
| auto | AI-driven routing (ANT-only) |
4.2 Settings Architecture
5-Layer Priority Cascade:
- userSettings (
~/.claude/settings.json) - projectSettings (
.claude/settings.json) - localSettings (
.claude/settings.local.json) - flagSettings (
--settings <path>) - policySettings (enterprise/MDM)
Change Detection: Chokidar with constants:
FILE_STABILITY_THRESHOLD_MS = 1000INTERNAL_WRITE_WINDOW_MS = 5000DELETION_GRACE_MS = 1700
4.3 Session Management
Append-only JSONL storage. Messages form linked list via parentUuid.
Write batching: 100ms local, 10ms cloud. Lazy session file materialization.
3 session states: idle, running, requires_action.
Resume: Head+tail reads (4KB head, 64KB tail) avoid parsing multi-GB files.
4.4 Analytics & Telemetry
Dual Pipeline:
- First-Party (OpenTelemetry): Detailed analytics with disk-backed retry
- Datadog: Operational monitoring (~40 events, 15s flush, 100-entry threshold)
Cardinality Reduction: Model names bucketed. User IDs hashed to 1-of-30 buckets via SHA-256.
5. Connectivity & Plugins
5.1 Plugin System
5 Conceptual Layers:
- Marketplace Sources (GitHub, git, npm, local, URL)
- Manifest Schema (
plugin.json) - Versioned Cache (immutable per-version snapshots)
- Dependency Resolution (DFS closure walk, fixed-point demote)
- Lifecycle (reconciliation -> autoupdate -> load -> registration)
27 Hook Lifecycle Events: Lifecycle(5), Tool execution(3), Agent/subagent(2), Compaction(2), Permission/policy(5), Collaborative(4), Filesystem(4), MCP elicitation(2)
Name Validation Security:
- Blocks impersonation patterns
- Reserved names require
anthropics/org - Non-ASCII blocked (homograph attack prevention)
5.2 Hooks System
5 Hook Command Types:
- command (shell subprocess) -- supports if, timeout, once, async, asyncRewake
- prompt (LLM prompt) --
$ARGUMENTS, 30s timeout - agent (agentic verifier) -- up to 50 turns, 60s timeout
- http (HTTP POST) -- SSRF guard, 10-min timeout
- function (TypeScript callback) -- in-process, 5s timeout
Exit Code Semantics: Exit 2 = model-visible blocking; other non-zero = user-visible only; exit 0 = silent success.
6 Configuration Sources: userSettings, projectSettings, localSettings, policySettings, pluginHook, sessionHook.
Security: Config frozen at startup. SSRF guard blocks private IPs. allowManagedHooksOnly and disableAllHooks policy controls.
5.3 Error Handling
4-Layer Architecture:
- Typed Error Classes
- API Retry Engine (10 retries, exponential backoff, 529 model fallback)
- Terminal Error Overlay
- Conversation Recovery (4 stages including interrupt detection)
Tool Error Formatting: Center-truncation at 10,000 chars (5k head + 5k tail).
5.4 OAuth Authentication
OAuth 2.0 Authorization Code + PKCE. Two targets: Console (API) and Claude.ai (subscribers). Profile skip optimization saves ~7M requests/day fleet-wide.
5.5 Git Integration
Filesystem-first design -- reads .git/ files directly via Node fs (no subprocess calls).
GitFileWatcher with dirty-bit pattern. Default branch cascade: origin/HEAD -> origin/main -> origin/master -> "main".
6. Unreleased Features
6.1 UltraPlan -- Remote Planning System
Status: Behind feature flags. Uses Opus 4.6 via GrowthBook.
4-Phase System (30-minute sessions):
- Trigger Detection -- Smart keyword scanning
- CCR Launch -- Cloud Code Runner session, git bundle upload
- Long-Poll -- Cursor-based pagination, 3s intervals, up to 30 minutes
- Plan Delivery -- Remote Execution (PR) or Teleport (back to terminal)
6.2 Kairos -- Always-On Autonomous Daemon
Status: Anthropic-internal only, behind 7 feature flags.
Key Capabilities:
- Memory switches from MEMORY.md to daily log files
- Tick Loop with
<tengu_tick>XML messages - Queue Priority: 'now', 'next', 'later'
- Tool Suite: SleepTool, SendUserMessage, PushNotification, SubscribePR, Cron
- AutoDream: Background memory consolidation, 8-step gate chain
- Kill-Switch: GrowthBook flags with 5-minute caching
6.3 Plugin Marketplace
5-layer system with GitHub/git/npm/local/URL sources, DFS dependency resolution, versioned immutable cache, background autoupdate, 27 lifecycle events.
6.4 Voice System
Push-to-talk pipeline: audio recording -> WebSocket STT -> transcript injection. 3 audio backends (audio-capture-napi, arecord, SoX). Hold-to-talk with silent-drop replay and focus mode.
6.5 Computer Use MCP (Chicago/Malort)
Gated by subscription. OS-level automation with coordinate modes, clipboard guards, mouse animation.
6.6 Agent SDK v2 (Alpha)
V1 (stable): query() for headless one-shot.
V2 (@alpha): createSession(), resumeSession(), prompt() for persistent multi-turn.
8 control protocol subtypes.
7. AQE Platform Current State
7.1 Metrics
| Metric | Value |
|---|---|
| Version | 3.8.14 |
| Source Lines | 548,703 across 1,195 files |
| Test Lines | 368,189 across 705 files |
| QE Agents | 60 specialized (qe-*.md) |
| Platform Agents | 116 non-QE |
| Skills | 187 across 125+ directories |
| Bounded Contexts | 13 DDD domains |
| MCP Tools | 200+ registered |
| Memory Backend | HNSW + SQLite hybrid |
| Learning Patterns | 1,000+ in memory.db |
| QCSD Feedback Loops | 5 cross-phase |
| Max Concurrent Agents | 15 |
7.2 13 Domain Bounded Contexts
| Domain | Purpose |
|---|---|
| test-generation | AI-powered test synthesis, mutation, property-based |
| test-execution | Parallel running, flaky detection, retry |
| coverage-analysis | O(log n) gap detection, sublinear algorithms, HNSW |
| quality-assessment | Quality gates, metrics, SLA enforcement |
| defect-intelligence | Risk prediction, root cause, pattern learning |
| requirements-validation | BDD scenarios, AC verification, HTSM v6.3 |
| code-intelligence | Complexity, dependency mapping, code indexing |
| security-compliance | SAST/DAST scanning, vulnerability classification |
| contract-testing | API contract validation, Pact integration |
| visual-accessibility | A11y auditing, axe-core |
| chaos-resilience | Fault injection, resilience testing |
| learning-optimization | Experience capture, pattern promotion, meta-learning |
| enterprise-integration | SAP, middleware, message brokers, OData/SOAP |
7.3 Architecture Strengths
- Domain-Driven Design with clear bounded contexts
- HNSW vector search + SQLite persistence (hybrid backend)
- Pattern lifecycle management (candidate -> promoted -> adopted)
- QCSD cross-phase feedback loops (5 loops)
- Economic routing model (quality-per-dollar)
- ONNX transformer models for real embeddings
- Dream cycles for pattern consolidation
- 3-tier model routing (WASM Booster < Haiku < Sonnet/Opus)
8. Six Thinking Hats Analysis
White Hat -- Facts & Data
See sections 7.1-7.3 for AQE metrics and sections 2-6 for Claude Code metrics.
Red Hat -- Gut Feelings
- Pride: 13-domain DDD architecture with 200+ MCP tools is unique
- Anxiety: Context compaction is primitive compared to CC's 4-tier system
- Excitement: Plugin marketplace + hooks could transform AQE into ecosystem
- Concern: Boot/startup never optimized (2-5x worse than needed)
- Frustration: No retry engine -- MCP failures cascade
- Confidence: Learning system is MORE sophisticated than CC's auto-memory
- Unease: Agent coordination feels fragile vs CC's battle-tested teams/swarm
Black Hat -- Risks & Critical Gaps
- No Context Compaction -- Long sessions fail without graceful degradation
- No Retry Engine -- Transient failures cascade across swarm
- No Sandbox/Permission System -- Agents can execute arbitrary commands
- No Streaming Tool Execution -- Latency penalty on multi-tool operations
- No Session Resume -- Long analyses lost on crash
- Startup Not Optimized -- Poor DX for frequent CLI use
- No Hook Security Model -- Hooks not frozen at startup
- Single MCP Transport -- Cannot integrate with IDEs
Yellow Hat -- Where AQE is Ahead
- Domain-Driven QE Architecture (13 bounded contexts)
- Learning System Sophistication (pattern lifecycle, dream cycles, ONNX)
- QCSD Cross-Phase Feedback Loops (5 declarative loops)
- Economic Routing Model (quality-per-dollar)
- Real Embeddings + HNSW (vs CC's Sonnet sideQuery)
Green Hat -- Creative Ideas
Quick Wins:
- Microcompact for MCP tool results (40% token savings)
- Tool concurrency partitioning (2-3x throughput)
- Transcript-first durability (crash recovery)
Medium-Term:
- 4-tier context compaction for QE agents
- Plugin architecture for QE domains
- Hook security hardening
Ambitious:
- QE Quality Daemon (always-on monitoring)
- UltraPlan for QE (remote test strategy)
- Voice-driven exploratory testing
- Agent fork with prompt cache sharing
Blue Hat -- Action Plan
| Priority | Improvement | Effort | Impact |
|---|---|---|---|
| P0 | Microcompact for MCP tools | 1-2 days | 40% token savings |
| P0 | Tool concurrency partitioning | 1-2 days | 2-3x throughput |
| P0 | Retry engine with backoff | 3-5 days | Resilience |
| P1 | Transcript-first durability | 3-5 days | Crash recovery |
| P1 | Prompt cache latch fields | 1 day | 30-50% cache hit |
| P1 | Startup fast paths | 3-5 days | DX improvement |
| P1 | Hook security hardening | 3-5 days | Security |
| P2 | 4-tier context compaction | 1-2 weeks | Session length |
| P2 | Plugin architecture for QE | 2-3 weeks | Ecosystem |
| P3 | QE Quality Daemon | 1 month | Transformative |
9. Improvement Recommendations
Key Insight
AQE's strength is domain specialization; Claude Code's strength is infrastructure robustness. We need to adopt their operational patterns (retry, compaction, concurrency, durability, security) and layer them onto our already-superior QE domain architecture.
Sprint 1 -- Quick Wins
- Microcompact for MCP tool results
isConcurrencySafeflags on all 200+ MCP tools- Retry engine with exponential backoff + jitter
Sprint 2 -- Durability & Performance
- Transcript-first session durability (JSONL)
- Prompt cache latch fields
- Startup fast paths for CLI vs MCP modes
Sprint 3-4 -- Security & Compaction
- Hook security hardening (snapshot freeze, SSRF guards)
- Full 4-tier context compaction
- Agent permission boundaries
Quarter 2 -- Platform Evolution
- Plugin architecture for QE domains
- Agent fork with prompt cache sharing
- QE Quality Daemon