import{s as e}from"./DrKWWDpL.js";import{t}from"./DYP3EJMY.js";import{n}from"./Dn7LRnjg2.js";var r=e(t(),1);function i(){return`AGT-${(0,r.randomBytes)(5).toString(`hex`)}`}function a(){return`P-agt-${(0,r.randomBytes)(4).toString(`hex`)}`}function o(){return`O-agt-${(0,r.randomBytes)(4).toString(`hex`)}`}function s(){return new Date().toISOString().replace(`T`,` `).slice(0,19)}async function c(e,t){let n=i(),r={success:`success`,failure:`failure`,partial:`mixed`},a=`Agent type "${e.agentType}" ${e.outcome===`success`?`successfully completed`:`failed (${e.errorType??`unknown`}) on`} task ${e.taskId} (type: ${e.taskType})`,o=[`Agent: ${e.agentId}`,`Task type: ${e.taskType}`,`Outcome: ${e.outcome}`,e.taskLabels?.length?`Labels: ${e.taskLabels.join(`, `)}`:null,e.errorMessage?`Error: ${e.errorMessage}`:null,e.durationMs==null?null:`Duration: ${e.durationMs}ms`].filter(Boolean).join(` | `),c=JSON.stringify({agentId:e.agentId,agentType:e.agentType,taskType:e.taskType,taskLabels:e.taskLabels??[],errorType:e.errorType??null,durationMs:e.durationMs??null,sessionId:e.sessionId??null});return await t.addDecision({id:n,type:`tactical`,decision:a,rationale:o,confidence:e.outcome===`success`?`high`:e.outcome===`partial`?`medium`:`low`,outcome:r[e.outcome],alternativesJson:c,contextTaskId:e.taskId,contextEpicId:null,contextPhase:null,createdAt:s(),updatedAt:null,decisionCategory:`agent_dispatch`})}async function l(e,t){let n=e.errorType??`unknown`,r=`Agent type "${e.agentType}" fails on task type "${e.taskType}" with ${n} error`,i=`Failure pattern detected: ${e.agentType} agent encountering ${n} errors on ${e.taskType} tasks`,o=(await t.findPatterns({type:`failure`,limit:200})).find(e=>e.pattern===r);if(o){let r=o.frequency+1,i=p(e.agentType,e.taskType,n,r);return await t.updatePattern(o.id,{frequency:r,successRate:0,mitigation:i}),{...o,frequency:r,successRate:0,mitigation:i}}let c=a(),l=s(),u=p(e.agentType,e.taskType,n,1);return t.addPattern({id:c,type:`failure`,pattern:r,context:i,frequency:1,successRate:0,impact:`medium`,antiPattern:`${e.agentType} assigned to ${e.taskType} task with ${n} error risk`,mitigation:u,examplesJson:JSON.stringify([e.taskId]),extractedAt:l,updatedAt:null})}async function u(e,t,n){let r=o(),i=s();return n.addObservation({id:r,type:`change`,title:`Healing strategy: ${e.agentType} on ${e.taskType}`,subtitle:`Error type: ${e.errorType??`unknown`}`,narrative:t,factsJson:JSON.stringify([`Agent: ${e.agentType}`,`Task type: ${e.taskType}`,`Error: ${e.errorMessage??`unspecified`}`,`Suggested action: ${t}`]),conceptsJson:JSON.stringify([`self-healing`,`agent-execution`,e.agentType,e.taskType]),project:null,filesReadJson:null,filesModifiedJson:null,sourceSessionId:e.sessionId??null,sourceType:`agent`,contentHash:null,discoveryTokens:null,createdAt:i,updatedAt:null})}async function d(e,t,n){let r=await n.findPatterns({type:`failure`,limit:200}),i=`Agent type "${e}" fails on task type "${t}"`;return r.filter(e=>e.pattern.startsWith(i)&&e.mitigation).map(e=>({patternId:e.id,failurePattern:e.pattern,frequency:e.frequency,suggestion:e.mitigation,confidence:Math.min(.3+e.frequency*.1,.9)})).sort((e,t)=>t.frequency-e.frequency)}async function f(e,t){let r={decisionId:null,patternId:null,observationId:null,healingSuggestions:[]};try{let i=await n(t),a=await c(e,i);if(a&&(r.decisionId=a.id),e.outcome===`failure`){let t=await l(e,i);if(t&&(r.patternId=t.id,t.frequency>=3&&t.mitigation)){let n=await u(e,t.mitigation,i);n&&(r.observationId=n.id)}}e.outcome!==`success`&&(r.healingSuggestions=await d(e.agentType,e.taskType,i))}catch{}return r}function p(e,t,n,r){return n===`retriable`?`Retry with exponential backoff. Consider switching to a different ${e} agent instance if failures exceed threshold (seen ${r}x).`:n===`permanent`?`Reassign task to a different agent type — ${e} has a permanent failure on ${t} tasks. Check permissions, inputs, and dependencies (seen ${r}x).`:r>=5?`High-frequency unknown failure (${r}x): escalate to orchestrator for manual investigation of ${e} on ${t} tasks.`:`Unknown failure for ${e} on ${t} tasks (${r}x). Check agent logs, retry with fresh context, or reassign to a different agent type.`}export{f as processAgentLifecycleEvent};