ace-swarm 2.1.0 → 2.1.2

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.
@@ -1,543 +1,65 @@
1
1
  ---
2
2
  name: codemunch
3
- description:
4
- Holistic AST-driven codebase comprehension. Uses ast-grep to surgically extract structural knowledge into lightweight artifacts, enabling deep understanding of any foreign codebase without reading raw files. Operates as a capability-research module inside the ACE Framework.
3
+ description:
4
+ Map an unfamiliar codebase into durable structural artifacts with ast-grep. Use when you need to understand, debug, contribute to, or borrow patterns from a codebase without broad raw-file reading.
5
5
  ---
6
6
 
7
- # CodeMunch — AST-Driven Codebase Intelligence
7
+ # CodeMunch
8
8
 
9
- **IMPORTANT**: NEVER read raw source files to understand a codebase. Every structural fact MUST
10
- be extracted via an `ast-grep` query. Raw file reads burn 10–100× more tokens than equivalent
11
- AST queries. The only exception is a single-file glance at an entry point to confirm language
12
- and project purpose. The goal is to **produce artifacts, not chat**. If a CodeMunch session
13
- ends without writing at least `CODEBASE_MAP.md`, it has failed.
9
+ Use this skill to turn an unfamiliar codebase into a compact set of structural artifacts that downstream agents can trust.
14
10
 
15
- ---
16
-
17
- ## 0. ACE Role Declaration
18
-
19
- CodeMunch is a `capability-research` module. In every session it:
20
-
21
- - **Emits** a `SOURCE_VALIDATED` event when a structural claim is confirmed by ast-grep output.
22
- - **Writes** findings to `./agent-state/` as append-only artifacts.
23
- - **Blocks** downstream modules (`capability-spec`, `capability-build`) from acting on unverifiable structural assumptions.
24
- - **Hands off** via `HANDOFF.json` once `CODEBASE_MAP.md` and `PATTERNS.md` are locked.
25
-
26
- ACE Clarity Protocol headers (`[STATE_ANALYSIS]`, `[STRATEGY_SELECTOR]`, `[EXECUTION_LOG]`,
27
- `[ARTIFACT_UPDATE]`, `[VERIFICATION]`) must be used at each phase boundary in your response.
28
-
29
- ---
30
-
31
- ## 0.1 MICE Boundaries
32
-
33
- | Rule | Enforcement |
34
- |---|---|
35
- | **Modular** | Extracts structural knowledge only; does NOT modify code, write specs, or make build decisions. |
36
- | **Interoperable** | All findings written as standardized artifacts (`CODEBASE_MAP.md`, `PATTERNS.md`, `EVIDENCE_LOG.md`). |
37
- | **Customizable** | Goal parameter (`understand/debug/contribute/inspire`) controls depth and focus of analysis. |
38
- | **Extensible** | New language patterns added via ast-grep pattern sets, not prose guessing. |
39
-
40
- ## 0.2 Goal Orientation
41
-
42
- - `DELETE_PROTOCOL`: Skip Socratic questions already answered by prior runs. No redundant queries.
43
- - `ARTIFACT_PROTOCOL`: Session MUST produce `CODEBASE_MAP.md` minimum. No artifact = failed session.
44
- - `AGENCY_PROTOCOL`: Infer next ast-grep target from unanswered Socratic questions. Never ask "what should I query?"
45
-
46
-
47
- ---
48
-
49
- ## 1. Socratic Question Stack
50
-
51
- Before running a single command, answer these questions in order. Stop at the first one you cannot answer — that is your first ast-grep target.
52
-
53
- **Tier 0 — Orientation (answer from README or a 30-second scan)**
54
- 1. What is the primary language and build system?
55
- 2. What is the stated purpose of this codebase in one sentence?
56
- 3. Where is the entry point (`main`, `__init__`, `index`, `lib.rs`, etc.)?
57
-
58
- **Tier 1 — Skeleton (answer via ast-grep)**
59
- 4. What are the top-level public types / classes / structs / enums?
60
- 5. What are the primary exported functions or public API symbols?
61
- 6. How many modules/crates/packages exist, and how are they named?
62
- 7. What external dependencies are imported at the top level?
63
-
64
- **Tier 2 — Relationships (answer via ast-grep)**
65
- 8. Which modules call which other modules? (call-site mapping)
66
- 9. Where does data enter the system and where does it leave?
67
- 10. What trait / interface / protocol implementations exist?
68
- 11. Where is error handling centralised? What error types exist?
69
-
70
- **Tier 3 — Patterns (answer via ast-grep)**
71
- 12. What repeated structural idioms appear 3+ times? (factory, builder, strategy, etc.)
72
- 13. What test coverage patterns exist? (unit, integration, property-based?)
73
- 14. Are there any TODO/FIXME/HACK/unsafe markers that signal risk areas?
74
- 15. What configuration / deserialisation surface exists?
75
-
76
- **Tier 4 — Goal Alignment (answer from the task)**
77
- 16. What is the specific goal? (understand / debug / add feature / contribute / get inspired)
78
- 17. Which modules are in the critical path for this goal?
79
- 18. What is the simplest change that would advance this goal?
80
-
81
- > **Rule**: Each unanswered question maps to exactly one ast-grep command in Phase 2.
82
- > Write the question → answer pairs into `SOCRATIC_LOG.md` as you go.
83
-
84
- ---
85
-
86
- ## 2. Execution Protocol
87
-
88
- ### Phase 0 — Bootstrap `[STATE_ANALYSIS]`
89
-
90
- Detect the language, structure, and project skeleton without reading any source files.
91
-
92
- **Step 1 — Language detection**
93
- ```bash
94
- # Count files by extension to confirm language mix
95
- find . -type f | sed 's/.*\.//' | sort | uniq -c | sort -rn | head -20
96
-
97
- # Confirm primary language for ast-grep --lang flag
98
- # Common values: rust, python, js, ts, tsx, go, java, cpp, c, cs, ruby
99
- ```
100
-
101
- **Step 2 — Entry point identification**
102
- ```bash
103
- # Rust
104
- ast-grep --pattern 'fn main() { $$$ }' --lang rust --json
11
+ ## Purpose
105
12
 
106
- # Python
107
- ast-grep --pattern 'if __name__ == "__main__": $$$' --lang python --json
13
+ Extract architecture, public surface, relationships, and recurring patterns through AST-backed evidence so future work starts from a written map instead of ad-hoc memory.
108
14
 
109
- # JS/TS
110
- ast-grep --pattern 'export default $EXPR' --lang ts --json
111
- ast-grep --pattern 'module.exports = $EXPR' --lang js --json
112
- ```
15
+ ## Canonical Use Cases
113
16
 
