MCPHub LabRegistrysanbuphy/claude-code-source-code
sanbuphy

sanbuphy/claude code source code

Built by sanbuphy โ€ข 829 stars

What is sanbuphy/claude code source code?

Claude Code v2.1.88 Source Code

How to use sanbuphy/claude code source code?

1. Install a compatible MCP client (like Claude Desktop). 2. Open your configuration settings. 3. Add sanbuphy/claude code source code using the following command: npx @modelcontextprotocol/sanbuphy-claude-code-source-code 4. Restart the client and verify the new tools are active.
๐Ÿ›ก๏ธ Scoped (Restricted)
npx @modelcontextprotocol/sanbuphy-claude-code-source-code --scope restricted
๐Ÿ”“ Unrestricted Access
npx @modelcontextprotocol/sanbuphy-claude-code-source-code

Key Features

Native MCP Protocol Support
Real-time Tool Activation & Execution
Verified High-performance Implementation
Secure Resource & Context Handling

Optimized Use Cases

Extending AI models with custom local capabilities
Automating system workflows via natural language
Connecting external data sources to LLM context windows

sanbuphy/claude code source code FAQ

Q

Is sanbuphy/claude code source code safe?

Yes, sanbuphy/claude code source code follows the standardized Model Context Protocol security patterns and only executes tools with explicit user-granted permissions.

Q

Is sanbuphy/claude code source code up to date?

sanbuphy/claude code source code is currently active in the registry with 829 stars on GitHub, indicating its reliability and community support.

Q

Are there any limits for sanbuphy/claude code source code?

Usage limits depend on the specific implementation of the MCP server and your system resources. Refer to the official documentation below for technical details.

Official Documentation

View on GitHub

Claude Code v2.1.88 โ€” Source Code Analysis

Disclaimer: All source code in this repository is the intellectual property of Anthropic and Claude. This repository is provided strictly for technical research, study, and educational exchange among enthusiasts. Commercial use is strictly prohibited. No individual, organization, or entity may use this content for commercial purposes, profit-making activities, illegal activities, or any other unauthorized scenarios. If any content infringes upon your legal rights, intellectual property, or other interests, please contact us and we will verify and remove it immediately.

Extracted from npm package @anthropic-ai/claude-code version 2.1.88. The published package ships a single bundled cli.js (~12MB). The src/ directory in this repo contains the unbundled TypeScript source extracted from the npm tarball.

Language: English | ไธญๆ–‡ | ํ•œ๊ตญ์–ด | ๆ—ฅๆœฌ่ชž


Table of Contents


Deep Analysis Reports (docs/)

Source code analysis reports derived from decompiled v2.1.88. Quadrilingual (EN/JA/KO/ZH).

docs/
โ”œโ”€โ”€ en/                                        # English
โ”‚   โ”œโ”€โ”€ [01-telemetry-and-privacy.md]          # Telemetry & Privacy โ€” what's collected, why you can't opt out
โ”‚   โ”œโ”€โ”€ [02-hidden-features-and-codenames.md]  # Codenames (Capybara/Tengu/Numbat), feature flags, internal vs external
โ”‚   โ”œโ”€โ”€ [03-undercover-mode.md]                # Undercover Mode โ€” hiding AI authorship in open-source repos
โ”‚   โ”œโ”€โ”€ [04-remote-control-and-killswitches.md]# Remote Control โ€” managed settings, killswitches, model overrides
โ”‚   โ””โ”€โ”€ [05-future-roadmap.md]                 # Future Roadmap โ€” Numbat, KAIROS, voice mode, unreleased tools
โ”‚
โ”œโ”€โ”€ ja/                                        # ๆ—ฅๆœฌ่ชž
โ”‚   โ”œโ”€โ”€ [01-ใƒ†ใƒฌใƒกใƒˆใƒชใจใƒ—ใƒฉใ‚คใƒใ‚ทใƒผ.md]          # ใƒ†ใƒฌใƒกใƒˆใƒชใจใƒ—ใƒฉใ‚คใƒใ‚ทใƒผ โ€” ๅŽ้›†้ …็›ฎใ€็„กๅŠนๅŒ–ไธๅฏใฎ็†็”ฑ
โ”‚   โ”œโ”€โ”€ [02-้š ใ—ๆฉŸ่ƒฝใจใ‚ณใƒผใƒ‰ใƒใƒผใƒ .md]           # ้š ใ—ๆฉŸ่ƒฝ โ€” ใƒขใƒ‡ใƒซใ‚ณใƒผใƒ‰ใƒใƒผใƒ ใ€feature flagใ€ๅ†…้ƒจ/ๅค–้ƒจใƒฆใƒผใ‚ถใƒผใฎ้•ใ„
โ”‚   โ”œโ”€โ”€ [03-ใ‚ขใƒณใƒ€ใƒผใ‚ซใƒใƒผใƒขใƒผใƒ‰.md]             # ใ‚ขใƒณใƒ€ใƒผใ‚ซใƒใƒผใƒขใƒผใƒ‰ โ€” ใ‚ชใƒผใƒ—ใƒณใ‚ฝใƒผใ‚นใงใฎAI่‘—ไฝœ้š ๅŒฟ
โ”‚   โ”œโ”€โ”€ [04-ใƒชใƒขใƒผใƒˆๅˆถๅพกใจใ‚ญใƒซใ‚นใ‚คใƒƒใƒ.md]       # ใƒชใƒขใƒผใƒˆๅˆถๅพก โ€” ็ฎก็†่จญๅฎšใ€ใ‚ญใƒซใ‚นใ‚คใƒƒใƒใ€ใƒขใƒ‡ใƒซใ‚ชใƒผใƒใƒผใƒฉใ‚คใƒ‰
โ”‚   โ””โ”€โ”€ [05-ไปŠๅพŒใฎใƒญใƒผใƒ‰ใƒžใƒƒใƒ—.md]               # ไปŠๅพŒใฎใƒญใƒผใƒ‰ใƒžใƒƒใƒ— โ€” Numbatใ€KAIROSใ€้Ÿณๅฃฐใƒขใƒผใƒ‰ใ€ๆœชๅ…ฌ้–‹ใƒ„ใƒผใƒซ
โ”‚
โ”œโ”€โ”€ ko/                                        # ํ•œ๊ตญ์–ด
โ”‚   โ”œโ”€โ”€ [01-ํ…”๋ ˆ๋ฉ”ํŠธ๋ฆฌ์™€-ํ”„๋ผ์ด๋ฒ„์‹œ.md]          # ํ…”๋ ˆ๋ฉ”ํŠธ๋ฆฌ ๋ฐ ํ”„๋ผ์ด๋ฒ„์‹œ โ€” ์ˆ˜์ง‘ ํ•ญ๋ชฉ, ๋น„ํ™œ์„ฑํ™” ๋ถˆ๊ฐ€ ์ด์œ 
โ”‚   โ”œโ”€โ”€ [02-์ˆจ๊ฒจ์ง„-๊ธฐ๋Šฅ๊ณผ-์ฝ”๋“œ๋„ค์ž„.md]          # ์ˆจ๊ฒจ์ง„ ๊ธฐ๋Šฅ โ€” ๋ชจ๋ธ ์ฝ”๋“œ๋„ค์ž„, feature flag, ๋‚ด๋ถ€/์™ธ๋ถ€ ์‚ฌ์šฉ์ž ์ฐจ์ด
โ”‚   โ”œโ”€โ”€ [03-์–ธ๋”์ปค๋ฒ„-๋ชจ๋“œ.md]                   # ์–ธ๋”์ปค๋ฒ„ ๋ชจ๋“œ โ€” ์˜คํ”ˆ์†Œ์Šค์—์„œ AI ์ €์ž‘ ์€ํ
โ”‚   โ”œโ”€โ”€ [04-์›๊ฒฉ-์ œ์–ด์™€-ํ‚ฌ์Šค์œ„์น˜.md]            # ์›๊ฒฉ ์ œ์–ด โ€” ๊ด€๋ฆฌ ์„ค์ •, ํ‚ฌ์Šค์œ„์น˜, ๋ชจ๋ธ ์˜ค๋ฒ„๋ผ์ด๋“œ
โ”‚   โ””โ”€โ”€ [05-ํ–ฅํ›„-๋กœ๋“œ๋งต.md]                     # ํ–ฅํ›„ ๋กœ๋“œ๋งต โ€” Numbat, KAIROS, ์Œ์„ฑ ๋ชจ๋“œ, ๋ฏธ๊ณต๊ฐœ ๋„๊ตฌ
โ”‚
โ””โ”€โ”€ zh/                                        # ไธญๆ–‡
    โ”œโ”€โ”€ [01-้ฅๆต‹ไธŽ้š็งๅˆ†ๆž.md]                    # ้ฅๆต‹ไธŽ้š็ง โ€” ๆ”ถ้›†ไบ†ไป€ไนˆ๏ผŒไธบไป€ไนˆๆ— ๆณ•้€€ๅ‡บ
    โ”œโ”€โ”€ [02-้š่—ๅŠŸ่ƒฝไธŽๆจกๅž‹ไปฃๅท.md]                # ้š่—ๅŠŸ่ƒฝ โ€” ๆจกๅž‹ไปฃๅท๏ผŒfeature flag๏ผŒๅ†…ๅค–็”จๆˆทๅทฎๅผ‚
    โ”œโ”€โ”€ [03-ๅงๅบ•ๆจกๅผๅˆ†ๆž.md]                     # ๅงๅบ•ๆจกๅผ โ€” ๅœจๅผ€ๆบ้กน็›ฎไธญ้š่— AI ่บซไปฝ
    โ”œโ”€โ”€ [04-่ฟœ็จ‹ๆŽงๅˆถไธŽ็ดงๆ€ฅๅผ€ๅ…ณ.md]                # ่ฟœ็จ‹ๆŽงๅˆถ โ€” ๆ‰˜็ฎก่ฎพ็ฝฎ๏ผŒ็ดงๆ€ฅๅผ€ๅ…ณ๏ผŒๆจกๅž‹่ฆ†็›–
    โ””โ”€โ”€ [05-ๆœชๆฅ่ทฏ็บฟๅ›พ.md]                       # ๆœชๆฅ่ทฏ็บฟๅ›พ โ€” Numbat๏ผŒKAIROS๏ผŒ่ฏญ้Ÿณๆจกๅผ๏ผŒๆœชไธŠ็บฟๅทฅๅ…ท

