@tea-agent/loop-agent 0.35.1-beta.0 → 0.35.1-beta.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.
Files changed (168) hide show
  1. package/AGENTS.md +110 -108
  2. package/CHANGELOG.md +16 -26
  3. package/README.md +165 -165
  4. package/bin/agent-worker.js +0 -0
  5. package/bin/loop-agent.js +57 -21
  6. package/dist/application/task-lifecycle/advance.js +0 -1
  7. package/dist/build-stamp.json +6 -0
  8. package/dist/cli/program.js +2 -2
  9. package/dist/commands/cursor-prompt.js +6 -6
  10. package/dist/commands/init-upgrade.js +19 -351
  11. package/dist/commands/init.js +67 -14
  12. package/dist/commands/loop-benchmark.js +11 -11
  13. package/dist/commands/pi-reuse-benchmark.js +16 -16
  14. package/dist/commands/run-dag-progress.js +0 -14
  15. package/dist/commands/task-advance.js +3 -33
  16. package/dist/shared/operator/capabilities.js +1 -38
  17. package/dist/shared/package-metadata.js +32 -0
  18. package/dist/sidecars/cursor-prompt/executor.js +1 -1
  19. package/dist/worker/console/chat/pi-runtime.js +25 -41
  20. package/dist/worker/console/chat/routes.js +4 -27
  21. package/dist/worker/console/operation-runner.js +0 -24
  22. package/dist/worker/console/operator-actions.js +0 -58
  23. package/dist/worker/console/static/assets/index-fsjzREob.js +56 -0
  24. package/dist/worker/console/static/assets/{index-Dups4sSM.css → index-hJqCPs_g.css} +1 -1
  25. package/dist/worker/console/static/index.html +2 -2
  26. package/dist/worker/console/static-src/operator-chat/useChatSessions.js +2 -13
  27. package/dist/worker/console/static-src/operator-chat/useComposer.js +7 -30
  28. package/dist/worker/loop-agent/loop-agent-client.js +13 -0
  29. package/dist/worker/observe/static/copy.js +67 -67
  30. package/dist/worker/observe/static/dag-layout.d.ts +36 -36
  31. package/dist/worker/observe/static/dom.js +220 -220
  32. package/dist/worker/observe/static/relations.js +133 -133
  33. package/dist/worker/observe/static/run-processing.js +148 -148
  34. package/dist/worker/observe/static/views/batch.js +227 -227
  35. package/dist/worker/observe/static/views/failures.js +143 -143
  36. package/dist/worker/observe/static/views/feature.js +492 -492
  37. package/dist/worker/observe/static/views/run.js +453 -453
  38. package/dist/worker/observe/static/views/shell.js +7 -7
  39. package/dist/worker/observe/static/views/timeline.js +163 -163
  40. package/dist/workflows/dag/canvas-observer.js +275 -275
  41. package/dist/workflows/dag/contract-output-registry.js +14 -0
  42. package/dist/workflows/dag/contract-validator-registrations.js +8 -0
  43. package/dist/workflows/dag/dynamic-runtime/shared.js +9 -1
  44. package/dist/workflows/dag/frontend-implementation-contract.js +233 -39
  45. package/dist/workflows/dag/frontend-prewrite-gate.js +364 -61
  46. package/dist/workflows/dag/frontend-repair.js +219 -18
  47. package/dist/workflows/dag/frontend-verification-trace.js +47 -32
  48. package/dist/workflows/dag/init-hybrid.js +41 -24
  49. package/dist/workflows/dag/node-execution.js +89 -0
  50. package/dist/workflows/dag/runner.js +52 -3
  51. package/dist/workflows/dag/scheduler.js +98 -3
  52. package/dist/workflows/dag/types.js +23 -2
  53. package/docs/architecture/evolution.md +73 -73
  54. package/docs/architecture/system-overview.md +100 -100
  55. package/docs/architecture/worker-and-feature.md +122 -122
  56. package/docs/skills/README.md +7 -7
  57. package/docs/templates/adr.md +60 -60
  58. package/docs/templates/agent-dag-authority-surface-audit.prompt.md +94 -94
  59. package/docs/templates/agent-dag-decision-envelope.schema.json +213 -213
  60. package/docs/templates/agent-dag-decision-gate.prompt.md +246 -246
  61. package/docs/templates/agent-dag-process-supervisor.prompt.md +98 -98
  62. package/docs/templates/agent-dag-report.schema.json +473 -473
  63. package/docs/templates/agent-dag-review-verdict.prompt.md +68 -68
  64. package/docs/templates/backend-test-result.schema.json +99 -99
  65. package/docs/templates/evaluation/agents-map-slim-v1.md +87 -87
  66. package/docs/templates/evaluation/agents-map-verbose-v0.md +153 -153
  67. package/docs/templates/feature-spec.md +53 -53
  68. package/docs/templates/frontend-design-contract.md +42 -42
  69. package/docs/templates/frontend-eval/fixtures/failures/01-type-build-error.md +17 -17
  70. package/docs/templates/frontend-eval/fixtures/failures/02-unit-component-test-fail.md +16 -16
  71. package/docs/templates/frontend-eval/fixtures/failures/03-fixture-schema-drift.md +16 -16
  72. package/docs/templates/frontend-eval/fixtures/failures/04-missing-loading-empty-error-state.md +16 -16
  73. package/docs/templates/frontend-eval/fixtures/failures/05-forbidden-write-writeset-expansion.md +16 -16
  74. package/docs/templates/frontend-eval/fixtures/failures/06-unapproved-dependency-add.md +16 -16
  75. package/docs/templates/frontend-eval/fixtures/failures/07-mock-production-on.md +21 -21
  76. package/docs/templates/frontend-eval/fixtures/functional/01-simple-component-style.md +29 -29
  77. package/docs/templates/frontend-eval/fixtures/functional/02-form-validation.md +28 -28
  78. package/docs/templates/frontend-eval/fixtures/functional/03-list-detail-page.md +28 -28
  79. package/docs/templates/frontend-eval/fixtures/functional/04-api-mock.md +29 -29
  80. package/docs/templates/frontend-eval/fixtures/functional/05-permission-auth-gated-ui.md +27 -27
  81. package/docs/templates/frontend-eval/fixtures/functional/06-ssr-server-client-boundary.md +28 -28
  82. package/docs/templates/frontend-eval/fixtures/functional/07-shared-public-component-api.md +28 -28
  83. package/docs/templates/frontend-eval/fixtures/functional/08-pure-local-no-remote.md +27 -27
  84. package/docs/templates/frontend-eval/metrics.md +138 -138
  85. package/docs/templates/frontend-eval/smoke-targets.md +53 -53
  86. package/docs/templates/frontend-task-constraints.md +35 -35
  87. package/docs/templates/frontend-task-requirement.md +70 -70
  88. package/docs/templates/init-evolution-review.md +35 -35
  89. package/docs/templates/init-managed-agents.md +154 -156
  90. package/docs/templates/interactive-ui-round2-experiment.md +66 -66
  91. package/docs/templates/knowledge-graph-bootstrap-dag.json +118 -118
  92. package/docs/templates/knowledge-sync-dag.json +178 -178
  93. package/docs/templates/knowledge-sync-draft.schema.json +71 -71
  94. package/docs/templates/product-line/closeout.yaml +9 -9
  95. package/docs/templates/product-line/design.md +13 -13
  96. package/docs/templates/product-line/links.md +10 -10
  97. package/docs/templates/product-line/requirement.md +17 -17
  98. package/docs/templates/product-line/test-plan.md +7 -7
  99. package/docs/templates/project-start-checklist.md +9 -9
  100. package/docs/templates/qa-report.md +48 -48
  101. package/docs/templates/sprint-contract.md +29 -29
  102. package/docs/templates/worker-dogfood-evidence.md +80 -80
  103. package/docs/templates/worker-dogfood-setup.md +68 -68
  104. package/harness.json +2 -5
  105. package/package.json +2 -2
  106. package/scripts/kb-bootstrap-init-skeleton.sh +0 -0
  107. package/scripts/kb-graph-incremental-prepare.mjs +0 -0
  108. package/scripts/kb-graph-materialize.mjs +105 -105
  109. package/scripts/kb-graph-promote.mjs +164 -164
  110. package/scripts/kb-query.mjs +554 -554
  111. package/skills/agent-worker/SKILL.md +48 -48
  112. package/skills/agent-worker/references/agent-worker-operator.md +159 -159
  113. package/skills/ai-engineering-context/SKILL.md +48 -48
  114. package/skills/analyze-product-dependencies/scripts/test-validators.mjs +0 -0
  115. package/skills/analyze-product-dependencies/scripts/validate-api-documentation.mjs +0 -0
  116. package/skills/analyze-product-dependencies/scripts/validate-dependency-analysis.mjs +0 -0
  117. package/skills/analyze-product-dependencies/scripts/validate-product-requirement-input.mjs +0 -0
  118. package/skills/analyze-product-requirements/scripts/compute-source-identity.mjs +0 -0
  119. package/skills/analyze-product-requirements/scripts/test-validators.mjs +0 -0
  120. package/skills/analyze-product-requirements/scripts/validate-product-analysis.mjs +0 -0
  121. package/skills/analyze-product-requirements/scripts/validate-product-requirement.mjs +0 -0
  122. package/skills/analyze-product-requirements/scripts/validate-requirement-clarification.mjs +0 -0
  123. package/skills/browser-tools/browser-content.js +103 -103
  124. package/skills/browser-tools/browser-cookies.js +35 -35
  125. package/skills/browser-tools/browser-eval.js +53 -53
  126. package/skills/browser-tools/browser-hn-scraper.js +108 -108
  127. package/skills/browser-tools/browser-nav.js +44 -44
  128. package/skills/browser-tools/browser-pick.js +162 -162
  129. package/skills/browser-tools/browser-screenshot.js +34 -34
  130. package/skills/browser-tools/browser-start.js +86 -86
  131. package/skills/browser-tools/package-lock.json +2556 -2556
  132. package/skills/browser-tools/package.json +19 -19
  133. package/skills/code-review-core/SKILL.md +20 -20
  134. package/skills/codebase-scout/SKILL.md +19 -19
  135. package/skills/grill-me/SKILL.md +10 -10
  136. package/skills/local-jacoco-coverage/scripts/run-coverage-analysis.sh +0 -0
  137. package/skills/local-jacoco-coverage/scripts/start-jacoco-agent.sh +0 -0
  138. package/skills/loop-agent/SKILL.md +0 -1
  139. package/skills/loop-agent/references/command-reference.md +639 -641
  140. package/skills/loop-agent/references/docs-converge.md +126 -126
  141. package/skills/loop-agent/references/learned/README.md +21 -21
  142. package/skills/loop-agent/references/pi-prompt.md +23 -23
  143. package/skills/loop-agent/references/pi-subagent-assisted-mode.md +84 -84
  144. package/skills/playwright-cli/references/element-attributes.md +23 -23
  145. package/skills/playwright-cli/references/playwright-tests.md +39 -39
  146. package/skills/playwright-cli/references/request-mocking.md +87 -87
  147. package/skills/playwright-cli/references/running-code.md +241 -241
  148. package/skills/playwright-cli/references/session-management.md +225 -225
  149. package/skills/playwright-cli/references/storage-state.md +275 -275
  150. package/skills/playwright-cli/references/test-generation.md +433 -433
  151. package/skills/requesting-code-review/SKILL.md +101 -101
  152. package/skills/requesting-code-review/code-reviewer.md +168 -168
  153. package/skills/systematic-debugging/CREATION-LOG.md +119 -119
  154. package/skills/systematic-debugging/condition-based-waiting-example.ts +158 -158
  155. package/skills/systematic-debugging/condition-based-waiting.md +115 -115
  156. package/skills/systematic-debugging/defense-in-depth.md +122 -122
  157. package/skills/systematic-debugging/find-polluter.sh +63 -63
  158. package/skills/systematic-debugging/root-cause-tracing.md +169 -169
  159. package/skills/systematic-debugging/test-academic.md +14 -14
  160. package/skills/systematic-debugging/test-pressure-1.md +58 -58
  161. package/skills/systematic-debugging/test-pressure-2.md +68 -68
  162. package/skills/systematic-debugging/test-pressure-3.md +69 -69
  163. package/skills/using-git-worktrees/SKILL.md +215 -215
  164. package/skills/verification-before-completion/SKILL.md +154 -154
  165. package/skills/webapp-testing/SKILL.md +19 -19
  166. package/dist/worker/console/operation-wait.js +0 -241
  167. package/dist/worker/console/static/assets/index-SjjjZnV3.js +0 -56
  168. package/dist/worker/console/static-src/operator-chat/slash-palette-nav.js +0 -141
