You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/patterns/discover-before-implement.md
+69-10Lines changed: 69 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,21 @@
2
2
3
3
Before implementing ANY new feature, search for existing utilities, constants, and templates in the codebase. Duplicating content that already exists is the most common source of architectural drift.
4
4
5
+
## CRITICAL: Check Test Files First for Expected API Contracts
6
+
7
+
If a test file already exists for the module you're creating (check `__tests__/` directories), **read it before writing a single line of implementation**. Tests reveal:
8
+
- The **exact function name** the codebase expects to export
9
+
- The **exact function signature** (arguments and return type)
10
+
- The **exact behavior** expected (message format, file creation patterns, etc.)
11
+
12
+
Example: `cli/src/commands/__tests__/init.test.ts` imports `{ handleInitializationFlowLocally }` from `'../init'`. This means the function MUST be named `handleInitializationFlowLocally`, not `handleInitCommand`.
13
+
14
+
**Decision tree:**
15
+
1. Does `__tests__/[filename].test.ts` exist? → Read it FIRST
16
+
2. What does it import? → That's your required export name
17
+
3. How does it call the function? → That's your required signature
18
+
4. What does it assert? → That's your required behavior
19
+
5
20
## Key Locations to Check in This Codebase
6
21
7
22
### Project Root Utilities
@@ -31,25 +46,41 @@ import agentDefinitionSource from '../../../common/src/templates/initial-agents-
31
46
32
47
## CLI Command Pattern
33
48
34
-
When a command produces system messages (not sending to the AI), the handler returns `{ postUserMessage }` and the command-registry calls `params.sendMessage({ content, agentMode, postUserMessage })`:
49
+
Commands that scaffold files AND show messages use the `postUserMessage` + `sendMessage` pattern:
35
50
36
51
```typescript
37
-
// In command-registry.ts:
52
+
// In init.ts - the handler returns { postUserMessage }, doesn't call setMessages directly
1. **Is there a constant for this?** Search `common/src/constants/` first
70
-
2. **Is there a utility for this path operation?** Check `cli/src/project-files.ts`
71
-
3. **Does a template file already exist?** Check `common/src/templates/`
72
-
4. **Should I track analytics?** Most user-facing actions should call `trackEvent()`
73
-
5. **What is the naming convention?** Look at 2-3 existing similar handlers (e.g., `handleHelpCommand`, `handleUsageCommand`) before naming your function
119
+
1.**Does a test file already exist?** Check `__tests__/[filename].test.ts` — read it FIRST for expected function name and signature
120
+
2.**Is there a constant for this?** Search `common/src/constants/` first
121
+
3.**Is there a utility for this path operation?** Check `cli/src/project-files.ts`
122
+
4.**Does a template file already exist?** Check `common/src/templates/`
123
+
5.**Should I track analytics?** Most user-facing actions should call `trackEvent()`
124
+
6.**What is the naming convention?** Look at 2-3 existing similar handlers before naming your function
74
125
75
126
## Anti-patterns
76
127
128
+
**DON'T** ignore existing test files that reveal expected exports:
129
+
```typescript
130
+
// BAD - test expects handleInitializationFlowLocally but you created:
0 commit comments