import{H6 as D,M8 as v,Q8 as F,z6 as I}from"./cli-0.5.3-t65dek0m.mjs";import"./cli-0.5.3-kavpx97n.mjs";import"./cli-0.5.3-9bdwhdys.mjs";import"./cli-0.5.3-ttc72eq3.mjs";import"./cli-0.5.3-g75gdzrp.mjs";import"./cli-0.5.3-1rg2r7n7.mjs";import"./cli-0.5.3-6j2jk6dt.mjs";import"./cli-0.5.3-66650sba.mjs";import{Lea as M,Nea as N}from"./cli-0.5.3-9vh3vpap.mjs";import{Dfa as f,Nfa as _,Qfa as j,qfa as E,vfa as P,zfa as G}from"./cli-0.5.3-n3cxyxpg.mjs";import"./cli-0.5.3-kbvzmaax.mjs";import"./cli-0.5.3-7wdgps7z.mjs";import{Dga as w,Lga as H}from"./cli-0.5.3-c8bcrqa4.mjs";import{gma as k,pia as O}from"./cli-0.5.3-qrvd6cmy.mjs";import"./cli-0.5.3-2dk1sk8w.mjs";import{Xma as U}from"./cli-0.5.3-0df3r78b.mjs";import{createWriteStream as b,writeFileSync as h}from"fs";import{readdir as S,readFile as l,writeFile as c}from"fs/promises";import{join as R}from"path";import{pipeline as d}from"stream/promises";import{getHeapSnapshot as m,getHeapSpaceStatistics as u,getHeapStatistics as n}from"v8";async function g(z,A=0){let $=process.memoryUsage(),x=n(),Q=process.resourceUsage(),T=process.uptime(),X;try{X=u()}catch{}let Y=process._getActiveHandles().length,q=process._getActiveRequests().length,J;try{J=(await S("/proc/self/fd")).length}catch{}let V;try{V=await l("/proc/self/smaps_rollup","utf8")}catch{}let C=$.rss-$.heapUsed,B=T>0?$.rss/T:0,W=B*3600/1048576,K=[];if(x.number_of_detached_contexts>0)K.push(`${x.number_of_detached_contexts} detached context(s) - possible iframe/context leak`);if(Y>100)K.push(`${Y} active handles - possible timer/socket leak`);if(C>$.heapUsed)K.push("Native memory > heap - leak may be in native addons (node-pty, sharp, etc.)");if(W>100)K.push(`High memory growth rate: ${W.toFixed(1)} MB/hour`);if(J&&J>500)K.push(`${J} open file descriptors - possible file/socket leak`);return{timestamp:new Date().toISOString(),sessionId:O(),trigger:z,dumpNumber:A,uptimeSeconds:T,memoryUsage:{heapUsed:$.heapUsed,heapTotal:$.heapTotal,external:$.external,arrayBuffers:$.arrayBuffers,rss:$.rss},memoryGrowthRate:{bytesPerSecond:B,mbPerHour:W},v8HeapStats:{heapSizeLimit:x.heap_size_limit,mallocedMemory:x.malloced_memory,peakMallocedMemory:x.peak_malloced_memory,detachedContexts:x.number_of_detached_contexts,nativeContexts:x.number_of_native_contexts},v8HeapSpaces:X?.map((Z)=>({name:Z.space_name,size:Z.space_size,used:Z.space_used_size,available:Z.space_available_size})),resourceUsage:{maxRSS:Q.maxRSS*1024,userCPUTime:Q.userCPUTime,systemCPUTime:Q.systemCPUTime},activeHandles:Y,activeRequests:q,openFileDescriptors:J,analysis:{potentialLeaks:K,recommendation:K.length>0?`WARNING: ${K.length} potential leak indicator(s) found. See potentialLeaks array.`:"No obvious leak indicators. Check heap snapshot for retained objects."},smapsRollup:V,platform:process.platform,nodeVersion:process.version,ccVersion:"99.0.0"}}async function y(z="manual",A=0){try{let $=O(),x=await g(z,A),Q=(C)=>(C/1024/1024/1024).toFixed(3);_(`[HeapDump] Memory state:
  heapUsed: ${Q(x.memoryUsage.heapUsed)} GB (in snapshot)
  external: ${Q(x.memoryUsage.external)} GB (NOT in snapshot)
  rss: ${Q(x.memoryUsage.rss)} GB (total process)
  ${x.analysis.recommendation}`);let T=v();await G().mkdir(T);let X=A>0?`-dump${A}`:"",Y=`${$}${X}.heapsnapshot`,q=`${$}${X}-diagnostics.json`,J=R(T,Y),V=R(T,q);return await c(V,E(x,null,2),{mode:384}),_(`[HeapDump] Diagnostics written to ${V}`),await o(J),_(`[HeapDump] Heap dump written to ${J}`),M("tengu_heap_dump",{triggerManual:z==="manual",triggerAuto15GB:z==="auto-1.5GB",dumpNumber:A,success:!0}),{success:!0,heapPath:J,diagPath:V}}catch($){let x=w($);return I(x),M("tengu_heap_dump",{triggerManual:z==="manual",triggerAuto15GB:z==="auto-1.5GB",dumpNumber:A,success:!1}),{success:!1,error:x.message}}}async function o(z){if(typeof Bun<"u"){h(z,Bun.generateHeapSnapshot("v8","arraybuffer"),{mode:384}),Bun.gc(!0);return}let A=b(z,{mode:384}),$=m();await d($,A)}var L=U(()=>{k();N();j();H();F();f();D();P()});async function X$(){let z=await y();if(!z.success)return{type:"text",value:`Failed to create heap dump: ${z.error}`};return{type:"text",value:`${z.heapPath}
${z.diagPath}`}}var p=U(()=>{L()});p();export{X$ as call};
