mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-20 15:25:50 +00:00
* feat(workflow): add workflow engine, /workflows panel, /ultracode skill
将 feat/sdk-backend 分支中 workflow 相关的 20 个 commit 压缩为单 commit:
- 工作流引擎核心:phase / agent / parallel / pipeline 编排原语(packages/workflow-engine/)
- /workflows 面板:三区焦点布局(顶部 run tabs + 左侧 phase 侧栏 + 右侧 agent 列表)
- /ultracode skill:多 agent workflow 编排入口
- 进度存储 / journal / notification 系统
- WorkflowService 生命周期管理 + SentryErrorBoundary
- 脚本沙箱:禁用 dynamic import()、JSON args 防御性归一化
- journal 与 named-workflow 路径统一在 projectRoot
- 错误处理:parallel/pipeline hooks 错误日志、failure routing、semaphore abort
- workflow 工具升级为 core 工具 + PascalCase 命名
Co-Authored-By: glm-5.1 <zai-org@claude-code-best.win>
* feat(workflow): 复刻 ultracode 手册并修复 worktree/inline/opt-in 三处缺口
围绕 ultracode skill 审查 agent 系统一致性后:
- ultracode.ts: 用系统提示版完整 Workflow 编排手册替换中文精简版
- HIGH#1 isolation:'worktree': claudeCodeBackend.run() 用 createAgentWorktree +
runWithCwdOverride 包裹 runAgent + finally 清理实现真正的 cwd 隔离;slug 用
sha256(runId:agentId) 派生以匹配 cleanupStaleAgentWorktrees 清理正则
(修 runId 为 w+base36 非 UUID 导致的泄漏盲区);worktree.ts 注释同步修正
- HIGH#2 inline 持久化: 新增 persistInlineScript,WorkflowTool + service 两条
inline 路径对称持久化到 .claude/workflow-runs/<runId>/script.js,返回可复用
scriptPath(闭环 inline→编辑→scriptPath 重提迭代循环)
- HIGH#3 opt-in 分工: ultracode/WorkflowTool/effort 注明 session reminder 由
harness 注入,repo 内无 ultracode 信号,保持 feature('WORKFLOW_SCRIPTS') +
isEnabled 两层 gate,不自造注入
- 测试: 新增 persistInline.test.ts;扩展 claudeCodeBackend(isolation 4 用例)/
WorkflowTool(inline)/service(scriptPath)/ultracode(harness)
含配套 workflow engine/panel 完善与 run-state-persistence design doc。
Co-Authored-By: Claude <noreply@anthropic.com>
* feat(workflow): run 终态落盘 state.json 支持跨重启恢复
终态 RunProgress(含 returnValue/error)此前只在内存 ProgressStore,进程
重启即丢失。本次让其落盘到 .claude/workflow-runs/<runId>/state.json,使
(a) 重启后可按 runId 取 return、(b) /workflows 面板跨重启展示历史 run。
跨进程 resume 明确不在范围。
- persistence.ts: getRunsDir/writeRunState/readRunState/listPersistedRuns
+ attachRunStatePersistence;原子覆盖写(tmp+rename),读容错(缺文件/
损坏/schemaVersion 不符 → null),写 best-effort(IO 失败只 log warn)
- progress/store.ts: 加 hydrate(run) 直接注入磁盘 run(已存在 runId 跳过,
内存优先)
- service.ts: getWorkflowService() 接线 attachRunStatePersistence(bus,
store) 订阅 run_done(completed/failed/killed 三态共用,shutdown-kill
也走同路径,无需额外钩子);WorkflowService 加 getRunAsync(id) 内存
miss→读盘 fallback(不注入内存)+ loadPersistedRuns() 扫盘 hydrate
(persistedLoaded flag 守护幂等)
- panel/WorkflowsPanel.tsx: mount 时调一次 loadPersistedRuns(重 mount
不重复)
- ports.ts: runsDir 改用 getRunsDir() 消除拼接重复
- 测试: persistence.test.ts(11)/runStatePersistence.test.ts(5)/
progressStore(2)/service(5)/WorkflowsPanel(1) 共 24 个新测试;
precheck 5629 pass / 0 fail
设计偏离: 计划原写 monkey-patch getRunsDir 指向 tmpdir,Bun ESM namespace
不可变不可行;改用可选 runsDirProvider 参数(默认 getRunsDir)DI 注入,
加到 attachRunStatePersistence 与 makeService(cwdOverride 之后第 4 参),
与现有 cwdOverride 模式一致。makeService 的 cwdOverride 保持不变,不破坏
inline 持久化特性。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(workflow): 默认并发降为 3 并支持 per-run maxConcurrency 注入
- DEFAULT_MAX_CONCURRENCY=3 替代旧的 min(16, cores-2);MAX_CONCURRENCY_CAP=16 保留为用户输入的绝对上限
- 新增 clampMaxConcurrency() 处理 undefined/<1/>CAP 边界
- WorkflowInput schema 新增 maxConcurrency: number.int().min(1).max(16).optional()
- 引擎层 context/runWorkflow 全链路透传:semaphore 容量来自 per-run 入参
- WorkflowTool prompt 增加指引:fan-out 场景先用 AskUserQuestion 与用户确认并发再启动
- 同步 ultracode skill + audit workflow spec 的并发文字(删 cpu-cores 公式)
- 同步 docs/features/workflow-scripts.md 旧公式
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* fix(workflow): 面板 UI 字符串英文化
WorkflowsPanel 中 4 处面向用户的中文(onDone 错误消息、键位提示行)
改为英文;其他面板组件(AgentList/TabsBar)原本已是英文。代码注释
保留中文,与 workflow 模块惯例一致。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(workflow): 中断系统(x 杀单 agent / K 杀整个 workflow,Dialog 二次确认)
- claudeCodeBackend 桥接 ctx.signal → runAgent.override.abortController(修 'x' 无效根因:abort 到不了内部 fetch)
- AbortError 识别为 throw WorkflowAbortedError(不再吞成 dead,workflow 能感知被 kill)
- ports.taskRegistrar 加 registerAgentAbort/unregisterAgentAbort/killAgent;service.killAgent(runId, agentId) 精确中断
- 面板键位:'x' 杀当前 agent(agents 列聚焦时) / 'K' 杀整个 workflow;Dialog 二次确认 + confirm 模式吞导航键防误触
- 新增测试 8 项(backend signal bridge / hooks inject / ports killAgent / service killAgent)
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* docs(workflow): ultracode skill 加 model tier 选择指引(haiku/sonnet/opus/best 场景匹配)
补足 agent() 已有 model 参数缺的判断依据:列出 4 个 tier 的成本/延迟量级和典型场景,
明确"无法 articulate 为什么换 tier 就 omit"的 rule of thumb。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(workflow): maxConcurrency≠3 必须先 AskUserQuestion(默认 3 推荐值)
把 fan-out 时才问改成任何 maxConcurrency≠3 都必须问。
唯一例外:用户在当前会话已明确说过并发数("use 6" / "maxConcurrency 9")。
prompt (WorkflowTool.ts) + skill (ultracode.ts) + audit spec 三处同步。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(workflow): agent 失败自动重试一次(dead 或非 abort throw)
- hooks.agent 包装 invokeBackend:第一次 dead 或非 abort throw → 重试一次
- WorkflowAbortedError(kill)不重试——是用户意图
- registry.resolve 配置错(AdapterNotFoundError 等)在 try 外直接上抛,不走重试——
配置问题重试无意义且掩盖 bug
- 重试仍失败:dead 保持 dead;throw 降级 dead(不击穿 workflow,
与 parallel/pipeline null-on-error 契约一致)
- budget 不重复扣:dead 不 addOutputTokens,重试 ok 才扣一次
- 新增 7 项 hooks 层重试测试 + 1 项 service 层降级测试
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* fix(workflow): 面板 label 截断保留 #数字 后缀(同 dim 多 finding 可区分)
audit workflow 用 verify:\${dim}#\${findingIdx} 命名 verify agent。
旧逻辑 slice(0, 18) 从右切把 #idx 全吃了——同 dimension 多 finding
肉眼无法区分。新逻辑:含 #数字 后缀时保留后缀,前缀截断 + … 省略号。
例:verify:correctness#0 → verify:correctn…#0
verify:architecture#15 → verify:archite…#15
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(workflow): kill 整个 workflow 后立即回主 chat
run_done→store→notifications.ts 的通知路径已有,但 confirmYes 后面板继续
挂着挡住主 chat,用户看不到"已停止"反馈。kill 后调 onDone() 立即退出面板,
让主 chat 的 `Workflow "<name>" was stopped` 通知直接可见。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* fix(workflow): agent dead 带 reason/detail + prompt 加压 StructuredOutput
12 agent audit workflow 8 个 dead,journal 只记 {kind:"dead"} 无信息,
事后无法区分 "agent 没产 StructuredOutput" vs "runAgent 抛错"。
证据指向主因:sonnet 长 tool chain 后忘记调 StructuredOutput,
extractStructuredOutput 返回 null 即降级 dead。
- types.ts: AgentRunResult.dead 加可选 reason/detail 字段
(no-structured-output / runagent-threw / worktree-failed / unknown)
兼容旧 journal(均 optional)。
- claudeCodeBackend.ts: 三处 dead 填 reason + detail;
no-structured-output 把 finalized 文本前 200 字符做 detail,
让日志/面板能立刻看到 agent 最后说了什么。
- claudeCodeBackend.ts: schema 模式 prompt 首尾各放一次
StructuredOutput 强制要求,针对 sonnet 长 tool chain 后忘记收尾。
- hooks.ts: retry 日志带 reason;retry 仍 throw 时降级 dead 也填
reason=runagent-threw + detail。
- types.test.ts: 加 reason JSON 往返 + 旧 journal 兼容测试。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* fix(workflow): schema 模式弃用 StructuredOutput 工具契约,改鲁棒 JSON 文本解析
上一轮 70a2f76 把"agent 长 tool chain 后忘调 StructuredOutput"当作死因,
加 prompt 头尾双强制。但实测跑 5 个 review agent 4 个 dead,detail 全是
"StructuredOutput tool is not available as a deferred tool"——根因是
该工具从未注入 workflow sub-agent 的工具集(assembleToolPool 默认池不含,
只有 stop_hook 路径 execAgentHook.ts 显式 createStructuredOutputTool())。
prompt 反复要求调一个不可达的工具,agent 困扰、长篇辩解、最终没产 JSON。
- claudeCodeBackend.ts:
- extractStructuredOutput 重写:括号栈扫描替代 indexOf/lastIndexOf,
处理嵌套对象、字符串内的括号、转义符;新增 fenced code block
优先路径(```json / ```),多 JSON 块取第一个 parse 成功的;
只返回 plain object(拒 array/number/string/null)。不做语法修复
(尾逗号/单引号/注释)——避免在字符串内误改(如 "http://" 被 // 注释正则吃)。
- schema 模式 prompt 简化:删首尾双 STRUCTURED OUTPUT 强制(600+ token),
改成指示 agent 在最后文本块 emit raw JSON;明确告知"StructuredOutput
is not available in this environment",消除调用幻觉。
- hooks.ts: detail.slice 用 typeof === 'string' 守卫;catch 块用
e instanceof Error ? e.message : String(e)(旧 journal / 第三方 adapter
可能写非 string detail,直接 .slice 会抛 TypeError 击穿日志)。
- claudeCodeBackend.test.ts: +9 测试覆盖 fenced / 嵌套 / 字符串内括号 /
转义引号 / 多块取首 / 类型守卫 / 损坏 JSON。
precheck: 5663 pass / 0 fail。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* docs(effort): 新增 /effort 交互面板设计 spec
设计要点:
- /effort 无参 → 横向 slider 面板(low/medium/high/xhigh/max/ultracode)
- ←/→ 移动光标,Enter 确认,Esc 取消
- ultracode 仅视觉占位,确认后提示走 /ultracode <context>
- env override 时双标记 + 顶部警告
- 模型不支持时面板禁用
- 两阶段交付:先基础面板 commit,再做 ultracode 波纹动画
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* docs(effort): 新增 EffortPanel 基础面板实施计划(第一阶段)
按 TDD 分 6 个 task:纯函数状态 → keybinding 注册 → 组件 → 命令挂载 → 分支测试 → precheck。
波纹动画在第二阶段单独 commit。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* docs(effort): plan 补 q/ctrl+c 取消绑定,对齐 spec §5 状态机
verifier 抓到的 gap:spec §5 写明 Esc / Ctrl+C / q 都是取消事件,
但 plan Task 2.3 只绑了 escape。补上 q 和 ctrl+c → effortPanel:cancel。
同时把 Step 2.2 直接写成 6 个 action 版本(home/end),删除迂回表达。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* docs(effort): plan 修订执行前 review 发现的 5 处 gap
- Task 3.3 EffortPanel.tsx 草稿:Faster/Smarter padEnd 语法错乱重写;
useKeybindings import 路径从 @anthropic/ink 修正为 ../../keybindings/useKeybinding.js;
移除冗余 renderSeparatorLine;保留 renderPaddedLine
- Task 5.2 computeConfirmOutcome 改为注入 ApplyFn 模式:
避免 effortPanelState → effort.tsx → EffortPanel 循环依赖;
测试可注入 mockApply,无需 mock settings
- Step 5.3 测试代码对齐注入版签名
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(effort): 新增 EffortPanel 纯函数状态模块(PanelPosition + 移动/初始光标)
仅含纯函数与类型,无 React/Ink 依赖,便于单测。
- PANEL_POSITIONS:low → medium → high → xhigh → max → ultracode
- moveLeft/moveRight:边界钳制(low 不再左移、ultracode 不再右移)
- getInitialCursor:env override > displayed level
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(keybindings): 注册 EffortPanel context 与 6 个 action
绑定 ←/→/h/l/home/end/enter/escape/q/ctrl+c 到 effortPanel:* action。
与 ModelPicker context 范式一致,避免左右键被全局 keybinding 拦截。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(effort): 实现 EffortPanel 组件主体(渲染 + 键盘交互 + 确认/取消分支)
- 横向 slider 布局:Faster ↔ Smarter 两极,6 档刻度
- useKeybindings 注册 EffortPanel context(←/→/h/l/home/end/enter/escape/q/ctrl+c)
- Enter 在 5 档之一 → 调 executeEffort 写 settings + AppState
- Enter 在 ultracode → 输出引导文案,不写状态
- Esc/q → "Effort unchanged."
- env override 时顶部黄色警告
- computeConfirmOutcome 注入 ApplyFn,便于测试(Task 5 补测试)
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(effort): /effort 无参时挂载 EffortPanel 交互面板
- 无参 → <EffortPanelWrapper> 透传 AppState.effortValue
- current/status → 仍显示文本(不变)
- 有参 → 直跳 executeEffort(不变)
- help/-h/--help → 不变
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* test(effort): 补 computeConfirmOutcome 分支测试(注入 mockApply)
- ultracode → kind=ultracode-hint,不调 applyFn
- low → kind=apply,message/effortUpdate 来自 applyFn
- applyFn 返回无 effortUpdate 时 outcome.effortUpdate 为 undefined
- CANCEL_MESSAGE / ULTRACODE_HINT 常量
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* fix(effort): 测试里 cursor cast 为 EffortValue,避免 PanelPosition 含 ultracode 触发 TS 错误
computeConfirmOutcome 的 ApplyFn 契约要求 EffortValue,但测试 mockApply 接收 PanelPosition。
实际运行时 computeConfirmOutcome 在 ultracode 档位走 hint 分支不会调 applyFn,
cast 安全。precheck 全量通过:5688 tests / 0 fail。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* fix(effort): 面板对齐与配色修复
- 对齐:用 Box width={SEGMENT} + justifyContent="center" 让 ▲ 与档位名严格居中对齐,
替代之前 string padEnd(11) 与 SEGMENT=12 不一致导致的 1 列偏移
- 配色:所有面板文字改用 theme.claude(Claude Orange rgb(215,119,87)),
替代终端默认紫;分隔线/副标签/底栏用 theme.subtle;env 警告用 theme.warning
- 光标档位的档位名也加粗,强化视觉焦点
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* fix(effort): 面板文字改紫色,ULTRACODE_HINT 英文化
- 颜色:theme.claude(橙)→ theme.purple_FOR_SUBAGENTS_ONLY(Purple 600, rgb(147,51,234)),
覆盖标题、Faster/Smarter、▲、档位名
- ULTRACODE_HINT:中文 → 英文
"ultracode is not an effort level. Use /ultracode <context> to start a multi-agent workflow."
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* fix(effort): 统一用色版——选中 suggestion(蓝),未选中 subtle(灰)
弃用 purple_FOR_SUBAGENTS_ONLY(subagent 专用)。改与项目其他面板一致:
- 选中档位 + ▲:color="suggestion"(Medium blue rgb(87,105,247))+ bold
- 未选中档位 + 空 ▲ 占位:color="subtle"(Light gray rgb(175,175,175))
- 标题 / Faster / Smarter:color="suggestion"
- 分隔线 / 副标签 / 底栏:color="subtle"
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* fix(workflow): 终态前补发 phase_done,面板自动退出 running→terminal 转换
runWorkflow:脚本结束时 hook.phase 不会触发最后一个 phase 的 phase_done,
UI 左栏会永远显示 running。三路径(completed/killed/failed)统一在 run_done
之前补发 emitTerminalPhaseDone。
WorkflowsPanel:抽 isRunTerminatedTransition 纯函数判定 running → terminal,
面板 useEffect 检测到转换后自动退出聚焦。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(effort): 波纹动画纯函数 pickChar/computeRippleLine/mergeLayers + 18 测试
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(effort): useRippleFrame hook 包装 useAnimationFrame,按需订阅时钟
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(effort): EffortPanel 集成波纹背景——cursor 停在 ultracode 时切换波纹模式
仅在 cursor === 'ultracode' 时启用 useRippleFrame,渲染 5 行波纹背景
+ overlay 文字(Faster/Smarter、分隔线、▲、档位名、副标签)。
其余档位保持原 PlainContent 渲染路径不动。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* refactor(effort): 波纹动画从字符密度改为颜色渐变
按原版风格把波纹背景从 INTENSITY_CHARS 密度字符('·∙░▒▓')改为
suggestion 系颜色渐变(transparent → 暗深紫蓝 → suggestion → 高光):
rippleAnimation.ts:
- 删除 pickChar / INTENSITY_CHARS / WAVE_PEAK_CHARS / mergeLayers
- 新增 intensityToColor(intensity) → 'transparent' | '#xxxxxx'
- 新增 computeRippleCells 返回 Cell[](每位置 char+color)
- 新增 applyOverlaysToCells(cells, overlays) 替代 mergeLayers
- 新增 cellsToSegments(cells) 合并相邻同色段(减少 Text 节点)
EffortPanel.tsx:
- RippleContent 用 cells→segments→tokens 渲染
- 空格段用 BaseText backgroundColor 染色块(纯色块视觉)
- 文字段用 Text color 染色(亮色突出)
- tokens 按空格/文字二次拆分,避免混合段渲染歧义
测试: 29 个 rippleAnimation 测试覆盖 intensityToColor 边界、
computeRippleCells 长度/震源/衰减、applyOverlaysToCells 覆盖/截断/
防御式拷贝、cellsToSegments 合并逻辑。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* fix(effort): 波纹参数调优——铺满左侧 + 速度调慢 + 全面板有底色
用户反馈三个问题:
1. "低峰部分没有颜色变化" → intensity ≤ 0.1 返回 transparent 导致波谷
位置看不见。改为永不返回 transparent,最低档 #0a0d1a 作为面板
底色(暗紫黑海洋),波峰在底色上流动。
2. "波浪速度太快" → time 系数 0.012 → 0.004(约 1/3 速)。波峰移动
速度从 34 cell/s 降到 11 cell/s,每帧颜色变化从 45% 降到 36%。
3. "波浪只到中间部分,没覆盖左侧" → falloff 覆盖半径 40 → 90。
震源 x=65,左侧 dist=65 < 90,波纹可达最左端(约 30-50% 覆盖)。
色阶调整:
- 删除 transparent 档,新增 #0a0d1a 作最暗档(底色)
- 最高档从 #8aa0ff(高光)改为 #5769F7(suggestion),避免与
文字 overlay 同色互相吞噬
- 7 档颜色:#0a0d1a → #15182b → #1f2543 → #2a3360 →
#3a4582 → #4a5bb0 → #5769F7
测试:删除 transparent 期望,改为期望具体颜色(#0a0d1a 等)。
新增"覆盖半径扩大"测试验证 dist=65 仍有非最暗颜色。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* fix(effort): 波纹 v3 — 去黑边 + 删中心高频涟漪 + y 轴覆盖快捷键行
用户反馈三个问题:
1. "黑色边感觉不太对" — 最暗档 #0a0d1a (rgb 10,13,26) 太接近纯黑,
远端波谷看起来像硬黑边。改为 #1a1f3a (rgb 26,31,58),紫蓝感
更强而非纯黑。
2. "中心的快速波纹有点奇怪" — 删除震源附近 dist<6 的高频涟漪叠加
(time*0.02,5 倍主波纹频率)。原本想让震源附近"水波感"更强,
实际效果像"快速闪烁"反而突兀。主波纹已经足够,无需叠加。
3. "y 方向覆盖快捷键" — RippleContent 新增 y=2 行渲染快捷键 overlay
("←/→ adjust · Enter confirm · Esc cancel")。PlainContent 路径
保持原 Box marginTop=1 + Text 渲染。
色阶调整(紫蓝感更强):
- #1a1f3a (原 #0a0d1a) — 最暗档
- #1f2543 / #252c55 / #2e3870 / #3a4582 / #4a5bb0 / #5769F7
(中间档略调亮度,保持平滑过渡)
测试:震源点测试更新为"time=0 时波谷最暗,time 推进后扫过波峰变亮",
反映删除高频涟漪后的纯主波纹行为。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* chore(workflow): 工作流相关代码中文文案全部英文化
源码(src/workflow/ + packages/workflow-engine/src/)的中文注释、
用户可见错误消息、字符串字面量;测试文件的标题与注释;同步 6 条
硬编码断言到英文化后的错误消息。
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* feat(effort): 波纹 v4 — 平滑波 + 全色环旋转 + 淡入淡出 + 宽度自适应
- 波函数改 (sin+1)/2:消除 max(0,sin) 平直暗带(约 6 行宽)
- 主色相连续旋转(0.03°/ms,12s/圈全色环):蓝→紫→品红→红→橙→黄→绿→青
- 文字 overlay 同步色相旋转(rotateHue 应用到 Faster/▲/档位名/分隔线/副标签)
- 淡入淡出动画:fadeColor/fadeCells + fade 状态机 ~300ms 进出过渡
- 副标签固定 ultracode 段下方,不跟随光标移动
- 顶部/底部各加一行纯波纹行,视觉一致
- 宽度自适应终端列数:窄则 72,宽则铺满(computeSegment/computeRippleSourceX)
- 快捷键改 plain Text,不参与波纹背景渲染
- 新增 18 测试(fadeColor/fadeCells/rotateHue/getHueShiftAtTime)
Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
* refactor: remove CYBER_RISK_MITIGATION_REMINDER from FileReadTool
Co-Authored-By: deepseek-v4-pro <deepseek-ai@claude-code-best.win>
* fix: prevent ReDoS in extractMeta regex by anchoring to splice boundary
Co-Authored-By: deepseek-v4-pro <deepseek-ai@claude-code-best.win>
* chore: 更新脚本
---------
Co-authored-by: glm-5.1 <zai-org@claude-code-best.win>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: deepseek-v4-pro <deepseek-ai@claude-code-best.win>
1176 lines
38 KiB
TypeScript
1176 lines
38 KiB
TypeScript
import type { Base64ImageSource } from '@anthropic-ai/sdk/resources/index.mjs'
|
|
import { readdir, readFile as readFileAsync } from 'fs/promises'
|
|
import * as path from 'path'
|
|
import { posix, win32 } from 'path'
|
|
import { z } from 'zod/v4'
|
|
import {
|
|
PDF_AT_MENTION_INLINE_THRESHOLD,
|
|
PDF_EXTRACT_SIZE_THRESHOLD,
|
|
PDF_MAX_PAGES_PER_READ,
|
|
} from 'src/constants/apiLimits.js'
|
|
import { hasBinaryExtension } from 'src/constants/files.js'
|
|
import { memoryFreshnessNote } from 'src/memdir/memoryAge.js'
|
|
import { getFeatureValue_CACHED_MAY_BE_STALE } from 'src/services/analytics/growthbook.js'
|
|
import { logEvent } from 'src/services/analytics/index.js'
|
|
import {
|
|
type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
getFileExtensionForAnalytics,
|
|
} from 'src/services/analytics/metadata.js'
|
|
import {
|
|
countTokensWithAPI,
|
|
roughTokenCountEstimationForFileType,
|
|
} from 'src/services/tokenEstimation.js'
|
|
import {
|
|
activateConditionalSkillsForPaths,
|
|
addSkillDirectories,
|
|
discoverSkillDirsForPaths,
|
|
} from 'src/skills/loadSkillsDir.js'
|
|
import type { ToolUseContext } from 'src/Tool.js'
|
|
import { buildTool, type ToolDef } from 'src/Tool.js'
|
|
import { getCwd } from 'src/utils/cwd.js'
|
|
import { getClaudeConfigHomeDir, isEnvTruthy } from 'src/utils/envUtils.js'
|
|
import { getErrnoCode, isENOENT } from 'src/utils/errors.js'
|
|
import {
|
|
addLineNumbers,
|
|
FILE_NOT_FOUND_CWD_NOTE,
|
|
findSimilarFile,
|
|
getFileModificationTimeAsync,
|
|
suggestPathUnderCwd,
|
|
} from 'src/utils/file.js'
|
|
import { logFileOperation } from 'src/utils/fileOperationAnalytics.js'
|
|
import { formatFileSize } from 'src/utils/format.js'
|
|
import { getFsImplementation } from 'src/utils/fsOperations.js'
|
|
import {
|
|
compressImageBufferWithTokenLimit,
|
|
createImageMetadataText,
|
|
detectImageFormatFromBuffer,
|
|
type ImageDimensions,
|
|
ImageResizeError,
|
|
maybeResizeAndDownsampleImageBuffer,
|
|
} from 'src/utils/imageResizer.js'
|
|
import { lazySchema } from 'src/utils/lazySchema.js'
|
|
import { logError } from 'src/utils/log.js'
|
|
import { isAutoMemFile } from 'src/utils/memoryFileDetection.js'
|
|
import { createUserMessage } from 'src/utils/messages.js'
|
|
import {
|
|
mapNotebookCellsToToolResult,
|
|
readNotebook,
|
|
} from 'src/utils/notebook.js'
|
|
import { expandPath } from 'src/utils/path.js'
|
|
import { extractPDFPages, getPDFPageCount, readPDF } from 'src/utils/pdf.js'
|
|
import {
|
|
isPDFExtension,
|
|
isPDFSupported,
|
|
parsePDFPageRange,
|
|
} from 'src/utils/pdfUtils.js'
|
|
import {
|
|
checkReadPermissionForTool,
|
|
matchingRuleForInput,
|
|
} from 'src/utils/permissions/filesystem.js'
|
|
import type { PermissionDecision } from 'src/utils/permissions/PermissionResult.js'
|
|
import { matchWildcardPattern } from 'src/utils/permissions/shellRuleMatching.js'
|
|
import { readFileInRange } from 'src/utils/readFileInRange.js'
|
|
import { semanticNumber } from 'src/utils/semanticNumber.js'
|
|
import { jsonStringify } from 'src/utils/slowOperations.js'
|
|
import { BASH_TOOL_NAME } from '../BashTool/toolName.js'
|
|
import { getDefaultFileReadingLimits } from './limits.js'
|
|
import {
|
|
DESCRIPTION,
|
|
FILE_READ_TOOL_NAME,
|
|
FILE_UNCHANGED_STUB,
|
|
LINE_FORMAT_INSTRUCTION,
|
|
OFFSET_INSTRUCTION_DEFAULT,
|
|
OFFSET_INSTRUCTION_TARGETED,
|
|
renderPromptTemplate,
|
|
} from './prompt.js'
|
|
import {
|
|
getToolUseSummary,
|
|
renderToolResultMessage,
|
|
renderToolUseErrorMessage,
|
|
renderToolUseMessage,
|
|
renderToolUseTag,
|
|
userFacingName,
|
|
} from './UI.js'
|
|
|
|
// Device files that would hang the process: infinite output or blocking input.
|
|
// Checked by path only (no I/O). Safe devices like /dev/null are intentionally omitted.
|
|
const BLOCKED_DEVICE_PATHS = new Set([
|
|
// Infinite output — never reach EOF
|
|
'/dev/zero',
|
|
'/dev/random',
|
|
'/dev/urandom',
|
|
'/dev/full',
|
|
// Blocks waiting for input
|
|
'/dev/stdin',
|
|
'/dev/tty',
|
|
'/dev/console',
|
|
// Nonsensical to read
|
|
'/dev/stdout',
|
|
'/dev/stderr',
|
|
// fd aliases for stdin/stdout/stderr
|
|
'/dev/fd/0',
|
|
'/dev/fd/1',
|
|
'/dev/fd/2',
|
|
])
|
|
|
|
function isBlockedDevicePath(filePath: string): boolean {
|
|
if (BLOCKED_DEVICE_PATHS.has(filePath)) return true
|
|
// /proc/self/fd/0-2 and /proc/<pid>/fd/0-2 are Linux aliases for stdio
|
|
if (
|
|
filePath.startsWith('/proc/') &&
|
|
(filePath.endsWith('/fd/0') ||
|
|
filePath.endsWith('/fd/1') ||
|
|
filePath.endsWith('/fd/2'))
|
|
)
|
|
return true
|
|
return false
|
|
}
|
|
|
|
// Narrow no-break space (U+202F) used by some macOS versions in screenshot filenames
|
|
const THIN_SPACE = String.fromCharCode(8239)
|
|
|
|
/**
|
|
* Resolves macOS screenshot paths that may have different space characters.
|
|
* macOS uses either regular space or thin space (U+202F) before AM/PM in screenshot
|
|
* filenames depending on the macOS version. This function tries the alternate space
|
|
* character if the file doesn't exist with the given path.
|
|
*
|
|
* @param filePath - The normalized file path to resolve
|
|
* @returns The path to the actual file on disk (may differ in space character)
|
|
*/
|
|
/**
|
|
* For macOS screenshot paths with AM/PM, the space before AM/PM may be a
|
|
* regular space or a thin space depending on the macOS version. Returns
|
|
* the alternate path to try if the original doesn't exist, or undefined.
|
|
*/
|
|
function getAlternateScreenshotPath(filePath: string): string | undefined {
|
|
const filename = path.basename(filePath)
|
|
const amPmPattern = /^(.+)([ \u202F])(AM|PM)(\.png)$/
|
|
const match = filename.match(amPmPattern)
|
|
if (!match) return undefined
|
|
|
|
const currentSpace = match[2]
|
|
const alternateSpace = currentSpace === ' ' ? THIN_SPACE : ' '
|
|
return filePath.replace(
|
|
`${currentSpace}${match[3]}${match[4]}`,
|
|
`${alternateSpace}${match[3]}${match[4]}`,
|
|
)
|
|
}
|
|
|
|
// File read listeners - allows other services to be notified when files are read
|
|
type FileReadListener = (filePath: string, content: string) => void
|
|
const fileReadListeners: FileReadListener[] = []
|
|
|
|
export function registerFileReadListener(
|
|
listener: FileReadListener,
|
|
): () => void {
|
|
fileReadListeners.push(listener)
|
|
return () => {
|
|
const i = fileReadListeners.indexOf(listener)
|
|
if (i >= 0) fileReadListeners.splice(i, 1)
|
|
}
|
|
}
|
|
|
|
export class MaxFileReadTokenExceededError extends Error {
|
|
constructor(
|
|
public tokenCount: number,
|
|
public maxTokens: number,
|
|
) {
|
|
super(
|
|
`File content (${tokenCount} tokens) exceeds maximum allowed tokens (${maxTokens}). Use offset and limit parameters to read specific portions of the file, or search for specific content instead of reading the whole file.`,
|
|
)
|
|
this.name = 'MaxFileReadTokenExceededError'
|
|
}
|
|
}
|
|
|
|
// Common image extensions
|
|
const IMAGE_EXTENSIONS = new Set(['png', 'jpg', 'jpeg', 'gif', 'webp'])
|
|
|
|
/**
|
|
* Detects if a file path is a session-related file for analytics logging.
|
|
* Only matches files within the Claude config directory (e.g., ~/.claude).
|
|
* Returns the type of session file or null if not a session file.
|
|
*/
|
|
function detectSessionFileType(
|
|
filePath: string,
|
|
): 'session_memory' | 'session_transcript' | null {
|
|
const configDir = getClaudeConfigHomeDir()
|
|
|
|
// Only match files within the Claude config directory
|
|
if (!filePath.startsWith(configDir)) {
|
|
return null
|
|
}
|
|
|
|
// Normalize path to use forward slashes for consistent matching across platforms
|
|
const normalizedPath = filePath.split(win32.sep).join(posix.sep)
|
|
|
|
// Session memory files: ~/.claude/session-memory/*.md (including summary.md)
|
|
if (
|
|
normalizedPath.includes('/session-memory/') &&
|
|
normalizedPath.endsWith('.md')
|
|
) {
|
|
return 'session_memory'
|
|
}
|
|
|
|
// Session JSONL transcript files: ~/.claude/projects/*/*.jsonl
|
|
if (
|
|
normalizedPath.includes('/projects/') &&
|
|
normalizedPath.endsWith('.jsonl')
|
|
) {
|
|
return 'session_transcript'
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
const inputSchema = lazySchema(() =>
|
|
z.strictObject({
|
|
file_path: z.string().describe('The absolute path to the file to read'),
|
|
offset: semanticNumber(z.number().int().nonnegative().optional()).describe(
|
|
'The line number to start reading from. Only provide if the file is too large to read at once',
|
|
),
|
|
limit: semanticNumber(z.number().int().positive().optional()).describe(
|
|
'The number of lines to read. Only provide if the file is too large to read at once.',
|
|
),
|
|
pages: z
|
|
.string()
|
|
.optional()
|
|
.describe(
|
|
`Page range for PDF files (e.g., "1-5", "3", "10-20"). Only applicable to PDF files. Maximum ${PDF_MAX_PAGES_PER_READ} pages per request.`,
|
|
),
|
|
}),
|
|
)
|
|
type InputSchema = ReturnType<typeof inputSchema>
|
|
|
|
export type Input = z.infer<InputSchema>
|
|
|
|
const outputSchema = lazySchema(() => {
|
|
// Define the media types supported for images
|
|
const imageMediaTypes = z.enum([
|
|
'image/jpeg',
|
|
'image/png',
|
|
'image/gif',
|
|
'image/webp',
|
|
])
|
|
|
|
return z.discriminatedUnion('type', [
|
|
z.object({
|
|
type: z.literal('text'),
|
|
file: z.object({
|
|
filePath: z.string().describe('The path to the file that was read'),
|
|
content: z.string().describe('The content of the file'),
|
|
numLines: z
|
|
.number()
|
|
.describe('Number of lines in the returned content'),
|
|
startLine: z.number().describe('The starting line number'),
|
|
totalLines: z.number().describe('Total number of lines in the file'),
|
|
}),
|
|
}),
|
|
z.object({
|
|
type: z.literal('image'),
|
|
file: z.object({
|
|
base64: z.string().describe('Base64-encoded image data'),
|
|
type: imageMediaTypes.describe('The MIME type of the image'),
|
|
originalSize: z.number().describe('Original file size in bytes'),
|
|
dimensions: z
|
|
.object({
|
|
originalWidth: z
|
|
.number()
|
|
.optional()
|
|
.describe('Original image width in pixels'),
|
|
originalHeight: z
|
|
.number()
|
|
.optional()
|
|
.describe('Original image height in pixels'),
|
|
displayWidth: z
|
|
.number()
|
|
.optional()
|
|
.describe('Displayed image width in pixels (after resizing)'),
|
|
displayHeight: z
|
|
.number()
|
|
.optional()
|
|
.describe('Displayed image height in pixels (after resizing)'),
|
|
})
|
|
.optional()
|
|
.describe('Image dimension info for coordinate mapping'),
|
|
}),
|
|
}),
|
|
z.object({
|
|
type: z.literal('notebook'),
|
|
file: z.object({
|
|
filePath: z.string().describe('The path to the notebook file'),
|
|
cells: z.array(z.any()).describe('Array of notebook cells'),
|
|
}),
|
|
}),
|
|
z.object({
|
|
type: z.literal('pdf'),
|
|
file: z.object({
|
|
filePath: z.string().describe('The path to the PDF file'),
|
|
base64: z.string().describe('Base64-encoded PDF data'),
|
|
originalSize: z.number().describe('Original file size in bytes'),
|
|
}),
|
|
}),
|
|
z.object({
|
|
type: z.literal('parts'),
|
|
file: z.object({
|
|
filePath: z.string().describe('The path to the PDF file'),
|
|
originalSize: z.number().describe('Original file size in bytes'),
|
|
count: z.number().describe('Number of pages extracted'),
|
|
outputDir: z
|
|
.string()
|
|
.describe('Directory containing extracted page images'),
|
|
}),
|
|
}),
|
|
z.object({
|
|
type: z.literal('file_unchanged'),
|
|
file: z.object({
|
|
filePath: z.string().describe('The path to the file'),
|
|
}),
|
|
}),
|
|
])
|
|
})
|
|
type OutputSchema = ReturnType<typeof outputSchema>
|
|
|
|
export type Output = z.infer<OutputSchema>
|
|
|
|
export const FileReadTool = buildTool({
|
|
name: FILE_READ_TOOL_NAME,
|
|
searchHint: 'read files, images, PDFs, notebooks',
|
|
// Output is bounded by maxTokens (validateContentTokens). Results exceeding
|
|
// 100KB are persisted to disk (reducing memory pressure in long sessions)
|
|
// rather than kept in the message array indefinitely.
|
|
maxResultSizeChars: 100_000,
|
|
strict: true,
|
|
async description() {
|
|
return DESCRIPTION
|
|
},
|
|
async prompt() {
|
|
const limits = getDefaultFileReadingLimits()
|
|
const maxSizeInstruction = limits.includeMaxSizeInPrompt
|
|
? `. Files larger than ${formatFileSize(limits.maxSizeBytes)} will return an error; use offset and limit for larger files`
|
|
: ''
|
|
const offsetInstruction = limits.targetedRangeNudge
|
|
? OFFSET_INSTRUCTION_TARGETED
|
|
: OFFSET_INSTRUCTION_DEFAULT
|
|
return renderPromptTemplate(
|
|
pickLineFormatInstruction(),
|
|
maxSizeInstruction,
|
|
offsetInstruction,
|
|
)
|
|
},
|
|
get inputSchema(): InputSchema {
|
|
return inputSchema()
|
|
},
|
|
get outputSchema(): OutputSchema {
|
|
return outputSchema()
|
|
},
|
|
userFacingName,
|
|
getToolUseSummary,
|
|
getActivityDescription(input) {
|
|
const summary = getToolUseSummary(input)
|
|
return summary ? `Reading ${summary}` : 'Reading file'
|
|
},
|
|
isConcurrencySafe() {
|
|
return true
|
|
},
|
|
isReadOnly() {
|
|
return true
|
|
},
|
|
toAutoClassifierInput(input) {
|
|
return input.file_path
|
|
},
|
|
isSearchOrReadCommand() {
|
|
return { isSearch: false, isRead: true }
|
|
},
|
|
getPath({ file_path }): string {
|
|
return file_path || getCwd()
|
|
},
|
|
backfillObservableInput(input) {
|
|
// hooks.mdx documents file_path as absolute; expand so hook allowlists
|
|
// can't be bypassed via ~ or relative paths.
|
|
if (typeof input.file_path === 'string') {
|
|
input.file_path = expandPath(input.file_path)
|
|
}
|
|
},
|
|
async preparePermissionMatcher({ file_path }) {
|
|
return pattern => matchWildcardPattern(pattern, file_path)
|
|
},
|
|
async checkPermissions(input, context): Promise<PermissionDecision> {
|
|
const appState = context.getAppState()
|
|
return checkReadPermissionForTool(
|
|
FileReadTool,
|
|
input,
|
|
appState.toolPermissionContext,
|
|
)
|
|
},
|
|
renderToolUseMessage,
|
|
renderToolUseTag,
|
|
renderToolResultMessage,
|
|
// UI.tsx:140 — ALL types render summary chrome only: "Read N lines",
|
|
// "Read image (42KB)". Never the content itself. The model-facing
|
|
// serialization (below) sends content + line prefixes; UI shows none of it.
|
|
extractSearchText() {
|
|
return ''
|
|
},
|
|
renderToolUseErrorMessage,
|
|
async validateInput({ file_path, pages }, toolUseContext: ToolUseContext) {
|
|
// Validate pages parameter (pure string parsing, no I/O)
|
|
if (pages !== undefined) {
|
|
const parsed = parsePDFPageRange(pages)
|
|
if (!parsed) {
|
|
return {
|
|
result: false,
|
|
message: `Invalid pages parameter: "${pages}". Use formats like "1-5", "3", or "10-20". Pages are 1-indexed.`,
|
|
errorCode: 7,
|
|
}
|
|
}
|
|
const rangeSize =
|
|
parsed.lastPage === Infinity
|
|
? PDF_MAX_PAGES_PER_READ + 1
|
|
: parsed.lastPage - parsed.firstPage + 1
|
|
if (rangeSize > PDF_MAX_PAGES_PER_READ) {
|
|
return {
|
|
result: false,
|
|
message: `Page range "${pages}" exceeds maximum of ${PDF_MAX_PAGES_PER_READ} pages per request. Please use a smaller range.`,
|
|
errorCode: 8,
|
|
}
|
|
}
|
|
}
|
|
|
|
// Path expansion + deny rule check (no I/O)
|
|
const fullFilePath = expandPath(file_path)
|
|
|
|
const appState = toolUseContext.getAppState()
|
|
const denyRule = matchingRuleForInput(
|
|
fullFilePath,
|
|
appState.toolPermissionContext,
|
|
'read',
|
|
'deny',
|
|
)
|
|
if (denyRule !== null) {
|
|
return {
|
|
result: false,
|
|
message:
|
|
'File is in a directory that is denied by your permission settings.',
|
|
errorCode: 1,
|
|
}
|
|
}
|
|
|
|
// SECURITY: UNC path check (no I/O) — defer filesystem operations
|
|
// until after user grants permission to prevent NTLM credential leaks
|
|
const isUncPath =
|
|
fullFilePath.startsWith('\\\\') || fullFilePath.startsWith('//')
|
|
if (isUncPath) {
|
|
return { result: true }
|
|
}
|
|
|
|
// Binary extension check (string check on extension only, no I/O).
|
|
// PDF, images, and SVG are excluded - this tool renders them natively.
|
|
const ext = path.extname(fullFilePath).toLowerCase()
|
|
if (
|
|
hasBinaryExtension(fullFilePath) &&
|
|
!isPDFExtension(ext) &&
|
|
!IMAGE_EXTENSIONS.has(ext.slice(1))
|
|
) {
|
|
return {
|
|
result: false,
|
|
message: `This tool cannot read binary files. The file appears to be a binary ${ext} file. Please use appropriate tools for binary file analysis.`,
|
|
errorCode: 4,
|
|
}
|
|
}
|
|
|
|
// Block specific device files that would hang (infinite output or blocking input).
|
|
// This is a path-based check with no I/O — safe special files like /dev/null are allowed.
|
|
if (isBlockedDevicePath(fullFilePath)) {
|
|
return {
|
|
result: false,
|
|
message: `Cannot read '${file_path}': this device file would block or produce infinite output.`,
|
|
errorCode: 9,
|
|
}
|
|
}
|
|
|
|
return { result: true }
|
|
},
|
|
async call(
|
|
{ file_path, offset = 1, limit = undefined, pages },
|
|
context,
|
|
_canUseTool?,
|
|
parentMessage?,
|
|
) {
|
|
const { readFileState, fileReadingLimits } = context
|
|
|
|
const defaults = getDefaultFileReadingLimits()
|
|
const maxSizeBytes =
|
|
fileReadingLimits?.maxSizeBytes ?? defaults.maxSizeBytes
|
|
const maxTokens = fileReadingLimits?.maxTokens ?? defaults.maxTokens
|
|
|
|
// Telemetry: track when callers override default read limits.
|
|
// Only fires on override (low volume) — event count = override frequency.
|
|
if (fileReadingLimits !== undefined) {
|
|
logEvent('tengu_file_read_limits_override', {
|
|
hasMaxTokens: fileReadingLimits.maxTokens !== undefined,
|
|
hasMaxSizeBytes: fileReadingLimits.maxSizeBytes !== undefined,
|
|
})
|
|
}
|
|
|
|
const ext = path.extname(file_path).toLowerCase().slice(1)
|
|
// Use expandPath for consistent path normalization with FileEditTool/FileWriteTool
|
|
// (especially handles whitespace trimming and Windows path separators)
|
|
const fullFilePath = expandPath(file_path)
|
|
|
|
// Dedup: if we've already read this exact range and the file hasn't
|
|
// changed on disk, return a stub instead of re-sending the full content.
|
|
// The earlier Read tool_result is still in context — two full copies
|
|
// waste cache_creation tokens on every subsequent turn. BQ proxy shows
|
|
// ~18% of Read calls are same-file collisions (up to 2.64% of fleet
|
|
// cache_creation). Only applies to text/notebook reads — images/PDFs
|
|
// aren't cached in readFileState so won't match here.
|
|
//
|
|
// Ant soak: 1,734 dedup hits in 2h, no Read error regression.
|
|
// Killswitch pattern: GB can disable if the stub message confuses
|
|
// the model externally.
|
|
// 3P default: killswitch off = dedup enabled. Client-side only — no
|
|
// server support needed, safe for Bedrock/Vertex/Foundry.
|
|
const dedupKillswitch = getFeatureValue_CACHED_MAY_BE_STALE(
|
|
'tengu_read_dedup_killswitch',
|
|
false,
|
|
)
|
|
const existingState = dedupKillswitch
|
|
? undefined
|
|
: readFileState.get(fullFilePath)
|
|
// Only dedup entries that came from a prior Read (offset is always set
|
|
// by Read). Edit/Write store offset=undefined — their readFileState
|
|
// entry reflects post-edit mtime, so deduping against it would wrongly
|
|
// point the model at the pre-edit Read content.
|
|
if (
|
|
existingState &&
|
|
!existingState.isPartialView &&
|
|
existingState.offset !== undefined
|
|
) {
|
|
const rangeMatch =
|
|
existingState.offset === offset && existingState.limit === limit
|
|
if (rangeMatch) {
|
|
try {
|
|
const mtimeMs = await getFileModificationTimeAsync(fullFilePath)
|
|
if (mtimeMs === existingState.timestamp) {
|
|
const analyticsExt = getFileExtensionForAnalytics(fullFilePath)
|
|
logEvent('tengu_file_read_dedup', {
|
|
...(analyticsExt !== undefined && { ext: analyticsExt }),
|
|
})
|
|
return {
|
|
data: {
|
|
type: 'file_unchanged' as const,
|
|
file: { filePath: file_path },
|
|
},
|
|
}
|
|
}
|
|
} catch {
|
|
// stat failed — fall through to full read
|
|
}
|
|
}
|
|
}
|
|
|
|
// Discover skills from this file's path (fire-and-forget, non-blocking)
|
|
// Skip in simple mode - no skills available
|
|
const cwd = getCwd()
|
|
if (!isEnvTruthy(process.env.CLAUDE_CODE_SIMPLE)) {
|
|
const newSkillDirs = await discoverSkillDirsForPaths([fullFilePath], cwd)
|
|
if (newSkillDirs.length > 0) {
|
|
// Store discovered dirs for attachment display
|
|
for (const dir of newSkillDirs) {
|
|
context.dynamicSkillDirTriggers?.add(dir)
|
|
}
|
|
// Don't await - let skill loading happen in the background
|
|
addSkillDirectories(newSkillDirs).catch(() => {})
|
|
}
|
|
|
|
// Activate conditional skills whose path patterns match this file
|
|
activateConditionalSkillsForPaths([fullFilePath], cwd)
|
|
}
|
|
|
|
try {
|
|
return await callInner(
|
|
file_path,
|
|
fullFilePath,
|
|
fullFilePath,
|
|
ext,
|
|
offset,
|
|
limit,
|
|
pages,
|
|
maxSizeBytes,
|
|
maxTokens,
|
|
readFileState,
|
|
context,
|
|
parentMessage?.message.id,
|
|
)
|
|
} catch (error) {
|
|
// Handle file-not-found: suggest similar files
|
|
const code = getErrnoCode(error)
|
|
if (code === 'ENOENT') {
|
|
// macOS screenshots may use a thin space or regular space before
|
|
// AM/PM — try the alternate before giving up.
|
|
const altPath = getAlternateScreenshotPath(fullFilePath)
|
|
if (altPath) {
|
|
try {
|
|
return await callInner(
|
|
file_path,
|
|
fullFilePath,
|
|
altPath,
|
|
ext,
|
|
offset,
|
|
limit,
|
|
pages,
|
|
maxSizeBytes,
|
|
maxTokens,
|
|
readFileState,
|
|
context,
|
|
parentMessage?.message.id,
|
|
)
|
|
} catch (altError) {
|
|
if (!isENOENT(altError)) {
|
|
throw altError
|
|
}
|
|
// Alt path also missing — fall through to friendly error
|
|
}
|
|
}
|
|
|
|
const similarFilename = findSimilarFile(fullFilePath)
|
|
const cwdSuggestion = await suggestPathUnderCwd(fullFilePath)
|
|
let message = `File does not exist. ${FILE_NOT_FOUND_CWD_NOTE} ${getCwd()}.`
|
|
if (cwdSuggestion) {
|
|
message += ` Did you mean ${cwdSuggestion}?`
|
|
} else if (similarFilename) {
|
|
message += ` Did you mean ${similarFilename}?`
|
|
}
|
|
throw new Error(message)
|
|
}
|
|
throw error
|
|
}
|
|
},
|
|
mapToolResultToToolResultBlockParam(data, toolUseID) {
|
|
switch (data.type) {
|
|
case 'image': {
|
|
return {
|
|
tool_use_id: toolUseID,
|
|
type: 'tool_result',
|
|
content: [
|
|
{
|
|
type: 'image',
|
|
source: {
|
|
type: 'base64',
|
|
data: data.file.base64,
|
|
media_type: data.file.type,
|
|
},
|
|
},
|
|
],
|
|
}
|
|
}
|
|
case 'notebook':
|
|
return mapNotebookCellsToToolResult(data.file.cells, toolUseID)
|
|
case 'pdf':
|
|
// Return PDF metadata only - the actual content is sent as a supplemental DocumentBlockParam
|
|
return {
|
|
tool_use_id: toolUseID,
|
|
type: 'tool_result',
|
|
content: `PDF file read: ${data.file.filePath} (${formatFileSize(data.file.originalSize)})`,
|
|
}
|
|
case 'parts':
|
|
// Extracted page images are read and sent as image blocks in mapToolResultToAPIMessage
|
|
return {
|
|
tool_use_id: toolUseID,
|
|
type: 'tool_result',
|
|
content: `PDF pages extracted: ${data.file.count} page(s) from ${data.file.filePath} (${formatFileSize(data.file.originalSize)})`,
|
|
}
|
|
case 'file_unchanged':
|
|
return {
|
|
tool_use_id: toolUseID,
|
|
type: 'tool_result',
|
|
content: FILE_UNCHANGED_STUB,
|
|
}
|
|
case 'text': {
|
|
let content: string
|
|
|
|
if (data.file.content) {
|
|
content = memoryFileFreshnessPrefix(data) + formatFileLines(data.file)
|
|
} else {
|
|
// Determine the appropriate warning message
|
|
content =
|
|
data.file.totalLines === 0
|
|
? '<system-reminder>Warning: the file exists but the contents are empty.</system-reminder>'
|
|
: `<system-reminder>Warning: the file exists but is shorter than the provided offset (${data.file.startLine}). The file has ${data.file.totalLines} lines.</system-reminder>`
|
|
}
|
|
|
|
return {
|
|
tool_use_id: toolUseID,
|
|
type: 'tool_result',
|
|
content,
|
|
}
|
|
}
|
|
}
|
|
},
|
|
} satisfies ToolDef<InputSchema, Output>)
|
|
|
|
function pickLineFormatInstruction(): string {
|
|
return LINE_FORMAT_INSTRUCTION
|
|
}
|
|
|
|
/** Format file content with line numbers. */
|
|
function formatFileLines(file: { content: string; startLine: number }): string {
|
|
return addLineNumbers(file)
|
|
}
|
|
|
|
/**
|
|
* Side-channel from call() to mapToolResultToToolResultBlockParam: mtime
|
|
* of auto-memory files, keyed by the `data` object identity. Avoids
|
|
* adding a presentation-only field to the output schema (which flows
|
|
* into SDK types) and avoids sync fs in the mapper. WeakMap auto-GCs
|
|
* when the data object becomes unreachable after rendering.
|
|
*/
|
|
const memoryFileMtimes = new WeakMap<object, number>()
|
|
|
|
function memoryFileFreshnessPrefix(data: object): string {
|
|
const mtimeMs = memoryFileMtimes.get(data)
|
|
if (mtimeMs === undefined) return ''
|
|
return memoryFreshnessNote(mtimeMs)
|
|
}
|
|
|
|
async function validateContentTokens(
|
|
content: string,
|
|
ext: string,
|
|
maxTokens?: number,
|
|
): Promise<void> {
|
|
const effectiveMaxTokens =
|
|
maxTokens ?? getDefaultFileReadingLimits().maxTokens
|
|
|
|
// Fast rejection: if raw byte count exceeds 4x the token limit,
|
|
// no encoding can possibly fit (worst case is ~4 bytes/token).
|
|
const byteLength = Buffer.byteLength(content)
|
|
if (byteLength > effectiveMaxTokens * 4) {
|
|
throw new MaxFileReadTokenExceededError(
|
|
Math.ceil(byteLength / 4),
|
|
effectiveMaxTokens,
|
|
)
|
|
}
|
|
|
|
const tokenEstimate = roughTokenCountEstimationForFileType(content, ext)
|
|
if (!tokenEstimate || tokenEstimate <= effectiveMaxTokens / 4) return
|
|
|
|
const tokenCount = await countTokensWithAPI(content)
|
|
const effectiveCount = tokenCount ?? tokenEstimate
|
|
|
|
if (effectiveCount > effectiveMaxTokens) {
|
|
throw new MaxFileReadTokenExceededError(effectiveCount, effectiveMaxTokens)
|
|
}
|
|
}
|
|
|
|
type ImageResult = {
|
|
type: 'image'
|
|
file: {
|
|
base64: string
|
|
type: Base64ImageSource['media_type']
|
|
originalSize: number
|
|
dimensions?: ImageDimensions
|
|
}
|
|
}
|
|
|
|
function createImageResponse(
|
|
buffer: Buffer,
|
|
mediaType: string,
|
|
originalSize: number,
|
|
dimensions?: ImageDimensions,
|
|
): ImageResult {
|
|
return {
|
|
type: 'image',
|
|
file: {
|
|
base64: buffer.toString('base64'),
|
|
type: `image/${mediaType}` as Base64ImageSource['media_type'],
|
|
originalSize,
|
|
dimensions,
|
|
},
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Inner implementation of call, separated to allow ENOENT handling in the outer call.
|
|
*/
|
|
async function callInner(
|
|
file_path: string,
|
|
fullFilePath: string,
|
|
resolvedFilePath: string,
|
|
ext: string,
|
|
offset: number,
|
|
limit: number | undefined,
|
|
pages: string | undefined,
|
|
maxSizeBytes: number,
|
|
maxTokens: number,
|
|
readFileState: ToolUseContext['readFileState'],
|
|
context: ToolUseContext,
|
|
messageId: string | undefined,
|
|
): Promise<{
|
|
data: Output
|
|
newMessages?: ReturnType<typeof createUserMessage>[]
|
|
}> {
|
|
// --- Notebook ---
|
|
if (ext === 'ipynb') {
|
|
const cells = await readNotebook(resolvedFilePath)
|
|
const cellsJson = jsonStringify(cells)
|
|
|
|
const cellsJsonBytes = Buffer.byteLength(cellsJson)
|
|
if (cellsJsonBytes > maxSizeBytes) {
|
|
throw new Error(
|
|
`Notebook content (${formatFileSize(cellsJsonBytes)}) exceeds maximum allowed size (${formatFileSize(maxSizeBytes)}). ` +
|
|
`Use ${BASH_TOOL_NAME} with jq to read specific portions:\n` +
|
|
` cat "${file_path}" | jq '.cells[:20]' # First 20 cells\n` +
|
|
` cat "${file_path}" | jq '.cells[100:120]' # Cells 100-120\n` +
|
|
` cat "${file_path}" | jq '.cells | length' # Count total cells\n` +
|
|
` cat "${file_path}" | jq '.cells[] | select(.cell_type=="code") | .source' # All code sources`,
|
|
)
|
|
}
|
|
|
|
await validateContentTokens(cellsJson, ext, maxTokens)
|
|
|
|
// Get mtime via async stat (single call, no prior existence check)
|
|
const stats = await getFsImplementation().stat(resolvedFilePath)
|
|
readFileState.set(fullFilePath, {
|
|
content: cellsJson,
|
|
timestamp: Math.floor(stats.mtimeMs),
|
|
offset,
|
|
limit,
|
|
})
|
|
context.nestedMemoryAttachmentTriggers?.add(fullFilePath)
|
|
|
|
const data = {
|
|
type: 'notebook' as const,
|
|
file: { filePath: file_path, cells },
|
|
}
|
|
|
|
logFileOperation({
|
|
operation: 'read',
|
|
tool: 'FileReadTool',
|
|
filePath: fullFilePath,
|
|
content: cellsJson,
|
|
})
|
|
|
|
return { data }
|
|
}
|
|
|
|
// --- Image (single read, no double-read) ---
|
|
if (IMAGE_EXTENSIONS.has(ext)) {
|
|
// Images have their own size limits (token budget + compression) —
|
|
// don't apply the text maxSizeBytes cap.
|
|
const data = await readImageWithTokenBudget(resolvedFilePath, maxTokens)
|
|
context.nestedMemoryAttachmentTriggers?.add(fullFilePath)
|
|
|
|
logFileOperation({
|
|
operation: 'read',
|
|
tool: 'FileReadTool',
|
|
filePath: fullFilePath,
|
|
content: data.file.base64,
|
|
})
|
|
|
|
const metadataText = data.file.dimensions
|
|
? createImageMetadataText(data.file.dimensions)
|
|
: null
|
|
|
|
return {
|
|
data,
|
|
...(metadataText && {
|
|
newMessages: [
|
|
createUserMessage({ content: metadataText, isMeta: true }),
|
|
],
|
|
}),
|
|
}
|
|
}
|
|
|
|
// --- PDF ---
|
|
if (isPDFExtension(ext)) {
|
|
if (pages) {
|
|
const parsedRange = parsePDFPageRange(pages)
|
|
const extractResult = await extractPDFPages(
|
|
resolvedFilePath,
|
|
parsedRange ?? undefined,
|
|
)
|
|
if (!extractResult.success) {
|
|
throw new Error((extractResult as any).error.message)
|
|
}
|
|
logEvent('tengu_pdf_page_extraction', {
|
|
success: true,
|
|
pageCount: (extractResult as any).data.file.count,
|
|
fileSize: extractResult.data.file.originalSize,
|
|
hasPageRange: true,
|
|
})
|
|
logFileOperation({
|
|
operation: 'read',
|
|
tool: 'FileReadTool',
|
|
filePath: fullFilePath,
|
|
content: `PDF pages ${pages}`,
|
|
})
|
|
const entries = await readdir(extractResult.data.file.outputDir)
|
|
const imageFiles = entries.filter(f => f.endsWith('.jpg')).sort()
|
|
const imageBlocks = await Promise.all(
|
|
imageFiles.map(async f => {
|
|
const imgPath = path.join(extractResult.data.file.outputDir, f)
|
|
const imgBuffer = await readFileAsync(imgPath)
|
|
const resized = await maybeResizeAndDownsampleImageBuffer(
|
|
imgBuffer,
|
|
imgBuffer.length,
|
|
'jpeg',
|
|
)
|
|
return {
|
|
type: 'image' as const,
|
|
source: {
|
|
type: 'base64' as const,
|
|
media_type:
|
|
`image/${resized.mediaType}` as Base64ImageSource['media_type'],
|
|
data: resized.buffer.toString('base64'),
|
|
},
|
|
}
|
|
}),
|
|
)
|
|
return {
|
|
data: extractResult.data,
|
|
...(imageBlocks.length > 0 && {
|
|
newMessages: [
|
|
createUserMessage({ content: imageBlocks, isMeta: true }),
|
|
],
|
|
}),
|
|
}
|
|
}
|
|
|
|
const pageCount = await getPDFPageCount(resolvedFilePath)
|
|
if (pageCount !== null && pageCount > PDF_AT_MENTION_INLINE_THRESHOLD) {
|
|
throw new Error(
|
|
`This PDF has ${pageCount} pages, which is too many to read at once. ` +
|
|
`Use the pages parameter to read specific page ranges (e.g., pages: "1-5"). ` +
|
|
`Maximum ${PDF_MAX_PAGES_PER_READ} pages per request.`,
|
|
)
|
|
}
|
|
|
|
const fs = getFsImplementation()
|
|
const stats = await fs.stat(resolvedFilePath)
|
|
const shouldExtractPages =
|
|
!isPDFSupported() || stats.size > PDF_EXTRACT_SIZE_THRESHOLD
|
|
|
|
if (shouldExtractPages) {
|
|
const extractResult = await extractPDFPages(resolvedFilePath)
|
|
if (extractResult.success) {
|
|
logEvent('tengu_pdf_page_extraction', {
|
|
success: true,
|
|
pageCount: extractResult.data.file.count,
|
|
fileSize: extractResult.data.file.originalSize,
|
|
})
|
|
} else {
|
|
logEvent('tengu_pdf_page_extraction', {
|
|
success: false,
|
|
available: (extractResult as any).error.reason !== 'unavailable',
|
|
fileSize: stats.size,
|
|
})
|
|
}
|
|
}
|
|
|
|
if (!isPDFSupported()) {
|
|
throw new Error(
|
|
'Reading full PDFs is not supported with this model. Use a newer model (Sonnet 3.5 v2 or later), ' +
|
|
`or use the pages parameter to read specific page ranges (e.g., pages: "1-5", maximum ${PDF_MAX_PAGES_PER_READ} pages per request). ` +
|
|
'Page extraction requires poppler-utils: install with `brew install poppler` on macOS or `apt-get install poppler-utils` on Debian/Ubuntu.',
|
|
)
|
|
}
|
|
|
|
const readResult = await readPDF(resolvedFilePath)
|
|
if (!readResult.success) {
|
|
throw new Error((readResult as any).error.message)
|
|
}
|
|
const pdfData = readResult.data
|
|
logFileOperation({
|
|
operation: 'read',
|
|
tool: 'FileReadTool',
|
|
filePath: fullFilePath,
|
|
content: pdfData.file.base64,
|
|
})
|
|
|
|
return {
|
|
data: pdfData,
|
|
newMessages: [
|
|
createUserMessage({
|
|
content: [
|
|
{
|
|
type: 'document',
|
|
source: {
|
|
type: 'base64',
|
|
media_type: 'application/pdf',
|
|
data: pdfData.file.base64,
|
|
},
|
|
},
|
|
],
|
|
isMeta: true,
|
|
}),
|
|
],
|
|
}
|
|
}
|
|
|
|
// --- Text file (single async read via readFileInRange) ---
|
|
const lineOffset = offset === 0 ? 0 : offset - 1
|
|
const { content, lineCount, totalLines, totalBytes, readBytes, mtimeMs } =
|
|
await readFileInRange(
|
|
resolvedFilePath,
|
|
lineOffset,
|
|
limit,
|
|
limit === undefined ? maxSizeBytes : undefined,
|
|
context.abortController.signal,
|
|
)
|
|
|
|
await validateContentTokens(content, ext, maxTokens)
|
|
|
|
readFileState.set(fullFilePath, {
|
|
content,
|
|
timestamp: Math.floor(mtimeMs),
|
|
offset,
|
|
limit,
|
|
})
|
|
context.nestedMemoryAttachmentTriggers?.add(fullFilePath)
|
|
|
|
// Snapshot before iterating — a listener that unsubscribes mid-callback
|
|
// would splice the live array and skip the next listener.
|
|
for (const listener of fileReadListeners.slice()) {
|
|
listener(resolvedFilePath, content)
|
|
}
|
|
|
|
const data = {
|
|
type: 'text' as const,
|
|
file: {
|
|
filePath: file_path,
|
|
content,
|
|
numLines: lineCount,
|
|
startLine: offset,
|
|
totalLines,
|
|
},
|
|
}
|
|
if (isAutoMemFile(fullFilePath)) {
|
|
memoryFileMtimes.set(data, mtimeMs)
|
|
}
|
|
|
|
logFileOperation({
|
|
operation: 'read',
|
|
tool: 'FileReadTool',
|
|
filePath: fullFilePath,
|
|
content,
|
|
})
|
|
|
|
const sessionFileType = detectSessionFileType(fullFilePath)
|
|
const analyticsExt = getFileExtensionForAnalytics(fullFilePath)
|
|
logEvent('tengu_session_file_read', {
|
|
totalLines,
|
|
readLines: lineCount,
|
|
totalBytes,
|
|
readBytes,
|
|
offset,
|
|
...(limit !== undefined && { limit }),
|
|
...(analyticsExt !== undefined && { ext: analyticsExt }),
|
|
...(messageId !== undefined && {
|
|
messageID:
|
|
messageId as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
|
|
}),
|
|
is_session_memory: sessionFileType === 'session_memory',
|
|
is_session_transcript: sessionFileType === 'session_transcript',
|
|
})
|
|
|
|
return { data }
|
|
}
|
|
|
|
/**
|
|
* Reads an image file and applies token-based compression if needed.
|
|
* Reads the file ONCE, then applies standard resize. If the result exceeds
|
|
* the token limit, applies aggressive compression from the same buffer.
|
|
*
|
|
* @param filePath - Path to the image file
|
|
* @param maxTokens - Maximum token budget for the image
|
|
* @returns Image data with appropriate compression applied
|
|
*/
|
|
export async function readImageWithTokenBudget(
|
|
filePath: string,
|
|
maxTokens: number = getDefaultFileReadingLimits().maxTokens,
|
|
maxBytes?: number,
|
|
): Promise<ImageResult> {
|
|
// Read file ONCE — capped to maxBytes to avoid OOM on huge files
|
|
const imageBuffer = await getFsImplementation().readFileBytes(
|
|
filePath,
|
|
maxBytes,
|
|
)
|
|
const originalSize = imageBuffer.length
|
|
|
|
if (originalSize === 0) {
|
|
throw new Error(`Image file is empty: ${filePath}`)
|
|
}
|
|
|
|
const detectedMediaType = detectImageFormatFromBuffer(imageBuffer)
|
|
const detectedFormat = detectedMediaType.split('/')[1] || 'png'
|
|
|
|
// Try standard resize
|
|
let result: ImageResult
|
|
try {
|
|
const resized = await maybeResizeAndDownsampleImageBuffer(
|
|
imageBuffer,
|
|
originalSize,
|
|
detectedFormat,
|
|
)
|
|
result = createImageResponse(
|
|
resized.buffer,
|
|
resized.mediaType,
|
|
originalSize,
|
|
resized.dimensions,
|
|
)
|
|
} catch (e) {
|
|
if (e instanceof ImageResizeError) throw e
|
|
logError(e)
|
|
result = createImageResponse(imageBuffer, detectedFormat, originalSize)
|
|
}
|
|
|
|
// Check if it fits in token budget
|
|
const estimatedTokens = Math.ceil(result.file.base64.length * 0.125)
|
|
if (estimatedTokens > maxTokens) {
|
|
// Aggressive compression from the SAME buffer (no re-read)
|
|
try {
|
|
const compressed = await compressImageBufferWithTokenLimit(
|
|
imageBuffer,
|
|
maxTokens,
|
|
detectedMediaType,
|
|
)
|
|
return {
|
|
type: 'image',
|
|
file: {
|
|
base64: compressed.base64,
|
|
type: compressed.mediaType,
|
|
originalSize,
|
|
},
|
|
}
|
|
} catch (e) {
|
|
logError(e)
|
|
// Fallback: heavily compressed version from the SAME buffer
|
|
try {
|
|
const sharpModule = await import('sharp')
|
|
const sharp =
|
|
(
|
|
sharpModule as unknown as {
|
|
default?: typeof sharpModule
|
|
} & typeof sharpModule
|
|
).default || sharpModule
|
|
|
|
const fallbackBuffer = await (sharp as any)(imageBuffer)
|
|
.resize(400, 400, {
|
|
fit: 'inside',
|
|
withoutEnlargement: true,
|
|
})
|
|
.jpeg({ quality: 20 })
|
|
.toBuffer()
|
|
|
|
return createImageResponse(fallbackBuffer, 'jpeg', originalSize)
|
|
} catch (error) {
|
|
logError(error)
|
|
return createImageResponse(imageBuffer, detectedFormat, originalSize)
|
|
}
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|