|
| 1 | +--- |
| 2 | +name: code-validation-gate |
| 3 | +description: Validate AI-generated code for common errors (imports, variables, types) before test execution |
| 4 | +argument-hint: "--session WFS-test-session-id [--fix] [--strict]" |
| 5 | +examples: |
| 6 | + - /workflow:tools:code-validation-gate --session WFS-test-auth |
| 7 | + - /workflow:tools:code-validation-gate --session WFS-test-auth --fix |
| 8 | + - /workflow:tools:code-validation-gate --session WFS-test-auth --strict |
| 9 | +--- |
| 10 | + |
| 11 | +# Code Validation Gate Command |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +Pre-test validation gate that checks AI-generated code for common errors before test execution. This prevents wasted test cycles on code with fundamental issues like import errors, variable conflicts, and type mismatches. |
| 16 | + |
| 17 | +## Core Philosophy |
| 18 | + |
| 19 | +- **Fail Fast**: Catch fundamental errors before expensive test execution |
| 20 | +- **AI-Aware**: Specifically targets common AI code generation mistakes |
| 21 | +- **Auto-Remediation**: Attempt safe fixes before failing |
| 22 | +- **Clear Feedback**: Provide actionable fix suggestions for manual intervention |
| 23 | + |
| 24 | +## Target Error Categories |
| 25 | + |
| 26 | +### L0.1: Compilation Errors |
| 27 | +- TypeScript compilation failures |
| 28 | +- Syntax errors |
| 29 | +- Module resolution failures |
| 30 | + |
| 31 | +### L0.2: Import Errors |
| 32 | +- Unresolved module imports (hallucinated packages) |
| 33 | +- Circular dependencies |
| 34 | +- Duplicate imports |
| 35 | +- Unused imports |
| 36 | + |
| 37 | +### L0.3: Variable Errors |
| 38 | +- Variable redeclaration |
| 39 | +- Scope conflicts (shadowing) |
| 40 | +- Undefined variable usage |
| 41 | +- Unused variables |
| 42 | + |
| 43 | +### L0.4: Type Errors (TypeScript) |
| 44 | +- Type mismatches |
| 45 | +- Missing type definitions |
| 46 | +- Excessive `any` usage |
| 47 | +- Implicit `any` types |
| 48 | + |
| 49 | +### L0.5: AI-Specific Patterns |
| 50 | +- Placeholder code (`// TODO: implement`) |
| 51 | +- Hallucinated package imports |
| 52 | +- Mock code in production files |
| 53 | +- Inconsistent naming patterns |
| 54 | + |
| 55 | +## Execution Process |
| 56 | + |
| 57 | +``` |
| 58 | +Input Parsing: |
| 59 | + ├─ Parse flags: --session (required), --fix, --strict |
| 60 | + └─ Load test-quality-config.json |
| 61 | +
|
| 62 | +Phase 1: Context Loading |
| 63 | + ├─ Load session metadata |
| 64 | + ├─ Identify target files (from IMPL-001 output or context-package) |
| 65 | + └─ Detect project configuration (tsconfig, eslint, etc.) |
| 66 | +
|
| 67 | +Phase 2: Validation Execution |
| 68 | + ├─ L0.1: Run TypeScript compilation check |
| 69 | + ├─ L0.2: Run import validation |
| 70 | + ├─ L0.3: Run variable validation |
| 71 | + ├─ L0.4: Run type validation |
| 72 | + └─ L0.5: Run AI-specific checks |
| 73 | +
|
| 74 | +Phase 3: Result Analysis |
| 75 | + ├─ Aggregate all findings by severity |
| 76 | + ├─ Calculate pass/fail status |
| 77 | + └─ Generate fix suggestions |
| 78 | +
|
| 79 | +Phase 4: Auto-Fix (if --fix enabled) |
| 80 | + ├─ Apply safe auto-fixes (imports, formatting) |
| 81 | + ├─ Re-run validation |
| 82 | + └─ Report remaining issues |
| 83 | +
|
| 84 | +Phase 5: Gate Decision |
| 85 | + ├─ PASS: Proceed to IMPL-001.5 |
| 86 | + ├─ SOFT_FAIL: Auto-fix applied, needs re-validation |
| 87 | + └─ HARD_FAIL: Block with detailed report |
| 88 | +``` |
| 89 | + |
| 90 | +## Execution Lifecycle |
| 91 | + |
| 92 | +### Phase 1: Context Loading |
| 93 | + |
| 94 | +**Load session and identify validation targets.** |
| 95 | + |
| 96 | +```javascript |
| 97 | +// Load session metadata |
| 98 | +Read(".workflow/active/{session_id}/workflow-session.json") |
| 99 | + |
| 100 | +// Load context package for target files |
| 101 | +Read(".workflow/active/{session_id}/.process/context-package.json") |
| 102 | +// OR |
| 103 | +Read(".workflow/active/{session_id}/.process/test-context-package.json") |
| 104 | + |
| 105 | +// Identify files to validate: |
| 106 | +// 1. Source files from context.implementation_files |
| 107 | +// 2. Test files from IMPL-001 output (if exists) |
| 108 | +// 3. All modified files since session start |
| 109 | +``` |
| 110 | + |
| 111 | +**Target File Discovery**: |
| 112 | +- Source files: `context.focus_paths` from context-package |
| 113 | +- Generated tests: `.workflow/active/{session_id}/.task/IMPL-001-output/` |
| 114 | +- All TypeScript/JavaScript in target directories |
| 115 | + |
| 116 | +### Phase 2: Validation Execution |
| 117 | + |
| 118 | +**Execute validation checks in order of dependency.** |
| 119 | + |
| 120 | +#### L0.1: TypeScript Compilation |
| 121 | + |
| 122 | +```bash |
| 123 | +# Primary check - catches most fundamental errors |
| 124 | +npx tsc --noEmit --skipLibCheck --project tsconfig.json 2>&1 |
| 125 | + |
| 126 | +# Parse output for errors |
| 127 | +# Critical: Any compilation error blocks further validation |
| 128 | +``` |
| 129 | + |
| 130 | +**Error Patterns**: |
| 131 | +``` |
| 132 | +error TS2307: Cannot find module 'xxx' |
| 133 | +error TS2451: Cannot redeclare block-scoped variable 'xxx' |
| 134 | +error TS2322: Type 'xxx' is not assignable to type 'yyy' |
| 135 | +``` |
| 136 | + |
| 137 | +#### L0.2: Import Validation |
| 138 | + |
| 139 | +```bash |
| 140 | +# Check for circular dependencies |
| 141 | +npx madge --circular --extensions ts,tsx,js,jsx {target_dirs} |
| 142 | + |
| 143 | +# ESLint import rules |
| 144 | +npx eslint --rule 'import/no-duplicates: error' --rule 'import/no-unresolved: error' {files} |
| 145 | +``` |
| 146 | + |
| 147 | +**Hallucinated Package Check**: |
| 148 | +```javascript |
| 149 | +// Extract all imports from files |
| 150 | +// Verify each package exists in package.json or node_modules |
| 151 | +// Flag any unresolvable imports as "hallucinated" |
| 152 | +``` |
| 153 | + |
| 154 | +#### L0.3: Variable Validation |
| 155 | + |
| 156 | +```bash |
| 157 | +# ESLint variable rules |
| 158 | +npx eslint --rule 'no-shadow: error' --rule 'no-undef: error' --rule 'no-redeclare: error' {files} |
| 159 | +``` |
| 160 | + |
| 161 | +#### L0.4: Type Validation |
| 162 | + |
| 163 | +```bash |
| 164 | +# TypeScript strict checks |
| 165 | +npx tsc --noEmit --strict {files} |
| 166 | + |
| 167 | +# Check for any abuse |
| 168 | +npx eslint --rule '@typescript-eslint/no-explicit-any: warn' {files} |
| 169 | +``` |
| 170 | + |
| 171 | +#### L0.5: AI-Specific Checks |
| 172 | + |
| 173 | +```bash |
| 174 | +# Check for placeholder code |
| 175 | +grep -rn "// TODO: implement\|// Add your code here\|throw new Error.*Not implemented" {files} |
| 176 | + |
| 177 | +# Check for mock code in production files |
| 178 | +grep -rn "jest\.mock\|sinon\.\|vi\.mock" {source_files_only} |
| 179 | +``` |
| 180 | + |
| 181 | +### Phase 3: Result Analysis |
| 182 | + |
| 183 | +**Aggregate and categorize findings.** |
| 184 | + |
| 185 | +```javascript |
| 186 | +const findings = { |
| 187 | + critical: [], // Blocks all progress |
| 188 | + error: [], // Blocks with threshold |
| 189 | + warning: [] // Advisory only |
| 190 | +}; |
| 191 | + |
| 192 | +// Apply thresholds from config |
| 193 | +const config = loadConfig("test-quality-config.json"); |
| 194 | +const thresholds = config.code_validation.severity_thresholds; |
| 195 | + |
| 196 | +// Gate decision |
| 197 | +if (findings.critical.length > thresholds.critical) { |
| 198 | + decision = "HARD_FAIL"; |
| 199 | +} else if (findings.error.length > thresholds.error) { |
| 200 | + decision = "SOFT_FAIL"; // Try auto-fix |
| 201 | +} else { |
| 202 | + decision = "PASS"; |
| 203 | +} |
| 204 | +``` |
| 205 | + |
| 206 | +### Phase 4: Auto-Fix (Optional) |
| 207 | + |
| 208 | +**Apply safe automatic fixes when --fix flag provided.** |
| 209 | + |
| 210 | +```bash |
| 211 | +# Safe fixes only |
| 212 | +npx eslint --fix --rule 'import/no-duplicates: error' --rule 'unused-imports/no-unused-imports: error' {files} |
| 213 | + |
| 214 | +# Re-run validation after fixes |
| 215 | +# Report what was fixed vs what remains |
| 216 | +``` |
| 217 | + |
| 218 | +**Safe Fix Categories**: |
| 219 | +- Remove unused imports |
| 220 | +- Remove duplicate imports |
| 221 | +- Fix import ordering |
| 222 | +- Remove unused variables (with caution) |
| 223 | +- Formatting fixes |
| 224 | + |
| 225 | +**Unsafe (Manual Only)**: |
| 226 | +- Missing imports (need to determine correct package) |
| 227 | +- Type errors (need to understand intent) |
| 228 | +- Variable shadowing (need to understand scope intent) |
| 229 | + |
| 230 | +### Phase 5: Gate Decision |
| 231 | + |
| 232 | +**Determine next action based on results.** |
| 233 | + |
| 234 | +| Decision | Condition | Action | |
| 235 | +|----------|-----------|--------| |
| 236 | +| **PASS** | critical=0, error<=3, warning<=10 | Proceed to IMPL-001.5 | |
| 237 | +| **SOFT_FAIL** | critical=0, error>3 OR fixable issues | Auto-fix and retry (max 2) | |
| 238 | +| **HARD_FAIL** | critical>0 OR max retries exceeded | Block with report | |
| 239 | + |
| 240 | +## Output Artifacts |
| 241 | + |
| 242 | +### Validation Report |
| 243 | + |
| 244 | +**File**: `.workflow/active/{session_id}/.process/code-validation-report.md` |
| 245 | + |
| 246 | +```markdown |
| 247 | +# Code Validation Report |
| 248 | + |
| 249 | +**Session**: {session_id} |
| 250 | +**Timestamp**: {timestamp} |
| 251 | +**Status**: PASS | SOFT_FAIL | HARD_FAIL |
| 252 | + |
| 253 | +## Summary |
| 254 | +- Files Validated: {count} |
| 255 | +- Critical Issues: {count} |
| 256 | +- Errors: {count} |
| 257 | +- Warnings: {count} |
| 258 | + |
| 259 | +## Critical Issues (Must Fix) |
| 260 | +### Import Errors |
| 261 | +- `src/auth/service.ts:5` - Cannot find module 'non-existent-package' |
| 262 | + - **Suggestion**: Check if package exists, may be hallucinated by AI |
| 263 | + |
| 264 | +### Variable Conflicts |
| 265 | +- `src/utils/helper.ts:12` - Cannot redeclare block-scoped variable 'config' |
| 266 | + - **Suggestion**: Rename one of the variables or merge declarations |
| 267 | + |
| 268 | +## Errors (Should Fix) |
| 269 | +... |
| 270 | + |
| 271 | +## Warnings (Consider Fixing) |
| 272 | +... |
| 273 | + |
| 274 | +## Auto-Fix Applied |
| 275 | +- Removed 3 unused imports in `src/auth/service.ts` |
| 276 | +- Fixed import ordering in `src/utils/index.ts` |
| 277 | + |
| 278 | +## Remaining Issues Requiring Manual Fix |
| 279 | +... |
| 280 | + |
| 281 | +## Next Steps |
| 282 | +- [ ] Fix critical issues before proceeding |
| 283 | +- [ ] Review error suggestions |
| 284 | +- [ ] Re-run validation: `/workflow:tools:code-validation-gate --session {session_id}` |
| 285 | +``` |
| 286 | + |
| 287 | +### JSON Report (Machine-Readable) |
| 288 | + |
| 289 | +**File**: `.workflow/active/{session_id}/.process/code-validation-report.json` |
| 290 | + |
| 291 | +```json |
| 292 | +{ |
| 293 | + "session_id": "WFS-test-xxx", |
| 294 | + "timestamp": "2025-01-30T10:00:00Z", |
| 295 | + "status": "HARD_FAIL", |
| 296 | + "summary": { |
| 297 | + "files_validated": 15, |
| 298 | + "critical": 2, |
| 299 | + "error": 5, |
| 300 | + "warning": 8 |
| 301 | + }, |
| 302 | + "findings": { |
| 303 | + "critical": [ |
| 304 | + { |
| 305 | + "category": "import", |
| 306 | + "file": "src/auth/service.ts", |
| 307 | + "line": 5, |
| 308 | + "message": "Cannot find module 'non-existent-package'", |
| 309 | + "suggestion": "Check if package exists in package.json", |
| 310 | + "auto_fixable": false |
| 311 | + } |
| 312 | + ], |
| 313 | + "error": [...], |
| 314 | + "warning": [...] |
| 315 | + }, |
| 316 | + "auto_fixes_applied": [...], |
| 317 | + "gate_decision": "HARD_FAIL", |
| 318 | + "retry_count": 0, |
| 319 | + "max_retries": 2 |
| 320 | +} |
| 321 | +``` |
| 322 | + |
| 323 | +## Command Options |
| 324 | + |
| 325 | +| Option | Description | Default | |
| 326 | +|--------|-------------|---------| |
| 327 | +| `--session` | Test session ID (required) | - | |
| 328 | +| `--fix` | Enable auto-fix for safe issues | false | |
| 329 | +| `--strict` | Use strict thresholds (0 errors allowed) | false | |
| 330 | +| `--files` | Specific files to validate (comma-separated) | All target files | |
| 331 | +| `--skip-types` | Skip TypeScript type checks | false | |
| 332 | + |
| 333 | +## Integration |
| 334 | + |
| 335 | +### Command Chain |
| 336 | + |
| 337 | +- **Called By**: `/workflow:test-fix-gen` (after IMPL-001) |
| 338 | +- **Requires**: IMPL-001 output OR context-package.json |
| 339 | +- **Followed By**: IMPL-001.5 (Test Quality Gate) on PASS |
| 340 | + |
| 341 | +### Task JSON Integration |
| 342 | + |
| 343 | +When used in test-fix workflow, generates task: |
| 344 | + |
| 345 | +```json |
| 346 | +{ |
| 347 | + "id": "IMPL-001.3-validation", |
| 348 | + "meta": { |
| 349 | + "type": "code-validation", |
| 350 | + "agent": "@test-fix-agent" |
| 351 | + }, |
| 352 | + "context": { |
| 353 | + "depends_on": ["IMPL-001"], |
| 354 | + "requirements": "Validate generated code for AI common errors" |
| 355 | + }, |
| 356 | + "flow_control": { |
| 357 | + "validation_config": "~/.claude/workflows/test-quality-config.json", |
| 358 | + "max_retries": 2, |
| 359 | + "auto_fix_enabled": true |
| 360 | + }, |
| 361 | + "acceptance_criteria": [ |
| 362 | + "Zero critical issues", |
| 363 | + "Maximum 3 error issues", |
| 364 | + "All imports resolvable", |
| 365 | + "No variable redeclarations" |
| 366 | + ] |
| 367 | +} |
| 368 | +``` |
| 369 | + |
| 370 | +## Error Handling |
| 371 | + |
| 372 | +| Error | Resolution | |
| 373 | +|-------|------------| |
| 374 | +| tsconfig.json not found | Use default compiler options | |
| 375 | +| ESLint not installed | Skip ESLint checks, use tsc only | |
| 376 | +| madge not installed | Skip circular dependency check | |
| 377 | +| No files to validate | Return PASS (nothing to check) | |
| 378 | + |
| 379 | +## Best Practices |
| 380 | + |
| 381 | +1. **Run Early**: Execute validation immediately after code generation |
| 382 | +2. **Use --fix First**: Let auto-fix resolve trivial issues |
| 383 | +3. **Review Suggestions**: AI fix suggestions may need human judgment |
| 384 | +4. **Don't Skip Critical**: Never proceed with critical errors |
| 385 | +5. **Track Patterns**: Common failures indicate prompt improvement opportunities |
| 386 | + |
| 387 | +## Related Commands |
| 388 | + |
| 389 | +- `/workflow:test-fix-gen` - Parent workflow that invokes this command |
| 390 | +- `/workflow:tools:test-quality-gate` - Next phase (IMPL-001.5) for test quality |
| 391 | +- `/workflow:test-cycle-execute` - Execute tests after validation passes |
0 commit comments