liteagents 3.0.0 → 3.6.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/CHANGELOG.md +158 -1
- package/README.md +113 -169
- package/installer/cli.js +4 -2
- package/installer/package-manager.js +3 -1
- package/package.json +4 -3
- package/packages/ampcode/AGENT.md +10 -18
- package/packages/ampcode/agents/code-developer.md +11 -17
- package/packages/ampcode/agents/orchestrator.md +1 -1
- package/packages/ampcode/agents/quality-assurance.md +3 -1
- package/packages/ampcode/{commands/brainstorming.md → skills/brainstorming/SKILL.md} +2 -3
- package/packages/ampcode/{commands/branch-review.md → skills/branch-review/SKILL.md} +27 -13
- package/packages/{claude/commands/docs-builder.md → ampcode/skills/docs-builder/SKILL.md} +1 -1
- package/packages/ampcode/{commands/live-canvas.md → skills/live-canvas/SKILL.md} +8 -4
- package/packages/ampcode/{commands/refactor.md → skills/refactor/SKILL.md} +49 -7
- package/packages/ampcode/{commands/release.md → skills/release/SKILL.md} +4 -4
- package/packages/{claude/commands → ampcode/skills}/remember/AGENT_RULES.md +44 -84
- package/packages/ampcode/{commands/remember.md → skills/remember/SKILL.md} +11 -6
- package/packages/ampcode/{commands → skills}/remember/friction.cjs +0 -0
- package/packages/ampcode/{commands → skills}/remember/stub-check.cjs +28 -19
- package/packages/ampcode/{commands → skills}/remember/sync-rules.cjs +28 -19
- package/packages/{claude/commands → ampcode/skills}/remember/version-check.cjs +1 -1
- package/packages/ampcode/skills/root-cause/SKILL.md +220 -0
- package/packages/ampcode/{commands/trace-back → skills/root-cause}/find-polluter.sh +0 -0
- package/packages/{claude/commands/security.md → ampcode/skills/security/SKILL.md} +1 -1
- package/packages/{claude/commands/ship.md → ampcode/skills/ship/SKILL.md} +1 -1
- package/packages/ampcode/skills/skill-creator/LICENSE.txt +202 -0
- package/packages/ampcode/{commands/skill-creator.md → skills/skill-creator/SKILL.md} +1 -2
- package/packages/ampcode/{commands → skills}/skill-creator/scripts/init_skill.py +0 -0
- package/packages/ampcode/{commands → skills}/skill-creator/scripts/package_skill.py +0 -0
- package/packages/ampcode/{commands → skills}/skill-creator/scripts/quick_validate.py +0 -0
- package/packages/ampcode/{commands/stash.md → skills/stash/SKILL.md} +2 -1
- package/packages/{claude/commands/test-generate.md → ampcode/skills/test-generate/SKILL.md} +2 -2
- package/packages/ampcode/variants.json +2 -2
- package/packages/claude/CLAUDE.md +10 -17
- package/packages/claude/agents/code-developer.md +11 -17
- package/packages/claude/agents/orchestrator.md +2 -3
- package/packages/claude/agents/quality-assurance.md +3 -1
- package/packages/claude/skills/brainstorming/SKILL.md +1 -2
- package/packages/claude/{commands/branch-review.md → skills/branch-review/SKILL.md} +15 -1
- package/packages/{ampcode/commands/docs-builder.md → claude/skills/docs-builder/SKILL.md} +7 -7
- package/packages/claude/skills/live-canvas/SKILL.md +5 -1
- package/packages/claude/{commands/refactor.md → skills/refactor/SKILL.md} +45 -3
- package/packages/claude/{commands/release.md → skills/release/SKILL.md} +1 -1
- package/packages/{ampcode/commands → claude/skills}/remember/AGENT_RULES.md +38 -78
- package/packages/claude/{commands/remember.md → skills/remember/SKILL.md} +11 -6
- package/packages/claude/{commands → skills}/remember/friction.cjs +0 -0
- package/packages/claude/{commands → skills}/remember/stub-check.cjs +28 -19
- package/packages/claude/{commands → skills}/remember/sync-rules.cjs +28 -19
- package/packages/{ampcode/commands → claude/skills}/remember/version-check.cjs +3 -3
- package/packages/claude/skills/root-cause/SKILL.md +220 -0
- package/packages/{ampcode/commands/security.md → claude/skills/security/SKILL.md} +2 -2
- package/packages/{ampcode/commands/ship.md → claude/skills/ship/SKILL.md} +2 -2
- package/packages/claude/skills/skill-creator/SKILL.md +1 -2
- package/packages/claude/{commands/stash.md → skills/stash/SKILL.md} +2 -1
- package/packages/{ampcode/commands/test-generate.md → claude/skills/test-generate/SKILL.md} +3 -3
- package/packages/claude/variants.json +1 -2
- package/packages/droid/AGENTS.md +10 -15
- package/packages/droid/commands/brainstorming.md +1 -4
- package/packages/droid/commands/branch-review.md +25 -14
- package/packages/droid/commands/docs-builder.md +0 -3
- package/packages/droid/commands/live-canvas.md +7 -5
- package/packages/droid/commands/refactor.md +47 -8
- package/packages/droid/commands/release.md +2 -5
- package/packages/droid/commands/remember/AGENT_RULES.md +38 -78
- package/packages/droid/commands/remember/stub-check.cjs +28 -19
- package/packages/droid/commands/remember/sync-rules.cjs +28 -19
- package/packages/droid/commands/remember/version-check.cjs +1 -1
- package/packages/droid/commands/remember.md +6 -4
- package/packages/droid/commands/root-cause.md +218 -0
- package/packages/droid/commands/security.md +0 -3
- package/packages/droid/commands/ship.md +0 -3
- package/packages/droid/commands/skill-creator/LICENSE.txt +202 -0
- package/packages/droid/commands/skill-creator.md +0 -4
- package/packages/droid/commands/stash.md +0 -2
- package/packages/droid/commands/test-generate.md +1 -4
- package/packages/droid/droids/1-create-prd.md +6 -2
- package/packages/droid/droids/2-generate-tasks.md +1 -2
- package/packages/droid/droids/3-process-task-list.md +1 -2
- package/packages/droid/droids/code-developer.md +12 -19
- package/packages/droid/droids/feature-planner.md +1 -2
- package/packages/droid/droids/market-researcher.md +1 -2
- package/packages/droid/droids/orchestrator.md +1 -2
- package/packages/droid/droids/quality-assurance.md +4 -3
- package/packages/droid/droids/system-architect.md +1 -2
- package/packages/droid/droids/ui-designer.md +1 -2
- package/packages/opencode/AGENTS.md +10 -15
- package/packages/opencode/agent/code-developer.md +11 -17
- package/packages/opencode/agent/quality-assurance.md +3 -1
- package/packages/opencode/command/brainstorming.md +1 -4
- package/packages/opencode/command/branch-review.md +25 -15
- package/packages/opencode/command/docs-builder.md +0 -4
- package/packages/opencode/command/live-canvas.md +7 -5
- package/packages/opencode/command/refactor.md +47 -9
- package/packages/opencode/command/release.md +2 -5
- package/packages/opencode/command/remember/AGENT_RULES.md +38 -78
- package/packages/opencode/command/remember/stub-check.cjs +28 -19
- package/packages/opencode/command/remember/sync-rules.cjs +28 -19
- package/packages/opencode/command/remember/version-check.cjs +1 -1
- package/packages/opencode/command/remember.md +6 -4
- package/packages/opencode/command/root-cause.md +218 -0
- package/packages/opencode/command/security.md +0 -4
- package/packages/opencode/command/ship.md +0 -3
- package/packages/opencode/command/skill-creator/LICENSE.txt +202 -0
- package/packages/opencode/command/skill-creator.md +0 -4
- package/packages/opencode/command/stash.md +0 -3
- package/packages/opencode/command/test-generate.md +1 -5
- package/packages/opencode/opencode.jsonc +4 -24
- package/packages/subagentic-manual.md +147 -314
- package/tools/ampcode/manifest-template.json +0 -1
- package/tools/claude/manifest-template.json +0 -1
- package/tools/droid/manifest-template.json +0 -1
- package/tools/opencode/manifest-template.json +0 -1
- package/packages/ampcode/commands/debug-method.md +0 -297
- package/packages/ampcode/commands/live-canvas/README.md +0 -264
- package/packages/ampcode/commands/optimize.md +0 -61
- package/packages/ampcode/commands/tdd-flow.md +0 -390
- package/packages/ampcode/commands/test-traps/example.ts +0 -158
- package/packages/ampcode/commands/test-traps.md +0 -378
- package/packages/ampcode/commands/trace-back.md +0 -176
- package/packages/ampcode/commands/verify-done.md +0 -152
- package/packages/claude/commands/optimize.md +0 -61
- package/packages/claude/plugins/live-canvas-marketplace/plugins/live-canvas-channel/README.md +0 -89
- package/packages/claude/skills/debug-method/CREATION-LOG.md +0 -119
- package/packages/claude/skills/debug-method/SKILL.md +0 -296
- package/packages/claude/skills/debug-method/test-academic.md +0 -14
- package/packages/claude/skills/debug-method/test-pressure-1.md +0 -58
- package/packages/claude/skills/debug-method/test-pressure-2.md +0 -68
- package/packages/claude/skills/debug-method/test-pressure-3.md +0 -69
- package/packages/claude/skills/live-canvas/README.md +0 -269
- package/packages/claude/skills/tdd-flow/SKILL.md +0 -392
- package/packages/claude/skills/test-traps/SKILL.md +0 -378
- package/packages/claude/skills/test-traps/example.ts +0 -158
- package/packages/claude/skills/trace-back/SKILL.md +0 -176
- package/packages/claude/skills/verify-done/SKILL.md +0 -152
- package/packages/droid/commands/debug-method.md +0 -297
- package/packages/droid/commands/live-canvas/README.md +0 -264
- package/packages/droid/commands/optimize.md +0 -61
- package/packages/droid/commands/tdd-flow.md +0 -390
- package/packages/droid/commands/test-traps/example.ts +0 -158
- package/packages/droid/commands/test-traps.md +0 -378
- package/packages/droid/commands/trace-back.md +0 -176
- package/packages/droid/commands/verify-done.md +0 -152
- package/packages/opencode/command/debug-method.md +0 -297
- package/packages/opencode/command/live-canvas/README.md +0 -264
- package/packages/opencode/command/optimize.md +0 -61
- package/packages/opencode/command/tdd-flow.md +0 -390
- package/packages/opencode/command/test-traps/example.ts +0 -158
- package/packages/opencode/command/test-traps.md +0 -378
- package/packages/opencode/command/trace-back.md +0 -176
- package/packages/opencode/command/verify-done.md +0 -152
- /package/packages/ampcode/{commands → skills}/docs-builder/docs-builder.cjs +0 -0
- /package/packages/ampcode/{commands → skills}/live-canvas/DESIGN_PRINCIPLES.md +0 -0
- /package/packages/ampcode/{commands → skills}/live-canvas/dev/post-variants.html +0 -0
- /package/packages/ampcode/{commands → skills}/live-canvas/templates/lab-banner.html +0 -0
- /package/packages/ampcode/{commands → skills}/live-canvas/templates/overlay-vanilla.js +0 -0
- /package/packages/claude/{commands → skills}/docs-builder/docs-builder.cjs +0 -0
- /package/packages/claude/skills/{trace-back → root-cause}/find-polluter.sh +0 -0
- /package/packages/droid/commands/{trace-back → root-cause}/find-polluter.sh +0 -0
- /package/packages/opencode/command/{trace-back → root-cause}/find-polluter.sh +0 -0
|
@@ -1,378 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: test-traps
|
|
3
|
-
description: Use when writing or changing tests, adding mocks, or tempted to add test-only methods to production code - prevents testing mock behavior, production pollution with test-only methods, and mocking without understanding dependencies
|
|
4
|
-
usage: /test-traps <testing-scenario>
|
|
5
|
-
auto_trigger: true
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
# Test Traps
|
|
9
|
-
|
|
10
|
-
## Overview
|
|
11
|
-
|
|
12
|
-
Tests must verify real behavior, not mock behavior. Mocks are a means to isolate, not the thing being tested.
|
|
13
|
-
|
|
14
|
-
**Core principle:** Test what the code does, not what the mocks do.
|
|
15
|
-
|
|
16
|
-
**Following strict TDD prevents these anti-patterns.**
|
|
17
|
-
|
|
18
|
-
## The Iron Laws
|
|
19
|
-
|
|
20
|
-
```
|
|
21
|
-
1. NEVER test mock behavior
|
|
22
|
-
2. NEVER add test-only methods to production classes
|
|
23
|
-
3. NEVER mock without understanding dependencies
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
## Anti-Pattern 1: Testing Mock Behavior
|
|
27
|
-
|
|
28
|
-
**The violation:**
|
|
29
|
-
```typescript
|
|
30
|
-
// ❌ BAD: Testing that the mock exists
|
|
31
|
-
test('renders sidebar', () => {
|
|
32
|
-
render(<Page />);
|
|
33
|
-
expect(screen.getByTestId('sidebar-mock')).toBeInTheDocument();
|
|
34
|
-
});
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
**Why this is wrong:**
|
|
38
|
-
- You're verifying the mock works, not that the component works
|
|
39
|
-
- Test passes when mock is present, fails when it's not
|
|
40
|
-
- Tells you nothing about real behavior
|
|
41
|
-
|
|
42
|
-
**your human partner's correction:** "Are we testing the behavior of a mock?"
|
|
43
|
-
|
|
44
|
-
**The fix:**
|
|
45
|
-
```typescript
|
|
46
|
-
// ✅ GOOD: Test real component or don't mock it
|
|
47
|
-
test('renders sidebar', () => {
|
|
48
|
-
render(<Page />); // Don't mock sidebar
|
|
49
|
-
expect(screen.getByRole('navigation')).toBeInTheDocument();
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// OR if sidebar must be mocked for isolation:
|
|
53
|
-
// Don't assert on the mock - test Page's behavior with sidebar present
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
### Gate Function
|
|
57
|
-
|
|
58
|
-
```
|
|
59
|
-
BEFORE asserting on any mock element:
|
|
60
|
-
Ask: "Am I testing real component behavior or just mock existence?"
|
|
61
|
-
|
|
62
|
-
IF testing mock existence:
|
|
63
|
-
STOP - Delete the assertion or unmock the component
|
|
64
|
-
|
|
65
|
-
Test real behavior instead
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
## Anti-Pattern 2: Test-Only Methods in Production
|
|
69
|
-
|
|
70
|
-
**The violation:**
|
|
71
|
-
```typescript
|
|
72
|
-
// ❌ BAD: destroy() only used in tests
|
|
73
|
-
class Session {
|
|
74
|
-
async destroy() { // Looks like production API!
|
|
75
|
-
await this._workspaceManager?.destroyWorkspace(this.id);
|
|
76
|
-
// ... cleanup
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// In tests
|
|
81
|
-
afterEach(() => session.destroy());
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
**Why this is wrong:**
|
|
85
|
-
- Production class polluted with test-only code
|
|
86
|
-
- Dangerous if accidentally called in production
|
|
87
|
-
- Violates YAGNI and separation of concerns
|
|
88
|
-
- Confuses object lifecycle with entity lifecycle
|
|
89
|
-
|
|
90
|
-
**The fix:**
|
|
91
|
-
```typescript
|
|
92
|
-
// ✅ GOOD: Test utilities handle test cleanup
|
|
93
|
-
// Session has no destroy() - it's stateless in production
|
|
94
|
-
|
|
95
|
-
// In test-utils/
|
|
96
|
-
export async function cleanupSession(session: Session) {
|
|
97
|
-
const workspace = session.getWorkspaceInfo();
|
|
98
|
-
if (workspace) {
|
|
99
|
-
await workspaceManager.destroyWorkspace(workspace.id);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// In tests
|
|
104
|
-
afterEach(() => cleanupSession(session));
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
### Gate Function
|
|
108
|
-
|
|
109
|
-
```
|
|
110
|
-
BEFORE adding any method to production class:
|
|
111
|
-
Ask: "Is this only used by tests?"
|
|
112
|
-
|
|
113
|
-
IF yes:
|
|
114
|
-
STOP - Don't add it
|
|
115
|
-
Put it in test utilities instead
|
|
116
|
-
|
|
117
|
-
Ask: "Does this class own this resource's lifecycle?"
|
|
118
|
-
|
|
119
|
-
IF no:
|
|
120
|
-
STOP - Wrong class for this method
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
## Anti-Pattern 3: Mocking Without Understanding
|
|
124
|
-
|
|
125
|
-
**The violation:**
|
|
126
|
-
```typescript
|
|
127
|
-
// ❌ BAD: Mock breaks test logic
|
|
128
|
-
test('detects duplicate server', () => {
|
|
129
|
-
// Mock prevents config write that test depends on!
|
|
130
|
-
vi.mock('ToolCatalog', () => ({
|
|
131
|
-
discoverAndCacheTools: vi.fn().mockResolvedValue(undefined)
|
|
132
|
-
}));
|
|
133
|
-
|
|
134
|
-
await addServer(config);
|
|
135
|
-
await addServer(config); // Should throw - but won't!
|
|
136
|
-
});
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
**Why this is wrong:**
|
|
140
|
-
- Mocked method had side effect test depended on (writing config)
|
|
141
|
-
- Over-mocking to "be safe" breaks actual behavior
|
|
142
|
-
- Test passes for wrong reason or fails mysteriously
|
|
143
|
-
|
|
144
|
-
**The fix:**
|
|
145
|
-
```typescript
|
|
146
|
-
// ✅ GOOD: Mock at correct level
|
|
147
|
-
test('detects duplicate server', () => {
|
|
148
|
-
// Mock the slow part, preserve behavior test needs
|
|
149
|
-
vi.mock('MCPServerManager'); // Just mock slow server startup
|
|
150
|
-
|
|
151
|
-
await addServer(config); // Config written
|
|
152
|
-
await addServer(config); // Duplicate detected ✓
|
|
153
|
-
});
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
### Gate Function
|
|
157
|
-
|
|
158
|
-
```
|
|
159
|
-
BEFORE mocking any method:
|
|
160
|
-
STOP - Don't mock yet
|
|
161
|
-
|
|
162
|
-
1. Ask: "What side effects does the real method have?"
|
|
163
|
-
2. Ask: "Does this test depend on any of those side effects?"
|
|
164
|
-
3. Ask: "Do I fully understand what this test needs?"
|
|
165
|
-
|
|
166
|
-
IF depends on side effects:
|
|
167
|
-
Mock at lower level (the actual slow/external operation)
|
|
168
|
-
OR use test doubles that preserve necessary behavior
|
|
169
|
-
NOT the high-level method the test depends on
|
|
170
|
-
|
|
171
|
-
IF unsure what test depends on:
|
|
172
|
-
Run test with real implementation FIRST
|
|
173
|
-
Observe what actually needs to happen
|
|
174
|
-
THEN add minimal mocking at the right level
|
|
175
|
-
|
|
176
|
-
Red flags:
|
|
177
|
-
- "I'll mock this to be safe"
|
|
178
|
-
- "This might be slow, better mock it"
|
|
179
|
-
- Mocking without understanding the dependency chain
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
## Anti-Pattern 4: Incomplete Mocks
|
|
183
|
-
|
|
184
|
-
**The violation:**
|
|
185
|
-
```typescript
|
|
186
|
-
// ❌ BAD: Partial mock - only fields you think you need
|
|
187
|
-
const mockResponse = {
|
|
188
|
-
status: 'success',
|
|
189
|
-
data: { userId: '123', name: 'Alice' }
|
|
190
|
-
// Missing: metadata that downstream code uses
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
// Later: breaks when code accesses response.metadata.requestId
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
**Why this is wrong:**
|
|
197
|
-
- **Partial mocks hide structural assumptions** - You only mocked fields you know about
|
|
198
|
-
- **Downstream code may depend on fields you didn't include** - Silent failures
|
|
199
|
-
- **Tests pass but integration fails** - Mock incomplete, real API complete
|
|
200
|
-
- **False confidence** - Test proves nothing about real behavior
|
|
201
|
-
|
|
202
|
-
**The Iron Rule:** Mock the COMPLETE data structure as it exists in reality, not just fields your immediate test uses.
|
|
203
|
-
|
|
204
|
-
**The fix:**
|
|
205
|
-
```typescript
|
|
206
|
-
// ✅ GOOD: Mirror real API completeness
|
|
207
|
-
const mockResponse = {
|
|
208
|
-
status: 'success',
|
|
209
|
-
data: { userId: '123', name: 'Alice' },
|
|
210
|
-
metadata: { requestId: 'req-789', timestamp: 1234567890 }
|
|
211
|
-
// All fields real API returns
|
|
212
|
-
};
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
### Gate Function
|
|
216
|
-
|
|
217
|
-
```
|
|
218
|
-
BEFORE creating mock responses:
|
|
219
|
-
Check: "What fields does the real API response contain?"
|
|
220
|
-
|
|
221
|
-
Actions:
|
|
222
|
-
1. Examine actual API response from docs/examples
|
|
223
|
-
2. Include ALL fields system might consume downstream
|
|
224
|
-
3. Verify mock matches real response schema completely
|
|
225
|
-
|
|
226
|
-
Critical:
|
|
227
|
-
If you're creating a mock, you must understand the ENTIRE structure
|
|
228
|
-
Partial mocks fail silently when code depends on omitted fields
|
|
229
|
-
|
|
230
|
-
If uncertain: Include all documented fields
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
## Anti-Pattern 5: Integration Tests as Afterthought
|
|
234
|
-
|
|
235
|
-
**The violation:**
|
|
236
|
-
```
|
|
237
|
-
✅ Implementation complete
|
|
238
|
-
❌ No tests written
|
|
239
|
-
"Ready for testing"
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
**Why this is wrong:**
|
|
243
|
-
- Testing is part of implementation, not optional follow-up
|
|
244
|
-
- TDD would have caught this
|
|
245
|
-
- Can't claim complete without tests
|
|
246
|
-
|
|
247
|
-
**The fix:**
|
|
248
|
-
```
|
|
249
|
-
TDD cycle:
|
|
250
|
-
1. Write failing test
|
|
251
|
-
2. Implement to pass
|
|
252
|
-
3. Refactor
|
|
253
|
-
4. THEN claim complete
|
|
254
|
-
```
|
|
255
|
-
|
|
256
|
-
## Anti-Pattern 6: Timeout-Based Waiting
|
|
257
|
-
|
|
258
|
-
**The violation:**
|
|
259
|
-
```typescript
|
|
260
|
-
// Wait an arbitrary amount of time, then check
|
|
261
|
-
await new Promise(r => setTimeout(r, 50));
|
|
262
|
-
const result = getResult();
|
|
263
|
-
expect(result).toBeDefined();
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
```python
|
|
267
|
-
time.sleep(0.5)
|
|
268
|
-
assert get_result() is not None
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
**Why this is wrong:**
|
|
272
|
-
- Passes on fast machines, fails in CI under load (or vice versa).
|
|
273
|
-
- Either too short (flaky) or too long (slow suite) — every value is wrong somewhere.
|
|
274
|
-
- Hides the actual condition the test cares about.
|
|
275
|
-
- A real bug in the async machinery looks identical to a "too-short sleep."
|
|
276
|
-
|
|
277
|
-
**The fix — poll for the actual condition:**
|
|
278
|
-
```typescript
|
|
279
|
-
await waitFor(() => getResult() !== undefined);
|
|
280
|
-
const result = getResult();
|
|
281
|
-
expect(result).toBeDefined();
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
| Scenario | Pattern |
|
|
285
|
-
|----------|---------|
|
|
286
|
-
| Wait for event | `waitFor(() => events.find(e => e.type === 'DONE'))` |
|
|
287
|
-
| Wait for state | `waitFor(() => machine.state === 'ready')` |
|
|
288
|
-
| Wait for count | `waitFor(() => items.length >= 5)` |
|
|
289
|
-
| Wait for file | `waitFor(() => fs.existsSync(path))` |
|
|
290
|
-
| Complex condition | `waitFor(() => obj.ready && obj.value > 10)` |
|
|
291
|
-
|
|
292
|
-
Minimal generic poller:
|
|
293
|
-
```typescript
|
|
294
|
-
async function waitFor<T>(
|
|
295
|
-
condition: () => T | undefined | null | false,
|
|
296
|
-
description = 'condition',
|
|
297
|
-
timeoutMs = 5000
|
|
298
|
-
): Promise<T> {
|
|
299
|
-
const start = Date.now();
|
|
300
|
-
while (true) {
|
|
301
|
-
const result = condition();
|
|
302
|
-
if (result) return result;
|
|
303
|
-
if (Date.now() - start > timeoutMs) {
|
|
304
|
-
throw new Error(`Timeout waiting for ${description} after ${timeoutMs}ms`);
|
|
305
|
-
}
|
|
306
|
-
await new Promise(r => setTimeout(r, 10));
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
```
|
|
310
|
-
|
|
311
|
-
See `example.ts` for domain-specific helpers (`waitForEvent`, `waitForEventCount`, `waitForEventMatch`) drawn from a real debugging session.
|
|
312
|
-
|
|
313
|
-
**Polling pitfalls:**
|
|
314
|
-
- **Polling too fast** (`setTimeout(check, 1)`) wastes CPU. **Fix:** poll every ~10ms.
|
|
315
|
-
- **No timeout** means a never-met condition hangs the suite forever. **Fix:** always include a timeout with a descriptive error.
|
|
316
|
-
- **Stale data** — if you read state once before the loop, you'll never see updates. **Fix:** call the getter *inside* the loop.
|
|
317
|
-
|
|
318
|
-
**When an arbitrary timeout IS correct:**
|
|
319
|
-
```typescript
|
|
320
|
-
// Tool ticks every 100ms; observe 2 ticks of partial output.
|
|
321
|
-
await waitForEvent(manager, 'TOOL_STARTED'); // condition first
|
|
322
|
-
await new Promise(r => setTimeout(r, 200)); // then a documented, calculated wait
|
|
323
|
-
// 200ms = 2 ticks at the documented 100ms cadence
|
|
324
|
-
```
|
|
325
|
-
Requirements: (1) wait for the triggering condition first, (2) the delay is based on a *known* interval (not a guess), (3) a comment explains why a sleep is correct here.
|
|
326
|
-
|
|
327
|
-
**Real-world impact** (debugging session, 2025-10-03): 15 flaky tests across 3 files, pass rate 60% → 100%, suite 40% faster, race conditions gone.
|
|
328
|
-
|
|
329
|
-
## When Mocks Become Too Complex
|
|
330
|
-
|
|
331
|
-
**Warning signs:**
|
|
332
|
-
- Mock setup longer than test logic
|
|
333
|
-
- Mocking everything to make test pass
|
|
334
|
-
- Mocks missing methods real components have
|
|
335
|
-
- Test breaks when mock changes
|
|
336
|
-
|
|
337
|
-
**your human partner's question:** "Do we need to be using a mock here?"
|
|
338
|
-
|
|
339
|
-
**Consider:** Integration tests with real components often simpler than complex mocks
|
|
340
|
-
|
|
341
|
-
## TDD Prevents These Anti-Patterns
|
|
342
|
-
|
|
343
|
-
**Why TDD helps:**
|
|
344
|
-
1. **Write test first** → Forces you to think about what you're actually testing
|
|
345
|
-
2. **Watch it fail** → Confirms test tests real behavior, not mocks
|
|
346
|
-
3. **Minimal implementation** → No test-only methods creep in
|
|
347
|
-
4. **Real dependencies** → You see what the test actually needs before mocking
|
|
348
|
-
|
|
349
|
-
**If you're testing mock behavior, you violated TDD** - you added mocks without watching test fail against real code first.
|
|
350
|
-
|
|
351
|
-
## Quick Reference
|
|
352
|
-
|
|
353
|
-
| Anti-Pattern | Fix |
|
|
354
|
-
|--------------|-----|
|
|
355
|
-
| Assert on mock elements | Test real component or unmock it |
|
|
356
|
-
| Test-only methods in production | Move to test utilities |
|
|
357
|
-
| Mock without understanding | Understand dependencies first, mock minimally |
|
|
358
|
-
| Incomplete mocks | Mirror real API completely |
|
|
359
|
-
| Tests as afterthought | TDD - tests first |
|
|
360
|
-
| Timeout-based waiting | Poll for the actual condition with `waitFor()` |
|
|
361
|
-
| Over-complex mocks | Consider integration tests |
|
|
362
|
-
|
|
363
|
-
## Red Flags
|
|
364
|
-
|
|
365
|
-
- Assertion checks for `*-mock` test IDs
|
|
366
|
-
- Methods only called in test files
|
|
367
|
-
- Mock setup is >50% of test
|
|
368
|
-
- Test fails when you remove mock
|
|
369
|
-
- Can't explain why mock is needed
|
|
370
|
-
- Mocking "just to be safe"
|
|
371
|
-
|
|
372
|
-
## The Bottom Line
|
|
373
|
-
|
|
374
|
-
**Mocks are tools to isolate, not things to test.**
|
|
375
|
-
|
|
376
|
-
If TDD reveals you're testing mock behavior, you've gone wrong.
|
|
377
|
-
|
|
378
|
-
Fix: Test real behavior or question why you're mocking at all.
|
|
@@ -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,176 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: trace-back
|
|
3
|
-
description: Use when errors occur deep in execution and you need to trace back to find the original trigger - systematically traces bugs backward through call stack, adding instrumentation when needed, to identify source of invalid data or incorrect behavior
|
|
4
|
-
usage: /trace-back <issue-description>
|
|
5
|
-
auto_trigger: false
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
# Trace Back
|
|
9
|
-
|
|
10
|
-
## Overview
|
|
11
|
-
|
|
12
|
-
Bugs often manifest deep in the call stack (git init in wrong directory, file created in wrong location, database opened with wrong path). Your instinct is to fix where the error appears, but that's treating a symptom.
|
|
13
|
-
|
|
14
|
-
**Core principle:** Trace backward through the call chain until you find the original trigger, then fix at the source.
|
|
15
|
-
|
|
16
|
-
## When to Use
|
|
17
|
-
|
|
18
|
-
```dot
|
|
19
|
-
digraph when_to_use {
|
|
20
|
-
"Bug appears deep in stack?" [shape=diamond];
|
|
21
|
-
"Can trace backwards?" [shape=diamond];
|
|
22
|
-
"Fix at symptom point" [shape=box];
|
|
23
|
-
"Trace to original trigger" [shape=box];
|
|
24
|
-
"BETTER: Also add defense-in-depth" [shape=box];
|
|
25
|
-
|
|
26
|
-
"Bug appears deep in stack?" -> "Can trace backwards?" [label="yes"];
|
|
27
|
-
"Can trace backwards?" -> "Trace to original trigger" [label="yes"];
|
|
28
|
-
"Can trace backwards?" -> "Fix at symptom point" [label="no - dead end"];
|
|
29
|
-
"Trace to original trigger" -> "BETTER: Also add defense-in-depth";
|
|
30
|
-
}
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
**Use when:**
|
|
34
|
-
- Error happens deep in execution (not at entry point)
|
|
35
|
-
- Stack trace shows long call chain
|
|
36
|
-
- Unclear where invalid data originated
|
|
37
|
-
- Need to find which test/code triggers the problem
|
|
38
|
-
|
|
39
|
-
## The Tracing Process
|
|
40
|
-
|
|
41
|
-
### 1. Observe the Symptom
|
|
42
|
-
```
|
|
43
|
-
Error: git init failed in /Users/jesse/project/packages/core
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
### 2. Find Immediate Cause
|
|
47
|
-
**What code directly causes this?**
|
|
48
|
-
```typescript
|
|
49
|
-
await execFileAsync('git', ['init'], { cwd: projectDir });
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
### 3. Ask: What Called This?
|
|
53
|
-
```typescript
|
|
54
|
-
WorktreeManager.createSessionWorktree(projectDir, sessionId)
|
|
55
|
-
→ called by Session.initializeWorkspace()
|
|
56
|
-
→ called by Session.create()
|
|
57
|
-
→ called by test at Project.create()
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
### 4. Keep Tracing Up
|
|
61
|
-
**What value was passed?**
|
|
62
|
-
- `projectDir = ''` (empty string!)
|
|
63
|
-
- Empty string as `cwd` resolves to `process.cwd()`
|
|
64
|
-
- That's the source code directory!
|
|
65
|
-
|
|
66
|
-
### 5. Find Original Trigger
|
|
67
|
-
**Where did empty string come from?**
|
|
68
|
-
```typescript
|
|
69
|
-
const context = setupCoreTest(); // Returns { tempDir: '' }
|
|
70
|
-
Project.create('name', context.tempDir); // Accessed before beforeEach!
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
## Adding Stack Traces
|
|
74
|
-
|
|
75
|
-
When you can't trace manually, add instrumentation:
|
|
76
|
-
|
|
77
|
-
```typescript
|
|
78
|
-
// Before the problematic operation
|
|
79
|
-
async function gitInit(directory: string) {
|
|
80
|
-
const stack = new Error().stack;
|
|
81
|
-
console.error('DEBUG git init:', {
|
|
82
|
-
directory,
|
|
83
|
-
cwd: process.cwd(),
|
|
84
|
-
nodeEnv: process.env.NODE_ENV,
|
|
85
|
-
stack,
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
await execFileAsync('git', ['init'], { cwd: directory });
|
|
89
|
-
}
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
**Critical:** Use `console.error()` in tests (not logger - may not show)
|
|
93
|
-
|
|
94
|
-
**Run and capture:**
|
|
95
|
-
```bash
|
|
96
|
-
npm test 2>&1 | grep 'DEBUG git init'
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
**Analyze stack traces:**
|
|
100
|
-
- Look for test file names
|
|
101
|
-
- Find the line number triggering the call
|
|
102
|
-
- Identify the pattern (same test? same parameter?)
|
|
103
|
-
|
|
104
|
-
## Finding Which Test Causes Pollution
|
|
105
|
-
|
|
106
|
-
If something appears during tests but you don't know which test:
|
|
107
|
-
|
|
108
|
-
Use the bisection script: @find-polluter.sh
|
|
109
|
-
|
|
110
|
-
```bash
|
|
111
|
-
./find-polluter.sh '.git' 'src/**/*.test.ts'
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
Runs tests one-by-one, stops at first polluter. See script for usage.
|
|
115
|
-
|
|
116
|
-
## Real Example: Empty projectDir
|
|
117
|
-
|
|
118
|
-
**Symptom:** `.git` created in `packages/core/` (source code)
|
|
119
|
-
|
|
120
|
-
**Trace chain:**
|
|
121
|
-
1. `git init` runs in `process.cwd()` ← empty cwd parameter
|
|
122
|
-
2. WorktreeManager called with empty projectDir
|
|
123
|
-
3. Session.create() passed empty string
|
|
124
|
-
4. Test accessed `context.tempDir` before beforeEach
|
|
125
|
-
5. setupCoreTest() returns `{ tempDir: '' }` initially
|
|
126
|
-
|
|
127
|
-
**Root cause:** Top-level variable initialization accessing empty value
|
|
128
|
-
|
|
129
|
-
**Fix:** Made tempDir a getter that throws if accessed before beforeEach
|
|
130
|
-
|
|
131
|
-
**Also added defense-in-depth:**
|
|
132
|
-
- Layer 1: Project.create() validates directory
|
|
133
|
-
- Layer 2: WorkspaceManager validates not empty
|
|
134
|
-
- Layer 3: NODE_ENV guard refuses git init outside tmpdir
|
|
135
|
-
- Layer 4: Stack trace logging before git init
|
|
136
|
-
|
|
137
|
-
## Key Principle
|
|
138
|
-
|
|
139
|
-
```dot
|
|
140
|
-
digraph principle {
|
|
141
|
-
"Found immediate cause" [shape=ellipse];
|
|
142
|
-
"Can trace one level up?" [shape=diamond];
|
|
143
|
-
"Trace backwards" [shape=box];
|
|
144
|
-
"Is this the source?" [shape=diamond];
|
|
145
|
-
"Fix at source" [shape=box];
|
|
146
|
-
"Add validation at each layer" [shape=box];
|
|
147
|
-
"Bug impossible" [shape=doublecircle];
|
|
148
|
-
"NEVER fix just the symptom" [shape=octagon, style=filled, fillcolor=red, fontcolor=white];
|
|
149
|
-
|
|
150
|
-
"Found immediate cause" -> "Can trace one level up?";
|
|
151
|
-
"Can trace one level up?" -> "Trace backwards" [label="yes"];
|
|
152
|
-
"Can trace one level up?" -> "NEVER fix just the symptom" [label="no"];
|
|
153
|
-
"Trace backwards" -> "Is this the source?";
|
|
154
|
-
"Is this the source?" -> "Trace backwards" [label="no - keeps going"];
|
|
155
|
-
"Is this the source?" -> "Fix at source" [label="yes"];
|
|
156
|
-
"Fix at source" -> "Add validation at each layer";
|
|
157
|
-
"Add validation at each layer" -> "Bug impossible";
|
|
158
|
-
}
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
**NEVER fix just where the error appears.** Trace back to find the original trigger.
|
|
162
|
-
|
|
163
|
-
## Stack Trace Tips
|
|
164
|
-
|
|
165
|
-
**In tests:** Use `console.error()` not logger - logger may be suppressed
|
|
166
|
-
**Before operation:** Log before the dangerous operation, not after it fails
|
|
167
|
-
**Include context:** Directory, cwd, environment variables, timestamps
|
|
168
|
-
**Capture stack:** `new Error().stack` shows complete call chain
|
|
169
|
-
|
|
170
|
-
## Real-World Impact
|
|
171
|
-
|
|
172
|
-
From debugging session (2025-10-03):
|
|
173
|
-
- Found root cause through 5-level trace
|
|
174
|
-
- Fixed at source (getter validation)
|
|
175
|
-
- Added 4 layers of defense
|
|
176
|
-
- 1847 tests passed, zero pollution
|