mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-15 12:55:51 +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>
1114 lines
38 KiB
Markdown
1114 lines
38 KiB
Markdown
# Workflow Run State Persistence Implementation Plan
|
||
|
||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||
|
||
**Goal:** 让 workflow 的终态 `RunProgress`(含 `returnValue`)落盘到 `.claude/workflow-runs/<runId>/state.json`,跨进程重启可恢复,供 `/workflows` 面板展示历史 run 与按 runId 取 return。
|
||
|
||
**Architecture:** host 侧新增 `persistence.ts` 模块(原子写 + 容错读 + 扫盘列表),引擎层零改动。`service.ts` 订阅 bus 的 `run_done` 事件写盘;`store.ts` 加 `hydrate()` 注入磁盘 run;面板 mount 时扫盘 hydrate;`getRun` 内存 miss 走 async fallback。三种终态(completed/failed/killed)共用 `run_done` 写盘入口,shutdown 时 kill 也走同路径,无需额外钩子。
|
||
|
||
**Tech Stack:** TypeScript strict、Bun runtime、`node:fs/promises`(mkdir/writeFile/readdir/rename)、`bun:test`、现有 `@claude-code-best/workflow-engine` 进度事件总线。
|
||
|
||
**Spec:** `docs/superpowers/specs/2026-06-13-workflow-run-state-persistence-design.md`
|
||
|
||
**Commit 规范提示:** 每个 task 末尾的 commit step 遵循项目 Conventional Commits(中文描述)。实际是否提交由执行决策——项目 CLAUDE.md 要求 commit 需用户显式确认,执行 agent 在 commit 前应问。
|
||
|
||
---
|
||
|
||
## File Structure
|
||
|
||
| 文件 | 改动 | 责任 |
|
||
|---|---|---|
|
||
| `src/workflow/persistence.ts` | 新增 | `getRunsDir()` / `writeRunState(runsDir, run)` / `readRunState(runsDir, runId)` / `listPersistedRuns(runsDir)`;原子覆盖写;容错读 |
|
||
| `src/workflow/__tests__/persistence.test.ts` | 新增 | 持久化往返、原子性、损坏容错、扫盘 |
|
||
| `src/workflow/progress/store.ts` | 改 | `ProgressStore` 类型 + 实现加 `hydrate(run)` |
|
||
| `src/workflow/__tests__/progressStore.test.ts` | 扩展 | hydrate 注入 / 已存在跳过 / 通知 listener |
|
||
| `src/workflow/ports.ts` | 改 | `${getProjectRoot()}/.claude/workflow-runs` → `getRunsDir()` |
|
||
| `src/workflow/service.ts` | 改 | `makeService(ports, store, bus)`;订阅 `run_done` 写盘;`loadPersistedRuns()`;`getRunAsync(id)` fallback;`persistedLoaded` flag |
|
||
| `src/workflow/__tests__/service.test.ts` | 扩展 | run_done 写盘断言、getRunAsync fallback、loadPersistedRuns、签名更新 |
|
||
| `src/workflow/panel/WorkflowsPanel.tsx` | 改 | mount 时 `void svc.loadPersistedRuns()` |
|
||
| `src/workflow/__tests__/WorkflowsPanel.test.tsx` | 扩展 | mount 调一次 loadPersistedRuns(spy) |
|
||
|
||
---
|
||
|
||
## Task 1: persistence.ts + 单测
|
||
|
||
**Files:**
|
||
- Create: `src/workflow/persistence.ts`
|
||
- Create: `src/workflow/__tests__/persistence.test.ts`
|
||
|
||
- [ ] **Step 1: 写失败测试(往返 + 容错)**
|
||
|
||
Create `src/workflow/__tests__/persistence.test.ts`:
|
||
|
||
```ts
|
||
import { expect, test } from 'bun:test'
|
||
import { mkdtemp, rm, readFile, readdir, writeFile as fsWriteFile } from 'node:fs/promises'
|
||
import { tmpdir } from 'node:os'
|
||
import { join } from 'node:path'
|
||
import { writeRunState, readRunState, listPersistedRuns } from '../persistence.js'
|
||
import type { RunProgress } from '../progress/store.js'
|
||
|
||
function makeRun(over: Partial<RunProgress> = {}): RunProgress {
|
||
return {
|
||
runId: 'r1',
|
||
workflowName: 'w',
|
||
status: 'completed',
|
||
phases: [],
|
||
declaredPhases: [],
|
||
currentPhase: null,
|
||
agents: [],
|
||
agentCount: 0,
|
||
startedAt: 1000,
|
||
updatedAt: 2000,
|
||
...over,
|
||
} as RunProgress
|
||
}
|
||
|
||
test('writeRunState → readRunState 往返一致(returnValue 为对象)', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-'))
|
||
try {
|
||
const run = makeRun({ returnValue: { confirmedCount: 2, items: ['a', 'b'] } })
|
||
await writeRunState(dir, run)
|
||
const got = await readRunState(dir, 'r1')
|
||
expect(got).not.toBeNull()
|
||
expect(got!.runId).toBe('r1')
|
||
expect(got!.returnValue).toEqual({ confirmedCount: 2, items: ['a', 'b'] })
|
||
} finally {
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('readRunState 缺文件 → null', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-'))
|
||
try {
|
||
const got = await readRunState(dir, 'never-exists')
|
||
expect(got).toBeNull()
|
||
} finally {
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('readRunState 损坏 JSON → null', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-'))
|
||
try {
|
||
const target = join(dir, 'rX', 'state.json')
|
||
const { mkdir } = await import('node:fs/promises')
|
||
await mkdir(join(dir, 'rX'), { recursive: true })
|
||
await fsWriteFile(target, '{not valid json', 'utf-8')
|
||
const got = await readRunState(dir, 'rX')
|
||
expect(got).toBeNull()
|
||
} finally {
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('readRunState schemaVersion 不符 → null', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-'))
|
||
try {
|
||
const { mkdir } = await import('node:fs/promises')
|
||
await mkdir(join(dir, 'rX'), { recursive: true })
|
||
await fsWriteFile(
|
||
join(dir, 'rX', 'state.json'),
|
||
JSON.stringify({ schemaVersion: 999, run: makeRun({ runId: 'rX' }) }),
|
||
'utf-8',
|
||
)
|
||
const got = await readRunState(dir, 'rX')
|
||
expect(got).toBeNull()
|
||
} finally {
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('writeRunState 原子写:成功后无 tmp 残留', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-'))
|
||
try {
|
||
await writeRunState(dir, makeRun({ runId: 'rAtom' }))
|
||
const sub = await readdir(join(dir, 'rAtom'))
|
||
expect(sub).toContain('state.json')
|
||
expect(sub).not.toContain('state.json.tmp')
|
||
} finally {
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('listPersistedRuns 扫多子目录、跳过无 state.json 的目录、按 updatedAt 降序', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-'))
|
||
try {
|
||
const { mkdir } = await import('node:fs/promises')
|
||
// 三个有效 run + 一个只有 journal 没 state.json 的半残目录
|
||
await writeRunState(dir, makeRun({ runId: 'old', updatedAt: 1000 }))
|
||
await writeRunState(dir, makeRun({ runId: 'mid', updatedAt: 2000 }))
|
||
await writeRunState(dir, makeRun({ runId: 'new', updatedAt: 3000 }))
|
||
await mkdir(join(dir, 'half-broken'), { recursive: true })
|
||
|
||
const runs = await listPersistedRuns(dir)
|
||
expect(runs.map(r => r.runId)).toEqual(['new', 'mid', 'old'])
|
||
} finally {
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('listPersistedRuns 扫到损坏 state.json → 跳过该单个,继续扫其余', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-'))
|
||
try {
|
||
const { mkdir } = await import('node:fs/promises')
|
||
await writeRunState(dir, makeRun({ runId: 'good' }))
|
||
await mkdir(join(dir, 'bad'), { recursive: true })
|
||
await fsWriteFile(join(dir, 'bad', 'state.json'), 'corrupt', 'utf-8')
|
||
|
||
const runs = await listPersistedRuns(dir)
|
||
expect(runs.map(r => r.runId)).toEqual(['good'])
|
||
} finally {
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('writeRunState 不抛 returnValue 为 null/字符串/数组', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-'))
|
||
try {
|
||
await writeRunState(dir, makeRun({ runId: 'n', returnValue: null }))
|
||
await writeRunState(dir, makeRun({ runId: 's', returnValue: 'text' }))
|
||
await writeRunState(dir, makeRun({ runId: 'a', returnValue: [1, 2, 3] }))
|
||
expect((await readRunState(dir, 'n'))!.returnValue).toBeNull()
|
||
expect((await readRunState(dir, 's'))!.returnValue).toBe('text')
|
||
expect((await readRunState(dir, 'a'))!.returnValue).toEqual([1, 2, 3])
|
||
} finally {
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
```
|
||
|
||
- [ ] **Step 2: 运行测试验证失败**
|
||
|
||
Run: `bun test src/workflow/__tests__/persistence.test.ts`
|
||
Expected: FAIL — `Cannot find module '../persistence.js'`
|
||
|
||
- [ ] **Step 3: 实现 persistence.ts**
|
||
|
||
Create `src/workflow/persistence.ts`:
|
||
|
||
```ts
|
||
import { mkdir, readFile, readdir, rename, writeFile } from 'node:fs/promises'
|
||
import { join } from 'node:path'
|
||
import { getProjectRoot } from '../bootstrap/state.js'
|
||
import { logForDebugging } from '../utils/debug.js'
|
||
import type { RunProgress } from './progress/store.js'
|
||
|
||
/** state.json 当前 schema 版本;升级时引入迁移链。 */
|
||
const SCHEMA_VERSION = 1
|
||
const STATE_FILE = 'state.json'
|
||
const STATE_TMP = 'state.json.tmp'
|
||
|
||
/**
|
||
* runsDir 统一来源:与 ports.ts journalStore 同根(${projectRoot}/.claude/workflow-runs)。
|
||
* 提取为函数:消除 ports.ts 与持久化逻辑的路径拼接重复,进入 worktree/子目录时保持同根。
|
||
*/
|
||
export function getRunsDir(): string {
|
||
return join(getProjectRoot(), '.claude', 'workflow-runs')
|
||
}
|
||
|
||
type StateFile = {
|
||
schemaVersion: number
|
||
run: RunProgress
|
||
}
|
||
|
||
/**
|
||
* 原子覆盖写终态 RunProgress 到 <runsDir>/<runId>/state.json。
|
||
* 原子性:writeFile(tmp) → rename(tmp, target),rename 原子;最坏留 tmp,下次写覆盖。
|
||
* 失败 best-effort:IO 异常只 log warn,不抛(workflow 已成功,持久化失败只意味着重启后取不到)。
|
||
*/
|
||
export async function writeRunState(
|
||
runsDir: string,
|
||
run: RunProgress,
|
||
): Promise<void> {
|
||
const dir = join(runsDir, run.runId)
|
||
const target = join(dir, STATE_FILE)
|
||
const tmp = join(dir, STATE_TMP)
|
||
const payload: StateFile = { schemaVersion: SCHEMA_VERSION, run }
|
||
try {
|
||
await mkdir(dir, { recursive: true })
|
||
await writeFile(tmp, JSON.stringify(payload), 'utf-8')
|
||
await rename(tmp, target)
|
||
} catch (e) {
|
||
logForDebugging(
|
||
`[workflow warn] writeRunState failed for ${run.runId}: ${(e as Error).message}`,
|
||
)
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 读 <runsDir>/<runId>/state.json,容错:
|
||
* - 文件不存在 → null(调用方按 miss 处理)
|
||
* - JSON 解析失败 / schema 结构不符 / schemaVersion 不符 → null(log warn,不崩)
|
||
*/
|
||
export async function readRunState(
|
||
runsDir: string,
|
||
runId: string,
|
||
): Promise<RunProgress | null> {
|
||
const target = join(runsDir, runId, STATE_FILE)
|
||
let raw: string
|
||
try {
|
||
raw = await readFile(target, 'utf-8')
|
||
} catch {
|
||
return null
|
||
}
|
||
try {
|
||
const parsed = JSON.parse(raw) as Partial<StateFile>
|
||
if (parsed.schemaVersion !== SCHEMA_VERSION) return null
|
||
const run = parsed.run
|
||
if (!run || typeof run !== 'object') return null
|
||
if (typeof run.runId !== 'string') return null
|
||
if (typeof run.status !== 'string') return null
|
||
return run as RunProgress
|
||
} catch (e) {
|
||
logForDebugging(
|
||
`[workflow warn] readRunState parse failed for ${runId}: ${(e as Error).message}`,
|
||
)
|
||
return null
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 扫描 runsDir 下所有子目录,读取每个 state.json,返回非空 RunProgress 列表。
|
||
* - runsDir 不存在 → 空数组
|
||
* - 某子目录无 state.json(半残 run)→ 跳过
|
||
* - 某子目录 state.json 损坏 → 跳过该单个,继续扫其余
|
||
* - 按 updatedAt 降序(与 store.list() 排序一致)
|
||
*/
|
||
export async function listPersistedRuns(
|
||
runsDir: string,
|
||
): Promise<RunProgress[]> {
|
||
let entries: string[]
|
||
try {
|
||
entries = await readdir(runsDir)
|
||
} catch {
|
||
return []
|
||
}
|
||
const runs: RunProgress[] = []
|
||
for (const name of entries) {
|
||
const run = await readRunState(runsDir, name)
|
||
if (run) runs.push(run)
|
||
}
|
||
return runs.sort((a, b) => b.updatedAt - a.updatedAt)
|
||
}
|
||
```
|
||
|
||
- [ ] **Step 4: 运行测试验证通过**
|
||
|
||
Run: `bun test src/workflow/__tests__/persistence.test.ts`
|
||
Expected: PASS — 8 tests pass
|
||
|
||
- [ ] **Step 5: Commit**
|
||
|
||
```bash
|
||
git add src/workflow/persistence.ts src/workflow/__tests__/persistence.test.ts
|
||
git commit -m "feat(workflow): 添加 run state 持久化模块(原子写 + 容错读)"
|
||
```
|
||
|
||
---
|
||
|
||
## Task 2: store.hydrate + 单测
|
||
|
||
**Files:**
|
||
- Modify: `src/workflow/progress/store.ts`
|
||
- Modify: `src/workflow/__tests__/progressStore.test.ts`
|
||
|
||
- [ ] **Step 1: 写失败测试**
|
||
|
||
Append to `src/workflow/__tests__/progressStore.test.ts`:
|
||
|
||
```ts
|
||
test('hydrate 注入新 run → get 命中 + list 含该项 + 通知 listener', () => {
|
||
const { store } = newStore()
|
||
let notified = 0
|
||
store.subscribe(() => notified++)
|
||
|
||
const historical: RunProgress = {
|
||
runId: 'hist-1',
|
||
workflowName: 'old-job',
|
||
status: 'completed',
|
||
phases: [],
|
||
declaredPhases: [],
|
||
currentPhase: null,
|
||
agents: [],
|
||
agentCount: 5,
|
||
returnValue: { summary: 'past' },
|
||
startedAt: 1,
|
||
updatedAt: 2,
|
||
}
|
||
store.hydrate(historical)
|
||
|
||
expect(store.get('hist-1')).toBe(historical)
|
||
expect(store.list().map(r => r.runId)).toContain('hist-1')
|
||
expect(notified).toBeGreaterThan(0)
|
||
})
|
||
|
||
test('hydrate 已存在的 runId → 跳过(内存优先,不被磁盘覆盖)', () => {
|
||
const { bus, store } = newStore()
|
||
bus.emit({ type: 'run_started', runId: 'r1', workflowName: 'live', meta: null })
|
||
|
||
const stale: RunProgress = {
|
||
runId: 'r1',
|
||
workflowName: 'STALE-SHOULD-NOT-WIN',
|
||
status: 'completed',
|
||
phases: [],
|
||
declaredPhases: [],
|
||
currentPhase: null,
|
||
agents: [],
|
||
agentCount: 0,
|
||
startedAt: 1,
|
||
updatedAt: 2,
|
||
}
|
||
store.hydrate(stale)
|
||
|
||
const got = store.get('r1')!
|
||
expect(got.workflowName).toBe('live')
|
||
expect(got.status).toBe('running')
|
||
})
|
||
```
|
||
|
||
同时在文件顶部 import 添加 `RunProgress` 类型(如尚未导入):
|
||
|
||
```ts
|
||
import type { RunProgress } from '../progress/store.js'
|
||
```
|
||
|
||
- [ ] **Step 2: 运行测试验证失败**
|
||
|
||
Run: `bun test src/workflow/__tests__/progressStore.test.ts`
|
||
Expected: FAIL — `store.hydrate is not a function`
|
||
|
||
- [ ] **Step 3: 实现 hydrate**
|
||
|
||
Modify `src/workflow/progress/store.ts`:
|
||
|
||
在 `ProgressStore` type 加 `hydrate` 成员(在 `get` 之后):
|
||
|
||
```ts
|
||
export type ProgressStore = {
|
||
apply(event: ProgressEvent): void
|
||
list(): RunProgress[]
|
||
get(runId: string): RunProgress | undefined
|
||
/** 直接注入磁盘读出的 run(绕过 bus);已存在的 runId 跳过——内存优先。 */
|
||
hydrate(run: RunProgress): void
|
||
/** 供 useSyncExternalStore:返回稳定引用,无变更时同一数组。 */
|
||
subscribe(listener: () => void): () => void
|
||
getSnapshot(): RunProgress[]
|
||
}
|
||
```
|
||
|
||
在 `createProgressStoreFromBus` 返回对象里加 `hydrate`(在 `get` 之后):
|
||
|
||
```ts
|
||
get: id => byId.get(id),
|
||
hydrate(run) {
|
||
if (byId.has(run.runId)) return
|
||
byId.set(run.runId, run)
|
||
notify()
|
||
},
|
||
subscribe: fn => {
|
||
```
|
||
|
||
- [ ] **Step 4: 运行测试验证通过**
|
||
|
||
Run: `bun test src/workflow/__tests__/progressStore.test.ts`
|
||
Expected: PASS — 所有现有 + 2 个新测试
|
||
|
||
- [ ] **Step 5: Commit**
|
||
|
||
```bash
|
||
git add src/workflow/progress/store.ts src/workflow/__tests__/progressStore.test.ts
|
||
git commit -m "feat(workflow): store 添加 hydrate 用于注入磁盘历史 run"
|
||
```
|
||
|
||
---
|
||
|
||
## Task 3: ports.ts 引用 getRunsDir(消除重复拼接)
|
||
|
||
**Files:**
|
||
- Modify: `src/workflow/ports.ts:72`
|
||
|
||
无测试改动——这是路径来源重构,行为不变(`ports.test.ts` 现有断言覆盖 `journalStore` 创建,路径仍是同一处)。
|
||
|
||
- [ ] **Step 1: 替换 runsDir 拼接**
|
||
|
||
Modify `src/workflow/ports.ts`:
|
||
|
||
import 添加(在现有 `@claude-code-best/workflow-engine` import 之前或之后):
|
||
|
||
```ts
|
||
import { getRunsDir } from './persistence.js'
|
||
```
|
||
|
||
把第 72 行:
|
||
|
||
```ts
|
||
const runsDir = `${getProjectRoot()}/.claude/workflow-runs`
|
||
```
|
||
|
||
改为:
|
||
|
||
```ts
|
||
const runsDir = getRunsDir()
|
||
```
|
||
|
||
- [ ] **Step 2: 运行 ports 测试验证未破坏**
|
||
|
||
Run: `bun test src/workflow/__tests__/ports.test.ts`
|
||
Expected: PASS — 现有断言全通过(`journalStore` 仍用同一 runsDir)
|
||
|
||
- [ ] **Step 3: 类型检查(确保 import 正确)**
|
||
|
||
Run: `bunx tsc --noEmit`
|
||
Expected: 0 errors
|
||
|
||
- [ ] **Step 4: Commit**
|
||
|
||
```bash
|
||
git add src/workflow/ports.ts
|
||
git commit -m "refactor(workflow): ports 引用 getRunsDir 消除路径拼接重复"
|
||
```
|
||
|
||
---
|
||
|
||
## Task 4: service 订阅 run_done 写盘
|
||
|
||
**Files:**
|
||
- Modify: `src/workflow/service.ts`
|
||
- Modify: `src/workflow/__tests__/service.test.ts`
|
||
|
||
- [ ] **Step 1: 写失败测试(run_done → 写盘)**
|
||
|
||
在 `src/workflow/__tests__/service.test.ts` 顶部 import 添加:
|
||
|
||
```ts
|
||
import { readRunState } from '../persistence.js'
|
||
```
|
||
|
||
文件末尾追加测试(复用现有 `fakePorts` helper;它已返回 bus、store、ports):
|
||
|
||
```ts
|
||
test('run_done completed → 写盘 state.json,returnValue 一致', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-svc-'))
|
||
const origGetRunsDir = await import('../persistence.js').then(m => m.getRunsDir)
|
||
// 通过 monkey-patch getRunsDir 让真实 writeRunState 写到 tmpdir
|
||
const persistence = await import('../persistence.js')
|
||
;(persistence as any).getRunsDir = () => dir
|
||
try {
|
||
const { ports, store } = fakePorts()
|
||
const bus = createProgressBus()
|
||
const storeFromBus = createProgressStoreFromBus(bus)
|
||
// 重新构造:让 service 用我们的 bus(fakePorts 内部也有 bus 但未暴露)
|
||
const svc = makeService(ports, storeFromBus, bus)
|
||
|
||
bus.emit({ type: 'run_started', runId: 'rW', workflowName: 'w', meta: null })
|
||
bus.emit({
|
||
type: 'run_done',
|
||
runId: 'rW',
|
||
status: 'completed',
|
||
returnValue: { ok: true, n: 3 },
|
||
})
|
||
|
||
// 写盘是 async(订阅里 await writeRunState);让 microtask 跑完
|
||
await new Promise(r => setTimeout(r, 50))
|
||
|
||
const got = await readRunState(dir, 'rW')
|
||
expect(got).not.toBeNull()
|
||
expect(got!.status).toBe('completed')
|
||
expect(got!.returnValue).toEqual({ ok: true, n: 3 })
|
||
} finally {
|
||
;(persistence as any).getRunsDir = origGetRunsDir
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('run_done failed → 写盘 status=failed + error 字段', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-svc-'))
|
||
const persistence = await import('../persistence.js')
|
||
const orig = persistence.getRunsDir
|
||
;(persistence as any).getRunsDir = () => dir
|
||
try {
|
||
const { ports } = fakePorts()
|
||
const bus = createProgressBus()
|
||
const store = createProgressStoreFromBus(bus)
|
||
makeService(ports, store, bus)
|
||
|
||
bus.emit({ type: 'run_started', runId: 'rF', workflowName: 'w', meta: null })
|
||
bus.emit({
|
||
type: 'run_done',
|
||
runId: 'rF',
|
||
status: 'failed',
|
||
error: 'boom',
|
||
})
|
||
await new Promise(r => setTimeout(r, 50))
|
||
|
||
const got = await readRunState(dir, 'rF')
|
||
expect(got).not.toBeNull()
|
||
expect(got!.status).toBe('failed')
|
||
expect(got!.error).toBe('boom')
|
||
} finally {
|
||
;(persistence as any).getRunsDir = orig
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('run_done killed → 写盘 status=killed', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-svc-'))
|
||
const persistence = await import('../persistence.js')
|
||
const orig = persistence.getRunsDir
|
||
;(persistence as any).getRunsDir = () => dir
|
||
try {
|
||
const { ports } = fakePorts()
|
||
const bus = createProgressBus()
|
||
const store = createProgressStoreFromBus(bus)
|
||
makeService(ports, store, bus)
|
||
|
||
bus.emit({ type: 'run_started', runId: 'rK', workflowName: 'w', meta: null })
|
||
bus.emit({ type: 'run_done', runId: 'rK', status: 'killed' })
|
||
await new Promise(r => setTimeout(r, 50))
|
||
|
||
const got = await readRunState(dir, 'rK')
|
||
expect(got?.status).toBe('killed')
|
||
} finally {
|
||
;(persistence as any).getRunsDir = orig
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('makeService 现有调用兼容(签名加 bus 参数后,旧测试 fakePorts 路径仍可构造)', async () => {
|
||
// 烟雾测试:确保 makeService(ports, store, bus) 能正常返回 service 对象
|
||
const { ports } = fakePorts()
|
||
const bus = createProgressBus()
|
||
const store = createProgressStoreFromBus(bus)
|
||
const svc = makeService(ports, store, bus)
|
||
expect(typeof svc.getRun).toBe('function')
|
||
expect(typeof svc.listRuns).toBe('function')
|
||
})
|
||
```
|
||
|
||
**同时**:现有 `service.test.ts` 里所有 `makeService(ports, store)` 调用都要改成 `makeService(ports, store, bus)`——bus 从 fakePorts 拿不到(未暴露),需要在 fakePorts 返回值里加 `bus`,或每个测试自己 createProgressBus。最小改动:让 fakePorts 返回 bus。
|
||
|
||
Modify `fakePorts` 返回类型与 return 对象(在 `ports`、`store`、`killed`、`calls` 之外加 `bus`):
|
||
|
||
```ts
|
||
function fakePorts(opts = {}) {
|
||
const bus = createProgressBus()
|
||
const store = createProgressStoreFromBus(bus)
|
||
// ...(其余不变)
|
||
return { ports, store, bus, killed, calls }
|
||
}
|
||
```
|
||
|
||
然后把所有现有测试里的 `const { ports, store } = fakePorts()` 改成 `const { ports, store, bus } = fakePorts()`,并把 `makeService(ports, store)` 改成 `makeService(ports, store, bus)`。
|
||
|
||
- [ ] **Step 2: 运行测试验证失败**
|
||
|
||
Run: `bun test src/workflow/__tests__/service.test.ts`
|
||
Expected: FAIL — `makeService` 参数数量不符 / `bus.subscribe` 找不到 / readRunState 拿不到值
|
||
|
||
- [ ] **Step 3: 实现 service 订阅**
|
||
|
||
Modify `src/workflow/service.ts`:
|
||
|
||
import 添加(顶部):
|
||
|
||
```ts
|
||
import { writeRunState, getRunsDir } from './persistence.js'
|
||
import type { ProgressBus } from './progress/bus.js'
|
||
```
|
||
|
||
`makeService` 签名改为接收 bus:
|
||
|
||
```ts
|
||
export function makeService(
|
||
ports: WorkflowPorts,
|
||
store: ProgressStore,
|
||
bus: ProgressBus,
|
||
): WorkflowService {
|
||
```
|
||
|
||
在 `makeService` 函数体开头(`const buildHost = ...` 之前)加订阅:
|
||
|
||
```ts
|
||
// 订阅 run_done:写终态快照到磁盘(覆盖 completed/failed/killed 三态)。
|
||
// store 先于本订阅注册到 bus,故 listener 执行时 store.get(runId) 已是 apply 后的终态。
|
||
// 注意:getRunsDir() 在 listener 内调用(运行时解析),便于测试 monkey-patch。
|
||
bus.subscribe(event => {
|
||
if (event.type !== 'run_done') return
|
||
const run = store.get(event.runId)
|
||
if (!run) return
|
||
void writeRunState(getRunsDir(), run)
|
||
})
|
||
```
|
||
|
||
更新 `getWorkflowService()` 单例创建处(第 73 行附近):
|
||
|
||
```ts
|
||
export function getWorkflowService(): WorkflowService {
|
||
if (cached) return cached
|
||
const bus = createProgressBus()
|
||
const store = createProgressStoreFromBus(bus)
|
||
const ports = createWorkflowPorts({ bus, store })
|
||
const service = makeService(ports, store, bus)
|
||
installWorkflowNotifications(service)
|
||
cached = service
|
||
return cached
|
||
}
|
||
```
|
||
|
||
(`createProgressBus` import 在 service.ts 顶部应已存在;若未 import 则补 `import { createProgressBus } from './progress/bus.js'`。)
|
||
|
||
- [ ] **Step 4: 运行测试验证通过**
|
||
|
||
Run: `bun test src/workflow/__tests__/service.test.ts`
|
||
Expected: PASS — 现有 + 4 个新测试
|
||
|
||
- [ ] **Step 5: Commit**
|
||
|
||
```bash
|
||
git add src/workflow/service.ts src/workflow/__tests__/service.test.ts
|
||
git commit -m "feat(workflow): service 订阅 run_done 写终态快照到磁盘"
|
||
```
|
||
|
||
---
|
||
|
||
## Task 5: service 的 loadPersistedRuns + getRunAsync fallback
|
||
|
||
**Files:**
|
||
- Modify: `src/workflow/service.ts`
|
||
- Modify: `src/workflow/__tests__/service.test.ts`
|
||
|
||
- [ ] **Step 1: 写失败测试**
|
||
|
||
在 `src/workflow/__tests__/service.test.ts` import 添加(若尚未):
|
||
|
||
```ts
|
||
import { writeRunState, readRunState, listPersistedRuns } from '../persistence.js'
|
||
```
|
||
|
||
文件末尾追加:
|
||
|
||
```ts
|
||
test('loadPersistedRuns 扫盘 hydrate 历史 run;已有内存 run 不被覆盖', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-svc-'))
|
||
const persistence = await import('../persistence.js')
|
||
const orig = persistence.getRunsDir
|
||
;(persistence as any).getRunsDir = () => dir
|
||
try {
|
||
// 磁盘先有两个历史 run
|
||
const historicalA: RunProgress = {
|
||
runId: 'hA', workflowName: 'old-A', status: 'completed',
|
||
phases: [], declaredPhases: [], currentPhase: null,
|
||
agents: [], agentCount: 1, returnValue: 'a',
|
||
startedAt: 10, updatedAt: 20,
|
||
} as RunProgress
|
||
const historicalB: RunProgress = {
|
||
runId: 'hB', workflowName: 'old-B', status: 'failed',
|
||
phases: [], declaredPhases: [], currentPhase: null,
|
||
agents: [], agentCount: 2, error: 'x',
|
||
startedAt: 30, updatedAt: 40,
|
||
} as RunProgress
|
||
await writeRunState(dir, historicalA)
|
||
await writeRunState(dir, historicalB)
|
||
|
||
const { ports, bus } = fakePorts()
|
||
const store = createProgressStoreFromBus(bus)
|
||
// 内存先有一个本次会话 run
|
||
bus.emit({ type: 'run_started', runId: 'live', workflowName: 'live-w', meta: null })
|
||
const svc = makeService(ports, store, bus)
|
||
|
||
await svc.loadPersistedRuns()
|
||
|
||
const ids = svc.listRuns().map(r => r.runId)
|
||
expect(ids).toContain('hA')
|
||
expect(ids).toContain('hB')
|
||
expect(ids).toContain('live')
|
||
// 内存优先:live 仍是 running(不被磁盘覆盖;磁盘里没有 live 也不会注入 STALE)
|
||
expect(svc.getRun('live')!.status).toBe('running')
|
||
expect(svc.getRun('hA')!.returnValue).toBe('a')
|
||
} finally {
|
||
;(persistence as any).getRunsDir = orig
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('loadPersistedRuns 重复调用仅扫盘一次(persistedLoaded flag)', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-svc-'))
|
||
const persistence = await import('../persistence.js')
|
||
const orig = persistence.getRunsDir
|
||
let listCalls = 0
|
||
;(persistence as any).getRunsDir = () => dir
|
||
const origList = persistence.listPersistedRuns
|
||
;(persistence as any).listPersistedRuns = async (d: string) => {
|
||
listCalls++
|
||
return origList(d)
|
||
}
|
||
try {
|
||
const { ports, bus } = fakePorts()
|
||
const store = createProgressStoreFromBus(bus)
|
||
const svc = makeService(ports, store, bus)
|
||
|
||
await svc.loadPersistedRuns()
|
||
await svc.loadPersistedRuns()
|
||
await svc.loadPersistedRuns()
|
||
|
||
expect(listCalls).toBe(1)
|
||
} finally {
|
||
;(persistence as any).getRunsDir = orig
|
||
;(persistence as any).listPersistedRuns = origList
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('getRunAsync 内存命中 → 不读盘', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-svc-'))
|
||
const persistence = await import('../persistence.js')
|
||
const orig = persistence.getRunsDir
|
||
let readCalls = 0
|
||
;(persistence as any).getRunsDir = () => dir
|
||
const origRead = persistence.readRunState
|
||
;(persistence as any).readRunState = async (d: string, id: string) => {
|
||
readCalls++
|
||
return origRead(d, id)
|
||
}
|
||
try {
|
||
const { ports, bus } = fakePorts()
|
||
const store = createProgressStoreFromBus(bus)
|
||
const svc = makeService(ports, store, bus)
|
||
bus.emit({ type: 'run_started', runId: 'live', workflowName: 'w', meta: null })
|
||
|
||
const got = await svc.getRunAsync('live')
|
||
expect(got?.runId).toBe('live')
|
||
expect(readCalls).toBe(0)
|
||
} finally {
|
||
;(persistence as any).getRunsDir = orig
|
||
;(persistence as any).readRunState = origRead
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('getRunAsync 内存 miss + 磁盘命中 → 返回磁盘值,且不注入内存(再次 get 仍读盘)', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-svc-'))
|
||
const persistence = await import('../persistence.js')
|
||
const orig = persistence.getRunsDir
|
||
let readCalls = 0
|
||
;(persistence as any).getRunsDir = () => dir
|
||
const origRead = persistence.readRunState
|
||
;(persistence as any).readRunState = async (d: string, id: string) => {
|
||
readCalls++
|
||
return origRead(d, id)
|
||
}
|
||
try {
|
||
const historical: RunProgress = {
|
||
runId: 'hist-only', workflowName: 'old', status: 'completed',
|
||
phases: [], declaredPhases: [], currentPhase: null,
|
||
agents: [], agentCount: 0, returnValue: { x: 1 },
|
||
startedAt: 1, updatedAt: 2,
|
||
} as RunProgress
|
||
await writeRunState(dir, historical)
|
||
|
||
const { ports, bus } = fakePorts()
|
||
const store = createProgressStoreFromBus(bus)
|
||
const svc = makeService(ports, store, bus)
|
||
|
||
const got = await svc.getRunAsync('hist-only')
|
||
expect(got?.returnValue).toEqual({ x: 1 })
|
||
expect(readCalls).toBe(1)
|
||
// 不注入内存:再次 get 仍读盘
|
||
const got2 = await svc.getRunAsync('hist-only')
|
||
expect(got2?.returnValue).toEqual({ x: 1 })
|
||
expect(readCalls).toBe(2)
|
||
// 内存 list 不含(未 hydrate)
|
||
expect(svc.listRuns().map(r => r.runId)).not.toContain('hist-only')
|
||
} finally {
|
||
;(persistence as any).getRunsDir = orig
|
||
;(persistence as any).readRunState = origRead
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
test('getRunAsync 内存 miss + 磁盘 miss → undefined', async () => {
|
||
const dir = await mkdtemp(join(tmpdir(), 'wf-svc-'))
|
||
const persistence = await import('../persistence.js')
|
||
const orig = persistence.getRunsDir
|
||
;(persistence as any).getRunsDir = () => dir
|
||
try {
|
||
const { ports, bus } = fakePorts()
|
||
const store = createProgressStoreFromBus(bus)
|
||
const svc = makeService(ports, store, bus)
|
||
|
||
const got = await svc.getRunAsync('no-such-run')
|
||
expect(got).toBeUndefined()
|
||
} finally {
|
||
;(persistence as any).getRunsDir = orig
|
||
await rm(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
```
|
||
|
||
顶部 import 补 `RunProgress` 类型(若尚未):
|
||
|
||
```ts
|
||
import type { RunProgress } from '../progress/store.js'
|
||
```
|
||
|
||
- [ ] **Step 2: 运行测试验证失败**
|
||
|
||
Run: `bun test src/workflow/__tests__/service.test.ts`
|
||
Expected: FAIL — `svc.loadPersistedRuns is not a function` / `svc.getRunAsync is not a function`
|
||
|
||
- [ ] **Step 3: 实现 loadPersistedRuns + getRunAsync**
|
||
|
||
Modify `src/workflow/service.ts`:
|
||
|
||
import 添加:
|
||
|
||
```ts
|
||
import { writeRunState, readRunState, listPersistedRuns, getRunsDir } from './persistence.js'
|
||
```
|
||
(替换 Task 4 里只 import `writeRunState, getRunsDir` 的那行——合并为完整 import)
|
||
|
||
`WorkflowService` type 加两个方法(在 `getRun` 之后):
|
||
|
||
```ts
|
||
export type WorkflowService = {
|
||
ports: WorkflowPorts
|
||
launch(
|
||
input: Pick<
|
||
WorkflowInput,
|
||
| 'script' | 'name' | 'scriptPath' | 'args' | 'description' | 'resumeFromRunId' | 'title'
|
||
>,
|
||
toolUseContext: ToolUseContext,
|
||
canUseTool: CanUseToolFn,
|
||
): Promise<{ runId: string }>
|
||
kill(runId: string): void
|
||
shutdown(): void
|
||
listRuns(): RunProgress[]
|
||
getRun(runId: string): RunProgress | undefined
|
||
/**
|
||
* 异步按 runId 查:内存命中则返回;miss 读盘 state.json(不注入内存)。
|
||
* 供"按 runId 取历史 return"场景;面板展示请走 loadPersistedRuns + listRuns。
|
||
*/
|
||
getRunAsync(runId: string): Promise<RunProgress | undefined>
|
||
/**
|
||
* 扫盘把所有历史 run 的 state.json hydrate 进 store(已存在 runId 跳过)。
|
||
* 进程单例内仅实际扫盘一次(persistedLoaded flag);重复调用立即返回。
|
||
*/
|
||
loadPersistedRuns(): Promise<void>
|
||
subscribe(listener: () => void): () => void
|
||
listNamed(workflowDir?: string): Promise<string[]>
|
||
}
|
||
```
|
||
|
||
在 `makeService` 函数体里(订阅 run_done 之后、`return {` 之前)加:
|
||
|
||
```ts
|
||
let persistedLoaded = false
|
||
```
|
||
|
||
在返回对象里加(在 `getRun` 之后、`subscribe` 之前):
|
||
|
||
```ts
|
||
getRun: id => store.get(id),
|
||
getRunAsync: async id => {
|
||
const mem = store.get(id)
|
||
if (mem) return mem
|
||
return (await readRunState(getRunsDir(), id)) ?? undefined
|
||
},
|
||
async loadPersistedRuns() {
|
||
if (persistedLoaded) return
|
||
persistedLoaded = true
|
||
try {
|
||
const runs = await listPersistedRuns(getRunsDir())
|
||
for (const run of runs) store.hydrate(run)
|
||
} catch (e) {
|
||
// 扫盘失败不阻断面板:log + 复位 flag 允许下次重试
|
||
logForDebugging(
|
||
`[workflow warn] loadPersistedRuns failed: ${(e as Error).message}`,
|
||
)
|
||
persistedLoaded = false
|
||
}
|
||
},
|
||
subscribe: fn => store.subscribe(fn),
|
||
```
|
||
|
||
- [ ] **Step 4: 运行测试验证通过**
|
||
|
||
Run: `bun test src/workflow/__tests__/service.test.ts`
|
||
Expected: PASS — Task 4 + Task 5 共 9 个新测试 + 现有全过
|
||
|
||
- [ ] **Step 5: Commit**
|
||
|
||
```bash
|
||
git add src/workflow/service.ts src/workflow/__tests__/service.test.ts
|
||
git commit -m "feat(workflow): service 添加 loadPersistedRuns 与 getRunAsync fallback"
|
||
```
|
||
|
||
---
|
||
|
||
## Task 6: WorkflowsPanel mount 触发 loadPersistedRuns
|
||
|
||
**Files:**
|
||
- Modify: `src/workflow/panel/WorkflowsPanel.tsx`
|
||
- Modify: `src/workflow/__tests__/WorkflowsPanel.test.tsx`
|
||
|
||
- [ ] **Step 1: 写失败测试**
|
||
|
||
在 `src/workflow/__tests__/WorkflowsPanel.test.tsx` import 添加(若尚未,需要渲染 WorkflowsPanel 来 spy):
|
||
|
||
```ts
|
||
import React from 'react'
|
||
import { render } from '@anthropic/ink'
|
||
import { WorkflowsPanel } from '../panel/WorkflowsPanel.js'
|
||
import { getWorkflowService } from '../service.js'
|
||
```
|
||
|
||
文件末尾追加(用 spy 替换 service 单例的 loadPersistedRuns,断言被调一次):
|
||
|
||
```ts
|
||
test('WorkflowsPanel mount 触发一次 loadPersistedRuns', async () => {
|
||
__resetWorkflowServiceForTests()
|
||
// 强制单例创建,挂 spy
|
||
const svc = getWorkflowService()
|
||
let calls = 0
|
||
const orig = svc.loadPersistedRuns.bind(svc)
|
||
svc.loadPersistedRuns = async () => { calls++ }
|
||
|
||
try {
|
||
const onDone = () => {}
|
||
const ctx = { canUseTool: undefined } as any
|
||
const { unmount } = render(
|
||
React.createElement(WorkflowsPanel, { onDone, context: ctx }),
|
||
)
|
||
// mount 后 useEffect 异步触发;等一个 tick
|
||
await new Promise(r => setTimeout(r, 10))
|
||
|
||
expect(calls).toBe(1)
|
||
|
||
// 重渲染不应再次调用
|
||
unmount()
|
||
} finally {
|
||
svc.loadPersistedRuns = orig
|
||
__resetWorkflowServiceForTests()
|
||
}
|
||
})
|
||
```
|
||
|
||
- [ ] **Step 2: 运行测试验证失败**
|
||
|
||
Run: `bun test src/workflow/__tests__/WorkflowsPanel.test.tsx`
|
||
Expected: FAIL — `calls` 仍为 0(mount 没触发 loadPersistedRuns)
|
||
|
||
- [ ] **Step 3: 实现 mount 触发**
|
||
|
||
Modify `src/workflow/panel/WorkflowsPanel.tsx`:
|
||
|
||
在 `useWorkflowKeyboard(handlers)` 之后、`const running = ...` 之前,加 useEffect:
|
||
|
||
```ts
|
||
// mount 时触发一次扫盘 hydrate 历史 run(service 内部 persistedLoaded flag 守护幂等)。
|
||
useEffect(() => {
|
||
void svc.loadPersistedRuns()
|
||
}, [svc])
|
||
```
|
||
|
||
`useEffect` 应已在顶部 import(`import React, { useEffect, useState, useSyncExternalStore } from 'react'`)—— 现状已含。
|
||
|
||
- [ ] **Step 4: 运行测试验证通过**
|
||
|
||
Run: `bun test src/workflow/__tests__/WorkflowsPanel.test.tsx`
|
||
Expected: PASS — 现有 5 个 + 新增 1 个
|
||
|
||
- [ ] **Step 5: Commit**
|
||
|
||
```bash
|
||
git add src/workflow/panel/WorkflowsPanel.tsx src/workflow/__tests__/WorkflowsPanel.test.tsx
|
||
git commit -m "feat(workflow): 面板 mount 时加载历史 run 到内存"
|
||
```
|
||
|
||
---
|
||
|
||
## Task 7: 全量回归(precheck)
|
||
|
||
**Files:** 无改动,只验证。
|
||
|
||
- [ ] **Step 1: 类型检查**
|
||
|
||
Run: `bunx tsc --noEmit`
|
||
Expected: 0 errors
|
||
|
||
- [ ] **Step 2: 全套 workflow 测试**
|
||
|
||
Run: `bun test src/workflow/`
|
||
Expected: 所有测试通过(含现有 65+ 与新增约 20 个)
|
||
|
||
- [ ] **Step 3: Lint 改动文件**
|
||
|
||
Run: `bunx biome check src/workflow/persistence.ts src/workflow/progress/store.ts src/workflow/ports.ts src/workflow/service.ts src/workflow/panel/WorkflowsPanel.tsx src/workflow/__tests__/persistence.test.ts src/workflow/__tests__/progressStore.test.ts src/workflow/__tests__/service.test.ts src/workflow/__tests__/WorkflowsPanel.test.tsx`
|
||
Expected: No fixes applied / 无 error
|
||
|
||
- [ ] **Step 4: 完整 precheck**
|
||
|
||
Run: `bun run precheck`
|
||
Expected: 0 errors(typecheck + lint fix + test 全通过)
|
||
|
||
- [ ] **Step 5: (可选)手工烟雾验证**
|
||
|
||
启动 `bun run dev`,跑一个会完成的 workflow(如某个简单命名 workflow),确认:
|
||
1. `.claude/workflow-runs/<runId>/state.json` 生成且含 returnValue
|
||
2. 重启 CLI 后打开 `/workflows`,能看到该历史 run
|
||
3. (若面板有详情视图)选中历史 run 能看到 agents/phases
|
||
|
||
如果手工烟雾失败,回到对应 Task 修正。
|
||
|
||
- [ ] **Step 6: 最终 commit(如有未提交的 lint 修复)**
|
||
|
||
```bash
|
||
git status
|
||
# 若有改动:
|
||
git add -p
|
||
git commit -m "chore(workflow): 持久化特性 precheck 收尾"
|
||
```
|
||
|
||
---
|
||
|
||
## Self-Review
|
||
|
||
**Spec coverage(逐节核对):**
|
||
|
||
- ✅ 问题陈述 → 整体计划回应
|
||
- ✅ 目标 (a) 重启取 return → Task 4 写盘 + Task 5 `getRunAsync` fallback
|
||
- ✅ 目标 (b) 面板跨重启 → Task 5 `loadPersistedRuns` + Task 6 面板触发
|
||
- ✅ 非目标 (c) 跨进程 resume → 计划不涉及 abort/binding 恢复
|
||
- ✅ 架构(5 个文件改动) → Task 1-6 全覆盖
|
||
- ✅ 数据流 写入(run_done 订阅) → Task 4
|
||
- ✅ 数据流 读取① 面板 hydrate → Task 5 + Task 6
|
||
- ✅ 数据流 读取② getRun fallback → Task 5 `getRunAsync`(spec 称 getRun,实现为 async 版本以保留同步语义;已在 Task 5 注释说明)
|
||
- ✅ state.json 格式(schemaVersion=1 + RunProgress) → Task 1
|
||
- ✅ 错误处理(writeRunState best-effort / readRunState 容错 / 扫盘跳过损坏) → Task 1 实现 + 测试
|
||
- ✅ 关键不变量(内存优先 / 磁盘纯终态 / getRunAsync 不注入 / 持久化不阻断 / 引擎零改动) → Task 1/4/5 实现 + 测试断言
|
||
- ✅ 测试策略 → persistence.test / progressStore.test / service.test / WorkflowsPanel.test 全覆盖
|
||
|
||
**Placeholder scan:** 无 TBD/TODO;每个 step 含完整代码或精确命令。
|
||
|
||
**Type consistency:**
|
||
- `writeRunState(runsDir, run)` / `readRunState(runsDir, runId)` / `listPersistedRuns(runsDir)` —— 三处签名一致(runsDir 首参)
|
||
- `store.hydrate(run: RunProgress)` —— Task 2 定义、Task 5 使用,签名一致
|
||
- `makeService(ports, store, bus)` —— Task 4 改签名、Task 5 沿用
|
||
- `svc.loadPersistedRuns()` / `svc.getRunAsync(id)` —— Task 5 定义、Task 6 使用,签名一致
|
||
- `getRunsDir()` —— Task 1 定义、Task 3 ports 引用、Task 4 service 引用,统一来源
|
||
|
||
**歧义/已知偏离:**
|
||
- spec 写"`getRun` fallback",实现为新增 `getRunAsync`(同步 getRun 保留内存语义)。理由:避免破坏现有同步调用方(WorkflowsPanel 等);fallback 是低频路径,async 更诚实。Task 5 测试显式断言"不注入内存"。
|
||
|
||
---
|
||
|
||
## Execution Handoff
|
||
|
||
Plan complete and saved to `docs/superpowers/plans/2026-06-13-workflow-run-state-persistence.md`. Two execution options:
|
||
|
||
**1. Subagent-Driven (recommended)** — 每个 task 派 fresh subagent,task 间 review,迭代快、上下文干净
|
||
**2. Inline Execution** — 本会话内 executing-plans 批量执行 + checkpoint 审阅
|
||
|
||
Which approach?
|