ZyVOP Logo
Content That Connects
SeriesAI NewsWhy ZyVOPJoin Discord
ZyVOP Logo
Content That Connects

Empowering developers and creators with cutting-edge insights, comprehensive tutorials, and innovative solutions for the digital future.

Content

  • Categories
  • Tags
  • Badges
  • Leaderboard
  • Write Article
  • Newsletter

Company

  • About Us
  • Why ZyVOP
  • API Documentation
  • Write for Us
  • Contact

Connect

  • Privacy Policy
  • Terms of Service
  • Cookie Policy
  • DMCA Policy
  • Code of Conduct

ยฉ 2026 ZyVOP. Crafted with care for the developer community.

Made with โค๏ธ by the ZyVOP team
All systems operational
HomeClaude Code v2.1.71: Complete Architecture Analysis Report

Claude Code v2.1.71: Complete Architecture Analysis Report

I0veD
I0veDcyber security researcher
August 12, 2026
22 min read
Claude Code v2.1.71: Complete Architecture Analysis Report
Article

The reason is that we need to use claude-sdk to do some agent business development. , so it needs to be analyzed, but I am very troubled by their closed source encryption.

Claude Code is a single-file executable program packaged by Bun, with a size of 183MB. All JS code is stuffed in the __BUN Mach-O section, 4945 modules, all obfuscated and compressed. Even if --bytecode is used, Bun still packages the complete JS source code together (bytecode only starts acceleration)

Later it was discovered that this project can be used to decrypt claudecode. Interested experts can decrypt and analyze it by themselves.

Version: 2.1.71
Build time: 2026-03-06T22:45:36Z
package name: @anthropic-ai/claude-code
Analysis date: 2026-03-09
Analysis tools: bun-demincer


Table of contents

  1. Overall architecture overview

  1. Module system principle

  1. Detailed explanation of startup process

  1. Agent cycle architecture

  1. API interaction mechanism

  1. Detailed explanation of tool system

  1. Permissions and security model

  1. Hook system

  1. context management

  1. UI framework and rendering

  1. Authentication system

  1. MCP integration

  1. Plug-ins and Marketplace

  1. telemetry system

  1. Enterprise Strategy Function

  1. Configuring the file system

  1. subagent system

  1. Session management

  1. Git integration

  1. Automatic update mechanism

  1. Key constants and configurations

  1. security features

  1. Performance optimization strategies

  1. Dependency analysis


1. Overall architecture overview

1.1 Packaging structure

cli.js (12.2MB ๅ•ๆ–‡ไปถ)
โ”‚
โ”œโ”€โ”€ Bun bundler ๆ‰“ๅŒ…็š„ 4,697 ไธชๆจกๅ—
โ”‚   โ”œโ”€โ”€ 2,894 ไธชๅบ”็”จๆจกๅ— (Claude Code ่‡ช่บซไปฃ็ )
โ”‚   โ””โ”€โ”€ 1,805 ไธช็ฌฌไธ‰ๆ–นๆจกๅ— (vendor)
โ”‚
โ”œโ”€โ”€ ๆจกๅ—็ณป็ปŸ่ฟ่กŒๆ—ถ
โ”‚   โ””โ”€โ”€ C() / k() / m1() / e() / W3()
โ”‚
โ””โ”€โ”€ ่ต„ๆบๆ–‡ไปถ
    โ”œโ”€โ”€ resvg.wasm (2.4MB) - SVGๆธฒๆŸ“
    โ”œโ”€โ”€ tree-sitter.wasm (201KB) - ไปฃ็ ่งฃๆž
    โ””โ”€โ”€ tree-sitter-bash.wasm (1.3MB) - Bash่งฃๆž

1.2 High-level architecture diagram

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                         CLI ๅ…ฅๅฃๅฑ‚ (cli.js)                          โ”‚
โ”‚                     Node.js ๅ•ๆ–‡ไปถๅฏๆ‰ง่กŒ็จ‹ๅบ                          โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  00-runtime.js โ€” ๆจกๅ—็ณป็ปŸ่ฟ่กŒๆ—ถ                                       โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚ C() = CommonJS ๆจกๅ—ๅทฅๅŽ‚ (ๆƒฐๆ€งๅŠ ่ฝฝ)                               โ”‚ โ”‚
โ”‚  โ”‚ k() = ESM ๆจกๅ—ๅˆๅง‹ๅŒ–ๅ™จ (ๅ‰ฏไฝœ็”จๆ‰ง่กŒ)                               โ”‚ โ”‚
โ”‚  โ”‚ m1() = ๅฏผๅ‡บ็ป‘ๅฎš็ณป็ปŸ (ๅŠจๆ€ getter)                               โ”‚ โ”‚
โ”‚  โ”‚ e() / W3() = CJSโ†”ESM ไบ’ๆ“ไฝœๆ€ง่ฝฌๆข                               โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  4696.js (Waq) โ€” ไธปๅ…ฅๅฃๆจกๅ— (Commander.js CLI)                       โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚   mcp ๅ‘ฝไปค        โ”‚   auth ๅ‘ฝไปค        โ”‚   plugin ๅ‘ฝไปค           โ”‚ โ”‚
โ”‚  โ”‚ โ”œโ”€ serve         โ”‚ โ”œโ”€ login          โ”‚ โ”œโ”€ validate            โ”‚ โ”‚
โ”‚  โ”‚ โ”œโ”€ add-json      โ”‚ โ”œโ”€ status         โ”‚ โ”œโ”€ list                โ”‚ โ”‚
โ”‚  โ”‚ โ”œโ”€ list          โ”‚ โ”œโ”€ logout         โ”‚ โ”œโ”€ install             โ”‚ โ”‚
โ”‚  โ”‚ โ”œโ”€ get           โ”‚ โ””โ”€ SSO ๆ”ฏๆŒ        โ”‚ โ”œโ”€ marketplace         โ”‚ โ”‚
โ”‚  โ”‚ โ””โ”€ remove        โ”‚                   โ”‚ โ””โ”€ skills              โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                        ๆ ธๅฟƒๆœๅŠกๅฑ‚                                    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚  โ”‚  Auth    โ”‚   MCP    โ”‚  Tool    โ”‚  Agent   โ”‚      UI          โ”‚   โ”‚
โ”‚  โ”‚  OAuth   โ”‚  Server  โ”‚  System  โ”‚  Loop    โ”‚  React/Ink TUI   โ”‚   โ”‚
โ”‚  โ”‚  APIKey  โ”‚  Stdio   โ”‚  15+ๅทฅๅ…ท  โ”‚  Stream  โ”‚  ็ป„ไปถๅŒ–ๆธฒๆŸ“       โ”‚   โ”‚
โ”‚  โ”‚  SSO     โ”‚  SSE     โ”‚  ๆƒ้™ๆจกๅž‹  โ”‚  ๅญไปฃ็†   โ”‚  Hooks + Context โ”‚   โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

1.3 Module statistics

index

numerical value

illustrate

Total number of modules

4,697

Contains vendor

application module

2,894

Claude Code own code

Vendor module

1,805

third party dependencies

Vendor bag

127

Recognized npm packages

Number of dependent edges

16,740

Calling relationships between modules

average dependency

3.6

Average number of dependencies per module


2. Principle of module system

Claude Code uses Bun bundler to package all source code into a single file and implement module loading through a custom runtime.

2.1 Detailed explanation of core functions

2.1.1 C() - CommonJS module factory

C = (A, q) => () => (
  q || A((q = {exports: {}}).exports, q),
  q.exports
)

Working principle:
1. Receive module functionAand cache variablesq2. Return a lazy-evaluated factory function
3. Execute the module function when called for the first time and cache the results
4. Subsequent calls directly return the cachedexports

Usage scenarios: wraps vendor libraries (axios, semver, etc.) and CJS format modules

Example:

var axios = C((module, exports) => {
  // axios ๆจกๅ—ไปฃ็ 
  module.exports = { /* ... */ };
});

2.1.2 k() - ESM module initializer

k = (A, q) => () => (A && (q = A(A = 0)), q)

Working principle:
1. Receive initialization functionAand cache variablesq2. Executed when called for the first timeA(0)(0 means executed)
3. Cache the return value toq4. Subsequent calls return the cached value

Usage scenarios: Side-effect execution and export binding of ESM module

Example:

var myModule = k(() => {
  // ๆจกๅ—ๅˆๅง‹ๅŒ–ไปฃ็ 
  Dependency1();  // ่งฆๅ‘ไพ่ต–ๅˆๅง‹ๅŒ–
  Dependency2();

// ็ป‘ๅฎšๅฏผๅ‡บ
m1(exports, {
export1: () => value1,
export2: () => value2,
});

return exports;
});

2.1.3 m1() - Export binding system

m1 = (A, q) => {
  for (var K in q)
    cy6(A, K, {
      get: q[K],
      enumerable: !0,
      configurable: !0,
      set: Qtq.bind(q, K)
    });
};

Working principle:
1. For the target audienceAAdd properties
2. The getter of each attribute points toq[K]function
3. Support dynamic rebinding
4. Configurable and enumerable

use: Implement named export similar to ESM

Example:

var exports = {};
m1(exports, {
  mainFunction: () => mainFn,
  helperFunction: () => helperFn,
  CONST_VALUE: () => 42,
});

2.1.4 e() / W3() - Module interoperability

// e() - CommonJS ่ฝฌ ESM
e = (A) => A && A.__esModule ? A : {default: A}

// W3() - ESM wrapper
W3 = (A) => ({
__esModule: true,
get default() { return A; },
// ... ๅ…ถไป–ๅฏผๅ‡บ
})

2.2 Module dependency graph structure

DAG (ๆœ‰ๅ‘ๆ— ็Žฏๅ›พ) ไพ่ต–็ป“ๆž„:

rm1 (ๅ…จๅฑ€็Šถๆ€ๅ…ฅๅฃ)
โ†“
yy โ†’ Z46 โ†’ tAA โ†’ G46
โ†“ โ†“
A7A โ† Pf โ† M11
โ†“
K7A โ†’ w7A
โ†“
$7A (่šๅˆๆจกๅ—)
โ†“
yo โ†’ iy6 โ†’ J7A โ† T7A
โ†“
ry6, I7A (็ปˆ็ซฏ/ๆธฒๆŸ“)

