
/**
* Runs the full agent pipeline: parallel research → implementation with
* self-correction → optional parallel review. Only uses the chat model API,
* so it works with any model the extension can select — free or paid.
*/
async function runPipeline(task, config, tools, token, progress, toolInvocationToken) {
	progress.report({ message: `Researching (parallel, ${config.research.map(label).join(" + ")})` });
	const findings = await runResearch(task, config, tools, token, progress);
	progress.report({ message: `Implementing and running tests (model: ${label(config.implement)})...` });
	const { diffSummary, testsPassed, ranTests, turns } = await runImplementation(task, findings, config, tools, token, toolInvocationToken);
	let reviewNote;
	if (config.review?.length) {
		progress.report({ message: `Reviewing the diff (models: ${config.review.map(label).join(" + ")})` });
		const review = await runOptionalReview(diffSummary, config, token);
		if (review?.verdict === "issues") reviewNote = review.notes;
	}
	return {
		diffSummary,
		testsPassed,
		ranTests,
		turns,
		researchAreas: findings.length,
		reviewNote
	};
}
/** Builds the final chat report from the pipeline result. */
function formatReport(result) {
	const lines = [result.researchAreas > 0 ? `Research covered ${result.researchAreas} area(s) in parallel before implementation.` : "No research pre-pass was needed for this task.", `Implementation finished in ${result.turns} turn(s). ` + (result.ranTests ? `Tests ${result.testsPassed ? "passed." : "did NOT pass — check manually."}` : "Tests were not run by the agent — verify manually.")];
	if (result.reviewNote) lines.push(`Second-opinion review flagged something:\n${result.reviewNote}`);
	lines.push("", result.diffSummary);
	return lines.join("\n");
}
/** Human-readable model name for progress messages. */
function label(ref) {
	return ref.id ?? ref.family;
}