Click any filename above to jump to the full report.

#TopicKey Findings
01Telemetry & PrivacyTwo analytics sinks (1P โ†’ Anthropic, Datadog). Environment fingerprint, process metrics, repo hash on every event. No UI-exposed opt-out for 1st-party logging. OTEL_LOG_TOOL_DETAILS=1 enables full tool input capture.
02Hidden Features & CodenamesAnimal codenames (Capybara v8, Tengu, Fennecโ†’Opus 4.6, Numbat next). Feature flags use random word pairs (tengu_frond_boric) to obscure purpose. Internal users get better prompts, verification agents, and effort anchors. Hidden commands: /btw, /stickers.
03Undercover ModeAnthropic employees auto-enter undercover mode in public repos. Model instructed: "Do not blow your cover" โ€” strip all AI attribution, write commits "as a human developer would." No force-OFF exists. Raises transparency questions for open-source communities.
04Remote ControlHourly polling of /api/claude_code/settings. Dangerous changes show blocking dialog โ€” reject = app exits. 6+ killswitches (bypass permissions, fast mode, voice mode, analytics sink). GrowthBook flags can change any user's behavior without consent.
05Future RoadmapNumbat codename confirmed. Opus 4.7 / Sonnet 4.8 in development. KAIROS = fully autonomous agent mode with <tick> heartbeats, push notifications, PR subscriptions. Voice mode (push-to-talk) ready but gated. 17 unreleased tools found.

Missing Modules Notice (108 modules)

This source is incomplete. 108 modules referenced by feature()-gated branches are not included in the npm package. They exist only in Anthropic's internal monorepo and were dead-code-eliminated at compile time. They cannot be recovered from cli.js, sdk-tools.d.ts, or any published artifact.

Anthropic Internal Code (~70 modules, never published)

These modules have no source files anywhere in the npm package. They are internal Anthropic infrastructure.

<details> <summary>Click to expand full list</summary>
ModulePurposeFeature Gate
daemon/main.jsBackground daemon supervisorDAEMON
daemon/workerRegistry.jsDaemon worker registryDAEMON
proactive/index.jsProactive notification systemPROACTIVE
contextCollapse/index.jsContext collapse service (experimental)CONTEXT_COLLAPSE
contextCollapse/operations.jsCollapse operationsCONTEXT_COLLAPSE
contextCollapse/persist.jsCollapse persistenceCONTEXT_COLLAPSE
skillSearch/featureCheck.jsRemote skill feature checkEXPERIMENTAL_SKILL_SEARCH
skillSearch/remoteSkillLoader.jsRemote skill loaderEXPERIMENTAL_SKILL_SEARCH
skillSearch/remoteSkillState.jsRemote skill stateEXPERIMENTAL_SKILL_SEARCH
skillSearch/telemetry.jsSkill search telemetryEXPERIMENTAL_SKILL_SEARCH
skillSearch/localSearch.jsLocal skill searchEXPERIMENTAL_SKILL_SEARCH
skillSearch/prefetch.jsSkill prefetchEXPERIMENTAL_SKILL_SEARCH
coordinator/workerAgent.jsMulti-agent coordinator workerCOORDINATOR_MODE
bridge/peerSessions.jsBridge peer session managementBRIDGE_MODE
assistant/index.jsKairos assistant modeKAIROS
assistant/AssistantSessionChooser.jsAssistant session pickerKAIROS
compact/reactiveCompact.jsReactive context compactionCACHED_MICROCOMPACT
compact/snipCompact.jsSnip-based compactionHISTORY_SNIP
compact/snipProjection.jsSnip projectionHISTORY_SNIP
compact/cachedMCConfig.jsCached micro-compact configCACHED_MICROCOMPACT
sessionTranscript/sessionTranscript.jsSession transcript serviceTRANSCRIPT_CLASSIFIER
commands/agents-platform/index.jsInternal agents platformant (internal)
commands/assistant/index.jsAssistant commandKAIROS
commands/buddy/index.jsBuddy system notificationsBUDDY
commands/fork/index.jsFork subagent commandFORK_SUBAGENT
commands/peers/index.jsMulti-peer commandsBRIDGE_MODE
commands/proactive.jsProactive commandPROACTIVE
commands/remoteControlServer/index.jsRemote control serverDAEMON + BRIDGE_MODE
commands/subscribe-pr.jsGitHub PR subscriptionKAIROS_GITHUB_WEBHOOKS
commands/torch.jsInternal debug toolTORCH
commands/workflows/index.jsWorkflow commandsWORKFLOW_SCRIPTS
jobs/classifier.jsInternal job classifierTEMPLATES
memdir/memoryShapeTelemetry.jsMemory shape telemetryMEMORY_SHAPE_TELEMETRY
services/sessionTranscript/sessionTranscript.jsSession transcriptTRANSCRIPT_CLASSIFIER
tasks/LocalWorkflowTask/LocalWorkflowTask.jsLocal workflow taskWORKFLOW_SCRIPTS
protectedNamespace.jsInternal namespace guardant (internal)
protectedNamespace.js (envUtils)Protected namespace runtimeant (internal)
coreTypes.generated.jsGenerated core typesant (internal)
devtools.jsInternal dev toolsant (internal)
attributionHooks.jsInternal attribution hooksCOMMIT_ATTRIBUTION
systemThemeWatcher.jsSystem theme watcherAUTO_THEME
udsClient.js / udsMessaging.jsUDS messaging clientUDS_INBOX
systemThemeWatcher.jsTheme watcherAUTO_THEME
</details>