Features:
- Depth first execution order
- Circular dependencies are resolved through lazy evaluation
- Approximately 4,697 independent module nodes

2.3 Module numbering rules

Number range

type

illustrate

00-runtime.js

runtime

Module system core definition

0001-289x.js

application module

Claude Code own code

3000-4697.js

application module

More application modules

vendor/*.js

third party

npm packages, organized by package name


3. Detailed explanation of startup process

3.1 Main function execution flow

Node.js ๆ‰ง่กŒ cli.js
  โ†“
ใ€้˜ถๆฎต1ใ€‘็Žฏๅขƒๅˆๅง‹ๅŒ– (4696.js:288-297)
  โ”œโ”€ Uq("main_function_start") - ไบ‹ไปถ่ฎฐๅฝ•
  โ”œโ”€ process.env.NoDefaultCurrentDirectoryInExePath = "1"
  โ”œโ”€ txq() - ่ญฆๅ‘Šๅค„็†ๅˆๅง‹ๅŒ–
  โ””โ”€ SIGINT ไฟกๅทๅค„็†ๅ™จๆณจๅ†Œ
  โ†“
ใ€้˜ถๆฎต2ใ€‘ๅ‚ๆ•ฐๆฃ€ๆต‹ (4696.js:298-322)
  โ”œโ”€ -p / --print ๆ ‡ๅฟ— (JSON ่พ“ๅ‡บๆจกๅผ)
  โ”œโ”€ --init-only ๆ ‡ๅฟ—
  โ”œโ”€ --sdk-url ๆ ‡ๅฟ— (WebSocket ๆจกๅผ)
  โ”œโ”€ ็ฆ็”จ TTY ๆฃ€ๆต‹
  โ””โ”€ ้ข„่งˆๆ ผๅผๆฃ€ๆต‹ (markdown/html)
  โ†“
ใ€้˜ถๆฎต3ใ€‘ๅ…ฅๅฃ็‚น่ฏ†ๅˆซ (4696.js:305-318)
  โ”‚
  โ”œโ”€ GITHUB_ACTIONS โ†’ "github-action"
  โ”œโ”€ CLAUDE_CODE_ENTRYPOINT ็Žฏๅขƒๅ˜้‡
  โ”‚   โ”œโ”€ "sdk-typescript" / "sdk-python" / "sdk-cli"
  โ”‚   โ”œโ”€ "claude-vscode"
  โ”‚   โ”œโ”€ "claude-desktop"
  โ”‚   โ”œโ”€ "local-agent"
  โ”‚   โ””โ”€ "remote" (SESSION_ACCESS_TOKEN ๆฃ€ๆต‹)
  โ””โ”€ ้ป˜่ฎค: "cli"
  โ†“
ใ€้˜ถๆฎต4ใ€‘ๅฎขๆˆท็ซฏ็ฑปๅž‹็กฎๅฎš (4696.js:319)
  โ””โ”€ ig1(_) - ่ฎพ็ฝฎๅ…จๅฑ€็Šถๆ€
  โ†“
ใ€้˜ถๆฎต5ใ€‘ไธป่ฟ่กŒๅ‡ฝๆ•ฐ่ฐƒ็”จ (4696.js:328)
  โ””โ”€ await Paz() - ๅผ‚ๆญฅๅ‘ฝไปคๅค„็†

3.2 Command processing function Paz()

Location: 4696.js, line 380

async function Paz() {
  // 1. ๅˆ›ๅปบ Commander.js ๅฎžไพ‹
  let q = new Command();

// 2. ้…็ฝฎๅ…จๅฑ€้€‰้กน
q
.version("2.1.71 (Claude Code)")
.option("-p, --print", "JSON ๆต่พ“ๅ‡บๆจกๅผ")
.option("-w, --worktree [name]", "Git worktree ๆ”ฏๆŒ")
.option("--tmux", "Tmux ไผš่ฏๅˆ›ๅปบ")
.option("--enable-auto-mode", "่‡ชๅŠจๆจกๅผ๏ผˆ้š่—๏ผ‰")
.option("--agent-id <id>", "ๅคš Agent ๆ”ฏๆŒ๏ผˆ้š่—๏ผ‰")
.option("--team-name <name>", "ๅ›ข้˜Ÿๅ่ฐƒ๏ผˆ้š่—๏ผ‰");

// 3. ๆณจๅ†Œๅญๅ‘ฝไปค
registerMcpCommand(q); // mcp/
registerAuthCommand(q); // auth/
registerPluginCommand(q); // plugin/
registerAgentsCommand(q); // agents
registerDoctorCommand(q); // doctor
registerUpdateCommand(q); // update

// 4. ๅผ‚ๆญฅ่งฃๆžๅ‘ฝไปค่กŒ
await q.parseAsync(process.argv);
}

3.3 Supported entry point types

entry point

Detection conditions

use

cli

default

Standard command line interface

github-action

GITHUB_ACTIONS environment variable

GitHub Actions integration

sdk-typescript

SDK_TYPESCRIPT_MODE

TypeScript SDK calls

sdk-python

SDK_PYTHON_MODE

Python SDK calls

sdk-cli

SDK_CLI_MODE

CLI SDK calls

claude-vscode

VSCODE_EXTENSION

VS Code extension

claude-desktop

DESKTOP_APP

desktop application

local-agent

LOCAL_AGENT

local proxy mode

remote

SESSION_ACCESS_TOKEN

remote session


4. Agent cycle architecture

4.1 Main cycle flow chart

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                          ็”จๆˆท่พ“ๅ…ฅ                                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    ๆถˆๆฏ้ข„ๅค„็† + ไธŠไธ‹ๆ–‡ๆณจๅ…ฅ                             โ”‚
โ”‚  โ”œโ”€ CLAUDE.md ๅŠ ่ฝฝ                                                  โ”‚
โ”‚  โ”œโ”€ Git ็Šถๆ€ๆณจๅ…ฅ                                                    โ”‚
โ”‚  โ”œโ”€ ๅฝ“ๅ‰ๆ—ฅๆœŸๆณจๅ…ฅ                                                    โ”‚
โ”‚  โ””โ”€ ็”จๆˆทๅ…จๅฑ€ๆŒ‡ไปค                                                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                      ็ณป็ปŸๆ็คบๆž„ๅปบ                                     โ”‚
โ”‚  โ”œโ”€ ๅทฅๅ…ทๅˆ—่กจ (ๅ†…็ฝฎ + MCP)                                            โ”‚
โ”‚  โ”œโ”€ ๆƒ้™ๆจกๅผ่ฏดๆ˜Ž                                                    โ”‚
โ”‚  โ”œโ”€ ่พ“ๅ‡บๆ ผๅผ่ฆๆฑ‚                                                    โ”‚
โ”‚  โ””โ”€ Beta ๅŠŸ่ƒฝๆ ‡ๅฟ—                                                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—                       โ”‚
โ”‚                    โ•‘   Agent ไธปๅพช็Žฏๅผ€ๅง‹        โ•‘                       โ”‚
โ”‚                    โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•                       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ใ€ๆญฅ้ชค1ใ€‘ๅพฎ็ดงๅ‡‘ (microcompact)                                       โ”‚
โ”‚  ๆธ…็†ๅ†—ไฝ™ๆถˆๆฏๅ…ƒ็ด ใ€ๅˆๅนถ้‡ๅคๅ†…ๅฎน                                       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ใ€ๆญฅ้ชค2ใ€‘่‡ชๅŠจ็ดงๅ‡‘ๆฃ€ๆŸฅ                                                โ”‚
โ”‚  if (tokenCount > threshold) {                                       โ”‚
โ”‚    autocompact(messages) โ†’ ็”จ LLM ๆ‘˜่ฆๅŽ‹็ผฉๅކๅฒ                        โ”‚
โ”‚  }                                                                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ใ€ๆญฅ้ชค3ใ€‘API ่ฐƒ็”จ                                                    โ”‚
โ”‚  messages.create({                                                   โ”‚
โ”‚    model, messages, system, tools,                                   โ”‚
โ”‚    thinking, betas, stream, ...                                     โ”‚
โ”‚  })                                                                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ใ€ๆญฅ้ชค4ใ€‘ๆตๅผไบ‹ไปถๅค„็†                                                โ”‚
โ”‚  for await (event of stream) {                                      โ”‚
โ”‚    switch (event.type) {                                            โ”‚
โ”‚      case "message_start":     โ†’ ๅˆๅง‹ๅŒ–, ่ฎฐๅฝ• TTFT                   โ”‚
โ”‚      case "content_block_start":  โ†’ ๅผ€ๅง‹ๆ–ฐๅ—                        โ”‚
โ”‚      case "content_block_delta":  โ†’ ๅขž้‡ๅ†…ๅฎน                        โ”‚
โ”‚      case "content_block_stop":   โ†’ ๅ—ๅฎŒๆˆ                         โ”‚
โ”‚      case "message_delta":       โ†’ ไฝฟ็”จ้‡ๆ›ดๆ–ฐ                      โ”‚
โ”‚      case "message_stop":       โ†’ ๆตๅฎŒๆˆ                          โ”‚
โ”‚    }                                                                 โ”‚
โ”‚  }                                                                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ใ€ๆญฅ้ชค5ใ€‘ๅทฅๅ…ทๆ‰ง่กŒๆฃ€ๆŸฅ                                                โ”‚
โ”‚  if (responseๅŒ…ๅซ tool_use) {                                        โ”‚
โ”‚    for each tool_use {                                               โ”‚
โ”‚      โ”œโ”€ ๆƒ้™ๆฃ€ๆŸฅ (allow/deny/ask)                                   โ”‚
โ”‚      โ”œโ”€ Hook ่งฆๅ‘ (PreToolUse)                                      โ”‚
โ”‚      โ”œโ”€ ๅทฅๅ…ทๆ‰ง่กŒ                                                     โ”‚
โ”‚      โ”œโ”€ Hook ่งฆๅ‘ (PostToolUse)                                     โ”‚
โ”‚      โ””โ”€ ็ป“ๆžœๆณจๅ…ฅๆถˆๆฏ โ†’ ็ปง็ปญๅพช็Žฏ                                      โ”‚
โ”‚    }                                                                 โ”‚
โ”‚  } else {                                                            โ”‚
โ”‚    ้€€ๅ‡บๅพช็Žฏ                                                          โ”‚
โ”‚  }                                                                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ†“
                         (ๅพช็Žฏ็ปง็ปญ)

4.2 Core loop code location

components

file location

function/module

Main loop entrance

3549.js

xC()

Message processing

3773.js

gzz()

API calls

4286.js

iSq(), nS()

Stream processing

4286.js

Event loop (lines 621-813)

tool execution

3768.js

Da6kind

4.3 Loop control variables

{
  maxIterations: 100,        // ๆœ€ๅคง่ฟญไปฃๆฌกๆ•ฐ
  stopOnFirstError: false,   // ้”™่ฏฏๆ—ถๅœๆญข
  compactThreshold: 0.8,     // ๅŽ‹็ผฉ้˜ˆๅ€ผ (80% ไธŠไธ‹ๆ–‡)
  thinkingBudget: 1024,      // ๆ€่€ƒ้ข„็ฎ— (tokens)
  streamIdleTimeout: 60000,  // ๆต็ฉบ้—ฒ่ถ…ๆ—ถ (60็ง’)
}

5. API interaction mechanism

5.1 API client creation

Location: 4286.js, lines 560-597

async function eb({ maxRetries, model, source }) {
  // 1. ็กฎๅฎšๅŽ็ซฏ็ฑปๅž‹
  const backend = D7(); // "firstParty" | "bedrock" | "vertex"

// 2. ่Žทๅ–่ฎค่ฏไฟกๆฏ
const apiKey = await getApiKey();

// 3. ๅˆ›ๅปบๅฎขๆˆท็ซฏๅฎžไพ‹
const client = new Anthropic({
apiKey,
maxRetries,
baseURL: getBaseUrl(backend),
defaultHeaders: {
"anthropic-version": "2023-06-01",
...(getBetaHeaders()),
},
});

return client;
}

5.2 API call parameter structure

{
  // ๆจกๅž‹้…็ฝฎ
  model: "claude-sonnet-4-6" | "claude-opus-4-6" | "claude-haiku",

// ๆถˆๆฏๅ†…ๅฎน
messages: normalizeMessages(rawMessages),

// ็ณป็ปŸๆ็คบ (ๅธฆ็ผ“ๅญ˜ๆŽงๅˆถ)
system: [
{ type: "text", text: "...", cache_control: { type: "ephemeral" } },
{ type: "text", text: "..." }
],

// ๅทฅๅ…ทๅฎšไน‰
tools: [
...builtInTools,
...mcpTools,
...customTools
],

// ๅทฅๅ…ท้€‰ๆ‹ฉ็ญ–็•ฅ
tool_choice: "auto" | { type: "tool", name: "..." } | any,

// ่พ“ๅ‡บ้…็ฝฎ
max_tokens: 16384,

// ๆ€่€ƒๆจกๅผ้…็ฝฎ
thinking: modelSupportsAdaptive(model)
? { type: "adaptive" }
: { type: "enabled", budget_tokens: N },

// Beta ๅŠŸ่ƒฝ
betas: [
"prompt-caching-2025-07-01", // ๆ็คบ็ผ“ๅญ˜
"interleaved-thinking-2025-05-14", // ไบค้”™ๆ€่€ƒ
"token-counting-2025-07-01", // ไปค็‰Œ่ฎกๆ•ฐ
"server-side-tools-2025-07-01" // ๆœๅŠกๅ™จ็ซฏๅทฅๅ…ท
],

// ๅ…ƒๆ•ฐๆฎ
metadata: {
user_id: getUserId()
},

// ๆตๅผๆŽงๅˆถ
stream: true,

// ่ฏทๆฑ‚ไธญๆญขไฟกๅท
signal: abortController.signal
}

5.3 Detailed explanation of streaming event types

event type

Trigger time

processing action

message_start

stream starts

Initialization state, recording TTFT start time

content_block_start

New content block begins

Create block accumulator

content_block_delta

content increment

Append text/thinking/tool โ€‹โ€‹input

thinking_delta

Think incrementally

Update thought buffer

content_block_stop

end of block

completion block, added to response

message_delta

Message metadata update

Update usage statistics

message_stop

end of stream

Complete the response and trigger the callback

5.4 Stream fault tolerance mechanism

// ๆต็ฉบ้—ฒ็›‘ๆŽง
const IDLE_WARNING = 30000;  // 30็ง’่ญฆๅ‘Š
const IDLE_TIMEOUT = 60000;  // 60็ง’่ถ…ๆ—ถ

let lastEventTime = Date.now();

const idleCheck = setInterval(() => {
const idle = Date.now() - lastEventTime;
if (idle > IDLE_TIMEOUT) {
stream.abort();
logError("Stream idle timeout");
} else if (idle > IDLE_WARNING) {
logWarning("Stream idle warning");
}
}, 10000);

stream.on('data', () => {
lastEventTime = Date.now();
});

5.5 Multiple backend support

rear end

detection function

Base URL

special handling

First Party

isFirstParty()

api.anthropic.com

Full feature support

Bedrock

isBedrock()

bedrock-runtime..amazonaws.com

Inference profile

Vertex AI

isVertex()

-aiplatform.googleapis.com

Beta filtering


6. Detailed explanation of tool system

6.1 Complete list of built-in tools

6.1.1 File operation tools

Tool name

Module ID

type

Concurrency safety

Function description

Read

2545.js

read only

โœ“

Read files, images, PDFs, Jupyter notebooks

Write

3643.js

write

โœ—

Create or overwrite file contents

Edit

4285.js

write

โœ—

Exact string replacementEdit

NotebookEdit

1245.js

write

โœ—

Edit Jupyter notebook cells

6.1.2 Search tools

Tool name

Module ID

type

Concurrency safety

Function description

Glob

1244.js

read only

โœ“

File pattern matching (glob syntax)

Grep

1244.js

read only

โœ“

Regular search based on ripgrep

LSP

3724.js

read only

โœ“

Language server protocol code analysis

ToolSearch

2545.js

read only

โœ“

Search available tools

6.1.3 Network tools

Tool name

Module ID

type

Concurrency safety

Function description

WebFetch

(condition)

read only

โœ“

Get web content and process it with AI

WebSearch

(condition)

read only

โœ“

web search

6.1.4 Execution tools

Tool name

Module ID

type

Concurrency safety

Function description

Bash

(condition)

write

โœ—

Execute shell command

PowerShell

(condition)

write

โœ—

Execute PowerShell commands

6.1.5 Interactive tools

Tool name

Module ID

type

Function description

AskUserQuestion

2782.js

interaction

Ask user questions/confirm

EnterPlanMode

(built-in)

interaction

Enter planning mode

6.1.6 Agent tools

Tool name

Module ID

Function description

Task

3603.js

Launch subagents to perform complex tasks

6.1.7 Task management tools

Tool name

Module ID

Function description

TaskCreate

2548.js

Create new task

TaskUpdate

2548.js

Update task status

TaskGet

2783.js

Get task details

TaskList

2783.js

list all tasks

TaskStop

0955.js

Stop task execution

TaskOutput

0955.js

Read background task output

6.1.8 MCP Tools

Tool name

Module ID

Function description

ListMcpResourcesTool

3727.js

List MCP resources

ReadMcpResourceTool

3731.js

Read MCP resources

TodoWrite

2410.js

Manage task list

6.2 Tool definition interface

interface ToolDefinition {
  // === ๆ ธๅฟƒๆ ‡่ฏ† ===
  name: string;                          // ๅทฅๅ…ทๅ”ฏไธ€ๆ ‡่ฏ†็ฌฆ
  userFacingName: () => string;          // ็”จๆˆทๆ˜พ็คบๅ็งฐ
  searchHint: string;                    // ๆœ็ดขๆ็คบๆ–‡ๆœฌ

// === ๆ่ฟฐๅ’Œๆ–‡ๆกฃ ===
description: () => Promise<string>; // ่ฏฆ็ป†ๆ่ฟฐ (ๅผ‚ๆญฅ็”Ÿๆˆ)
prompt: () => Promise<string>; // ็ณป็ปŸๆ็คบไธญ็š„่ฏดๆ˜Ž

// === ๆจกๅผๅฎšไน‰ ===
inputSchema: () => ZodSchema; // Zod ่พ“ๅ…ฅ้ชŒ่ฏๆจกๅผ
outputSchema: () => ZodSchema; // Zod ่พ“ๅ‡บ้ชŒ่ฏๆจกๅผ
inputParamAliases?: Record<string, string>; // ๅ‚ๆ•ฐๅˆซๅ

// === ๆ‰ง่กŒๆŽงๅˆถ ===
isEnabled: () => boolean; // ๆ˜ฏๅฆๅฏ็”จ
isConcurrencySafe: (input?) => boolean; // ๅนถๅ‘ๅฎ‰ๅ…จๆ€ง
isReadOnly: () => boolean; // ๅช่ฏปๆ ‡ๅฟ—

// === ๆƒ้™ๆฃ€ๆŸฅ ===
checkPermissions: (
input: unknown,
context: ToolContext
) => Promise<{
behavior: "allow" | "deny" | "ask";
updatedInput?: unknown;
reason?: string;
}>;

// === ๆ‰ง่กŒ ===
call: (
input: unknown,
context: ToolContext,
appState: AppState,
progressCallback?: (progress) => void
) => Promise<{ data: unknown }>;

validateInput?: (
input: unknown,
context: ToolContext
) => Promise<ValidationResult>;

// === ๆธฒๆŸ“ๅ’Œๆ˜พ็คบ ===
renderToolUseMessage?: (input) => string | null;
renderToolUseProgressMessage?: (progress) => JSX.Element;
renderToolResultMessage?: (result, ...) => JSX.Element;
renderToolUseRejectedMessage?: () => JSX.Element;
renderToolUseErrorMessage?: (error) => JSX.Element;

// === ไฝฟ็”จๆ€ป็ป“ ===
getToolUseSummary?: (input) => string;
getActivityDescription?: (input) => string;

// === ๅˆ†็ฑปๅ™จ่พ“ๅ…ฅ ===
toAutoClassifierInput: (input) => string;
}

6.3 Tool execution process

็”จๆˆท่ฏทๆฑ‚ๅทฅๅ…ทไฝฟ็”จ
  โ†“
ใ€ๆญฅ้ชค1ใ€‘่พ“ๅ…ฅ้ชŒ่ฏ
  โ”œโ”€ inputSchema.safeParse(input)
  โ””โ”€ validateInput(input, context)
  โ†“
ใ€ๆญฅ้ชค2ใ€‘ๆƒ้™ๆฃ€ๆŸฅ
  โ””โ”€ checkPermissions(input, context)
      โ”œโ”€ "allow" โ†’ ็ปง็ปญ
      โ”œโ”€ "deny" โ†’ ๆ‹’็ป๏ผŒ่ฟ”ๅ›ž้”™่ฏฏ
      โ””โ”€ "ask" โ†’ ็ญ‰ๅพ…็”จๆˆท็กฎ่ฎค
  โ†“
ใ€ๆญฅ้ชค3ใ€‘ๅนถๅ‘ๅฎ‰ๅ…จๆฃ€ๆŸฅ
  โ”œโ”€ isConcurrencySafe(input)
  โ”œโ”€ ๆ˜ฏ โ†’ ๅฏๅนถๅ‘ๆ‰ง่กŒ
  โ””โ”€ ๅฆ โ†’ ๅŠ ๅ…ฅ้˜Ÿๅˆ—๏ผŒ้กบๅบๆ‰ง่กŒ
  โ†“
ใ€ๆญฅ้ชค4ใ€‘Pre Hook ๆ‰ง่กŒ
  โ””โ”€ ่งฆๅ‘ "PreToolUse" hooks
  โ†“
ใ€ๆญฅ้ชค5ใ€‘ๅทฅๅ…ทๆ‰ง่กŒ
  โ””โ”€ call(input, context, appState, progressCallback)
      โ”œโ”€ ่ฟ›ๅบฆๅ›ž่ฐƒๆ›ดๆ–ฐ UI
      โ””โ”€ ๆ”ฏๆŒไธญๆญข
  โ†“
ใ€ๆญฅ้ชค6ใ€‘็ป“ๆžœๅค„็†
  โ”œโ”€ ่พ“ๅ‡บ้ชŒ่ฏ (outputSchema)
  โ”œโ”€ ็ป“ๆžœๆ ผๅผๅŒ–
  โ””โ”€ ๆ˜ ๅฐ„ๅˆฐ tool_result ๆถˆๆฏ
  โ†“
ใ€ๆญฅ้ชค7ใ€‘Post Hook ๆ‰ง่กŒ
  โ””โ”€ ่งฆๅ‘ "PostToolUse" hooks
  โ†“
่ฟ”ๅ›ž็ป“ๆžœ็ป™ Agent

6.4 Tool Execution Manager

Location: 3768.js - Da6kind

class ToolExecutionManager {
  toolDefinitions: ToolDefinition[];
  executionQueue: ToolExecution[];
  activeExecutions: Set<string>;

// ๆทปๅŠ ๅทฅๅ…ทๅˆฐ้˜Ÿๅˆ—
addTool(toolUseBlock, assistantMessage): void;

// ๆฃ€ๆŸฅๆ˜ฏๅฆๅฏๆ‰ง่กŒ
canExecuteTool(isConcurrencySafe): boolean;

// ๅค„็†ๆ‰ง่กŒ้˜Ÿๅˆ—
async processQueue(): Promise<void>;

// ๆ‰ง่กŒๅ•ไธชๅทฅๅ…ท
async executeTool(execution: ToolExecution): Promise<void>;

// ๅˆ›ๅปบๅˆๆˆ้”™่ฏฏๆถˆๆฏ
createSyntheticErrorMessage(): ToolResult;

// ่Žทๅ–ไธญๆญขๅŽŸๅ› 
getAbortReason(toolId): string;
}


7. Permissions and Security Model

7.1 Three-tier permission rule system

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                      ๆƒ้™่ง„ๅˆ™ๆฅๆบ (ไผ˜ๅ…ˆ็บง: ไฝŽโ†’้ซ˜)                      โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  1. userSettings       (~/.claude/settings.json)                    โ”‚
โ”‚  2. projectSettings    (.claude/settings.json)                      โ”‚
โ”‚  3. localSettings      (.claude/settings.json - gitignored)         โ”‚
โ”‚  4. session            (ไผš่ฏไธดๆ—ถ่ฎพ็ฝฎ)                                โ”‚
โ”‚  5. cliArg             (ๅ‘ฝไปค่กŒๅ‚ๆ•ฐ - ๆœ€้ซ˜ไผ˜ๅ…ˆ็บง)                      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

ๆฏๆก่ง„ๅˆ™ๅŒ…ๅซ:
{
toolName: string, // ๅทฅๅ…ทๅ็งฐ
ruleContent?: string, // ่ง„ๅˆ™ๅ†…ๅฎน (ๅฆ‚่ทฏๅพ„ใ€ๅ‘ฝไปคๆจกๅผ)
ruleBehavior: "allow" | "deny" | "ask",
ruleDisplay: string, // ็”จๆˆทๆ˜พ็คบๆ ผๅผ
sourceDisplay: string // ๆฅๆบๆ˜พ็คบๆ ผๅผ
}

7.2 Permission decision-making process

ๅทฅๅ…ท่ฐƒ็”จ่ฏทๆฑ‚
  โ†“
ใ€ๆญฅ้ชค1ใ€‘ๅŒน้…่ง„ๅˆ™
  โ”œโ”€ ๆฃ€ๆŸฅ alwaysDenyRules โ†’ ๆœ‰ๅŒน้… โ†’ DENY
  โ”œโ”€ ๆฃ€ๆŸฅ alwaysAllowRules โ†’ ๆœ‰ๅŒน้… โ†’ ALLOW
  โ””โ”€ ๆฃ€ๆŸฅ alwaysAskRules โ†’ ๆœ‰ๅŒน้… โ†’ ASK
  โ†“
ใ€ๆญฅ้ชค2ใ€‘ๅฑ้™ฉๆ“ไฝœๆฃ€ๆต‹
  โ”œโ”€ Bash ๆƒ้™ๆฃ€ๆŸฅ
  โ”‚   โ”œโ”€ ่ง„ๅˆ™ไธบ "" ๆˆ– ๆ— ๆกไปถ โ†’ ๅฑ้™ฉ
  โ”‚   โ””โ”€ ๅ‘ฝไปคๅŒน้…ๅฑ้™ฉๆจกๅผ โ†’ ๅฑ้™ฉ
  โ”œโ”€ Task ๅˆ†็ฑปๅ™จๆฃ€ๆŸฅ
  โ”‚   โ””โ”€ classifierType === "TaskType" โ†’ ๅฑ้™ฉ
  โ””โ”€ ็ป„ๅˆๆฃ€ๆต‹ Phq() = Whq() || Zhq()
  โ†“
ใ€ๆญฅ้ชค3ใ€‘ๆƒ้™ๆจกๅผๅบ”็”จ
  โ”œโ”€ "bypassPermissions" โ†’ ALLOW (็ป•่ฟ‡ๆ‰€ๆœ‰ๆฃ€ๆŸฅ)
  โ”œโ”€ "acceptEdits" โ†’ ๆ–‡ไปถ็ผ–่พ‘ๅทฅๅ…ท ALLOW
  โ”œโ”€ "plan" โ†’ ๅทฅๅ…ทไธๆ‰ง่กŒ๏ผŒไป…่ฟ”ๅ›ž่ฎกๅˆ’
  โ”œโ”€ "dontAsk" โ†’ ็”จ้ข„้…็ฝฎ่ง„ๅˆ™ๆˆ–้ป˜่ฎคๆ‹’็ป
  โ””โ”€ "default" โ†’ ๆญฃๅธธๆต็จ‹
  โ†“
ใ€ๆญฅ้ชค4ใ€‘ๆฒ™็ฎฑๆฃ€ๆŸฅ
  โ”œโ”€ ๆฃ€ๆŸฅ่ทฏๅพ„ๆ˜ฏๅฆๅœจๆฒ™็ฎฑๅ†…
  โ”œโ”€ ๆฃ€ๆŸฅๆ˜ฏๅฆ่ฎฟ้—ฎๅฑ้™ฉ่ฎพๅค‡ (/dev/)
  โ””โ”€ ้œ€่ฆๆฒ™็ฎฑๅค–ๆ‰ง่กŒ โ†’ ้ขๅค–็กฎ่ฎค
  โ†“
ใ€ๆญฅ้ชค5ใ€‘ๆœ€็ปˆๅ†ณ็ญ–
  โ”œโ”€ ALLOW โ†’ ๆ‰ง่กŒๅทฅๅ…ท
  โ”œโ”€ DENY โ†’ ่ฟ”ๅ›žๆ‹’็ปๆถˆๆฏ
  โ””โ”€ ASK โ†’ ๆ˜พ็คบ็กฎ่ฎคๅฏน่ฏๆก†๏ผŒ็ญ‰ๅพ…็”จๆˆทๅ“ๅบ”

7.3 Detailed explanation of permission mode

model

Trigger mode

Behavior

default

default

Standard behavior, dangerous operations need to be confirmed

plan

Enter planning mode

Does not execute the actual tool, only returns the plan

acceptEdits

-aor configure

Automatically accept file edits

dontAsk

Configuration

No prompts, use preconfigured rules

bypassPermissions

--dangerously-skip-permissions

Bypass all permission checks

7.4 Hazardous operation detection

// ๅฑ้™ฉ Bash ๅ‘ฝไปคๆจกๅผ
const DANGER_BASH_PATTERNS = [
  "rm -rf",
  "dd if=",
  "mkfs",
  "fdisk",
  "chmod 000",
  "chown -R",
  "curl | sh",
  "wget | sh",
  "eval $(",
  // ... ๆ›ดๅคš
];

// ๅฑ้™ฉๅˆ†็ฑปๅ™จๆฃ€ๆต‹
function isDangerousClassifier(toolName, ruleContent) {
return classifierType(toolName) === "TaskType";
}

// ็ป„ๅˆๆฃ€ๆต‹
function isDangerousPermission(toolName, ruleContent) {
return isDangerousBash(toolName, ruleContent) ||
isDangerousClassifier(toolName, ruleContent);
}

7.5 Sandbox mechanism

Sandbox type:
1. working directory sandbox- Restrict file access to project directory
2.command sandbox- Limit executable shell commands
3.permission sandbox- Execution control based on permission rules

Forbidden paths:

const FORBIDDEN_PATHS = [
  "/dev/zero",
  "/dev/random",
  "/dev/urandom",
  "/dev/full",
  "/dev/stdin",
  "/dev/stdout",
  "/dev/stderr",
  "/dev/tty",
  "/dev/console",
];

8. Hook system

8.1 Hook event type

event name

Trigger time

use

PreToolUse

Before tool execution

Verify, modify input, intercept

PostToolUse

After successful execution of the tool

Processing results, logs, follow-up actions

PostToolUseFailure

After tool execution fails

Error handling, cleanup

SessionStart

Session starts

Initialize resources

Stop

stop event

Clean up resources

PermissionRequest

permission request

Custom permission processing

ConfigChange

Configuration changes

Respond to configuration changes

Elicitation

elicit events

Custom data collection

ElicitationResult

elicit results

Process collection results

InstructionsLoaded

Command loading completed

Verify CLAUDE.md

SubagentStart

Subagent starts

Initialize subagent environment

SubagentStop

subagent stopped

Clean up subagent resources

PreCompact

Before context compression

Back up or analyze conversations

Notification

notification event

Custom notification handling

8.2 Hook configuration structure

{
  "hooksConfig": {
    "enabled": true,
    "disabled": [],
    "notifications": true
  },
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hookCallbackIds": ["my-validator"],
        "timeout": 5000
      },
      {
        "matcher": "Write:path/to/file.js",
        "hookCallbackIds": ["my-preprocessor"]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "*",
        "hookCallbackIds": ["my-logger"]
      }
    ],
    "SessionStart": [
      {
        "hookCallbackIds": ["init-workspace"]
      }
    ]
  }
}

8.3 Hook callback interface

interface HookCallback {
  id: string;
  event: HookEventType;
  matcher?: string;              // ๅŒน้…ๅ™จ (ๅฏ้€‰)
  handler: (context: HookContext) => Promise<HookResult>;
  timeout?: number;              // ่ถ…ๆ—ถๆ—ถ้—ด (ๆฏซ็ง’)
}

interface HookContext {
eventName: string;
toolName?: string;
input?: unknown;
result?: unknown;
error?: Error;
sessionId: string;
timestamp: Date;
}

interface HookResult {
action: "continue" | "modify" | "stop";
modifiedInput?: unknown;
modifiedResult?: unknown;
message?: string;
}


9. Context Management

9.1 Three-stage compression mechanism

level

name

Trigger condition

Processing method

Effect

L1

microcompact

Automatically every round

Clean up redundant message elements

5-10% reduction

L2

autocompact

exceeds threshold

LLM summary compression

50-70% reduction

L3

reactive compact

API rejection

Emergency compression retry

Make sure the request is successful

9.2 Detailed explanation of compression process

// ๅพฎ็ดงๅ‡‘
function microcompact(messages) {
  return messages
    .filter(m => !isRedundant(m))      // ็งป้™คๅ†—ไฝ™
    .map(m => normalizeContent(m));    // ่ง„่ŒƒๅŒ–ๅ†…ๅฎน
}

// ่‡ชๅŠจ็ดงๅ‡‘
async function autocompact(messages, options) {
const tokenCount = await countTokens(messages);
const threshold = options.contextWindow * 0.8;

if (tokenCount > threshold) {
// ่ฐƒ็”จ LLM ่ฟ›่กŒๆ‘˜่ฆ
const summary = await callLLM({
prompt: COMPACT_PROMPT,
messages: messages,
maxTokens: options.budget
});

return {
  messages: createCompactMessages(summary),
  stats: {
    preCompact: tokenCount,
    postCompact: await countTokens(summary),
    reduction: 1 - (postCompact / preCompact)
  }
};

}
}

// ๅๅบ”ๅผ็ดงๅ‡‘
async function reactiveCompact(error) {
if (error.type === "context_too_long") {
const compacted = await autocompact(currentMessages, {
aggressive: true // ๆ›ดๆฟ€่ฟ›็š„ๅŽ‹็ผฉ
});
return retryWith(compacted.messages);
}
}

9.3 Prompt caching strategy

// ็ผ“ๅญ˜้…็ฝฎ
const CACHE_CONFIG = {
  // REPL ๆจกๅผ๏ผš้•ฟ็ผ“ๅญ˜
  repl: {
    scope: "ephemeral",
    ttl: "1h"
  },

// ้ป˜่ฎค๏ผš็Ÿญ็ผ“ๅญ˜
default: {
scope: "ephemeral",
ttl: "5m"
},

// ๅ…จๅฑ€๏ผš็”จๆˆท็บง็ผ“ๅญ˜
global: {
scope: "global",
ttl: "1h"
}
};

// ๅบ”็”จ็ผ“ๅญ˜
function applyCacheControl(blocks, querySource) {
return blocks.map((block, index) => {
const isLast = index === blocks.length - 1;
const config = getCacheConfig(querySource);

return {
  ...block,
  ...(isLast ? { cache_control: config } : {})
};

});
}

9.4 Token tracking

Accurate counting:

async function countTokens(messages, tools) {
  const response = await client.beta.messages.countTokens({
    model: currentModel,
    messages,
    tools,
    betas: ["token-counting-2025-07-01"]
  });

return {
inputTokens: response.input_tokens,
cacheReadTokens: response.cache_read_input_tokens,
cacheCreationTokens: response.cache_creation_input_tokens
};
}

local estimate(quick prediction):

function estimateTokens(messages) {
  return messages.reduce((sum, msg) => {
    return sum + estimateMessageTokens(msg);
  }, 0);
}

function estimateMessageTokens(message) {
// ๆ–‡ๆœฌ: ๅญ—็ฌฆๆ•ฐ / 4
// ๅ›พ็‰‡: ๅ›บๅฎšไผฐ็ฎ—ๅ€ผ
// ๅทฅๅ…ท่ฐƒ็”จ: ๅ›บๅฎšไผฐ็ฎ—ๅ€ผ
}

9.5 Context window configuration

Model

context window

Output limits

compression threshold

claude-opus-4-6

200K

8192

160K (80%)

claude-sonnet-4-6

200K

8192

160K (80%)

claude-haiku

200K

8192

160K (80%)


10. UI framework and rendering

10.1 Technology stack

// ๆ ธๅฟƒ
React 19.2           // UI ๆก†ๆžถ
Ink 6.x              // ็ปˆ็ซฏๆธฒๆŸ“ๅผ•ๆ“Ž

// ็Šถๆ€็ฎก็†
React Context API // ๅ…จๅฑ€็Šถๆ€
useState / useEffect // Hooks

// ็ป„ไปถๅบ“ (Ink ๅ†…็ฝฎ)
<Box> // ๅธƒๅฑ€ๅฎนๅ™จ
<Text> // ๆ–‡ๆœฌๆธฒๆŸ“
<Newline> // ๆข่กŒ
<Static> // ้™ๆ€ๅ†…ๅฎน

10.2 UI component hierarchy

<AppContainer> (React ่พน็•Œ)
  โ”‚
  โ”œโ”€ <Provider ๅฑ‚>
  โ”‚   โ”œโ”€ <SettingsContext.Provider>
  โ”‚   โ”œโ”€ <SessionContext.Provider>
  โ”‚   โ”œโ”€ <KeypressContext.Provider>
  โ”‚   โ”œโ”€ <MouseContext.Provider>
  โ”‚   โ”œโ”€ <ScrollProvider>
  โ”‚   โ”œโ”€ <TerminalContext.Provider>
  โ”‚   โ”œโ”€ <OverflowProvider>
  โ”‚   โ””โ”€ <StreamingProvider>
  โ”‚
  โ””โ”€ <App> (ไธป็ป„ไปถ)
      โ”‚
      โ”œโ”€ <Header> (ๅคด้ƒจ: ็Šถๆ€ใ€ๆจกๅผๆŒ‡็คบๅ™จ)
      โ”‚
      โ”œโ”€ <MessageList> (ๆถˆๆฏๅކๅฒ)
      โ”‚   โ”œโ”€ <UserMessage>
      โ”‚   โ”œโ”€ <AssistantMessage>
      โ”‚   โ”œโ”€ <ToolMessage>
      โ”‚   โ”œโ”€ <ThinkingMessage>
      โ”‚   โ””โ”€ <InfoMessage>
      โ”‚
      โ”œโ”€ <ToolConfirmationQueue> (ๅทฅๅ…ท็กฎ่ฎค)
      โ”‚
      โ”œโ”€ <InputDialog> (่พ“ๅ…ฅๅŒบ)
      โ”‚
      โ””โ”€ <StatusBar> (ๅบ•้ƒจ็Šถๆ€ๆ )

10.3 Detailed explanation of core Context

10.3.1 UIStateContext

interface UIState {
  // ๆถˆๆฏๅކๅฒ
  history: HistoryItem[];
  pendingGeminiHistoryItems: HistoryItemWithoutId[];
  pendingSlashCommandHistoryItems: HistoryItemWithoutId[];

// ๅฏน่ฏๆก†็Šถๆ€
isAuthDialogOpen: boolean;
isSettingsDialogOpen: boolean;
isSessionBrowserOpen: boolean;
isModelDialogOpen: boolean;
isAgentConfigDialogOpen: boolean;
isPermissionsDialogOpen: boolean;

// ๅ‘ฝไปค็ณป็ปŸ
slashCommands: SlashCommand[];
commandContext: CommandContext;
commandConfirmationRequest: ConfirmationRequest | null;

// ็กฎ่ฎค่ฏทๆฑ‚
loopDetectionConfirmationRequest: ConfirmationRequest | null;
permissionConfirmationRequest: PermissionRequest | null;

// ๆตๅ’Œๅทฅๅ…ท
streamingState: "idle" | "responding" | "waiting";
pendingToolCalls: TrackedToolCall[];
thought: ThoughtSummary | null;

// ่ƒŒๆ™ฏ่ฟ›็จ‹
backgroundShells: Map<number, BackgroundShell>;
activePtyId: number | undefined;

// ไผš่ฏ็ปŸ่ฎก
sessionStats: SessionStatsState;
elapsedTime: number;

// ็ปˆ็ซฏ
terminalWidth: number;
terminalHeight: number;

// UI ๆŽงๅˆถ
renderMarkdown: boolean;
copyModeEnabled: boolean;
}

10.3.2 SessionContext

interface SessionContext {
  sessionId: string;
  sessionStartTime: Date;
  metrics: SessionMetrics;
  promptCount: number;

// ่ฎก็ฎ—ๅฑžๆ€ง
totalApiTime: number;
totalToolTime: number;
agentActiveTime: number;
apiTimePercent: number;
toolTimePercent: number;
cacheEfficiency: number;
successRate: number;
totalCachedTokens: number;
}

10.4 Rendering optimization

// ๅขž้‡ๆธฒๆŸ“ (damage tracking)
function renderMessages(messages) {
  const prevHash = hash(previousMessages);
  const currHash = hash(messages);

if (prevHash !== currHash) {
// ๅช้‡ๆ–ฐๆธฒๆŸ“ๅ˜ๅŒ–็š„้ƒจๅˆ†
const changed = findChangedRegion(prevMessages, messages);
renderRegion(changed);
}
}

// ่™šๆ‹ŸๆปšๅŠจ (้•ฟๅˆ—่กจ)
function renderLongList(items) {
const visible = calculateVisible(items, scrollPosition);
return visible.map(item => <Item {...item} />);
}


11. Authentication system

11.1 Supported authentication methods

Way

environment variables

Configuration file

illustrate

API Key

ANTHROPIC_API_KEY

settings.json

Standard certification

Auth Token

ANTHROPIC_AUTH_TOKEN

settings.json

OAuth token

Console Login

-

Browser OAuth

Interactive login

SSO

-

Enterprise configuration

Single sign-on

11.2 OAuth 2.0 process

็”จๆˆทๆ‰ง่กŒ claude auth login
  โ†“
ๆ‰“ๅผ€ๆต่งˆๅ™จ โ†’ https://platform.claude.com/oauth/authorize
  โ”œโ”€ client_id: 9d1c250a-e61b-44d9-88ed-5944d1962f5e
  โ”œโ”€ redirect_uri: http://localhost:5454/callback
  โ””โ”€ scope: "openid profile"
  โ†“
็”จๆˆทๆŽˆๆƒ
  โ†“
้‡ๅฎšๅ‘ๅˆฐ localhost ๅ›ž่ฐƒ
  โ†“
ไบคๆขๆŽˆๆƒ็  โ†’ POST https://platform.claude.com/v1/oauth/token
  โ†“
ๆŽฅๆ”ถ access_token + refresh_token
  โ†“
ไฟๅญ˜ๅˆฐ ~/.claude/oauth.json
  โ†“
ๅŽ็ปญ่ฏทๆฑ‚ไฝฟ็”จ Bearer token

11.3 API Key generation

OAuth ็™ปๅฝ•ๅŽ
  โ†“
POST https://api.anthropic.com/api/oauth/claude_cli/create_api_key
  โ”‚
  โ”œโ”€ Headers:
  โ”‚   โ”œโ”€ Authorization: Bearer <oauth_token>
  โ”‚   โ””โ”€ Content-Type: application/json
  โ”‚
  โ””โ”€ Body:
      {
        "description": "Claude CLI Key",
        "expires_after": null
      }
  โ†“
่ฟ”ๅ›ž:
{
  "api_key": "sk-ant-...",
  "created_at": "2026-03-09T00:00:00Z"
}

11.4 Token refresh

async function refreshAccessToken() {
  const refreshToken = getStoredRefreshToken();

const response = await fetch(TOKEN_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
grant_type: "refresh_token",
refresh_token: refreshToken
})
});

const { access_token, refresh_token: newRefresh } = await response.json();

// ๆ›ดๆ–ฐๅญ˜ๅ‚จ
storeTokens({ access_token, refresh_token: newRefresh });
}


12. MCP integration

12.1 MCP transmission method

Way

Implementation class

Applicable scenarios

Stdio

StdioClientTransport

local process

SSE

SSEClientTransport

HTTP service

12.2 MCP server configuration

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed"],
      "disabled": false
    },
    "brave-search": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-brave-search"],
      "env": {
        "BRAVE_API_KEY": "${BRAVE_API_KEY}"
      }
    }
  }
}

12.3 MCP tool calling process

Agent ๅ†ณๅฎšไฝฟ็”จ MCP ๅทฅๅ…ท
  โ†“
ๆŸฅๆ‰พ MCP ๆœๅŠกๅ™จ่ฟžๆŽฅ
  โ†“
ๆž„ๅปบ tool_use ๆถˆๆฏ
  โ†“
่ฐƒ็”จ MCP ๆœๅŠกๅ™จ
  โ”œโ”€ stdio: ๅ†™ๅ…ฅ stdin, ่ฏปๅ– stdout
  โ””โ”€ SSE: ๅ‘้€ HTTP ่ฏทๆฑ‚
  โ†“
่งฃๆžๅ“ๅบ”
  โ”œโ”€ ๆˆๅŠŸ โ†’ ่ฟ”ๅ›ž tool_result
  โ””โ”€ ๅคฑ่ดฅ โ†’ ่ฟ”ๅ›ž้”™่ฏฏไฟกๆฏ
  โ†“
็ปง็ปญ Agent ๅพช็Žฏ

12.4 MCP resource management

// ๅˆ—ๅ‡บ่ต„ๆบ
const resources = await mcpClient.listResources();

// ่ฏปๅ–่ต„ๆบ
const content = await mcpClient.readResource(uri);

// ่ฎข้˜…่ต„ๆบๆ›ดๆ–ฐ
await mcpClient.subscribeResource(uri);

// ่ฐƒ็”จๅทฅๅ…ท
const result = await mcpClient.callTool(name, args);


13. Plug-ins and Marketplace

13.1 Plug-in source types

type

illustrate

Example

github

GitHub repository

anthropics/official-plugin

git

Git repository URL

https://example.com/repo.git

git-subdir

Git subdirectory

https://example.com/repo.git#subdir

13.2 Marketplace configuration

{
  "marketplaces": [
    {
      "name": "official",
      "source": "github",
      "url": "https://github.com/anthropics",
      "autoUpdate": true
    }
  ]
}

13.3 Plug-in list

{
  "name": "my-plugin",
  "version": "1.0.0",
  "description": "My awesome plugin",
  "commands": [
    {
      "name": "my-command",
      "description": "Does something",
      "action": "command.js:myHandler"
    }
  ],
  "skills": [
    {
      "name": "my-skill",
      "description": "My skill",
      "module": "skills/my-skill.js"
    }
  ],
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "handler": "hooks/pre-bash.js"
      }
    ]
  }
}

13.4 Skills API

// ๆŠ€่ƒฝๅฎšไน‰
{
  name: "my-skill",
  description: "Performs a specialized task",
  parameters: {
    type: "object",
    properties: { /* ... */ }
  },
  handler: async (params, context) => {
    // ๆŠ€่ƒฝ้€ป่พ‘
  }
}