@@ -1,115 +1,115 @@
1
- # Condition-Based Waiting
2
-
3
- ## Overview
4
-
5
- Flaky tests 常用 arbitrary delays 猜 timing。这制造 race conditions:fast machines 上 pass,load 或 CI 下 fail。
6
-
7
- **Core principle:** Wait for 你真正关心的 actual condition,不是猜需要多久。
8
-
9
- ## When to Use
10
-
11
- ```dot
12
- digraph when_to_use {
13
- "Test uses setTimeout/sleep?" [shape=diamond];
14
- "Testing timing behavior?" [shape=diamond];
15
- "Document WHY timeout needed" [shape=box];
16
- "Use condition-based waiting" [shape=box];
17
-
18
- "Test uses setTimeout/sleep?" -> "Testing timing behavior?" [label="yes"];
19
- "Testing timing behavior?" -> "Document WHY timeout needed" [label="yes"];
20
- "Testing timing behavior?" -> "Use condition-based waiting" [label="no"];
21
- }
22
- ```
23
-
24
- **Use when:**
25
- - Tests 有 arbitrary delays(`setTimeout`、`sleep`、`time.sleep()`)
26
- - Tests flaky(有时 pass,load 下 fail)
27
- - Parallel 运行时 timeout
28
- - 等待 async operations 完成
29
-
30
- **Don't use when:**
31
- - 测试 actual timing behavior(debounce、throttle intervals)
32
- - 若用 arbitrary timeout,ALWAYS document WHY
33
-
34
- ## Core Pattern
35
-
36
- ```typescript
37
- // ❌ BEFORE: Guessing at timing
38
- await new Promise(r => setTimeout(r, 50));
39
- const result = getResult();
40
- expect(result).toBeDefined();
41
-
42
- // ✅ AFTER: Waiting for condition
43
- await waitFor(() => getResult() !== undefined);
44
- const result = getResult();
45
- expect(result).toBeDefined();
46
- ```
47
-
48
- ## Quick Patterns
49
-
50
- | Scenario | Pattern |
51
- |----------|---------|
52
- | Wait for event | `waitFor(() => events.find(e => e.type === 'DONE'))` |
53
- | Wait for state | `waitFor(() => machine.state === 'ready')` |
54
- | Wait for count | `waitFor(() => items.length >= 5)` |
55
- | Wait for file | `waitFor(() => fs.existsSync(path))` |
56
- | Complex condition | `waitFor(() => obj.ready && obj.value > 10)` |
57
-
58
- ## Implementation
59
-
60
- Generic polling function:
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)); // Poll every 10ms
78
- }
79
- }
80
- ```
81
-
82
- 完整实现及 domain-specific helpers(`waitForEvent`、`waitForEventCount`、`waitForEventMatch`)见本目录 `condition-based-waiting-example.ts`,来自 actual debugging session。
83
-
84
- ## Common Mistakes
85
-
86
- **❌ Polling too fast:** `setTimeout(check, 1)` — wastes CPU
87
- **✅ Fix:** Poll every 10ms
88
-
89
- **❌ No timeout:** 条件永不满足则 loop forever
90
- **✅ Fix:** Always include timeout with clear error
91
-
92
- **❌ Stale data:** Loop 前 cache state
93
- **✅ Fix:** Loop 内 call getter 取 fresh data
94
-
95
- ## When Arbitrary Timeout IS Correct
96
-
97
- ```typescript
98
- // Tool ticks every 100ms - need 2 ticks to verify partial output
99
- await waitForEvent(manager, 'TOOL_STARTED'); // First: wait for condition
100
- await new Promise(r => setTimeout(r, 200)); // Then: wait for timed behavior
101
- // 200ms = 2 ticks at 100ms intervals - documented and justified
102
- ```
103
-
104
- **Requirements:**
105
- 1. First wait for triggering condition
106
- 2. Based on known timing(not guessing)
107
- 3. Comment explaining WHY
108
-
109
- ## Real-World Impact
110
-
111
- 来自 debugging session (2025-10-03):
112
- - 修复 3 个文件中 15 个 flaky tests
113
- - Pass rate:60% → 100%
114
- - Execution time:40% faster
115
- - No more race conditions
1
+ # Condition-Based Waiting
2
+
3
+ ## Overview
4
+
5
+ Flaky tests 常用 arbitrary delays 猜 timing。这制造 race conditions:fast machines 上 pass,load 或 CI 下 fail。
6
+
7
+ **Core principle:** Wait for 你真正关心的 actual condition,不是猜需要多久。
8
+
9
+ ## When to Use
10
+
11
+ ```dot
12
+ digraph when_to_use {
13
+ "Test uses setTimeout/sleep?" [shape=diamond];
14
+ "Testing timing behavior?" [shape=diamond];
15
+ "Document WHY timeout needed" [shape=box];
16
+ "Use condition-based waiting" [shape=box];
17
+
18
+ "Test uses setTimeout/sleep?" -> "Testing timing behavior?" [label="yes"];
19
+ "Testing timing behavior?" -> "Document WHY timeout needed" [label="yes"];
20
+ "Testing timing behavior?" -> "Use condition-based waiting" [label="no"];
21
+ }
22
+ ```
23
+
24
+ **Use when:**
25
+ - Tests 有 arbitrary delays(`setTimeout`、`sleep`、`time.sleep()`)
26
+ - Tests flaky(有时 pass,load 下 fail)
27
+ - Parallel 运行时 timeout
28
+ - 等待 async operations 完成
29
+
30
+ **Don't use when:**
31
+ - 测试 actual timing behavior(debounce、throttle intervals)
32
+ - 若用 arbitrary timeout,ALWAYS document WHY
33
+
34
+ ## Core Pattern
35
+
36
+ ```typescript
37
+ // ❌ BEFORE: Guessing at timing
38
+ await new Promise(r => setTimeout(r, 50));
39
+ const result = getResult();
40
+ expect(result).toBeDefined();
41
+
42
+ // ✅ AFTER: Waiting for condition
43
+ await waitFor(() => getResult() !== undefined);
44
+ const result = getResult();
45
+ expect(result).toBeDefined();
46
+ ```
47
+
48
+ ## Quick Patterns
49
+
50
+ | Scenario | Pattern |
51
+ |----------|---------|
52
+ | Wait for event | `waitFor(() => events.find(e => e.type === 'DONE'))` |
53
+ | Wait for state | `waitFor(() => machine.state === 'ready')` |
54
+ | Wait for count | `waitFor(() => items.length >= 5)` |
55
+ | Wait for file | `waitFor(() => fs.existsSync(path))` |
56
+ | Complex condition | `waitFor(() => obj.ready && obj.value > 10)` |
57
+
58
+ ## Implementation
59
+
60
+ Generic polling function:
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)); // Poll every 10ms
78
+ }
79
+ }
80
+ ```
81
+
82
+ 完整实现及 domain-specific helpers(`waitForEvent`、`waitForEventCount`、`waitForEventMatch`)见本目录 `condition-based-waiting-example.ts`,来自 actual debugging session。
83
+
84
+ ## Common Mistakes
85
+
86
+ **❌ Polling too fast:** `setTimeout(check, 1)` — wastes CPU
87
+ **✅ Fix:** Poll every 10ms
88
+
89
+ **❌ No timeout:** 条件永不满足则 loop forever
90
+ **✅ Fix:** Always include timeout with clear error
91
+
92
+ **❌ Stale data:** Loop 前 cache state
93
+ **✅ Fix:** Loop 内 call getter 取 fresh data
94
+
95
+ ## When Arbitrary Timeout IS Correct
96
+
97
+ ```typescript
98
+ // Tool ticks every 100ms - need 2 ticks to verify partial output
99
+ await waitForEvent(manager, 'TOOL_STARTED'); // First: wait for condition
100
+ await new Promise(r => setTimeout(r, 200)); // Then: wait for timed behavior
101
+ // 200ms = 2 ticks at 100ms intervals - documented and justified
102
+ ```
103
+
104
+ **Requirements:**
105
+ 1. First wait for triggering condition
106
+ 2. Based on known timing(not guessing)
107
+ 3. Comment explaining WHY
108
+
109
+ ## Real-World Impact
110
+
111
+ 来自 debugging session (2025-10-03):
112
+ - 修复 3 个文件中 15 个 flaky tests
113
+ - Pass rate:60% → 100%
114
+ - Execution time:40% faster
115
+ - No more race conditions
@@ -1,122 +1,122 @@
1
- # Defense-in-Depth Validation
2
-
3
- ## Overview
4
-
5
- Bug 由 invalid data 引起时,在一处加 validation 感觉足够。但 single check 可被不同 code paths、refactoring 或 mocks 绕过。
6
-
7
- **Core principle:** 在 data 经过的 EVERY layer 验证。使 bug structurally impossible。
8
-
9
- ## Why Multiple Layers
10
-
11
- Single validation:"We fixed the bug"
12
- Multiple layers:"We made the bug impossible"
13
-
14
- 不同 layers 捕获不同 cases:
15
- - Entry validation 捕获 most bugs
16
- - Business logic 捕获 edge cases
17
- - Environment guards 防止 context-specific dangers
18
- - Debug logging 在其他 layers fail 时帮助 forensics
19
-
20
- ## The Four Layers
21
-
22
- ### Layer 1: Entry Point Validation
23
- **Purpose:** 在 API boundary 拒绝 obviously invalid input
24
-
25
- ```typescript
26
- function createProject(name: string, workingDirectory: string) {
27
- if (!workingDirectory || workingDirectory.trim() === '') {
28
- throw new Error('workingDirectory cannot be empty');
29
- }
30
- if (!existsSync(workingDirectory)) {
31
- throw new Error(`workingDirectory does not exist: ${workingDirectory}`);
32
- }
33
- if (!statSync(workingDirectory).isDirectory()) {
34
- throw new Error(`workingDirectory is not a directory: ${workingDirectory}`);
35
- }
36
- // ... proceed
37
- }
38
- ```
39
-
40
- ### Layer 2: Business Logic Validation
41
- **Purpose:** 确保 data 对此 operation 有意义
42
-
43
- ```typescript
44
- function initializeWorkspace(projectDir: string, sessionId: string) {
45
- if (!projectDir) {
46
- throw new Error('projectDir required for workspace initialization');
47
- }
48
- // ... proceed
49
- }
50
- ```
51
-
52
- ### Layer 3: Environment Guards
53
- **Purpose:** 在特定 contexts 阻止 dangerous operations
54
-
55
- ```typescript
56
- async function gitInit(directory: string) {
57
- // In tests, refuse git init outside temp directories
58
- if (process.env.NODE_ENV === 'test') {
59
- const normalized = normalize(resolve(directory));
60
- const tmpDir = normalize(resolve(tmpdir()));
61
-
62
- if (!normalized.startsWith(tmpDir)) {
63
- throw new Error(
64
- `Refusing git init outside temp dir during tests: ${directory}`
65
- );
66
- }
67
- }
68
- // ... proceed
69
- }
70
- ```
71
-
72
- ### Layer 4: Debug Instrumentation
73
- **Purpose:** 捕获 forensics context
74
-
75
- ```typescript
76
- async function gitInit(directory: string) {
77
- const stack = new Error().stack;
78
- logger.debug('About to git init', {
79
- directory,
80
- cwd: process.cwd(),
81
- stack,
82
- });
83
- // ... proceed
84
- }
85
- ```
86
-
87
- ## Applying the Pattern
88
-
89
- 找到 bug 时:
90
-
91
- 1. **Trace the data flow** — Bad value 从哪 originate?在哪使用?
92
- 2. **Map all checkpoints** — 列出 data 经过的 every point
93
- 3. **Add validation at each layer** — Entry、business、environment、debug
94
- 4. **Test each layer** — 尝试 bypass layer 1,verify layer 2 捕获
95
-
96
- ## Example from Session
97
-
98
- Bug:Empty `projectDir` 导致 `git init` 在 source code
99
-
100
- **Data flow:**
101
- 1. Test setup → empty string
102
- 2. `Project.create(name, '')`
103
- 3. `WorkspaceManager.createWorkspace('')`
104
- 4. `git init` 在 `process.cwd()` 运行
105
-
106
- **Four layers added:**
107
- - Layer 1: `Project.create()` validates not empty/exists/writable
108
- - Layer 2: `WorkspaceManager` validates projectDir not empty
109
- - Layer 3: `WorktreeManager` refuses git init outside tmpdir in tests
110
- - Layer 4: Stack trace logging before git init
111
-
112
- **Result:** All 1847 tests passed,bug impossible to reproduce
113
-
114
- ## Key Insight
115
-
116
- 四层都必要。Testing 中每层捕获 others 遗漏的 bugs:
117
- - 不同 code paths 绕过 entry validation
118
- - Mocks 绕过 business logic checks
119
- - 不同 platforms 的 edge cases 需要 environment guards
120
- - Debug logging 识别 structural misuse
121
-
122
- **Don't stop at one validation point.** 在 every layer 加 checks。
1
+ # Defense-in-Depth Validation
2
+
3
+ ## Overview
4
+
5
+ Bug 由 invalid data 引起时,在一处加 validation 感觉足够。但 single check 可被不同 code paths、refactoring 或 mocks 绕过。
6
+
7
+ **Core principle:** 在 data 经过的 EVERY layer 验证。使 bug structurally impossible。
8
+
9
+ ## Why Multiple Layers
10
+
11
+ Single validation:"We fixed the bug"
12
+ Multiple layers:"We made the bug impossible"
13
+
14
+ 不同 layers 捕获不同 cases:
15
+ - Entry validation 捕获 most bugs
16
+ - Business logic 捕获 edge cases
17
+ - Environment guards 防止 context-specific dangers
18
+ - Debug logging 在其他 layers fail 时帮助 forensics
19
+
20
+ ## The Four Layers
21
+
22
+ ### Layer 1: Entry Point Validation
23
+ **Purpose:** 在 API boundary 拒绝 obviously invalid input
24
+
25
+ ```typescript
26
+ function createProject(name: string, workingDirectory: string) {
27
+ if (!workingDirectory || workingDirectory.trim() === '') {
28
+ throw new Error('workingDirectory cannot be empty');
29
+ }
30
+ if (!existsSync(workingDirectory)) {
31
+ throw new Error(`workingDirectory does not exist: ${workingDirectory}`);
32
+ }
33
+ if (!statSync(workingDirectory).isDirectory()) {
34
+ throw new Error(`workingDirectory is not a directory: ${workingDirectory}`);
35
+ }
36
+ // ... proceed
37
+ }
38
+ ```
39
+
40
+ ### Layer 2: Business Logic Validation
41
+ **Purpose:** 确保 data 对此 operation 有意义
42
+
43
+ ```typescript
44
+ function initializeWorkspace(projectDir: string, sessionId: string) {
45
+ if (!projectDir) {
46
+ throw new Error('projectDir required for workspace initialization');
47
+ }
48
+ // ... proceed
49
+ }
50
+ ```
51
+
52
+ ### Layer 3: Environment Guards
53
+ **Purpose:** 在特定 contexts 阻止 dangerous operations
54
+
55
+ ```typescript
56
+ async function gitInit(directory: string) {
57
+ // In tests, refuse git init outside temp directories
58
+ if (process.env.NODE_ENV === 'test') {
59
+ const normalized = normalize(resolve(directory));
60
+ const tmpDir = normalize(resolve(tmpdir()));
61
+
62
+ if (!normalized.startsWith(tmpDir)) {
63
+ throw new Error(
64
+ `Refusing git init outside temp dir during tests: ${directory}`
65
+ );
66
+ }
67
+ }
68
+ // ... proceed
69
+ }
70
+ ```
71
+
72
+ ### Layer 4: Debug Instrumentation
73
+ **Purpose:** 捕获 forensics context
74
+
75
+ ```typescript
76
+ async function gitInit(directory: string) {
77
+ const stack = new Error().stack;
78
+ logger.debug('About to git init', {
79
+ directory,
80
+ cwd: process.cwd(),
81
+ stack,
82
+ });
83
+ // ... proceed
84
+ }
85
+ ```
86
+
87
+ ## Applying the Pattern
88
+
89
+ 找到 bug 时:
90
+
91
+ 1. **Trace the data flow** — Bad value 从哪 originate?在哪使用?
92
+ 2. **Map all checkpoints** — 列出 data 经过的 every point
93
+ 3. **Add validation at each layer** — Entry、business、environment、debug
94
+ 4. **Test each layer** — 尝试 bypass layer 1,verify layer 2 捕获
95
+
96
+ ## Example from Session
97
+
98
+ Bug:Empty `projectDir` 导致 `git init` 在 source code
99
+
100
+ **Data flow:**
101
+ 1. Test setup → empty string
102
+ 2. `Project.create(name, '')`
103
+ 3. `WorkspaceManager.createWorkspace('')`
104
+ 4. `git init` 在 `process.cwd()` 运行
105
+
106
+ **Four layers added:**
107
+ - Layer 1: `Project.create()` validates not empty/exists/writable
108
+ - Layer 2: `WorkspaceManager` validates projectDir not empty
109
+ - Layer 3: `WorktreeManager` refuses git init outside tmpdir in tests
110
+ - Layer 4: Stack trace logging before git init
111
+
112
+ **Result:** All 1847 tests passed,bug impossible to reproduce
113
+
114
+ ## Key Insight
115
+
116
+ 四层都必要。Testing 中每层捕获 others 遗漏的 bugs:
117
+ - 不同 code paths 绕过 entry validation
118
+ - Mocks 绕过 business logic checks
119
+ - 不同 platforms 的 edge cases 需要 environment guards
120
+ - Debug logging 识别 structural misuse
121
+
122
+ **Don't stop at one validation point.** 在 every layer 加 checks。
@@ -1,63 +1,63 @@
1
- #!/usr/bin/env bash
2
- # Bisection script to find which test creates unwanted files/state
3
- # Usage: ./find-polluter.sh <file_or_dir_to_check> <test_pattern>
4
- # Example: ./find-polluter.sh '.git' 'src/**/*.test.ts'
5
-
6
- set -e
7
-
8
- if [ $# -ne 2 ]; then
9
- echo "Usage: $0 <file_to_check> <test_pattern>"
10
- echo "Example: $0 '.git' 'src/**/*.test.ts'"
11
- exit 1
12
- fi
13
-
14
- POLLUTION_CHECK="$1"
15
- TEST_PATTERN="$2"
16
-
17
- echo "🔍 Searching for test that creates: $POLLUTION_CHECK"
18
- echo "Test pattern: $TEST_PATTERN"
19
- echo ""
20
-
21
- # Get list of test files
22
- TEST_FILES=$(find . -path "$TEST_PATTERN" | sort)
23
- TOTAL=$(echo "$TEST_FILES" | wc -l | tr -d ' ')
24
-
25
- echo "Found $TOTAL test files"
26
- echo ""
27
-
28
- COUNT=0
29
- for TEST_FILE in $TEST_FILES; do
30
- COUNT=$((COUNT + 1))
31
-
32
- # Skip if pollution already exists
33
- if [ -e "$POLLUTION_CHECK" ]; then
34
- echo "⚠️ Pollution already exists before test $COUNT/$TOTAL"
35
- echo " Skipping: $TEST_FILE"
36
- continue
37
- fi
38
-
39
- echo "[$COUNT/$TOTAL] Testing: $TEST_FILE"
40
-
41
- # Run the test
42
- npm test "$TEST_FILE" > /dev/null 2>&1 || true
43
-
44
- # Check if pollution appeared
45
- if [ -e "$POLLUTION_CHECK" ]; then
46
- echo ""
47
- echo "🎯 FOUND POLLUTER!"
48
- echo " Test: $TEST_FILE"
49
- echo " Created: $POLLUTION_CHECK"
50
- echo ""
51
- echo "Pollution details:"
52
- ls -la "$POLLUTION_CHECK"
53
- echo ""
54
- echo "To investigate:"
55
- echo " npm test $TEST_FILE # Run just this test"
56
- echo " cat $TEST_FILE # Review test code"
57
- exit 1
58
- fi
59
- done
60
-
61
- echo ""
62
- echo "✅ No polluter found - all tests clean!"
63
- exit 0
1
+ #!/usr/bin/env bash
2
+ # Bisection script to find which test creates unwanted files/state
3
+ # Usage: ./find-polluter.sh <file_or_dir_to_check> <test_pattern>
4
+ # Example: ./find-polluter.sh '.git' 'src/**/*.test.ts'
5
+
6
+ set -e
7
+
8
+ if [ $# -ne 2 ]; then
9
+ echo "Usage: $0 <file_to_check> <test_pattern>"
10
+ echo "Example: $0 '.git' 'src/**/*.test.ts'"
11
+ exit 1
12
+ fi
13
+
14
+ POLLUTION_CHECK="$1"
15
+ TEST_PATTERN="$2"
16
+
17
+ echo "🔍 Searching for test that creates: $POLLUTION_CHECK"
18
+ echo "Test pattern: $TEST_PATTERN"
19
+ echo ""
20
+
21
+ # Get list of test files
22
+ TEST_FILES=$(find . -path "$TEST_PATTERN" | sort)
23
+ TOTAL=$(echo "$TEST_FILES" | wc -l | tr -d ' ')
24
+
25
+ echo "Found $TOTAL test files"
26
+ echo ""
27
+
28
+ COUNT=0
29
+ for TEST_FILE in $TEST_FILES; do
30
+ COUNT=$((COUNT + 1))
31
+
32
+ # Skip if pollution already exists
33
+ if [ -e "$POLLUTION_CHECK" ]; then
34
+ echo "⚠️ Pollution already exists before test $COUNT/$TOTAL"
35
+ echo " Skipping: $TEST_FILE"
36
+ continue
37
+ fi
38
+
39
+ echo "[$COUNT/$TOTAL] Testing: $TEST_FILE"
40
+
41
+ # Run the test
42
+ npm test "$TEST_FILE" > /dev/null 2>&1 || true
43
+
44
+ # Check if pollution appeared
45
+ if [ -e "$POLLUTION_CHECK" ]; then
46
+ echo ""
47
+ echo "🎯 FOUND POLLUTER!"
48
+ echo " Test: $TEST_FILE"
49
+ echo " Created: $POLLUTION_CHECK"
50
+ echo ""
51
+ echo "Pollution details:"
52
+ ls -la "$POLLUTION_CHECK"
53
+ echo ""
54
+ echo "To investigate:"
55
+ echo " npm test $TEST_FILE # Run just this test"
56
+ echo " cat $TEST_FILE # Review test code"
57
+ exit 1
58
+ fi
59
+ done
60
+
61
+ echo ""
62
+ echo "✅ No polluter found - all tests clean!"
63
+ exit 0