var e={complexity:.25,historicalFailure:.25,blockingRisk:.3,dependencyDepth:.2},t={low:.3,medium:.6,high:.8},n={small:.2,medium:.5,large:.8};async function r(e,t,n){let r=await t.loadSingleTask(e);if(!r)return{taskId:e,riskScore:0,confidence:0,factors:[],recommendation:`Task ${e} not found — cannot assess risk.`};let u=[],d=0,f=await i(r,t);u.push(f),d+=1;let p=await a(r,n);u.push(p),p.value>0&&(d+=1);let m=await o(r,t);u.push(m),d+=1;let h=await s(r,t);u.push(h),d+=1;let g=c(u),_=Math.min(1,.25+d/4*.75),v=l(g,u);return{taskId:e,riskScore:Math.round(g*1e3)/1e3,confidence:Math.round(_*1e3)/1e3,factors:u,recommendation:v}}async function i(t,r){let i=t.depends?.length??0,a=0;try{a=await r.countChildren(t.id)}catch{}let o=n[t.size??`medium`]??.5,s=Math.min(i/10,1),c=Math.min(a/20,1),l=o*.4+s*.3+c*.3,u=[];return t.size&&u.push(`size=${t.size}`),i>0&&u.push(`${i} dependencies`),a>0&&u.push(`${a} children`),{name:`complexity`,weight:e.complexity,value:Math.round(l*1e3)/1e3,description:u.length>0?`Task complexity based on ${u.join(`, `)}`:`Default complexity (no size/deps/children data)`}}async function a(t,n){let r=await n.findPatterns({type:`failure`,limit:50}),i=await n.findPatterns({type:`blocker`,limit:50}),a=[...r,...i];if(a.length===0)return{name:`historical_failure`,weight:e.historicalFailure,value:0,description:`No historical failure patterns found in brain.db`};let o=0,s=0,c=new Set((t.labels??[]).map(e=>e.toLowerCase())),l=t.title.toLowerCase(),u=(t.description??``).toLowerCase();for(let e of a){let t=e.pattern.toLowerCase(),n=e.context.toLowerCase(),r=c.size>0&&[...c].some(e=>t.includes(e)||n.includes(e)),i=t.includes(l)||l.includes(t.slice(0,20)),a=u.length>10&&(t.includes(u.slice(0,30))||u.includes(t.slice(0,30)));if(r||i||a){o++;let t=e.successRate===null?.5:1-e.successRate;s+=t}}if(o===0)return{name:`historical_failure`,weight:e.historicalFailure,value:0,description:`No matching historical failure patterns for this task`};let d=s/o;return{name:`historical_failure`,weight:e.historicalFailure,value:Math.round(d*1e3)/1e3,description:`${o} matching failure/blocker pattern(s) with avg failure rate ${Math.round(d*100)}%`}}async function o(t,n){let r=0;try{let{tasks:e}=await n.queryTasks({status:[`pending`,`active`,`blocked`]});for(let n of e)n.depends?.includes(t.id)&&r++}catch{}let i=Math.min(r/5,1);return{name:`blocking_risk`,weight:e.blockingRisk,value:Math.round(i*1e3)/1e3,description:r>0?`This task blocks ${r} other task(s)`:`This task does not block any other tasks`}}async function s(t,n){let r=0,i=new Set,a=t;for(;a.depends&&a.depends.length>0&&r<10;){let e=a.depends[0];if(i.has(e))break;i.add(e);let t=await n.loadSingleTask(e);if(!t)break;r++,a=t}let o=0,s=t.parentId,c=new Set;for(;s&&o<10&&!c.has(s);){c.add(s);let e=await n.loadSingleTask(s);if(!e)break;o++,s=e.parentId}let l=r+o,u=Math.min(l/8,1);return{name:`dependency_depth`,weight:e.dependencyDepth,value:Math.round(u*1e3)/1e3,description:`Dependency chain depth: ${r}, hierarchy depth: ${o}`}}function c(e){let t=0,n=0;for(let r of e)n+=r.weight*r.value,t+=r.weight;return t>0?n/t:0}function l(e,n){return e<=t.low?`Low risk. Proceed with normal workflow.`:e<=t.medium?`Moderate risk (${n.filter(e=>e.value>.4).sort((e,t)=>t.value*t.weight-e.value*e.weight).slice(0,2).map(e=>e.name).join(`, `)}). Consider extra review or decomposition.`:e<=t.high?`High risk. Recommend decomposition, additional testing, or pair review before proceeding.`:`Critical risk. Strongly recommend breaking this task into smaller units and addressing blockers first.`}export{r as calculateTaskRisk};