import{hS as T,sS as c}from"./cli-0.5.4-8hdwp78f.mjs";import{WT as w,ZT as n}from"./cli-0.5.4-z5d3q8kh.mjs";import{O7 as y,P7 as g,Q6 as M,S6 as X,T6 as f}from"./cli-0.5.4-cbq01zg3.mjs";import{Pfa as Z,Sfa as h,cfa as E,efa as S,sfa as K,tfa as v,xfa as m}from"./cli-0.5.4-end8wj9r.mjs";import{$fa as R,mga as F}from"./cli-0.5.4-yd3rd9qt.mjs";import{Gga as Y,Lga as W,Nga as D}from"./cli-0.5.4-sgcaw17t.mjs";import{ima as j,ria as k,via as A,xia as N}from"./cli-0.5.4-jmc3bzbn.mjs";import{Zma as H}from"./cli-0.5.4-aacw1src.mjs";function O(q){if(q<=1)return!1;try{return process.kill(q,0),!0}catch{return!1}}async function i(q,J=10){if(process.platform==="win32"){let Q=`
      $pid = ${String(q)}
      $ancestors = @()
      for ($i = 0; $i -lt ${J}; $i++) {
        $proc = Get-CimInstance Win32_Process -Filter "ProcessId=$pid" -ErrorAction SilentlyContinue
        if (-not $proc -or -not $proc.ParentProcessId -or $proc.ParentProcessId -eq 0) { break }
        $pid = $proc.ParentProcessId
        $ancestors += $pid
      }
      $ancestors -join ','
    `.trim(),V=await X("powershell.exe",["-NoProfile","-Command",Q],{timeout:3000});if(V.code!==0||!V.stdout?.trim())return[];return V.stdout.trim().split(",").filter(Boolean).map((G)=>parseInt(G,10)).filter((G)=>!isNaN(G))}let L=`pid=${String(q)}; for i in $(seq 1 ${J}); do ppid=$(ps -o ppid= -p $pid 2>/dev/null | tr -d ' '); if [ -z "$ppid" ] || [ "$ppid" = "0" ] || [ "$ppid" = "1" ]; then break; fi; echo $ppid; pid=$ppid; done`,z=await X("sh",["-c",L],{timeout:3000});if(z.code!==0||!z.stdout?.trim())return[];return z.stdout.trim().split(`
`).filter(Boolean).map((Q)=>parseInt(Q,10)).filter((Q)=>!isNaN(Q))}function e(q){try{let J=String(q),L=process.platform==="win32"?`powershell.exe -NoProfile -Command "(Get-CimInstance Win32_Process -Filter \\"ProcessId=${J}\\").CommandLine"`:`ps -o command= -p ${J}`,z=M(L,{timeout:1000});return z?z.trim():null}catch{return null}}async function qq(q,J=10){if(process.platform==="win32"){let Q=`
      $currentPid = ${String(q)}
      $commands = @()
      for ($i = 0; $i -lt ${J}; $i++) {
        $proc = Get-CimInstance Win32_Process -Filter "ProcessId=$currentPid" -ErrorAction SilentlyContinue
        if (-not $proc) { break }
        if ($proc.CommandLine) { $commands += $proc.CommandLine }
        if (-not $proc.ParentProcessId -or $proc.ParentProcessId -eq 0) { break }
        $currentPid = $proc.ParentProcessId
      }
      $commands -join [char]0
    `.trim(),V=await X("powershell.exe",["-NoProfile","-Command",Q],{timeout:3000});if(V.code!==0||!V.stdout?.trim())return[];return V.stdout.split("\x00").filter(Boolean)}let L=`currentpid=${String(q)}; for i in $(seq 1 ${J}); do cmd=$(ps -o command= -p $currentpid 2>/dev/null); if [ -n "$cmd" ]; then printf '%s\\0' "$cmd"; fi; ppid=$(ps -o ppid= -p $currentpid 2>/dev/null | tr -d ' '); if [ -z "$ppid" ] || [ "$ppid" = "0" ] || [ "$ppid" = "1" ]; then break; fi; currentpid=$ppid; done`,z=await X("sh",["-c",L],{timeout:3000});if(z.code!==0||!z.stdout?.trim())return[];return z.stdout.split("\x00").filter(Boolean)}var _=H(()=>{f()});import{chmod as u,mkdir as a,readdir as l,readFile as p,unlink as P,writeFile as b}from"fs/promises";import{join as $}from"path";function U(){return $(R(),"sessions")}function o(){return}async function Uq(){if(T()!=null)return!1;let q=o()??"interactive",J=U(),L=$(J,`${process.pid}.json`);E(async()=>{try{await P(L)}catch{}});try{return await a(J,{recursive:!0,mode:448}),await u(J,448),await b(L,K({pid:process.pid,sessionId:k(),cwd:N(),startedAt:Date.now(),kind:q,entrypoint:process.env.CLAUDE_CODE_ENTRYPOINT,...{},...{}})),A((z)=>{B({sessionId:z})}),!0}catch(z){return Z(`[concurrentSessions] register failed: ${Y(z)}`),!1}}async function B(q){let J=$(U(),`${process.pid}.json`);try{let L=v(await p(J,"utf8"));await b(J,K({...L,...q}))}catch(L){Z(`[concurrentSessions] updatePidFile failed: ${Y(L)}`)}}async function Bq(q){if(!q)return;await B({name:q})}async function C(q){await B({bridgeSessionId:q})}async function Mq(){let q=U(),J;try{J=await l(q)}catch(z){if(!W(z))Z(`[concurrentSessions] readdir failed: ${Y(z)}`);return 0}let L=0;for(let z of J){if(!/^\d+\.json$/.test(z))continue;let Q=parseInt(z.slice(0,-5),10);if(Q===process.pid){L++;continue}if(O(Q))L++;else if(y()!=="wsl")P($(q,z)).catch(()=>{})}return L}var I=H(()=>{j();S();h();F();D();_();g();m();c()});function Aq(q){x=q,C(r()??null).catch(()=>{})}function d(){return x}function r(){let q=d();return q?w(q.bridgeSessionId):void 0}var x=null;var t=H(()=>{I();n()});
export{O as OR,i as PR,e as QR,qq as RR,_ as SR,Uq as TR,Bq as UR,C as VR,Mq as WR,I as XR,Aq as YR,d as ZR,r as _R,t as $R};
