|
| 1 | +--- |
| 2 | +phase: quick-380 |
| 3 | +plan: 01 |
| 4 | +type: execute |
| 5 | +wave: 1 |
| 6 | +depends_on: [] |
| 7 | +files_modified: |
| 8 | + - bin/quorum-slot-dispatch.cjs |
| 9 | + - bin/quorum-slot-dispatch.test.cjs |
| 10 | + - bin/coding-task-router.cjs |
| 11 | + - bin/coding-task-router.test.cjs |
| 12 | +autonomous: true |
| 13 | +requirements: [INTENT-01] |
| 14 | +formal_artifacts: none |
| 15 | + |
| 16 | +must_haves: |
| 17 | + truths: |
| 18 | + - "quorum-slot-dispatch.cjs accepts --mode C and builds a coding delegation prompt" |
| 19 | + - "coding-task-router.cjs routes a task description to a named provider slot and returns structured output" |
| 20 | + - "Mock agent CLI (echo-based) can receive a coding task via stdin and return structured result" |
| 21 | + - "Existing Mode A and Mode B dispatch paths are unaffected by the changes" |
| 22 | + artifacts: |
| 23 | + - path: "bin/quorum-slot-dispatch.cjs" |
| 24 | + provides: "Mode C prompt builder and dispatch path" |
| 25 | + contains: "buildModeCPrompt" |
| 26 | + - path: "bin/coding-task-router.cjs" |
| 27 | + provides: "Coding task router with pluggable slot selection" |
| 28 | + exports: ["routeCodingTask", "buildCodingPrompt", "parseCodingResult"] |
| 29 | + - path: "bin/coding-task-router.test.cjs" |
| 30 | + provides: "Tests for coding task router including mock CLI delegation" |
| 31 | + min_lines: 80 |
| 32 | + key_links: |
| 33 | + - from: "bin/coding-task-router.cjs" |
| 34 | + to: "bin/call-quorum-slot.cjs" |
| 35 | + via: "child_process.spawn for CLI delegation" |
| 36 | + pattern: "call-quorum-slot\\.cjs" |
| 37 | + - from: "bin/quorum-slot-dispatch.cjs" |
| 38 | + to: "bin/coding-task-router.cjs" |
| 39 | + via: "require and mode C branch" |
| 40 | + pattern: "coding-task-router" |
| 41 | + consumers: |
| 42 | + - artifact: "bin/coding-task-router.cjs" |
| 43 | + consumed_by: "bin/quorum-slot-dispatch.cjs" |
| 44 | + integration: "Mode C branch in main() delegates to router" |
| 45 | + verify_pattern: "coding-task-router" |
| 46 | +--- |
| 47 | + |
| 48 | +<objective> |
| 49 | +Add coding task delegation to the quorum slot dispatch system, enabling external agent CLIs |
| 50 | +(codex, gemini, opencode, copilot) to receive and execute coding tasks -- not just review tasks. |
| 51 | + |
| 52 | +Purpose: This is the foundational layer for Issue #60. Currently quorum slots only handle |
| 53 | +review/voting (Mode A: questions, Mode B: execution review). This adds Mode C: coding delegation, |
| 54 | +where a task description is sent to an external CLI agent to perform actual coding work, with |
| 55 | +structured result collection. |
| 56 | + |
| 57 | +Output: New coding-task-router.cjs module, Mode C support in quorum-slot-dispatch.cjs, and tests. |
| 58 | +</objective> |
| 59 | + |
| 60 | +<execution_context> |
| 61 | +@./.claude/nf/workflows/execute-plan.md |
| 62 | +@./.claude/nf/templates/summary.md |
| 63 | +</execution_context> |
| 64 | + |
| 65 | +<context> |
| 66 | +@.planning/STATE.md |
| 67 | +@bin/quorum-slot-dispatch.cjs |
| 68 | +@bin/call-quorum-slot.cjs |
| 69 | +@bin/providers.json |
| 70 | +</context> |
| 71 | + |
| 72 | +<tasks> |
| 73 | + |
| 74 | +<task type="auto"> |
| 75 | + <name>Task 1: Create coding-task-router.cjs with prompt builder, result parser, and routing logic</name> |
| 76 | + <files>bin/coding-task-router.cjs, bin/coding-task-router.test.cjs</files> |
| 77 | + <action> |
| 78 | +Create `bin/coding-task-router.cjs` -- a new module that handles coding task delegation to external agent CLIs. |
| 79 | + |
| 80 | +The module must export these pure functions (testable without subprocess): |
| 81 | + |
| 82 | +1. `buildCodingPrompt({ task, repoDir, files, constraints, context })` -- constructs a coding task prompt: |
| 83 | + - `task` (string): description of what to implement/fix/refactor |
| 84 | + - `repoDir` (string): absolute path to the repository |
| 85 | + - `files` (string[]): files the agent should focus on |
| 86 | + - `constraints` (string[]): optional constraints (e.g., "do not modify tests", "use CommonJS") |
| 87 | + - `context` (string): optional additional context (e.g., error output, prior attempt) |
| 88 | + - Returns a structured prompt string with clear sections: TASK, REPOSITORY, FILES, CONSTRAINTS, CONTEXT |
| 89 | + - Include instruction block telling the agent to output a structured result with: status (SUCCESS|PARTIAL|FAILED), files_modified (list), summary (what was done), diff_preview (optional abbreviated diff) |
| 90 | + |
| 91 | +2. `parseCodingResult(rawOutput)` -- extracts structured result from CLI output: |
| 92 | + - Looks for `status:`, `files_modified:`, `summary:`, `diff_preview:` fields in output |
| 93 | + - Returns `{ status, filesModified, summary, diffPreview, rawOutput }` or null on parse failure |
| 94 | + - Fail-open: if parsing fails, returns `{ status: 'UNKNOWN', filesModified: [], summary: rawOutput.slice(0, 500), diffPreview: null, rawOutput }` |
| 95 | + |
| 96 | +3. `routeCodingTask({ task, slot, repoDir, files, constraints, context, timeout })` -- orchestrates delegation: |
| 97 | + - Builds the coding prompt via buildCodingPrompt() |
| 98 | + - Spawns `call-quorum-slot.cjs --slot <slot> --timeout <timeout> --cwd <repoDir>` with prompt on stdin |
| 99 | + - Parses the raw output via parseCodingResult() |
| 100 | + - Returns `{ slot, status, filesModified, summary, diffPreview, latencyMs, rawOutput }` |
| 101 | + - On subprocess failure: returns `{ slot, status: 'UNAVAIL', error_type: classifyDispatchError(output), ... }` |
| 102 | + |
| 103 | +4. `selectSlot(taskType, providers)` -- simple slot selection (pluggable policy placeholder): |
| 104 | + - For now: returns the first subprocess-type provider with `has_file_access: true` |
| 105 | + - This is the hook point for future Q-learning routing (out of scope for this task) |
| 106 | + - `taskType` is a string like "implement", "fix", "refactor", "test" |
| 107 | + - Returns provider name string or null |
| 108 | + |
| 109 | +Follow existing coding patterns: |
| 110 | +- CommonJS (`'use strict'`, `require`, `module.exports`) |
| 111 | +- Fail-open error handling (try/catch with stderr logging, never crash) |
| 112 | +- Use `classifyDispatchError` from quorum-slot-dispatch.cjs for error classification |
| 113 | +- Use `path.join(__dirname, 'call-quorum-slot.cjs')` for subprocess path resolution |
| 114 | +- CLI entry point guarded by `if (require.main === module)` with arg parsing: |
| 115 | + `node coding-task-router.cjs --slot <name> --task <text> [--files <comma-separated>] [--timeout <ms>] [--cwd <dir>]` |
| 116 | + |
| 117 | +Also create `bin/coding-task-router.test.cjs` with tests using `node:test` and `node:assert`: |
| 118 | +- buildCodingPrompt produces prompt containing all sections (TASK, REPOSITORY, FILES, CONSTRAINTS) |
| 119 | +- buildCodingPrompt with no optional fields still produces valid prompt |
| 120 | +- parseCodingResult extracts status/files_modified/summary from well-formed output |
| 121 | +- parseCodingResult handles malformed output gracefully (fail-open) |
| 122 | +- selectSlot returns first file-access subprocess provider |
| 123 | +- selectSlot returns null when no suitable providers exist |
| 124 | +- Module exports all expected functions |
| 125 | + </action> |
| 126 | + <verify> |
| 127 | +Run `node --test bin/coding-task-router.test.cjs` -- all tests pass. |
| 128 | +Run `node -e "const m = require('./bin/coding-task-router.cjs'); console.log(Object.keys(m))"` -- exports buildCodingPrompt, parseCodingResult, routeCodingTask, selectSlot. |
| 129 | + </verify> |
| 130 | + <done> |
| 131 | +coding-task-router.cjs exports 4 functions, all tests pass, prompt builder produces structured coding prompts, |
| 132 | +result parser handles both well-formed and malformed output, selectSlot picks first file-access subprocess provider. |
| 133 | + </done> |
| 134 | +</task> |
| 135 | + |
| 136 | +<task type="auto"> |
| 137 | + <name>Task 2: Add Mode C coding delegation path to quorum-slot-dispatch.cjs</name> |
| 138 | + <files>bin/quorum-slot-dispatch.cjs, bin/quorum-slot-dispatch.test.cjs</files> |
| 139 | + <action> |
| 140 | +Extend `bin/quorum-slot-dispatch.cjs` to support `--mode C` for coding task delegation. |
| 141 | + |
| 142 | +1. Add `buildModeCPrompt` function (exported for testability): |
| 143 | + - Signature: `buildModeCPrompt({ repoDir, task, files, constraints, context })` |
| 144 | + - Delegates to `require('./coding-task-router.cjs').buildCodingPrompt()` internally |
| 145 | + - This keeps the prompt-building pattern consistent with buildModeAPrompt/buildModeBPrompt |
| 146 | + - Returns the constructed prompt string |
| 147 | + |
| 148 | +2. Add Mode C branch in the `buildPromptForProvider` closure (around line 1519): |
| 149 | + - When `mode === 'C'`, call `buildModeCPrompt({ repoDir, task: question, files: filesArg, constraints: constraintsArg, context: reviewContext })` |
| 150 | + - Add new CLI args: `--files <comma-separated>`, `--constraints <comma-separated>` |
| 151 | + - Parse these in the arg parsing section at top of main() |
| 152 | + |
| 153 | +3. Add Mode C result parsing in the post-dispatch section (around line 1785): |
| 154 | + - When mode is C and not UNAVAIL, use `require('./coding-task-router.cjs').parseCodingResult(output)` |
| 155 | + - Emit result block with coding-specific fields: add `coding_result` to emitResultBlock when mode is C |
| 156 | + - The verdict for Mode C should be derived from parseCodingResult status: SUCCESS -> APPROVE, PARTIAL -> FLAG, FAILED -> REJECT, UNKNOWN -> FLAG |
| 157 | + |
| 158 | +4. Export `buildModeCPrompt` from module.exports (add to the existing exports object at line ~1881) |
| 159 | + |
| 160 | +5. Add tests to `bin/quorum-slot-dispatch.test.cjs`: |
| 161 | + - `buildModeCPrompt` is exported as a function |
| 162 | + - `buildModeCPrompt` produces prompt containing TASK, REPOSITORY sections |
| 163 | + - `buildModeCPrompt` with files array includes FILES section |
| 164 | + - Existing Mode A and Mode B tests continue to pass (regression check) |
| 165 | + |
| 166 | +IMPORTANT: Do NOT modify the existing buildModeAPrompt, buildModeBPrompt, parseVerdict, or any other existing function behavior. Mode C is purely additive -- new code path, no changes to existing paths. The `buildPromptForProvider` closure's existing `if (mode === 'B')` and default (Mode A) branches must remain untouched. |
| 167 | + |
| 168 | +Guard against re-inlining: buildModeCPrompt MUST delegate to coding-task-router.cjs's buildCodingPrompt, not re-implement the prompt construction inline. |
| 169 | + </action> |
| 170 | + <verify> |
| 171 | +Run `node --test bin/quorum-slot-dispatch.test.cjs` -- all existing + new tests pass. |
| 172 | +Run `node -e "const m = require('./bin/quorum-slot-dispatch.cjs'); console.log(typeof m.buildModeCPrompt)"` -- prints "function". |
| 173 | +Run `grep 'coding-task-router' bin/quorum-slot-dispatch.cjs` -- confirms import exists (no re-inlining). |
| 174 | +Run `grep 'buildModeCPrompt' bin/quorum-slot-dispatch.cjs` -- confirms export exists. |
| 175 | + </verify> |
| 176 | + <done> |
| 177 | +quorum-slot-dispatch.cjs accepts --mode C, delegates prompt building to coding-task-router.cjs, |
| 178 | +parses coding results, maps status to verdict, exports buildModeCPrompt. All existing Mode A/B |
| 179 | +tests pass unchanged. New Mode C tests pass. |
| 180 | + </done> |
| 181 | +</task> |
| 182 | + |
| 183 | +<task type="auto"> |
| 184 | + <name>Task 3: Add mock CLI integration test for end-to-end coding delegation</name> |
| 185 | + <files>bin/coding-task-router.test.cjs</files> |
| 186 | + <action> |
| 187 | +Add an integration-level test to `bin/coding-task-router.test.cjs` that validates the full |
| 188 | +coding delegation pipeline using a mock agent CLI. |
| 189 | + |
| 190 | +1. Create an inline mock agent script (written to a temp file during test setup): |
| 191 | + - The mock reads stdin, extracts the TASK section from the prompt |
| 192 | + - Outputs a well-formed coding result: |
| 193 | + ``` |
| 194 | + status: SUCCESS |
| 195 | + files_modified: [src/example.js] |
| 196 | + summary: Implemented the requested feature |
| 197 | + diff_preview: | |
| 198 | + +function newFeature() { return true; } |
| 199 | + ``` |
| 200 | + - The mock script is a simple Node.js script that reads stdin and writes to stdout |
| 201 | +
|
| 202 | +2. Test `routeCodingTask` by: |
| 203 | + - Using `child_process.spawnSync` to run `coding-task-router.cjs --slot mock --task "add a function" --cwd /tmp` |
| 204 | + - BUT since routeCodingTask calls call-quorum-slot.cjs which needs providers.json, instead: |
| 205 | + - Test the pure function pipeline: buildCodingPrompt -> (mock output) -> parseCodingResult |
| 206 | + - Verify the full round-trip: prompt contains task, parsed result has correct status/summary |
| 207 | +
|
| 208 | +3. Test error handling path: |
| 209 | + - parseCodingResult with empty string returns fail-open result |
| 210 | + - parseCodingResult with output containing only "status: FAILED\nsummary: compilation error" returns FAILED status |
| 211 | +
|
| 212 | +4. Clean up temp files in test teardown. |
| 213 | +
|
| 214 | +Note: Full subprocess integration test (actually spawning call-quorum-slot.cjs) is deferred |
| 215 | +since it requires a live provider. The pure-function round-trip test validates the contract. |
| 216 | + </action> |
| 217 | + <verify> |
| 218 | +Run `node --test bin/coding-task-router.test.cjs` -- all tests pass including integration tests. |
| 219 | +Verify test count: `node --test bin/coding-task-router.test.cjs 2>&1 | grep -c 'ok'` shows >= 8 passing tests. |
| 220 | + </verify> |
| 221 | + <done> |
| 222 | +Integration tests validate the full prompt-build -> parse round-trip with mock data. |
| 223 | +Error handling paths tested. All tests pass. The coding delegation contract is verified |
| 224 | +end-to-end at the function level. |
| 225 | + </done> |
| 226 | +</task> |
| 227 | +
|
| 228 | +</tasks> |
| 229 | +
|
| 230 | +<verification> |
| 231 | +1. `node --test bin/coding-task-router.test.cjs` -- all tests pass |
| 232 | +2. `node --test bin/quorum-slot-dispatch.test.cjs` -- all tests pass (existing + new) |
| 233 | +3. `grep 'buildModeCPrompt' bin/quorum-slot-dispatch.cjs` -- export exists |
| 234 | +4. `grep 'coding-task-router' bin/quorum-slot-dispatch.cjs` -- import exists (no re-inlining) |
| 235 | +5. `node -e "const m = require('./bin/coding-task-router.cjs'); console.log(typeof m.routeCodingTask, typeof m.buildCodingPrompt, typeof m.parseCodingResult, typeof m.selectSlot)"` -- all "function" |
| 236 | +6. `npm run test:ci` -- full test suite passes (no regressions) |
| 237 | +</verification> |
| 238 | +
|
| 239 | +<success_criteria> |
| 240 | +- coding-task-router.cjs exists with 4 exported functions (buildCodingPrompt, parseCodingResult, routeCodingTask, selectSlot) |
| 241 | +- quorum-slot-dispatch.cjs supports --mode C and exports buildModeCPrompt |
| 242 | +- Mode C prompt contains structured TASK/REPOSITORY/FILES/CONSTRAINTS sections |
| 243 | +- Mode C result parsing maps SUCCESS->APPROVE, PARTIAL->FLAG, FAILED->REJECT |
| 244 | +- All existing Mode A/B tests pass unchanged (zero regressions) |
| 245 | +- New tests cover prompt construction, result parsing, slot selection, and round-trip integration |
| 246 | +</success_criteria> |
| 247 | +
|
| 248 | +<output> |
| 249 | +After completion, create `.planning/quick/380-delegate-quorum-slot-coding-to-external-/380-SUMMARY.md` |
| 250 | +</output> |
0 commit comments