// ๆŠ€่ƒฝ่ฐƒ็”จ
const result = await callSkill("my-skill", params);


14. Telemetry system

14.1 Types of data collected

category

event

data

Function usage

tool_executed

Tool name, parameters, duration

Permissions

permission_request

Tool name, decision, approval type

cost

cost_threshold_reached

Cost amount, session length

mistake

error_message

Error type, stack

renew

update_check

Current version, latest version

environment

environment_detected

IDE, cloud platform, OS

14.2 Telemetry event structure

{
  eventName: "tengu_tool_executed",
  properties: {
    tool_name: "Bash",
    permission_mode: "default",
    approval_type: "ask",
    duration_ms: 1234,
    success: true
  },
  userId: "user-uuid",
  sessionId: "session-uuid",
  timestamp: "2026-03-09T00:00:00Z"
}

14.3 GrowthBook Integration

// ๅŠŸ่ƒฝๆ ‡ๅฟ—
const features = await growthBook.getFeatures();

// A/B ๆต‹่ฏ•
const variant = growthBook.getFeatureValue("experiment-name", "control");

// ่ฟฝ่ธช
growthBook.track("event-name", properties);

14.4 Environmental testing

// IDE ๆฃ€ๆต‹
const IDES = {
  VSCODE: "vscode",
  JETBRAINS: ["intellij", "pycharm", "webstorm", /* ... */],
  VIM: "vim",
  EMACS: "emacs"
};

