import{n as e}from"./chunk-DR8-3Aex.js";import{n as t,s as n}from"./envUtils-BWeoiL4Y.js";import{Ct as r,cn as i}from"./state-sIHsFpDu.js";import{B as a,D as o,E as s,O as c,Q as l,U as u,Z as d,l as f,p}from"./debug-otxXY3j-.js";import{join as m}from"path";import{mkdir as h,readFile as g,unlink as _,writeFile as v}from"fs/promises";function y(e){return typeof e!=`object`||!e?!1:`sessionId`in e&&typeof e.sessionId==`string`&&`pid`in e&&typeof e.pid==`number`}function b(){return m(t(),k)}async function x(){try{let e=o(await g(b(),`utf8`));return y(e)?e:void 0}catch{return}}function S(e){try{return process.kill(e,0),!0}catch{return!1}}async function C(e){try{return await v(b(),c(e),{flag:`wx`}),!0}catch(e){if(a(e)===`EEXIST`)return!1;throw e}}function w(){A?.(),A=l(async()=>{await O()})}async function T(){let e=await x();return e?e.sessionId===r()?{kind:`held_by_self`}:S(e.pid)?{kind:`blocked`,by:e.sessionId}:(p(`Recovering stale computer-use lock from session ${e.sessionId} (PID ${e.pid})`),await _(b()).catch(()=>{}),{kind:`free`}):{kind:`free`}}function E(){return A!==void 0}async function D(){let e=r(),n={sessionId:e,pid:process.pid,acquiredAt:Date.now()};if(await h(t(),{recursive:!0}),await C(n))return w(),j;let i=await x();return i?i.sessionId===e?M:S(i.pid)?{kind:`blocked`,by:i.sessionId}:(p(`Recovering stale computer-use lock from session ${i.sessionId} (PID ${i.pid})`),await _(b()).catch(()=>{}),await C(n)?(w(),j):{kind:`blocked`,by:(await x())?.sessionId??`unknown`}):(await _(b()).catch(()=>{}),await C(n)?(w(),j):{kind:`blocked`,by:(await x())?.sessionId??`unknown`})}async function O(){A?.(),A=void 0;let e=await x();if(!e||e.sessionId!==r())return!1;try{return await _(b()),p(`Released computer-use lock`),!0}catch{return!1}}var k,A,j,M,N=e((()=>{i(),d(),f(),n(),s(),u(),k=`computer-use.lock`,j={kind:`acquired`,fresh:!0},M={kind:`acquired`,fresh:!1}}));export{D as a,O as i,N as n,E as r,T as t};