import{Bca as v,Jca as F,N8 as I,R8 as N}from"./cli-0.5.12-g4wb8mj2.mjs";import"./cli-0.5.12-vp5smcs1.mjs";import"./cli-0.5.12-y2kq7fe8.mjs";import"./cli-0.5.12-7vxpqr5a.mjs";import"./cli-0.5.12-hvtcdcev.mjs";import"./cli-0.5.12-asbn3d19.mjs";import"./cli-0.5.12-k1crwtdc.mjs";import{dfa as M,ffa as H}from"./cli-0.5.12-2jxq8gnm.mjs";import{Kfa as E,Pfa as f,Tfa as G,Xfa as j,fga as _,iga as D}from"./cli-0.5.12-p5ps8gf0.mjs";import"./cli-0.5.12-hp0508v9.mjs";import{Gga as P,yga as w}from"./cli-0.5.12-tfdwzg37.mjs";import"./cli-0.5.12-60w1a0bs.mjs";import{Aia as O,rma as k}from"./cli-0.5.12-c8f4daey.mjs";import"./cli-0.5.12-0qasc0ef.mjs";import"./cli-0.5.12-f8850v2j.mjs";import{aoa as U}from"./cli-0.5.12-xzeb7mtb.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=I();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 v(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();H();D();P();N();j();F();f()});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};