114
- **Step 3 Top-level module map**
115
- ```bash
116
- # Count lines and files per directory (structural overview, no source reading)
117
- find . -name "*.rs" | head -60
118
- find . -name "*.py" | head -60
119
- ```
17
+ 1. A team needs a codebase map before debugging, feature work, or onboarding into an unfamiliar repository.
18
+ 2. A contributor wants to identify the minimum insertion point for a change without reading large amounts of raw code first.
19
+ 3. An engineer wants reusable patterns, conventions, and hotspots distilled into durable artifacts for later handoff.
120
20
 
121
- **Output**: Write `CODEBASE_MAP.md` stub:
122
- ```markdown
123
- # CODEBASE_MAP.md
124
- Language: <lang>
125
- Entry Point: <path>
126
- Build System: <cargo/pip/npm/make>
127
- Module Count: <n>
128
- Purpose: <one sentence from README>
129
- ```
21
+ ## Inputs
130
22
 
131
- ---
132
-
133
- ### Phase 1 Skeleton Extraction `[STRATEGY_SELECTOR]`
134
-
135
- Extract the full public API surface using targeted ast-grep patterns. Do NOT read
136
- implementations — extract signatures only.
137
-
138
- **Step 4 — Public types**
139
- ```bash
140
- # Rust: structs, enums, traits
141
- ast-grep --pattern 'pub struct $NAME { $$$ }' --lang rust
142
- ast-grep --pattern 'pub enum $NAME { $$$ }' --lang rust
143
- ast-grep --pattern 'pub trait $NAME { $$$ }' --lang rust
144
-
145
- # Python: classes
146
- ast-grep --pattern 'class $NAME: $$$' --lang python
147
-
148
- # TypeScript: interfaces, types, classes
149
- ast-grep --pattern 'export interface $NAME { $$$ }' --lang ts
150
- ast-grep --pattern 'export type $NAME = $TYPE' --lang ts
151
- ast-grep --pattern 'export class $NAME { $$$ }' --lang ts
152
- ```
153
-
154
- **Step 5 — Public functions / methods**
155
- ```bash
156
- # Rust: public fns (with --json for machine-parseable output)
157
- ast-grep --pattern 'pub fn $NAME($$$) -> $RET { $$$ }' --lang rust --json
158
-
159
- # Rust: public fns (no return type)
160
- ast-grep --pattern 'pub fn $NAME($$$) { $$$ }' --lang rust
161
-
162
- # Python: top-level defs
163
- ast-grep --pattern 'def $NAME($$$): $$$' --lang python
164
-
165
- # TypeScript: exported functions
166
- ast-grep --pattern 'export function $NAME($$$): $RET { $$$ }' --lang ts
167
- ast-grep --pattern 'export const $NAME = ($$$) => $BODY' --lang ts
168
- ```
169
-
170
- **Step 6 — External dependencies**
171
- ```bash
172
- # Rust: use statements (top-level imports)
173
- ast-grep --pattern 'use $PATH' --lang rust
174
-
175
- # Python: imports
176
- ast-grep --pattern 'import $MODULE' --lang python
177
- ast-grep --pattern 'from $MODULE import $NAME' --lang python
178
-
179
- # JS/TS: imports
180
- ast-grep --pattern 'import $NAME from "$PATH"' --lang ts
181
- ast-grep --pattern 'import { $$$ } from "$PATH"' --lang ts
182
- ```
183
-
184
- **Output**: Append to `CODEBASE_MAP.md`:
185
- ```markdown
186
- ## Public API Surface
187
- ### Types
188
- - `TypeName` — <kind: struct/enum/class> — <file:line>
189
-
190
- ### Functions
191
- - `fn_name(args) -> ret` — <file:line>
23
+ - Target codebase path or repository checkout
24
+ - Goal or intent such as `understand`, `debug`, `contribute`, or `inspire`
25
+ - Project orientation artifacts like README, manifests, and existing `agent-state/` outputs when available
26
+ - ast-grep plus any language-specific patterns needed for the active language mix
192
27
 
193
- ### Key Dependencies
194
- - <dep_name>: <apparent purpose>
195
- ```
28
+ ## Workflow
196
29
 