// ไบ‘ๅนณๅฐๆฃ€ๆต‹
const CLOUDS = {
CODESPACES: "codespaces",
GITPOD: "gitpod",
REPLIT: "replit",
AWS: ["aws-lambda", "aws-ec2", "aws-ecs"],
GCP: "gcp-cloud-run",
AZURE: "azure-functions"
};


15. Enterprise Strategy Function

15.1 Hosting setup

{
  policySettings: {
    // ๆจกๅž‹้™ๅˆถ
    allowedModels: [
      "claude-sonnet-4-6",
      "claude-haiku"
    ],

// MCP ๆœๅŠกๅ™จๆŽงๅˆถ
allowedMcpServers: ["filesystem", "git"],
deniedMcpServers: ["*"],

// ๅŸŸ้™ๅˆถ
allowManagedDomainsOnly: true,
allowedDomains: ["*.company.com"],

// Hook ้™ๅˆถ
allowManagedHooksOnly: true,

// Marketplace ้™ๅˆถ
strictKnownMarketplaces: true,
knownMarketplaces: [
  "https://github.com/anthropics"
],
deniedMarketplaces: [],

// ๆƒ้™่ง„ๅˆ™้™ๅˆถ
allowManagedPermissionRulesOnly: true,

// ้”ๅฎšๆ ‡ๅฟ—
managedSettingsLocked: true

}
}

