|
| 1 | +--- |
| 2 | +status: in-progress |
| 3 | +created: '2025-12-18' |
| 4 | +tags: |
| 5 | + - cli |
| 6 | + - mcp |
| 7 | + - validation |
| 8 | + - ai-agents |
| 9 | + - quality |
| 10 | + - workflow |
| 11 | +priority: high |
| 12 | +created_at: '2025-12-18T02:47:39.011Z' |
| 13 | +updated_at: '2025-12-18T02:52:58.301Z' |
| 14 | +depends_on: |
| 15 | + - 018-spec-validation |
| 16 | + - 170-cli-mcp-core-rust-migration-evaluation |
| 17 | +transitions: |
| 18 | + - status: in-progress |
| 19 | + at: '2025-12-18T02:51:49.673Z' |
| 20 | +--- |
| 21 | + |
| 22 | +# Completion Status Verification Hook |
| 23 | + |
| 24 | +> **Status**: ⏳ In progress · **Priority**: High · **Created**: 2025-12-18 · **Tags**: cli, mcp, validation, ai-agents, quality, workflow |
| 25 | +
|
| 26 | +## Overview |
| 27 | + |
| 28 | +### Problem |
| 29 | + |
| 30 | +When AI agents complete work on a spec, they often update `status: complete` prematurely without verifying all tasks are done. This leads to: |
| 31 | + |
| 32 | +1. **Incomplete implementations** - Spec marked complete but TODOs remain in README checkboxes |
| 33 | +2. **Workflow friction** - Humans must manually verify completion vs. spec requirements |
| 34 | +3. **Quality gaps** - No automated enforcement to ensure spec fulfillment before completion |
| 35 | +4. **Lost context** - Outstanding items discovered later when context is lost |
| 36 | + |
| 37 | +### Solution |
| 38 | + |
| 39 | +Add a **verification checkpoint** in the `update` command/tool (CLI & MCP) that: |
| 40 | +- Triggers when `status` changes to `complete` |
| 41 | +- Parses spec README for unchecked checkboxes (`- [ ]`) |
| 42 | +- Provides actionable feedback to AI agents about outstanding items |
| 43 | +- Allows agents to self-correct before marking complete |
| 44 | + |
| 45 | +This creates a **feedback loop** where agents learn to verify their work before declaring completion. |
| 46 | + |
| 47 | +## Design |
| 48 | + |
| 49 | +### Architecture |
| 50 | + |
| 51 | +``` |
| 52 | +┌─────────────────────────────────────────────────┐ |
| 53 | +│ AI Agent: lean-spec update 174 --status complete│ |
| 54 | +└────────────────┬────────────────────────────────┘ |
| 55 | + │ |
| 56 | + ▼ |
| 57 | +┌─────────────────────────────────────────────────┐ |
| 58 | +│ Update Command/Tool │ |
| 59 | +│ 1. Detect status change to "complete" │ |
| 60 | +│ 2. Read spec README.md │ |
| 61 | +│ 3. Parse checkbox items (- [ ] / - [x]) │ |
| 62 | +│ 4. Check for unchecked items │ |
| 63 | +└────────────────┬────────────────────────────────┘ |
| 64 | + │ |
| 65 | + ┌────────┴────────┐ |
| 66 | + │ │ |
| 67 | + ▼ ▼ |
| 68 | +┌──────────────┐ ┌─────────────────┐ |
| 69 | +│ All checked? │ │ Has unchecked? │ |
| 70 | +│ ✓ Success │ │ ⚠ Warning │ |
| 71 | +└──────┬───────┘ └─────────┬───────┘ |
| 72 | + │ │ |
| 73 | + ▼ ▼ |
| 74 | + Update status Return detailed info: |
| 75 | + to complete - Outstanding count |
| 76 | + - Task locations |
| 77 | + - Suggested actions |
| 78 | + - Option to proceed anyway |
| 79 | +``` |
| 80 | + |
| 81 | +### Detection Logic |
| 82 | + |
| 83 | +**When to trigger:** |
| 84 | +- Status transition: `planned|in-progress → complete` |
| 85 | +- Command: `lean-spec update <spec> --status complete` |
| 86 | +- MCP tool: `update` with status field change |
| 87 | + |
| 88 | +**What to check:** |
| 89 | +```typescript |
| 90 | +function verifyCompletion(specPath: string): VerificationResult { |
| 91 | + const content = readSpecContent(specPath); |
| 92 | + const unchecked = parseUncheckedCheckboxes(content); |
| 93 | + |
| 94 | + return { |
| 95 | + complete: unchecked.length === 0, |
| 96 | + outstanding: unchecked.map(item => ({ |
| 97 | + line: item.lineNumber, |
| 98 | + text: item.text.trim(), |
| 99 | + section: item.section // Plan, Test, etc. |
| 100 | + })), |
| 101 | + totalCheckboxes: countTotalCheckboxes(content), |
| 102 | + completedCount: countCheckedCheckboxes(content) |
| 103 | + }; |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### Feedback Format |
| 108 | + |
| 109 | +**CLI Output:** |
| 110 | +``` |
| 111 | +⚠️ Spec has 3 outstanding checklist items: |
| 112 | +
|
| 113 | + Plan (line 42) |
| 114 | + • [ ] Update MCP prompts with validation step |
| 115 | + |
| 116 | + Test (line 78) |
| 117 | + • [ ] Test CLI verification with unchecked items |
| 118 | + • [ ] Test MCP verification behavior |
| 119 | +
|
| 120 | +❓ Mark complete anyway? (y/N) |
| 121 | +``` |
| 122 | + |
| 123 | +**MCP Tool Response:** |
| 124 | +```json |
| 125 | +{ |
| 126 | + "error": "INCOMPLETE_CHECKLIST", |
| 127 | + "message": "Cannot mark spec complete: 3 outstanding checklist items", |
| 128 | + "details": { |
| 129 | + "outstanding": [ |
| 130 | + { |
| 131 | + "section": "Plan", |
| 132 | + "line": 42, |
| 133 | + "text": "Update MCP prompts with validation step" |
| 134 | + }, |
| 135 | + { |
| 136 | + "section": "Test", |
| 137 | + "line": 78, |
| 138 | + "text": "Test CLI verification with unchecked items" |
| 139 | + }, |
| 140 | + { |
| 141 | + "section": "Test", |
| 142 | + "line": 79, |
| 143 | + "text": "Test MCP verification behavior" |
| 144 | + } |
| 145 | + ], |
| 146 | + "progress": "12/15 items complete (80%)", |
| 147 | + "suggestions": [ |
| 148 | + "Review outstanding items and complete them", |
| 149 | + "Update checkboxes: lean-spec view 174", |
| 150 | + "Or mark as work-in-progress: --status in-progress" |
| 151 | + ] |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +### Configuration |
| 157 | + |
| 158 | +Add optional config to allow/bypass: |
| 159 | +```json |
| 160 | +// .leanspec/config.json |
| 161 | +{ |
| 162 | + "validation": { |
| 163 | + "enforceCompletionChecklist": true, // Default: true |
| 164 | + "allowCompletionOverride": false // Default: false (require --force) |
| 165 | + } |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +**Override flag:** |
| 170 | +```bash |
| 171 | +lean-spec update 174 --status complete --force # Skip verification |
| 172 | +``` |
| 173 | + |
| 174 | +## Plan |
| 175 | + |
| 176 | +### Phase 1: Core Verification Logic |
| 177 | + |
| 178 | +- [ ] Create `src/core/completion-verification.ts` |
| 179 | + - [ ] `parseCheckboxes(content: string)` - Extract all checkbox items |
| 180 | + - [ ] `getUncheckedItems(content: string)` - Filter unchecked only |
| 181 | + - [ ] `getSectionContext(content: string, lineNum: number)` - Determine section (Plan/Test/etc) |
| 182 | + - [ ] `verifyCompletion(specPath: string)` - Main verification function |
| 183 | + |
| 184 | +- [ ] Add unit tests for parser |
| 185 | + - [ ] Parse mixed checked/unchecked items |
| 186 | + - [ ] Handle nested checkboxes (indented) |
| 187 | + - [ ] Extract line numbers and text correctly |
| 188 | + - [ ] Identify section headers (Plan, Test, etc.) |
| 189 | + |
| 190 | +### Phase 2: CLI Integration |
| 191 | + |
| 192 | +- [ ] Update `src/commands/update.ts` |
| 193 | + - [ ] Detect status change to `complete` |
| 194 | + - [ ] Call `verifyCompletion()` before applying change |
| 195 | + - [ ] Format and display warning with outstanding items |
| 196 | + - [ ] Add interactive prompt (Y/n) for override |
| 197 | + - [ ] Support `--force` flag to bypass |
| 198 | + |
| 199 | +- [ ] Add CLI tests |
| 200 | + - [ ] Test verification triggers on status change |
| 201 | + - [ ] Test warning display format |
| 202 | + - [ ] Test interactive prompt behavior |
| 203 | + - [ ] Test `--force` flag bypass |
| 204 | + |
| 205 | +### Phase 3: MCP Integration |
| 206 | + |
| 207 | +- [ ] Update `packages/mcp/src/tools/update.ts` |
| 208 | + - [ ] Call `verifyCompletion()` before status change |
| 209 | + - [ ] Return structured error with outstanding items |
| 210 | + - [ ] Include progress metrics (X/Y complete) |
| 211 | + - [ ] Provide actionable suggestions in response |
| 212 | + |
| 213 | +- [ ] Update MCP schema |
| 214 | + - [ ] Add `force` parameter to update tool |
| 215 | + - [ ] Document verification behavior in tool description |
| 216 | + |
| 217 | +- [ ] Update MCP prompts (`packages/mcp/src/prompts/`) |
| 218 | + - [ ] Add checkpoint guidance before marking complete |
| 219 | + - [ ] Teach pattern: verify → fix outstanding → mark complete |
| 220 | + |
| 221 | +### Phase 4: Configuration & Docs |
| 222 | + |
| 223 | +- [ ] Add config options to `ConfigSchema` |
| 224 | + - [ ] `validation.enforceCompletionChecklist` |
| 225 | + - [ ] `validation.allowCompletionOverride` |
| 226 | + |
| 227 | +- [ ] Update documentation |
| 228 | + - [ ] Add to CLI reference (update command) |
| 229 | + - [ ] Add to MCP tools reference |
| 230 | + - [ ] Add to AGENTS.md workflow guidance |
| 231 | + - [ ] Add to best practices |
| 232 | + |
| 233 | +- [ ] Update both locales |
| 234 | + - [ ] `packages/ui/src/locales/en/common.json` |
| 235 | + - [ ] `packages/ui/src/locales/zh-CN/common.json` |
| 236 | + - [ ] `packages/mcp/src/locales/en/common.json` (if needed) |
| 237 | + - [ ] `packages/mcp/src/locales/zh-CN/common.json` (if needed) |
| 238 | + |
| 239 | +## Test |
| 240 | + |
| 241 | +### Unit Tests |
| 242 | + |
| 243 | +- [ ] Parser correctly identifies unchecked items |
| 244 | +- [ ] Section detection works for all heading levels |
| 245 | +- [ ] Line numbers are accurate |
| 246 | +- [ ] Nested checkboxes handled properly |
| 247 | +- [ ] Edge cases: no checkboxes, all checked, mixed |
| 248 | + |
| 249 | +### Integration Tests |
| 250 | + |
| 251 | +- [ ] CLI: Verification triggers on status→complete |
| 252 | +- [ ] CLI: Warning displays with correct formatting |
| 253 | +- [ ] CLI: Interactive prompt works correctly |
| 254 | +- [ ] CLI: `--force` flag bypasses verification |
| 255 | +- [ ] MCP: Returns structured error with details |
| 256 | +- [ ] MCP: `force` parameter bypasses verification |
| 257 | + |
| 258 | +### E2E Workflow Tests |
| 259 | + |
| 260 | +- [ ] AI agent completes spec with outstanding items → receives feedback |
| 261 | +- [ ] Agent reviews feedback, completes items, marks complete → success |
| 262 | +- [ ] Human uses `--force` to override → succeeds with warning |
| 263 | +- [ ] Spec with no checkboxes → completes without verification |
| 264 | + |
| 265 | +### Real-World Validation |
| 266 | + |
| 267 | +- [ ] Test with actual spec during implementation |
| 268 | +- [ ] Verify feedback is actionable for AI agents |
| 269 | +- [ ] Confirm workflow feels natural, not burdensome |
| 270 | +- [ ] Validate performance impact is negligible (<50ms) |
| 271 | + |
| 272 | +## Notes |
| 273 | + |
| 274 | +### Design Decisions |
| 275 | + |
| 276 | +**Why checkboxes only?** |
| 277 | +- Structured, unambiguous signal of completion criteria |
| 278 | +- Already widely used in specs (Plan, Test sections) |
| 279 | +- Easy to parse reliably without AI/NLP |
| 280 | + |
| 281 | +**Why warning instead of hard block?** |
| 282 | +- Humans may have valid reasons to mark complete (e.g., deferred items) |
| 283 | +- Flexibility reduces friction while maintaining awareness |
| 284 | +- `--force` flag provides escape hatch |
| 285 | + |
| 286 | +**Why not validate during status update in any direction?** |
| 287 | +- Only `→ complete` transition matters for quality gate |
| 288 | +- Other transitions (planned→in-progress) don't require verification |
| 289 | +- Keeps feedback focused and actionable |
| 290 | + |
| 291 | +### Future Enhancements |
| 292 | + |
| 293 | +- [ ] **Codebase verification**: Check if files mentioned in checkboxes exist |
| 294 | +- [ ] **Test execution**: Run tests before allowing completion |
| 295 | +- [ ] **Dependency checks**: Verify `depends_on` specs are complete |
| 296 | +- [ ] **Quality metrics**: Token count, validation pass before complete |
| 297 | +- [ ] **Sub-spec coordination**: Check sub-specs completion status |
| 298 | + |
| 299 | +### Related Specs |
| 300 | + |
| 301 | +- [018-spec-validation](../018-spec-validation/README.md) - Existing validation infrastructure |
| 302 | +- [122-ai-agent-deps-management-fix](../122-ai-agent-deps-management-fix/README.md) - Workflow validation patterns |
0 commit comments