const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./9tMH3l7y2.js","./BFyEzYu52.js","./DrKWWDpL.js","./DsQIgu-4.js","./DTkg6Mnx.js","./DYP3EJMY.js","./ByGkWwzP2.js","./Dn7LRnjg2.js","./DsanjglN.js","./CbGRcr-D.js","./bG-fsHMS.js","./DeO5NWhN.js","./DgZn9uJn.js","./CudpqN6Z.js","./CX8DOZuw2.js","./DOqzs8kl2.js","./DIQmuJJZ.js","./D7w7HllH.js","./DB5ifLM-.js","./CxlOpib82.js","./Czb-RtcG.js","./C0nwgWmB.js","./D-eE9s2f.js","./CWxOfblT.js","./DlZwu-Y7.js","./CvFbqtf9.js"])))=>i.map(i=>d[i]);
import{n as e,t}from"./DsQIgu-4.js";import{a as n,r}from"./bG-fsHMS.js";e();var i=10,a=30,o=300*1e3,s=0,c=!1;function l(e){let t=n();if(!t)return 0;try{return t.prepare(`SELECT COUNT(*) AS cnt FROM brain_observations
         WHERE created_at > ? AND invalid_at IS NULL`).get(e)?.cnt??0}catch{return 0}}function u(){let e=n();if(!e)return null;try{return e.prepare(`SELECT started_at FROM brain_consolidation_events
         ORDER BY started_at DESC LIMIT 1`).get()?.started_at??null}catch{return null}}function d(){let e=n();if(!e)return null;try{return e.prepare(`SELECT created_at FROM brain_retrieval_log ORDER BY created_at DESC LIMIT 1`).get()?.created_at??null}catch{return null}}function f(e){if(!e)return 1/0;let t=e.includes(`T`)?e:e.replace(` `,`T`)+`Z`;return(Date.now()-new Date(t).getTime())/6e4}function p(e){let t=l(u()??`1970-01-01 00:00:00`);return{shouldTrigger:t>=e,newObservationCount:t}}function m(e){let t=d();if(t===null)return{shouldTrigger:!1,idleMinutes:0};let n=f(t);return{shouldTrigger:n>=e,idleMinutes:n}}async function h(e,n,r=!1){if(c)return;c=!0,s=Date.now();let i=async()=>{try{let{runConsolidation:r}=await t(async()=>{let{runConsolidation:e}=await import(`./9tMH3l7y2.js`);return{runConsolidation:e}},__vite__mapDeps([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]),import.meta.url);await r(e,n??null,`scheduled`)}catch(e){console.warn(`[dream-cycle] Consolidation failed:`,e)}finally{c=!1}};r?await i():setImmediate(i).unref()}async function g(e,t={}){let n=t.volumeThreshold??i,l=t.idleThresholdMinutes??a;try{await r(e)}catch{return{triggered:!1,tier:null,skippedReason:`brain.db unavailable`}}let u=Date.now()-s;if(u<o)return{triggered:!1,tier:null,skippedReason:`dream cooldown active (${Math.round(u/1e3)}s since last dream)`};if(c)return{triggered:!1,tier:null,skippedReason:`dream already in flight`};let d=p(n);if(d.shouldTrigger)return await h(e,t.sessionId,t.inline),{triggered:!0,tier:`volume`,newObservationCount:d.newObservationCount};let f=m(l);return f.shouldTrigger?(await h(e,t.sessionId,t.inline),{triggered:!0,tier:`idle`,idleMinutes:f.idleMinutes}):{triggered:!1,tier:null,skippedReason:`volume below threshold (${d.newObservationCount}/${n}); idle below threshold (${Math.round(f.idleMinutes)}/${l} min)`,newObservationCount:d.newObservationCount,idleMinutes:f.idleMinutes}}export{m as n,p as r,g as t};