.weft/workflows/audit-and-fix/main.tsabridged
// find bugs → refute across vendors → human gate → fix in worktrees → integrate → test
export default defineWorkflow({
description: "Find bugs, verify across vendors, fix with approval",
input: z.object({ paths: z.array(z.string()) }),
output: z.object({ fixed: z.array(Finding), skipped: z.array(Finding) }),
}, async (ctx, { paths }) => {
ctx.phase("Find");
const found = ctx.successes(await ctx.parallel(paths, (p) =>
ctx.agent(`Find correctness bugs in ${p}. Cite file:line.`, {
schema: z.object({ findings: z.array(Finding) }),
key: `find:${p}`,
})));
const findings = found.flatMap((r) => r.findings); // typed — no nulls
ctx.phase("Verify"); // a different vendor grades
const real = ctx.successes(await ctx.pipeline(findings)
.step((f) => ctx.parallel(["claude", "codex", "claude"], (provider, i) =>
ctx.agent(`Try to refute: ${f.claim} (${f.file}:${f.line})`, {
schema: Verdict, provider,
key: `refute:${f.file}:${f.line}:${i}`,
})))
.filter((votes) => votes.filter((v) => v.real).length >= 2) // majority
.map((_votes, f) => f)
.run());
ctx.phase("Fix");
const go = await ctx.gate({ // a human, durably
action: `Apply fixes for ${real.length} bugs`, risk: "medium",
});
if (!go.approved) return { fixed: [], skipped: real };
const fixes = ctx.successes(await ctx.parallel(real, (f) =>
ctx.agent.detailed(`Fix and add a focused test: ${f.claim}`, {
schema: FixResult,
isolation: "worktree", // own tree, returns a patch
write: { paths: [f.file, "**/*.test.ts"], mode: "warn" },
key: `fix:${f.file}`,
})));
const ledger = await ctx.integrate(fixes, {
order: "sequential", onConflict: "ask",
});
ctx.phase("Check");
await ctx.check("tests", { exec: ["pnpm", "test"], required: true });
return {
fixed: real.filter((f) => ledger.merged.includes(`fix:${f.file}`)),
skipped: [],
};
});