← back to The Ai Factory

src/stages/iterate.js

40 lines

// Stage 7 — iterate. If critic rejected, qwen3:14b applies the fix_instructions
// to the scaffolded file and overwrites it. Single retry only.

const fs = require('node:fs/promises');
const { ollama } = require('../llm');

const ITERATE_SYSTEM = `You revise a Claude Code subagent or skill file in place. Output ONLY the revised file content — same format, same frontmatter shape, no code fences, no commentary.`;

async function runIterate({ outPath, content, spec, review }) {
  if (review.approved) return { changed: false, content };

  const userPrompt = `Original spec:
${JSON.stringify(spec, null, 2)}

Current file content:
---BEGIN FILE---
${content}
---END FILE---

Issues found by critic:
${review.issues.map((i, n) => `${n + 1}. ${i}`).join('\n')}

Apply these fixes:
${review.fix_instructions.map((f, n) => `${n + 1}. ${f}`).join('\n')}

Output the revised file content now.`;

  const revised = (await ollama({
    model: 'qwen3:14b',
    system: ITERATE_SYSTEM,
    prompt: userPrompt,
    temperature: 0.2
  })).replace(/^```(?:markdown|md)?\s*\n/, '').replace(/\n```\s*$/, '').trim();

  await fs.writeFile(outPath, revised, 'utf8');
  return { changed: true, content: revised };
}

module.exports = { runIterate };