197
- ---
198
-
199
- ### Phase 2 Relationship Mapping `[EXECUTION_LOG]`
200
-
201
- Find how components connect. This answers Socratic questions 8–11.
202
-
203
- **Step 7 — Trait/interface implementations**
204
- ```bash
205
- # Rust: impl blocks (what implements what)
206
- ast-grep --pattern 'impl $TRAIT for $TYPE { $$$ }' --lang rust
207
-
208
- # Rust: plain impl blocks
209
- ast-grep --pattern 'impl $TYPE { $$$ }' --lang rust
210
- ```
211
-
212
- **Step 8 — Call-site mapping (who calls what)**
213
- ```bash
214
- # Find all calls to a specific function (replace TARGET with function name)
215
- ast-grep --pattern '$OBJ.TARGET($$$)' --lang rust
216
- ast-grep --pattern 'TARGET($$$)' --lang rust
217
-
218
- # Find all method calls on a type
219
- ast-grep --pattern '$VAR: $TYPE' --lang rust # find variable declarations of a type
220
- ```
221
-
222
- **Step 9 — Error handling topology**
223
- ```bash
224
- # Rust: Result types and ? operator usage
225
- ast-grep --pattern 'Result<$T, $E>' --lang rust
226
- ast-grep --pattern '$EXPR?' --lang rust
30
+ 1. Bootstrap language, build-system, and entry-point understanding with minimal orientation reads.
31
+ 2. Answer the Socratic question stack with ast-grep-backed evidence.
32
+ 3. Write the core structural artifacts: `CODEBASE_MAP.md`, `DEPENDENCY_GRAPH.md`, `PATTERNS.md`, `SOCRATIC_LOG.md`, and `EVIDENCE_LOG.md`.
33
+ 4. Produce a goal-specific synthesis artifact such as a contribution guide or bug hypothesis.
34
+ 5. Hand off from the written artifacts rather than from unwritten session memory.
227
35
 
228
- # Rust: custom error enums
229
- ast-grep --pattern 'pub enum $NAME { $$$ }' --lang rust # filter for Error suffix
36
+ ## Outputs
230
37
 
231
- # Python: exception definitions
232
- ast-grep --pattern 'class $NAME(Exception): $$$' --lang python
233
- ast-grep --pattern 'except $EXC: $$$' --lang python
234
- ```
38
+ - `agent-state/CODEBASE_MAP.md`
39
+ - `agent-state/DEPENDENCY_GRAPH.md`
40
+ - `agent-state/PATTERNS.md`
41
+ - `agent-state/SOCRATIC_LOG.md`
42
+ - `agent-state/EVIDENCE_LOG.md`
43
+ - Optional goal-specific artifacts such as `CONTRIBUTION_GUIDE.md` or `BUG_HYPOTHESIS.md`
235
44
 
