import{n as e}from"./chunk-DR8-3Aex.js";import{i as t,s as n}from"./envUtils-BWeoiL4Y.js";import{cn as r,ut as i}from"./state-sIHsFpDu.js";import{s as a,u as ee}from"./types-KeqFPzY5.js";import{AO as te,BO as ne,CO as o,D as re,EO as s,FO as c,KO as ie,MO as ae,NO as l,OO as u,PO as d,S as f,SO as p,Sf as oe,TO as m,ZO as se,bf as ce,jO as h,kO as le,w as ue,wO as g,xf as de}from"./loadAgentsDir-C_wFhytF.js";import{G as fe,U as pe,V as me}from"./prompt-6ZKyqv6z.js";import{n as he,r as _}from"./cron-g_P3I5uT.js";import{I as v,P as y}from"./teamHelpers-D8R_gGLb.js";import{a as b,n as x,o as S,t as C}from"./bridgeConfig-pjDV5Z2l.js";import{i as w,o as T}from"./bg-DgpgS_VU.js";import{n as E,t as D}from"./state-BiRT9RAF.js";import{a as O,i as k,n as A,r as j}from"./pipeTransport-EAYXsubu.js";import{join as M}from"path";import"crypto";import{readFile as N,readdir as P}from"fs/promises";function F(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function I(e){return typeof e==`string`&&H.includes(e)}function L(e){return typeof e==`string`&&U.includes(e)}function R(e){return!F(e)||typeof e.name!=`string`||!L(e.status)?null:{name:e.name,...typeof e.prompt==`string`?{prompt:e.prompt}:{},status:e.status,...typeof e.startedAt==`number`?{startedAt:e.startedAt}:{},...typeof e.completedAt==`number`?{completedAt:e.completedAt}:{}}}function ge(e){if(!F(e)||typeof e.runId!=`string`||typeof e.workflow!=`string`||!I(e.status)||typeof e.createdAt!=`number`||typeof e.updatedAt!=`number`||typeof e.currentStepIndex!=`number`||!Array.isArray(e.steps))return null;let t=e.steps.map(R).filter(e=>e!==null);return t.length===e.steps.length?{runId:e.runId,workflow:e.workflow,...typeof e.args==`string`?{args:e.args}:{},status:e.status,createdAt:e.createdAt,updatedAt:e.updatedAt,currentStepIndex:e.currentStepIndex,steps:t}:null}async function _e(e,t){try{return ge(ee(await N(M(e,B,`${t}.json`),`utf-8`),!1))}catch{return null}}async function ve(e=i()){let t;try{t=await P(M(e,B))}catch{return[]}let n=t.filter(e=>e.endsWith(`.json`));return(await Promise.all(n.slice(0,V).map(t=>_e(e,t.slice(0,-5))))).filter(e=>e!==null).sort((e,t)=>t.updatedAt-e.updatedAt)}function z(e){if(e.length===0)return[`Workflow runs: 0`,`  none`].join(`
`);let t=e.filter(e=>e.status===`running`).length,n=e.filter(e=>e.status===`completed`).length,r=e.filter(e=>e.status===`cancelled`).length,i=[`Workflow runs: ${e.length}`,`  Running: ${t}`,`  Completed: ${n}`,`  Cancelled: ${r}`];for(let t of e.slice(0,10)){let e=t.steps[t.currentStepIndex];i.push(`  ${t.runId}: ${t.workflow}: ${t.status} step=${e?.name??`none`} updated=${new Date(t.updatedAt).toLocaleString()}`)}return e.length>10&&i.push(`  ... ${e.length-10} more workflow run(s)`),i.join(`
`)}var B,V,H,U,ye=e((()=>{r(),a(),B=M(`.claude`,`workflow-runs`),V=200,H=[`running`,`completed`,`cancelled`],U=[`pending`,`running`,`completed`,`cancelled`]}));function be(){return M(A(),`registry.json`)}async function xe(){try{let e=await N(be(),`utf8`),t=JSON.parse(e);return t.version===1?t:{...W}}catch{return{...W}}}var W,Se=e((()=>{j(),W={version:1,mainMachineId:null,main:null,subs:[]}}));async function Ce(){return we(await xe())}function we(e){let t=[`Pipe registry: ${+!!e.main} main, ${e.subs.length} sub(s)`];e.mainMachineId&&t.push(`  main_machine=${e.mainMachineId.slice(0,8)}...`),e.main&&t.push(`  [main] ${e.main.pipeName} pid=${e.main.pid} host=${e.main.hostname} tcp=${e.main.tcpPort??`none`}`);for(let n of e.subs.slice(0,10))t.push(`  [sub-${n.subIndex}] ${n.pipeName} pid=${n.pid} host=${n.hostname} bound=${n.boundToMain??`none`} tcp=${n.tcpPort??`none`}`);return!e.main&&e.subs.length===0&&t.push(`  none`),e.subs.length>10&&t.push(`  ... ${e.subs.length-10} more sub pipe(s)`),t.join(`
`)}var Te=e((()=>{Se()}));function Ee(){try{let e=S(),t=C();return[`Remote Control: ${e?`self-hosted`:`official`}`,`  base_url=${x()}`,`  token=${t?`present`:`missing`}`,`  entitlement=checked at remote-control startup`].join(`
`)}catch(e){return[`Remote Control: unknown`,`  reason=${e instanceof Error?e.message:String(e)}`].join(`
`)}}var De=e((()=>{b()}));async function Oe(){try{return(await P(t(),{withFileTypes:!0})).filter(e=>e.isDirectory()).map(e=>e.name).sort()}catch{return[]}}async function ke(){let e=await Oe();if(e.length===0)return[`Teams: 0`,`  none`].join(`
`);let t=[`Teams: ${e.length}`];for(let n of e){let e=k(n),r=(await v(n)).filter(e=>e.status!==`completed`),i=e.filter(e=>e.status===`running`).length,a=e.filter(e=>e.status===`idle`).length;t.push(`  ${n}: teammates=${e.length} running=${i} idle=${a} open_tasks=${r.length}`);for(let n of e.slice(0,5)){let e=r.filter(e=>e.owner===n.name||e.owner===n.agentId);t.push(`    @${n.name}: ${n.status} backend=${n.backendType??`unknown`} mode=${n.mode??`default`} tasks=${e.length}`)}e.length>5&&t.push(`    ... ${e.length-5} more teammate(s)`)}return t.join(`
`)}async function Ae(e){let t=await pe();if(t.length===0)return[`Cron jobs: 0`,`  none`].join(`
`);let n=[`Cron jobs: ${t.length}`];for(let r of t.slice(0,10)){let t=fe(r.cron,e);n.push(`  ${r.id}: ${he(r.cron)} ${r.recurring?`recurring`:`one-shot`} ${r.durable===!1?`session-only`:`durable`} next=${t?new Date(t).toLocaleString():`none`}`)}return t.length>10&&n.push(`  ... ${t.length-10} more job(s)`),n.join(`
`)}async function je(){let e=E(),t=await T(),n=[`Daemon: ${e.status}${e.state?` pid=${e.state.pid} workers=${e.state.workerKinds.join(`,`)}`:``}`,`Background sessions: ${t.length}`];for(let e of t.slice(0,8))n.push(`  pid=${e.pid} kind=${e.kind} status=${e.status??`unknown`} cwd=${e.cwd}`);return t.length>8&&n.push(`  ... ${t.length-8} more session(s)`),n.join(`
`)}function Me(){let e=!1,t=null;try{e=re(),t=f()}catch(e){return[`Auto mode: unknown`,`  reason=${e instanceof Error?e.message:String(e)}`].join(`
`)}return[`Auto mode: ${e?`available`:`unavailable`}`,`  reason=${t??`none`}`].join(`
`)}async function G({runs:e,flows:t,nowMs:n=Date.now()}){return Promise.all([Promise.resolve({id:`auto-mode`,title:`Auto Mode`,content:Me()}),Promise.resolve({id:`runs`,title:`Runs`,content:o(e)}),Promise.resolve({id:`flows`,title:`Flows`,content:h(t)}),Ae(n).then(e=>({id:`cron`,title:`Cron`,content:e})),ve().then(e=>({id:`workflow-runs`,title:`Workflow Runs`,content:z(e)})),ke().then(e=>({id:`teams`,title:`Teams`,content:e})),Ce().then(e=>({id:`pipes`,title:`Pipes`,content:e})),je().then(e=>({id:`runtime`,title:`Runtime`,content:e})),Promise.resolve({id:`remote-control`,title:`Remote Control`,content:Ee()}),oe().then(e=>({id:`remote-trigger`,title:`RemoteTrigger`,content:ce(e)}))])}async function Ne(e){return(await G(e)).map((e,t)=>[t===0?`# Autonomy Deep Status`:`## ${e.title}`,e.content].join(`
`)).join(`

`)}var Pe=e((()=>{D(),w(),l(),g(),n(),ue(),_(),me(),O(),y(),de(),ye(),Te(),De()}));function Fe(e){let[t=`status`,n,r]=e.trim().split(/\s+/,3);return t===``||t===`status`?{type:`status`,deep:n===`--deep`}:t===`runs`?{type:`runs`,limit:n}:t===`flows`?{type:`flows`,limit:n}:t===`flow`?n===`cancel`?r?{type:`flow-cancel`,flowId:r}:{type:`usage`}:n===`resume`?r?{type:`flow-resume`,flowId:r}:{type:`usage`}:n?{type:`flow-detail`,flowId:n}:{type:`usage`}:{type:`usage`}}var K,Ie=e((()=>{K=`Usage: /autonomy [status [--deep]|runs [limit]|flows [limit]|flow <id>|flow cancel <id>|flow resume <id>]`}));function q(e){let t=typeof e==`number`?e:Number.parseInt(e??``,10);return!Number.isFinite(t)||t<=0?10:Math.min(t,50)}async function J(e){let t=e?.rootDir,[n,r]=await Promise.all([m(t),d(t)]);return e?.deep?Ne({runs:n,flows:r}):[o(n),h(r)].join(`
`)}async function Le(e,t){let[n,r]=await Promise.all([m(t?.rootDir),d(t?.rootDir)]),i=(await G({runs:n,flows:r})).find(t=>t.id===e);return i?[`# ${i.title}`,i.content].join(`
`):`Autonomy deep status section not found: ${e}`}async function Re(e){process.stdout.write(`${await J(e)}\n`)}async function Y(e,t){return p(await m(t?.rootDir),q(e))}async function ze(e){process.stdout.write(`${await Y(e)}\n`)}async function X(e,t){return te(await d(t?.rootDir),q(e))}async function Be(e){process.stdout.write(`${await X(e)}\n`)}async function Z(e,t){return le(await ae(e,t?.rootDir))}async function Ve(e){process.stdout.write(`${await Z(e)}\n`)}async function Q(e,t){let n=await c({flowId:e,rootDir:t?.rootDir});if(!n)return`Autonomy flow not found.`;if(!n.accepted)return`Autonomy flow ${e} is already terminal (${n.flow.status}).`;let r=0;if(t?.removeQueuedInMemory){let n=se(t=>t.autonomy?.flowId===e);r=n.length;for(let e of n)e.autonomy?.runId&&await s(e.autonomy.runId,t?.rootDir)}else{for(let e of n.queuedRunIds)await s(e,t?.rootDir);r=n.queuedRunIds.length}return n.flow.status===`running`?`Cancellation requested for flow ${e}. The current step is still running, and no new steps will be started.`:`Cancelled flow ${e}. Removed ${r} queued step(s).`}async function He(e){process.stdout.write(`${await Q(e)}\n`)}async function $(e,t){let n=await u({flowId:e,rootDir:t?.rootDir,currentDir:t?.currentDir});if(!n)return`Autonomy flow is not waiting or was not found.`;if(t?.enqueueInMemory)return ne(n),`Queued the next managed step for flow ${e}.`;let r=n.autonomy?.runId??`unknown`;return[`Prepared the next managed step for flow ${e}.`,`Run ID: ${r}`,``,`Prompt:`,typeof n.value==`string`?n.value:String(n.value)].join(`
`)}async function Ue(e){process.stdout.write(`${await $(e)}\n`)}async function We(e,t){let n=Fe(e);switch(n.type){case`status`:return J({deep:n.deep});case`runs`:return Y(n.limit);case`flows`:return X(n.limit);case`flow-detail`:return Z(n.flowId);case`flow-cancel`:return Q(n.flowId,{removeQueuedInMemory:t?.removeQueuedInMemory});case`flow-resume`:return $(n.flowId,{enqueueInMemory:t?.enqueueInMemory});case`usage`:return K}}var Ge=e((()=>{l(),g(),Pe(),Ie(),ie()}));export{ze as a,We as c,X as d,Y as f,$ as g,q as h,Be as i,Le as l,Ge as m,Ve as n,Re as o,J as p,Ue as r,Q as s,He as t,Z as u};