import{s as e}from"./DrKWWDpL.js";import{t}from"./DYP3EJMY.js";import{Qn as n}from"./DIQmuJJZ.js";import"./BSkUUZJr2.js";import"./DOqzs8kl2.js";import"./CxlOpib82.js";import{c as r}from"./CX8DOZuw2.js";import"./kwfd020K.js";import{r as i}from"./DVQI6VOy.js";import{t as a}from"./DTkg6Mnx.js";import"./C0nwgWmB.js";import"./CG84LPq-.js";import"./BmWTAsFW.js";import"./BVC3q6GM.js";import"./BVDTFQLg.js";import"./BKc98lKx.js";import{n as o,t as s}from"./BrUKpZTl.js";import"./LgUi1QRR2.js";var c=e(t(),1);async function l(e,t){let{tasks:n}=await(t??await i(e)).queryTasks({});if(n.length===0)return{path:[],length:0,totalEffort:0,completedInPath:0,remainingInPath:0};let r=new Map(n.map(e=>[e.id,e])),a=new Map,o=new Map;for(let e of n)if(a.has(e.id)||a.set(e.id,new Set),o.has(e.id)||o.set(e.id,new Set),e.depends)for(let t of e.depends)a.get(e.id).add(t),o.has(t)||o.set(t,new Set),o.get(t).add(e.id);let s=new Map;function c(e,t){if(s.has(e))return s.get(e);if(t.has(e))return[e];t.add(e);let n=a.get(e)||new Set,i=[];for(let e of n)if(r.has(e)){let n=c(e,t);n.length>i.length&&(i=n)}t.delete(e);let o=[...i,e];return s.set(e,o),o}let l=n.filter(e=>{let t=o.get(e.id);return!t||t.size===0}),u=[];for(let e of l){let t=c(e.id,new Set);t.length>u.length&&(u=t)}if(u.length===0)for(let e of n){let t=c(e.id,new Set);t.length>u.length&&(u=t)}let d={small:1,medium:3,large:8},f=u.map(e=>{let t=r.get(e),n=t?.size||`medium`,i=(t?.depends||[]).filter(e=>{let t=r.get(e);return t&&t.status!==`done`&&t.status!==`cancelled`});return{taskId:e,title:t?.title||e,status:t?.status||`unknown`,size:n,blockerCount:i.length}}),p=f.filter(e=>e.status===`done`||e.status===`cancelled`).length,m=f.length-p,h=f.filter(e=>e.status!==`done`&&e.status!==`cancelled`).reduce((e,t)=>e+(d[t.size]??3),0);return{path:f,length:u.length,totalEffort:h,completedInPath:p,remainingInPath:m}}a(`worktree:sentinel-index`);var{DatabaseSync:u}=(0,c.createRequire)(import.meta.url)(`node:sqlite`),{DatabaseSync:d}=(0,c.createRequire)(import.meta.url)(`node:sqlite`);Object.freeze({});var{DatabaseSync:f}=(0,c.createRequire)(import.meta.url)(`node:sqlite`);async function p(e,t){try{return(await s(t,{epicId:e})).initialized?{initialized:!1,alreadyInitialized:!0}:(await o(t,{taskId:e,stage:`research`,status:`in_progress`}),{initialized:!0,alreadyInitialized:!1})}catch(e){return{initialized:!1,alreadyInitialized:!1,error:e.message}}}async function m(e){try{let t=r(e);return{success:!0,data:await l(t,await i(t))}}catch(e){return n(`E_GENERAL`,e.message)}}export{p as initLoomForEpic,m as orchestrateCriticalPath};