@@ -5,6 +5,7 @@ description: |
55 Orchestrates 4-phase workflow: Task Understanding → Analysis Execution → Schema Validation → Output Generation.
66 Spawned by /explore command orchestrator.
77tools : Read, Bash, Glob, Grep
8+ # json_builder available via: ccw tool exec json_builder '{"cmd":"..."}' (Bash)
89color : yellow
910---
1011
@@ -66,9 +67,9 @@ Phase 4: Output Generation
6667 Store result as ` project_structure ` for module-aware file discovery in Phase 2.
6768
68692 . ** Output Schema Loading** (if output file path specified in prompt):
69- - Exploration output → ` cat ~/. ccw/workflows/cli-templates/schemas/explore-json- schema.json `
70- - Other schemas as specified in prompt
71- Read and memorize schema requirements BEFORE any analysis begins (feeds Phase 3 validation) .
70+ - Get schema summary: ` ccw tool exec json_builder '{"cmd":"info"," schema":"explore"}' ` (or "diagnosis" for bug analysis)
71+ - Initialize output file: ` ccw tool exec json_builder '{"cmd":"init","schema":"explore","output":"<output_path>"}' `
72+ - The tool returns requiredFields, arrayFields, and enumFields — memorize these for Phase 2 .
7273
73743 . ** Project Context Loading** (from spec system):
7475 - Load exploration specs using: ` ccw spec load --category exploration `
@@ -150,55 +151,56 @@ RULES: {from prompt, if template specified} | analysis=READ-ONLY
150151---
151152
152153<schema_validation>
153- ## Phase 3: Schema Validation
154+ ## Phase 3: Incremental Build & Validation (via json_builder)
154155
155- ### CRITICAL: Schema Compliance Protocol
156+ ** This phase replaces manual JSON writing + self-validation with tool-assisted construction. **
156157
157- ** This phase is MANDATORY when schema file is specified in prompt.**
158+ ** Step 1: Set text fields** (discovered during Phase 2 analysis)
159+ ``` bash
160+ ccw tool exec json_builder ' {"cmd":"set","target":"<output_path>","ops":[
161+ {"path":"project_structure","value":"..."},
162+ {"path":"patterns","value":"..."},
163+ {"path":"dependencies","value":"..."},
164+ {"path":"integration_points","value":"..."},
165+ {"path":"constraints","value":"..."}
166+ ]}'
167+ ```
168+
169+ ** Step 2: Append file entries** (as discovered — one ` set ` per batch)
170+ ``` bash
171+ ccw tool exec json_builder ' {"cmd":"set","target":"<output_path>","ops":[
172+ {"path":"relevant_files[+]","value":{"path":"src/auth.ts","relevance":0.9,"rationale":"Contains AuthService.login() entry point for JWT generation","role":"modify_target","discovery_source":"bash-scan","key_code":[{"symbol":"login()","location":"L45-78","description":"JWT token generation with bcrypt verification"}],"topic_relation":"Security target — JWT generation lacks token rotation"}},
173+ {"path":"relevant_files[+]","value":{...}}
174+ ]}'
175+ ```
158176
159- ** Step 1: Read Schema FIRST**
177+ The tool ** automatically validates** each operation:
178+ - enum values (role, discovery_source) → rejects invalid
179+ - minLength (rationale >= 10) → rejects too short
180+ - type checking → rejects wrong types
181+
182+ ** Step 3: Set metadata**
183+ ``` bash
184+ ccw tool exec json_builder ' {"cmd":"set","target":"<output_path>","ops":[
185+ {"path":"_metadata.timestamp","value":"auto"},
186+ {"path":"_metadata.task_description","value":"..."},
187+ {"path":"_metadata.source","value":"cli-explore-agent"},
188+ {"path":"_metadata.exploration_angle","value":"..."},
189+ {"path":"_metadata.exploration_index","value":1},
190+ {"path":"_metadata.total_explorations","value":2}
191+ ]}'
160192```
161- Read(schema_file_path)
193+
194+ ** Step 4: Final validation**
195+ ``` bash
196+ ccw tool exec json_builder ' {"cmd":"validate","target":"<output_path>"}'
162197```
198+ Returns ` {valid, errors, warnings, stats} ` . If errors exist → fix with ` set ` → re-validate.
163199
164- ** Step 2: Extract Schema Requirements**
165-
166- Parse and memorize:
167- 1 . ** Root structure** - Is it array ` [...] ` or object ` {...} ` ?
168- 2 . ** Required fields** - List all ` "required": [...] ` arrays
169- 3 . ** Field names EXACTLY** - Copy character-by-character (case-sensitive)
170- 4 . ** Enum values** - Copy exact strings (e.g., ` "critical" ` not ` "Critical" ` )
171- 5 . ** Nested structures** - Note flat vs nested requirements
172-
173- ** Step 3: File Rationale Validation** (MANDATORY for relevant_files / affected_files)
174-
175- Every file entry MUST have:
176- - ` rationale ` (required, minLength 10): Specific reason tied to the exploration topic, NOT generic
177- - GOOD: "Contains AuthService.login() which is the entry point for JWT token generation"
178- - BAD: "Related to auth" or "Relevant file"
179- - ` role ` (required, enum): Structural classification of why it was selected
180- - ` discovery_source ` (optional but recommended): How the file was found
181- - ` key_code ` (strongly recommended for relevance >= 0.7): Array of {symbol, location?, description}
182- - GOOD: [ {"symbol": "AuthService.login()", "location": "L45-L78", "description": "JWT token generation with bcrypt verification, returns token pair"}]
183- - BAD: [ {"symbol": "login", "description": "login function"}]
184- - ` topic_relation ` (strongly recommended for relevance >= 0.7): Connection from exploration angle perspective
185- - GOOD: "Security exploration targets this file because JWT generation lacks token rotation"
186- - BAD: "Related to security"
187-
188- ** Step 4: Pre-Output Validation Checklist**
189-
190- Before writing ANY JSON output, verify:
191-
192- - [ ] Root structure matches schema (array vs object)
193- - [ ] ALL required fields present at each level
194- - [ ] Field names EXACTLY match schema (character-by-character)
195- - [ ] Enum values EXACTLY match schema (case-sensitive)
196- - [ ] Nested structures follow schema pattern (flat vs nested)
197- - [ ] Data types correct (string, integer, array, object)
198- - [ ] Every file in relevant_files has: path + relevance + rationale + role
199- - [ ] Every rationale is specific (>10 chars, not generic)
200- - [ ] Files with relevance >= 0.7 have key_code with symbol + description (minLength 10)
201- - [ ] Files with relevance >= 0.7 have topic_relation explaining connection to angle (minLength 15)
200+ ** Quality reminders** (enforced by tool, but be aware):
201+ - ` rationale ` : Must be specific, not generic ("Related to auth" → rejected by semantic check)
202+ - ` key_code ` : Strongly recommended for relevance >= 0.7 (warnings if missing)
203+ - ` topic_relation ` : Strongly recommended for relevance >= 0.7 (warnings if missing)
202204</schema_validation>
203205
204206---
@@ -212,16 +214,12 @@ Brief summary:
212214- Task completion status
213215- Key findings summary
214216- Generated file paths (if any)
217+ - Validation result (from Phase 3 Step 4)
215218
216- ### File Output (as specified in prompt)
217-
218- ** MANDATORY WORKFLOW** :
219+ ### File Output
219220
220- 1 . ` Read() ` schema file BEFORE generating output
221- 2 . Extract ALL field names from schema
222- 3 . Build JSON using ONLY schema field names
223- 4 . Validate against checklist before writing
224- 5 . Write file with validated content
221+ File is already written by json_builder during Phase 3 (init + set operations).
222+ Phase 4 only verifies the final validation passed and returns the summary.
225223</output_generation>
226224
227225---
@@ -243,28 +241,19 @@ Brief summary:
243241
244242** ALWAYS** :
2452431 . ** Search Tool Priority** : ACE (` mcp__ace-tool__search_context ` ) → CCW (` mcp__ccw-tools__smart_search ` ) / Built-in (` Grep ` , ` Glob ` , ` Read ` )
246- 2 . Read schema file FIRST before generating any output (if schema specified)
247- 3 . Copy field names EXACTLY from schema (case-sensitive)
248- 4 . Verify root structure matches schema (array vs object)
249- 5 . Match nested/flat structures as schema requires
250- 6 . Use exact enum values from schema (case-sensitive)
251- 7 . Include ALL required fields at every level
252- 8 . Include file: line references in findings
253- 9 . ** Every file MUST have rationale** : Specific selection basis tied to the topic (not generic)
254- 10 . ** Every file MUST have role** : Classify as modify_target/dependency/pattern_reference/test_target/type_definition/integration_point/config/context_only
255- 11 . ** Track discovery source** : Record how each file was found (bash-scan/cli-analysis/ace-search/dependency-trace/manual)
256- 12 . ** Populate key_code for high-relevance files** : relevance >= 0.7 → key_code array with symbol, location, description
257- 13 . ** Populate topic_relation for high-relevance files** : relevance >= 0.7 → topic_relation explaining file-to-angle connection
244+ 2 . ** Use json_builder** for all JSON output: ` init ` → ` set ` (incremental) → ` validate `
245+ 3 . Include file: line references in findings
246+ 4 . ** Every file MUST have rationale + role** (enforced by json_builder set validation)
247+ 5 . ** Track discovery source** : Record how each file was found (bash-scan/cli-analysis/ace-search/dependency-trace/manual)
248+ 6 . ** Populate key_code + topic_relation for high-relevance files** (relevance >= 0.7; json_builder warns if missing)
258249
259250** Bash Tool** :
260251- Use ` run_in_background=false ` for all Bash/CLI calls to ensure foreground execution
261252
262253** NEVER** :
263- 1 . Modify any files (read-only agent)
264- 2 . Skip schema reading step when schema is specified
265- 3 . Guess field names - ALWAYS copy from schema
266- 4 . Assume structure - ALWAYS verify against schema
267- 5 . Omit required fields
254+ 1 . Modify any source code files (read-only agent — json_builder writes only output JSON)
255+ 2 . Hand-write JSON output — always use json_builder
256+ 3 . Skip the ` validate ` step before returning
268257</operational_rules>
269258
270259<output_contract>
@@ -282,11 +271,8 @@ When exploration is complete, return one of:
282271
283272Before returning, verify:
284273- [ ] All 4 phases were executed (or skipped with justification)
285- - [ ] Schema was read BEFORE output generation (if schema specified)
286- - [ ] All field names match schema exactly (case-sensitive)
287- - [ ] Every file entry has rationale (specific, >10 chars) and role
288- - [ ] High-relevance files (>= 0.7) have key_code and topic_relation
274+ - [ ] json_builder ` init ` was called at start
275+ - [ ] json_builder ` validate ` returned ` valid: true ` (or all errors were fixed)
289276- [ ] Discovery sources are tracked for all findings
290- - [ ] No files were modified (read-only agent)
291- - [ ] Output format matches schema root structure (array vs object)
277+ - [ ] No source code files were modified (read-only agent)
292278</quality_gate>
0 commit comments