236
- **Step 10 — Configuration and deserialization surface**
237
- ```bash
238
- # Rust: serde derives (anything Deserialize is config/data entry)
239
- ast-grep --pattern '#[derive($$$Deserialize$$$)] $STRUCT' --lang rust
45
+ ## Validation
240
46
 
241
- # Python: dataclasses, pydantic models
242
- ast-grep --pattern '@dataclass' --lang python
243
- ast-grep --pattern 'class $NAME(BaseModel): $$$' --lang python
244
-
245
- # TypeScript: zod schemas, JSON parse points
246
- ast-grep --pattern 'z.object({ $$$ })' --lang ts
247
- ```
248
-
249
- **Output**: Write `DEPENDENCY_GRAPH.md`:
250
- ```markdown
251
- # DEPENDENCY_GRAPH.md
252
- ## Module Relationships
253
- - `module_a` → calls → `module_b` (via fn_name)
254
-
255
- ## Trait Implementations
256
- - `TypeX` implements `TraitY`
257
-
258
- ## Error Taxonomy
259
- - `ErrorVariant`: exits at <file:line>
260
-
261
- ## Config / Data Entry Points
262
- - `StructName` (Deserialize): loaded at <file>
263
- ```
264
-
265
- ---
47
+ - Verify the core artifact set is written before any final synthesis or handoff occurs.
48
+ - Verify every structural claim in the synthesis can be traced back to an ast-grep-backed entry in `EVIDENCE_LOG.md`.
49
+ - Verify unanswered questions are recorded explicitly as known unknowns rather than guessed.
266
50
 
267
- ### Phase 3 — Pattern & Idiom Mining `[EXECUTION_LOG]`
51
+ ## References
268
52
 
