import{d as e}from"./home-dir-C44RaPME-B5iLWwCq.mjs";import{o as t}from"./paths-D92DjaZ--DdtJ-zYp.mjs";import{r as n}from"./delivery-context.shared-j5hC5qb1-BSLQG47q.mjs";import{n as r,t as i}from"./json-file-BQRNuxmK-hRjIgOXI.mjs";import a from"node:path";import o from"node:os";const s=new Map;function c(e,t,n){let r=t.trim();if(!r)return[];let i=n?.requesterRunId?.trim(),a=i?e.get(i):void 0,o=a&&a.childSessionKey===r?a:void 0,s=o?.startedAt??o?.createdAt,c=o?.endedAt;return[...e.values()].filter(e=>!(e.requesterSessionKey!==r||typeof s==`number`&&e.createdAt<s||typeof c==`number`&&e.createdAt>c))}function l(e,t){let n=t.trim();if(!n)return;let r;for(let t of e.values())t.childSessionKey===n&&(!r||t.createdAt>r.createdAt)&&(r=t);return r}function u(e,t){let n=l(e,t);return!!(n&&typeof n.endedAt!=`number`)}function d(e,t){let n=l(e,t);return n?{requesterSessionKey:n.requesterSessionKey,requesterOrigin:n.requesterOrigin}:null}function f(e,t){let n=l(e,t);return!!(n&&n.spawnMode!==`session`&&typeof n.endedAt==`number`&&typeof n.cleanupCompletedAt==`number`&&n.cleanupCompletedAt>=n.endedAt)}function p(e,t,n){let r=t.trim();if(!r)return!1;let i=[r],a=new Set([r]);for(let t=0;t<i.length;t+=1){let r=i[t];if(!r)continue;let o=new Map;for(let[t,n]of e.entries()){if(n.requesterSessionKey!==r)continue;let e=n.childSessionKey.trim(),i=o.get(e);(!i||n.createdAt>i[1].createdAt)&&o.set(e,[t,n])}for(let[t,s]of o.values()){let o=l(e,s.childSessionKey);if(!o||o.runId!==t||o.requesterSessionKey!==r)continue;n(t,s);let c=s.childSessionKey.trim();!c||a.has(c)||(a.add(c),i.push(c))}}return!0}function m(e,t){let n=0;return p(e,t,(e,t)=>{typeof t.endedAt!=`number`&&(n+=1)})?n:0}function h(e,t,n){let r=n?.trim(),i=0;return p(e,t,(e,t)=>{let n=typeof t.endedAt==`number`,a=typeof t.cleanupCompletedAt==`number`;(!n||!a)&&e!==r&&(i+=1)})?i:0}function g(e,t){return h(e,t)}function _(e,t,n){return h(e,t,n)}function v(e=process.env){return e.OPENCLAW_STATE_DIR?.trim()?t(e):e.VITEST||e.NODE_ENV===`test`?a.join(o.tmpdir(),`openclaw-test-state`,String(process.pid)):t(e)}function y(){return a.join(v(process.env),`subagents`,`runs.json`)}function b(){let t=i(y());if(!t||typeof t!=`object`)return new Map;let r=t;if(r.version!==1&&r.version!==2)return new Map;let a=r.runs;if(!a||typeof a!=`object`)return new Map;let o=new Map,s=r.version===1,c=!1;for(let[t,r]of Object.entries(a)){if(!r||typeof r!=`object`)continue;let i=r;if(!i.runId||typeof i.runId!=`string`)continue;let a=s&&typeof i.announceCompletedAt==`number`?i.announceCompletedAt:void 0,l=typeof i.cleanupCompletedAt==`number`?i.cleanupCompletedAt:a,u=typeof i.cleanupHandled==`boolean`?i.cleanupHandled:s?!!(i.announceHandled??l):void 0,d=n(i.requesterOrigin??{channel:e(i.requesterChannel),accountId:e(i.requesterAccountId)}),f=e(i.childSessionKey)?.trim()??``,p=e(i.requesterSessionKey)?.trim()??``,m=e(i.controllerSessionKey)?.trim()||p;if(!f||!p)continue;let{announceCompletedAt:h,announceHandled:g,requesterChannel:_,requesterAccountId:v,...y}=i;o.set(t,{...y,childSessionKey:f,requesterSessionKey:p,controllerSessionKey:m,requesterOrigin:d,cleanupCompletedAt:l,cleanupHandled:u,spawnMode:i.spawnMode===`session`?`session`:`run`}),s&&(c=!0)}if(c)try{x(o)}catch{}return o}function x(e){let t=y(),n={};for(let[t,r]of e.entries())n[t]=r;r(t,{version:2,runs:n})}function S(e){try{x(e)}catch{}}function C(e){let t=b();if(t.size===0)return 0;let n=0;for(let[r,i]of t.entries())!r||!i||e.mergeOnly&&e.runs.has(r)||(e.runs.set(r,i),n+=1);return n}function w(e){let t=new Map;if(process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_DISK===`1`||!process.env.VITEST)try{for(let[e,n]of b().entries())t.set(e,n)}catch{}for(let[n,r]of e.entries())t.set(n,r);return t}function T(e){return m(w(s),e)}function E(e){let t=e.trim();if(!t)return null;let n=null;for(let e of w(s).values())e.childSessionKey===t&&(!n||e.createdAt>n.createdAt)&&(n=e);return n}let D=null;function O(e){D=e.replaceSubagentRunAfterSteer}function k(e){return D?.(e)??!1}export{_ as a,u as c,d,C as f,E as i,c as l,s as m,k as n,g as o,f as p,T as r,w as s,O as t,S as u};