simple-skills 1.3.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/skills/engineering/brainstorming/SKILL.md +2 -2
- package/skills/engineering/brainstorming/scripts/frame-template.html +299 -51
- package/skills/engineering/brainstorming/scripts/start-server.sh +4 -4
- package/skills/engineering/brainstorming/scripts/stop-server.sh +1 -1
- package/skills/engineering/brainstorming/spec-document-reviewer-prompt.md +1 -1
- package/skills/engineering/brainstorming/visual-companion.md +4 -4
- package/skills/engineering/code-review/SKILL.md +43 -1
- package/skills/engineering/finishing-a-development-branch/SKILL.md +1 -6
- package/skills/engineering/implement/SKILL.md +2 -0
- package/skills/engineering/subagent-driven-development/SKILL.md +14 -19
- package/skills/engineering/subagent-driven-development/scripts/review-package +1 -1
- package/skills/engineering/subagent-driven-development/scripts/sdd-workspace +3 -3
- package/skills/engineering/subagent-driven-development/scripts/task-brief +1 -5
- package/skills/engineering/systematic-debugging/SKILL.md +2 -15
- package/skills/engineering/systematic-debugging/CREATION-LOG.md +0 -119
- package/skills/engineering/systematic-debugging/condition-based-waiting-example.ts +0 -158
- package/skills/engineering/systematic-debugging/condition-based-waiting.md +0 -115
- package/skills/engineering/systematic-debugging/defense-in-depth.md +0 -122
- package/skills/engineering/systematic-debugging/find-polluter.sh +0 -72
- package/skills/engineering/systematic-debugging/root-cause-tracing.md +0 -169
- package/skills/engineering/systematic-debugging/test-academic.md +0 -14
- package/skills/engineering/systematic-debugging/test-pressure-1.md +0 -58
- package/skills/engineering/systematic-debugging/test-pressure-2.md +0 -68
- package/skills/engineering/systematic-debugging/test-pressure-3.md +0 -69
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: subagent-driven-development
|
|
3
3
|
description: 当在当前会话中执行包含独立任务的实现计划时使用
|
|
4
|
-
version: "1.0.0"
|
|
5
|
-
license: MIT
|
|
6
|
-
metadata:
|
|
7
|
-
hermes:
|
|
8
|
-
tags: [agents, development]
|
|
9
4
|
---
|
|
10
5
|
|
|
11
6
|
# 子智能体驱动开发
|
|
@@ -75,10 +70,10 @@ digraph process {
|
|
|
75
70
|
|
|
76
71
|
"准备: 工作树、查账本、读计划、起飞前审查" [shape=box];
|
|
77
72
|
"还有任务?" [shape=diamond];
|
|
78
|
-
"分派最终代码审查者 (../
|
|
73
|
+
"分派最终代码审查者 (../code-review/code-reviewer.md)" [shape=box];
|
|
79
74
|
"最终审查有发现? 一次修复分派、一次定向复审、裁定残留项" [shape=box];
|
|
80
75
|
"最终审查干净: 删除本计划的工作区" [shape=box];
|
|
81
|
-
"使用
|
|
76
|
+
"使用 /finishing-a-development-branch" [shape=box style=filled fillcolor=lightgreen];
|
|
82
77
|
|
|
83
78
|
"准备: 工作树、查账本、读计划、起飞前审查" -> "分派实现子智能体 (./implementer-prompt.md)";
|
|
84
79
|
"分派实现子智能体 (./implementer-prompt.md)" -> "实现者有疑问?";
|
|
@@ -104,21 +99,21 @@ digraph process {
|
|
|
104
99
|
"把发现连同裁定搁置进账本" -> "往账本追加完成行,标记待办完成";
|
|
105
100
|
"往账本追加完成行,标记待办完成" -> "还有任务?";
|
|
106
101
|
"还有任务?" -> "分派实现子智能体 (./implementer-prompt.md)" [label="是"];
|
|
107
|
-
"还有任务?" -> "分派最终代码审查者 (../
|
|
108
|
-
"分派最终代码审查者 (../
|
|
102
|
+
"还有任务?" -> "分派最终代码审查者 (../code-review/code-reviewer.md)" [label="否"];
|
|
103
|
+
"分派最终代码审查者 (../code-review/code-reviewer.md)" -> "最终审查有发现? 一次修复分派、一次定向复审、裁定残留项";
|
|
109
104
|
"最终审查有发现? 一次修复分派、一次定向复审、裁定残留项" -> "最终审查干净: 删除本计划的工作区";
|
|
110
|
-
"最终审查干净: 删除本计划的工作区" -> "使用
|
|
105
|
+
"最终审查干净: 删除本计划的工作区" -> "使用 /finishing-a-development-branch";
|
|
111
106
|
}
|
|
112
107
|
```
|
|
113
108
|
|
|
114
109
|
## 准备
|
|
115
110
|
|
|
116
|
-
确保工作发生在一个隔离的工作区里:用
|
|
111
|
+
确保工作发生在一个隔离的工作区里:用 /using-git-worktrees 创建一个,或者核实已有的那个。没有你人类伙伴的明确同意,绝不在 main/master 分支上开始实现。
|
|
117
112
|
|
|
118
113
|
会话记忆无法在上下文压缩(compaction)中存活。在真实会话里,丢失了位置的控制者曾重新分派整段已经完成的任务序列——这是观察到的最昂贵的失败。把进度记在一个账本文件里,而不只是记在待办里。
|
|
119
114
|
|
|
120
|
-
- **每个计划拥有自己的工作区:** 技能启动时,运行本技能的 `scripts/sdd-workspace PLAN_FILE`——它会打印这个计划专属的、被 git 忽略的目录(`<repo-root
|
|
121
|
-
- 到 `<工作区>/progress.md` 查本计划的账本。如果它的第一行点名的是你的计划文件,那么带有 `Task <N>: complete` 行的任务就是**已完成**——不要重新分派它们;从第一个没有该行的任务处继续。如果某个任务的最后一行是一轮修复,说明它正卡在修复循环中:从下一轮继续。如果账本第一行点名的是**另一个**计划文件——或者你在旧的扁平路径
|
|
115
|
+
- **每个计划拥有自己的工作区:** 技能启动时,运行本技能的 `scripts/sdd-workspace PLAN_FILE`——它会打印这个计划专属的、被 git 忽略的目录(`<repo-root>/docs/sdd/<计划文件名>/`),**本计划**的一切产物都放在那里:账本、简报、报告、审查包。别的计划的目录不属于你,不读也不写。
|
|
116
|
+
- 到 `<工作区>/progress.md` 查本计划的账本。如果它的第一行点名的是你的计划文件,那么带有 `Task <N>: complete` 行的任务就是**已完成**——不要重新分派它们;从第一个没有该行的任务处继续。如果某个任务的最后一行是一轮修复,说明它正卡在修复循环中:从下一轮继续。如果账本第一行点名的是**另一个**计划文件——或者你在旧的扁平路径 `docs/sdd/progress.md` 发现了一个游离的账本——那是别人的进度:原地别动,另起你自己的新账本。
|
|
122
117
|
- 创建账本时,把它的身份写在第一行:`# SDD ledger — plan: <计划文件路径>`。
|
|
123
118
|
- 这个账本是你的恢复地图:它点名的那些提交,即使你的上下文已经不记得创建过它们,也确实存在于 git 中。压缩之后,相信账本和 `git log`,而不是你自己的记忆。
|
|
124
119
|
- `git clean -fdx` 会毁掉这个工作区(它是被 git 忽略的临时文件);万一发生了,就从 `git log` 恢复。
|
|
@@ -250,7 +245,7 @@ digraph process {
|
|
|
250
245
|
|
|
251
246
|
## 最终审查
|
|
252
247
|
|
|
253
|
-
覆盖整个分支的最终审查也拿到一个审查包:运行 `scripts/review-package PLAN_FILE MERGE_BASE HEAD`(MERGE_BASE = 分支起点的那个提交,例如 `git merge-base main HEAD`),把打印出的路径放进最终审查的分派里,这样最终审查者读一个文件就行,不必用 git 命令重新推导整个分支的 diff。用可用的最强模型分派(见"模型选择"),使用
|
|
248
|
+
覆盖整个分支的最终审查也拿到一个审查包:运行 `scripts/review-package PLAN_FILE MERGE_BASE HEAD`(MERGE_BASE = 分支起点的那个提交,例如 `git merge-base main HEAD`),把打印出的路径放进最终审查的分派里,这样最终审查者读一个文件就行,不必用 git 命令重新推导整个分支的 diff。用可用的最强模型分派(见"模型选择"),使用 /code-review 的 [code-reviewer.md](../code-review/code-reviewer.md)。把它指向账本里那些"延后的 Minor"和"已搁置"的行,让它甄别哪些必须在合并前修掉。
|
|
254
249
|
|
|
255
250
|
如果覆盖整个分支的最终审查返回了发现,用**一个**修复子智能体带着**完整的**发现清单去分派——不要一条发现一个修复者。逐条发现各派一个修复者,每个都要重建上下文、重跑测试套件;真实会话里,一次最终审查的修复浪潮花掉的成本超过它全部任务的总和。然后对这波修复跑**恰好一次**定向复审(对修复区间跑 `scripts/review-package PLAN_FILE FIX_BASE HEAD`,用 [re-review-prompt.md](re-review-prompt.md))。残留的发现按任务循环里熔断那套来裁定:带裁定搁置,或者在承重项上停下。**没有第二波修复**——残留的承重发现会在 finishing-a-development-branch 呈现选项时浮到你人类伙伴面前。
|
|
256
251
|
|
|
@@ -258,7 +253,7 @@ digraph process {
|
|
|
258
253
|
|
|
259
254
|
当覆盖整个分支的最终审查干净、且它的修复已合并时,删除**本计划**的工作区(`rm -rf <工作区>`)——现在 git 历史就是记录了。同级目录属于别的计划,别去动它们。
|
|
260
255
|
|
|
261
|
-
使用
|
|
256
|
+
使用 /finishing-a-development-branch。
|
|
262
257
|
|
|
263
258
|
## 常见的合理化借口
|
|
264
259
|
|
|
@@ -279,8 +274,8 @@ digraph process {
|
|
|
279
274
|
你:我正在用子智能体驱动开发来执行这个计划。
|
|
280
275
|
|
|
281
276
|
[准备:工作树已核实]
|
|
282
|
-
[把计划文件读一遍:docs/
|
|
283
|
-
[解析工作区:scripts/sdd-workspace docs/
|
|
277
|
+
[把计划文件读一遍:docs/plans/feature-plan.md]
|
|
278
|
+
[解析工作区:scripts/sdd-workspace docs/plans/feature-plan.md —— 里面没有账本,全新开始]
|
|
284
279
|
[为所有任务创建待办]
|
|
285
280
|
|
|
286
281
|
任务 1:Hook 安装脚本
|
|
@@ -289,7 +284,7 @@ digraph process {
|
|
|
289
284
|
|
|
290
285
|
实现者:"开始之前——这个 hook 应该装在用户级还是系统级?"
|
|
291
286
|
|
|
292
|
-
你:"用户级(~/.config/
|
|
287
|
+
你:"用户级(~/.config/hooks/)"
|
|
293
288
|
|
|
294
289
|
实现者:[稍后]
|
|
295
290
|
- 实现了 install-hook 命令
|
|
@@ -337,5 +332,5 @@ digraph process {
|
|
|
337
332
|
|
|
338
333
|
[删除本计划的工作区 —— 现在记录活在 git 里]
|
|
339
334
|
|
|
340
|
-
搞定!使用
|
|
335
|
+
搞定!使用 /finishing-a-development-branch。
|
|
341
336
|
```
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
# tasks intact.
|
|
6
6
|
#
|
|
7
7
|
# Usage: review-package PLAN_FILE BASE HEAD [OUTFILE]
|
|
8
|
-
# Default OUTFILE: <repo-root
|
|
8
|
+
# Default OUTFILE: <repo-root>/docs/sdd/<plan-basename>/review-<base7>..<head7>.diff
|
|
9
9
|
# (named per range, so a re-review after fixes gets a distinct fresh file).
|
|
10
10
|
set -euo pipefail
|
|
11
11
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
# short-lived artifacts: task briefs, implementer reports, review packages,
|
|
4
4
|
# and the progress ledger. Print the plan directory's absolute path.
|
|
5
5
|
#
|
|
6
|
-
# One directory per plan (
|
|
6
|
+
# One directory per plan (docs/sdd/<plan-basename>/) so a follow-up
|
|
7
7
|
# plan in the same working tree can never read or overwrite another plan's
|
|
8
8
|
# artifacts. A stale ledger misread as current progress makes controllers
|
|
9
9
|
# skip whole task sequences — plan-scoping removes that failure structurally.
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
# The workspace lives in the working tree (not under .git/) because Claude Code
|
|
12
12
|
# treats .git/ as a protected path and denies agent writes there — which blocks
|
|
13
13
|
# an implementer subagent from writing its report file. A self-ignoring
|
|
14
|
-
# .gitignore at
|
|
14
|
+
# .gitignore at docs/sdd/ keeps every plan's workspace out of
|
|
15
15
|
# `git status` and out of accidental commits without modifying any tracked file.
|
|
16
16
|
#
|
|
17
17
|
# Single source of truth for the workspace location, so task-brief and
|
|
@@ -33,7 +33,7 @@ slug=$(basename "$plan" .md)
|
|
|
33
33
|
|| { echo "cannot derive a workspace name from: $plan" >&2; exit 2; }
|
|
34
34
|
|
|
35
35
|
root=$(git rev-parse --show-toplevel)
|
|
36
|
-
base="$root
|
|
36
|
+
base="$root/docs/sdd"
|
|
37
37
|
dir="$base/$slug"
|
|
38
38
|
mkdir -p "$dir"
|
|
39
39
|
printf '*\n' > "$base/.gitignore"
|
|
@@ -4,13 +4,9 @@
|
|
|
4
4
|
# through the controller's context.
|
|
5
5
|
#
|
|
6
6
|
# Usage: task-brief PLAN_FILE TASK_NUMBER [OUTFILE]
|
|
7
|
-
# Default OUTFILE: <repo-root
|
|
7
|
+
# Default OUTFILE: <repo-root>/docs/sdd/<plan-basename>/task-<N>-brief.md
|
|
8
8
|
# (per plan and per worktree; concurrent runs of the SAME plan in the same
|
|
9
9
|
# working tree share it).
|
|
10
|
-
#
|
|
11
|
-
# 中文 fork 适配:上游只识别英文任务标题 "## Task N",而 superpowers-zh
|
|
12
|
-
# 的 writing-plans 产出的是 "### 任务 N:..."。下方 awk 同时匹配
|
|
13
|
-
# "Task" 与 "任务",两种计划都能抽取。
|
|
14
10
|
set -euo pipefail
|
|
15
11
|
|
|
16
12
|
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: systematic-debugging
|
|
3
3
|
description: 遇到任何 bug、测试失败或异常行为时使用,在提出修复方案之前执行
|
|
4
|
-
version: "1.0.0"
|
|
5
|
-
license: MIT
|
|
6
|
-
metadata:
|
|
7
|
-
hermes:
|
|
8
|
-
tags: [debugging]
|
|
9
4
|
---
|
|
10
5
|
|
|
11
6
|
# 系统化调试
|
|
@@ -162,7 +157,7 @@ metadata:
|
|
|
162
157
|
- 尽可能用自动化测试
|
|
163
158
|
- 没有测试框架就写一次性测试脚本
|
|
164
159
|
- 修复前必须先有测试
|
|
165
|
-
- 使用 `
|
|
160
|
+
- 使用 `/tdd` 技能来编写规范的失败测试
|
|
166
161
|
|
|
167
162
|
2. **实施单一修复**
|
|
168
163
|
- 修复已定位的根本原因
|
|
@@ -174,7 +169,7 @@ metadata:
|
|
|
174
169
|
- 测试现在通过了吗?
|
|
175
170
|
- 其他测试没有被破坏吧?
|
|
176
171
|
- 问题真的解决了吗?
|
|
177
|
-
- 宣称成功之前,使用
|
|
172
|
+
- 宣称成功之前,使用 `/verification-before-completion` 技能
|
|
178
173
|
|
|
179
174
|
4. **如果修复不起作用**
|
|
180
175
|
- 停下来
|
|
@@ -262,11 +257,3 @@ metadata:
|
|
|
262
257
|
|
|
263
258
|
**但是:** 95% 的"找不到根因"其实是排查不充分。
|
|
264
259
|
|
|
265
|
-
## 辅助技术
|
|
266
|
-
|
|
267
|
-
以下技术是系统化调试的组成部分,可在本目录中找到:
|
|
268
|
-
|
|
269
|
-
- **`root-cause-tracing.md`** - 沿调用栈反向追踪 bug,找到最初的触发点
|
|
270
|
-
- **`defense-in-depth.md`** - 找到根因后,在多个层级添加校验
|
|
271
|
-
- **`condition-based-waiting.md`** - 用条件轮询替代硬编码等待时间
|
|
272
|
-
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
# Creation Log: Systematic Debugging Skill
|
|
2
|
-
|
|
3
|
-
Reference example of extracting, structuring, and bulletproofing a critical skill.
|
|
4
|
-
|
|
5
|
-
## Source Material
|
|
6
|
-
|
|
7
|
-
Extracted debugging framework from `/Users/jesse/.claude/CLAUDE.md`:
|
|
8
|
-
- 4-phase systematic process (Investigation → Pattern Analysis → Hypothesis → Implementation)
|
|
9
|
-
- Core mandate: ALWAYS find root cause, NEVER fix symptoms
|
|
10
|
-
- Rules designed to resist time pressure and rationalization
|
|
11
|
-
|
|
12
|
-
## Extraction Decisions
|
|
13
|
-
|
|
14
|
-
**What to include:**
|
|
15
|
-
- Complete 4-phase framework with all rules
|
|
16
|
-
- Anti-shortcuts ("NEVER fix symptom", "STOP and re-analyze")
|
|
17
|
-
- Pressure-resistant language ("even if faster", "even if I seem in a hurry")
|
|
18
|
-
- Concrete steps for each phase
|
|
19
|
-
|
|
20
|
-
**What to leave out:**
|
|
21
|
-
- Project-specific context
|
|
22
|
-
- Repetitive variations of same rule
|
|
23
|
-
- Narrative explanations (condensed to principles)
|
|
24
|
-
|
|
25
|
-
## Structure Following skill-creation/SKILL.md
|
|
26
|
-
|
|
27
|
-
1. **Rich when_to_use** - Included symptoms and anti-patterns
|
|
28
|
-
2. **Type: technique** - Concrete process with steps
|
|
29
|
-
3. **Keywords** - "root cause", "symptom", "workaround", "debugging", "investigation"
|
|
30
|
-
4. **Flowchart** - Decision point for "fix failed" → re-analyze vs add more fixes
|
|
31
|
-
5. **Phase-by-phase breakdown** - Scannable checklist format
|
|
32
|
-
6. **Anti-patterns section** - What NOT to do (critical for this skill)
|
|
33
|
-
|
|
34
|
-
## Bulletproofing Elements
|
|
35
|
-
|
|
36
|
-
Framework designed to resist rationalization under pressure:
|
|
37
|
-
|
|
38
|
-
### Language Choices
|
|
39
|
-
- "ALWAYS" / "NEVER" (not "should" / "try to")
|
|
40
|
-
- "even if faster" / "even if I seem in a hurry"
|
|
41
|
-
- "STOP and re-analyze" (explicit pause)
|
|
42
|
-
- "Don't skip past" (catches the actual behavior)
|
|
43
|
-
|
|
44
|
-
### Structural Defenses
|
|
45
|
-
- **Phase 1 required** - Can't skip to implementation
|
|
46
|
-
- **Single hypothesis rule** - Forces thinking, prevents shotgun fixes
|
|
47
|
-
- **Explicit failure mode** - "IF your first fix doesn't work" with mandatory action
|
|
48
|
-
- **Anti-patterns section** - Shows exactly what shortcuts look like
|
|
49
|
-
|
|
50
|
-
### Redundancy
|
|
51
|
-
- Root cause mandate in overview + when_to_use + Phase 1 + implementation rules
|
|
52
|
-
- "NEVER fix symptom" appears 4 times in different contexts
|
|
53
|
-
- Each phase has explicit "don't skip" guidance
|
|
54
|
-
|
|
55
|
-
## Testing Approach
|
|
56
|
-
|
|
57
|
-
Created 4 validation tests following skills/meta/testing-skills-with-subagents:
|
|
58
|
-
|
|
59
|
-
### Test 1: Academic Context (No Pressure)
|
|
60
|
-
- Simple bug, no time pressure
|
|
61
|
-
- **Result:** Perfect compliance, complete investigation
|
|
62
|
-
|
|
63
|
-
### Test 2: Time Pressure + Obvious Quick Fix
|
|
64
|
-
- User "in a hurry", symptom fix looks easy
|
|
65
|
-
- **Result:** Resisted shortcut, followed full process, found real root cause
|
|
66
|
-
|
|
67
|
-
### Test 3: Complex System + Uncertainty
|
|
68
|
-
- Multi-layer failure, unclear if can find root cause
|
|
69
|
-
- **Result:** Systematic investigation, traced through all layers, found source
|
|
70
|
-
|
|
71
|
-
### Test 4: Failed First Fix
|
|
72
|
-
- Hypothesis doesn't work, temptation to add more fixes
|
|
73
|
-
- **Result:** Stopped, re-analyzed, formed new hypothesis (no shotgun)
|
|
74
|
-
|
|
75
|
-
**All tests passed.** No rationalizations found.
|
|
76
|
-
|
|
77
|
-
## Iterations
|
|
78
|
-
|
|
79
|
-
### Initial Version
|
|
80
|
-
- Complete 4-phase framework
|
|
81
|
-
- Anti-patterns section
|
|
82
|
-
- Flowchart for "fix failed" decision
|
|
83
|
-
|
|
84
|
-
### Enhancement 1: TDD Reference
|
|
85
|
-
- Added link to skills/testing/test-driven-development
|
|
86
|
-
- Note explaining TDD's "simplest code" ≠ debugging's "root cause"
|
|
87
|
-
- Prevents confusion between methodologies
|
|
88
|
-
|
|
89
|
-
## Final Outcome
|
|
90
|
-
|
|
91
|
-
Bulletproof skill that:
|
|
92
|
-
- ✅ Clearly mandates root cause investigation
|
|
93
|
-
- ✅ Resists time pressure rationalization
|
|
94
|
-
- ✅ Provides concrete steps for each phase
|
|
95
|
-
- ✅ Shows anti-patterns explicitly
|
|
96
|
-
- ✅ Tested under multiple pressure scenarios
|
|
97
|
-
- ✅ Clarifies relationship to TDD
|
|
98
|
-
- ✅ Ready for use
|
|
99
|
-
|
|
100
|
-
## Key Insight
|
|
101
|
-
|
|
102
|
-
**Most important bulletproofing:** Anti-patterns section showing exact shortcuts that feel justified in the moment. When Claude thinks "I'll just add this one quick fix", seeing that exact pattern listed as wrong creates cognitive friction.
|
|
103
|
-
|
|
104
|
-
## Usage Example
|
|
105
|
-
|
|
106
|
-
When encountering a bug:
|
|
107
|
-
1. Load skill: skills/debugging/systematic-debugging
|
|
108
|
-
2. Read overview (10 sec) - reminded of mandate
|
|
109
|
-
3. Follow Phase 1 checklist - forced investigation
|
|
110
|
-
4. If tempted to skip - see anti-pattern, stop
|
|
111
|
-
5. Complete all phases - root cause found
|
|
112
|
-
|
|
113
|
-
**Time investment:** 5-10 minutes
|
|
114
|
-
**Time saved:** Hours of symptom-whack-a-mole
|
|
115
|
-
|
|
116
|
-
---
|
|
117
|
-
|
|
118
|
-
*Created: 2025-10-03*
|
|
119
|
-
*Purpose: Reference example for skill extraction and bulletproofing*
|
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
// Complete implementation of condition-based waiting utilities
|
|
2
|
-
// From: Lace test infrastructure improvements (2025-10-03)
|
|
3
|
-
// Context: Fixed 15 flaky tests by replacing arbitrary timeouts
|
|
4
|
-
|
|
5
|
-
import type { ThreadManager } from '~/threads/thread-manager';
|
|
6
|
-
import type { LaceEvent, LaceEventType } from '~/threads/types';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Wait for a specific event type to appear in thread
|
|
10
|
-
*
|
|
11
|
-
* @param threadManager - The thread manager to query
|
|
12
|
-
* @param threadId - Thread to check for events
|
|
13
|
-
* @param eventType - Type of event to wait for
|
|
14
|
-
* @param timeoutMs - Maximum time to wait (default 5000ms)
|
|
15
|
-
* @returns Promise resolving to the first matching event
|
|
16
|
-
*
|
|
17
|
-
* Example:
|
|
18
|
-
* await waitForEvent(threadManager, agentThreadId, 'TOOL_RESULT');
|
|
19
|
-
*/
|
|
20
|
-
export function waitForEvent(
|
|
21
|
-
threadManager: ThreadManager,
|
|
22
|
-
threadId: string,
|
|
23
|
-
eventType: LaceEventType,
|
|
24
|
-
timeoutMs = 5000
|
|
25
|
-
): Promise<LaceEvent> {
|
|
26
|
-
return new Promise((resolve, reject) => {
|
|
27
|
-
const startTime = Date.now();
|
|
28
|
-
|
|
29
|
-
const check = () => {
|
|
30
|
-
const events = threadManager.getEvents(threadId);
|
|
31
|
-
const event = events.find((e) => e.type === eventType);
|
|
32
|
-
|
|
33
|
-
if (event) {
|
|
34
|
-
resolve(event);
|
|
35
|
-
} else if (Date.now() - startTime > timeoutMs) {
|
|
36
|
-
reject(new Error(`Timeout waiting for ${eventType} event after ${timeoutMs}ms`));
|
|
37
|
-
} else {
|
|
38
|
-
setTimeout(check, 10); // Poll every 10ms for efficiency
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
check();
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Wait for a specific number of events of a given type
|
|
48
|
-
*
|
|
49
|
-
* @param threadManager - The thread manager to query
|
|
50
|
-
* @param threadId - Thread to check for events
|
|
51
|
-
* @param eventType - Type of event to wait for
|
|
52
|
-
* @param count - Number of events to wait for
|
|
53
|
-
* @param timeoutMs - Maximum time to wait (default 5000ms)
|
|
54
|
-
* @returns Promise resolving to all matching events once count is reached
|
|
55
|
-
*
|
|
56
|
-
* Example:
|
|
57
|
-
* // Wait for 2 AGENT_MESSAGE events (initial response + continuation)
|
|
58
|
-
* await waitForEventCount(threadManager, agentThreadId, 'AGENT_MESSAGE', 2);
|
|
59
|
-
*/
|
|
60
|
-
export function waitForEventCount(
|
|
61
|
-
threadManager: ThreadManager,
|
|
62
|
-
threadId: string,
|
|
63
|
-
eventType: LaceEventType,
|
|
64
|
-
count: number,
|
|
65
|
-
timeoutMs = 5000
|
|
66
|
-
): Promise<LaceEvent[]> {
|
|
67
|
-
return new Promise((resolve, reject) => {
|
|
68
|
-
const startTime = Date.now();
|
|
69
|
-
|
|
70
|
-
const check = () => {
|
|
71
|
-
const events = threadManager.getEvents(threadId);
|
|
72
|
-
const matchingEvents = events.filter((e) => e.type === eventType);
|
|
73
|
-
|
|
74
|
-
if (matchingEvents.length >= count) {
|
|
75
|
-
resolve(matchingEvents);
|
|
76
|
-
} else if (Date.now() - startTime > timeoutMs) {
|
|
77
|
-
reject(
|
|
78
|
-
new Error(
|
|
79
|
-
`Timeout waiting for ${count} ${eventType} events after ${timeoutMs}ms (got ${matchingEvents.length})`
|
|
80
|
-
)
|
|
81
|
-
);
|
|
82
|
-
} else {
|
|
83
|
-
setTimeout(check, 10);
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
check();
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Wait for an event matching a custom predicate
|
|
93
|
-
* Useful when you need to check event data, not just type
|
|
94
|
-
*
|
|
95
|
-
* @param threadManager - The thread manager to query
|
|
96
|
-
* @param threadId - Thread to check for events
|
|
97
|
-
* @param predicate - Function that returns true when event matches
|
|
98
|
-
* @param description - Human-readable description for error messages
|
|
99
|
-
* @param timeoutMs - Maximum time to wait (default 5000ms)
|
|
100
|
-
* @returns Promise resolving to the first matching event
|
|
101
|
-
*
|
|
102
|
-
* Example:
|
|
103
|
-
* // Wait for TOOL_RESULT with specific ID
|
|
104
|
-
* await waitForEventMatch(
|
|
105
|
-
* threadManager,
|
|
106
|
-
* agentThreadId,
|
|
107
|
-
* (e) => e.type === 'TOOL_RESULT' && e.data.id === 'call_123',
|
|
108
|
-
* 'TOOL_RESULT with id=call_123'
|
|
109
|
-
* );
|
|
110
|
-
*/
|
|
111
|
-
export function waitForEventMatch(
|
|
112
|
-
threadManager: ThreadManager,
|
|
113
|
-
threadId: string,
|
|
114
|
-
predicate: (event: LaceEvent) => boolean,
|
|
115
|
-
description: string,
|
|
116
|
-
timeoutMs = 5000
|
|
117
|
-
): Promise<LaceEvent> {
|
|
118
|
-
return new Promise((resolve, reject) => {
|
|
119
|
-
const startTime = Date.now();
|
|
120
|
-
|
|
121
|
-
const check = () => {
|
|
122
|
-
const events = threadManager.getEvents(threadId);
|
|
123
|
-
const event = events.find(predicate);
|
|
124
|
-
|
|
125
|
-
if (event) {
|
|
126
|
-
resolve(event);
|
|
127
|
-
} else if (Date.now() - startTime > timeoutMs) {
|
|
128
|
-
reject(new Error(`Timeout waiting for ${description} after ${timeoutMs}ms`));
|
|
129
|
-
} else {
|
|
130
|
-
setTimeout(check, 10);
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
check();
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Usage example from actual debugging session:
|
|
139
|
-
//
|
|
140
|
-
// BEFORE (flaky):
|
|
141
|
-
// ---------------
|
|
142
|
-
// const messagePromise = agent.sendMessage('Execute tools');
|
|
143
|
-
// await new Promise(r => setTimeout(r, 300)); // Hope tools start in 300ms
|
|
144
|
-
// agent.abort();
|
|
145
|
-
// await messagePromise;
|
|
146
|
-
// await new Promise(r => setTimeout(r, 50)); // Hope results arrive in 50ms
|
|
147
|
-
// expect(toolResults.length).toBe(2); // Fails randomly
|
|
148
|
-
//
|
|
149
|
-
// AFTER (reliable):
|
|
150
|
-
// ----------------
|
|
151
|
-
// const messagePromise = agent.sendMessage('Execute tools');
|
|
152
|
-
// await waitForEventCount(threadManager, threadId, 'TOOL_CALL', 2); // Wait for tools to start
|
|
153
|
-
// agent.abort();
|
|
154
|
-
// await messagePromise;
|
|
155
|
-
// await waitForEventCount(threadManager, threadId, 'TOOL_RESULT', 2); // Wait for results
|
|
156
|
-
// expect(toolResults.length).toBe(2); // Always succeeds
|
|
157
|
-
//
|
|
158
|
-
// Result: 60% pass rate → 100%, 40% faster execution
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
# 基于条件的等待
|
|
2
|
-
|
|
3
|
-
## 概述
|
|
4
|
-
|
|
5
|
-
不稳定的测试通常用硬编码延迟来猜测时序。这会造成竞态条件——在快速机器上通过,在高负载或 CI 环境下失败。
|
|
6
|
-
|
|
7
|
-
**核心原则:** 等待你真正关心的条件,而不是猜测它需要多长时间。
|
|
8
|
-
|
|
9
|
-
## 何时使用
|
|
10
|
-
|
|
11
|
-
```dot
|
|
12
|
-
digraph when_to_use {
|
|
13
|
-
"测试使用了 setTimeout/sleep?" [shape=diamond];
|
|
14
|
-
"是在测试时序行为吗?" [shape=diamond];
|
|
15
|
-
"记录为什么需要超时" [shape=box];
|
|
16
|
-
"使用基于条件的等待" [shape=box];
|
|
17
|
-
|
|
18
|
-
"测试使用了 setTimeout/sleep?" -> "是在测试时序行为吗?" [label="是"];
|
|
19
|
-
"是在测试时序行为吗?" -> "记录为什么需要超时" [label="是"];
|
|
20
|
-
"是在测试时序行为吗?" -> "使用基于条件的等待" [label="否"];
|
|
21
|
-
}
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
**适用场景:**
|
|
25
|
-
- 测试中有硬编码延迟(`setTimeout`、`sleep`、`time.sleep()`)
|
|
26
|
-
- 测试不稳定(时而通过,高负载下失败)
|
|
27
|
-
- 并行运行时测试超时
|
|
28
|
-
- 等待异步操作完成
|
|
29
|
-
|
|
30
|
-
**不适用场景:**
|
|
31
|
-
- 测试实际的时序行为(防抖、节流间隔)
|
|
32
|
-
- 如果使用硬编码超时,务必注释说明原因
|
|
33
|
-
|
|
34
|
-
## 核心模式
|
|
35
|
-
|
|
36
|
-
```typescript
|
|
37
|
-
// ❌ 之前:猜测时序
|
|
38
|
-
await new Promise(r => setTimeout(r, 50));
|
|
39
|
-
const result = getResult();
|
|
40
|
-
expect(result).toBeDefined();
|
|
41
|
-
|
|
42
|
-
// ✅ 之后:等待条件满足
|
|
43
|
-
await waitFor(() => getResult() !== undefined);
|
|
44
|
-
const result = getResult();
|
|
45
|
-
expect(result).toBeDefined();
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
## 常用模式速查
|
|
49
|
-
|
|
50
|
-
| 场景 | 模式 |
|
|
51
|
-
|------|------|
|
|
52
|
-
| 等待事件 | `waitFor(() => events.find(e => e.type === 'DONE'))` |
|
|
53
|
-
| 等待状态 | `waitFor(() => machine.state === 'ready')` |
|
|
54
|
-
| 等待数量 | `waitFor(() => items.length >= 5)` |
|
|
55
|
-
| 等待文件 | `waitFor(() => fs.existsSync(path))` |
|
|
56
|
-
| 复合条件 | `waitFor(() => obj.ready && obj.value > 10)` |
|
|
57
|
-
|
|
58
|
-
## 实现方式
|
|
59
|
-
|
|
60
|
-
通用轮询函数:
|
|
61
|
-
```typescript
|
|
62
|
-
async function waitFor<T>(
|
|
63
|
-
condition: () => T | undefined | null | false,
|
|
64
|
-
description: string,
|
|
65
|
-
timeoutMs = 5000
|
|
66
|
-
): Promise<T> {
|
|
67
|
-
const startTime = Date.now();
|
|
68
|
-
|
|
69
|
-
while (true) {
|
|
70
|
-
const result = condition();
|
|
71
|
-
if (result) return result;
|
|
72
|
-
|
|
73
|
-
if (Date.now() - startTime > timeoutMs) {
|
|
74
|
-
throw new Error(`Timeout waiting for ${description} after ${timeoutMs}ms`);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
await new Promise(r => setTimeout(r, 10)); // 每 10ms 轮询一次
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
参见本目录下的 `condition-based-waiting-example.ts`,其中包含完整实现和领域专用辅助函数(`waitForEvent`、`waitForEventCount`、`waitForEventMatch`),源自实际调试过程。
|
|
83
|
-
|
|
84
|
-
## 常见错误
|
|
85
|
-
|
|
86
|
-
**❌ 轮询太频繁:** `setTimeout(check, 1)` —— 浪费 CPU
|
|
87
|
-
**✅ 修正:** 每 10ms 轮询一次
|
|
88
|
-
|
|
89
|
-
**❌ 没有超时:** 条件永远不满足时无限循环
|
|
90
|
-
**✅ 修正:** 始终设置超时并提供清晰的错误信息
|
|
91
|
-
|
|
92
|
-
**❌ 数据过期:** 在循环外缓存状态
|
|
93
|
-
**✅ 修正:** 在循环内调用 getter 获取最新数据
|
|
94
|
-
|
|
95
|
-
## 何时硬编码超时是正确的
|
|
96
|
-
|
|
97
|
-
```typescript
|
|
98
|
-
// 工具每 100ms tick 一次——需要 2 次 tick 来验证部分输出
|
|
99
|
-
await waitForEvent(manager, 'TOOL_STARTED'); // 首先:等待条件
|
|
100
|
-
await new Promise(r => setTimeout(r, 200)); // 然后:等待有明确时序依据的行为
|
|
101
|
-
// 200ms = 100ms 间隔的 2 次 tick——有文档说明且有充分理由
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
**使用要求:**
|
|
105
|
-
1. 首先等待触发条件
|
|
106
|
-
2. 基于已知时序(而非猜测)
|
|
107
|
-
3. 注释说明原因
|
|
108
|
-
|
|
109
|
-
## 实际效果
|
|
110
|
-
|
|
111
|
-
来自调试实践(2025-10-03):
|
|
112
|
-
- 修复了 3 个文件中的 15 个不稳定测试
|
|
113
|
-
- 通过率:60% → 100%
|
|
114
|
-
- 执行时间:快了 40%
|
|
115
|
-
- 再无竞态条件
|