Skip to content

100x is a skill by a former Cursor engineer that turns prompts into formally verified, production-ready code

32.4 KB
Raw Blame
import fs from 'node:fs';
import path from 'node:path';
import { clockStatus } from './clock.mjs';
import { checkCommand, cursorRoute, helperTarget } from './delegate.mjs';
import { sameModel } from './models.mjs';
import { mustNotBreakList } from './must-not-break.mjs';
import { commitsAfter, commitsSince, recentCommits, stageTags } from './git.mjs';
import { detect } from './init.mjs';
import { globalDir } from './paths.mjs';
import { matrices, readAsks, readRun, runRows, section, stateFile } from './project.mjs';
import { promptStyle, writtenSection } from './prompt.mjs';
import { modern } from './runfile.mjs';
import { LENSES, briefFile, findingsFile, lensFor, plannedRounds, reviewDir, roundDir, roundStatus, triageFile, readState } from './rounds.mjs';
import { CliError } from './errors.mjs';
import { BRIEF_INTRO, CLEAN_LOOK, EARLIER_SOLO, briefRules, briefSeat, isSolo, loopSteps } from './solo.mjs';
import { plural } from './text.mjs';

export const SEVERITIES = ['blocker', 'major', 'minor', 'nit'];
export { briefFile, findingsFile, reviewDir, triageFile };

