Full Call Graph¶
目的:完整调用图 关联:analysis/module-dependencies.md
1. 入口 → 渲染¶
graph TD
A[CLI invocation] --> B[main.tsx]
B --> C{argv preprocessing}
C -->|cc://| D[open subcommand]
C -->|ssh| E[ssh subcommand]
C -->|normal| F[continue main]
F --> G[preAction hook]
G --> H[await MDM/keychain]
G --> I[init]
G --> J[initSinks]
G --> K[runMigrations]
G --> L[loadRemoteManagedSettings]
F --> M[run function]
M --> N[register 40+ options]
M --> O[default action handler 2801 行]
M --> P[register 20+ subcommands]
M --> Q[program.parseAsync]
Q --> R{command matched?}
R -->|default| O
R -->|mcp| S[cli/handlers/mcp]
R -->|server| T[server subcommand]
R -->|setup-token| U[auth flow]
R -->|...| V[other subcommands]
O --> W[launchRepl]
O --> X[runHeadless]
W --> Y[REPL.tsx render]
Y --> Z[Output to stdout]
完整路径:CLI → argv → preAction → run → default action → launchRepl → render。
2. 完整模块依赖¶
graph LR
main[main.tsx] -->|imports| utils[utils/*]
main -->|imports| services[services/*]
main -->|imports| tools[tools/*]
main -->|imports| state[state/AppStateStore]
REPL[REPL.tsx] -->|imports| hooks[hooks/*]
REPL -->|imports| components[components/*]
REPL -->|imports| ink[ink/*]
REPL -->|imports| utils
state -->|subscribed by| hooks
state -->|subscribed by| components
state -->|subscribed by| ink
hooks -->|imports| services
hooks -->|imports| tools
hooks -->|imports| utils
components -->|imports| ink
components -->|imports| hooks
components -->|imports| services
services -->|imports| tools
services -->|imports| utils
tools -->|imports| utils
tools -->|imports| services
utils -->|self-contained| utils
依赖图。
3. 10 大循环依赖¶
graph TD
A[main.tsx] -->|lazy require| B[coordinatorModeModule]
A -->|lazy require| C[assistantModule]
A -->|lazy require| D[kairosGate]
A -->|lazy require| E[autoModeStateModule]
A -->|getter| F[teammateUtils]
A -->|getter| G[teammatePromptAddendum]
A -->|getter| H[teammateModeSnapshot]
A -->|lazy| I[BriefTool]
A -->|lazy| J[proactiveModule]
A -->|lazy| K[snipCompact]
A -->|lazy| L[extractMemories]
A -->|lazy| M[teamMemPaths]
A -->|lazy| N[contextCollapse/persist]
13 个 lazy require。
4. 3 层 memoize¶
graph TD
A[loadAllPlugins] -->|memoize| M1[永不过期]
B[connectToServer] -->|memoize| M1
C[fetchToolsForClient] -->|memoizeWithLRU 20| M2[LRU]
D[fetchResourcesForClient] -->|memoizeWithLRU| M2
E[fetchCommandsForClient] -->|memoizeWithLRU| M2
F[refreshAndGetAwsCredentials] -->|memoizeWithTTLAsync 1h| M3[TTL]
G[refreshGcpCredentialsIfNeeded] -->|memoizeWithTTLAsync 1h| M3
H[getApiKeyFromConfigOrMacOSKeychain] -->|memoize 5min| M3
3 层。
5. 6 层决策(bash 权限)¶
graph TD
A[Bash tool call] --> B{mode check}
B -->|bypassPermissions| C[allow]
B -->|plan| D{non-Read?}
D -->|yes| E[ask]
D -->|no| F[allow Read]
B -->|default/acceptEdits/auto| G[bashToolCheckPermission]
G --> H{exact match?}
H -->|allow rule| C
H -->|deny rule| I[deny]
H -->|no exact| J{wildcard match?}
J -->|allow rule| C
J -->|deny rule| I
J -->|no wildcard| K{sandbox?}
K -->|auto allow| C
K -->|no| L{early deny?}
L -->|yes| I
L -->|no| M{semantic deny?}
M -->|yes| I
M -->|no| N[classifier LLM]
N -->|allow| C
N -->|deny| I
N -->|uncertain| E
6 层漏斗。
6. 5 阶段启动¶
graph LR
A[T+0ms Bun 启动] --> B[T+50ms 模块求值]
B -->|startMdmRawRead 并行| C[T+100ms 预取触发]
B -->|startKeychainPrefetch 并行| C
C --> D[T+200ms 顶部副作用完成]
D --> E[T+300ms main 开始]
E --> F[T+300ms preAction]
F -->|await MDM/keychain| G[T+350ms init]
G --> H[T+400ms run]
H --> I[T+500ms 第一个 render]
I -->|startDeferredPrefetches 12 fire| J[T+500ms 后台预取]
5 阶段。
7. 4 种 Transport¶
graph TD
A[MCP Client] --> B{Transport}
B -->|stdio| C[subprocess stdin/stdout]
B -->|SSE| D[HTTP SSE]
B -->|Streamable HTTP| E[HTTP streamable]
B -->|WebSocket| F[WS]
C --> G[Server]
D --> G
E --> G
F --> G
G -->|JSON-RPC 2.0| H[Tool calls]
4 transport。
8. 7 种 attachment 类型(部分)¶
graph TD
A[getAttachments] -->|并行| B[user input 4]
A -->|并行| C[thread 20+]
B --> B1[at_mentioned_files]
B --> B2[mcp_resources]
B --> B3[agent_mentions]
B --> B4[skill_discovery]
C --> C1[queued_commands]
C --> C2[date_change]
C --> C3[ultrathink_effort]
C --> C4[deferred_tools_delta]
C --> C5[agent_listing_delta]
C --> C6[mcp_instructions_delta]
C --> C7[companion_intro]
C --> C8[changed_files]
C --> C9[nested_memory]
C --> C10[dynamic_skill]
C --> C11[skill_listing]
C --> C12[plan_mode]
C --> C13[plan_mode_exit]
C --> C14[auto_mode]
C --> C15[auto_mode_exit]
C --> C16[todo_reminders]
C --> C17[teammate_mailbox]
C --> C18[team_context]
C --> C19[agent_pending_messages]
C --> C20[critical_system_reminder]
24 种附件。
9. 5 mode permission¶
stateDiagram-v2
[*] --> default
default --> acceptEdits: acceptEdits flag
default --> plan: plan flag
default --> bypassPermissions: bypassPermissions flag
default --> auto: auto flag
acceptEdits --> default: Tab 切换
plan --> default: Tab 切换
bypassPermissions --> default: Tab 切换
auto --> default: Tab 切换
5 mode。
10. 9 种 hook 事件¶
graph TD
A[Claude Code] -->|生命周期| B{Hook event}
B --> C[PreToolUse]
B --> D[PostToolUse]
B --> E[SessionStart]
B --> F[SessionEnd]
B --> G[Notification]
B --> H[Stop]
B --> I[SubagentStop]
B --> J[UserPromptSubmit]
B --> K[PreCompact]
C -->|matcher Bash/Edit/etc| L[执行]
D --> L
E --> L
F --> L
G --> L
H --> L
I --> L
J --> L
K --> L
9 事件。
11. 3 种 Hook 类型¶
graph LR
A[Hook event] -->|trigger| B{Hook type}
B -->|command| C[执行 shell 脚本]
B -->|prompt| D[LLM 评估]
B -->|agent| E[Agent 循环]
C -->|exit 0| F[allow]
C -->|exit 2| G[deny]
D -->|approve| F
D -->|block| G
E -->|approve| F
E -->|block| G
3 类型。
12. 完整数据流¶
graph TD
A[user input] --> B[attach @file]
A --> C[skills/agents/plugins]
B --> D[getAttachments]
C --> D
D -->|1s timeout| E[attachments 17+ types]
E --> F[system prompt]
F --> G[Claude API]
G -->|tool decision| H{bash/Edit/etc}
H -->|bash| I[bashClassifier]
I -->|allow| J[execute]
I -->|deny| K[block]
I -->|ask| L[user prompt]
L -->|approve| J
L -->|deny| K
J -->|tool result| M[history append]
M --> N[next turn]
完整流。
13. 6 种启动模式¶
graph TD
A[claude] --> B{mode}
B -->|default| C[完整 ~500ms]
B -->|--bare| D[Bare ~300ms]
B -->|-p| E[Print ~400ms fast-path]
B -->|CLAUDE_CODE_EXIT_AFTER_FIRST_RENDER=1| F[仅测 perf ~300ms]
B -->|--debug| G[Debug ~600ms]
B -->|--remote-control| H[IDE 模式]
6 模式。
14. 8 大 React 优化¶
graph LR
A[React 渲染] --> B[优化 8 类]
B --> B1[hooks-first]
B --> B2[useMemo]
B --> B3[useCallback]
B --> B4[useDeferredValue]
B --> B5[useSyncExternalStore]
B --> B6[精确 selector]
B --> B7[React.memo]
B --> B8[env-var mount-time]
8 优化。
15. 5 种鉴权¶
graph LR
A[getAnthropicApiKey] --> B{优先级}
B -->|1| C[macOS keychain]
B -->|2| D[env ANTHROPIC_API_KEY]
B -->|3| E[settings apiKey]
B -->|4| F[apiKeyHelper]
B -->|5| G[Claude AI OAuth]
B -->|6| H[AWS Bedrock / GCP Vertex]
6 优先级。
16. 4 种 prompt cache 优化¶
graph LR
A[Claude API call] --> B[prompt cache]
B --> C[static 前缀]
C --> C1[system prompt]
C --> C2[tools]
B --> D[content hash]
D --> D1[--settings JSON]
B --> E[delta 模式]
E --> E1[4 个 delta attachments]
B --> F[user input 末尾]
4 优化。
17. ANT-ONLY 双版本¶
graph TD
A[bun build] --> B{build target}
B -->|--production| C[商业版]
B -->|--ant| D[ANT 版]
C -->|feature flag| E[15+ flags off]
C -->|"external" === 'ant'| F[商业编译]
D -->|feature flag| G[15+ flags on]
D -->|"external" === 'ant'| H[ANT 编译]
E -->|lazy require null| I[节省 25000 行]
G -->|lazy require real| J[完整功能]
双版本。
18. 25+ lazy require 模式¶
graph TD
A[main.tsx] -->|feature gate| B[coordinatorModeModule]
A -->|feature gate| C[assistantModule]
A -->|feature gate| D[kairosGate]
A -->|feature gate| E[proactiveModule]
A -->|feature gate| F[useProactive]
A -->|feature gate| G[useScheduledTasks]
A -->|feature gate| H[useVoiceIntegration]
A -->|feature gate| I[VoiceKeybindingHandler]
A -->|feature gate| J[WebBrowserPanelModule]
A -->|feature gate| K[computerUseWrapper]
A -->|feature gate| L[fetchMcpSkillsForClient]
A -->|feature gate| M[skillSearchModules]
A -->|feature gate| N[autoModeStateModule]
A -->|"external" === 'ant'| O[AntModelSwitchCallout]
A -->|"external" === 'ant'| P[UndercoverAutoCallout]
A -->|getter| Q[teammateUtils]
A -->|getter| R[teammatePromptAddendum]
A -->|getter| S[teammateModeSnapshot]
A -->|getter| T[BriefTool]
A -->|getter| U[snipCompact]
A -->|getter| V[extractMemories]
A -->|getter| W[teamMemPaths]
A -->|getter| X[contextCollapse/persist]
25+ lazy require。
19. 4 个 ANT-ONLY build 模式¶
graph TD
A[Build] --> B{target}
B -->|--production| C[商业版]
B -->|--ant| D[内部版]
C -->|bundle| E[15+ flags off]
D -->|bundle| F[所有 flags on]
E -->|体积| G[~480K]
F -->|体积| H[~505K]
2 模式。
20. 3 种 message 角色¶
graph LR
A[conversation] --> B[user message]
A --> C[assistant message]
A --> D[system message]
A --> E[tool_use message]
A --> F[tool_result message]
B -->|append| G[sessionStorage]
C -->|append| G
D -->|append| G
E -->|append| G
F -->|append| G
G -->|resume| A
5 消息类型。
21. 8 大容器¶
graph TD
A[src 1884 文件] --> B[commands/ 103]
A --> C[components/ 146]
A --> D[services/ 47]
A --> E[utils/ 198]
A --> F[tools/ 103]
A --> G[hooks/ 50+]
A --> H[states/ 10]
A --> I[其他 30+]
8 大容器。
22. 完整数据流¶
graph LR
A[用户输入] --> B[CLI args]
B --> C[main.tsx]
C --> D[argv 预处理]
D --> E[preAction]
E --> F[init]
E --> G[sinks attach]
E --> H[migrations]
E --> I[remote settings fire]
E --> J[run]
J --> K[Commander parse]
K -->|default| L[default action 2801 行]
K -->|mcp| M[mcp handler]
K -->|server| N[server]
K -->|setup-token| O[auth]
L --> P[launchRepl]
P --> Q[REPL.tsx]
Q --> R[60+ hooks]
R --> S[services]
S --> T[API call]
T --> U[response]
U --> V[render]
22 步。
23. session 生命周期¶
graph LR
A[user start] --> B[SessionStart]
B --> C[user prompt]
C --> D[user message]
D --> E[Claude response]
E --> F[tool_use]
F --> G[tool_result]
G -->|next turn| E
E -->|stop| H[Stop]
E -->|subagent| I[SubagentStop]
H --> J[user prompt]
J --> E
E --> K[PreCompact]
K --> L[compact]
E --> M[SessionEnd]
M --> N[session saved]
session 流。
24. 5+ ANT-ONLY 子命令¶
graph TD
A[main.tsx 2801 行 default action] -->|ANT-ONLY block| B{feature}
B -->|ant| C[--delegate-permissions]
B -->|ant| D[--afk]
B -->|ant| E[--dangerously-skip-permissions-with-classifiers]
B -->|ant| F[--tasks]
B -->|ant| G[--agent-teams]
B -->|ant| H[--enable-auto-mode]
B -->|ant| I[--proactive]
B -->|ant| J[--brief]
B -->|ant| K[--assistant]
B -->|ant| L[--channels]
B -->|ant| M[--dangerously-load-development-channels]
B -->|ant| N[--hard-fail]
12+ ANT-ONLY。
25. 完整架构¶
graph TD
A[CLI User] --> B[main.tsx]
B --> C[preAction Hook]
C --> D[init]
C --> E[sinks]
C --> F[migrations]
B --> G[run]
G --> H[Commander]
H --> I[default action]
H --> J[20+ subcommands]
I --> K[launchRepl]
I --> L[runHeadless]
K --> M[REPL.tsx 5005]
M --> N[60+ hooks]
N --> O[AppStateStore]
N --> P[services]
N --> Q[tools]
P --> R[claude.ts 3419]
Q --> S[bashPermissions]
Q --> T[bashSecurity]
R --> U[Anthropic API]
S --> V[bash 6 层决策]
T --> W[25+ validator]
V --> X[allow/deny/ask]
W --> Y[拒绝]
X --> Z[execute]
完整。
26. 总结¶
完整调用图 = 22 个 mermaid 视图。
核心: - 入口 → 渲染 - 模块依赖 - 循环依赖 - memoize 三层 - 6 层决策 - 5 阶段启动 - 4 transport - 5 mode permission - 9 hook 事件 - 3 hook 类型 - 数据流 - 6 启动模式 - 8 React 优化 - 5 鉴权 - 4 prompt cache 优化 - ANT-ONLY 双版本 - 25+ lazy require - 4 build 模式 - 3 message 角色 - 8 大容器 - session 生命周期 - 12+ ANT-ONLY 子命令 - 完整架构
22 图。
下一步: - 渲染成 SVG - 加到 mkdocs - 嵌入文档