import"./paths-D92DjaZ--DdtJ-zYp.mjs";import"./io-CH2g3vsv-JJtUIa_U.mjs";import{St as e}from"./bundled-compat-BpaDaAAu-DJtrIX6i.mjs";import"./config-Bim2pi8a-03N-28sw.mjs";import"node:fs";import"node:path";import"node:process";function t(){let e=globalThis;return e.__openclawDiagnosticEventsState||={enabled:!0,seq:0,listeners:new Set,dispatchDepth:0},e.__openclawDiagnosticEventsState}function n(){return t().enabled}function r(e){let n=t();if(!n.enabled)return;if(n.dispatchDepth>100){console.error(`[diagnostic-events] recursion guard tripped at depth=${n.dispatchDepth}, dropping type=${e.type}`);return}let r={...e,seq:n.seq+=1,ts:Date.now()};n.dispatchDepth+=1;for(let e of n.listeners)try{e(r)}catch(e){let t=e instanceof Error?e.stack??e.message:typeof e==`string`?e:String(e);console.error(`[diagnostic-events] listener error type=${r.type} seq=${r.seq}: ${t}`)}--n.dispatchDepth}const i=e(`diagnostic`),a=new Map,o=2e3;let s=0;function c(e=Date.now(),t=!1){let n=a.size>o;if(!t&&!n&&e-s<6e4)return;s=e;for(let[t,n]of a.entries()){let r=e-n.lastActivity;n.state===`idle`&&n.queueDepth<=0&&r>18e5&&a.delete(t)}if(a.size<=o)return;let r=a.size-o,i=Array.from(a.entries()).toSorted((e,t)=>e[1].lastActivity-t[1].lastActivity);for(let e=0;e<r;e+=1){let t=i[e]?.[0];if(!t)break;a.delete(t)}}function l({sessionKey:e,sessionId:t}){return e??t??`unknown`}function u(e){for(let t of a.values())if(t.sessionId===e)return t}function d(e){c();let t=l(e),n=a.get(t)??(e.sessionId&&u(e.sessionId));if(n)return e.sessionId&&(n.sessionId=e.sessionId),e.sessionKey&&(n.sessionKey=e.sessionKey),n;let r={sessionId:e.sessionId,sessionKey:e.sessionKey,lastActivity:Date.now(),state:`idle`,queueDepth:0};return a.set(t,r),c(Date.now(),!0),r}const f=1024*1024;1536*f,3072*f,1024*f,2048*f,512*f,1024*f;function p(e){if(!n())return;let t=d(e);t.queueDepth+=1,t.lastActivity=Date.now(),i.isEnabled(`debug`)&&i.debug(`message queued: sessionId=${t.sessionId??`unknown`} sessionKey=${t.sessionKey??`unknown`} source=${e.source} queueDepth=${t.queueDepth} sessionState=${t.state}`),r({type:`message.queued`,sessionId:t.sessionId,sessionKey:t.sessionKey,channel:e.channel,source:e.source,queueDepth:t.queueDepth})}function m(e){if(!n())return;let t=`tool loop: sessionId=${e.sessionId??`unknown`} sessionKey=${e.sessionKey??`unknown`} tool=${e.toolName} level=${e.level} action=${e.action} detector=${e.detector} count=${e.count}${e.pairedToolName?` pairedTool=${e.pairedToolName}`:``} message="${e.message}"`;e.level===`critical`?i.error(t):i.warn(t),r({type:`tool.loop`,sessionId:e.sessionId,sessionKey:e.sessionKey,toolName:e.toolName,level:e.level,action:e.action,detector:e.detector,count:e.count,message:e.message,pairedToolName:e.pairedToolName})}export{i,m as n,d as r,p as t};