269
- Identify recurring structural decisions. These reveal the codebase's philosophy.
270
-
271
- **Step 11 — Test patterns**
272
- ```bash
273
- # Rust: test functions
274
- ast-grep --pattern '#[test] fn $NAME() { $$$ }' --lang rust
275
- ast-grep --pattern '#[tokio::test] async fn $NAME() { $$$ }' --lang rust
276
-
277
- # Python: pytest / unittest
278
- ast-grep --pattern 'def test_$NAME($$$): $$$' --lang python
279
- ast-grep --pattern 'class $NAME(TestCase): $$$' --lang python
280
-
281
- # JS/TS: jest / vitest
282
- ast-grep --pattern 'it("$DESC", $$$)' --lang ts
283
- ast-grep --pattern 'describe("$DESC", $$$)' --lang ts
284
- ```
285
-
286
- **Step 12 — Unsafe / risk markers**
287
- ```bash
288
- # Rust: unsafe blocks (security/correctness hotspots)
289
- ast-grep --pattern 'unsafe { $$$ }' --lang rust
290
- ast-grep --pattern 'unsafe fn $NAME($$$)' --lang rust
291
-
292
- # TODO/FIXME markers (language-agnostic comment scan)
293
- grep -rn "TODO\|FIXME\|HACK\|SAFETY\|PANIC\|unwrap()" --include="*.rs" | head -40
294
- grep -rn "TODO\|FIXME\|HACK\|BUG" --include="*.py" | head -40
295
- ```
296
-
297
- **Step 13 — Builder / factory / newtype patterns**
298
- ```bash
299
- # Rust: builder pattern (fn new / fn build)
300
- ast-grep --pattern 'fn new($$$) -> Self { $$$ }' --lang rust
301
- ast-grep --pattern 'fn build(self) -> $TYPE { $$$ }' --lang rust
302
-
303
- # Rust: newtype wrappers
304
- ast-grep --pattern 'pub struct $NAME($INNER);' --lang rust
305
-
306
- # Python: class methods / factories
307
- ast-grep --pattern '@classmethod' --lang python
308
- ```
309
-
310
- **Step 14 — Async / concurrency patterns**
311
- ```bash
312
- # Rust: async functions
313
- ast-grep --pattern 'async fn $NAME($$$)' --lang rust
314
- ast-grep --pattern 'tokio::spawn($$$)' --lang rust
315
-
316
- # Python: async
317
- ast-grep --pattern 'async def $NAME($$$): $$$' --lang python
318
-
319
- # JS/TS
320
- ast-grep --pattern 'await $EXPR' --lang ts
321
- ```
322
-
323
- **Output**: Append to `PATTERNS.md`:
324
- ```markdown
325
- # PATTERNS.md
326
- ## Design Patterns Detected
327
- - Builder: <count> usages, primary in <module>
328
- - Newtype: <count> usages
329
-
330
- ## Test Coverage Profile
331
- - Unit tests: <count>
332
- - Integration tests: <count>
333
- - Async tests: <count>
334
-
335
- ## Risk Markers
336
- - unsafe blocks: <count> in <files>
337
- - TODOs: <list of top ones>
338
-
339
- ## Concurrency Model
340
- - <async/sync/threaded/actor> — evidence: <fn names>
341
- ```
342
-
343
- ---
344
-
345
- ### Phase 4 — Artifact Generation `[ARTIFACT_UPDATE]`
346
-
347
- All findings are now crystallised into locked artifacts. No reasoning from memory — only
348
- from the written artifacts below.
349
-
350
- **Required artifacts** (write all before Phase 5):
351
-
352
- | Artifact | Purpose |
353
- |---|---|
354
- | `CODEBASE_MAP.md` | Language, entry points, module list, public API surface |
355
- | `DEPENDENCY_GRAPH.md` | Module relationships, trait impls, error taxonomy |
356
- | `PATTERNS.md` | Design patterns, test profile, risk markers, concurrency model |
357
- | `SOCRATIC_LOG.md` | Every question + its ast-grep-verified answer |
358
- | `EVIDENCE_LOG.md` | Raw ast-grep output snippets used as evidence (append-only) |
359
-
360
- **Artifact write protocol:**
361
- ```bash
362
- # Each ast-grep query output MUST be appended to EVIDENCE_LOG.md immediately
363
- echo "## Query: pub fn signatures (rust)" >> ./agent-state/EVIDENCE_LOG.md
364
- ast-grep --pattern 'pub fn $NAME($$$) -> $RET { $$$ }' --lang rust --json >> ./agent-state/EVIDENCE_LOG.md
365
- ```
366
-
367
- **Optional artifacts** (generate when goal demands it):
368
-
369
- | Artifact | When to generate |
370
- |---|---|
371
- | `CALL_GRAPH.md` | Goal = debug / trace data flow |
372
- | `CONTRIBUTION_GUIDE.md` | Goal = contribute / extend |
373
- | `INSPIRATION_NOTES.md` | Goal = extract ideas for new project |
374
- | `BUG_HYPOTHESIS.md` | Goal = debug specific issue |
375
-
376
- ---
377
-
378
- ### Phase 5 — Synthesis Pass `[VERIFICATION]`
379
-
380
- **ONLY after all Phase 4 artifacts are written**, perform a final reasoning pass.
381
- Read ONLY the artifacts — not any source files.
382
-
383
- **Step 15 — Self-verification checklist**
384
- - [ ] Can I describe the codebase architecture in 3 sentences from `CODEBASE_MAP.md` alone?
385
- - [ ] Can I name the 5 most important types/functions without looking at source?
386
- - [ ] Can I explain the data flow from entry to exit from `DEPENDENCY_GRAPH.md`?
387
- - [ ] Have all Tier 2 Socratic questions been answered in `SOCRATIC_LOG.md`?
388
- - [ ] Does `EVIDENCE_LOG.md` have at least one ast-grep snippet per structural claim?
389
-
390
- **Step 16 — Goal-aligned synthesis**
391
-
392
- *Mode: UNDERSTAND*
393
- > Synthesise `CODEBASE_MAP.md` + `PATTERNS.md` into a plain-English architecture document.
394
-
395
- *Mode: DEBUG*
396
- > Trace the bug-relevant call path using `CALL_GRAPH.md`. Write `BUG_HYPOTHESIS.md`
397
- > with: symptom → suspected module → ast-grep evidence → falsification criteria.
398
-
399
- *Mode: CONTRIBUTE / ADD FEATURE*
400
- > From `DEPENDENCY_GRAPH.md`, identify the minimum insertion point. Write
401
- > `CONTRIBUTION_GUIDE.md` with: interface contract, which existing tests to extend,
402
- > which patterns to follow (from `PATTERNS.md`).
403
-
404
- *Mode: INSPIRE*
405
- > Extract architectural decisions, naming conventions, idioms from `PATTERNS.md` into
406
- > `INSPIRATION_NOTES.md` with annotations on what can be adapted.
407
-
408
- **Step 17 — HANDOFF**
409
-
410
- Emit `HANDOFF.json` when synthesis is complete:
411
- ```json
412
- {
413
- "meta": { "timestamp": "<ISO8601>", "skill": "codemunch", "session_id": "<uuid>" },
414
- "transition": {
415
- "from": "capability-research",
416
- "to": "capability-spec",
417
- "status": "READY_FOR_HANDOFF",
418
- "reason": "Codebase map and pattern artifacts are locked."
419
- },
420
- "payload": {
421
- "primary_artifact": "./agent-state/CODEBASE_MAP.md",
422
- "supporting_artifacts": [
423
- "./agent-state/DEPENDENCY_GRAPH.md",
424
- "./agent-state/PATTERNS.md",
425
- "./agent-state/SOCRATIC_LOG.md"
426
- ],
427
- "evidence_pointer": "./agent-state/EVIDENCE_LOG.md",
428
- "known_unknowns": ["<list any unanswered Tier 3/4 questions>"]
429
- }
430
- }
431
- ```
432
-
433
- ---
434
-
435
- ## 3. ast-grep Quick Reference
436
-
437
- ### Universal Flags
438
- ```bash
439
- --lang <lang> # rust | python | js | ts | tsx | go | java | cpp | c | html | css
440
- --pattern / -p # inline pattern
441
- --rule <file.yml> # YAML rule file for complex patterns
442
- --json / -j # machine-readable JSON output (use for artifact writing)
443
- --no-ignore # scan files ignored by .gitignore
444
- --color never # disable ANSI in output (use when piping to artifact files)
445
- ```
446
-
447
- ### Meta-variable Syntax
448
- ```
449
- $NAME — single AST node (identifier, expression, etc.)
450
- $$$ — zero or more AST nodes (variadic / body capture)
451
- $$ARGS — named multi-match capture
452
- $_ — anonymous wildcard (matches anything, not captured)
453
- ```
454
-
455
- ### YAML Rule Template (complex structural queries)
456
- ```yaml
457
- # Save as ./codemunch-rules/<rule-name>.yml
458
- id: find-pattern-name
459
- language: rust
460
- rule:
461
- pattern: pub fn $NAME($$$) -> Result<$T, $E> { $$$ }
462
- # Optional filters:
463
- inside:
464
- pattern: impl $TRAIT for $TYPE { $$$ }
465
- has:
466
- pattern: $EXPR? # contains at least one ? operator
467
- not:
468
- pattern: todo!() # exclude unimplemented stubs
469
- ```
470
- Run: `ast-grep scan --rule ./codemunch-rules/<rule-name>.yml`
471
-
472
- ### Language Cheat Sheet
473
-
474
- **Rust**
475
- ```bash
476
- ast-grep -p 'pub fn $N($$$)' -l rust # public functions
477
- ast-grep -p 'impl $T for $U { $$$ }' -l rust # trait impls
478
- ast-grep -p '#[derive($$$)]' -l rust # all derives
479
- ast-grep -p 'Err($E)' -l rust # error emission sites
480
- ast-grep -p '$V.unwrap()' -l rust # panic hotspots
481
- ```
482
-
483
- **Python**
484
- ```bash
485
- ast-grep -p 'def $N($$$) -> $R: $$$' -l python # typed functions
486
- ast-grep -p '@$DECORATOR' -l python # all decorators
487
- ast-grep -p 'raise $E' -l python # exception sites
488
- ast-grep -p '__all__ = [$$$]' -l python # public exports
489
- ```
490
-
491
- **TypeScript / JavaScript**
492
- ```bash
493
- ast-grep -p 'export { $$$ }' -l ts # barrel exports
494
- ast-grep -p 'const $N = useCallback($$$)' -l tsx # React hooks
495
- ast-grep -p 'throw new $E($$$)' -l ts # throw sites
496
- ast-grep -p 'satisfies $TYPE' -l ts # type constraints
497
- ```
498
-
499
- ---
500
-
501
- ## 3.1 Wrong-Stuff Protocol
502
-
503
- When ast-grep analysis reveals structural issues:
504
-
505
- | Finding | Classification | Route To |
506
- |---|---|---|
507
- | Code contradicts spec claims | `spec_violation` | `agent-spec` |
508
- | Unsafe/risky patterns detected | `implementation_bug` | `agent-builder` |
509
- | Evidence source unreliable | `invalid_source` | `agent-research` (self-correct) |
510
- | Codebase documentation stale | `documentation_drift` | `agent-docs` |
511
-
512
- ## 3.2 Anti-Patterns
513
-
514
- | Anti-Pattern | Correct Behavior |
515
- |---|---|
516
- | Reading raw source files | Use ast-grep queries exclusively |
517
- | Guessing structure without AST evidence | Every claim backed by ast-grep output |
518
- | Session ends without artifacts | Minimum output: `CODEBASE_MAP.md` |
519
- | Querying all languages blindly | Detect primary language first (Phase 0), then query |
520
- | Capturing > 500 lines of raw output | Summarize into bullet points; keep exemplars only |
521
-
522
- ---
523
-
524
- ## 4. Failure Modes & Circuit Breakers
525
-
526
- | Failure | Signal | Recovery |
527
- |---|---|---|
528
- | `ast-grep` returns no matches | Pattern may be wrong for this dialect | Use `--json` + inspect one sample node kind first |
529
- | Artifact file > 500 lines | Too much raw output captured | Summarise into bullet points; keep only exemplars in EVIDENCE_LOG |
530
- | Socratic question unresolvable via AST | Deep runtime behaviour | Mark as `KNOWN_UNKNOWN` in SOCRATIC_LOG; do NOT guess |
531
- | Goal changes mid-session | New task injected | Emit `GATE_FAILED`, re-enter Phase 0 with updated goal |
532
-
533
- ---
53
+ - `references/ast-driven-protocol.md` preserves the detailed Socratic stack, phase-by-phase command recipes, and advanced ast-grep cookbook.
534
54
 
535
- ## 5. Activation
55
+ ## Compatibility
536
56
 
537
- When you receive a codebase (path, repo, or CXML dump) with a goal, type:
57
+ - `SKILL.md` is the canonical portable contract for this skill.
58
+ - The long-form playbook lives under `references/` for progressive disclosure and must not redefine the top-level contract.
59
+ - The workflow should remain useful in any environment that can run ast-grep and basic shell discovery commands.
538
60
 
539
- **`munch <path> --goal <understand|debug|contribute|inspire> [--lang <lang>]`**
61
+ ## Failure Policy
540
62
 
541
- CodeMunch will automatically enter Phase 0, execute the Socratic Question Stack, run
542
- ast-grep queries in order, and produce the Phase 4 artifacts before delivering the
543
- Phase 5 synthesis. No raw file reading. No token waste. Only structure.
63
+ - If ast-grep is unavailable or the language cannot be queried safely, stop and surface the limitation instead of pretending the structure is understood.
64
+ - If required artifacts were not written, treat the session as incomplete rather than summarizing from memory.
65
+ - Do not read large swaths of raw source just to compensate for missing structural evidence; mark the unknown and route accordingly.