15.2 Policy execution process

็”จๆˆทๆ“ไฝœ่ฏทๆฑ‚
  โ†“
ๆฃ€ๆŸฅ managedSettingsLocked
  โ”œโ”€ true โ†’ ๅชๅบ”็”จๆ‰˜็ฎก่ฎพ็ฝฎ
  โ””โ”€ false โ†’ ๅˆๅนถ็”จๆˆท่ฎพ็ฝฎ
  โ†“
ๆฃ€ๆŸฅ allowManagedXOnly
  โ”œโ”€ true โ†’ ๅชไฝฟ็”จๆ‰˜็ฎก้…็ฝฎ
  โ””โ”€ false โ†’ ๅ…่ฎธ็”จๆˆท้…็ฝฎ
  โ†“
้ชŒ่ฏ่ฏทๆฑ‚็ฌฆๅˆ็ญ–็•ฅ
  โ”œโ”€ ็ฌฆๅˆ โ†’ ๆ‰ง่กŒ
  โ””โ”€ ไธ็ฌฆๅˆ โ†’ ๆ‹’็ป + ่ฎฐๅฝ•

16. Configuring the file system

16.1 Configuration file location

Scope

path

Git

priority

System

/Library/Application Support/Claude Code/

-

lowest

User

~/.claude/settings.json