const DASHES = '\\u2013\\u2014';
const BRACKET_SEVERITY = new RegExp(`[\\[(]\\s*(${SEVERITIES.join('|')})\\s*[\\])]`, 'i');
const LEADING_SEVERITY = new RegExp(`^[\\s:.|*${DASHES}-]*(${SEVERITIES.join('|')})\\b\\s*(?:[:.|${DASHES}-]|$)`, 'i');
const FINDING_LINE = /^(?:#{1,4}\s+|\s*[-*]\s+)\**(F\d+)\b\**(.*)$/;
const TRIAGE_LINE = new RegExp(`^\\s*(?:[-*]\\s*)?\\**(F\\d+)\\**\\s*[:.${DASHES}-]?\\s*\\**(accept|accepted|reject|rejected|defer|deferred)\\**\\b[\\s.:${DASHES}-]*(.*)$`, 'i');
const NO_FINDINGS = /^\W*no findings\b/im;
const DECISION = { accept: 'accept', accepted: 'accept', reject: 'reject', rejected: 'reject', defer: 'defer', deferred: 'defer' };

// The reviewer's other sections list lines that start with F1 and F2 too. Those lines are not new findings.
const OTHER_SECTION = /^#{1,4}\s+(?:earlier findings|what is good|verdict)\b/i;

export function parseFindings(text) {
  const findings = [];
  const warnings = [];
  const seen = new Set();
  let skipLevel = 0;
  for (const line of text.split('\n')) {
    const heading = /^(#{1,4})\s/.exec(line);
    if (heading) {
      if (OTHER_SECTION.test(line)) skipLevel = heading[1].length;
      else if (heading[1].length <= skipLevel) skipLevel = 0;
    }
    if (skipLevel) continue;
    const hit = FINDING_LINE.exec(line);
    if (!hit) continue;
    const [, id, rest] = hit;
    if (seen.has(id)) {
      warnings.push(`${id} appears more than once in findings.md, only the first counts`);
      continue;
    }
    seen.add(id);
    const word = BRACKET_SEVERITY.exec(rest) ?? LEADING_SEVERITY.exec(rest);
    if (!word) warnings.push(`${id} has no severity word, so it counts as a blocker. The reviewer writes "## ${id} [blocker|major|minor|nit] Title"`);
    const title = (word ? rest.replace(word[0], ' ') : rest)
      .replace(/[[\]()*`]/g, '')
      .replace(new RegExp(`^[\\s:.|${DASHES}-]+`), '')
      .trim();
    findings.push({ id, severity: word ? word[1].toLowerCase() : 'blocker', title: title || '(no title)' });
  }
  return { findings, warnings };
}

// A reason has to point at something a reader can check. A commit, a path, a command or a number does.
const CHECKABLE = /`[^`]+`|\b[0-9a-f]{7,40}\b|[\[email protected]]+\/[\w@./-]+|\b[\w-]+\.[a-z][a-z0-9]{0,5}\b|\d/i;
const REASON_MIN = { accept: 15, reject: 25, defer: 15 };

export const pointsAtSomething = (text) => CHECKABLE.test(text);

export function reasonProblem(decision, reason) {
  if (reason.length < REASON_MIN[decision]) return `needs a reason of at least ${REASON_MIN[decision]} characters. Got "${reason}"`;
  if (decision !== 'defer' && !CHECKABLE.test(reason)) return `needs a reason that points at something checkable, like a commit hash, a file path, a command in backticks or a number. Got "${reason}"`;
  return null;
}

export function parseTriage(text) {
  const entries = new Map();
  const errors = [];
  for (const line of text.split('\n')) {
    const hit = TRIAGE_LINE.exec(line);
    if (!hit) continue;
    const id = hit[1];
    const decision = DECISION[hit[2].toLowerCase()];
    const reason = hit[3].trim();
    if (entries.has(id)) errors.push(`${id} is triaged twice in triage.md`);
    entries.set(id, { decision, reason });
  }
  return { entries, errors };
}

export function checkTriage(root) {
  const errors = [];
  const warnings = [];
  const where = findingsFile(root);
  if (!fs.existsSync(where)) {
    return { errors: ['no findings file at .100x/review/findings.md. The reviewer has not written it.'], warnings, findings: [], rows: [], summary: null };
  }
  const text = fs.readFileSync(where, 'utf8');
  const parsed = parseFindings(text);
  warnings.push(...parsed.warnings);
  if (parsed.findings.length === 0 && !NO_FINDINGS.test(text)) {
    errors.push('findings.md has no findings (F1, F2 and so on) and no "No findings" line. Treat the review as unfinished.');
  }
  const triage = fs.existsSync(triageFile(root)) ? parseTriage(fs.readFileSync(triageFile(root), 'utf8')) : { entries: new Map(), errors: [] };
  errors.push(...triage.errors);

  const known = new Set(parsed.findings.map((finding) => finding.id));
  for (const id of triage.entries.keys()) if (!known.has(id)) errors.push(`triage.md mentions ${id}, which is not in findings.md`);

  const rows = parsed.findings.map((finding) => ({ ...finding, ...(triage.entries.get(finding.id) ?? { decision: null, reason: '' }) }));
  const untriaged = rows.filter((row) => !row.decision).map((row) => row.id);
  if (untriaged.length) errors.push(`no triage for ${untriaged.join(', ')}. Write "<id> accept|reject|defer. reason" in .100x/review/triage.md.`);
  for (const row of rows.filter((r) => r.decision)) {
    const problem = reasonProblem(row.decision, row.reason);
    if (problem) errors.push(`${row.id} ${row.decision} ${problem}`);
  }
  for (const row of rows.filter((r) => r.severity === 'blocker' && r.decision === 'defer')) errors.push(`${row.id} is a blocker and cannot be deferred. Fix it or reject it with evidence.`);

  const count = (decision) => rows.filter((row) => row.decision === decision).length;
  const bySeverity = Object.fromEntries(SEVERITIES.map((s) => [s, rows.filter((row) => row.severity === s).length]));
  const summary = { flags: rows.length, accepted: count('accept'), rejected: count('reject'), deferred: count('defer'), untriaged: untriaged.length, bySeverity };
  return { errors, warnings, findings: parsed.findings, rows, summary };
}

const demote = (markdown, levels) => markdown.replace(/^(#{1,6}) /gm, (_, hashes) => `${'#'.repeat(Math.min(6, hashes.length + levels))} `);
const clip = (text, length) => (text.length > length ? `${text.slice(0, length - 1)}…` : text);
const MARK = { open: '[ ]', done: '[x]', dropped: '[~]' };

// What the builder wrote into its own prompt that the judge can hold it to.
const OWN_TERMS = [
  ['The builder\'s own rules. Check that the product keeps them', 'Rules'],
  ['Out of scope, and what the product says it cannot do. Do not ask for these, and check it says so', 'Scope'],
  ['What the builder promised not to do', 'Do not'],
];

// Every product is good or bad for its own reasons, so the judge names them first and the generic list comes second.
const FOCUS_FIRST = `Decide what makes this kind of product good or bad, the two to four things a user in this category judges hardest. A game, its feel, art and sound. A money tool, its math. An asset pack, the quality and consistency of the assets themselves. A CLI, its errors and docs. Start from the builder's focus above, which may have blind spots, and add what it missed. Judge those things first and deepest, in every round. Then walk the list below, skip what does not apply to this product and name each skipped item in one line.`;

const RUBRICS = {
  code: `## What to judge, in this order. The code and how it behaves

1. The asks. Open every ask in the ledger above. Is it done where it says, and does the evidence hold? A missing or quietly narrowed ask is a blocker.
2. Does it do what it claims? Run the main flow, then try to break it. Real inputs, extremes, empty, hostile, double clicks, reload in the middle, slow or no network, odd locales and time zones, long sessions.
3. The one thing that must not break. Try to break it, with numbers and cases you chose yourself, not the builder's.
4. Data and state. What is stored, where, and what happens on refresh, upgrade, corruption or two tabs. Anything that can lose or leak a user's data is a blocker.
5. Security. Secrets in the repo, injection, missing auth or checks, unsafe input handling, risky dependencies, anything that spends money or deploys without the owner saying so.
6. Code health. Structure, duplication, dead code, errors swallowed at boundaries, tests that check behavior instead of the implementation. Could a stranger maintain this?
7. From clean. A fresh checkout, install, build and start. Missing setup notes, unpinned versions, scripts that only work on the builder's machine.
8. Console and speed. Errors and warnings in the console, failing requests, slow paths, memory that grows.
`,
  product: `## What to judge, in this order. The UI and UX

1. The first minute. Open it cold at a phone size and a desktop size. What does a stranger see and do in 60 seconds with no explanation? Is the claimed moment the first thing they feel, or is it buried or weak? A weak moment is a major finding, and your fix should name a bigger idea, not a tweak.
2. The look. Does it look designed or generated? Flag default fonts, gradient heroes, card piles, decorative icons, empty sections and anything that looks like the median AI app. Say what a person with taste would change, concretely.
3. Flows. Do the main tasks finish in few steps? Dead ends, unclear next steps, hidden actions, missing or noisy confirmations. Click everything once.
4. States. Empty, loading, error, offline, long text, one item, many items. Each one that is missing or ugly is a finding.
5. Scaling. Run the resolution matrix, then open the screenshots and look. Overflow, clipped controls, unreadable text, tap targets under 44 px, layouts that never adapt. Counts alone do not prove a layout works.
6. Feel. Feedback on every action, motion that helps and motion that annoys, speed you can feel, keyboard use and focus, contrast, reduced motion, names for screen readers.
7. Words. Labels, errors, empty states and buttons. Vague, padded or generic copy is a finding.
8. Consistency. Spacing, type, color, icons and components behave the same on every screen.
`,
  ship: `## What to judge. Would you ship this

Judge everything, as a user and as the person who has to stand behind it in public. Cover only what applies to this product.

Shipping means putting it in front of the people it is for. It may be a paid product, a free tool, an open-source or MIT-licensed project or a skill that people install. Never count the lack of a price, payments, a business model or a sales route against it unless the asks name one. Judge whether it works, does what it says, and is something the owner would be proud to publish.

1. Would you use it, or tell a friend to? If not, say what is missing before anything else.
2. What is the best alternative, free or paid, and where does this one lose to it?
3. The first real user. What breaks, confuses or embarrasses them within the first week? Install or open, the first run, errors, privacy, what happens to their data, a README or help a stranger can follow, the license, how it gets published or deployed. Sign up, payments and support only if this product has them.
4. The asks, the moment and the mustNotBreak, once more. Do they hold?
5. The five things that must change before you would put your name on it, in order.
6. What is good and must not be touched.
`,
};

const FORMAT = (root) => `## Findings format

Write ${findingsFile(root)}. One block per finding, most important first, at most 25.

\`\`\`
## F1 [blocker] Short title
Where: file and line, or screen, viewport and state
Evidence: what you ran or saw, with the exact output or a screenshot path under ${reviewDir(root)}
Fix: the smallest change that removes the problem
\`\`\`

Severity. blocker: the owner would stop using it, or an ask is missing or wrong. major: clearly hurts the experience or correctness. minor: a rough edge. nit: taste.
Every finding needs evidence from running or reading, or the word "unverified" on the Evidence line.
Then add two short sections. "What is good" lists up to six things to keep, so nobody cuts them. "Verdict" starts with one line, exactly "Ready to ship: yes" or "Ready to ship: no". Then it answers three questions in one line each, with exactly these labels: "Would the owner keep going:", "Single most valuable change:" and "What I would cut first:".
If you find nothing, write "No findings." and say what you ran.
`;

// The builder's side of the earlier rounds, for the judge to check instead of trust. Finding, builder's decision and reason.
function earlierRounds(root) {
  const { entries } = readState(root);
  const lines = [];
  for (const entry of entries) {
    const dir = roundDir(root, entry.slot);
    const read = (name) => (fs.existsSync(path.join(dir, name)) ? fs.readFileSync(path.join(dir, name), 'utf8') : '');
    const findings = parseFindings(read('findings.md')).findings;
    const triage = parseTriage(read('triage.md')).entries;
    lines.push(`Round ${entry.slot}, ${LENSES[entry.lens]}. The judge said ready to ship: ${entry.ready === null ? 'no line' : entry.ready ? 'yes' : 'no'}.`);
    if (findings.length === 0) lines.push('- no findings');
    for (const finding of findings) {
      const decision = triage.get(finding.id);
      lines.push(`- ${finding.id} [${finding.severity}] ${clip(finding.title, 120)}. ${decision ? `${decision.decision}. ${clip(decision.reason, 220)}` : 'no decision'}`);
    }
    lines.push('');
  }
  return lines;
}

// A judge needs this long to run the product and write a first verdict. With less on the clock there is no review to start.
export const MIN_JUDGE_MINUTES = 15;
const FINISH_MARGIN_MINUTES = 5;

// How long the judge gets: review.timeoutMinutes at most, and less when a clock is running. A 90 minute wait that the deadline
// cannot hold would mean no review at all, and a short review beats none. Five minutes stay free for triage and finish. On a
// plain deadline the last fifth belongs to wrap up (references/unattended.md), so a judge never runs into it. A fill clock has
// no such rule: its final judge sits in the reserve.
export function judgeRoom(root, config, now = new Date()) {
  const wanted = config.review.timeoutMinutes;
  const clock = clockStatus(root, now);
  if (clock.state === 'none' || clock.state === 'unreadable') return { minutes: wanted, noRoom: false, left: null };
  const kept = clock.fill ? FINISH_MARGIN_MINUTES : Math.max(FINISH_MARGIN_MINUTES, Math.round(clock.totalMinutes * 0.2));
  const room = clock.state === 'passed' ? 0 : clock.leftMinutes - kept;
  const minutes = Math.max(0, Math.min(wanted, room));
  return { minutes, noRoom: minutes < Math.min(wanted, MIN_JUDGE_MINUTES), left: Math.max(0, clock.leftMinutes) };
}

export function buildBrief(root, { config, round, now = new Date() }) {
  const project = fs.existsSync(stateFile(root, 'project.md')) ? fs.readFileSync(stateFile(root, 'project.md'), 'utf8') : '';
  const run = modern(readRun(root) ?? {});
  const title = run.title ?? project.split('\n').find((line) => line.startsWith('# '))?.slice(2) ?? path.basename(root);
  const { asks } = readAsks(root);
  const stack = detect(root);
  const { minutes } = judgeRoom(root, config, now);
  const total = config.review.rounds;
  const plan = round ?? { slot: 1, total, lens: lensFor(1, total) };
  const earlier = readState(root).entries;
  // A solo run has no other model, so the brief is written for the builder in another chair. The checks and the file formats stay the same.
  const solo = isSolo(root);
  const lines = [`# Review brief. ${title}`, `Round ${plan.slot} of ${plan.total}. Lens: ${LENSES[plan.lens]}.`, ''];

  lines.push(
    solo
      ? BRIEF_INTRO
      : 'Another AI model built this and thinks it is good. Its owner is about to decide whether to publish it and put their name on it, for the people it is meant for. Your question: is this ready to ship? Be brutal. Do not soften, balance or praise to be polite. Shipping may mean a paid product, a free tool, an open-source or MIT-licensed project or a skill other people install. Never count the lack of a price, payments, a business model or a sales route against it unless the asks name one. Every flaw you miss comes back as a bug report, a bad review or a lost user. If you would not use it or recommend it to the person it is for, say that first. You see differently from the model that built it. Use that.',
    '',
    ...(solo ? [briefSeat(plan.lens, Array.from({ length: plan.slot - 1 }, (_, i) => lensFor(i + 1, plan.total)).filter((lens) => lens === plan.lens).length), ''] : []),
    '## This round',
    '',
    plan.lens === 'ship'
      ? `Round ${plan.slot} of ${plan.total}, the last. Judge everything, as a user would.`
      : `Round ${plan.slot} of ${plan.total}. Judge ${LENSES[plan.lens]} and stay on it. A problem from another lens counts only when it is a blocker. A later round covers the rest.`,
    '',
    '## Ground rules',
    '',
    '- Run it. Reading code is not a review.',
    '- When the product has a screen, judge with your eyes. Open the contact sheet, then the single full-size screenshots: the smallest phone, the landscape phone and the widest desktop. Never judge a screen from its code, and never from counts alone. If you cannot see images, say so as your first finding with severity blocker.',
    ...(solo ? briefRules(findingsFile(root), reviewDir(root)) : [`- Write only ${findingsFile(root)}, plus screenshots under ${reviewDir(root)}. Do not edit source, install packages, commit, deploy or push.`]),
    '- Never play sound on this machine. Keep audio muted in every browser you drive.',
    '- Run at most one dev server, with the command below. Stop it when you finish.',
    '- Do not start subagents.',
    '- Do not ask the owner questions. Decide, and write the assumption into the findings file.',
    `- You have ${minutes} minutes. Write a first findings.md within ${Math.min(15, Math.ceil(minutes / 3))} minutes and keep it current, so a timeout loses nothing.`,
    '',
  );

  lines.push('## What was asked', '');
  const brief = section(project, 'Brief');
  lines.push(brief || '(no brief recorded in project.md)', '');
  const goal = section(project, 'Goal');
  if (goal) lines.push(`Goal. ${goal}`, '');
  if (asks.length) lines.push('Asks ledger.', ...asks.map((ask) => `- ${MARK[ask.status]} ${ask.text}`), '');
  const assumptions = section(project, 'Assumptions');
  if (assumptions) lines.push('Assumptions the builder made instead of asking.', assumptions, '');

  lines.push('## What matters most for this product', '');
  lines.push(`Moment. ${run.moment ?? (section(project, 'Moment') || 'not named')}`);
  const checks = mustNotBreakList(run);
  lines.push(`Must not break. ${checks.length ? checks.map((check) => `${check.kind}, ${check.status}. ${check.summary}`).join(' | ') : section(project, 'Must not break') || 'not named'}`);
  const focus = section(project, 'Judge focus');
  if (focus) lines.push(`The builder's focus. ${focus}`);
  // Only a run that began with the prompt step has terms of its own. An older project.md is read as it always was.
  for (const [label, heading] of promptStyle(root) === 'prompt' ? OWN_TERMS : []) {
    const body = writtenSection(project, heading);
    if (body) lines.push(`${label}.`, body);
  }
  lines.push('', FOCUS_FIRST, '');

  lines.push('## How to run', '');
  const runCmd = run.runCmd ?? stack.devCommand;
  const url = run.url ?? stack.urlGuess;
  lines.push(runCmd ? `Run: \`${runCmd}\` in ${root}` : `Run: no command was detected. The Stack and run notes below say how to start it, in ${root}`, url ? `Open: ${url}` : 'Open: no address was detected. See the notes below.');
  if (stack.kind !== 'unknown') lines.push(`Stack: ${[stack.kind, ...stack.frameworks].join(', ')}`);
  const runNotes = section(project, 'Stack and run');
  if (runNotes) lines.push(runNotes);
  lines.push('');

  lines.push('## What was done', '');
  const tags = stageTags(root);
  if (tags.length) lines.push(`Stage tags. ${tags.join(', ')}`);
  // The commits and the decision notes are the builder's reasons, and a self-judge that reads them first believes them.
  const commits = solo ? '' : recentCommits(root, 25);
  if (commits) lines.push('Recent commits.', '```', commits, '```');
  const rows = solo ? [] : runRows(root).slice(-30);
  if (rows.length) lines.push('Last decisions (phase, decision, result).', ...rows.map((row) => `- ${row.phase}. ${clip(row.decision, 220)} (${clip(row.result, 60)})`));
  const found = matrices(root);
  if (found.length) {
    lines.push('Resolution matrices already run.');
    for (const m of found) lines.push(`- ${m.label}. ${m.matrix}, ${plural(m.viewports, 'viewport')}, ${plural(m.states, 'state')}, broken ${m.totals.broken}, weak ${m.totals.weak}. Contact sheet image ${path.join(m.dir, 'contact-sheet.png')}`);
  }
  const checkDir = stateFile(root, 'must-not-break');
  const checkFiles = fs.existsSync(checkDir) ? fs.readdirSync(checkDir).filter((name) => !name.startsWith('.')).slice(0, 30) : [];
  if (checkFiles.length) lines.push(`must-not-break outputs in ${checkDir}. ${checkFiles.join(', ')}`);
  lines.push('');

  lines.push('## Tools you may use', '');
  lines.push(`- \`100x shots <url> --matrix all --label review\` drives the page across phones, tablets and desktops with touch, DPR and rotation, writes screenshots and a contact sheet to ${stateFile(root, 'shots', 'review')}. \`--steps\` and \`--init-script\` reach deeper states.`);
  lines.push('- `100x text lint <file> --profile ui` checks interface copy. `100x shots` leaves every string the page showed in `texts.txt` next to the screenshots.');
  lines.push('- Playwright, a browser and any command the project already has.', '');

  if (earlier.length) {
    lines.push('## Earlier rounds', '', solo ? EARLIER_SOLO : 'You are the next judge in a back and forth. Another judge already looked, and the builder answered each finding. Do not trust the answers. Check them.', '');
    lines.push(...earlierRounds(root));
    const lastRound = earlier.at(-1);
    const since = lastRound.briefHead ? commitsAfter(root, lastRound.briefHead) : commitsSince(root, lastRound.briefedAt ?? lastRound.closedAt);
    // A self-judge checks a fix by running the product. The builder's commit messages are the reasons it is trying to get away from.
    const count = since ? since.split('\n').filter(Boolean).length : 0;
    if (solo) lines.push(count ? `${plural(count, 'commit')} since the last round was briefed, the builder's fixes. Check each accepted finding by running the product, not by reading what a commit says.` : 'No commits since the last round was briefed.', '');
    else lines.push(since ? `Commits since the last round was briefed. These are the builder's fixes.\n\`\`\`\n${since}\n\`\`\`` : 'No commits since the last round was briefed.', '');
    lines.push(
      '- For every accepted finding, run the product and check the fix holds. Do not read the commit message and stop there. Write one line each under "## Earlier findings check", as "Round 1 F3 - fixed, <what you saw>" ("fixed", "not fixed" or "regressed").',
      '- A rejected finding whose reason is weak is a new finding titled "Reopened: ...". A rejection that cites evidence you can reproduce stays closed.',
      '- Do not repeat a finding that is fixed. Do not go easier because the builder worked hard.',
      '',
    );
  }
  lines.push(RUBRICS[plan.lens], FORMAT(root), ...(solo ? [CLEAN_LOOK, ''] : []));

  const profile = path.join(globalDir(), 'profile.md');
  if (fs.existsSync(profile)) lines.push('## What the owner cares about', '', demote(clip(fs.readFileSync(profile, 'utf8').trim(), 8000), 2), '');

  lines.push(`Built ${now.toISOString()} by 100x brief.`);
  return `${lines.join('\n')}\n`;
}

// A loop judge is the small, brutal look inside one loop: one polish pass, one fill round. It gets its own folder so it can
// never clash with the stage 6 rounds, and it answers two questions only: are we drifting, and what is still bad.
export const LOOP_MINUTES = 20;
export const loopDir = (root, name) => stateFile(root, 'loops', name);
export const loopAskFile = (root, name) => path.join(loopDir(root, name), 'ask.md');
export const loopFindingsFile = (root, name) => path.join(loopDir(root, name), 'judge.md');
export const loopAnswersFile = (root, name) => path.join(loopDir(root, name), 'answers.md');
const LOOP_NAME = /^[a-z0-9][a-z0-9-]*$/;

export function checkLoopName(root, name) {
  if (!LOOP_NAME.test(name ?? '')) throw new CliError(`A loop name is one short word with hyphens, for example pass-1 or round-2. Got "${name}".`);
  if (!fs.existsSync(loopAskFile(root, name))) throw new CliError(`No ask file for loop ${name}. Write ${loopAskFile(root, name)} first: what this loop tried to do, the moment, the before and after screenshots or numbers, and what changed.`);
  return { name, ask: loopAskFile(root, name), findings: loopFindingsFile(root, name), answers: loopAnswersFile(root, name) };
}

export const loopTask = (root, name) => [
  `You are the independent judge of one loop of work that another AI model just did. Loop ${name}. The builder thinks it went well. Your job is to stop it drifting into bad ideas.`,
  'Question: is this ready to ship? Be brutal. Do not soften, balance or praise to be polite. A free, open-source or MIT-licensed product is shipped to its users. Never count the lack of a price, payments or a sales route against it unless the asks name one.',
  `Project folder. ${root}`,
  `1. Read ${loopAskFile(root, name)} in full. It says what this loop tried to do, how to run the product, and where the before and after proof is.`,
  '2. Decide how this loop must be judged, from what it changed. Say your choice on the first line of your findings: "Judged by: looking" for work a user sees or feels, "Judged by: running" for flows and behavior, "Judged by: numbers" for math, speed and data, "Judged by: words" for text. A loop can need two. Judge that way, not the way that is easiest for you.',
  '3. When the product has a screen, judge with your eyes. Open the before and after contact sheets first, then open the single full-size screenshots: the smallest phone, the landscape phone and the widest desktop, before and after. Look at what a user sees: layout, order, art, spacing, text fitting its box, anything cut off or blurry. Counts are hints only, they are not looking. Never judge a screen from its code: do not read source files for a visual loop.',
  '4. Run it end to end. Start the product with the run command in the ask, do the main flow the ask names, and look at each state you reach. If the ask gives no run command or no flow for a product with a screen, that is a blocker finding: the loop cannot be judged from files alone.',
  '5. When the loop is not visual, judge by doing: run the checks or the flow yourself, with your own inputs, and write the numbers you got. Do not repeat the builder\'s numbers back.',
  '6. If you cannot see images at all, say so as the first finding with severity blocker, and write "Better: no". A visual loop judged blind does not count, and the builder must start a judge that can see.',
  `7. Write your findings to ${loopFindingsFile(root, name)}. That is the only file you may create or change, apart from screenshots under ${loopDir(root, name)}. Short: each finding one or two lines, with a severity word (blocker, major, minor, nit), and what you saw, not what you read.`,
  '8. End the file with exactly these two lines: "Drifting: yes" or "Drifting: no", then "Better: yes" or "Better: no". Drifting means the work is moving away from the moment or into a bad idea, even if each single change looks fine.',
  `9. You have ${LOOP_MINUTES} minutes. Write a first version within 5 minutes and keep it current. Never play sound. Do not edit source, install, commit or start other agents.`,
  '10. When done, reply with the Judged by line, the finding counts, the Drifting and Better lines, and the findings path.',
].join('\n');

export const reviewTask = (root, plan) => [
  `You are the independent judge of a prototype that another AI model built. Round ${plan.slot} of ${plan.total}, ${LENSES[plan.lens]}.`,
  `Project folder. ${root}`,
  `1. Read ${briefFile(root)} in full and follow it exactly.`,
  `2. Write your findings to ${findingsFile(root)}. That is the only file you may create or change, apart from screenshots under ${reviewDir(root)}.`,
  '3. When done, reply with the count of findings per severity, the Ready to ship line, and the findings path.',
].join('\n');

// via says how to start the judge. command means `100x review-run`, harness means the agent app's own subagent tool with
// delegateTask, none means no judge is chosen yet.
// A solo run has no judge to start. The call says where the files are and that the builder judges them itself, so it ignores
// review.enabled: that setting turns off a second model, and a solo run never had one.
function soloCall(root, config, { loop, now }) {
  const base = { enabled: true, solo: true, via: 'self', configured: true, selfReview: true };
  if (loop) {
    const info = checkLoopName(root, loop);
    return { ...base, brief: info.ask, findings: info.findings, deadlineMinutes: LOOP_MINUTES, loop: info, steps: loopSteps({ name: loop, ask: info.ask, findings: info.findings, answers: info.answers, minutes: LOOP_MINUTES }) };
  }
  const total = plannedRounds(root, config.review);
  const status = roundStatus(root, total);
  const room = judgeRoom(root, config, now);
  return { ...base, brief: briefFile(root), findings: findingsFile(root), deadlineMinutes: room.minutes, ...(room.noRoom ? { noRoom: true, minutesLeft: room.left } : {}), round: { slot: status.slot, total, lens: status.lens, held: status.entries.length, open: status.inProgress, done: status.done } };
}

export function reviewCall(root, config, { loop = null, now = new Date() } = {}) {
  if (isSolo(root)) return soloCall(root, config, { loop, now });
  const review = config.review;
  if (!review.enabled) return { enabled: false };
  // A target outside the owner's limits is refused here whether the judge starts through a command or through an app's subagent tool.
  const fallbackTarget = helperTarget(config, 'fallback', 'fallback judge');
  const target = helperTarget(config, 'review', 'judge');
  checkCommand(review.command, review.provider, 'judge', { only: config.helper?.only ?? null });
  // A judge that is the model that builds shares the builder's blind spots, so its look is a self-review. The call says so, and the review note must.
  const sameAsBuilder = sameModel(fallbackTarget?.model, config.builder?.model);
  const fallback = fallbackTarget ? { fallbackTarget, ...(sameAsBuilder ? { fallbackSameAsBuilder: true } : {}) } : {};
  const selfReview = sameModel(target?.model ?? review.model, config.builder?.model) ? { selfReview: true } : {};
  if (loop) {
    const info = checkLoopName(root, loop);
    const base = { enabled: true, brief: info.ask, findings: info.findings, deadlineMinutes: LOOP_MINUTES, loop: info, ...selfReview };
    if (review.command) return { ...base, via: 'command', configured: true, model: target?.model ?? review.model ?? null, ...fallback };
    if (!target) return { ...base, via: 'none', configured: false };
    const delegateTask = { title: `100x loop judge. ${loop}`, role: 'review', isolation: 'shared', mode: 'async', target, task: loopTask(root, loop) };
    return { ...base, via: 'harness', configured: true, delegateTask, startInCursor: cursorRoute(target), ...fallback };
  }
  const total = plannedRounds(root, review);
  const status = roundStatus(root, total);
  const room = judgeRoom(root, config, now);
  const base = { enabled: true, brief: briefFile(root), findings: findingsFile(root), deadlineMinutes: room.minutes, ...selfReview, ...(room.noRoom ? { noRoom: true, minutesLeft: room.left } : {}), round: { slot: status.slot, total, lens: status.lens, held: status.entries.length, open: status.inProgress, done: status.done } };
  if (review.command) return { ...base, via: 'command', configured: true, model: target?.model ?? review.model ?? null, ...fallback };
  if (!target) return { ...base, via: 'none', configured: false };
  const delegateTask = {
    title: `100x review. ${path.basename(root)}`,
    role: 'review',
    isolation: 'shared',
    mode: 'async',
    target,
    task: reviewTask(root, { slot: status.slot ?? total, total, lens: status.lens ?? 'ship' }),
  };
  return { ...base, via: 'harness', configured: true, delegateTask, startInCursor: cursorRoute(target), ...fallback };
}