Feature-Gated Tools (~20 modules, DCE'd from bundle)

These tools have type signatures in sdk-tools.d.ts but their implementations were stripped at compile time.

<details> <summary>Click to expand full list</summary>
ToolPurposeFeature Gate
REPLToolInteractive REPL (VM sandbox)ant (internal)
SnipToolContext snippingHISTORY_SNIP
SleepToolSleep/delay in agent loopPROACTIVE / KAIROS
MonitorToolMCP monitoringMONITOR_TOOL
OverflowTestToolOverflow testingOVERFLOW_TEST_TOOL
WorkflowToolWorkflow executionWORKFLOW_SCRIPTS
WebBrowserToolBrowser automationWEB_BROWSER_TOOL
TerminalCaptureToolTerminal captureTERMINAL_PANEL
TungstenToolInternal perf monitoringant (internal)
VerifyPlanExecutionToolPlan verificationCLAUDE_CODE_VERIFY_PLAN
SendUserFileToolSend files to usersKAIROS
SubscribePRToolGitHub PR subscriptionKAIROS_GITHUB_WEBHOOKS
SuggestBackgroundPRToolSuggest background PRsKAIROS
PushNotificationToolPush notificationsKAIROS
CtxInspectToolContext inspectionCONTEXT_COLLAPSE
ListPeersToolList active peersUDS_INBOX
DiscoverSkillsToolSkill discoveryEXPERIMENTAL_SKILL_SEARCH
</details>

Text/Prompt Assets (~6 files)

These are internal prompt templates and documentation, never published.

<details> <summary>Click to expand</summary>
FilePurpose
yolo-classifier-prompts/auto_mode_system_prompt.txtAuto-mode system prompt for classifier
yolo-classifier-prompts/permissions_anthropic.txtAnthropic-internal permission prompt
yolo-classifier-prompts/permissions_external.txtExternal user permission prompt
verify/SKILL.mdVerification skill documentation
verify/examples/cli.mdCLI verification examples
verify/examples/server.mdServer verification examples
</details>

Why They're Missing

  Anthropic Internal Monorepo              Published npm Package
  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€               โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  feature('DAEMON') โ†’ true    โ”€โ”€buildโ”€โ”€โ†’   feature('DAEMON') โ†’ false
  โ†“                                         โ†“
  daemon/main.js  โ† INCLUDED    โ”€โ”€bundleโ”€โ†’  daemon/main.js  โ† DELETED (DCE)
  tools/REPLTool  โ† INCLUDED    โ”€โ”€bundleโ”€โ†’  tools/REPLTool  โ† DELETED (DCE)
  proactive/      โ† INCLUDED    โ”€โ”€bundleโ”€โ†’  (referenced but absent from src/)

Bun's feature() is a compile-time intrinsic:

  • Returns true in Anthropic's internal build โ†’ code is kept in the bundle
  • Returns false in the published build โ†’ code is dead-code-eliminated
  • The 108 modules simply do not exist anywhere in the published artifact

Copyright & Disclaimer

Copyright (c) Anthropic. All rights reserved.

All source code in this repository is the intellectual property of Anthropic and Claude.
This repository is provided strictly for technical research and educational purposes.
Commercial use is strictly prohibited.

If you are the copyright owner and believe this repository infringes your rights,
please contact the repository owner for immediate removal.

Stats

ItemCount
Source files (.ts/.tsx)~1,884
Lines of code~512,664
Largest single filequery.ts (~785KB)
Built-in tools~40+
Slash commands~80+
Dependencies (node_modules)~192 packages
RuntimeBun (compiled to Node.js >= 18 bundle)

The Agent Pattern

                    THE CORE LOOP
                    =============

    User --> messages[] --> Claude API --> response
                                          |
                                stop_reason == "tool_use"?
                               /                          \
                             yes                           no
                              |                             |
                        execute tools                    return text
                        append tool_result
                        loop back -----------------> messages[]


    That is the minimal agent loop. Claude Code wraps this loop
    with a production-grade harness: permissions, streaming,
    concurrency, compaction, sub-agents, persistence, and MCP.

Directory Reference

src/
โ”œโ”€โ”€ main.tsx                 # REPL bootstrap, 4,683 lines
โ”œโ”€โ”€ QueryEngine.ts           # SDK/headless query lifecycle engine
โ”œโ”€โ”€ query.ts                 # Main agent loop (785KB, largest file)
โ”œโ”€โ”€ Tool.ts                  # Tool interface + buildTool factory
โ”œโ”€โ”€ Task.ts                  # Task types, IDs, state base
โ”œโ”€โ”€ tools.ts                 # Tool registry, presets, filtering
โ”œโ”€โ”€ commands.ts              # Slash command definitions
โ”œโ”€โ”€ context.ts               # User input context
โ”œโ”€โ”€ cost-tracker.ts          # API cost accumulation
โ”œโ”€โ”€ setup.ts                 # First-run setup flow
โ”‚
โ”œโ”€โ”€ bridge/                  # Claude Desktop / remote bridge
โ”‚   โ”œโ”€โ”€ bridgeMain.ts        #   Session lifecycle manager
โ”‚   โ”œโ”€โ”€ bridgeApi.ts         #   HTTP client
โ”‚   โ”œโ”€โ”€ bridgeConfig.ts      #   Connection config
โ”‚   โ”œโ”€โ”€ bridgeMessaging.ts   #   Message relay
โ”‚   โ”œโ”€โ”€ sessionRunner.ts     #   Process spawning
โ”‚   โ”œโ”€โ”€ jwtUtils.ts          #   JWT refresh
โ”‚   โ”œโ”€โ”€ workSecret.ts        #   Auth tokens
โ”‚   โ””โ”€โ”€ capacityWake.ts      #   Capacity-based wakeup
โ”‚
โ”œโ”€โ”€ cli/                     # CLI infrastructure
โ”‚   โ”œโ”€โ”€ handlers/            #   Command handlers
โ”‚   โ””โ”€โ”€ transports/          #   I/O transports (stdio, structured)
โ”‚
โ”œโ”€โ”€ commands/                # ~80 slash commands
โ”‚   โ”œโ”€โ”€ agents/              #   Agent management
โ”‚   โ”œโ”€โ”€ compact/             #   Context compaction
โ”‚   โ”œโ”€โ”€ config/              #   Settings management
โ”‚   โ”œโ”€โ”€ help/                #   Help display
โ”‚   โ”œโ”€โ”€ login/               #   Authentication
โ”‚   โ”œโ”€โ”€ mcp/                 #   MCP server management
โ”‚   โ”œโ”€โ”€ memory/              #   Memory system
โ”‚   โ”œโ”€โ”€ plan/                #   Plan mode
โ”‚   โ”œโ”€โ”€ resume/              #   Session resume
โ”‚   โ”œโ”€โ”€ review/              #   Code review
โ”‚   โ””โ”€โ”€ ...                  #   70+ more commands
โ”‚
โ”œโ”€โ”€ components/              # React/Ink terminal UI
โ”‚   โ”œโ”€โ”€ design-system/       #   Reusable UI primitives
โ”‚   โ”œโ”€โ”€ messages/            #   Message rendering
โ”‚   โ”œโ”€โ”€ permissions/         #   Permission dialogs
โ”‚   โ”œโ”€โ”€ PromptInput/         #   Input field + suggestions
โ”‚   โ”œโ”€โ”€ LogoV2/              #   Branding + welcome screen
โ”‚   โ”œโ”€โ”€ Settings/            #   Settings panels
โ”‚   โ”œโ”€โ”€ Spinner.tsx          #   Loading indicators
โ”‚   โ””โ”€โ”€ ...                  #   40+ component groups
โ”‚
โ”œโ”€โ”€ entrypoints/             # Application entry points
โ”‚   โ”œโ”€โ”€ cli.tsx              #   CLI main (version, help, daemon)
โ”‚   โ”œโ”€โ”€ sdk/                 #   Agent SDK (types, sessions)
โ”‚   โ””โ”€โ”€ mcp.ts               #   MCP server entry
โ”‚
โ”œโ”€โ”€ hooks/                   # React hooks
โ”‚   โ”œโ”€โ”€ useCanUseTool.tsx    #   Permission checking
โ”‚   โ”œโ”€โ”€ useReplBridge.tsx    #   Bridge connection
โ”‚   โ”œโ”€โ”€ notifs/              #   Notification hooks
โ”‚   โ””โ”€โ”€ toolPermission/      #   Tool permission handlers
โ”‚
โ”œโ”€โ”€ services/                # Business logic layer
โ”‚   โ”œโ”€โ”€ api/                 #   Claude API client
โ”‚   โ”‚   โ”œโ”€โ”€ claude.ts        #     Streaming API calls
โ”‚   โ”‚   โ”œโ”€โ”€ errors.ts        #     Error categorization
โ”‚   โ”‚   โ””โ”€โ”€ withRetry.ts     #     Retry logic
โ”‚   โ”œโ”€โ”€ analytics/           #   Telemetry + GrowthBook
โ”‚   โ”œโ”€โ”€ compact/             #   Context compression
โ”‚   โ”œโ”€โ”€ mcp/                 #   MCP connection management
โ”‚   โ”œโ”€โ”€ tools/               #   Tool execution engine
โ”‚   โ”‚   โ”œโ”€โ”€ StreamingToolExecutor.ts  # Parallel tool runner
โ”‚   โ”‚   โ””โ”€โ”€ toolOrchestration.ts      # Batch orchestration
โ”‚   โ”œโ”€โ”€ plugins/             #   Plugin loader
โ”‚   โ””โ”€โ”€ settingsSync/        #   Cross-device settings
โ”‚
โ”œโ”€โ”€ state/                   # Application state
โ”‚   โ”œโ”€โ”€ AppStateStore.ts     #   Store definition
โ”‚   โ””โ”€โ”€ AppState.tsx         #   React provider + hooks
โ”‚
โ”œโ”€โ”€ tasks/                   # Task implementations
โ”‚   โ”œโ”€โ”€ LocalShellTask/      #   Bash command execution
โ”‚   โ”œโ”€โ”€ LocalAgentTask/      #   Sub-agent execution
โ”‚   โ”œโ”€โ”€ RemoteAgentTask/     #   Remote agent via bridge
โ”‚   โ”œโ”€โ”€ InProcessTeammateTask/ # In-process teammate
โ”‚   โ””โ”€โ”€ DreamTask/           #   Background thinking
โ”‚
โ”œโ”€โ”€ tools/                   # 40+ tool implementations
โ”‚   โ”œโ”€โ”€ AgentTool/           #   Sub-agent spawning + fork
โ”‚   โ”œโ”€โ”€ BashTool/            #   Shell command execution
โ”‚   โ”œโ”€โ”€ FileReadTool/        #   File reading (PDF, image, etc)
โ”‚   โ”œโ”€โ”€ FileEditTool/        #   String-replace editing
โ”‚   โ”œโ”€โ”€ FileWriteTool/       #   Full file creation
โ”‚   โ”œโ”€โ”€ GlobTool/            #   File pattern search
โ”‚   โ”œโ”€โ”€ GrepTool/            #   Content search (ripgrep)
โ”‚   โ”œโ”€โ”€ WebFetchTool/        #   HTTP fetching
โ”‚   โ”œโ”€โ”€ WebSearchTool/       #   Web search
โ”‚   โ”œโ”€โ”€ MCPTool/             #   MCP tool wrapper
โ”‚   โ”œโ”€โ”€ SkillTool/           #   Skill invocation
โ”‚   โ”œโ”€โ”€ AskUserQuestionTool/ #   User interaction
โ”‚   โ””โ”€โ”€ ...                  #   30+ more tools
โ”‚
โ”œโ”€โ”€ types/                   # Type definitions
โ”‚   โ”œโ”€โ”€ message.ts           #   Message discriminated unions
โ”‚   โ”œโ”€โ”€ permissions.ts       #   Permission types
โ”‚   โ”œโ”€โ”€ tools.ts             #   Tool progress types
โ”‚   โ””โ”€โ”€ ids.ts               #   Branded ID types
โ”‚
โ”œโ”€โ”€ utils/                   # Utilities (largest directory)
โ”‚   โ”œโ”€โ”€ permissions/         #   Permission rule engine
โ”‚   โ”œโ”€โ”€ messages/            #   Message formatting
โ”‚   โ”œโ”€โ”€ model/               #   Model selection logic
โ”‚   โ”œโ”€โ”€ settings/            #   Settings management
โ”‚   โ”œโ”€โ”€ sandbox/             #   Sandbox runtime adapter
โ”‚   โ”œโ”€โ”€ hooks/               #   Hook execution
โ”‚   โ”œโ”€โ”€ memory/              #   Memory system utils
โ”‚   โ”œโ”€โ”€ git/                 #   Git operations
โ”‚   โ”œโ”€โ”€ github/              #   GitHub API
โ”‚   โ”œโ”€โ”€ bash/                #   Bash execution helpers
โ”‚   โ”œโ”€โ”€ swarm/               #   Multi-agent swarm
โ”‚   โ”œโ”€โ”€ telemetry/           #   Telemetry reporting
โ”‚   โ””โ”€โ”€ ...                  #   30+ more util groups
โ”‚
โ””โ”€โ”€ vendor/                  # Native module source stubs
    โ”œโ”€โ”€ audio-capture-src/   #   Audio input
    โ”œโ”€โ”€ image-processor-src/ #   Image processing
    โ”œโ”€โ”€ modifiers-napi-src/  #   Native modifiers
    โ””โ”€โ”€ url-handler-src/     #   URL handling

Architecture Overview

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                         ENTRY LAYER                                 โ”‚
โ”‚  cli.tsx โ”€โ”€> main.tsx โ”€โ”€> REPL.tsx (interactive)                   โ”‚
โ”‚                     โ””โ”€โ”€> QueryEngine.ts (headless/SDK)              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚
                               โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                       QUERY ENGINE                                  โ”‚
โ”‚  submitMessage(prompt) โ”€โ”€> AsyncGenerator<SDKMessage>               โ”‚
โ”‚    โ”‚                                                                โ”‚
โ”‚    โ”œโ”€โ”€ fetchSystemPromptParts()    โ”€โ”€> assemble system prompt       โ”‚
โ”‚    โ”œโ”€โ”€ processUserInput()          โ”€โ”€> handle /commands             โ”‚
โ”‚    โ”œโ”€โ”€ query()                     โ”€โ”€> main agent loop              โ”‚
โ”‚    โ”‚     โ”œโ”€โ”€ StreamingToolExecutor โ”€โ”€> parallel tool execution       โ”‚
โ”‚    โ”‚     โ”œโ”€โ”€ autoCompact()         โ”€โ”€> context compression          โ”‚
โ”‚    โ”‚     โ””โ”€โ”€ runTools()            โ”€โ”€> tool orchestration           โ”‚
โ”‚    โ””โ”€โ”€ yield SDKMessage            โ”€โ”€> stream to consumer           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ–ผ                โ–ผ                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   TOOL SYSTEM    โ”‚ โ”‚  SERVICE LAYER  โ”‚ โ”‚   STATE LAYER    โ”‚
โ”‚                  โ”‚ โ”‚                 โ”‚ โ”‚                  โ”‚
โ”‚ Tool Interface   โ”‚ โ”‚ api/claude.ts   โ”‚ โ”‚ AppState Store   โ”‚
โ”‚  โ”œโ”€ call()       โ”‚ โ”‚  API client     โ”‚ โ”‚  โ”œโ”€ permissions  โ”‚
โ”‚  โ”œโ”€ validate()   โ”‚ โ”‚ compact/        โ”‚ โ”‚  โ”œโ”€ fileHistory  โ”‚
โ”‚  โ”œโ”€ checkPerms() โ”‚ โ”‚  auto-compact   โ”‚ โ”‚  โ”œโ”€ agents       โ”‚
โ”‚  โ”œโ”€ render()     โ”‚ โ”‚ mcp/            โ”‚ โ”‚  โ””โ”€ fastMode     โ”‚
โ”‚  โ””โ”€ prompt()     โ”‚ โ”‚  MCP protocol   โ”‚ โ”‚                  โ”‚
โ”‚                  โ”‚ โ”‚ analytics/      โ”‚ โ”‚ React Context    โ”‚
โ”‚ 40+ Built-in:    โ”‚ โ”‚  telemetry      โ”‚ โ”‚  โ”œโ”€ useAppState  โ”‚
โ”‚  โ”œโ”€ BashTool     โ”‚ โ”‚ tools/          โ”‚ โ”‚  โ””โ”€ useSetState  โ”‚
โ”‚  โ”œโ”€ FileRead     โ”‚ โ”‚  executor       โ”‚ โ”‚                  โ”‚
โ”‚  โ”œโ”€ FileEdit     โ”‚ โ”‚ plugins/        โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚  โ”œโ”€ Glob/Grep    โ”‚ โ”‚  loader         โ”‚
โ”‚  โ”œโ”€ AgentTool    โ”‚ โ”‚ settingsSync/   โ”‚
โ”‚  โ”œโ”€ WebFetch     โ”‚ โ”‚  cross-device   โ”‚
โ”‚  โ””โ”€ MCPTool      โ”‚ โ”‚ oauth/          โ”‚
โ”‚                  โ”‚ โ”‚  auth flow      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
              โ”‚                โ”‚
              โ–ผ                โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   TASK SYSTEM    โ”‚ โ”‚   BRIDGE LAYER  โ”‚
โ”‚                  โ”‚ โ”‚                 โ”‚
โ”‚ Task Types:      โ”‚ โ”‚ bridgeMain.ts   โ”‚
โ”‚  โ”œโ”€ local_bash   โ”‚ โ”‚  session mgmt   โ”‚
โ”‚  โ”œโ”€ local_agent  โ”‚ โ”‚ bridgeApi.ts    โ”‚
โ”‚  โ”œโ”€ remote_agent โ”‚ โ”‚  HTTP client    โ”‚
โ”‚  โ”œโ”€ in_process   โ”‚ โ”‚ workSecret.ts   โ”‚
โ”‚  โ”œโ”€ dream        โ”‚ โ”‚  auth tokens    โ”‚
โ”‚  โ””โ”€ workflow     โ”‚ โ”‚ sessionRunner   โ”‚
โ”‚                  โ”‚ โ”‚  process spawn  โ”‚
โ”‚ ID: prefix+8chr  โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚  b=bash a=agent  โ”‚
โ”‚  r=remote t=team โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Data Flow: A Single Query Lifecycle

 USER INPUT (prompt / slash command)
     โ”‚
     โ–ผ
 processUserInput()                โ† parse /commands, build UserMessage
     โ”‚
     โ–ผ
 fetchSystemPromptParts()          โ† tools โ†’ prompt sections, CLAUDE.md memory
     โ”‚
     โ–ผ
 recordTranscript()                โ† persist user message to disk (JSONL)
     โ”‚
     โ–ผ
 โ”Œโ”€โ†’ normalizeMessagesForAPI()     โ† strip UI-only fields, compact if needed
 โ”‚   โ”‚
 โ”‚   โ–ผ
 โ”‚   Claude API (streaming)        โ† POST /v1/messages with tools + system prompt
 โ”‚   โ”‚
 โ”‚   โ–ผ
 โ”‚   stream events                 โ† message_start โ†’ content_block_delta โ†’ message_stop
 โ”‚   โ”‚
 โ”‚   โ”œโ”€ text block โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ yield to consumer (SDK / REPL)
 โ”‚   โ”‚
 โ”‚   โ””โ”€ tool_use block?
 โ”‚       โ”‚
 โ”‚       โ–ผ
 โ”‚   StreamingToolExecutor         โ† partition: concurrent-safe vs serial
 โ”‚       โ”‚
 โ”‚       โ–ผ
 โ”‚   canUseTool()                  โ† permission check (hooks + rules + UI prompt)
 โ”‚       โ”‚
 โ”‚       โ”œโ”€ DENY โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ append tool_result(error), continue loop
 โ”‚       โ”‚
 โ”‚       โ””โ”€ ALLOW
 โ”‚           โ”‚
 โ”‚           โ–ผ
 โ”‚       tool.call()               โ† execute the tool (Bash, Read, Edit, etc.)
 โ”‚           โ”‚
 โ”‚           โ–ผ
 โ”‚       append tool_result        โ† push to messages[], recordTranscript()
 โ”‚           โ”‚
 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                      โ† loop back to API call
     โ”‚
     โ–ผ (stop_reason != "tool_use")
 yield result message              โ† final text, usage, cost, session_id

Tool System Architecture

                    TOOL INTERFACE
                    ==============

    buildTool(definition) โ”€โ”€> Tool<Input, Output, Progress>

    Every tool implements:
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  LIFECYCLE                                             โ”‚
    โ”‚  โ”œโ”€โ”€ validateInput()      โ†’ reject bad args early     โ”‚
    โ”‚  โ”œโ”€โ”€ checkPermissions()   โ†’ tool-specific authz       โ”‚
    โ”‚  โ””โ”€โ”€ call()               โ†’ execute and return result โ”‚
    โ”‚                                                        โ”‚
    โ”‚  CAPABILITIES                                          โ”‚
    โ”‚  โ”œโ”€โ”€ isEnabled()          โ†’ feature gate check        โ”‚
    โ”‚  โ”œโ”€โ”€ isConcurrencySafe()  โ†’ can run in parallel?      โ”‚
    โ”‚  โ”œโ”€โ”€ isReadOnly()         โ†’ no side effects?          โ”‚
    โ”‚  โ”œโ”€โ”€ isDestructive()      โ†’ irreversible ops?         โ”‚
    โ”‚  โ””โ”€โ”€ interruptBehavior()  โ†’ cancel or block on user?  โ”‚
    โ”‚                                                        โ”‚
    โ”‚  RENDERING (React/Ink)                                 โ”‚
    โ”‚  โ”œโ”€โ”€ renderToolUseMessage()     โ†’ input display        โ”‚
    โ”‚  โ”œโ”€โ”€ renderToolResultMessage()  โ†’ output display       โ”‚
    โ”‚  โ”œโ”€โ”€ renderToolUseProgressMessage() โ†’ spinner/status   โ”‚
    โ”‚  โ””โ”€โ”€ renderGroupedToolUse()     โ†’ parallel tool groups โ”‚
    โ”‚                                                        โ”‚
    โ”‚  AI FACING                                             โ”‚
    โ”‚  โ”œโ”€โ”€ prompt()             โ†’ tool description for LLM  โ”‚
    โ”‚  โ”œโ”€โ”€ description()        โ†’ dynamic description       โ”‚
    โ”‚  โ””โ”€โ”€ mapToolResultToAPI() โ†’ format for API response   โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Complete Tool Inventory

    FILE OPERATIONS          SEARCH & DISCOVERY        EXECUTION
    โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•        โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•     โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    FileReadTool             GlobTool                  BashTool
    FileEditTool             GrepTool                  PowerShellTool
    FileWriteTool            ToolSearchTool
    NotebookEditTool                                   INTERACTION
                                                       โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    WEB & NETWORK           AGENT / TASK              AskUserQuestionTool
    โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•        โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•        BriefTool
    WebFetchTool             AgentTool
    WebSearchTool            SendMessageTool           PLANNING & WORKFLOW
                             TeamCreateTool            โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
    MCP PROTOCOL             TeamDeleteTool            EnterPlanModeTool
    โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•           TaskCreateTool            ExitPlanModeTool
    MCPTool                  TaskGetTool               EnterWorktreeTool
    ListMcpResourcesTool     TaskUpdateTool            ExitWorktreeTool
    ReadMcpResourceTool      TaskListTool              TodoWriteTool
                             TaskStopTool
                             TaskOutputTool            SYSTEM
                                                       โ•โ•โ•โ•โ•โ•โ•โ•
                             SKILLS & EXTENSIONS       ConfigTool
                             โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•     SkillTool
                             SkillTool                 ScheduleCronTool
                             LSPTool                   SleepTool
                                                       TungstenTool

Permission System

    TOOL CALL REQUEST
          โ”‚
          โ–ผ
    โ”Œโ”€ validateInput() โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  reject invalid inputs before any permission check โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                         โ–ผ
    โ”Œโ”€ PreToolUse Hooks โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  user-defined shell commands (settings.json hooks) โ”‚
    โ”‚  can: approve, deny, or modify input               โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                         โ–ผ
    โ”Œโ”€ Permission Rules โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  alwaysAllowRules:  match tool name/pattern โ†’ auto โ”‚
    โ”‚  alwaysDenyRules:   match tool name/pattern โ†’ deny โ”‚
    โ”‚  alwaysAskRules:    match tool name/pattern โ†’ ask  โ”‚
    โ”‚  Sources: settings, CLI args, session decisions    โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                    no rule match?
                         โ”‚
                         โ–ผ
    โ”Œโ”€ Interactive Prompt โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  User sees tool name + input                       โ”‚
    โ”‚  Options: Allow Once / Allow Always / Deny         โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                         โ–ผ
    โ”Œโ”€ checkPermissions() โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  Tool-specific logic (e.g. path sandboxing)        โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚
                    APPROVED โ†’ tool.call()

Sub-Agent & Multi-Agent Architecture

                        MAIN AGENT
                        ==========
                            โ”‚
            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
            โ–ผ               โ–ผ               โ–ผ
     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
     โ”‚  FORK AGENT  โ”‚ โ”‚ REMOTE   โ”‚ โ”‚ IN-PROCESS   โ”‚
     โ”‚              โ”‚ โ”‚ AGENT    โ”‚ โ”‚ TEAMMATE     โ”‚
     โ”‚ Fork process โ”‚ โ”‚ Bridge   โ”‚ โ”‚ Same process โ”‚
     โ”‚ Shared cache โ”‚ โ”‚ session  โ”‚ โ”‚ Async contextโ”‚
     โ”‚ Fresh msgs[] โ”‚ โ”‚ Isolated โ”‚ โ”‚ Shared state โ”‚
     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

    SPAWN MODES:
    โ”œโ”€ default    โ†’ in-process, shared conversation
    โ”œโ”€ fork       โ†’ child process, fresh messages[], shared file cache
    โ”œโ”€ worktree   โ†’ isolated git worktree + fork
    โ””โ”€ remote     โ†’ bridge to Claude Code Remote / container

    COMMUNICATION:
    โ”œโ”€ SendMessageTool     โ†’ agent-to-agent messages
    โ”œโ”€ TaskCreate/Update   โ†’ shared task board
    โ””โ”€ TeamCreate/Delete   โ†’ team lifecycle management

    SWARM MODE (feature-gated):
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  Lead Agent                                 โ”‚
    โ”‚    โ”œโ”€โ”€ Teammate A โ”€โ”€> claims Task 1         โ”‚
    โ”‚    โ”œโ”€โ”€ Teammate B โ”€โ”€> claims Task 2         โ”‚
    โ”‚    โ””โ”€โ”€ Teammate C โ”€โ”€> claims Task 3         โ”‚
    โ”‚                                             โ”‚
    โ”‚  Shared: task board, message inbox          โ”‚
    โ”‚  Isolated: messages[], file cache, cwd      โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Context Management (Compact System)

    CONTEXT WINDOW BUDGET
    โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  System Prompt (tools, permissions, CLAUDE.md)      โ”‚
    โ”‚  โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•      โ”‚
    โ”‚                                                     โ”‚
    โ”‚  Conversation History                               โ”‚
    โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
    โ”‚  โ”‚ [compacted summary of older messages]        โ”‚    โ”‚
    โ”‚  โ”‚ โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•  โ”‚    โ”‚
    โ”‚  โ”‚ [compact_boundary marker]                    โ”‚    โ”‚
    โ”‚  โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚    โ”‚
    โ”‚  โ”‚ [recent messages โ€” full fidelity]            โ”‚    โ”‚
    โ”‚  โ”‚ user โ†’ assistant โ†’ tool_use โ†’ tool_result   โ”‚    โ”‚
    โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
    โ”‚                                                     โ”‚
    โ”‚  Current Turn (user + assistant response)            โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

    THREE COMPRESSION STRATEGIES:
    โ”œโ”€ autoCompact     โ†’ triggers when token count exceeds threshold
    โ”‚                     summarizes old messages via a compact API call
    โ”œโ”€ snipCompact     โ†’ removes zombie messages and stale markers
    โ”‚                     (HISTORY_SNIP feature flag)
    โ””โ”€ contextCollapse โ†’ restructures context for efficiency
                         (CONTEXT_COLLAPSE feature flag)

    COMPACTION FLOW:
    messages[] โ”€โ”€> getMessagesAfterCompactBoundary()
                        โ”‚
                        โ–ผ
                  older messages โ”€โ”€> Claude API (summarize) โ”€โ”€> compact summary
                        โ”‚
                        โ–ผ
                  [summary] + [compact_boundary] + [recent messages]

MCP (Model Context Protocol) Integration

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚                  MCP ARCHITECTURE                        โ”‚
    โ”‚                                                         โ”‚
    โ”‚  MCPConnectionManager.tsx                               โ”‚
    โ”‚    โ”œโ”€โ”€ Server Discovery (config from settings.json)     โ”‚
    โ”‚    โ”‚     โ”œโ”€โ”€ stdio  โ†’ spawn child process               โ”‚
    โ”‚    โ”‚     โ”œโ”€โ”€ sse    โ†’ HTTP EventSource                  โ”‚
    โ”‚    โ”‚     โ”œโ”€โ”€ http   โ†’ Streamable HTTP                   โ”‚
    โ”‚    โ”‚     โ”œโ”€โ”€ ws     โ†’ WebSocket                         โ”‚
    โ”‚    โ”‚     โ””โ”€โ”€ sdk    โ†’ in-process transport              โ”‚
    โ”‚    โ”‚                                                    โ”‚
    โ”‚    โ”œโ”€โ”€ Client Lifecycle                                  โ”‚
    โ”‚    โ”‚     โ”œโ”€โ”€ connect โ†’ initialize โ†’ list tools          โ”‚
    โ”‚    โ”‚     โ”œโ”€โ”€ tool calls via MCPTool wrapper              โ”‚
    โ”‚    โ”‚     โ””โ”€โ”€ disconnect / reconnect with backoff        โ”‚
    โ”‚    โ”‚                                                    โ”‚
    โ”‚    โ”œโ”€โ”€ Authentication                                   โ”‚
    โ”‚    โ”‚     โ”œโ”€โ”€ OAuth 2.0 flow (McpOAuthConfig)            โ”‚
    โ”‚    โ”‚     โ”œโ”€โ”€ Cross-App Access (XAA / SEP-990)           โ”‚
    โ”‚    โ”‚     โ””โ”€โ”€ API key via headers                        โ”‚
    โ”‚    โ”‚                                                    โ”‚
    โ”‚    โ””โ”€โ”€ Tool Registration                                โ”‚
    โ”‚          โ”œโ”€โ”€ mcp__<server>__<tool> naming convention     โ”‚
    โ”‚          โ”œโ”€โ”€ Dynamic schema from MCP server              โ”‚
    โ”‚          โ”œโ”€โ”€ Permission passthrough to Claude Code       โ”‚
    โ”‚          โ””โ”€โ”€ Resource listing (ListMcpResourcesTool)     โ”‚
    โ”‚                                                         โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Bridge Layer (Claude Desktop / Remote)

    Claude Desktop / Web / Cowork          Claude Code CLI
    โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•            โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  Bridge Client    โ”‚  โ†โ”€ HTTP โ”€โ”€โ†’   โ”‚  bridgeMain.ts   โ”‚
    โ”‚  (Desktop App)    โ”‚                 โ”‚                  โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                 โ”‚  Session Manager โ”‚
                                          โ”‚  โ”œโ”€โ”€ spawn CLI   โ”‚
    PROTOCOL:                             โ”‚  โ”œโ”€โ”€ poll status  โ”‚
    โ”œโ”€ JWT authentication                 โ”‚  โ”œโ”€โ”€ relay msgs   โ”‚
    โ”œโ”€ Work secret exchange               โ”‚  โ””โ”€โ”€ capacityWake โ”‚
    โ”œโ”€ Session lifecycle                  โ”‚                  โ”‚
    โ”‚  โ”œโ”€โ”€ create                         โ”‚  Backoff:        โ”‚
    โ”‚  โ”œโ”€โ”€ run                            โ”‚  โ”œโ”€ conn: 2sโ†’2m  โ”‚
    โ”‚  โ””โ”€ stop                            โ”‚  โ””โ”€ gen: 500msโ†’30sโ”‚
    โ””โ”€ Token refresh scheduler            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Session Persistence

    SESSION STORAGE
    โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

    ~/.claude/projects/<hash>/sessions/
    โ””โ”€โ”€ <session-id>.jsonl           โ† append-only log
        โ”œโ”€โ”€ {"type":"user",...}
        โ”œโ”€โ”€ {"type":"assistant",...}
        โ”œโ”€โ”€ {"type":"progress",...}
        โ””โ”€โ”€ {"type":"system","subtype":"compact_boundary",...}

    RESUME FLOW:
    getLastSessionLog() โ”€โ”€> parse JSONL โ”€โ”€> rebuild messages[]
         โ”‚
         โ”œโ”€โ”€ --continue     โ†’ last session in cwd
         โ”œโ”€โ”€ --resume <id>  โ†’ specific session
         โ””โ”€โ”€ --fork-session โ†’ new ID, copy history

    PERSISTENCE STRATEGY:
    โ”œโ”€ User messages  โ†’ await write (blocking, for crash recovery)
    โ”œโ”€ Assistant msgs โ†’ fire-and-forget (order-preserving queue)
    โ”œโ”€ Progress       โ†’ inline write (dedup on next query)
    โ””โ”€ Flush          โ†’ on result yield / cowork eager flush

Feature Flag System

    DEAD CODE ELIMINATION (Bun compile-time)
    โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

    feature('FLAG_NAME')  โ”€โ”€โ†’  true  โ†’ included in bundle
                           โ”€โ”€โ†’  false โ†’ stripped from bundle

    FLAGS (observed in source):
    โ”œโ”€ COORDINATOR_MODE      โ†’ multi-agent coordinator
    โ”œโ”€ HISTORY_SNIP          โ†’ aggressive history trimming
    โ”œโ”€ CONTEXT_COLLAPSE      โ†’ context restructuring
    โ”œโ”€ DAEMON                โ†’ background daemon workers
    โ”œโ”€ AGENT_TRIGGERS        โ†’ cron/remote triggers
    โ”œโ”€ AGENT_TRIGGERS_REMOTE โ†’ remote trigger support
    โ”œโ”€ MONITOR_TOOL          โ†’ MCP monitoring tool
    โ”œโ”€ WEB_BROWSER_TOOL      โ†’ browser automation
    โ”œโ”€ VOICE_MODE            โ†’ voice input/output
    โ”œโ”€ TEMPLATES             โ†’ job classifier
    โ”œโ”€ EXPERIMENTAL_SKILL_SEARCH โ†’ skill discovery
    โ”œโ”€ KAIROS                โ†’ push notifications, file sends
    โ”œโ”€ PROACTIVE             โ†’ sleep tool, proactive behavior
    โ”œโ”€ OVERFLOW_TEST_TOOL    โ†’ testing tool
    โ”œโ”€ TERMINAL_PANEL        โ†’ terminal capture
    โ”œโ”€ WORKFLOW_SCRIPTS      โ†’ workflow tool
    โ”œโ”€ CHICAGO_MCP           โ†’ computer use MCP
    โ”œโ”€ DUMP_SYSTEM_PROMPT    โ†’ prompt extraction (ant-only)
    โ”œโ”€ UDS_INBOX             โ†’ peer discovery
    โ”œโ”€ ABLATION_BASELINE     โ†’ experiment ablation
    โ””โ”€ UPGRADE_NOTICE        โ†’ upgrade notifications

    RUNTIME GATES:
    โ”œโ”€ process.env.USER_TYPE === 'ant'  โ†’ Anthropic-internal features
    โ””โ”€ GrowthBook feature flags         โ†’ A/B experiments at runtime

State Management

    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚                  AppState Store                           โ”‚
    โ”‚                                                          โ”‚
    โ”‚  AppState {                                              โ”‚
    โ”‚    toolPermissionContext: {                              โ”‚
    โ”‚      mode: PermissionMode,           โ† default/plan/etc โ”‚
    โ”‚      additionalWorkingDirectories,                        โ”‚
    โ”‚      alwaysAllowRules,               โ† auto-approve      โ”‚
    โ”‚      alwaysDenyRules,                โ† auto-reject       โ”‚
    โ”‚      alwaysAskRules,                 โ† always prompt     โ”‚
    โ”‚      isBypassPermissionsModeAvailable                    โ”‚
    โ”‚    },                                                    โ”‚
    โ”‚    fileHistory: FileHistoryState,    โ† undo snapshots    โ”‚
    โ”‚    attribution: AttributionState,    โ† commit tracking   โ”‚
    โ”‚    verbose: boolean,                                     โ”‚
    โ”‚    mainLoopModel: string,           โ† active model       โ”‚
    โ”‚    fastMode: FastModeState,                              โ”‚
    โ”‚    speculation: SpeculationState                          โ”‚
    โ”‚  }                                                       โ”‚
    โ”‚                                                          โ”‚
    โ”‚  React Integration:                                      โ”‚
    โ”‚  โ”œโ”€โ”€ AppStateProvider   โ†’ creates store via createContext โ”‚
    โ”‚  โ”œโ”€โ”€ useAppState(sel)   โ†’ selector-based subscriptions   โ”‚
    โ”‚  โ””โ”€โ”€ useSetAppState()   โ†’ immer-style updater function   โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The 12 Progressive Harness Mechanisms

This source code demonstrates 12 layered mechanisms that a production AI agent harness needs beyond the basic loop. Each builds on the previous:

    s01  THE LOOP             "One loop & Bash is all you need"
         query.ts: the while-true loop that calls Claude API,
         checks stop_reason, executes tools, appends results.

    s02  TOOL DISPATCH        "Adding a tool = adding one handler"
         Tool.ts + tools.ts: every tool registers into the dispatch
         map. The loop stays identical. buildTool() factory provides
         safe defaults.

    s03  PLANNING             "An agent without a plan drifts"
         EnterPlanModeTool/ExitPlanModeTool + TodoWriteTool:
         list steps first, then execute. Doubles completion rate.

    s04  SUB-AGENTS           "Break big tasks; clean context per subtask"
         AgentTool + forkSubagent.ts: each child gets fresh messages[],
         keeping the main conversation clean.

    s05  KNOWLEDGE ON DEMAND  "Load knowledge when you need it"
         SkillTool + memdir/: inject via tool_result, not system prompt.
         CLAUDE.md files loaded lazily per directory.

    s06  CONTEXT COMPRESSION  "Context fills up; make room"
         services/compact/: three-layer strategy:
         autoCompact (summarize) + snipCompact (trim) + contextCollapse

    s07  PERSISTENT TASKS     "Big goals โ†’ small tasks โ†’ disk"
         TaskCreate/Update/Get/List: file-based task graph with
         status tracking, dependencies, and persistence.

    s08  BACKGROUND TASKS     "Slow ops in background; agent keeps thinking"
         DreamTask + LocalShellTask: daemon threads run commands,
         inject notifications on completion.

    s09  AGENT TEAMS          "Too big for one โ†’ delegate to teammates"
         TeamCreate/Delete + InProcessTeammateTask: persistent
         teammates with async mailboxes.

    s10  TEAM PROTOCOLS       "Shared communication rules"
         SendMessageTool: one request-response pattern drives
         all negotiation between agents.

    s11  AUTONOMOUS AGENTS    "Teammates scan and claim tasks themselves"
         coordinator/coordinatorMode.ts: idle cycle + auto-claim,
         no need for lead to assign each task.

    s12  WORKTREE ISOLATION   "Each works in its own directory"
         EnterWorktreeTool/ExitWorktreeTool: tasks manage goals,
         worktrees manage directories, bound by ID.

Key Design Patterns

PatternWherePurpose
AsyncGenerator streamingQueryEngine, query()Full-chain streaming from API to consumer
Builder + FactorybuildTool()Safe defaults for tool definitions
Branded TypesSystemPrompt, asSystemPrompt()Prevent string/array confusion
Feature Flags + DCEfeature() from bun:bundleCompile-time dead code elimination
Discriminated UnionsMessage typesType-safe message handling
Observer + State MachineStreamingToolExecutorTool execution lifecycle tracking
Snapshot StateFileHistoryStateUndo/redo for file operations
Ring BufferError logBounded memory for long sessions
Fire-and-Forget WriterecordTranscript()Non-blocking persistence with ordering
Lazy SchemalazySchema()Defer Zod schema evaluation for performance
Context IsolationAsyncLocalStoragePer-agent context in shared process

Build Notes

This source is not directly compilable from this repo alone:

  • Missing tsconfig.json, build scripts, and Bun bundler config
  • feature() calls are Bun compile-time intrinsics โ€” resolved during bundling
  • MACRO.VERSION is injected at build time
  • process.env.USER_TYPE === 'ant' sections are Anthropic-internal
  • The compiled cli.js is a self-contained 12MB bundle requiring only Node.js >= 18
  • Source maps (cli.js.map, 60MB) map back to these source files for debugging

See QUICKSTART.md for build instructions and workarounds.


License

All source code in this repository is copyright Anthropic and Claude. This repository is for technical research and education only. See the original npm package for full license terms.

Global Ranking

8.5
Trust ScoreMCPHub Index

Based on codebase health & activity.

Manual Config

{ "mcpServers": { "sanbuphy-claude-code-source-code": { "command": "npx", "args": ["sanbuphy-claude-code-source-code"] } } }