โœ“

Low

Project

.claude/settings.json

โœ“

middle

Local

.claude/settings.json (gitignored)

โœ—

high

Session

memory (temporary)

-

higher

CLI

Command line parameters

-

Highest

16.2 CLAUDE.md system

CLAUDE.md ๅŠ ่ฝฝ้กบๅบ:
  โ†“

  1. ๅ…จๅฑ€ CLAUDE.md (~/.claude/CLAUDE.md)
    โ†“

  2. ้กน็›ฎ CLAUDE.md (้กน็›ฎๆ น/CLAUDE.md)
    โ†“

  3. ๆœฌๅœฐ CLAUDE.md (.claude/CLAUDE.md)
    โ†“

  4. ๆ‰˜็ฎก CLAUDE.md (ไผไธš้…็ฝฎ)
    โ†“
    ๅˆๅนถๅนถๆณจๅ…ฅ็ณป็ปŸๆ็คบ

16.3 Set Schema

{
  // ้€š็”จ่ฎพ็ฝฎ
  general: {
 vimMode: boolean,
 defaultApprovalMode: "default" | "auto_edit" | "yolo" | "plan",
 checkpointing: {
   enabled: boolean,
   interval: number
 },
 sessionRetention: {
   enabled: boolean,
   maxDays: number
 }
  },

// ๆจกๅž‹่ฎพ็ฝฎ
model: {
name: string,
summarizeToolOutput: { [toolName]: { tokenBudget: number } },
compressionThreshold: 0.5,
disableLoopDetection: false,
skipNextSpeakerCheck: true
},

// ไธŠไธ‹ๆ–‡่ฎพ็ฝฎ
context: {
fileName: "CLAUDE.md",
includeDirectoryTree: true,
includeDirectories: string[],
fileFiltering: {
respectGitIgnore: true,
respectClaudeIgnore: true,
enableRecursiveFileSearch: true,
customIgnoreFilePaths: string[]
}
},

// ๅทฅๅ…ท่ฎพ็ฝฎ
tools: {
sandbox: true,
allowed: { [toolName]: Rule[] },
denied: { [toolName]: Rule[] },
discoveryCommand: "find . -type f -name&quot;*.js&quot;"
},

// Hook ่ฎพ็ฝฎ
hooksConfig: {
enabled: true,
disabled: string[],
notifications: true
},
hooks: {
PreToolUse: Hook[],
PostToolUse: Hook[],
// ... ๅ…ถไป–ไบ‹ไปถ
},

// UI ่ฎพ็ฝฎ
ui: {
theme: "dark" | "light",
accessibility: {
highContrast: boolean,
reducedMotion: boolean
},
footer: {
showSessionStats: boolean,
showModelInfo: boolean
}
},

// ๅฎ‰ๅ…จ่ฎพ็ฝฎ
security: {
folderTrust: {
enabled: true,
trustedFolders: string[]
},
disableYoloMode: false,
environmentVariableRedaction: true
},

// Agent ่ฎพ็ฝฎ
agents: {
overrides: {
[agentName]: {
enabled: boolean,
modelConfig: {},
runConfig: {
maxTimeMinutes: number,
maxTurns: number
}
}
}
}
}


