mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-15 12:55:51 +00:00
* docs: 修复文档巡检发现的 4 处错误 - daemon.md: 反映实际实现状态(supervisor/worker 已实现而非 stub) - bridge-mode.md: API 操作数量从 7 修正为 9 - web-search-tool.md: 文件路径从 src/tools/ 修正为 packages/builtin-tools/src/tools/ - remote-control-self-hosting.md: 补充缺失的 RCS_WS_IDLE_TIMEOUT 和 RCS_WS_KEEPALIVE_INTERVAL 配置项 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 Safety 和 Context 文档中的代码引用和类型错误 - permission-model: 修正规则来源从"五层"到八层,优先级顺序对齐代码 - permission-model: PermissionUpdate 类型改为实际的 addRules/replaceRules 等 - permission-model: 补充 acceptEdits 和 dontAsk 两种权限模式 - permission-model: DENIAL_LIMITS 字段名对齐实际代码 - plan-mode: 工具路径从 src/tools/ 改为 packages/builtin-tools/src/tools/ - compaction: 修正 COMPACTABLE_TOOLS 和 POST_COMPACT_* 的行号 - project-memory: 修正 ENTRYPOINT_NAME 常量的行号 - system-prompt: 修正 SystemPrompt 类型定义文件路径和多个行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复 introduction 文档中的错误路径和行号引用 - why-this-whitepaper.mdx: BashTool 路径从 src/tools/ 修正为 packages/builtin-tools/src/tools/ - what-is-claude-code.mdx: 移除不存在的 Azure provider,改为实际的 7 种 provider - architecture-overview.mdx: State 类型行号从 204 修正为 207 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复 conversation/features 文档中的错误 - streaming.mdx: queryStreamRaw → queryModelWithStreaming 函数名修正 - streaming.mdx: Azure 提供商不存在,替换为实际 7 个提供商 - debug-mode.mdx: --inspect-wait 描述错误,实际使用 BUN_INSPECT 环境变量 - buddy.mdx: 补充缺失的 companionReact.ts、CompanionCard.tsx、index.ts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修复文档巡检中的源码引用错误 - feature-flags.mdx: 修正 feature() 兜底描述,实际从 bun:bundle 导入而非 cli.tsx:3 内联 - feature-flags.mdx: 修正工具 require 路径为 @claude-code-best/builtin-tools 包路径 - ant-only-world.mdx: 修正 tools.ts 中 require 路径为包路径 - ant-only-world.mdx: 修正 INTERNAL_ONLY_COMMANDS 行号 (267-295) 和数量 (24+) - skills.mdx: 修正 COMMANDS memoize 行号 258 → 299 - mcp-protocol.mdx: 修正 fetchToolsForClient LRU 缓存上限 20 → 100 - streaming.mdx: 修正流式事件引用 - file-operations.mdx: 修正工具路径引用 - search-and-navigation.mdx: 修正搜索工具引用 - shell-execution.mdx: 修正 shell 工具引用 - buddy.mdx: 补充缺失的 frontmatter 字段 - debug-mode.mdx: 修正调试模式描述 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 tools/agent 文档中的文件路径和行号引用 - 修正 TodoWriteTool、AgentTool、ToolSearchTool 等工具路径 src/tools/ → packages/builtin-tools/src/tools/ - 更新 Tool.ts、tools.ts、BashTool.tsx 中过时的行号引用 - 修正 WebSearchTool/WebFetchTool/EnterWorktreeTool/ExitWorktreeTool 路径 - 修正 AgentTool.tsx 中多行行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature 文档中的文件路径和行号引用 - ultraplan.md: 更新文件行数(525/349/127) - fork-subagent.md: 路径迁移 src/tools/ → packages/builtin-tools/ - mcp-skills.md: 修正 getMcpSkillCommands 行号 547→604,client.ts 行号 117→129 - kairos.md: 修正 getBriefSection/getProactiveSection 行号 - proactive.md: 修正 getProactiveSection 行号 860→864 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正顶层文档中的路径迁移和行号引用 - auto-updater.md: config.ts 行号 1735→1737,标注未接入启动流程的函数 - external-dependencies.md: WebSearchTool/WebFetchTool 路径迁移到 builtin-tools 包,Vertex 行号修正 - lsp-integration.md: LSPTool 路径从 src/tools/ 迁移到 packages/builtin-tools/ - stub-recovery-design-1-4.md: 修正 Windows 绝对路径链接为标准代码引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 task 文档中的文件扩展名和路径引用 - task-004: AssistantSessionChooser.ts → .tsx, assistant.ts → .tsx - task-003: cli.tsx 行号 249→272, markdownConfigLoader.ts 行号 29→35 - lan-pipes: SendMessageTool 路径迁移到 packages/builtin-tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 补充 computer-use-tools-reference 缺失的 Windows 工具 添加遗漏的 open_terminal 和 activate_window 两个 Windows 专属工具, 修正工具总数 37→39,Windows 工具数 10→12。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 audit/bash-classifier/token-budget/tree-sitter 文档 - feature-flags-audit: ScheduleCronTool 路径迁移、DAEMON 状态更新为 COMPLETE、assistant 文件标记已补全、UDS 标记已实现 - bash-classifier: BashPermissionRequest 文件路径修正、withRetry 行号移除 - token-budget: attachments.ts 行号范围修正 - tree-sitter-bash: bashPermissions.ts 路径迁移到 packages/builtin-tools Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 langfuse-monitoring AgentTool 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 bridgeApi 行号和 Tool.ts 行号引用 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 Safety/Extensibility 文档中的工具路径迁移和行号引用 - sandbox.mdx: shouldUseSandbox.ts 和 bashPermissions.ts 路径迁移至 packages/builtin-tools - why-safety-matters.mdx: bashPermissions.ts 路径迁移(3 处) - plan-mode.mdx: EnterPlanModeTool/prompt.ts 路径迁移 - auto-mode.mdx: Auto mode 指令行号 3464→3481 - hooks.mdx: AgentTool/runAgent.ts 路径迁移 - skills.mdx: SkillTool.ts 路径迁移 - custom-agents.mdx: Agent built-in 目录和 exploreAgent.ts 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 internals 文档引用计数和路径 - ant-only-world: USER_TYPE 引用计数 465→410+,工具路径迁移到 builtin-tools - growthbook-ab-testing: growthbook.ts 行数 1156→1258 - hidden-features: 语音模式状态更新(audio-napi 已恢复) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正工具文档中的行号引用 - sub-agents: AgentTool.call 入口行号 340→387 - shell-execution: ShellCommand onTimeout 行号 129→144 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature 文档中的状态、路径和计数 - all-features-guide: 修正 feature flag 启用范围(dev only vs dev+build) - tier3-stubs: 大量状态修正(stub→已实现),缩减过时条目 - workflow-scripts: 路径迁移到 builtin-tools,状态更新 - web-browser-tool: 工具状态缺失→已实现,路径迁移 - context-collapse: CtxInspectTool 状态缺失→已实现 - computer-use: 行号引用更新,平台分发描述修正 - computer-use-tools-reference: 工具数 39→38 - voice-mode: voiceModeEnabled 行数 55→54 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 更新 the-loop 查询循环行号引用 query.ts 代码变更后终止原因行号整体偏移约 40 行 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 补充 feature-flags-audit 完整 build 默认 feature 列表 添加 ULTRATHINK/LODESTONE/ACP/DAEMON 等 19 个缺失的 build 默认 feature, 修正 dev-only 特征标注(UDS_INBOX/LAN_PIPES/BG_SESSIONS/TEMPLATES) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit ConfigTool 路径迁移 ConfigTool 路径从 src/tools/ 迁移到 packages/builtin-tools/src/tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit BashTool 路径迁移 BashTool 路径从 src/tools/ 迁移到 packages/builtin-tools/src/tools/ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 修正 feature-flags-audit SkillTool 路径迁移 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: 更新 feature-flags-audit WorkflowTool 状态为已实现 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
221 lines
9.8 KiB
Plaintext
221 lines
9.8 KiB
Plaintext
---
|
||
title: "文件操作工具 - 三大工具的源码级解剖"
|
||
description: "逆向分析 FileRead、FileEdit、FileWrite 三大工具的完整执行链路:去重缓存、AST 安全编辑、原子性读写、文件历史快照的实现细节。"
|
||
keywords: ["文件操作", "FileRead", "FileEdit", "FileWrite", "代码编辑", "原子写入"]
|
||
---
|
||
|
||
{/* 本章目标:从源码层面解剖三大文件工具的完整执行链路 */}
|
||
|
||
## 三大工具的职责分化
|
||
|
||
Claude Code 将文件操作拆分为三个独立工具——这不是功能划分,而是**风险分级**:
|
||
|
||
| 工具 | 权限级别 | 核心方法 | 关键属性 |
|
||
|------|---------|---------|---------|
|
||
| **Read** | 只读(免审批) | `isReadOnly() → true` | `maxResultSizeChars: Infinity` |
|
||
| **Edit** | 写入(需确认) | `checkWritePermissionForTool()` | `maxResultSizeChars: 100,000` |
|
||
| **Write** | 写入(需确认) | `checkWritePermissionForTool()` | `maxResultSizeChars: 100,000` |
|
||
|
||
<Tip>
|
||
Read 的 `maxResultSizeChars` 是 `Infinity`,但这并不意味着无限制输出——真正的截断发生在 `validateContentTokens()` 中基于 token 预算的动态判定,而非字符数硬限制。
|
||
</Tip>
|
||
|
||
## FileRead:多模态文件读取引擎
|
||
|
||
源码路径:`packages/builtin-tools/src/tools/FileReadTool/FileReadTool.ts`
|
||
|
||
### 读取去重机制
|
||
|
||
Read 工具有一个常被忽视但至关重要的**去重层**。当 AI 重复读取同一个文件的同一范围时,系统不会浪费 token 发送两份完整内容:
|
||
|
||
```typescript
|
||
// FileReadTool.ts — 去重逻辑
|
||
const existingState = readFileState.get(fullFilePath)
|
||
if (existingState && !existingState.isPartialView && existingState.offset !== undefined) {
|
||
const rangeMatch = existingState.offset === offset && existingState.limit === limit
|
||
if (rangeMatch) {
|
||
const mtimeMs = await getFileModificationTimeAsync(fullFilePath)
|
||
if (mtimeMs === existingState.timestamp) {
|
||
return { data: { type: 'file_unchanged', file: { filePath: file_path } } }
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
关键设计点:
|
||
- 去重仅对 **Read 工具自身的读取**生效(通过 `offset !== undefined` 判定)
|
||
- Edit/Write 也会写入 `readFileState`,但它们的 `offset` 为 `undefined`,所以不会误命中去重
|
||
- 通过 mtime 比对确保文件未被外部修改
|
||
- 有 GrowthBook killswitch(`tengu_read_dedup_killswitch`)可紧急关闭
|
||
|
||
实测数据:BQ proxy 显示约 18% 的 Read 调用是同文件碰撞,占 fleet `cache_creation` 的 2.64%。
|
||
|
||
### 多格式分发:文本、图片、PDF、Notebook 四条路径
|
||
|
||
Read 工具的 `callInner()` 按 `ext` 分发到四条完全不同的处理路径:
|
||
|
||
```
|
||
.ipynb → readNotebook() → JSON cell 解析 → token 校验
|
||
.png/.jpg/.gif/.webp → readImageWithTokenBudget() → 压缩+降采样
|
||
.pdf → extractPDFPages() / readPDF() → 页面级提取
|
||
其他 → readFileInRange() → 分页读取
|
||
```
|
||
|
||
**图片路径的压缩策略**特别精细:
|
||
1. 先用 `maybeResizeAndDownsampleImageBuffer()` 标准缩放
|
||
2. 用 `base64.length * 0.125` 估算 token 数
|
||
3. 超出预算时调用 `compressImageBufferWithTokenLimit()` 激进压缩
|
||
4. 仍然超限时用 sharp 做最后兜底:`resize(400,400).jpeg({quality:20})`
|
||
|
||
**PDF 路径**有页数阈值:超过 `PDF_AT_MENTION_INLINE_THRESHOLD`(默认值在 `apiLimits.ts`)时强制分页读取,每请求最多 `PDF_MAX_PAGES_PER_READ` 页。
|
||
|
||
### 安全防线
|
||
|
||
Read 工具在 `validateInput()` 中设置了多层安全门:
|
||
|
||
1. **设备文件屏蔽**(`BLOCKED_DEVICE_PATHS`):`/dev/zero`、`/dev/random`、`/dev/tty` 等——防止无限输出或阻塞挂起
|
||
2. **二进制文件拒绝**(`hasBinaryExtension`):排除 PDF 和图片扩展名后,阻止读取 `.exe`、`.so` 等二进制文件
|
||
3. **UNC 路径跳过**:Windows 下 `\\server\share` 路径跳过文件系统操作,防止 SMB NTLM 凭据泄露
|
||
4. **权限拒绝规则**(`matchingRuleForInput`):匹配 `deny` 规则后直接拒绝
|
||
|
||
### 文件未找到时的智能建议
|
||
|
||
当文件不存在时,Read 不会只报一个 "file not found":
|
||
|
||
```typescript
|
||
// FileReadTool.ts
|
||
const similarFilename = findSimilarFile(fullFilePath) // 相似扩展名
|
||
const cwdSuggestion = await suggestPathUnderCwd(fullFilePath) // cwd 相对路径建议
|
||
// macOS 截图特殊处理:薄空格(U+202F) vs 普通空格
|
||
const altPath = getAlternateScreenshotPath(fullFilePath)
|
||
```
|
||
|
||
对 macOS 截图文件名中 AM/PM 前的薄空格(U+202F)做了特殊处理——这是实测中发现的跨 macOS 版本兼容性问题。
|
||
|
||
## FileEdit:精确字符串替换引擎
|
||
|
||
源码路径:`packages/builtin-tools/src/tools/FileEditTool/FileEditTool.ts` + `utils.ts`
|
||
|
||
### 引号标准化:AI 无法输出的字符怎么办
|
||
|
||
AI 模型只能输出直引号(`'` `"`),但源码中可能使用弯引号(`'` `'` `"` `"`)。`findActualString()` 函数处理了这个不对齐:
|
||
|
||
```typescript
|
||
// utils.ts:73-93
|
||
export function findActualString(fileContent: string, searchString: string): string | null {
|
||
if (fileContent.includes(searchString)) return searchString // 精确匹配
|
||
const normalizedSearch = normalizeQuotes(searchString) // 弯引号→直引号
|
||
const normalizedFile = normalizeQuotes(fileContent)
|
||
const idx = normalizedFile.indexOf(normalizedSearch)
|
||
if (idx !== -1) return fileContent.substring(idx, idx + searchString.length)
|
||
return null
|
||
}
|
||
```
|
||
|
||
匹配后还有**反向引号保持**(`preserveQuoteStyle`):如果文件用弯引号,替换后的新字符串也自动转换为弯引号,包括缩写中的撇号(如 "don't")。
|
||
|
||
### 原子性读-改-写
|
||
|
||
Edit 工具的 `call()` 方法实现了一个**无锁原子更新**协议:
|
||
|
||
```
|
||
1. await fs.mkdir(dir) ← 确保目录存在(异步,在临界区外)
|
||
2. await fileHistoryTrackEdit() ← 备份旧内容(异步,在临界区外)
|
||
3. readFileSyncWithMetadata() ← 同步读取当前文件内容(临界区开始)
|
||
4. getFileModificationTime() ← mtime 校验
|
||
5. findActualString() ← 引号标准化匹配
|
||
6. getPatchForEdit() ← 计算 diff
|
||
7. writeTextContent() ← 写入磁盘
|
||
8. readFileState.set() ← 更新缓存(临界区结束)
|
||
```
|
||
|
||
步骤 3-8 之间**不允许任何异步操作**(源码注释明确写道:"Please avoid async operations between here and writing to disk to preserve atomicity")。这确保了在 mtime 校验和实际写入之间不会有其他进程修改文件。
|
||
|
||
### 防覆写校验
|
||
|
||
Edit 工具在 `validateInput()` 中检查两个条件:
|
||
1. **必须先读取**(`readFileState` 中有记录且不是局部视图)
|
||
2. **文件未被外部修改**(`mtime` 未变,或全量读取时内容完全一致)
|
||
|
||
```typescript
|
||
// FileEditTool.ts — Windows 特殊处理
|
||
const isFullRead = readTimestamp.offset === undefined && readTimestamp.limit === undefined
|
||
if (isFullRead && fileContent === readTimestamp.content) {
|
||
// 内容不变,安全继续(Windows 云同步/杀毒可能改 mtime)
|
||
}
|
||
```
|
||
|
||
Windows 上的 mtime 可能因云同步、杀毒软件等被修改而不改变内容,因此对全量读取做了内容级比对作为兜底。
|
||
|
||
### 编辑大小限制
|
||
|
||
```typescript
|
||
const MAX_EDIT_FILE_SIZE = 1024 * 1024 * 1024 // 1 GiB
|
||
```
|
||
|
||
超过 1 GiB 的文件直接拒绝编辑——这是 V8 字符串长度限制(~2^30 字符)的安全边界。
|
||
|
||
## FileWrite:全量写入与创建
|
||
|
||
源码路径:`packages/builtin-tools/src/tools/FileWriteTool/FileWriteTool.ts`
|
||
|
||
Write 工具与 Edit 共享大部分基础设施(权限检查、mtime 校验、fileHistory 备份),但有两个关键差异:
|
||
|
||
### 行尾处理
|
||
|
||
```typescript
|
||
// FileWriteTool.ts:300-305 — 关键注释
|
||
// Write is a full content replacement — the model sent explicit line endings
|
||
// in `content` and meant them. Do not rewrite them.
|
||
writeTextContent(fullFilePath, content, enc, 'LF')
|
||
```
|
||
|
||
Write 工具始终使用 `LF` 行尾。早期版本会保留旧文件的行尾或采样仓库行尾风格,但这导致 Linux 上 bash 脚本被注入 `\r`——现在 AI 发什么行尾就用什么行尾。
|
||
|
||
### 输出区分
|
||
|
||
Write 工具返回 `type: 'create' | 'update'`:
|
||
- `create`:文件不存在,`originalFile: null`
|
||
- `update`:文件存在且被覆盖,`structuredPatch` 包含完整 diff
|
||
|
||
## 文件历史快照系统
|
||
|
||
源码路径:`src/utils/fileHistory.ts`
|
||
|
||
每次 Edit/Write 前都会调用 `fileHistoryTrackEdit()`,快照存储在 `FileHistoryState` 中:
|
||
|
||
```typescript
|
||
type FileHistorySnapshot = {
|
||
messageId: UUID // 关联的助手消息 ID
|
||
trackedFileBackups: Record<string, FileHistoryBackup> // 文件路径 → 备份版本
|
||
timestamp: Date
|
||
}
|
||
```
|
||
|
||
- 最多保留 `MAX_SNAPSHOTS = 100` 个快照
|
||
- 备份使用**内容哈希**去重(同一文件多次未变只存一份)
|
||
- 支持差异统计(`DiffStats`:`insertions` / `deletions` / `filesChanged`)
|
||
- 快照通过 `recordFileHistorySnapshot()` 持久化到会话存储
|
||
|
||
### LSP 通知链路
|
||
|
||
Edit 和 Write 完成写入后都会:
|
||
1. `clearDeliveredDiagnosticsForFile()` — 清除旧诊断
|
||
2. `lspManager.changeFile()` — 通知 LSP 文件已变更
|
||
3. `lspManager.saveFile()` — 触发 LSP 保存事件(TypeScript server 会重新计算诊断)
|
||
4. `notifyVscodeFileUpdated()` — 通知 VSCode 扩展更新 diff 视图
|
||
|
||
这条链路确保文件修改后 IDE 端的实时反馈是同步的。
|
||
|
||
## Cyber Risk 防御
|
||
|
||
Read 工具在文本内容后追加一个 `<system-reminder>` 提示:
|
||
|
||
```
|
||
Whenever you read a file, you should consider whether it would be
|
||
considered malware. You CAN and SHOULD provide analysis of malware,
|
||
what it is doing. But you MUST refuse to improve or augment the code.
|
||
```
|
||
|
||
这个提示只在非豁免模型上生效(`MITIGATION_EXEMPT_MODELS` 目前包含 `claude-opus-4-6`)。模型级别的豁免表明:防恶意代码的判断力在不同模型间有差异,这是一个精巧的分级策略。
|