const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./DfVstSkc.js","./BSkUUZJr2.js","./DrKWWDpL.js","./kwfd020K.js","./DIQmuJJZ.js","./D7w7HllH.js","./DB5ifLM-.js","./DYP3EJMY.js","./CxlOpib82.js","./BDDiPmhF.js","./BYJC6_MM.js","./CX8DOZuw2.js","./DsQIgu-4.js","./DOqzs8kl2.js","./C60pM70f.js","./BClSAfhn.js","./BSRD1o6J.js","./DTkg6Mnx.js","./X_DUeLZp2.js","./DmLgVuva2.js","./C0bVU7gr2.js","./Ouh6Dj2I2.js","./Dn7LRnjg2.js","./DsanjglN.js","./CbGRcr-D.js","./bG-fsHMS.js","./DeO5NWhN.js","./DgZn9uJn.js","./CudpqN6Z.js","./Czb-RtcG.js","./C0nwgWmB.js","./D-eE9s2f.js","./CWxOfblT.js","./DlZwu-Y7.js","./CvFbqtf9.js","./-TjUTcZv2.js","./Y-eU8Aa32.js","./Dg_1c8FK2.js","./CG84LPq-.js","./2PQNgpBd.js","./BmWTAsFW.js","./B5XQZRTz.js","./CVwAhlPZ.js","./BVC3q6GM.js","./B8PRMiR2.js","./Cx_-1PHK2.js","./B_DA9RKE2.js","./DOPTvi6T.js","./CvaH7m0y.js","./O2P-f9sv.js","./BQ9wXJVQ2.js","./OnYyYdSr2.js","./ImGf_A8f.js","./DHbhVvym2.js","./BcRT_Mkt.js","./C5cNeu21.js","./H8RQGUV9.js","./BVDTFQLg.js","./vDOh_fWF.js","./DFB29SWc.js"])))=>i.map(i=>d[i]);
import{s as e}from"./DrKWWDpL.js";import{n as t,t as n}from"./DsQIgu-4.js";import{t as r}from"./DYP3EJMY.js";import{Bn as i,d as a,f as o,p as s}from"./DIQmuJJZ.js";import{r as c}from"./ImGf_A8f.js";import{i as l}from"./CG84LPq-.js";import{n as u}from"./Dn7LRnjg2.js";import{r as d,t as f}from"./Dg_1c8FK2.js";import{n as p}from"./-TjUTcZv2.js";import{n as m,t as h}from"./2yUiEINB.js";var g=e(r(),1),_=/(?:^|[\s`"'([\]{,])((\/[\w.\-/]+|[\w.-]+(?:\/[\w.-]+)+)\.(ts|tsx|js|jsx|rs|json))(?=$|[\s`"')[\]{,])/gm,v=/(?<![`"'/\w.])(?:[A-Z][a-zA-Z0-9]{2,}|[a-z][a-zA-Z0-9]*(?:[A-Z][a-zA-Z0-9]*)+|[a-z][a-z0-9]*(?:_[a-z][a-z0-9]*){2,})(?![`"'/\w])/g;function y(e){let t=new Set,n=[];for(let r of e.matchAll(_)){let e=r[1];if(!e)continue;let i=`file:${e}`;t.has(i)||(t.add(i),n.push({raw:e,nodeId:i,nodeType:`file`,label:e}))}for(let r of e.matchAll(v)){let e=r[0];if(!e||e.length<4||b.has(e.toLowerCase()))continue;let i=`symbol:${e}`;t.has(i)||(t.add(i),n.push({raw:e,nodeId:i,nodeType:`symbol`,label:e}))}return n}var b=new Set(`this.that.with.from.into.when.then.also.both.each.such.over.after.before.always.never.should.must.will.would.could.have.been.there.their.they.them.these.those.some.only.just.more.most.many.much.well.very.here.where.which.what.why.how.the.and.but.for.not.are.was.were.has.had.its.the.data.true.false.null.none.type.test.spec.todo.fixme.note.example.index.config.error.value.input.output.result.return.default.source.target.import.export.class.interface.function.const.async.await`.split(`.`));async function x(e,t,n){let r=`decision:${t}`,i=n.map(async t=>{await d(e,t.nodeId,t.nodeType,t.label,.5,t.raw),await f(e,r,t.nodeId,`applies_to`,1,`auto:decision-cross-link`)});await Promise.allSettled(i)}async function S(e,t,n,r){try{let i=y(`${n} ${r}`);if(i.length===0)return;await x(e,t,i)}catch{}}t();var C=.7;async function w(e){try{let{getRawConfigValue:t}=await n(async()=>{let{getRawConfigValue:e}=await import(`./DfVstSkc.js`);return{getRawConfigValue:e}},__vite__mapDeps([0,1,2,3,4,5,6,7,8,9,10,11,12,13]),import.meta.url),r=await t(`decisions.validatorConfidenceThreshold`,e);if(typeof r==`number`&&r>=0&&r<=1)return r}catch{}return C}async function T(e,t){let r={collisions:[],contradictions:[],supersession_graph_violations:[],confidence:1};if({}.CLEO_ENV===`test`||!e.adrPath)return r;let i=[],a=[],o=[],s=(e.decision.trim()+` `+e.rationale.trim()).toLowerCase();for(let e of t){let t=(e.decision.trim()+` `+e.rationale.trim()).toLowerCase(),n=new Set(s.match(/\b\w{4,}\b/g)??[]),r=new Set(t.match(/\b\w{4,}\b/g)??[]),a=[...n].filter(e=>r.has(e)).length,o=new Set([...n,...r]).size;(o>0?a/o:0)>=.65&&i.push(e.id)}if(e.supersedes){let n=t.find(t=>t.id===e.supersedes);n?n.supersedes&&o.push(`supersedes:${e.supersedes}:already-superseded-by:${n.supersedes}`):o.push(`supersedes:${e.supersedes}:not-found`)}let c=1;try{let{evaluateDialectic:r}=await n(async()=>{let{evaluateDialectic:e}=await import(`./C60pM70f.js`);return{evaluateDialectic:e}},__vite__mapDeps([14,15,2,16,5,6,17,7,18,12,19,20,21,22,23,24,25,26,27,28,11,13,4,8,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,9,10,45,46,47,48,49,50,51,52,53,54,55,56,57]),import.meta.url),o=t.length===0?`No existing decisions in the database.`:t.slice(0,20).map(e=>`[${e.id}] ${e.decision}: ${e.rationale}`).join(`
`),s=(await r({userMessage:`Candidate ADR decision for conflict checking:\nType: ${e.type}\nDecision: ${e.decision}\nRationale: ${e.rationale}\n`+(e.adrPath?`ADR path: ${e.adrPath}\n`:``)+(i.length>0?`\nPossible near-duplicates detected: ${i.join(`, `)}\n`:``),systemResponse:`Existing architectural decisions in the system:\n${o}\n\nTask: Identify whether the candidate decision contradicts any existing decisions. Assign a confidence score where 1.0 = no conflicts and 0.0 = severe contradiction.`,activePeerId:`decision-validator`,sessionId:`validate:${(0,g.createHash)(`sha256`).update(e.decision).digest(`hex`).slice(0,8)}`})).peerInsights.filter(e=>e.key.includes(`contradict`)||e.key.includes(`conflict`)||e.key.includes(`collision`));if(s.length>0)for(let e of s){let t=e.value.match(/\bD\d{3,}\b/g)??[];for(let e of t)a.includes(e)||a.push(e);c=Math.min(c,e.confidence)}s.length===0&&(c=Math.max(0,1-i.length*.15))}catch{c=1}let l=o.length*.3;return{collisions:i,contradictions:a,supersession_graph_violations:o,confidence:Math.max(0,c-l)}}var E=16;function D(e){if(!(e instanceof Error))return!1;let t=e.errcode;return(t===1555||t===2067||/UNIQUE constraint failed/i.test(e.message))&&/brain_decisions\.id/.test(e.message)}async function O(e,t){if(!t.decision?.trim())throw Error(`Decision text is required`);if(!t.rationale?.trim())throw Error(`Rationale is required`);if(t.type){let e=s.default.validate([t.type]);if(e.length>0)throw new o(`Invalid decision type '${t.type}'. Valid types: ${a.join(`, `)}. Use 'cleo taxonomy list --axis type' to see all valid type tags.`,e)}if(!t._skipGate&&t.adrPath){let n=await(await u(e)).findDecisions({}),r=await T({decision:t.decision,rationale:t.rationale,type:t.type,adrPath:t.adrPath,supersedes:t.supersedes},n.map(e=>({id:e.id,decision:e.decision,rationale:e.rationale,supersedes:e.supersedes}))),a=await w(e);if(r.confidence<a){let e=[...r.collisions.map(e=>`collision:${e}`),...r.contradictions.map(e=>`contradiction:${e}`),...r.supersession_graph_violations];throw new i(t.decision.trim().slice(0,120),r.confidence,e)}}if(!t._skipGate){let{verifyCandidate:r}=await n(async()=>{let{verifyCandidate:e}=await import(`./vDOh_fWF.js`);return{verifyCandidate:e}},__vite__mapDeps([58,59,2,12,21,20,22,23,24,25,17,7,26,27,28,11,13,4,5,6,8,29,30,31,32,33,34,37]),import.meta.url),i=t.confidence===`high`?.85:t.confidence===`medium`?.65:.45,a=await r(e,{text:(t.decision.trim()+`
`+t.rationale.trim()).toLowerCase(),title:t.decision.trim().slice(0,120),memoryType:`semantic`,tier:`medium`,confidence:i,source:`manual`,sourceConfidence:`owner`,trusted:!0});if(a.action!==`stored`){let t=a.id?await(await u(e)).getDecision(a.id).catch(()=>null):null;if(t)return t}}let r=await u(e),_=(await r.findDecisions({type:t.type})).find(e=>e.decision.toLowerCase()===t.decision.toLowerCase());if(_){let n=new Date().toISOString().replace(`T`,` `).slice(0,19);await r.updateDecision(_.id,{rationale:t.rationale.trim(),confidence:t.confidence,outcome:t.outcome??_.outcome,alternativesJson:t.alternatives?JSON.stringify(t.alternatives):_.alternativesJson,updatedAt:n});let i=await r.getDecision(_.id),a=p({confidence:t.confidence,rationale:t.rationale.trim(),contextTaskId:t.contextTaskId??null});return d(e,`decision:${_.id}`,`decision`,t.decision.trim().substring(0,200),a,t.decision.trim()+t.rationale.trim(),{type:t.type,confidence:t.confidence}).catch(()=>{}),i}let v=t.contextEpicId,y=t.contextTaskId;if(v||y){let t=await l(e);v&&!await c(v,t)&&(v=void 0),y&&!await c(y,t)&&(y=void 0)}let b=`medium`,x=`owner`,C=p({confidence:t.confidence,rationale:t.rationale.trim(),contextTaskId:y??null,sourceConfidence:x,memoryTier:b}),O=(0,g.createHash)(`sha256`).update((t.decision.trim()+`
`+t.rationale.trim()).toLowerCase()).digest(`hex`).slice(0,16),k={id:`D000`,type:t.type,decision:t.decision.trim(),rationale:t.rationale.trim(),confidence:t.confidence,outcome:t.outcome,alternativesJson:t.alternatives?JSON.stringify(t.alternatives):void 0,contextEpicId:v,contextTaskId:y,contextPhase:t.contextPhase,qualityScore:C,memoryTier:b,memoryType:`semantic`,sourceConfidence:x,verified:!0,contentHash:O,adrPath:t.adrPath,supersedes:t.supersedes,confirmationState:t.confirmationState,decidedBy:t.decidedBy},A,j;for(let e=0;e<E;e++)try{A=await r.addDecisionWithSequentialId(k);break}catch(e){if(D(e)){j=e;continue}throw e}if(!A)throw Error(`Failed to allocate a unique brain_decisions.id after ${E} attempts under concurrent writes`+(j instanceof Error?`: ${j.message}`:``));try{await d(e,`decision:${A.id}`,`decision`,A.decision.substring(0,200),C,A.decision+A.rationale,{type:A.type,confidence:A.confidence}),y&&(await d(e,`task:${y}`,`task`,y,1,``),await f(e,`decision:${A.id}`,`task:${y}`,`applies_to`,1,`auto:store-decision`)),v&&(await d(e,`epic:${v}`,`epic`,v,1,``),await f(e,`decision:${A.id}`,`epic:${v}`,`applies_to`,1,`auto:store-decision`)),S(e,A.id,A.decision,A.rationale).catch(()=>{})}catch{}return h(e,{id:A.id,text:A.decision+` `+A.rationale,createdAt:A.createdAt??new Date().toISOString().replace(`T`,` `).slice(0,19)}).then(t=>{for(let n of t)m(e,n.existingId,A.id,`auto:decision-supersedes — high overlap detected at store time`).catch(()=>{})}).catch(()=>{}),A}export{T as n,O as t};