17. Sub-agent system

17.1 Task tool interface

interface TaskToolParams {
  prompt: string;              // ไปปๅŠกๆ่ฟฐ
  subagent_type?: string;      // ไปฃ็†็ฑปๅž‹
  description?: string;        // ๆ่ฟฐ
  resume?: string;             // ๆขๅคๅญไปฃ็† ID
  run_in_background?: boolean; // ๅŽๅฐ่ฟ่กŒ
  name?: string;               // ไปปๅŠกๅ็งฐ
  team_name?: string;          // ๅ›ข้˜Ÿๅ็งฐ
  mode?: "tmux" | "in-process" | "auto";
  isolation?: "shared" | "isolated";
  cwd?: string;                // ๅทฅไฝœ็›ฎๅฝ•
}

17.2 Subagent types

type

illustrate

Start mode

general-purpose

Common tasks

default

statusline-setup

Status bar configuration

Internal use

Explore

Code exploration

Internal use

Plan

Plan generation

Internal use

ui-ux-designer

UI design

Internal use

17.3 Subagent life cycle

Task ๅทฅๅ…ท่ฐƒ็”จ
  โ†“
ใ€ๅˆ›ๅปบใ€‘
  โ”œโ”€ ็”Ÿๆˆๅญไปฃ็† ID
  โ”œโ”€ ๅˆ›ๅปบๅญไปฃ็†ไผš่ฏ็›ฎๅฝ•
  โ””โ”€ ๅˆๅง‹ๅŒ–ๅญไปฃ็†็Šถๆ€
  โ†“
ใ€้…็ฝฎใ€‘
  โ”œโ”€ ๅบ”็”จไปฃ็†็ฑปๅž‹้…็ฝฎ
  โ”œโ”€ ่ฎพ็ฝฎๆจกๅž‹ๅ’Œๅทฅๅ…ท
  โ””โ”€ ็ปงๆ‰ฟๆˆ–้š”็ฆปๆƒ้™
  โ†“
ใ€ๆ‰ง่กŒใ€‘
  โ”œโ”€ ๅฏๅŠจๅญไปฃ็†ๅพช็Žฏ
  โ”œโ”€ ็‹ฌ็ซ‹ไธŠไธ‹ๆ–‡็ฎก็†
  โ””โ”€ ไบ‹ไปถ้€šไฟก
  โ†“
ใ€ๅฎŒๆˆใ€‘
  โ”œโ”€ ๆ”ถ้›†็ป“ๆžœ
  โ”œโ”€ ๆธ…็†่ต„ๆบ
  โ””โ”€ ่ฟ”ๅ›ž็ป™็ˆถไปฃ็†

17.4 Teamwork

{
  team_name: "my-team",
  teammates: [
    {
      agent_id: "agent-1",
      agent_name: "Researcher",
      agent_type: "general-purpose",
      model: "claude-haiku"
    },
    {
      agent_id: "agent-2",
      agent_name: "Implementer",
      agent_type: "general-purpose",
      model: "claude-sonnet-4-6"
    }
  ],
  coordination: {
    mode: "tmux" | "in-process",
    shared_context: boolean
  }
}

18. Session Management

18.1 Session state

interface SessionState {
  sessionId: string;
  createdAt: Date;
  lastActiveAt: Date;

// ๆถˆๆฏๅކๅฒ
messages: Message[];

// ้…็ฝฎๅฟซ็…ง
config: {
model: string;
tools: string[];
permissions: PermissionRules;
};

// ็ปŸ่ฎก
stats: {
turnCount: number;
totalTokens: number;
totalCost: number;
};

// ๅ…ƒๆ•ฐๆฎ
metadata: {
projectPath: string;
gitBranch?: string;
trustLevel: "trusted" | "untrusted" | "unknown";
};
}

18.2 Session persistence

ไผš่ฏไฟๅญ˜่งฆๅ‘:
  โ”œโ”€ ๆฏๆฌกๅทฅๅ…ทๆ‰ง่กŒๅŽ
  โ”œโ”€ ๆฏ N ็ง’่‡ชๅŠจไฟๅญ˜
  โ””โ”€ ไผš่ฏ็ป“ๆŸๆ—ถ
  โ†“
ไฟๅญ˜ๅ†…ๅฎน:
  โ”œโ”€ ๆถˆๆฏๅކๅฒ
  โ”œโ”€ ้…็ฝฎๅฟซ็…ง
  โ”œโ”€ ไปปๅŠก็Šถๆ€
  โ””โ”€ ็ปŸ่ฎกไฟกๆฏ
  โ†“
ๅญ˜ๅ‚จไฝ็ฝฎ:
  โ””โ”€ ~/.claude/sessions/<sessionId>/transcript.json

18.3 Session recovery

// ๆขๅคๆต็จ‹
async function resumeSession(sessionId, mode) {
  // 1. ๅŠ ่ฝฝไผš่ฏๆ•ฐๆฎ
  const session = await loadSession(sessionId);

// 2. ๆขๅค้…็ฝฎ
applyConfig(session.config);

// 3. ๆขๅคๆถˆๆฏ
restoreMessages(session.messages);

// 4. ๆขๅค็Šถๆ€
restoreState(session.metadata);

// 5. ๆจกๅผๅค„็†
if (mode === "fork") {
// ๅˆ›ๅปบๅˆ†ๆ”ฏ
sessionId = generateNewId();
copySessionData(sessionId, session);
}

return sessionId;
}

18.4 Session Cleanup

// ่‡ชๅŠจๆธ…็†
async function cleanupOldSessions() {
  const retention = getRetentionDays();
  const cutoff = Date.now() - (retention * 86400000);

const sessions = await listSessions();
const expired = sessions.filter(s => s.lastActiveAt < cutoff);

for (const session of expired) {
await deleteSession(session.id);
}
}


19. Git integration

19.1 Git command integration

integration point

Function

state injection

Inject git branch/status into the system prompt

Worktree

Support creation and management of git worktree

Submission tracking

Track the number of git commits

path resolution

Intelligent parsing of git warehouse paths

