10. Rewrite Challenge Defects
Purpose: Pre-split defective challenges into partitions, rewrite them with AI to fix CQ rule violations, and upsert the corrected versions back to the database.
Prerequisites:
.env.localhasDATABASE_URLand API key for the routed model (see available models)- Challenge content exists in
fact_challenge_content(run prompt 04 first) - Validation has identified defects (
generate-challenge-content.ts --validate)
Cost / Duration: ~$3-$20 depending on defect count | 1-3 hours
Prompt
I need to rewrite defective challenge content that failed CQ rule validation.
This is a 3-step sequence -- all steps must run in order.
### Step 1: Pre-split defects into partitions
```bash
bun scripts/seed/presplit-defects.ts --partitions 4
```
This queries defective challenges from the database and splits them into
4 partition files for parallel processing. Report how many defects were found.
If 0 defects are found, validation is already passing -- no action needed.
### Step 2: Rewrite defects in parallel
Run each partition in a separate terminal:
```bash
bun scripts/seed/rewrite-challenge-defects.ts --concurrency 5 --partition 1/4
bun scripts/seed/rewrite-challenge-defects.ts --concurrency 5 --partition 2/4
bun scripts/seed/rewrite-challenge-defects.ts --concurrency 5 --partition 3/4
bun scripts/seed/rewrite-challenge-defects.ts --concurrency 5 --partition 4/4
```
Each instance rewrites defective challenges using AI and writes corrected
output to JSONL files. Reduce `--concurrency` to 3 if hitting API rate limits.
### Step 3: Upsert rewritten challenges
```bash
bun scripts/seed/upsert-rewritten-challenges.ts
```
Reads all rewritten JSONL output files and upserts corrected challenges
back into `fact_challenge_content`.
### Step 4: Re-validate
```bash
bun scripts/seed/generate-challenge-content.ts --validate
```
CQ violation count should drop significantly. CQ-002 pass rate should be
above 95%. If the same defects reappear, the prompt rules in
`packages/ai/src/challenge-content-rules.ts` may need adjustment for
the specific defect type.
Cost Table
| Defects | Estimated Cost |
|---|---|
| 500 | ~$3 |
| 2,000 | ~$12 |
| 5,000 | ~$20 |
Verification
- Pre-split found and partitioned defective challenges
- All 4 rewrite partitions completed without errors
- Upsert reported non-zero rows written
- CQ violation count dropped significantly after re-validation
- CQ-002 pass rate above 95%
Related Prompts
- Generate Challenge Content -- Initial challenge generation
- Run Voice Enforcement -- Voice-specific regeneration pass
- CQ-002 Compliance Patch -- Targeted CQ-002 fixes