@tea-agent/loop-agent 0.35.0 → 0.35.1-beta.0
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/AGENTS.md +108 -108
- package/CHANGELOG.md +30 -0
- package/README.md +165 -165
- package/bin/agent-worker.js +0 -0
- package/bin/loop-agent.js +21 -21
- package/dist/application/task-lifecycle/advance.js +1 -0
- package/dist/commands/cursor-prompt.js +6 -6
- package/dist/commands/init-upgrade.js +351 -19
- package/dist/commands/init.js +14 -67
- package/dist/commands/loop-benchmark.js +11 -11
- package/dist/commands/pi-reuse-benchmark.js +16 -16
- package/dist/commands/run-dag-progress.js +14 -0
- package/dist/commands/task-advance.js +33 -3
- package/dist/shared/operator/capabilities.js +38 -1
- package/dist/sidecars/cursor-prompt/executor.js +1 -1
- package/dist/worker/console/chat/pi-runtime.js +41 -25
- package/dist/worker/console/chat/routes.js +27 -4
- package/dist/worker/console/operation-runner.js +24 -0
- package/dist/worker/console/operation-wait.js +241 -0
- package/dist/worker/console/operator-actions.js +58 -0
- package/dist/worker/console/static/assets/{index-hJqCPs_g.css → index-Dups4sSM.css} +1 -1
- package/dist/worker/console/static/assets/index-SjjjZnV3.js +56 -0
- package/dist/worker/console/static/index.html +2 -2
- package/dist/worker/console/static-src/operator-chat/slash-palette-nav.js +141 -0
- package/dist/worker/console/static-src/operator-chat/useChatSessions.js +13 -2
- package/dist/worker/console/static-src/operator-chat/useComposer.js +30 -7
- package/dist/worker/observe/static/copy.js +67 -67
- package/dist/worker/observe/static/dag-layout.d.ts +36 -36
- package/dist/worker/observe/static/dom.js +220 -220
- package/dist/worker/observe/static/relations.js +133 -133
- package/dist/worker/observe/static/run-processing.js +148 -148
- package/dist/worker/observe/static/views/batch.js +227 -227
- package/dist/worker/observe/static/views/failures.js +143 -143
- package/dist/worker/observe/static/views/feature.js +492 -492
- package/dist/worker/observe/static/views/run.js +453 -453
- package/dist/worker/observe/static/views/shell.js +7 -7
- package/dist/worker/observe/static/views/timeline.js +163 -163
- package/dist/workflows/dag/canvas-observer.js +275 -275
- package/docs/architecture/evolution.md +73 -73
- package/docs/architecture/system-overview.md +100 -100
- package/docs/architecture/worker-and-feature.md +122 -122
- package/docs/skills/README.md +7 -7
- package/docs/templates/adr.md +60 -60
- package/docs/templates/agent-dag-authority-surface-audit.prompt.md +94 -94
- package/docs/templates/agent-dag-decision-envelope.schema.json +213 -213
- package/docs/templates/agent-dag-decision-gate.prompt.md +246 -246
- package/docs/templates/agent-dag-process-supervisor.prompt.md +98 -98
- package/docs/templates/agent-dag-report.schema.json +473 -473
- package/docs/templates/agent-dag-review-verdict.prompt.md +68 -68
- package/docs/templates/backend-test-result.schema.json +99 -99
- package/docs/templates/evaluation/agents-map-slim-v1.md +87 -87
- package/docs/templates/evaluation/agents-map-verbose-v0.md +153 -153
- package/docs/templates/feature-spec.md +53 -53
- package/docs/templates/frontend-design-contract.md +42 -42
- package/docs/templates/frontend-eval/fixtures/failures/01-type-build-error.md +17 -17
- package/docs/templates/frontend-eval/fixtures/failures/02-unit-component-test-fail.md +16 -16
- package/docs/templates/frontend-eval/fixtures/failures/03-fixture-schema-drift.md +16 -16
- package/docs/templates/frontend-eval/fixtures/failures/04-missing-loading-empty-error-state.md +16 -16
- package/docs/templates/frontend-eval/fixtures/failures/05-forbidden-write-writeset-expansion.md +16 -16
- package/docs/templates/frontend-eval/fixtures/failures/06-unapproved-dependency-add.md +16 -16
- package/docs/templates/frontend-eval/fixtures/failures/07-mock-production-on.md +21 -21
- package/docs/templates/frontend-eval/fixtures/functional/01-simple-component-style.md +29 -29
- package/docs/templates/frontend-eval/fixtures/functional/02-form-validation.md +28 -28
- package/docs/templates/frontend-eval/fixtures/functional/03-list-detail-page.md +28 -28
- package/docs/templates/frontend-eval/fixtures/functional/04-api-mock.md +29 -29
- package/docs/templates/frontend-eval/fixtures/functional/05-permission-auth-gated-ui.md +27 -27
- package/docs/templates/frontend-eval/fixtures/functional/06-ssr-server-client-boundary.md +28 -28
- package/docs/templates/frontend-eval/fixtures/functional/07-shared-public-component-api.md +28 -28
- package/docs/templates/frontend-eval/fixtures/functional/08-pure-local-no-remote.md +27 -27
- package/docs/templates/frontend-eval/metrics.md +138 -138
- package/docs/templates/frontend-eval/smoke-targets.md +53 -53
- package/docs/templates/frontend-task-constraints.md +35 -35
- package/docs/templates/frontend-task-requirement.md +70 -70
- package/docs/templates/init-evolution-review.md +35 -35
- package/docs/templates/init-managed-agents.md +156 -154
- package/docs/templates/interactive-ui-round2-experiment.md +66 -66
- package/docs/templates/knowledge-graph-bootstrap-dag.json +118 -118
- package/docs/templates/knowledge-sync-dag.json +178 -178
- package/docs/templates/knowledge-sync-draft.schema.json +71 -71
- package/docs/templates/product-line/closeout.yaml +9 -9
- package/docs/templates/product-line/design.md +13 -13
- package/docs/templates/product-line/links.md +10 -10
- package/docs/templates/product-line/requirement.md +17 -17
- package/docs/templates/product-line/test-plan.md +7 -7
- package/docs/templates/project-start-checklist.md +9 -9
- package/docs/templates/qa-report.md +48 -48
- package/docs/templates/sprint-contract.md +29 -29
- package/docs/templates/worker-dogfood-evidence.md +80 -80
- package/docs/templates/worker-dogfood-setup.md +68 -68
- package/harness.json +5 -2
- package/package.json +1 -1
- package/scripts/kb-bootstrap-init-skeleton.sh +0 -0
- package/scripts/kb-graph-incremental-prepare.mjs +0 -0
- package/scripts/kb-graph-materialize.mjs +105 -105
- package/scripts/kb-graph-promote.mjs +164 -164
- package/scripts/kb-query.mjs +554 -554
- package/skills/agent-worker/SKILL.md +48 -48
- package/skills/agent-worker/references/agent-worker-operator.md +159 -159
- package/skills/ai-engineering-context/SKILL.md +48 -48
- package/skills/analyze-product-dependencies/scripts/test-validators.mjs +0 -0
- package/skills/analyze-product-dependencies/scripts/validate-api-documentation.mjs +0 -0
- package/skills/analyze-product-dependencies/scripts/validate-dependency-analysis.mjs +0 -0
- package/skills/analyze-product-dependencies/scripts/validate-product-requirement-input.mjs +0 -0
- package/skills/analyze-product-requirements/scripts/compute-source-identity.mjs +0 -0
- package/skills/analyze-product-requirements/scripts/test-validators.mjs +0 -0
- package/skills/analyze-product-requirements/scripts/validate-product-analysis.mjs +0 -0
- package/skills/analyze-product-requirements/scripts/validate-product-requirement.mjs +0 -0
- package/skills/analyze-product-requirements/scripts/validate-requirement-clarification.mjs +0 -0
- package/skills/browser-tools/browser-content.js +103 -103
- package/skills/browser-tools/browser-cookies.js +35 -35
- package/skills/browser-tools/browser-eval.js +53 -53
- package/skills/browser-tools/browser-hn-scraper.js +108 -108
- package/skills/browser-tools/browser-nav.js +44 -44
- package/skills/browser-tools/browser-pick.js +162 -162
- package/skills/browser-tools/browser-screenshot.js +34 -34
- package/skills/browser-tools/browser-start.js +86 -86
- package/skills/browser-tools/package-lock.json +2556 -2556
- package/skills/browser-tools/package.json +19 -19
- package/skills/code-review-core/SKILL.md +20 -20
- package/skills/codebase-scout/SKILL.md +19 -19
- package/skills/grill-me/SKILL.md +10 -10
- package/skills/local-jacoco-coverage/scripts/run-coverage-analysis.sh +0 -0
- package/skills/local-jacoco-coverage/scripts/start-jacoco-agent.sh +0 -0
- package/skills/loop-agent/SKILL.md +1 -0
- package/skills/loop-agent/references/command-reference.md +641 -639
- package/skills/loop-agent/references/docs-converge.md +126 -126
- package/skills/loop-agent/references/learned/README.md +21 -21
- package/skills/loop-agent/references/pi-prompt.md +23 -23
- package/skills/loop-agent/references/pi-subagent-assisted-mode.md +84 -84
- package/skills/playwright-cli/references/element-attributes.md +23 -23
- package/skills/playwright-cli/references/playwright-tests.md +39 -39
- package/skills/playwright-cli/references/request-mocking.md +87 -87
- package/skills/playwright-cli/references/running-code.md +241 -241
- package/skills/playwright-cli/references/session-management.md +225 -225
- package/skills/playwright-cli/references/storage-state.md +275 -275
- package/skills/playwright-cli/references/test-generation.md +433 -433
- package/skills/requesting-code-review/SKILL.md +101 -101
- package/skills/requesting-code-review/code-reviewer.md +168 -168
- package/skills/systematic-debugging/CREATION-LOG.md +119 -119
- package/skills/systematic-debugging/condition-based-waiting-example.ts +158 -158
- package/skills/systematic-debugging/condition-based-waiting.md +115 -115
- package/skills/systematic-debugging/defense-in-depth.md +122 -122
- package/skills/systematic-debugging/find-polluter.sh +63 -63
- package/skills/systematic-debugging/root-cause-tracing.md +169 -169
- package/skills/systematic-debugging/test-academic.md +14 -14
- package/skills/systematic-debugging/test-pressure-1.md +58 -58
- package/skills/systematic-debugging/test-pressure-2.md +68 -68
- package/skills/systematic-debugging/test-pressure-3.md +69 -69
- package/skills/using-git-worktrees/SKILL.md +215 -215
- package/skills/verification-before-completion/SKILL.md +154 -154
- package/skills/webapp-testing/SKILL.md +19 -19
- package/dist/worker/console/static/assets/index-fsjzREob.js +0 -56
|
@@ -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
|