var S=Object.defineProperty;var p=(t,a)=>S(t,"name",{value:a,configurable:!0});import{readLastRunSummary as v,getLastRunSummaryPath as x}from"@visulima/task-runner";import{p as c,E as $,F as l,H as I,I as y}from"./bin.js";var w=Object.defineProperty,n=p((t,a)=>w(t,"name",{value:a,configurable:!0}),"s");const T=new Set(["json","table"]),C=n((t,a)=>t??a,"resolveWorkspaceRoot"),h=n(t=>t===void 0?"-":t<1e3?`${String(t)}ms`:`${(t/1e3).toFixed(2)}s`,"formatDuration"),k=n(t=>t.exitCode===void 0?t.cacheStatus:t.exitCode===0?t.cacheStatus==="MISS"?"OK":t.cacheStatus:`FAIL(${String(t.exitCode)})`,"formatStatus"),o=n((t,a)=>t.length>=a?t:t+" ".repeat(a-t.length),"padCell"),R=n(t=>t.exitCode!==void 0&&t.exitCode!==0,"isFailedTask"),g=n((t,a)=>{a.info(`Run ${t.id}`),a.info(`  start:    ${t.startTime}`),a.info(`  end:      ${t.endTime}`),a.info(`  duration: ${h(t.duration)}`),a.info(`  totals:   ${String(t.stats.total)} total · ${String(t.stats.succeeded)} ok · ${String(t.stats.cached)} cached · ${String(t.stats.skipped)} skipped · ${String(t.stats.failed)} failed`),a.info(`  env:      node ${t.environment.nodeVersion} · ${t.environment.platform}/${t.environment.arch}`),a.info("")},"renderRunHeader"),D=n((t,a)=>{if(t.length===0){a.info("(no tasks match the current filter)");return}const s=t.map(e=>({duration:h(e.duration),hash:e.hash?e.hash.slice(0,12):"-",status:k(e),taskId:e.taskId})),r={duration:Math.max(8,...s.map(e=>e.duration.length)),hash:Math.max(4,...s.map(e=>e.hash.length)),status:Math.max(6,...s.map(e=>e.status.length)),taskId:Math.max(4,...s.map(e=>e.taskId.length))};a.info(`  ${o("task",r.taskId)}  ${o("status",r.status)}  ${o("duration",r.duration)}  ${o("hash",r.hash)}`),a.info(`  ${"-".repeat(r.taskId)}  ${"-".repeat(r.status)}  ${"-".repeat(r.duration)}  ${"-".repeat(r.hash)}`);for(const e of s)a.info(`  ${o(e.taskId,r.taskId)}  ${o(e.status,r.status)}  ${o(e.duration,r.duration)}  ${o(e.hash,r.hash)}`)},"renderTaskTable"),j=n((t,a,s)=>{g(t,s),s.info(`Task ${a.taskId}`),s.info(`  status:    ${k(a)}`),s.info(`  cache:     ${a.cacheStatus}`),s.info(`  duration:  ${h(a.duration)}`),s.info(`  exit:      ${a.exitCode===void 0?"-":String(a.exitCode)}`),s.info(`  hash:      ${a.hash??"(none)"}`),s.info(`  start:     ${a.startTime??"-"}`),s.info(`  end:       ${a.endTime??"-"}`),a.dependencies.length>0&&s.info(`  deps:      ${a.dependencies.join(", ")}`),s.info(""),c.info(`Drill into hash inputs with: vis cache why ${a.taskId} --run ${t.id}`)},"renderTaskDetail"),M=n(t=>{process.stdout.write(`${JSON.stringify(t.map(a=>({id:a.id,mtime:new Date(a.mtimeMs).toISOString(),path:a.path})),void 0,2)}
`)},"renderListJson"),O=n((t,a,s)=>{if(t.length===0){c.info(`No recorded runs found in ${$(a)}/. Run with --summarize to record a run.`);return}const r={id:Math.max(2,...t.map(e=>e.id.length)),mtime:24};s.info(`  ${o("id",r.id)}  ${o("mtime",r.mtime)}`),s.info(`  ${"-".repeat(r.id)}  ${"-".repeat(r.mtime)}`);for(const e of t)s.info(`  ${o(e.id,r.id)}  ${new Date(e.mtimeMs).toISOString()}`)},"renderListTable"),b=n((t,a)=>{let{tasks:s}=t;return a.task!==void 0&&(s=s.filter(r=>r.taskId===a.task)),a.failed&&(s=s.filter(r=>R(r))),s},"filterTasks"),N=n(async(t,a)=>{const{failed:s,format:r,runId:e,task:d,workspaceRoot:u}=t,i=e===void 0?await v(u,{dataDirectory:l(u)}):await I(u,e);if(!i){if(r==="json"){process.stdout.write(`${JSON.stringify({error:"no-summary",runId:e??null},void 0,2)}
`),process.exitCode=1;return}e===void 0?c.error(`No previous run summary found. Run a task first to populate \`${x(u,{dataDirectory:l(u)})}\`.`):c.error(`Run summary "${e}" not found in ${$(u)}/.`),process.exitCode=1;return}const f=b(i,{failed:s,task:d});if(d!==void 0&&f.length===0){if(r==="json"){process.stdout.write(`${JSON.stringify({error:"task-not-in-summary",runId:i.id,taskId:d},void 0,2)}
`),process.exitCode=1;return}c.error(`Task "${d}" was not part of run ${i.id}.`),process.exitCode=1;return}if(i.stats.failed>0&&(process.exitCode=1),r==="json"){process.stdout.write(`${JSON.stringify({duration:i.duration,endTime:i.endTime,environment:i.environment,runId:i.id,startTime:i.startTime,stats:i.stats,tasks:f.map(m=>({cacheStatus:m.cacheStatus,dependencies:m.dependencies,duration:m.duration,endTime:m.endTime,exitCode:m.exitCode,hash:m.hash??null,startTime:m.startTime,taskId:m.taskId}))},void 0,2)}
`);return}if(d!==void 0){j(i,f[0],a);return}g(i,a),D(f,a)},"runReplay"),F=n(async({logger:t,options:a,process:s,workspaceRoot:r})=>{const e=C(r,s.cwd),d=a.format??"table";if(!T.has(d)){c.error(`Invalid --format: ${d}. Expected "table" or "json".`),process.exitCode=1;return}if(a.list===!0){const u=await y(e);if(d==="json"){M(u);return}O(u,e,t);return}await N({failed:a.failed===!0,format:d,runId:a.run,task:a.task,workspaceRoot:e},t)},"replayExecute");export{F as default,F as replayExecute,N as runReplay};