19.2 Worktree support

# ๅˆ›ๅปบ worktree ไผš่ฏ
claude -w feature-branch

ๆˆ–ๆŒ‡ๅฎšๅ็งฐ

claude -w my-feature

ไธŽ tmux ็ป“ๅˆ

claude -w feature --tmux

19.3 Git state injection

const gitContext = {
  branch: await getCurrentBranch(),
  status: await getGitStatus(),
  recentCommits: await getRecentCommits(5),
  root: await getGitRoot()
};

// ๆณจๅ…ฅ็ณป็ปŸๆ็คบ
systemPrompt += `
Current git context:

  • Branch:${gitContext.branch}

  • Status:${gitContext.status}

  • Recent commits:${gitContext.recentCommits.join('\n')}
    `;


20. Automatic update mechanism

20.1 Update check

async function checkForUpdate() {
// 1. ๆฃ€ๆต‹ๅฎ‰่ฃ…็ฑปๅž‹
const installType = detectInstallType();

// 2. ่Žทๅ–ๅฝ“ๅ‰็‰ˆๆœฌ
const currentVersion = getVersion();

// 3. ๆŸฅ่ฏขๆœ€ๆ–ฐ็‰ˆๆœฌ
const latest = await fetchLatestVersion(installType);

// 4. ๆฏ”่พƒ็‰ˆๆœฌ
if (semver.gt(latest, currentVersion)) {
return {
available: true,
current: currentVersion,
latest: latest,
installType
};
}

return { available: false };

}

20.2 Installation type detection

type

Detection method

Update method

npm-local

package.json exists

npm update

npm-global

which claude in npm prefix

npm update -g

native

Binary in specific path

Download new version

homebrew

brew list claude-code

brew upgrade

development

.git directory exists

git pull

20.3 Update channel

{
  autoUpdatesChannel: "latest" | "stable" | "beta",

channels: {
latest: {
url: "https://registry.npmjs.org/@anthropic-ai/claude-code",
checkInterval: 86400000 // 24ๅฐๆ—ถ
},
stable: {
url: "https://registry.npmjs.org/@anthropic-ai/claude-code",
tag: "latest"
},
beta: {
url: "https://registry.npmjs.org/@anthropic-ai/claude-code",
tag: "beta"
}
}
}


21. Key constants and configurations

21.1 Version information

{
  VERSION: "2.1.71",
  BUILD_TIME: "2026-03-06T22:45:36Z",
  PACKAGE_URL: "@anthropic-ai/claude-code",
  FEEDBACK_CHANNEL: "https://github.com/anthropics/claude-code/issues",
  README_URL: "https://code.claude.com/docs/en/overview",
  ISSUES_EXPLAINER: "report the issue at https://github.com/anthropics/claude-code/issues"
}

21.2 Model constants

{
  // ้ป˜่ฎคๆจกๅž‹
  DEFAULT_MODEL: "claude-sonnet-4-6",
  DEFAULT_HAIKU: "claude-haiku",
  DEFAULT_OPUS: "claude-opus-4-6",

// ๆจกๅž‹ๆ˜ ๅฐ„
MODEL_ALIASES: {
"sonnet": "claude-sonnet-4-6",
"opus": "claude-opus-4-6",
"haiku": "claude-haiku"
}
}

21.3 Timeout configuration

{
  // API ่ถ…ๆ—ถ
  API_TIMEOUT: 120000,           // 2ๅˆ†้’Ÿ
  STREAM_IDLE_TIMEOUT: 60000,    // 1ๅˆ†้’Ÿ
  STREAM_IDLE_WARNING: 30000,    // 30็ง’่ญฆๅ‘Š

// ๅทฅๅ…ท่ถ…ๆ—ถ
TOOL_DEFAULT_TIMEOUT: 300000, // 5ๅˆ†้’Ÿ
MCP_TOOL_TIMEOUT: 120000, // 2ๅˆ†้’Ÿ

// Hook ่ถ…ๆ—ถ
HOOK_DEFAULT_TIMEOUT: 5000, // 5็ง’
}

21.4 Path constants

{
  // ้…็ฝฎ็›ฎๅฝ•
  CONFIG_DIR: process.env.CLAUDE_CONFIG_DIR || path.join(homedir(), ".claude"),

// ไผš่ฏ็›ฎๅฝ•
SESSIONS_DIR: path.join(CONFIG_DIR, "sessions"),

// MCP ๆ—ฅๅฟ—็›ฎๅฝ•
MCP_LOGS_DIR: path.join(CONFIG_DIR, "mcp-logs"),

// OAuth ็ผ“ๅญ˜
OAUTH_CACHE: path.join(CONFIG_DIR, "oauth.json")
}


22. Security features

22.1 Key protection

// ้˜ฒๆญขๆต่งˆๅ™จ็Žฏๅขƒ
if (isBrowser() && !dangerouslyAllowBrowser) {
  throw new Error("Cannot use API key in browser environment");
}

// ็Žฏๅขƒๅ˜้‡้ฎ่”ฝ
function redactSecrets(str) {
return str
.replace(/sk-ant-[a-zA-Z0-9-]{40,}/g, "sk-ant-***")
.replace(/Bearer[a-zA-Z0-9-]{20,}/g, "Bearer ***");
}

22.2 Folder trust

// ไฟกไปปๆฃ€ๆŸฅ
function checkFolderTrust(folderPath) {
  const trustRecord = getTrustRecord(folderPath);

if (!trustRecord) {
return {
trusted: false,
action: "ask",
reason: "Folder not in trust database"
};
}

return {
trusted: trustRecord.trusted,
action: trustRecord.trusted ? "allow" : "deny",
trustDate: trustRecord.date
};
}

22.3 Dangerous path detection

const DANGEROUS_PATHS = [
  // ็ณป็ปŸ็›ฎๅฝ•
  "/",
  "/System",
  "/bin",
  "/sbin",
  "/etc",

// ็”จๆˆทๅ…ณ้”ฎ็›ฎๅฝ•
"/.ssh",
"/.gnupg",
"~/.config",

// ่ฎพๅค‡ๆ–‡ไปถ
"/dev/*"
];


23. Performance optimization strategies

23.1 Lazy loading

// ๆจกๅ—ๆŒ‰้œ€ๅŠ ่ฝฝ
var heavyModule = k(() => {
  // ๅชๅœจ้ฆ–ๆฌกไฝฟ็”จๆ—ถๆ‰ง่กŒ
  return loadHeavyModule();
});

23.2 Caching strategy

// ๅคš็บง็ผ“ๅญ˜
interface CacheStrategy {
  // ๅ†…ๅญ˜็ผ“ๅญ˜ (ๆœ€ๅฟซ)
  memory: {
    ttl: 60000,        // 1ๅˆ†้’Ÿ
    maxSize: 1000
  },

// ๆ–‡ไปถ็ผ“ๅญ˜ (ไธญ็ญ‰)
file: {
ttl: 3600000, // 1ๅฐๆ—ถ
location: ".cache/"
},

// API ็ผ“ๅญ˜ (ๆœ€ๆ…ขไฝ†ๆŒไน…)
api: {
ttl: 86400000, // 24ๅฐๆ—ถ
scope: "global"
}
}

23.3 Concurrency control

// ๅนถๅ‘้™ๅˆถ
class ConcurrencyLimit {
  constructor(maxConcurrent) {
    this.maxConcurrent = maxConcurrent;
    this.running = 0;
    this.queue = [];
  }

async run(fn) {
while (this.running >= this.maxConcurrent) {
await new Promise(resolve => this.queue.push(resolve));
}

this.running++;
try {
  return await fn();
} finally {
  this.running--;
  this.queue.shift()?.();
}

}
}


24. Dependency analysis

24.1 Core Vendor Package

package name

Version

use

@anthropic-ai/sdk

latest

Anthropic API Client

@modelcontextprotocol/sdk

^1.23.0

MCP protocol

react

^19.2.0

UI framework

ink

^6.x

TUI rendering

commander

^12.x

CLI parsing

zod

^3.23.8

Data validation

chalk

^5.x

terminal color

simple-git

^3.28.0

Git operations

24.2 Dependency graph

cli.js
  โ”œโ”€ React (UI ๆก†ๆžถ)
  โ”‚   โ””โ”€ Ink (TUI)
  โ”œโ”€ Commander.js (CLI)
  โ”œโ”€ Anthropic SDK (API)
  โ”œโ”€ MCP SDK (ๆ‰ฉๅฑ•)
  โ”œโ”€ Zod (้ชŒ่ฏ)
  โ”œโ”€ Chalk (ๆ ทๅผ)
  โ””โ”€ simple-git (Git)

Appendix A: Key Document Index

File/module

content

00-runtime.js

module system runtime

4696.js

CLI entry and command definitions

3549.js

Agent main loop

4286.js

API clients and stream processing

3768.js

Tool Execution Manager

4300.js

Permission check core

4441.js

Hook system

2545.js

Read tools

4285.js

Edit tool

3643.js

Write tool


Appendix B: Environment variables

variable

use

ANTHROPIC_API_KEY

API key

ANTHROPIC_AUTH_TOKEN

OAuth token

CLAUDE_CONFIG_DIR

Configure directory override

CLAUDECODE

Detect nested runs

NODE_OPTIONS

Node.js parameters

GITHUB_ACTIONS

GitHub Actions detection


Appendix C: Glossary

the term

illustrate

TTFT

Time to First Token, first byte time

MCP

Model Context Protocol, model context protocol

Agent

Agent, an abstraction of AI entities

Tool

Tools, operations that Agent can perform

Hook

Hooks, event-driven extension points

Skill

Skills, predefined task processing capabilities

Compact

Compression, the operation of reducing context length

REPL

Read-Eval-Print Loop, interactive environment

TUI

Terminal User Interface, terminal user interface


End of report

This report is generated based on decompiled source code analysis of Claude Code v2.1.71


I0veD

I0veD

cyber security researcher

Cloud Native & AI Sec Researcher Red Team | BAS | K8s | Evasion 20+ CVEs | CNVD/CNNVD Contributor ๐Ÿ›ก๏ธ AI-Driven Blue Team ๐Ÿ‘‡ Works

Comments (0)

Login to post a comment.