var O=Object.defineProperty;var v=(e,t)=>O(e,"name",{value:t,configurable:!0});import{createRequire as q}from"node:module";import{j as $,E as P,e as R,q as D,T as j,I}from"../packem_shared/Table-3pFgIUZ2-DABgc6rj.js";import{z as _}from"../packem_shared/readFileSync-DseCu8sg-DEq4Fn3a.js";import{r as W,a as B,b as L,c as w}from"../packem_shared/ai-fix-TsjDQz_0.js";import{p}from"./bin.js";import{l as N}from"../packem_shared/failure-log-Dxafk3t4.js";const A=q(import.meta.url),m=typeof globalThis<"u"&&typeof globalThis.process<"u"?globalThis.process:process,J=v(e=>{if(typeof m<"u"&&m.versions&&m.versions.node){const[t,r]=m.versions.node.split(".").map(Number);if(t>22||t===22&&r>=3||t===20&&r>=16)return m.getBuiltinModule(e)}return A(e)},"__cjs_getBuiltinModule"),{createInterface:z}=J("node:readline");var M=Object.defineProperty,l=v((e,t)=>M(e,"name",{value:t,configurable:!0}),"c");const U={"ambiguous-match":"ambiguous match",applied:"applied",error:"error","missing-file":"missing file","no-match":"no match","outside-workspace":"outside workspace"},S=l((e,t,r)=>{const o=W(e,t,r),s=_(e,o);return s===""||s.startsWith("..")?o:s},"formatDisplayPath"),E=l((e,t,r)=>{const o=[$(`Fix proposal (${e.provider}, confidence: ${e.confidence})`),"",e.explanation||P("<no explanation>")];if(e.cannotFix)return o.push(""),o.push(R(`Cannot fix automatically: ${e.cannotFix}`)),o.join(`
`);if(e.patches.length===0)return o.push(""),o.push(R("No patches were proposed.")),o.join(`
`);o.push(""),o.push($(`Patches (${String(e.patches.length)}):`));for(const[s,i]of e.patches.entries()){const a=S(t,r,i.file);o.push(""),o.push(D(`[${String(s+1)}] ${a}`)),i.reason&&o.push(P(`    reason: ${i.reason}`));for(const c of i.oldString.split(`
`))o.push(j(`  - ${c}`));for(const c of i.newString.split(`
`))o.push(I(`  + ${c}`))}return o.join(`
`)},"formatProposalText"),C=l((e,t,r)=>{const o=[];for(const s of e){const i=s.absolutePath?_(t,s.absolutePath)||s.absolutePath:S(t,r,s.patch.file),a=U[s.status];s.status==="applied"?o.push(I(`  ✓ ${i}: ${a}`)):o.push(j(`  ✗ ${i}: ${a}${s.error?` (${s.error})`:""}`))}return o.join(`
`)},"formatPatchResultsText"),x=l(e=>{let t=0,r=0;for(const o of e)o.status==="applied"?t+=1:r+=1;return{applied:t,failed:r}},"summarizePatchResults"),G=l(e=>new Promise(t=>{const r=z({input:process.stdin,output:process.stderr});r.question(`${e} (y/N) `,o=>{r.close();const s=o.trim().toLowerCase();t(s==="y"||s==="yes")})}),"confirmPrompt"),H=l(e=>e??process.cwd(),"resolveWorkspaceRoot"),et=l(async({argument:e,logger:t,options:r,visConfig:o,workspaceRoot:s})=>{const i=e[0],a=H(s),c=(r.format??"text")==="json";if(!i){if(c){const f=N(a);process.stdout.write(`${JSON.stringify({availableTasks:f,error:"No task ID specified"},void 0,2)}
`)}else{p.error("No task ID specified. Usage: vis ai fix <project>:<target>");const f=N(a);if(f.length>0){p.info("Tasks with captured failure logs:");for(const h of f)p.info(`  - ${h}`)}else p.notice("No failure logs found. Re-run a failing task with `vis run` to capture logs.")}process.exitCode=1;return}const n=await B(a,i,{runId:r.run});if(!n){c?process.stdout.write(`${JSON.stringify({error:"No failure log or run summary found",taskId:i},void 0,2)}
`):(p.error(`No failure log or run summary found for task "${i}".`),p.notice("Re-run the task with `vis run` so its terminal output and run metadata are captured.")),process.exitCode=1;return}n.terminalOutputCaptured||p.warn(`No captured terminal output for "${i}". Re-run with \`vis run\` for a better fix proposal.`);const F=o?.ai,u=await L(n,t,{cache:r.cache!==!1,config:F});if(!u){c&&process.stdout.write(`${JSON.stringify({error:"AI fix proposal failed or no provider available",taskId:i},void 0,2)}
`),process.exitCode=1;return}const g=r.apply===!0;if(c){const f=g?void 0:await w(a,n.cwd,u,{dryRun:!0}),h=g?await w(a,n.cwd,u):void 0;if(process.stdout.write(`${JSON.stringify({appliedResults:h,dryRunResults:f,failureContext:{cwd:n.cwd,hash:n.hash,runId:n.runId,taskId:n.taskId,terminalOutputCaptured:n.terminalOutputCaptured},proposal:u},void 0,2)}
`),g&&h){const{failed:T}=x(h);T>0&&(process.exitCode=1)}return}if(t.info(E(u,a,n.cwd)),u.cannotFix||u.patches.length===0)return;const k=await w(a,n.cwd,u,{dryRun:!0}),y=x(k);if(t.info(""),t.info($("Patch validation:")),t.info(C(k,a,n.cwd)),!g){t.info(""),p.info("Re-run with --apply to write these patches to disk.");return}if(y.applied===0){p.error("No patches can be applied (every patch failed validation)."),process.exitCode=1;return}if(r.yes!==!0&&(t.info(""),!await G(`Apply ${String(y.applied)} patch${y.applied===1?"":"es"} to disk?`))){p.notice("Aborted. Nothing written.");return}const b=await w(a,n.cwd,u),d=x(b);t.info(""),t.info($("Apply results:")),t.info(C(b,a,n.cwd)),t.info(""),d.failed===0?p.success(`Applied ${String(d.applied)} patch${d.applied===1?"":"es"}.`):(p.warn(`${String(d.applied)} applied, ${String(d.failed)} failed.`),process.exitCode=1)},"aiFix");export{et as aiFix};
