{"version":3,"file":"iterate.d.ts","sourceRoot":"","sources":["../src/iterate.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEpD,MAAM,WAAW,cAAc;IAC9B,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,iBAAiB,CAAC;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CAChC;AAED,MAAM,WAAW,aAAa;IAC7B,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;CACvB;AAED,wBAAsB,UAAU,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC,CAmFhF","sourcesContent":["import { createPiRunner } from \"./pi-runner.js\";\nimport { createPiTextCaller } from \"./pi-text-caller.js\";\nimport { runWorker } from \"./worker.js\";\nimport { loadProjectTasks } from \"./tasks.js\";\nimport { generateNextTask } from \"./task-generator.js\";\nimport type { ContributionStore } from \"./store.js\";\n\nexport interface IterateOptions {\n\tprojectDir: string;\n\tstore: ContributionStore;\n\tgoalPath: string;\n\tmodel: string;\n\tpiBin: string;\n\tmaxTasks: number;\n\tmaxAttempts: number;\n\ttimeMs: number | null;\n\tproductDir?: string;\n\tlog?: (message: string) => void;\n}\n\nexport interface IterateReport {\n\ttasksGenerated: number;\n\tattempts: number;\n\tstoppedBecause: string;\n}\n\nexport async function runIterate(options: IterateOptions): Promise<IterateReport> {\n\tconst say = options.log ?? (() => {});\n\tconst runner = createPiRunner({ piBin: options.piBin, model: options.model });\n\tlet timeUp = false;\n\tlet stopTimer: NodeJS.Timeout | null = null;\n\tif (options.timeMs !== null) {\n\t\tstopTimer = setTimeout(() => {\n\t\t\ttimeUp = true;\n\t\t\tsay(`time budget reached. Asking the running agent to commit and stop.`);\n\t\t\trunner.signalStop();\n\t\t}, options.timeMs);\n\t}\n\tlet tasksGenerated = 0;\n\tlet attempts = 0;\n\tlet stoppedBecause = \"unknown\";\n\tconst generateOne = async (hint?: string) => {\n\t\tconst caller = createPiTextCaller(options.piBin, options.model);\n\t\ttry {\n\t\t\treturn await generateNextTask({\n\t\t\t\tprojectDir: options.projectDir,\n\t\t\t\tgoalPath: options.goalPath,\n\t\t\t\tstore: options.store,\n\t\t\t\tcaller,\n\t\t\t\t...(options.productDir ? { productDir: options.productDir } : {}),\n\t\t\t\t...(hint ? { hint } : {}),\n\t\t\t});\n\t\t} catch (error) {\n\t\t\treturn { status: \"unparseable\", raw: error instanceof Error ? error.message : String(error) } as const;\n\t\t}\n\t};\n\tfor (;;) {\n\t\tconst report = await runWorker({\n\t\t\tstore: options.store,\n\t\t\ttasks: loadProjectTasks(options.projectDir),\n\t\t\trunner,\n\t\t\tmaxAttempts: options.maxAttempts,\n\t\t\tshouldContinue: options.timeMs === null ? undefined : () => !timeUp,\n\t\t});\n\t\tattempts += report.attempts.length;\n\t\tfor (const attempt of report.attempts) {\n\t\t\tsay(`${attempt.task}: ${attempt.passed ? \"verified\" : \"failed\"} — ${attempt.summary.slice(0, 120)}`);\n\t\t}\n\t\tif (timeUp) {\n\t\t\tstoppedBecause = \"time budget reached\";\n\t\t\tbreak;\n\t\t}\n\t\tif (tasksGenerated + 1 > options.maxTasks) {\n\t\t\tstoppedBecause = \"max-tasks reached\";\n\t\t\tbreak;\n\t\t}\n\t\tsay(\"task pool dry. Generating the next task from the goal.\");\n\t\tconst result = await generateOne();\n\t\tif (result.status === \"installed\") {\n\t\t\ttasksGenerated++;\n\t\t\tsay(`generated task ${tasksGenerated}/${options.maxTasks}: ${result.task.name}`);\n\t\t\tcontinue;\n\t\t}\n\t\tif (result.status === \"complete\") {\n\t\t\tdescribeStop(result, say);\n\t\t\tstoppedBecause = result.status;\n\t\t\tbreak;\n\t\t}\n\t\t// Retryable: unparseable, duplicate-name, already-satisfied. Retry once with a hint.\n\t\tconst hint = hintFor(result);\n\t\tsay(`generation returned ${result.status}. Retrying once with a hint.`);\n\t\tconst retry = await generateOne(hint);\n\t\tif (retry.status === \"installed\") {\n\t\t\ttasksGenerated++;\n\t\t\tsay(`generated task ${tasksGenerated}/${options.maxTasks}: ${retry.task.name}`);\n\t\t\tcontinue;\n\t\t}\n\t\tif (retry.status === \"unparseable\") {\n\t\t\tsay(`generator response still unparseable. Raw response (first 400 chars):\\n${retry.raw.slice(0, 400)}`);\n\t\t} else {\n\t\t\tdescribeStop(retry, say);\n\t\t}\n\t\tstoppedBecause = retry.status;\n\t\tbreak;\n\t}\n\tif (stopTimer !== null) {\n\t\tclearTimeout(stopTimer);\n\t}\n\treturn { tasksGenerated, attempts, stoppedBecause };\n}\n\nfunction hintFor(result: { status: string; name?: string; raw?: string }): string {\n\tif (result.status === \"duplicate-name\") {\n\t\treturn `A task named '${result.name}' already exists. Propose a DIFFERENT task toward the goal.`;\n\t}\n\tif (result.status === \"already-satisfied\") {\n\t\treturn `The proposed task '${result.name}' already passes against the current product. Propose NEW work that moves the goal forward.`;\n\t}\n\tconst analysis = (result.raw ?? \"\").slice(0, 600);\n\tif (analysis.length > 0 && !analysis.trim().startsWith(\"{\")) {\n\t\treturn `Your previous response was analysis, not JSON. Convert THAT analysis into the task. Output ONLY the JSON object, first character { last character }.\\nYour previous analysis:\\n${analysis}`;\n\t}\n\treturn \"Your previous response was not parseable JSON. Respond with ONLY the JSON object for the next task.\";\n}\n\nfunction describeStop(result: { status: string; name?: string }, say: (m: string) => void): void {\n\tif (result.status === \"complete\") {\n\t\tsay(\"generator reports the goal is complete.\");\n\t} else if (result.status === \"already-satisfied\") {\n\t\tsay(`generated task '${result.name}' already passes against the current product. No new work encoded.`);\n\t} else if (result.status === \"duplicate-name\") {\n\t\tsay(`generated task name '${result.name}' already exists. Stopping.`);\n\t}\n}\n"]}