openclaw-code-agent 4.7.13 → 4.7.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/chunks/agent-pr.js +1 -1
- package/dist/chunks/chunk.js +36 -36
- package/dist/index.js +158 -153
- package/npm-shrinkwrap.json +5 -5
- package/openclaw.plugin.json +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,224 +1,229 @@
|
|
|
1
|
-
import{$ as Zo,A as de,Aa as qt,B as br,C as Rr,D as Ar,E as Rt,F as At,G as It,H as Tt,I as zo,J as Ho,K as C,L as D,M as z,N as Qo,O as xt,P as Yo,Pa as Vt,Q as Ir,R as L,S as Ct,T as Je,U as Mt,V as Jo,W as Xo,X as Ee,Y as oe,Z as Xe,_ as _t,a as Te,aa as H,b as xe,ba as Tr,c as wr,ca as xr,d as qo,da as Ze,e as Ce,ea as et,f as ue,fa as B,g as Z,ga as Dt,h as le,ha as ei,i as Ye,ia as tt,j as Vo,ja as ie,k as _,ka as Ne,l as Uo,la as ti,m as Y,ma as $t,n as Me,na as nt,o as Pt,oa as Et,p as ce,pa as Nt,q as _e,qa as Oe,r as wt,ra as Ot,s as bt,sa as ni,t as Go,ta as Wt,u as De,ua as Ft,v as $e,va as ri,w as jo,wa as Lt,x as p,xa as Bt,y as k,ya as Kt,z as $,za as Se}from"./chunks/chunk.js";import{readdirSync as hf,statSync as yf,rmSync as Sf}from"fs";import{execFileSync as kf}from"child_process";import{join as Bo}from"path";var oi={name:"openclaw-code-agent",version:"4.7.13",type:"module",packageManager:"pnpm@11.15.1",main:"dist/index.js",scripts:{"audit:prod":"pnpm audit --prod",build:"esbuild index.ts --bundle --splitting --platform=node --target=node22 --format=esm --outdir=dist --entry-names=index --chunk-names=chunks/[name] --minify --charset=utf8 --pure:console.log --pure:console.warn --pure:console.info --pure:console.debug --external:@anthropic-ai/claude-agent-sdk --external:openclaw/plugin-sdk/plugin-entry","generate:npm-shrinkwrap":"node scripts/generate-npm-shrinkwrap.mjs","check-static-guardrails":"node scripts/check-static-guardrails.mjs && node scripts/check-npm-shrinkwrap.mjs","check-plugin-security":"node scripts/check-plugin-security.mjs",test:"node scripts/run-tests.mjs","test:integ:crabbox":"node scripts/run-tests.mjs tests/oca-codex-crabbox-integ.test.ts tests/oca-plugin-workflows-integ.test.ts tests/oca-codex-telegram-proof.test.ts tests/oca-codex-proof-app-server.test.ts tests/oca-codex-proof-workflow.test.ts","test:serial":"node scripts/run-tests.mjs","test:file":"node scripts/run-tests.mjs","smoke:backend":"node scripts/run-tests.mjs tests/codex-harness.test.ts tests/plan-mode-e2e.test.ts tests/session-restore-service.test.ts","smoke:backend-parity":"node scripts/run-tests.mjs tests/backend-contract-smoke.test.ts tests/codex-harness.test.ts tests/harness-registry.test.ts","smoke:codex-worktrees":"node scripts/run-tests.mjs tests/codex-harness.test.ts tests/session-bootstrap.test.ts tests/session-restore-service.test.ts","smoke:codex-live":"OPENCLAW_RUN_LIVE_CODEX_SMOKE=1 node scripts/run-tests.mjs tests/live-codex-app-server-smoke.test.ts","proof:codex-doctor":"node --import tsx scripts/e2e/oca-codex-telegram-proof.ts doctor","proof:codex-local":"node --import tsx scripts/e2e/oca-codex-telegram-proof.ts local-smoke","proof:codex-telegram":"node scripts/e2e/run-oca-codex-telegram-proof.mjs","smoke:codex-release":"OPENCLAW_RUN_LIVE_CODEX_RELEASE_SMOKE=1 node scripts/run-tests.mjs tests/live-codex-app-server-smoke.test.ts","smoke:opencode-live":"OPENCLAW_RUN_LIVE_OPENCODE_SMOKE=1 node scripts/run-tests.mjs tests/live-opencode-server-smoke.test.ts",typecheck:"tsc -p tsconfig.typecheck.json --noEmit","validate:release-metadata":"node scripts/validate-release-metadata.mjs","verify:npm-consumer":"node scripts/verify-npm-consumer-install.mjs",verify:"pnpm check-static-guardrails && pnpm typecheck && pnpm build && pnpm test"},openclaw:{extensions:["./dist/index.js"],install:{npmSpec:"openclaw-code-agent",defaultChoice:"npm",minHostVersion:">=2026.9.1"},compat:{pluginApi:">=2026.8.1",minGatewayVersion:"2026.8.1"},build:{openclawVersion:"2026.9.1",pluginSdkVersion:"2026.9.1"}},files:["dist/","skills/","openclaw.plugin.json","npm-shrinkwrap.json","README.md","LICENSE"],dependencies:{"@anthropic-ai/claude-agent-sdk":"0.3.251","@anthropic-ai/sdk":"0.122.0","@hono/node-server":"2.1.1","@modelcontextprotocol/sdk":"1.30.0","express-rate-limit":"8.7.0","fast-uri":"3.1.6",hono:"4.12.34","ip-address":"10.7.0",qs:"6.16.0",zod:"4.5.4"},devDependencies:{"@types/node":"^26.0.1",esbuild:"^0.28.1",nanoid:"^6.0.0",openclaw:"2026.9.1",tsx:"^4.21.0",typescript:"^7.0.2"},keywords:["openclaw","plugin","claude","claude-code","opencode","coding-agent","codex","ai","anthropic","agent","code-generation","automation","cli","background-process","developer-tools"],repository:{type:"git",url:"git+https://github.com/goldmar/openclaw-code-agent.git"},homepage:"https://github.com/goldmar/openclaw-code-agent#readme",bugs:{url:"https://github.com/goldmar/openclaw-code-agent/issues"},license:"MIT",author:"Mark Goldenstein",publishConfig:{access:"public",provenance:!0},description:"An OpenClaw plugin that orchestrates coding agent sessions as managed background processes. Launch, monitor, and control coding agents (Claude Code, Codex, experimental OpenCode, and more) directly from your AI gateway.",peerDependencies:{openclaw:">=2026.8.1"}};import{execFile as xl}from"node:child_process";import{mkdirSync as Cl,readFileSync as Ml,renameSync as _l,writeFileSync as Dl}from"node:fs";import{dirname as $l,join as El}from"node:path";import{promisify as Nl}from"node:util";import*as pi from"child_process";import{createHash as fl}from"crypto";var W="code-agent";var gl=new Set(["1","true","yes","on"]);function Mr(){let n=process.env.OPENCLAW_CODE_AGENT_BUTTON_DIAGNOSTICS?.trim().toLowerCase();return!!(n&&gl.has(n))}function K(n){if(!(typeof n!="string"||n.length===0))return fl("sha256").update(n).digest("hex").slice(0,12)}function ii(n){return typeof n.callbackData=="string"?n.callbackData:typeof n.value=="string"?n.value:void 0}function E(n){let e=(n??[]).filter(r=>Array.isArray(r)&&r.length>0),t=e.flat();return{buttonRows:e.length||void 0,buttonCount:t.length||void 0,buttonLabels:t.length>0?t.map(r=>typeof r.label=="string"?r.label:void 0):void 0,callbackByteLengths:t.length>0?t.map(r=>{let o=ii(r);return o?Buffer.byteLength(o,"utf8"):void 0}):void 0,callbackHashes:t.length>0?t.map(r=>K(ii(r))):void 0,buttonStyles:t.length>0?t.map(r=>typeof r.style=="string"?r.style:void 0):void 0}}function pe(n){return n&&typeof n=="object"&&!Array.isArray(n)?n:void 0}function ai(n){let e=pe(n),t=Array.isArray(e?.blocks)?e.blocks:void 0;if(t)return t.map(r=>pe(r)?.type).filter(r=>typeof r=="string"&&r.length>0)}function li(n){let e=pe(n),t=Array.isArray(e?.blocks)?e.blocks:void 0;if(!t)return;let r=t.map(o=>{let i=pe(o);return i?.type!=="buttons"||!Array.isArray(i.buttons)?[]:i.buttons}).filter(o=>o.length>0);return r.length>0?r:void 0}function _r(n){let e=li(n);return{presentationBlockTypes:ai(n),...E(e)}}function Dr(n){let e=pe(n),t=e?.interactive,r=pe(e?.channelData),o=pe(r?.telegram),i=Array.isArray(o?.buttons)?o.buttons:void 0;return{renderedPayloadKeys:e?Object.keys(e).sort():void 0,interactiveBlockTypes:ai(t),...si("interactive",E(li(t))),...si("telegramChannelData",E(i)),hasPresentation:!!e?.presentation,hasInteractive:!!t,hasTelegramChannelDataButtons:!!i?.some(s=>s.length>0)}}function di(n){let e=pe(n);return e?{sendResultKeys:Object.keys(e).sort(),messageId:Cr(e.messageId),chatId:Cr(e.chatId),telegramThreadId:Cr(e.messageThreadId??e.threadId),channel:typeof e.channel=="string"?e.channel:void 0}:{sendResultType:typeof n}}function Cr(n){return typeof n=="string"||typeof n=="number"?n:void 0}function si(n,e){let t={};for(let[r,o]of Object.entries(e))o!==void 0&&(t[`${n}${r.charAt(0).toUpperCase()}${r.slice(1)}`]=o);return t}function M(n,e){if(!Mr())return;let t=Object.fromEntries(Object.entries(e).filter(([,r])=>r!==void 0));`${JSON.stringify({event:n,callbackNamespace:W,...t})}`}var ui=5e3,mi=64,hl={primary:"primary",success:"success",positive:"success",danger:"danger",destructive:"danger",negative:"danger"},yl={execFile:pi.execFile},me=class extends Error{constructor(e){super(e),this.name="MissingRuntimeDirectNotificationCapabilityError"}},We=class{constructor(e=yl.execFile){this.execFile=e}execFile;async send(e,t,r){M("direct_send_started",{...J(e),messageTextLength:t.length,...E(r)});try{await this.sendViaRuntime(e,t,r),M("direct_send_succeeded",{...J(e),deliveryPath:"runtime",...E(r)});return}catch(o){if(!(o instanceof me))throw M("direct_send_failed",{...J(e),deliveryPath:"runtime",...E(r),error:$r(o)}),o;M("direct_send_runtime_capability_missing",{...J(e),deliveryPath:"runtime",...E(r),reason:o.message}),await this.sendViaBoundedCliFallback(e,t,r,o.message),M("direct_send_succeeded",{...J(e),deliveryPath:"cli-fallback",...E(r)})}}async sendViaRuntime(e,t,r){let o=xe(),i=qo(),s=o?.channel?.outbound?.loadAdapter;if(!o)throw new me("OpenClaw plugin runtime is unavailable for direct notification delivery");if(i==null)throw new me("OpenClaw runtime config snapshot is unavailable for direct notification delivery");if(!o.channel)throw new me(`OpenClaw plugin runtime channel surface is unavailable for direct notification delivery (runtimeVersion=${Er(o)})`);if(!o.channel.outbound)throw new me(`OpenClaw plugin runtime channel outbound surface is unavailable for direct notification delivery (runtimeVersion=${Er(o)})`);if(!s)throw new me(`OpenClaw plugin runtime channel outbound loadAdapter is unavailable for direct notification delivery (runtimeVersion=${Er(o)})`);let a=await s(e.channel);if(!a)throw new Error(`OpenClaw outbound adapter for channel "${e.channel}" is unavailable`);let l=jt(r);if(M("runtime_adapter_loaded",{...J(e),adapterHasRenderPresentation:typeof a.renderPresentation=="function",adapterHasSendPayload:typeof a.sendPayload=="function",adapterHasSendText:typeof a.sendText=="function",...E(r),...l?_r(l):{}}),l){await this.sendPresentation(a,i,e,t,l);return}if(!a.sendText)throw new Error(`OpenClaw outbound adapter for channel "${e.channel}" cannot send text`);await a.sendText({cfg:i,to:e.target,text:t,accountId:e.accountId,threadId:e.threadId})}async sendPresentation(e,t,r,o,i){let s=kl(i),a={text:o,presentation:i,interactive:s};M("presentation_render_started",{...J(r),messageTextLength:o.length,adapterHasRenderPresentation:typeof e.renderPresentation=="function",adapterHasSendPayload:typeof e.sendPayload=="function",..._r(i)});let l={cfg:t,to:r.target,text:o,payload:a,accountId:r.accountId,threadId:r.threadId},d=e.renderPresentation?await e.renderPresentation({payload:a,presentation:i,ctx:l}):a,u=Pl(d,s,r.channel);if(M("presentation_render_completed",{...J(r),adapterHasRenderPresentation:typeof e.renderPresentation=="function",adapterHasSendPayload:typeof e.sendPayload=="function",...Dr(u)}),!u||typeof u!="object"||!e.sendPayload)throw new Error(`OpenClaw outbound adapter for channel "${r.channel}" cannot preserve interactive presentation`);let f=await e.sendPayload({...l,payload:u});M("presentation_send_payload_completed",{...J(r),...Dr(u),...di(f)})}sendViaBoundedCliFallback(e,t,r,o){return new Promise((i,s)=>{let a=Sl(e,t,r);M("direct_send_cli_fallback_started",{...J(e),deliveryPath:"cli-fallback",reason:o,messageTextLength:t.length,...E(r)}),this.execFile("openclaw",a,{timeout:ui,killSignal:"SIGKILL"},(l,d,u)=>{if(!l){M("direct_send_cli_fallback_completed",{...J(e),deliveryPath:"cli-fallback",...E(r)}),i();return}let f=u?.trim()?` | stderr: ${u.trim()}`:"";M("direct_send_cli_fallback_failed",{...J(e),deliveryPath:"cli-fallback",...E(r),error:$r(l),stderrLength:u?.length}),s(new Error(`${o}; bounded openclaw message send fallback failed within ${ui}ms timeout: ${$r(l)}${f}`))})})}};function J(n){return{channel:n.channel,target:n.target,accountId:n.accountId,threadId:n.threadId,sessionKey:n.sessionKey}}function Sl(n,e,t){let r=jt(t),o=["message","send","--channel",n.channel,"--target",n.target,"--message",e];return n.accountId&&o.push("--account",n.accountId),n.threadId&&o.push("--thread-id",n.threadId),r&&o.push("--presentation",JSON.stringify(r)),o}function $r(n){return n instanceof Error?n.message:String(n)}function Er(n){if(!n||typeof n!="object")return"unknown";let e=n.version;return typeof e=="string"&&e.trim()?e:"unknown"}function jt(n){let e=(n??[]).filter(t=>Array.isArray(t)&&t.length>0).map(t=>({type:"buttons",buttons:t.map(r=>({label:r.label,value:Tl(r.callbackData),...r.style?{style:r.style}:{}}))}));return e.length>0?{blocks:e}:void 0}function kl(n){let e=n.blocks.filter(t=>t.type==="buttons"&&t.buttons.length>0).map(t=>({type:"buttons",buttons:t.buttons.map(r=>({label:r.label,value:r.value,...r.style?{style:r.style}:{}}))}));return e.length>0?{blocks:e}:void 0}function vl(n){if(!n||typeof n!="object"||Array.isArray(n))return!1;let e=n.interactive;if(!e||typeof e!="object"||Array.isArray(e))return!1;let t=e.blocks;return Array.isArray(t)&&t.some(r=>{if(!r||typeof r!="object"||Array.isArray(r))return!1;let o=r;return o.type==="buttons"&&Array.isArray(o.buttons)&&o.buttons.length>0})}function Pl(n,e,t){if(!e)return n;let r=vl(n)?n:wl(n,e);return t!=="telegram"?r:bl(r,e)}function wl(n,e){return!n||typeof n!="object"||Array.isArray(n)?n:{...n,interactive:e}}function bl(n,e){if(!n||typeof n!="object"||Array.isArray(n))return n;let t=n,r=Fe(t.channelData),o=Fe(r?.telegram),i=Al(o?.buttons);if(i)return{...t,channelData:{...r??{},telegram:{...o??{},buttons:i}}};let s=Rl(e);return s?{...t,channelData:{...r??{},telegram:{...o??{},buttons:s}}}:n}function Rl(n){let e=n.blocks.filter(t=>t.type==="buttons"&&t.buttons.length>0).map(t=>t.buttons.filter(r=>typeof r.value=="string"&&r.value.trim().length>0&&Buffer.byteLength(r.value,"utf8")<=mi).map(r=>({text:r.label,callback_data:r.value,...fi(r.style)}))).filter(t=>t.length>0);return e.length>0?e:void 0}function Al(n){if(!Array.isArray(n))return;let e=n.map(t=>Array.isArray(t)?t.map(r=>{if(!r||typeof r!="object"||Array.isArray(r))return;let{style:o,...i}=r;if(!(typeof i.text!="string"||i.text.trim().length===0)&&!(i.callback_data!==void 0&&(typeof i.callback_data!="string"||i.callback_data.trim().length===0||Buffer.byteLength(i.callback_data,"utf8")>mi))&&Il(i))return{...i,...fi(o)}}).filter(r=>!!r):[]).filter(t=>t.length>0);return e.length>0?e:void 0}function Il(n){return Gt(n.callback_data)||Gt(n.url)||Ut(n.web_app)||Ut(n.webApp)||Ut(n.login_url)||Ut(n.loginUrl)||typeof n.switch_inline_query=="string"||typeof n.switch_inline_query_current_chat=="string"||Fe(n.switch_inline_query_chosen_chat)!==void 0||Fe(n.callback_game)!==void 0||n.pay===!0||ci(n.copy_text)||ci(n.copyText)}function Gt(n){return typeof n=="string"&&n.trim().length>0}function Ut(n){let e=Fe(n);return Gt(e?.url)}function ci(n){let e=Fe(n);return Gt(e?.text)}function fi(n){if(typeof n!="string")return{};let e=n.trim().toLowerCase(),t=hl[e];return t?{style:t}:{}}function Fe(n){return n&&typeof n=="object"&&!Array.isArray(n)?n:void 0}function Tl(n){return n.startsWith(`${W}:`)?n:`${W}:${n}`}var Ol=Nl(xl),rt="openclaw-code-agent",Wl=`https://registry.npmjs.org/${encodeURIComponent(rt)}/latest`,Fl="openclaw-code-agent-auto-update.json",Si=1440*60*1e3,Ll=7*Si,Bl=1e4,Le="plugin:auto-update";function fe(n){return!!n&&typeof n=="object"&&!Array.isArray(n)}function Nr(n){return n instanceof Error?n.message:String(n)}function te(n){return n?.trim().replace(/^v/i,"")||void 0}function ee(n,e){let t=n[e];return typeof t=="string"&&t.trim()?t.trim():void 0}function gi(n){let e=n?.trim().replace(/^npm:/,"");if(!e)return;if(e.startsWith("@")){let r=e.indexOf("@",1);return r===-1?e:e.slice(0,r)}let t=e.indexOf("@");return t===-1?e:e.slice(0,t)}function Or(n){let e=`${n.stdout}
|
|
2
|
-
${n.stderr}`;if(/No install record/i.test(e))throw new Error("OpenClaw reported no managed install record for OpenClaw Code Agent.");let t;try{t=JSON.parse(n.stdout)}catch{throw new Error("OpenClaw returned invalid JSON while inspecting the OpenClaw Code Agent install.")}if(!fe(t)||!fe(t.plugin)||!fe(t.install))throw new Error("OpenClaw inspection did not include the OpenClaw Code Agent plugin and managed install record.");let r=ee(t.plugin,"id"),o=te(ee(t.plugin,"version"));if(r!==
|
|
3
|
-
${i.stderr}`))throw new Error("OpenClaw reported no managed install record while updating OpenClaw Code Agent.");let s=
|
|
4
|
-
`)}async restartConfirmed(e){let t=te(e);return await this.runCommand("openclaw",["gateway","restart"]),t?`Gateway restart requested for OpenClaw Code Agent ${t}.`:"Gateway restart requested."}dismiss(e){return this.postpone(e,"Dismissed")}remindLater(e){return this.postpone(e,"Will remind later about")}postpone(e,t){let r=te(e),o=this.readState();return this.writeState({...o,...r?{dismissedVersion:r,lastDismissedAt:new Date(this.now()).toISOString()}:{}}),r?`${t} OpenClaw Code Agent ${r} update reminder.`:`${t} OpenClaw Code Agent update reminder.`}async checkForUpdate(e){let t=this.readState(),r=t.lastCheckedAt?Date.parse(t.lastCheckedAt):Number.NaN;if(Number.isFinite(r)&&this.now()-r<
|
|
5
|
-
`),[[this.options.actionButtonFactory(
|
|
6
|
-
`),[[this.options.actionButtonFactory(
|
|
7
|
-
`)
|
|
8
|
-
`)
|
|
9
|
-
`)
|
|
10
|
-
`):"",
|
|
1
|
+
import{$ as tt,A as k,Aa as jt,B as $,Ba as Se,C as de,Ca as zt,D as xr,E as Cr,F as Mr,G as Ct,H as Mt,I as _t,J as Dt,K as ti,L as ni,M as C,N as D,O as z,P as ri,Q as $t,R as oi,Ra as Ht,S as _r,T as L,U as Et,V as et,W as Nt,X as ii,Y as si,Z as Ne,_ as oe,a as Te,aa as Ot,b as xe,ba as ai,c as Tr,ca as H,d as Qo,da as Dr,e as Ce,ea as $r,f as ue,fa as nt,g as Z,ga as rt,h as le,ha as B,i as Ze,ia as Wt,j as Yo,ja as li,k as _,ka as ot,l as Jo,la as ie,m as Y,ma as Oe,n as Me,na as di,o as It,oa as Ft,p as ce,pa as it,q as _e,qa as Lt,r as Tt,ra as Bt,s as xt,sa as We,t as Xo,ta as Kt,u as De,ua as ui,v as $e,va as qt,w as Zo,wa as Vt,x as Ee,xa as ci,y as ei,ya as Ut,z as p,za as Gt}from"./chunks/chunk.js";import{readdirSync as kf,statSync as vf,rmSync as Pf}from"fs";import{execFileSync as wf}from"child_process";import{join as zo}from"path";var pi={name:"openclaw-code-agent",version:"4.7.14",type:"module",packageManager:"pnpm@11.15.1",main:"dist/index.js",scripts:{"audit:prod":"pnpm audit --prod",build:"esbuild index.ts --bundle --splitting --platform=node --target=node22 --format=esm --outdir=dist --entry-names=index --chunk-names=chunks/[name] --minify --charset=utf8 --pure:console.log --pure:console.warn --pure:console.info --pure:console.debug --external:@anthropic-ai/claude-agent-sdk --external:openclaw/plugin-sdk/plugin-entry","generate:npm-shrinkwrap":"node scripts/generate-npm-shrinkwrap.mjs","check-static-guardrails":"node scripts/check-static-guardrails.mjs && node scripts/check-npm-shrinkwrap.mjs","check-plugin-security":"node scripts/check-plugin-security.mjs",test:"node scripts/run-tests.mjs","test:integ:crabbox":"node scripts/run-tests.mjs tests/oca-codex-crabbox-integ.test.ts tests/oca-plugin-workflows-integ.test.ts tests/oca-codex-telegram-proof.test.ts tests/oca-codex-proof-app-server.test.ts tests/oca-codex-proof-workflow.test.ts","test:serial":"node scripts/run-tests.mjs","test:file":"node scripts/run-tests.mjs","smoke:backend":"node scripts/run-tests.mjs tests/codex-harness.test.ts tests/plan-mode-e2e.test.ts tests/session-restore-service.test.ts","smoke:backend-parity":"node scripts/run-tests.mjs tests/backend-contract-smoke.test.ts tests/codex-harness.test.ts tests/harness-registry.test.ts","smoke:codex-worktrees":"node scripts/run-tests.mjs tests/codex-harness.test.ts tests/session-bootstrap.test.ts tests/session-restore-service.test.ts","smoke:codex-live":"OPENCLAW_RUN_LIVE_CODEX_SMOKE=1 node scripts/run-tests.mjs tests/live-codex-app-server-smoke.test.ts","proof:codex-doctor":"node --import tsx scripts/e2e/oca-codex-telegram-proof.ts doctor","proof:codex-local":"node --import tsx scripts/e2e/oca-codex-telegram-proof.ts local-smoke","proof:codex-telegram":"node scripts/e2e/run-oca-codex-telegram-proof.mjs","smoke:codex-release":"OPENCLAW_RUN_LIVE_CODEX_RELEASE_SMOKE=1 node scripts/run-tests.mjs tests/live-codex-app-server-smoke.test.ts","smoke:opencode-live":"OPENCLAW_RUN_LIVE_OPENCODE_SMOKE=1 node scripts/run-tests.mjs tests/live-opencode-server-smoke.test.ts",typecheck:"tsc -p tsconfig.typecheck.json --noEmit","validate:release-metadata":"node scripts/validate-release-metadata.mjs","verify:npm-consumer":"node scripts/verify-npm-consumer-install.mjs",verify:"pnpm check-static-guardrails && pnpm typecheck && pnpm build && pnpm test"},openclaw:{extensions:["./dist/index.js"],install:{npmSpec:"openclaw-code-agent",defaultChoice:"npm",minHostVersion:">=2026.9.2"},compat:{pluginApi:">=2026.8.1",minGatewayVersion:"2026.8.1"},build:{openclawVersion:"2026.9.2",pluginSdkVersion:"2026.9.2"}},files:["dist/","skills/","openclaw.plugin.json","npm-shrinkwrap.json","README.md","LICENSE"],dependencies:{"@anthropic-ai/claude-agent-sdk":"0.3.251","@anthropic-ai/sdk":"0.122.0","@hono/node-server":"2.1.1","@modelcontextprotocol/sdk":"1.30.0","express-rate-limit":"8.7.0","fast-uri":"3.1.6",hono:"4.12.34","ip-address":"10.7.0",qs:"6.16.0",zod:"4.5.4"},devDependencies:{"@types/node":"^26.0.1",esbuild:"^0.28.1",nanoid:"^6.0.0",openclaw:"2026.9.2",tsx:"^4.21.0",typescript:"^7.0.2"},keywords:["openclaw","plugin","claude","claude-code","opencode","coding-agent","codex","ai","anthropic","agent","code-generation","automation","cli","background-process","developer-tools"],repository:{type:"git",url:"git+https://github.com/goldmar/openclaw-code-agent.git"},homepage:"https://github.com/goldmar/openclaw-code-agent#readme",bugs:{url:"https://github.com/goldmar/openclaw-code-agent/issues"},license:"MIT",author:"Mark Goldenstein",publishConfig:{access:"public",provenance:!0},description:"An OpenClaw plugin that orchestrates coding agent sessions as managed background processes. Launch, monitor, and control coding agents (Claude Code, Codex, experimental OpenCode, and more) directly from your AI gateway.",peerDependencies:{openclaw:">=2026.8.1"}};import{execFile as Nl}from"node:child_process";import{mkdirSync as Ol,readFileSync as Wl,renameSync as Fl,writeFileSync as Ll}from"node:fs";import{dirname as Bl,join as Kl}from"node:path";import{promisify as ql}from"node:util";import*as vi from"child_process";import{createHash as Pl}from"crypto";var W="code-agent";var wl=new Set(["1","true","yes","on"]);function Nr(){let n=process.env.OPENCLAW_CODE_AGENT_BUTTON_DIAGNOSTICS?.trim().toLowerCase();return!!(n&&wl.has(n))}function K(n){if(!(typeof n!="string"||n.length===0))return Pl("sha256").update(n).digest("hex").slice(0,12)}function mi(n){return typeof n.callbackData=="string"?n.callbackData:typeof n.value=="string"?n.value:void 0}function E(n){let e=(n??[]).filter(r=>Array.isArray(r)&&r.length>0),t=e.flat();return{buttonRows:e.length||void 0,buttonCount:t.length||void 0,buttonLabels:t.length>0?t.map(r=>typeof r.label=="string"?r.label:void 0):void 0,callbackByteLengths:t.length>0?t.map(r=>{let o=mi(r);return o?Buffer.byteLength(o,"utf8"):void 0}):void 0,callbackHashes:t.length>0?t.map(r=>K(mi(r))):void 0,buttonStyles:t.length>0?t.map(r=>typeof r.style=="string"?r.style:void 0):void 0}}function pe(n){return n&&typeof n=="object"&&!Array.isArray(n)?n:void 0}function gi(n){let e=pe(n),t=Array.isArray(e?.blocks)?e.blocks:void 0;if(t)return t.map(r=>pe(r)?.type).filter(r=>typeof r=="string"&&r.length>0)}function hi(n){let e=pe(n),t=Array.isArray(e?.blocks)?e.blocks:void 0;if(!t)return;let r=t.map(o=>{let i=pe(o);return i?.type!=="buttons"||!Array.isArray(i.buttons)?[]:i.buttons}).filter(o=>o.length>0);return r.length>0?r:void 0}function Or(n){let e=hi(n);return{presentationBlockTypes:gi(n),...E(e)}}function Wr(n){let e=pe(n),t=e?.interactive,r=pe(e?.channelData),o=pe(r?.telegram),i=Array.isArray(o?.buttons)?o.buttons:void 0;return{renderedPayloadKeys:e?Object.keys(e).sort():void 0,interactiveBlockTypes:gi(t),...fi("interactive",E(hi(t))),...fi("telegramChannelData",E(i)),hasPresentation:!!e?.presentation,hasInteractive:!!t,hasTelegramChannelDataButtons:!!i?.some(s=>s.length>0)}}function yi(n){let e=pe(n);return e?{sendResultKeys:Object.keys(e).sort(),messageId:Er(e.messageId),chatId:Er(e.chatId),telegramThreadId:Er(e.messageThreadId??e.threadId),channel:typeof e.channel=="string"?e.channel:void 0}:{sendResultType:typeof n}}function Er(n){return typeof n=="string"||typeof n=="number"?n:void 0}function fi(n,e){let t={};for(let[r,o]of Object.entries(e))o!==void 0&&(t[`${n}${r.charAt(0).toUpperCase()}${r.slice(1)}`]=o);return t}function M(n,e){if(!Nr())return;let t=Object.fromEntries(Object.entries(e).filter(([,r])=>r!==void 0));`${JSON.stringify({event:n,callbackNamespace:W,...t})}`}var Si=5e3,Pi=64,bl={primary:"primary",success:"success",positive:"success",danger:"danger",destructive:"danger",negative:"danger"},Rl={execFile:vi.execFile},me=class extends Error{constructor(e){super(e),this.name="MissingRuntimeDirectNotificationCapabilityError"}},Fe=class{constructor(e=Rl.execFile){this.execFile=e}execFile;async send(e,t,r){M("direct_send_started",{...J(e),messageTextLength:t.length,...E(r)});try{await this.sendViaRuntime(e,t,r),M("direct_send_succeeded",{...J(e),deliveryPath:"runtime",...E(r)});return}catch(o){if(!(o instanceof me))throw M("direct_send_failed",{...J(e),deliveryPath:"runtime",...E(r),error:Fr(o)}),o;M("direct_send_runtime_capability_missing",{...J(e),deliveryPath:"runtime",...E(r),reason:o.message}),await this.sendViaBoundedCliFallback(e,t,r,o.message),M("direct_send_succeeded",{...J(e),deliveryPath:"cli-fallback",...E(r)})}}async sendViaRuntime(e,t,r){let o=xe(),i=Qo(),s=o?.channel?.outbound?.loadAdapter;if(!o)throw new me("OpenClaw plugin runtime is unavailable for direct notification delivery");if(i==null)throw new me("OpenClaw runtime config snapshot is unavailable for direct notification delivery");if(!o.channel)throw new me(`OpenClaw plugin runtime channel surface is unavailable for direct notification delivery (runtimeVersion=${Lr(o)})`);if(!o.channel.outbound)throw new me(`OpenClaw plugin runtime channel outbound surface is unavailable for direct notification delivery (runtimeVersion=${Lr(o)})`);if(!s)throw new me(`OpenClaw plugin runtime channel outbound loadAdapter is unavailable for direct notification delivery (runtimeVersion=${Lr(o)})`);let a=await s(e.channel);if(!a)throw new Error(`OpenClaw outbound adapter for channel "${e.channel}" is unavailable`);let l=Jt(r);if(M("runtime_adapter_loaded",{...J(e),adapterHasRenderPresentation:typeof a.renderPresentation=="function",adapterHasSendPayload:typeof a.sendPayload=="function",adapterHasSendText:typeof a.sendText=="function",...E(r),...l?Or(l):{}}),l){await this.sendPresentation(a,i,e,t,l);return}if(!a.sendText)throw new Error(`OpenClaw outbound adapter for channel "${e.channel}" cannot send text`);await a.sendText({cfg:i,to:e.target,text:t,accountId:e.accountId,threadId:e.threadId})}async sendPresentation(e,t,r,o,i){let s=Il(i),a={text:o,presentation:i,interactive:s};M("presentation_render_started",{...J(r),messageTextLength:o.length,adapterHasRenderPresentation:typeof e.renderPresentation=="function",adapterHasSendPayload:typeof e.sendPayload=="function",...Or(i)});let l={cfg:t,to:r.target,text:o,payload:a,accountId:r.accountId,threadId:r.threadId},d=e.renderPresentation?await e.renderPresentation({payload:a,presentation:i,ctx:l}):a,u=xl(d,s,r.channel);if(M("presentation_render_completed",{...J(r),adapterHasRenderPresentation:typeof e.renderPresentation=="function",adapterHasSendPayload:typeof e.sendPayload=="function",...Wr(u)}),!u||typeof u!="object"||!e.sendPayload)throw new Error(`OpenClaw outbound adapter for channel "${r.channel}" cannot preserve interactive presentation`);let m=await e.sendPayload({...l,payload:u});M("presentation_send_payload_completed",{...J(r),...Wr(u),...yi(m)})}sendViaBoundedCliFallback(e,t,r,o){return new Promise((i,s)=>{let a=Al(e,t,r);M("direct_send_cli_fallback_started",{...J(e),deliveryPath:"cli-fallback",reason:o,messageTextLength:t.length,...E(r)}),this.execFile("openclaw",a,{timeout:Si,killSignal:"SIGKILL"},(l,d,u)=>{if(!l){M("direct_send_cli_fallback_completed",{...J(e),deliveryPath:"cli-fallback",...E(r)}),i();return}let m=u?.trim()?` | stderr: ${u.trim()}`:"";M("direct_send_cli_fallback_failed",{...J(e),deliveryPath:"cli-fallback",...E(r),error:Fr(l),stderrLength:u?.length}),s(new Error(`${o}; bounded openclaw message send fallback failed within ${Si}ms timeout: ${Fr(l)}${m}`))})})}};function J(n){return{channel:n.channel,target:n.target,accountId:n.accountId,threadId:n.threadId,sessionKey:n.sessionKey}}function Al(n,e,t){let r=Jt(t),o=["message","send","--channel",n.channel,"--target",n.target,"--message",e];return n.accountId&&o.push("--account",n.accountId),n.threadId&&o.push("--thread-id",n.threadId),r&&o.push("--presentation",JSON.stringify(r)),o}function Fr(n){return n instanceof Error?n.message:String(n)}function Lr(n){if(!n||typeof n!="object")return"unknown";let e=n.version;return typeof e=="string"&&e.trim()?e:"unknown"}function Jt(n){let e=(n??[]).filter(t=>Array.isArray(t)&&t.length>0).map(t=>({type:"buttons",buttons:t.map(r=>({label:r.label,value:El(r.callbackData),...r.style?{style:r.style}:{}}))}));return e.length>0?{blocks:e}:void 0}function Il(n){let e=n.blocks.filter(t=>t.type==="buttons"&&t.buttons.length>0).map(t=>({type:"buttons",buttons:t.buttons.map(r=>({label:r.label,value:r.value,...r.style?{style:r.style}:{}}))}));return e.length>0?{blocks:e}:void 0}function Tl(n){if(!n||typeof n!="object"||Array.isArray(n))return!1;let e=n.interactive;if(!e||typeof e!="object"||Array.isArray(e))return!1;let t=e.blocks;return Array.isArray(t)&&t.some(r=>{if(!r||typeof r!="object"||Array.isArray(r))return!1;let o=r;return o.type==="buttons"&&Array.isArray(o.buttons)&&o.buttons.length>0})}function xl(n,e,t){if(!e)return n;let r=Tl(n)?n:Cl(n,e);return t!=="telegram"?r:Ml(r,e)}function Cl(n,e){return!n||typeof n!="object"||Array.isArray(n)?n:{...n,interactive:e}}function Ml(n,e){if(!n||typeof n!="object"||Array.isArray(n))return n;let t=n,r=Le(t.channelData),o=Le(r?.telegram),i=Dl(o?.buttons);if(i)return{...t,channelData:{...r??{},telegram:{...o??{},buttons:i}}};let s=_l(e);return s?{...t,channelData:{...r??{},telegram:{...o??{},buttons:s}}}:n}function _l(n){let e=n.blocks.filter(t=>t.type==="buttons"&&t.buttons.length>0).map(t=>t.buttons.filter(r=>typeof r.value=="string"&&r.value.trim().length>0&&Buffer.byteLength(r.value,"utf8")<=Pi).map(r=>({text:r.label,callback_data:r.value,...wi(r.style)}))).filter(t=>t.length>0);return e.length>0?e:void 0}function Dl(n){if(!Array.isArray(n))return;let e=n.map(t=>Array.isArray(t)?t.map(r=>{if(!r||typeof r!="object"||Array.isArray(r))return;let{style:o,...i}=r;if(!(typeof i.text!="string"||i.text.trim().length===0)&&!(i.callback_data!==void 0&&(typeof i.callback_data!="string"||i.callback_data.trim().length===0||Buffer.byteLength(i.callback_data,"utf8")>Pi))&&$l(i))return{...i,...wi(o)}}).filter(r=>!!r):[]).filter(t=>t.length>0);return e.length>0?e:void 0}function $l(n){return Yt(n.callback_data)||Yt(n.url)||Qt(n.web_app)||Qt(n.webApp)||Qt(n.login_url)||Qt(n.loginUrl)||typeof n.switch_inline_query=="string"||typeof n.switch_inline_query_current_chat=="string"||Le(n.switch_inline_query_chosen_chat)!==void 0||Le(n.callback_game)!==void 0||n.pay===!0||ki(n.copy_text)||ki(n.copyText)}function Yt(n){return typeof n=="string"&&n.trim().length>0}function Qt(n){let e=Le(n);return Yt(e?.url)}function ki(n){let e=Le(n);return Yt(e?.text)}function wi(n){if(typeof n!="string")return{};let e=n.trim().toLowerCase(),t=bl[e];return t?{style:t}:{}}function Le(n){return n&&typeof n=="object"&&!Array.isArray(n)?n:void 0}function El(n){return n.startsWith(`${W}:`)?n:`${W}:${n}`}var Vl=ql(Nl),st="openclaw-code-agent",Ul=`https://registry.npmjs.org/${encodeURIComponent(st)}/latest`,Gl="openclaw-code-agent-auto-update.json",Ii=1440*60*1e3,jl=7*Ii,zl=1e4,Be="plugin:auto-update";function fe(n){return!!n&&typeof n=="object"&&!Array.isArray(n)}function Br(n){return n instanceof Error?n.message:String(n)}function te(n){return n?.trim().replace(/^v/i,"")||void 0}function ee(n,e){let t=n[e];return typeof t=="string"&&t.trim()?t.trim():void 0}function bi(n){let e=n?.trim().replace(/^npm:/,"");if(!e)return;if(e.startsWith("@")){let r=e.indexOf("@",1);return r===-1?e:e.slice(0,r)}let t=e.indexOf("@");return t===-1?e:e.slice(0,t)}function Kr(n){let e=`${n.stdout}
|
|
2
|
+
${n.stderr}`;if(/No install record/i.test(e))throw new Error("OpenClaw reported no managed install record for OpenClaw Code Agent.");let t;try{t=JSON.parse(n.stdout)}catch{throw new Error("OpenClaw returned invalid JSON while inspecting the OpenClaw Code Agent install.")}if(!fe(t)||!fe(t.plugin)||!fe(t.install))throw new Error("OpenClaw inspection did not include the OpenClaw Code Agent plugin and managed install record.");let r=ee(t.plugin,"id"),o=te(ee(t.plugin,"version"));if(r!==st||!o)throw new Error("OpenClaw inspection returned an unexpected OpenClaw Code Agent plugin identity or version.");let i=ee(t.install,"source"),s=te(ee(t.install,"version")),a=te(ee(t.install,"resolvedVersion"));if(i==="npm"){let l=ee(t.install,"resolvedName")??bi(ee(t.install,"spec"))??bi(ee(t.install,"resolvedSpec"));if(!l)throw new Error("The OpenClaw Code Agent npm install record does not identify its package.");return{pluginVersion:o,install:{source:i,packageName:l,recordedVersion:s,resolvedVersion:a}}}if(i==="clawhub"){let l=ee(t.install,"clawhubPackage");if(!l)throw new Error("The OpenClaw Code Agent ClawHub install record does not identify its package.");return{pluginVersion:o,install:{source:i,packageName:l,recordedVersion:s,resolvedVersion:a}}}throw new Error(`OpenClaw Code Agent self-update does not support managed install source ${i??"unknown"}.`)}function Hl(n,e){return["plugins","install",n.source==="npm"?`${n.packageName}@${e}`:`clawhub:${n.packageName}@${e}`,"--force"]}function Ql(n,e,t){if(e.install.source!==n.install.source||e.install.packageName!==n.install.packageName)throw new Error("OpenClaw Code Agent update verification found that the managed install source or package changed.");if(e.pluginVersion!==t)throw new Error(`OpenClaw Code Agent update verification found installed plugin version ${e.pluginVersion}, expected ${t}.`);let r=[e.install.recordedVersion,e.install.resolvedVersion].filter(o=>!!o);if(r.length===0||r.some(o=>o!==t))throw new Error(`OpenClaw Code Agent update verification found managed install version ${r.join("/")||"unknown"}, expected ${t}.`)}function qr(n){let t=te(n)?.match(/^((?:0|[1-9]\d*))\.((?:0|[1-9]\d*))\.((?:0|[1-9]\d*))$/);if(!t?.[1]||!t[2]||!t[3])return;let r=[Number(t[1]),Number(t[2]),Number(t[3])];return r.every(Number.isSafeInteger)?r:void 0}function Yl(n,e){let t=qr(n),r=qr(e);if(!t||!r)return!1;for(let o=0;o<3;o+=1){if(t[o]>r[o])return!0;if(t[o]<r[o])return!1}return!1}function Jl(n){if(!fe(n))return{};let e={};for(let t of["lastCheckedAt","latestVersion","promptedVersion","lastPromptedAt","dismissedVersion","lastDismissedAt","updateInstalledVersion","restartPromptedVersion","lastError"]){let r=n[t];typeof r=="string"&&r.trim()&&(e[t]=r)}return e}function Ri(n){if(!(!n?.provider||!n.target||n.provider==="system"||n.target==="system"))return{channel:n.provider,target:n.target,accountId:n.accountId,threadId:n.threadId,sessionKey:n.sessionKey}}function Ai(){if(_.fallbackChannel)return ue(_.fallbackChannel)}async function Xl(){let{fetchNpmReleaseMetadata:n}=await import("./chunks/npm-release-client.js"),e=await n(Ul,zl),t=fe(e)&&typeof e.version=="string"?te(e.version):void 0;return t?{version:t}:void 0}async function Zl(n,e){let t=await n("openclaw",["plugins","search",e,"--limit","100","--json"]),r;try{r=JSON.parse(t.stdout)}catch{throw new Error("OpenClaw returned invalid JSON while checking ClawHub for OpenClaw Code Agent updates.")}let o=fe(r)&&Array.isArray(r.results)?r.results:[];for(let i of o){if(!fe(i)||!fe(i.package)||ee(i.package,"name")!==e)continue;let s=te(ee(i.package,"latestVersion"));return s?{version:s}:void 0}}async function ed(n){let e=Kr(await n("openclaw",["plugins","inspect",st,"--json"]));return e.install.source==="npm"?Xl():Zl(n,e.install.packageName)}async function td(n,e){let t=await Vl(n,e,{timeout:12e4,maxBuffer:1048576});return{stdout:t.stdout?.toString()??"",stderr:t.stderr?.toString()??""}}var Xt=class{constructor(e){this.options=e;this.statePath=Kl(e.stateDir,Gl),this.notifier=e.notifier??new Fe,this.runCommand=e.runCommand??td,this.fetchLatestRelease=e.fetchLatestRelease??(()=>ed(this.runCommand)),this.now=e.now??Date.now}options;statePath;notifier;fetchLatestRelease;runCommand;now;checkInFlight;maybeCheckForUpdate(e={}){this.checkInFlight||(this.checkInFlight=this.checkForUpdate(e).catch(t=>{`${Br(t)}`}).finally(()=>{this.checkInFlight=void 0}))}async waitForIdle(){await this.checkInFlight}async installConfirmed(e,t){let r=te(e);if(!r||!qr(r))return"Could not determine which stable OpenClaw Code Agent version to update to.";let o=Kr(await this.runCommand("openclaw",["plugins","inspect",st,"--json"])),i=await this.runCommand("openclaw",Hl(o.install,r));if(/No install record/i.test(`${i.stdout}
|
|
3
|
+
${i.stderr}`))throw new Error("OpenClaw reported no managed install record while updating OpenClaw Code Agent.");let s=Kr(await this.runCommand("openclaw",["plugins","inspect",st,"--json"]));Ql(o,s,r);let a=this.readState();this.writeState({...a,updateInstalledVersion:r});let l=Ri(t?.route??Ai());if(l)try{return await this.sendRestartPrompt(l,r),`OpenClaw Code Agent ${r} installation was verified. Restart confirmation was sent.`}catch(d){`${r}${Br(d)}`}return[`OpenClaw Code Agent ${r} installation was verified.`,"Restart the Gateway explicitly to load it: openclaw gateway restart"].join(`
|
|
4
|
+
`)}async restartConfirmed(e){let t=te(e);return await this.runCommand("openclaw",["gateway","restart"]),t?`Gateway restart requested for OpenClaw Code Agent ${t}.`:"Gateway restart requested."}dismiss(e){return this.postpone(e,"Dismissed")}remindLater(e){return this.postpone(e,"Will remind later about")}postpone(e,t){let r=te(e),o=this.readState();return this.writeState({...o,...r?{dismissedVersion:r,lastDismissedAt:new Date(this.now()).toISOString()}:{}}),r?`${t} OpenClaw Code Agent ${r} update reminder.`:`${t} OpenClaw Code Agent update reminder.`}async checkForUpdate(e){let t=this.readState(),r=t.lastCheckedAt?Date.parse(t.lastCheckedAt):Number.NaN;if(Number.isFinite(r)&&this.now()-r<Ii){await this.promptFromState(t,e);return}let o=new Date(this.now()).toISOString(),i={...t,lastCheckedAt:o};delete i.lastError,this.writeState(i);try{let s=await this.fetchLatestRelease(),a=te(s?.version),l={...this.readState(),latestVersion:a};delete l.lastError,this.writeState(l),await this.promptFromState(l,e)}catch(s){throw this.writeState({...this.readState(),lastError:Br(s)}),s}}async promptFromState(e,t){let r=te(e.latestVersion);if(!Yl(r,this.options.currentVersion)||!this.shouldPromptForVersion(e,r))return;let o=Ri(t.route??Ai());o&&(await this.sendUpdatePrompt(o,r),this.writeState({...this.readState(),latestVersion:r,promptedVersion:r,lastPromptedAt:new Date(this.now()).toISOString()}))}async sendUpdatePrompt(e,t){await this.notifier.send(e,[`OpenClaw Code Agent update available: ${this.options.currentVersion} -> ${t}.`,"","Update now will reinstall the exact approved version from OpenClaw Code Agent's recorded npm or ClawHub source, then verify it.","If the update succeeds, OpenClaw Code Agent will ask separately before restarting the Gateway."].join(`
|
|
5
|
+
`),[[this.options.actionButtonFactory(Be,"plugin-update-install","Update now",{pluginUpdateVersion:t,route:{provider:e.channel,accountId:e.accountId,target:e.target,threadId:e.threadId,sessionKey:e.sessionKey}}),this.options.actionButtonFactory(Be,"plugin-update-remind-later","Remind later",{pluginUpdateVersion:t}),this.options.actionButtonFactory(Be,"plugin-update-dismiss","Dismiss",{pluginUpdateVersion:t})]])}shouldPromptForVersion(e,t){let r=e.promptedVersion===t?e.lastPromptedAt:void 0,o=e.dismissedVersion===t?e.lastDismissedAt:void 0,i=[r,o].map(s=>s?Date.parse(s):Number.NaN).filter(s=>Number.isFinite(s));return i.length===0?!0:this.now()-Math.max(...i)>=jl}async sendRestartPrompt(e,t){await this.notifier.send(e,[`OpenClaw Code Agent ${t} installation was verified.`,"","Restart the Gateway now to load the updated plugin?","This will run: openclaw gateway restart"].join(`
|
|
6
|
+
`),[[this.options.actionButtonFactory(Be,"plugin-update-restart","Restart Gateway",{pluginUpdateVersion:t}),this.options.actionButtonFactory(Be,"plugin-update-remind-later","Remind later",{pluginUpdateVersion:t}),this.options.actionButtonFactory(Be,"plugin-update-dismiss","Dismiss",{pluginUpdateVersion:t})]]),this.writeState({...this.readState(),restartPromptedVersion:t})}readState(){try{return Jl(JSON.parse(Wl(this.statePath,"utf-8")))}catch{return{}}}writeState(e){Ol(Bl(this.statePath),{recursive:!0});let t=`${this.statePath}.tmp`;Ll(t,JSON.stringify(e,null,2),"utf-8"),Fl(t,this.statePath)}};var Vr=["off","manual","ask","delegate","auto-merge","auto-pr"],Zt=new Set(Vr),nd=["pr-required","pr-allowed","never-pr","manual"],Ti=new Set(nd),Ke=["low","medium","high","xhigh","max"],xi=new Set(Ke);function ge(n){let e=n.harness?.trim(),t=n.model?.trim();return e&&t?`${e} | ${t}${at(n)}`:e?`${e} | default`:t}function at(n){let e=n.reasoningEffort;if(!e||!Ke.includes(e))return"";let t=n.model?.trim().toLowerCase().replace(/^(openai|anthropic)\//,"").replace(/-(?:\d{4}-\d{2}-\d{2}|\d{8})$/,"");if(!t)return"";if(n.harness==="codex"){if(!/^(gpt-6-astra|gpt-5\.6-(sol|terra|luna)|gpt-5(?:\.[1-5])?(?:-codex(?:-max|-mini)?|-mini|-nano)?|o[134](?:-mini)?)$/.test(t)||e==="xhigh"&&/^(gpt-5(?:-mini|-nano|-codex)?|gpt-5\.1(?:-codex(?:-mini)?)?|o[134](?:-mini)?)$/.test(t)||e==="max"&&!/^(gpt-6-astra|gpt-5\.6-(sol|terra|luna))$/.test(t))return""}else if(n.harness==="claude-code"){if(!(/^(?:claude-)?(?:opus|sonnet)(?:-4-[678]|-5)?$/.test(t)||/^(?:claude-)?opus-4-5$/.test(t))||!["low","medium","high","xhigh","max"].includes(e)||e==="xhigh"&&!/^(?:claude-)?(?:opus-(?:4-[78]|5)|sonnet-5)$/.test(t)||e==="max"&&/opus-4-5/.test(t))return""}else return"";return` | reasoning: ${e}`}function Ur(n,e){if(!n||!e)return n;let t=n.indexOf(`
|
|
7
|
+
`),r=t<0?n:n.slice(0,t);return r.endsWith(e)?n:`${r}${e}${t<0?"":n.slice(t)}`}function rd(n){return n.length>80?`${n.slice(0,80)}...`:n}function od(n){return n.worktreePath?!n.originalWorkdir||n.originalWorkdir===n.worktreePath?`${n.worktreePath} (worktree)`:`${n.worktreePath} (worktree of ${n.originalWorkdir})`:n.workdir}function id(n){let e=["Session launched successfully.",` Name: ${n.sessionName}`,` ID: ${n.sessionId}`,` Harness: ${n.harness}`,` Permission mode: ${n.permissionMode}`,` Plan approval: ${n.planApproval}`,` Worktree strategy: ${n.worktreeStrategy??"off"}`,...n.repoIntegrationPolicy?[` Repo policy: ${n.repoIntegrationPolicy}${n.repoProvider?` (${n.repoProvider})`:""}`]:[],` Resolved workdir: ${od(n)}`,` Model: ${n.model??"default"}`,` Prompt: "${rd(n.prompt)}"`];if(at(n)&&e.push(` Reasoning effort: ${n.reasoningEffort}`),n.fastMode&&e.push(" Fast mode: enabled"),n.resumeSessionId){let t=n.resumeSessionName?`${n.resumeSessionName} [${n.resumeSessionId}]`:n.resumeSessionId;e.push(` Resume: ${t}${n.forkSession?" (forked)":""}`),!n.forkSession&&n.resumeSessionName&&n.sessionName!==n.resumeSessionName&&e.push(` Follow-up label: ${n.sessionName}`)}else n.forceNewSession&&e.push(" Force new session: true");return n.clearedPersistedCodexResume&&e.push(" Thread state: historical Codex state cleared; starting a fresh thread."),e.push(" Mode: multi-turn (use agent_respond to send follow-up messages)"),e.push("","Use agent_sessions to check status, agent_output to see output."),e.join(`
|
|
8
|
+
`)}function en(n,e){return id({...n,sessionId:e.id,sessionName:e.name,model:e.model,reasoningEffort:e.reasoningEffort,fastMode:e.fastMode,worktreeStrategy:e.worktreeStrategy??"off",repoIntegrationPolicy:e.repoIntegrationPolicy,repoProvider:e.repoProvider,worktreePath:e.worktreePath,originalWorkdir:e.originalWorkdir,resumeSessionName:e.resumedFromSessionName??n.resumeSessionName})}function tn(n){let e=n.resumedFromSessionName||n.sessionName,t=n.resumedFromSessionName&&n.sessionName!==n.resumedFromSessionName?` | Follow-up label: ${n.sessionName}`:"";return`▶️ [${e}] Resumed${t} | ${n.workdirLabel} | ${n.harnessLabel}`}function qe(n){let{requestedResumeSessionId:e,activeSession:t,persistedSession:r}=n;return e?t?{resumeSessionId:e,clearedPersistedCodexResume:!1}:r?.harness==="codex"&&r.backendRef?.kind!=="codex-app-server"?{resumeSessionId:void 0,clearedPersistedCodexResume:!0}:{resumeSessionId:e,clearedPersistedCodexResume:!1}:{resumeSessionId:void 0,clearedPersistedCodexResume:!1}}function nn(n){return"id"in n?n.id:n.sessionId}function lt(n){return n.status==="completed"||n.killReason==="done"}function sd(n){return!lt(n)||!C(n)?!1:n.backendRef?.kind==="codex-app-server"||n.backendRef?.kind==="opencode-server"}function ad(n){return(n.killReason==="shutdown"||n.killReason==="startup-timeout")&&!C(n)}function ke(n){if(n.status==="running")return{kind:"direct",reason:"already_running"};let e=nn(n);if(lt(n)&&!sd(n))return{kind:"unavailable",reason:"completed",stableSessionId:e};let t=C(n);if(t){let r="harnessName"in n?void 0:n,{resumeSessionId:o,clearedPersistedCodexResume:i}=qe({requestedResumeSessionId:t,activeSession:"harnessName"in n?{harnessSessionId:t}:void 0,persistedSession:r?{harness:r.harness,backendRef:r.backendRef}:void 0});return o?{kind:"resume",resumeSessionId:o,stableSessionId:e,clearedPersistedCodexResume:i}:{kind:"unavailable",reason:"legacy_non_resumable",stableSessionId:e,clearedPersistedCodexResume:i}}return ad(n)?{kind:"relaunch",stableSessionId:e}:{kind:"unavailable",reason:"missing_backend_state",stableSessionId:e}}import{existsSync as Di}from"fs";function ld(n){return n.oneShotCliRun===!0?!0:!!n.sessionKey?.includes(":cron:")}function rn(n){let e=Z({route:n.route});if(Ce(e))return{kind:"resolved",route:e,recovered:!1};let t=n.recoveredRouteSource?Z(n.recoveredRouteSource):void 0;return Ce(t)?{kind:"resolved",route:t,recovered:!0}:ld(n.ctx)?{kind:"resolved",route:e??{provider:"system",target:"system",sessionKey:n.ctx.sessionKey},recovered:!1}:{kind:"error",text:[`Error: Cannot launch the asynchronous ${n.operation} because OpenClaw did not provide a trustworthy lifecycle delivery route.`,"The invocation context is missing session, delivery, and workspace identity (as occurs in the standalone deferred/nested plugin-tool bridge).","Retry from the originating chat/session or update OpenClaw so nested plugin-tool invocations preserve ToolContext routing fields. No coding session was started."].join(" ")}}function Ci(n){return n==null?void 0:String(n).trim()||void 0}function dd(n){return n.trim().replace(/^['"`](.*)['"`]$/s,"$1").trim()}function ud(n){let e=n.split(/\n\s*\n/,1)[0]?.split(`
|
|
9
|
+
`).map(t=>t.trim()).filter(Boolean)??[];if(e.length!==0)for(let t of e){let r=t.match(/^(Workdir|Repo):\s*(.+)$/),o=dd(r?.[2]??"");if(o.startsWith("/")&&Di(o))return o}}function Mi(n,e){return n.workdir!==e.workdir?!1:e.originSessionKey&&n.originSessionKey?n.originSessionKey===e.originSessionKey:!e.originChannel||!n.originChannel?!1:n.originChannel===e.originChannel&&Ci(n.originThreadId)===Ci(e.originThreadId)}function _i(n){return n.slice(0,3).map(e=>` - ${e.name} [${e.ref}] | status=${e.status}${e.lifecycle?` | lifecycle=${e.lifecycle}`:""}`).join(`
|
|
10
|
+
`)}function cd(n,e){let t=[],r=[],o=new Set;for(let i of n.list?.("all")??[]){if(!Mi(i,e))continue;let s=i.id;if(!o.has(s)){if(o.add(s),i.isExplicitlyResumable){t.push({ref:i.id,name:i.name,status:i.status,lifecycle:i.lifecycle,resumable:!0});continue}(i.status==="starting"||i.status==="running")&&r.push({ref:i.id,name:i.name,status:i.status,lifecycle:i.lifecycle,resumable:!1})}}for(let i of n.listPersistedSessions?.()??[]){if(!i.resumable||!Mi(i,e))continue;let s=D(i)??i.harnessSessionId,a=i.sessionId??C(i)??i.harnessSessionId;!s||!a||o.has(a)||(o.add(a),t.push({ref:s,name:i.name,status:i.status,lifecycle:i.lifecycle,resumable:!0}))}return{resumable:t,active:r}}function on(n,e,t){let r=n.workdir||ud(n.prompt)||e.workspaceDir||_.defaultWorkdir||process.cwd();if(!Di(r))return{kind:"error",text:`Error: Working directory does not exist: ${r}`};let o=n.harness??Y(),i=Me(o),s=n.model??i,a=Ct(o,s),l=n.model!==void 0,d=It(o);if(!a&&(o!=="opencode"||d&&d.length>0))return{kind:"error",text:`Error: No default model configured for harness "${o}". Set plugins.entries["openclaw-code-agent"].config.harnesses.${o}.defaultModel or pass model explicitly.`};if(a&&!_t(o,a))return{kind:"error",text:`Error: Model "${s}" is not supported for harness "${o}". Use a bare Codex model id such as "gpt-6-astra" or "gpt-5.6-sol".`};if(a&&d&&d.length>0&&!Mt(o,a,d))return{kind:"error",text:l?`Error: Model "${s}" is not allowed. Permitted models: ${d.join(", ")}`:`Error: Default model "${s||"undefined"}" is not in allowedModels (${d.join(", ")}). Update your plugin config to set a compatible defaultModel.`};let u=Dt(o,a,d),m=Tt(e),g=xt(e,m||$e(r)),S=e.sessionKey||void 0,y=Zo(S)??Xo(e);if(!n.resume_session_id&&!n.force_new_session){let T=cd(t,{workdir:r,originSessionKey:S,originChannel:g,originThreadId:y});if(T.resumable.length>0||T.active.length>0){let F=T.resumable.length>0?["Linked resumable session(s) already exist for this thread/workdir:",_i(T.resumable),"","Resume the latest one with:",` agent_respond(session='${T.resumable[0].ref}', message='<next instruction>')`,"Fork from it with:",` agent_launch(prompt='<new task>', resume_session_id='${T.resumable[0].ref}', fork_session=true)`].join(`
|
|
11
|
+
`):"",V=T.active.length>0?[T.resumable.length>0?"Linked active session(s):":"Linked active session(s) already exist for this thread/workdir:",_i(T.active),"","Send a follow-up instead of launching a duplicate:",` agent_respond(session='${T.active[0].ref}', message='<next instruction>')`].join(`
|
|
11
12
|
`):"";return{kind:"blocked",text:["Resume-first protection blocked a fresh launch.","",F,V,["If you intentionally want a brand-new independent session here, call:"," agent_launch(prompt='<new task>', force_new_session=true)"].join(`
|
|
12
13
|
`)].filter(Boolean).join(`
|
|
13
14
|
|
|
14
|
-
`)}}}let c=n.resume_session_id,h=c?t.resolve?.(c):void 0,
|
|
15
|
-
`)}function
|
|
16
|
-
`)}function G(n,e){return e<=0?"":n.length<=e?n:e<=3?".".repeat(e):n.slice(0,e-3)+"..."}function
|
|
15
|
+
`)}}}let c=n.resume_session_id,h=c?t.resolve?.(c):void 0,P=c?t.getPersistedSession?.(c):void 0;if(c){let T=t.resolveBackendConversationId?.(c)??t.resolveHarnessSessionId?.(c);if(!T)return{kind:"error",text:`Error: Could not resolve resume_session_id "${c}" to a session ID. Use agent_sessions to list available sessions.`};c=T}let{resumeSessionId:b,clearedPersistedCodexResume:A}=qe({requestedResumeSessionId:c,activeSession:h?{harnessSessionId:h.backendConversationId??h.harnessSessionId}:void 0,persistedSession:P?{harness:P.harness,backendRef:P.backendRef}:void 0}),I=n.permission_mode??_.permissionMode,R=n.plan_approval??_.planApproval,f=rn({ctx:e,route:De(e,g,S),recoveredRouteSource:P,operation:"coding session"});if(f.kind==="error")return f;let v=f.route;return{kind:"resolved",workdir:r,harness:o,resolvedModel:u,permissionMode:I,planApproval:R,originChannel:f.recovered?v.accountId?`${v.provider}|${v.accountId}|${v.target}`:`${v.provider}|${v.target}`:g,originThreadId:f.recovered?v.threadId:y,originSessionKey:f.recovered?v.sessionKey:S,route:v,resumeSessionId:b,resolvedResumeId:c,clearedPersistedCodexResume:A,reasoningEffort:n.reasoning_effort??(h?.harnessName===o?h.reasoningEffort:void 0)??(P?.harness===o?P.reasoningEffort:void 0)??ce(o),fastMode:_e(o)}}function se(n){if(!Number.isFinite(n))return"0s";let e=Math.floor(n/1e3),t=Math.floor(e/60),r=e%60;return t>0?`${t}m${r}s`:`${r}s`}var pd=new Set(["a","an","the","is","are","was","were","be","been","being","have","has","had","do","does","did","will","would","could","should","may","might","shall","can","need","must","i","me","my","we","our","you","your","it","its","he","she","to","of","in","for","on","with","at","by","from","as","into","through","about","that","this","these","those","and","or","but","if","then","so","not","no","please","just","also","very","all","some","any","each","make","write","create","build","implement","add","update"]);function Ei(n){let t=n.toLowerCase().replace(/[^a-z0-9\s-]/g," ").split(/\s+/).filter(r=>r.length>1&&!pd.has(r)).slice(0,3);return t.length===0?"session":t.join("-")}var $i={starting:"🟡",running:"🟢",completed:"✅",failed:"❌",killed:"⛔",awaiting_plan_decision:"📋",awaiting_user_input:"❓",awaiting_worktree_decision:"🌿",suspended:"⏸️",terminal:"🏁"};function md(n){let e=n.worktreeLifecycle?.state??n.worktreeState;return n.worktreeMerged||e==="merged"?"[merged ✓]":e==="released"?"[released]":n.worktreePrUrl?`[PR: ${n.worktreePrUrl}]`:e==="pr_open"?"[PR open]":e==="dismissed"?"[dismissed]":e==="no_change"?"[no change]":"[not merged]"}function Ni(n){let e=$i[n.phase]??$i[n.status]??"❓",t=se(n.duration),r=n.multiTurn?"multi-turn":"single",o=n.prompt.length>80?n.prompt.slice(0,80)+"...":n.prompt,i=n.costUsd>0?` | $${n.costUsd.toFixed(2)}`:"",s=[`${e} ${n.name} [${n.id}] (${t}${i}) — ${r}`,` 📁 ${n.workdir}`,` 📝 "${o}"`];if(n.worktreePath&&n.worktreeBranch&&s.push(` 🌿 Worktree: ${n.worktreeBranch} ${md(n)}`),n.phase!==n.status&&s.push(` ⚙️ Phase: ${n.phase}`),n.lifecycle&&n.lifecycle!==n.phase&&s.push(` 🔄 Lifecycle: ${n.lifecycle}`),(n.requestedPermissionMode||n.currentPermissionMode||n.approvalExecutionState)&&s.push(` 🔐 Approval: ${n.approvalExecutionState??"unknown"} (requested=${n.requestedPermissionMode??"unknown"}, effective=${n.currentPermissionMode??"unknown"})`),n.resumable&&s.push(" ↩️ Resumable: yes"),n.recovered&&n.runtimeRecovery){let l=n.runtimeRecovery?.reason?` (${n.runtimeRecovery.reason})`:"";s.push(` ♻️ Recovered: persisted metadata only; no live process to kill${l}`),s.push(` 🩺 Recovery: raw=${n.runtimeRecovery.rawStatus??"unknown"}/${n.runtimeRecovery.rawLifecycle??"unknown"}/${n.runtimeRecovery.rawRuntimeState??"unknown"} → normalized=${n.runtimeRecovery.normalizedStatus}/${n.runtimeRecovery.normalizedLifecycle??"unknown"}/${n.runtimeRecovery.normalizedRuntimeState??"unknown"}`)}else n.recovered&&s.push(" 💾 Persisted: no live runtime process in this Gateway");n.harness?s.push(` 🧰 Harness | model: ${ge({harness:n.harness,model:n.model,reasoningEffort:n.reasoningEffort})}`):n.model&&s.push(` 🧰 Harness | model: ${n.model}`);let a=C(n);return a&&s.push(` 🔗 Backend ID: ${a}`),n.resumeSessionId&&s.push(` ↩️ Resumed from: ${n.resumeSessionId}${n.forkSession?" (forked)":""}`),s.join(`
|
|
16
|
+
`)}function sn(n,e){let t=n.sessionsWithDuration>0?n.totalDurationMs/n.sessionsWithDuration:0,{completed:r,failed:o,killed:i}=n.sessionsByStatus,s=["📊 OpenClaw Code Agent Stats","","📋 Sessions",` Launched: ${n.totalLaunched}`,` Running: ${e}`,` Completed: ${r}`,` Failed: ${o}`,` Killed: ${i}`,"",`⏱️ Average duration: ${t>0?se(t):"n/a"}`,`💵 Estimated API cost: $${n.totalCostUsd.toFixed(2)}`];if(n.mostExpensive){let a=n.mostExpensive;s.push("","🏆 Notable session",` ${a.name} [${a.id}]`,` 💵 $${a.costUsd.toFixed(2)}`,` 📝 "${a.prompt}"`)}return s.join(`
|
|
17
|
+
`)}function G(n,e){return e<=0?"":n.length<=e?n:e<=3?".".repeat(e):n.slice(0,e-3)+"..."}function Oi(n,e){let t=n.split(`
|
|
17
18
|
`),r=[],o=0;for(let i=t.length-1;i>=0;i--){let s=t[i].length+(r.length>0?1:0);if(o+s>e&&r.length>0)break;r.unshift(t[i]),o+=s}return r.join(`
|
|
18
|
-
`)}function
|
|
19
|
+
`)}function Wi(n,e){let t=n.split(`
|
|
19
20
|
`),r=[],o=0;for(let i=0;i<t.length;i++){let s=t[i].length+(r.length>0?1:0);if(o+s>e&&r.length>0)break;r.push(t[i]),o+=s}return r.join(`
|
|
20
|
-
`)}var sd="openclaw-code-agent",ad=160,en={create(){},progress(){},finalize(){}};function tn(n,e){let t=e instanceof Error?e.message:String(e);`${n}${t}`}function Lr(n,e){if(e.applied===!1){let t=e.code?` (${e.code})`:"";`${n}${t}`}}function ld(n){let e=n.prompt.trim().replace(/\s+/g," ");return G(e||n.name,ad)}function xi(n){if(n.status==="starting"||n.lifecycle==="starting")return"Starting";if(n.lifecycle==="active")return"Running";if(n.lifecycle==="awaiting_plan_decision")return"Waiting for plan approval";if(n.lifecycle==="awaiting_user_input")return"Waiting for input";if(n.lifecycle==="awaiting_worktree_decision")return"Waiting for worktree decision";if(n.lifecycle==="suspended")return"Suspended after idle timeout"}function Ci(n){if(n.status==="completed")return"succeeded";if(n.status==="failed")return"failed";if(n.status==="killed")return n.killReason==="idle-timeout"||n.killReason==="startup-timeout"?"timed_out":"cancelled"}function nn(n,e){return n==="completed"?"Completed":n==="failed"?"Failed":e==="idle-timeout"?"Timed out after idle timeout":e==="startup-timeout"?"Timed out during startup":e==="shutdown"?"Cancelled during shutdown":e==="user"?"Cancelled by user":"Cancelled"}function dd(n){return rn(n.lifecycle)}function rn(n){return n==="awaiting_plan_decision"||n==="awaiting_user_input"||n==="awaiting_worktree_decision"||n==="suspended"}function ud(n){if(n.pendingPlanApproval||n.pendingWorktreeDecisionSince)return!0;if(n.runtimeRecovery?.reason==="persisted-running-without-runtime"){let e=n.runtimeRecovery.rawLifecycle;return e==="suspended"?n.runtimeRecovery.rawResumable===!0||n.resumable===!0:rn(e)}return n.lifecycle==="suspended"?n.resumable===!0:rn(n.lifecycle)}function cd(n){if(n.runtimeRecovery?.reason==="persisted-running-without-runtime"){let e=n.runtimeRecovery.rawLifecycle;if(rn(e))return e}return n.lifecycle}function Fr(n,e,t){return{phase:e,integration:"phase-1-managed-task-flow",sessionId:n.id,sessionName:n.name,sessionStatus:n.status,sessionLifecycle:n.lifecycle,summary:t}}function pd(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.createManaged=="function"&&typeof e.resume=="function"&&typeof e.setWaiting=="function"&&typeof e.finish=="function"&&typeof e.fail=="function"}function on(n,e){if(e.applied)return e.flow;let t=e.current;return t?.flowId&&typeof t.revision=="number"?t:n}var Br=class{constructor(e){this.taskFlow=e}taskFlow;flow;finalized=!1;lastProgressKey;create(e){if(this.flow||this.finalized)return;let t="Starting",r=Date.now();try{this.flow=this.taskFlow.createManaged({controllerId:sd,goal:ld(e),status:"running",notifyPolicy:"silent",currentStep:t,stateJson:Fr(e,"created",t),createdAt:e.startedAt,updatedAt:r}),e.taskFlowMirror=this.flow,this.lastProgressKey=this.progressKey(e,t)}catch(o){tn("create",o)}}progress(e){if(!this.flow||this.finalized)return;let t=xi(e);if(!t)return;let r=this.progressKey(e,t);if(r!==this.lastProgressKey)try{let o=Fr(e,"progress",t),i=Date.now(),s=dd(e)?this.taskFlow.setWaiting({flowId:this.flow.flowId,expectedRevision:this.flow.revision,currentStep:t,stateJson:o,waitJson:{reason:t,sessionId:e.id},blockedSummary:t,updatedAt:i}):this.taskFlow.resume({flowId:this.flow.flowId,expectedRevision:this.flow.revision,status:"running",currentStep:t,stateJson:o,updatedAt:i});this.flow=on(this.flow,s),this.flow&&(e.taskFlowMirror=this.flow),this.lastProgressKey=r}catch(o){tn("progress",o)}}finalize(e){if(!this.flow||this.finalized)return;let t=Ci(e);if(!t)return;let r=t==="succeeded"?"Completed":t==="failed"?"Failed":nn(e.status,e.killReason);try{let o=e.completedAt??Date.now(),i={...Fr(e,"terminal",r),terminalStatus:t,terminalSummary:nn(e.status,e.killReason),...e.status==="failed"&&e.error?{error:e.error}:{}},s=t==="succeeded"?this.taskFlow.finish({flowId:this.flow.flowId,expectedRevision:this.flow.revision,stateJson:i,updatedAt:o,endedAt:o}):this.taskFlow.fail({flowId:this.flow.flowId,expectedRevision:this.flow.revision,stateJson:i,blockedSummary:nn(e.status,e.killReason),updatedAt:o,endedAt:o});Lr("finalize",s),this.flow=on(this.flow,s),this.flow&&(e.taskFlowMirror=this.flow),this.finalized=!0}catch(o){tn("finalize",o)}}progressKey(e,t){return`${e.status}:${e.lifecycle}:${t}`}};function md(n){return n==="succeeded"||n==="failed"||n==="cancelled"||n==="lost"}function fd(n){if(!n?.trim())return;let e=wr()?.fromToolContext;if(typeof e!="function")return;let t=e({sessionKey:n});if(!t||typeof t!="object")return;let r=t;if(!(typeof r.setWaiting!="function"||typeof r.finish!="function"||typeof r.fail!="function"))return{setWaiting:r.setWaiting,finish:r.finish,fail:r.fail}}function gd(n){return n.originSessionKey??n.route?.sessionKey}function hd(n){return n.runtimeRecovery?.reason==="persisted-running-without-runtime"?"Lost after OpenClaw Code Agent restart without live process":nn(n.status,n.killReason??"unknown")}function Mi(n){let e=n.taskFlowMirror?{...n.taskFlowMirror}:void 0;if(!e||md(e.status))return;let t=fd(gd(n));if(!t)return;let r=Date.now();if(ud(n)){let d=xi({status:n.status,lifecycle:cd(n)??"suspended"})??"Waiting",u=t.setWaiting({flowId:e.flowId,expectedRevision:e.revision,currentStep:d,stateJson:{phase:"progress",integration:"phase-1-managed-task-flow",sessionId:n.sessionId,sessionName:n.name,sessionStatus:n.status,sessionLifecycle:n.lifecycle,summary:d,reconciled:!0},waitJson:{reason:d,sessionId:n.sessionId},blockedSummary:d,updatedAt:r});return Lr("reconcile-waiting",u),on(e,u)}let o=Ci({status:n.status,killReason:n.killReason??"unknown"})??"failed",i=hd(n),s=n.completedAt??r,a={phase:"terminal",integration:"phase-1-managed-task-flow",sessionId:n.sessionId,sessionName:n.name,sessionStatus:n.status,sessionLifecycle:n.lifecycle,summary:i,terminalStatus:n.runtimeRecovery?.reason==="persisted-running-without-runtime"?"lost":o,terminalSummary:i,reconciled:!0,runtimeRecovery:n.runtimeRecovery},l=o==="succeeded"?t.finish({flowId:e.flowId,expectedRevision:e.revision,stateJson:a,updatedAt:s,endedAt:s}):t.fail({flowId:e.flowId,expectedRevision:e.revision,stateJson:a,blockedSummary:i,updatedAt:s,endedAt:s});return Lr("reconcile-terminal",l),on(e,l)}function sn(n){if(!n.sessionKey?.trim())return en;try{let e=wr()?.fromToolContext;if(typeof e!="function")return en;let t=e(n);return pd(t)?new Br(t):en}catch(e){return tn("resolve",e),en}}function yd(...n){let e;for(let t of n)t!=null&&(e=e==null?t:Math.max(e,t));return e}function Kr(n){if(n.actionablePlanDecisionVersion!=null)return n.actionablePlanDecisionVersion;let e=yd(n.approvalPromptRequiredVersion,n.approvalPromptVersion);return e??n.canonicalPlanPromptVersion??n.planDecisionVersion}function qr(n,e){return n.kind==="plan-approve"&&n.planDecisionVersion!=null&&e.approvalState==="approved"&&!e.pendingPlanApproval&&e.planDecisionVersion===n.planDecisionVersion+1}function _i(n,e){return n.worktreePath&&e&&!e.worktreePath?{...n,worktreePath:void 0,worktreeBranch:void 0,worktreeState:e.worktreeState,worktreeDisposition:e.worktreeDisposition,worktreeLifecycle:e.worktreeLifecycle,pendingWorktreeDecisionSince:e.pendingWorktreeDecisionSince,lastWorktreeReminderAt:e.lastWorktreeReminderAt,worktreeDecisionSnoozedUntil:e.worktreeDecisionSnoozedUntil,worktreeMerged:e.worktreeMerged,worktreeMergedAt:e.worktreeMergedAt,worktreeDismissedAt:e.worktreeDismissedAt,worktreeRemoteOutcome:e.worktreeRemoteOutcome}:n}function an(n,e){let t=Kr(n);if(!n.pendingPlanApproval)return{permissionMode:e,approvalApplied:!1,patch:{}};let r={planApprovalContext:n.planApprovalContext,canonicalPlanPromptVersion:n.canonicalPlanPromptVersion,approvalPromptRequiredVersion:n.approvalPromptRequiredVersion,approvalPromptVersion:n.approvalPromptVersion,approvalPromptStatus:n.approvalPromptStatus,approvalPromptTransport:n.approvalPromptTransport,approvalPromptMessageKind:n.approvalPromptMessageKind,approvalPromptLastAttemptAt:n.approvalPromptLastAttemptAt,approvalPromptDeliveredAt:n.approvalPromptDeliveredAt,approvalPromptFailedAt:n.approvalPromptFailedAt};return e==="bypassPermissions"&&n.actionablePlanDecisionVersion!=null&&n.actionablePlanDecisionVersion>0&&t===n.actionablePlanDecisionVersion&&n.approvalState!=="rejected"?{permissionMode:"bypassPermissions",approvalApplied:!0,decisionVersion:t,patch:{...r,pendingPlanApproval:!1,approvalState:"approved",approvalExecutionState:"awaiting_plan_output",planModeApproved:!0,planDecisionVersion:t+1,actionablePlanDecisionVersion:void 0}}:{permissionMode:"plan",approvalApplied:!1,decisionVersion:t,patch:{...r,pendingPlanApproval:!0,approvalState:n.approvalState??"pending",planDecisionVersion:t??n.planDecisionVersion,actionablePlanDecisionVersion:n.actionablePlanDecisionVersion}}}function Sd(n){return n instanceof Error?n.message:String(n)}function Di(n,e){return n?("id"in n?n.id:n.sessionId)??e??"unknown-session":e??"unknown-session"}function kd(n){return!n||typeof n!="object"?!1:typeof n.prompt=="string"}function vd(n){return!!n&&typeof n=="object"&&typeof n.formatLaunchResult=="function"}function Pd(n){return!!n&&typeof n=="object"&&typeof n.checkRepoPolicyForLaunch=="function"&&typeof n.requestRepoPolicyForLaunch=="function"}function $i(n){return{name:"agent_launch",description:"Launch a coding agent session in background to execute a development task. Sessions are multi-turn — they stay open for follow-up messages via agent_respond. Supports resuming previous sessions. Returns a session ID and name for tracking.",parameters:p.Object({prompt:p.String({description:"The task prompt to execute"}),name:p.Optional(p.String({description:"Short human-readable name for the session (kebab-case, e.g. 'fix-auth'). Auto-generated from prompt if omitted."})),workdir:p.Optional(p.String({description:"Working directory (defaults to cwd)"})),model:p.Optional(p.String({description:"Model name to use"})),system_prompt:p.Optional(p.String({description:"Additional system prompt"})),allowed_tools:p.Optional(p.Array(p.String(),{description:"List of allowed tools"})),resume_session_id:p.Optional(p.String({description:"Session reference to continue or fork from. Prefer the plugin session ID shown by agent_launch or agent_sessions; persisted backend conversation IDs also resolve when available."})),fork_session:p.Optional(p.Boolean({description:"When resuming, fork to a new session instead of continuing the existing one. Use with resume_session_id."})),force_new_session:p.Optional(p.Boolean({description:"Bypass resume-first protection and start a brand-new linked session even when a resumable or active linked session already exists."})),permission_mode:p.Optional(p.Union([p.Literal("default"),p.Literal("plan"),p.Literal("bypassPermissions")],{description:"Permission mode: 'default' (plugin-managed interactive execution), 'plan' (present the plan first and block implementation until approval), 'bypassPermissions' (fully autonomous execution). Defaults to plugin config ('plan' by default)."})),plan_approval:p.Optional(p.Union([p.Literal("ask"),p.Literal("delegate"),p.Literal("approve")],{description:"Plan approval policy for this session: 'ask' (send a decision-grade plan brief to the user with Approve/Revise/Reject buttons), 'delegate' (orchestrator must review the full plan, then either approve directly or escalate back to the user with the same decision brief and buttons), 'approve' (auto-approve after verification). Overrides the plugin-level planApproval setting."})),harness:p.Optional(p.String({description:"Agent harness to use ('claude-code', 'codex', or experimental 'opencode'). Defaults to the plugin defaultHarness."})),worktree_strategy:p.Optional(p.Union([p.Literal("off"),p.Literal("manual"),p.Literal("ask"),p.Literal("delegate"),p.Literal("auto-merge"),p.Literal("auto-pr")],{description:"Worktree strategy: 'off' (no worktree), 'manual' (create worktree but no auto merge-back), 'ask' (prompt user with Merge/Open PR/Later/Discard buttons), 'delegate' (orchestrator decides), 'auto-merge' (merge automatically), 'auto-pr' (open/update a PR automatically). Defaults to the plugin config when unset."})),worktree_base_branch:p.Optional(p.String({description:"Base branch for worktree merge/PR operations (default: auto-detected or 'main')"})),worktree_pr_target_repo:p.Optional(p.String({description:"Target repository for cross-repo PRs (e.g. 'openai/codex' for fork-to-upstream workflow). If not set, auto-detected from 'upstream' remote or defaults to 'origin'."}))}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};if(!kd(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected at least { prompt }."}]};t.agentId&&`${t.agentId}${n.agentId}`;try{let r=Xt(t,n,k);if(r.kind!=="resolved")return{content:[{type:"text",text:r.text}]};let{workdir:o,harness:i,resolvedModel:s,permissionMode:a,planApproval:l,originChannel:d,originThreadId:u,originSessionKey:f,route:g,resumeSessionId:S,resolvedResumeId:y,clearedPersistedCodexResume:c,reasoningEffort:h,fastMode:v}=r,b=t.resume_session_id?k.resolve(t.resume_session_id)??k.getPersistedSession(t.resume_session_id):void 0,A=!t.fork_session&&b?ke(b):void 0;if(A?.kind==="direct")return{content:[{type:"text",text:`Session ${b.name} [${Di(b,t.resume_session_id)}] is already running. Use agent_respond(session='${t.resume_session_id}', message='<next instruction>') instead of agent_launch(resume_session_id=...).`}]};if(A?.kind==="unavailable")return{content:[{type:"text",text:`Resume unavailable for session ${b.name} [${Di(b,t.resume_session_id)}] (${A.reason}). Use agent_launch(prompt='<new task>') for a fresh session or set fork_session=true to fork from prior context.`}]};let I=b&&"resumedFromSessionName"in b?b.resumedFromSessionName:void 0,R=A?.kind==="resume"&&!t.fork_session?I??b?.name:void 0,m=R&&!t.name?R:t.name,w=t.worktree_strategy??_.defaultWorktreeStrategy??"off",T=t.fork_session?void 0:A?.kind==="resume"||A?.kind==="relaunch"?A.stableSessionId:void 0,F=A?.kind==="resume"&&!t.fork_session&&b?an(b,a):{permissionMode:a,approvalApplied:!1,patch:{}};if(w!=="off"&&Pd(k)&&k.checkRepoPolicyForLaunch(o,t.worktree_strategy).ok===!1)return{content:[{type:"text",text:k.requestRepoPolicyForLaunch({route:g,prompt:t.prompt,workdir:o,name:m,model:s,reasoningEffort:h,fastMode:v,systemPrompt:t.system_prompt,allowedTools:t.allowed_tools,resumeSessionId:A?.kind==="resume"?A.resumeSessionId:S,resumedFromSessionName:R,resumeWorktreeFrom:T??t.resume_session_id??y,sessionIdOverride:T,clearedPersistedCodexResume:c,forkSession:S?t.fork_session:!1,forceNewSession:t.force_new_session,permissionMode:a,planApproval:l,harness:i,worktreeStrategy:t.worktree_strategy,worktreeBaseBranch:t.worktree_base_branch,worktreePrTargetRepo:t.worktree_pr_target_repo,originAgentId:n.agentId||void 0})}]};let V=k.spawn({prompt:t.prompt,sessionIdOverride:T,name:m,workdir:o,model:s,reasoningEffort:h,fastMode:v,systemPrompt:t.system_prompt,allowedTools:t.allowed_tools,resumeSessionId:A?.kind==="resume"?A.resumeSessionId:S,resumedFromSessionName:R,resumeWorktreeFrom:T??t.resume_session_id??y,forkSession:S?t.fork_session:!1,multiTurn:!0,permissionMode:F.permissionMode,planApproval:l,codexApprovalPolicy:i==="codex"?"never":void 0,...F.patch,originChannel:d,originThreadId:u,originAgentId:n.agentId||void 0,originSessionKey:f,route:g,harness:i,taskLifecycle:sn(n),worktreeStrategy:t.worktree_strategy,worktreeBaseBranch:t.worktree_base_branch,worktreePrTargetRepo:t.worktree_pr_target_repo});return{content:[{type:"text",text:vd(k)?k.formatLaunchResult({prompt:t.prompt,workdir:o,harness:i,permissionMode:F.permissionMode,planApproval:l,forceNewSession:t.force_new_session,resumeSessionId:t.resume_session_id,resumeSessionName:R,forkSession:t.fork_session,clearedPersistedCodexResume:c},V):Ht({prompt:t.prompt,workdir:o,harness:i,permissionMode:F.permissionMode,planApproval:l,resumeSessionId:t.resume_session_id,resumeSessionName:R,forkSession:t.fork_session,forceNewSession:t.force_new_session,clearedPersistedCodexResume:c},{id:V.id,name:V.name,model:V.model,reasoningEffort:V.reasoningEffort??ce(i),fastMode:V.fastMode??v,worktreeStrategy:V.worktreeStrategy??t.worktree_strategy??_.defaultWorktreeStrategy??"off",repoIntegrationPolicy:V.repoIntegrationPolicy,repoProvider:V.repoProvider,worktreePath:V.worktreePath,originalWorkdir:V.originalWorkdir})}]}}catch(r){let o=Sd(r),i=o.includes("Max sessions")?"":`
|
|
21
|
+
`)}var fd="openclaw-code-agent",gd=160,an={create(){},progress(){},finalize(){}};function ln(n,e){let t=e instanceof Error?e.message:String(e);`${n}${t}`}function jr(n,e){if(e.applied===!1){let t=e.code?` (${e.code})`:"";`${n}${t}`}}function hd(n){let e=n.prompt.trim().replace(/\s+/g," ");return G(e||n.name,gd)}function Fi(n){if(n.status==="starting"||n.lifecycle==="starting")return"Starting";if(n.lifecycle==="active")return"Running";if(n.lifecycle==="awaiting_plan_decision")return"Waiting for plan approval";if(n.lifecycle==="awaiting_user_input")return"Waiting for input";if(n.lifecycle==="awaiting_worktree_decision")return"Waiting for worktree decision";if(n.lifecycle==="suspended")return"Suspended after idle timeout"}function Li(n){if(n.status==="completed")return"succeeded";if(n.status==="failed")return"failed";if(n.status==="killed")return n.killReason==="idle-timeout"||n.killReason==="startup-timeout"?"timed_out":"cancelled"}function dn(n,e){return n==="completed"?"Completed":n==="failed"?"Failed":e==="idle-timeout"?"Timed out after idle timeout":e==="startup-timeout"?"Timed out during startup":e==="shutdown"?"Cancelled during shutdown":e==="user"?"Cancelled by user":"Cancelled"}function yd(n){return un(n.lifecycle)}function un(n){return n==="awaiting_plan_decision"||n==="awaiting_user_input"||n==="awaiting_worktree_decision"||n==="suspended"}function Sd(n){if(n.pendingPlanApproval||n.pendingWorktreeDecisionSince)return!0;if(n.runtimeRecovery?.reason==="persisted-running-without-runtime"){let e=n.runtimeRecovery.rawLifecycle;return e==="suspended"?n.runtimeRecovery.rawResumable===!0||n.resumable===!0:un(e)}return n.lifecycle==="suspended"?n.resumable===!0:un(n.lifecycle)}function kd(n){if(n.runtimeRecovery?.reason==="persisted-running-without-runtime"){let e=n.runtimeRecovery.rawLifecycle;if(un(e))return e}return n.lifecycle}function Gr(n,e,t){return{phase:e,integration:"phase-1-managed-task-flow",sessionId:n.id,sessionName:n.name,sessionStatus:n.status,sessionLifecycle:n.lifecycle,summary:t}}function vd(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.createManaged=="function"&&typeof e.resume=="function"&&typeof e.setWaiting=="function"&&typeof e.finish=="function"&&typeof e.fail=="function"}function cn(n,e){if(e.applied)return e.flow;let t=e.current;return t?.flowId&&typeof t.revision=="number"?t:n}var zr=class{constructor(e){this.taskFlow=e}taskFlow;flow;finalized=!1;lastProgressKey;create(e){if(this.flow||this.finalized)return;let t="Starting",r=Date.now();try{this.flow=this.taskFlow.createManaged({controllerId:fd,goal:hd(e),status:"running",notifyPolicy:"silent",currentStep:t,stateJson:Gr(e,"created",t),createdAt:e.startedAt,updatedAt:r}),e.taskFlowMirror=this.flow,this.lastProgressKey=this.progressKey(e,t)}catch(o){ln("create",o)}}progress(e){if(!this.flow||this.finalized)return;let t=Fi(e);if(!t)return;let r=this.progressKey(e,t);if(r!==this.lastProgressKey)try{let o=Gr(e,"progress",t),i=Date.now(),s=yd(e)?this.taskFlow.setWaiting({flowId:this.flow.flowId,expectedRevision:this.flow.revision,currentStep:t,stateJson:o,waitJson:{reason:t,sessionId:e.id},blockedSummary:t,updatedAt:i}):this.taskFlow.resume({flowId:this.flow.flowId,expectedRevision:this.flow.revision,status:"running",currentStep:t,stateJson:o,updatedAt:i});this.flow=cn(this.flow,s),this.flow&&(e.taskFlowMirror=this.flow),this.lastProgressKey=r}catch(o){ln("progress",o)}}finalize(e){if(!this.flow||this.finalized)return;let t=Li(e);if(!t)return;let r=t==="succeeded"?"Completed":t==="failed"?"Failed":dn(e.status,e.killReason);try{let o=e.completedAt??Date.now(),i={...Gr(e,"terminal",r),terminalStatus:t,terminalSummary:dn(e.status,e.killReason),...e.status==="failed"&&e.error?{error:e.error}:{}},s=t==="succeeded"?this.taskFlow.finish({flowId:this.flow.flowId,expectedRevision:this.flow.revision,stateJson:i,updatedAt:o,endedAt:o}):this.taskFlow.fail({flowId:this.flow.flowId,expectedRevision:this.flow.revision,stateJson:i,blockedSummary:dn(e.status,e.killReason),updatedAt:o,endedAt:o});jr("finalize",s),this.flow=cn(this.flow,s),this.flow&&(e.taskFlowMirror=this.flow),this.finalized=!0}catch(o){ln("finalize",o)}}progressKey(e,t){return`${e.status}:${e.lifecycle}:${t}`}};function Pd(n){return n==="succeeded"||n==="failed"||n==="cancelled"||n==="lost"}function wd(n){if(!n?.trim())return;let e=Tr()?.fromToolContext;if(typeof e!="function")return;let t=e({sessionKey:n});if(!t||typeof t!="object")return;let r=t;if(!(typeof r.setWaiting!="function"||typeof r.finish!="function"||typeof r.fail!="function"))return{setWaiting:r.setWaiting,finish:r.finish,fail:r.fail}}function bd(n){return n.originSessionKey??n.route?.sessionKey}function Rd(n){return n.runtimeRecovery?.reason==="persisted-running-without-runtime"?"Lost after OpenClaw Code Agent restart without live process":dn(n.status,n.killReason??"unknown")}function Bi(n){let e=n.taskFlowMirror?{...n.taskFlowMirror}:void 0;if(!e||Pd(e.status))return;let t=wd(bd(n));if(!t)return;let r=Date.now();if(Sd(n)){let d=Fi({status:n.status,lifecycle:kd(n)??"suspended"})??"Waiting",u=t.setWaiting({flowId:e.flowId,expectedRevision:e.revision,currentStep:d,stateJson:{phase:"progress",integration:"phase-1-managed-task-flow",sessionId:n.sessionId,sessionName:n.name,sessionStatus:n.status,sessionLifecycle:n.lifecycle,summary:d,reconciled:!0},waitJson:{reason:d,sessionId:n.sessionId},blockedSummary:d,updatedAt:r});return jr("reconcile-waiting",u),cn(e,u)}let o=Li({status:n.status,killReason:n.killReason??"unknown"})??"failed",i=Rd(n),s=n.completedAt??r,a={phase:"terminal",integration:"phase-1-managed-task-flow",sessionId:n.sessionId,sessionName:n.name,sessionStatus:n.status,sessionLifecycle:n.lifecycle,summary:i,terminalStatus:n.runtimeRecovery?.reason==="persisted-running-without-runtime"?"lost":o,terminalSummary:i,reconciled:!0,runtimeRecovery:n.runtimeRecovery},l=o==="succeeded"?t.finish({flowId:e.flowId,expectedRevision:e.revision,stateJson:a,updatedAt:s,endedAt:s}):t.fail({flowId:e.flowId,expectedRevision:e.revision,stateJson:a,blockedSummary:i,updatedAt:s,endedAt:s});return jr("reconcile-terminal",l),cn(e,l)}function pn(n){if(!n.sessionKey?.trim())return an;try{let e=Tr()?.fromToolContext;if(typeof e!="function")return an;let t=e(n);return vd(t)?new zr(t):an}catch(e){return ln("resolve",e),an}}function Ad(...n){let e;for(let t of n)t!=null&&(e=e==null?t:Math.max(e,t));return e}function Hr(n){if(n.actionablePlanDecisionVersion!=null)return n.actionablePlanDecisionVersion;let e=Ad(n.approvalPromptRequiredVersion,n.approvalPromptVersion);return e??n.canonicalPlanPromptVersion??n.planDecisionVersion}function Qr(n,e){return n.kind==="plan-approve"&&n.planDecisionVersion!=null&&e.approvalState==="approved"&&!e.pendingPlanApproval&&e.planDecisionVersion===n.planDecisionVersion+1}function Ki(n,e){return n.worktreePath&&e&&!e.worktreePath?{...n,worktreePath:void 0,worktreeBranch:void 0,worktreeState:e.worktreeState,worktreeDisposition:e.worktreeDisposition,worktreeLifecycle:e.worktreeLifecycle,pendingWorktreeDecisionSince:e.pendingWorktreeDecisionSince,lastWorktreeReminderAt:e.lastWorktreeReminderAt,worktreeDecisionSnoozedUntil:e.worktreeDecisionSnoozedUntil,worktreeMerged:e.worktreeMerged,worktreeMergedAt:e.worktreeMergedAt,worktreeDismissedAt:e.worktreeDismissedAt,worktreeRemoteOutcome:e.worktreeRemoteOutcome}:n}function mn(n,e){let t=Hr(n);if(!n.pendingPlanApproval)return{permissionMode:e,approvalApplied:!1,patch:{}};let r={planApprovalContext:n.planApprovalContext,canonicalPlanPromptVersion:n.canonicalPlanPromptVersion,approvalPromptRequiredVersion:n.approvalPromptRequiredVersion,approvalPromptVersion:n.approvalPromptVersion,approvalPromptStatus:n.approvalPromptStatus,approvalPromptTransport:n.approvalPromptTransport,approvalPromptMessageKind:n.approvalPromptMessageKind,approvalPromptLastAttemptAt:n.approvalPromptLastAttemptAt,approvalPromptDeliveredAt:n.approvalPromptDeliveredAt,approvalPromptFailedAt:n.approvalPromptFailedAt};return e==="bypassPermissions"&&n.actionablePlanDecisionVersion!=null&&n.actionablePlanDecisionVersion>0&&t===n.actionablePlanDecisionVersion&&n.approvalState!=="rejected"?{permissionMode:"bypassPermissions",approvalApplied:!0,decisionVersion:t,patch:{...r,pendingPlanApproval:!1,approvalState:"approved",approvalExecutionState:"awaiting_plan_output",planModeApproved:!0,planDecisionVersion:t+1,actionablePlanDecisionVersion:void 0}}:{permissionMode:"plan",approvalApplied:!1,decisionVersion:t,patch:{...r,pendingPlanApproval:!0,approvalState:n.approvalState??"pending",planDecisionVersion:t??n.planDecisionVersion,actionablePlanDecisionVersion:n.actionablePlanDecisionVersion}}}function Id(n){return n instanceof Error?n.message:String(n)}function qi(n,e){return n?("id"in n?n.id:n.sessionId)??e??"unknown-session":e??"unknown-session"}function Td(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.prompt=="string"&&(e.reasoning_effort===void 0||Ke.includes(e.reasoning_effort))}function xd(n){return!!n&&typeof n=="object"&&typeof n.formatLaunchResult=="function"}function Cd(n){return!!n&&typeof n=="object"&&typeof n.checkRepoPolicyForLaunch=="function"&&typeof n.requestRepoPolicyForLaunch=="function"}function Vi(n){return{name:"agent_launch",description:"Launch a coding agent session in background to execute a development task. Sessions are multi-turn — they stay open for follow-up messages via agent_respond. Supports resuming previous sessions. Returns a session ID and name for tracking.",parameters:p.Object({prompt:p.String({description:"The task prompt to execute"}),name:p.Optional(p.String({description:"Short human-readable name for the session (kebab-case, e.g. 'fix-auth'). Auto-generated from prompt if omitted."})),workdir:p.Optional(p.String({description:"Working directory (defaults to cwd)"})),model:p.Optional(p.String({description:"Model name to use"})),reasoning_effort:p.Optional(p.Union(Ke.map(e=>p.Literal(e)),{description:"Reasoning effort override for this launch. Otherwise retains the resumed/forked session setting, then uses the harness default. Only supported harness/model combinations apply it."})),system_prompt:p.Optional(p.String({description:"Additional system prompt"})),allowed_tools:p.Optional(p.Array(p.String(),{description:"List of allowed tools"})),resume_session_id:p.Optional(p.String({description:"Session reference to continue or fork from. Prefer the plugin session ID shown by agent_launch or agent_sessions; persisted backend conversation IDs also resolve when available."})),fork_session:p.Optional(p.Boolean({description:"When resuming, fork to a new session instead of continuing the existing one. Use with resume_session_id."})),force_new_session:p.Optional(p.Boolean({description:"Bypass resume-first protection and start a brand-new linked session even when a resumable or active linked session already exists."})),permission_mode:p.Optional(p.Union([p.Literal("default"),p.Literal("plan"),p.Literal("bypassPermissions")],{description:"Permission mode: 'default' (plugin-managed interactive execution), 'plan' (present the plan first and block implementation until approval), 'bypassPermissions' (fully autonomous execution). Defaults to plugin config ('plan' by default)."})),plan_approval:p.Optional(p.Union([p.Literal("ask"),p.Literal("delegate"),p.Literal("approve")],{description:"Plan approval policy for this session: 'ask' (send a decision-grade plan brief to the user with Approve/Revise/Reject buttons), 'delegate' (orchestrator must review the full plan, then either approve directly or escalate back to the user with the same decision brief and buttons), 'approve' (auto-approve after verification). Overrides the plugin-level planApproval setting."})),harness:p.Optional(p.String({description:"Agent harness to use ('claude-code', 'codex', or experimental 'opencode'). Defaults to the plugin defaultHarness."})),worktree_strategy:p.Optional(p.Union([p.Literal("off"),p.Literal("manual"),p.Literal("ask"),p.Literal("delegate"),p.Literal("auto-merge"),p.Literal("auto-pr")],{description:"Worktree strategy: 'off' (no worktree), 'manual' (create worktree but no auto merge-back), 'ask' (prompt user with Merge/Open PR/Later/Discard buttons), 'delegate' (orchestrator decides), 'auto-merge' (merge automatically), 'auto-pr' (open/update a PR automatically). Defaults to the plugin config when unset."})),worktree_base_branch:p.Optional(p.String({description:"Literal Git branch name for worktree merge/PR operations; options and revision expressions are rejected (default: auto-detected or 'main')"})),worktree_pr_target_repo:p.Optional(p.String({description:"Target repository for cross-repo PRs (e.g. 'openai/codex' for fork-to-upstream workflow). If not set, auto-detected from 'upstream' remote or defaults to 'origin'."}))}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};if(!Td(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected at least { prompt }."}]};if(t.worktree_base_branch!==void 0){let r=Ee(t.worktree_base_branch);if(r)return{content:[{type:"text",text:`Error: ${r}`}]}}t.agentId&&`${t.agentId}${n.agentId}`;try{let r=on(t,n,k);if(r.kind!=="resolved")return{content:[{type:"text",text:r.text}]};let{workdir:o,harness:i,resolvedModel:s,permissionMode:a,planApproval:l,originChannel:d,originThreadId:u,originSessionKey:m,route:g,resumeSessionId:S,resolvedResumeId:y,clearedPersistedCodexResume:c,reasoningEffort:h,fastMode:P}=r,b=t.resume_session_id?k.resolve(t.resume_session_id)??k.getPersistedSession(t.resume_session_id):void 0,A=!t.fork_session&&b?ke(b):void 0;if(A?.kind==="direct")return{content:[{type:"text",text:`Session ${b.name} [${qi(b,t.resume_session_id)}] is already running. Use agent_respond(session='${t.resume_session_id}', message='<next instruction>') instead of agent_launch(resume_session_id=...).`}]};if(A?.kind==="unavailable")return{content:[{type:"text",text:`Resume unavailable for session ${b.name} [${qi(b,t.resume_session_id)}] (${A.reason}). Use agent_launch(prompt='<new task>') for a fresh session or set fork_session=true to fork from prior context.`}]};let I=b&&"resumedFromSessionName"in b?b.resumedFromSessionName:void 0,R=A?.kind==="resume"&&!t.fork_session?I??b?.name:void 0,f=R&&!t.name?R:t.name,v=t.worktree_strategy??_.defaultWorktreeStrategy??"off",T=t.fork_session?void 0:A?.kind==="resume"||A?.kind==="relaunch"?A.stableSessionId:void 0,F=A?.kind==="resume"&&!t.fork_session&&b?mn(b,a):{permissionMode:a,approvalApplied:!1,patch:{}};if(v!=="off"&&Cd(k)&&k.checkRepoPolicyForLaunch(o,t.worktree_strategy).ok===!1)return{content:[{type:"text",text:k.requestRepoPolicyForLaunch({route:g,prompt:t.prompt,workdir:o,name:f,model:s,reasoningEffort:h,fastMode:P,systemPrompt:t.system_prompt,allowedTools:t.allowed_tools,resumeSessionId:A?.kind==="resume"?A.resumeSessionId:S,resumedFromSessionName:R,resumeWorktreeFrom:T??t.resume_session_id??y,sessionIdOverride:T,clearedPersistedCodexResume:c,forkSession:S?t.fork_session:!1,forceNewSession:t.force_new_session,permissionMode:a,planApproval:l,harness:i,worktreeStrategy:t.worktree_strategy,worktreeBaseBranch:t.worktree_base_branch,worktreePrTargetRepo:t.worktree_pr_target_repo,originAgentId:n.agentId||void 0})}]};let V=k.spawn({prompt:t.prompt,sessionIdOverride:T,name:f,workdir:o,model:s,reasoningEffort:h,fastMode:P,systemPrompt:t.system_prompt,allowedTools:t.allowed_tools,resumeSessionId:A?.kind==="resume"?A.resumeSessionId:S,resumedFromSessionName:R,resumeWorktreeFrom:T??t.resume_session_id??y,forkSession:S?t.fork_session:!1,multiTurn:!0,permissionMode:F.permissionMode,planApproval:l,codexApprovalPolicy:i==="codex"?"never":void 0,...F.patch,originChannel:d,originThreadId:u,originAgentId:n.agentId||void 0,originSessionKey:m,route:g,harness:i,taskLifecycle:pn(n),worktreeStrategy:t.worktree_strategy,worktreeBaseBranch:t.worktree_base_branch,worktreePrTargetRepo:t.worktree_pr_target_repo});return{content:[{type:"text",text:xd(k)?k.formatLaunchResult({prompt:t.prompt,workdir:o,harness:i,permissionMode:F.permissionMode,planApproval:l,forceNewSession:t.force_new_session,resumeSessionId:t.resume_session_id,resumeSessionName:R,forkSession:t.fork_session,clearedPersistedCodexResume:c},V):en({prompt:t.prompt,workdir:o,harness:i,permissionMode:F.permissionMode,planApproval:l,resumeSessionId:t.resume_session_id,resumeSessionName:R,forkSession:t.fork_session,forceNewSession:t.force_new_session,clearedPersistedCodexResume:c},{id:V.id,name:V.name,model:V.model,reasoningEffort:V.reasoningEffort??ce(i),fastMode:V.fastMode??P,worktreeStrategy:V.worktreeStrategy??t.worktree_strategy??_.defaultWorktreeStrategy??"off",repoIntegrationPolicy:V.repoIntegrationPolicy,repoProvider:V.repoProvider,worktreePath:V.worktreePath,originalWorkdir:V.originalWorkdir})}]}}catch(r){let o=Id(r),i=o.includes("Max sessions")?"":`
|
|
21
22
|
|
|
22
|
-
Use agent_sessions to see active sessions and their status.`;return{content:[{type:"text",text:`Error launching session: ${o}${i}`}]}}}}}import{existsSync as
|
|
23
|
-
`),[r="",...o]=t;n.length===0?n.push(r):n[n.length-1]+=r;for(let i of o)n.push(i)}function
|
|
24
|
-
${e}`:e,this.deps.emitOutput(e)}noteToolCall(e){let{name:t,input:r,currentPermissionMode:o,permissionMode:i,planModeApproved:s}=e;if(t==="Write"){let a=r;typeof a?.file_path=="string"&&a.file_path.includes("/.claude/plans/")&&this.deps.setPlanFilePath(a.file_path)}t==="AskUserQuestion"?(this.lastTurnHadQuestion=!0,this.deps.applyInputRequested(),(o==="plan"||i==="plan")&&!s&&this.deps.markPendingPlanApproval("plan-mode")):(t==="ExitPlanMode"||t==="set_permission_mode")&&!s&&(this.lastTurnHadQuestion=!0,this.deps.markPendingPlanApproval("plan-mode")),this.deps.emitToolUse(t,r)}notePendingInput(e){this.lastTurnHadQuestion=!0,this.deps.applyInputRequested();let t=e?.activeQuestionIndex??0,r=e?.questions?.[t],o=e?.requestId?`${e.requestId}:${r?.id??(e.activeQuestionIndex!=null?`q${e.activeQuestionIndex}`:"request")}`:void 0;(!this.waitingForInputFired||o&&o!==this.pendingInputNotificationIdentity)&&(this.waitingForInputFired=!0,this.pendingInputNotificationIdentity=o,this.deps.emitTurnEnd(!0))}clearResolvedPendingInput(e,t){if(!e||t?.requestId===e){this.pendingInputNotificationIdentity=void 0;return}return t}notePlanArtifact(e,t){if(this.currentTurnPlanArtifact=e,this.deps.setLatestPlanArtifact(e),!t)return;let r=e.markdown.trim();!r||this.currentTurnText.trim()===r||(this.deps.appendOutput(r),this.currentTurnText=r,this.deps.emitOutput(r))}noteSettingsChanged(e){let{oldMode:t,permissionMode:r,planModeApproved:o}=e;if(r)return r!=="plan"&&t==="plan"&&!o&&(this.deps.markPendingPlanApproval("plan-mode"),this.lastTurnHadQuestion=!0),r}finishSuccessfulTurn(e){let{currentPermissionMode:t,permissionMode:r,pendingPlanApproval:o,planModeApproved:i,pendingInputState:s,hasPendingMessages:a}=e,l=o;(t==="plan"||r==="plan")&&!l&&!i&&(this.deps.markPendingPlanApproval("plan-mode"),l=!0);let d=l||this.lastTurnHadQuestion||!!s;if(this.turnInProgress=a,d&&!this.waitingForInputFired)this.waitingForInputFired=!0,l||this.deps.markAwaitingUserInput(),this.deps.emitTurnEnd(!0);else if(!a&&!d){if(this.deps.queueWorktreeFinalizationPrompt?.()){this.turnInProgress=!0;return}this.deps.completeTurn(),this.deps.emitTurnEnd(!1)}}finishTerminalTurn(){this.turnInProgress=!1}finishInterruptedTurn(e){this.turnInProgress=e,this.waitingForInputFired=!1,this.lastTurnHadQuestion=!1,this.currentTurnText="",this.currentTurnPlanArtifact=void 0}resetAfterRun(){this.lastTurnHadQuestion=!1,this.currentTurnText="",this.currentTurnPlanArtifact=void 0}};var pn=class{constructor(e){this.deps=e}deps;applyMessage(e,t){if(e.type==="backend_ref"){this.deps.clearStartupTimer(),this.deps.assignBackendRef({...e.ref}),this.deps.transitionRunning();return}if(e.type==="run_started"){e.runId&&this.deps.noteRunStarted(e.runId);return}if(e.type==="text_delta"){this.deps.noteTextDelta(e.text,t.pendingPlanApproval);return}if(e.type==="tool_call"){this.deps.noteToolCall({name:e.name,input:e.input,currentPermissionMode:t.currentPermissionMode,permissionMode:t.permissionMode,planModeApproved:t.planModeApproved});return}if(e.type==="pending_input"){this.deps.setPendingInputState(e.state),this.deps.notePendingInput(e.state);return}if(e.type==="pending_input_resolved"){this.deps.setPendingInputState(this.deps.clearResolvedPendingInput(e.requestId,t.pendingInputState));return}if(e.type==="plan_artifact"){this.deps.notePlanArtifact(e);return}if(e.type==="settings_changed"){let r=this.deps.noteSettingsChanged({oldMode:t.currentPermissionMode,permissionMode:e.permissionMode,planModeApproved:t.planModeApproved});r&&this.deps.setCurrentPermissionMode(r);return}if(e.type==="run_completed"){this.deps.handleRunCompleted(e.data);return}e.type}};var Dd=[/\bfailed to authenticate\b/i,/\binvalid bearer token\b/i,/\bapi error:\s*401\b/i,/\b401\s+unauthori[sz]ed\b/i,/\binvalid api key\b/i,/\binvalid authentication\b/i,/\bauthentication failed\b/i,/\bauth(?:orization)? error\b/i];function Gr(n){return n?.trim()?Dd.some(e=>e.test(n)):!1}function Vi(n){if(!n?.trim())return;let e=n.split(/\r?\n/).map(t=>t.trim()).filter(Boolean);return e.find(t=>Gr(t))??e.at(-1)}var Ed=120*1e3;function it(n){return n instanceof Error?n.message:String(n)}function Nd(n,e){JSON.stringify({component:"Session",event:n,at:new Date().toISOString(),...e})}function Od(n){return n?{backendRefKind:n.kind,hasBackendConversationId:!!n.conversationId,hasBackendRunId:!!n.runId,hasBackendWorktreeId:!!n.worktreeId,hasBackendWorktreePath:!!n.worktreePath}:{}}var mn=class extends $d{id;name;harnessSessionId;backendRef;harness;harnessHandle;prompt;workdir;model;reasoningEffort;fastMode;systemPrompt;allowedTools;permissionMode;requestedPermissionMode;planApproval;codexApprovalPolicy;currentPermissionMode;pendingModeSwitch;resumeSessionId;resumedFromSessionName;forkSession;worktreePath;originalWorkdir;worktreeBranch;worktreeParentBranch;worktreeStrategy;repoIntegrationPolicy;repoIntegrationPolicySource;repoProvider;worktreeBaseBranch;worktreePrTargetRepo;autoMergeParentSessionId;autoMergeConflictResolutionAttemptCount;autoMergeResolverSessionId;worktreePushRemote;worktreeDisposition;worktreePrUrl;worktreePrNumber;worktreeMerged;worktreeMergedAt;worktreeLifecycle;worktreeState="none";multiTurn;goalTaskId;messageStream;_status="starting";error;startedAt;completedAt;abortController;teardownPromise;outputBuffer=[];result;costUsd=0;originChannel;originThreadId;originAgentId;originSessionKey;route;pendingInputState;lastPendingInputSubmissionRequiresMore=!1;pendingPlanApproval=!1;planApprovalContext;planDecisionVersion=0;actionablePlanDecisionVersion;canonicalPlanPromptVersion;approvalPromptRequiredVersion;approvalPromptVersion;approvalPromptStatus="not_sent";approvalPromptTransport="none";approvalPromptMessageKind="none";approvalPromptLastAttemptAt;approvalPromptDeliveredAt;approvalPromptFailedAt;planFilePath;killReason="unknown";planModeApproved=!1;turnRuntime;harnessEvents;worktreeFinalizationPromptIssued=!1;lifecycle="starting";approvalState="not_required";approvalExecutionState="not_plan_gated";approvalRationale;latestPlanArtifact;latestPlanArtifactVersion;runtimeState="live";taskFlowMirror;deliveryState="idle";canUseTool;autoRespondCount=0;timers=new un;constructor(e,t){super(),this.id=e.sessionIdOverride??ln(8),this.name=t,this.harness=e.harness?zo(e.harness):Ho(),this.prompt=e.prompt,this.workdir=e.workdir,this.model=e.model??Me(this.harness.name),this.reasoningEffort=e.reasoningEffort??ce(this.harness.name),this.fastMode=this.harness.name==="codex"?e.fastMode??_e(this.harness.name):void 0,this.systemPrompt=e.systemPrompt,this.allowedTools=e.allowedTools,this.permissionMode=e.permissionMode??_.permissionMode,this.requestedPermissionMode=e.requestedPermissionMode??this.permissionMode,this.planApproval=e.planApproval??_.planApproval,this.codexApprovalPolicy=this.harness.name==="codex"?"never":void 0,this.currentPermissionMode=this.permissionMode,this.originChannel=e.originChannel,this.originThreadId=e.originThreadId,this.originAgentId=e.originAgentId,this.originSessionKey=e.originSessionKey,this.route=e.route?{...e.route}:void 0,this.backendRef=e.backendRef?{...e.backendRef}:void 0,this.resumeSessionId=e.resumeSessionId,this.resumedFromSessionName=e.resumedFromSessionName,this.forkSession=e.forkSession,this.multiTurn=e.multiTurn??!0,this.goalTaskId=e.goalTaskId,this.worktreeStrategy=e.worktreeStrategy,this.repoIntegrationPolicy=e.repoIntegrationPolicy,this.repoIntegrationPolicySource=e.repoIntegrationPolicySource,this.repoProvider=e.repoProvider,this.worktreeBaseBranch=e.worktreeBaseBranch,this.worktreeParentBranch=e.worktreeParentBranch,e.worktreePrTargetRepo&&(this.worktreePrTargetRepo=e.worktreePrTargetRepo),e.autoMergeParentSessionId&&(this.autoMergeParentSessionId=e.autoMergeParentSessionId),e.autoMergeConflictResolutionAttemptCount!==void 0&&(this.autoMergeConflictResolutionAttemptCount=e.autoMergeConflictResolutionAttemptCount),e.autoMergeResolverSessionId&&(this.autoMergeResolverSessionId=e.autoMergeResolverSessionId),this.canUseTool=e.canUseTool,this.startedAt=Date.now(),this.abortController=new AbortController,this.turnRuntime=new cn({appendOutput:r=>qi(this.outputBuffer,this.id,r),emitOutput:r=>this.emit("output",this,r),emitToolUse:(r,o)=>this.emit("toolUse",this,r,o),emitTurnEnd:r=>this.emit("turnEnd",this,r),markPendingPlanApproval:r=>this.markPendingPlanApproval(r),markAwaitingUserInput:()=>this.markAwaitingUserInput(),applyInputRequested:()=>this.applyControlEvent({type:"input.requested"}),completeTurn:()=>{this.applyControlEvent({type:"terminal.entered"}),this.complete("done")},queueWorktreeFinalizationPrompt:()=>this.queueWorktreeFinalizationPrompt(),setPlanFilePath:r=>{this.planFilePath=r},setLatestPlanArtifact:r=>{this.latestPlanArtifact=r;let o=this.pendingPlanApproval?this.actionablePlanDecisionVersion??this.planDecisionVersion:void 0;this.latestPlanArtifactVersion=o&&o>0?o:void 0}}),this.harnessEvents=new pn({clearStartupTimer:()=>this.clearTimer("startup"),assignBackendRef:r=>{this.backendRef=r,this.harnessSessionId=r.conversationId,r.worktreePath&&(this.worktreePath=r.worktreePath,this.originalWorkdir??=this.workdir,this.worktreeBranch??=oe(r.worktreePath),this.worktreeStrategy&&this.worktreeStrategy!=="off"&&this.applyControlEvent({type:"worktree.state_set",worktreeState:"provisioned"}))},noteRunStarted:r=>{this.backendRef&&(this.backendRef={...this.backendRef,runId:r})},transitionRunning:()=>{this._status==="starting"&&this.transition("running")},noteTextDelta:(r,o)=>this.turnRuntime.noteTextDelta(r,o),noteToolCall:r=>this.turnRuntime.noteToolCall(r),setPendingInputState:r=>this.setPendingInputState(r),notePendingInput:r=>this.turnRuntime.notePendingInput(r),clearResolvedPendingInput:(r,o)=>this.turnRuntime.clearResolvedPendingInput(r,o),notePlanArtifact:r=>this.turnRuntime.notePlanArtifact(r.artifact,r.finalized),noteSettingsChanged:r=>this.turnRuntime.noteSettingsChanged(r),setCurrentPermissionMode:r=>{this.currentPermissionMode=r,this.applyControlEvent({type:"permission.mode_changed",currentPermissionMode:r})},handleRunCompleted:r=>{let o=r.outcome??(r.success?"completed":"failed"),i=r.num_turns===0?[r.result,...this.outputBuffer.slice(-5)].filter(f=>typeof f=="string").join(`
|
|
25
|
-
`):"",s=o==="completed"&&
|
|
23
|
+
Use agent_sessions to see active sessions and their status.`;return{content:[{type:"text",text:`Error launching session: ${o}${i}`}]}}}}}import{existsSync as Zr,readFileSync as ts}from"fs";import{EventEmitter as Kd}from"events";var Ui="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";var zi=65536;function Yr(n){let e=0;for(;e<n.length;){let t=Math.min(e+zi,n.length);crypto.getRandomValues(n.subarray(e,t)),e=t}}function Gi(n){if(n|=0,n<0)throw new RangeError("Wrong ID size");let e=Buffer.allocUnsafe(n);return Yr(e),e}function ji(n,e,t){let r=256-256%n.length;if(r===256){let i=n.length-1;return(s=e)=>{if(!s)return"";let a="";for(;;){let l=t(s),d=s;for(;d--;)if(a+=n[l[d]&i],a.length>=s)return a}}}let o=Math.ceil(1.6*256*e/r);return(i=e)=>{if(!i)return"";let s="";for(;;){let a=t(o),l=o;for(;l--;)if(a[l]<r&&(s+=n[a[l]%n.length],s.length>=i))return s}}}var Md=zi/2;function _d(n,e=21){if(typeof n!="string"||!n||n.length>256)return ji(n,e,Gi);for(let l=0;l<n.length;l++)if(n.charCodeAt(l)>255)return ji(n,e,Gi);let t=Uint8Array.from(n,l=>l.charCodeAt(0)),r=n.length,o=(2<<31-Math.clz32(r-1|1))-1,i="",s=0,a=0;return(l=e)=>{if(l|=0,l<0)throw new RangeError("Wrong ID size");if(l===0)return"";if(s+l>i.length){let d=Math.max(a,l);a=Math.min(d*16,Md);let u=Buffer.allocUnsafe(d);if(o===r-1){Yr(u);for(let m=0;m<d;m++)u[m]=t[u[m]&o]}else{let m=Buffer.allocUnsafe(Math.ceil(1.6*(o+1)*d/r)),g=0;for(;g<d;){Yr(m);for(let S=0;S<m.length;S++){let y=m[S]&o;if(y<r&&(u[g++]=t[y],g===d))break}}}i=u.toString("latin1"),s=0}return s+=l,i.substring(s-l,s)}}var fn=_d(Ui);var Hi={starting:["running","failed","killed"],running:["completed","failed","killed"],completed:[],failed:[],killed:[]};function O(n,e){return Object.hasOwn(n,e)}var Qi=new Set(["merged","released","pr_open","dismissed","cleanup_failed"]);function Dd(n){return n.requestedPermissionMode!=="plan"?"not_plan_gated":n.pendingPlanApproval?"awaiting_approval":n.planModeApproved?n.status==="starting"||n.status!=="running"&&n.approvalExecutionState==="awaiting_plan_output"?"awaiting_plan_output":"approved_then_implemented":n.currentPermissionMode!=="plan"?"implemented_without_required_approval":"awaiting_plan_output"}function U(n){return{...n,approvalExecutionState:Dd(n)}}function Jr(n,e){switch(e.type){case"initialize":return U({...n,worktreeState:e.hasWorktree&&n.worktreeState==="none"?"provisioned":n.worktreeState,lifecycle:n.status==="starting"?"starting":n.lifecycle,runtimeState:n.status==="starting"?"live":n.runtimeState});case"status.transition":return e.status==="starting"?U({...n,status:e.status,lifecycle:"starting",runtimeState:"live"}):e.status==="running"?U({...n,status:e.status,lifecycle:n.lifecycle==="starting"||n.lifecycle==="suspended"?"active":n.lifecycle,runtimeState:"live"}):U({...n,status:e.status,runtimeState:"stopped",lifecycle:n.lifecycle==="suspended"?"suspended":"terminal"});case"permission.mode_changed":return U({...n,currentPermissionMode:e.currentPermissionMode});case"turn.started":return U({...n,lifecycle:"active",runtimeState:"live"});case"input.requested":return U({...n,lifecycle:n.pendingPlanApproval?"awaiting_plan_decision":"awaiting_user_input"});case"plan.requested":if(n.planModeApproved)return n;{let t=n.pendingPlanApproval&&n.approvalState==="pending"&&n.planApprovalContext===e.context,r=n.pendingPlanApproval&&n.approvalState==="changes_requested"&&n.planApprovalContext===e.context&&n.planDecisionVersion>0,o=!n.pendingPlanApproval&&n.approvalState==="changes_requested"&&n.planApprovalContext===e.context&&n.planDecisionVersion>0,i=t||r||o?n.planDecisionVersion:n.planDecisionVersion+1;return U({...n,pendingPlanApproval:!0,planApprovalContext:e.context,approvalState:"pending",planDecisionVersion:i,actionablePlanDecisionVersion:i,canonicalPlanPromptVersion:t?n.canonicalPlanPromptVersion:void 0,approvalPromptRequiredVersion:t?n.approvalPromptRequiredVersion:void 0,approvalPromptVersion:t?n.approvalPromptVersion:void 0,approvalPromptStatus:t?n.approvalPromptStatus:"not_sent",approvalPromptTransport:t?n.approvalPromptTransport:"none",approvalPromptMessageKind:t?n.approvalPromptMessageKind:"none",approvalPromptLastAttemptAt:t?n.approvalPromptLastAttemptAt:void 0,approvalPromptDeliveredAt:t?n.approvalPromptDeliveredAt:void 0,approvalPromptFailedAt:t?n.approvalPromptFailedAt:void 0,lifecycle:"awaiting_plan_decision"})}case"plan.cleared":return U({...n,pendingPlanApproval:!1,planApprovalContext:void 0,actionablePlanDecisionVersion:void 0,approvalState:n.approvalState==="pending"?"not_required":n.approvalState});case"plan.approved":return U({...n,pendingPlanApproval:!1,planApprovalContext:void 0,actionablePlanDecisionVersion:void 0,planModeApproved:!0,approvalState:"approved",planDecisionVersion:n.planDecisionVersion+1,approvalPromptVersion:void 0,approvalPromptStatus:"not_sent",lifecycle:n.status==="running"?"active":n.lifecycle});case"plan.changes_requested":return U({...n,pendingPlanApproval:!0,approvalState:"changes_requested",planDecisionVersion:n.planDecisionVersion+1,actionablePlanDecisionVersion:void 0,canonicalPlanPromptVersion:void 0,approvalPromptRequiredVersion:void 0,approvalPromptVersion:void 0,approvalPromptStatus:"not_sent",approvalPromptTransport:"none",approvalPromptMessageKind:"none",approvalPromptLastAttemptAt:void 0,approvalPromptDeliveredAt:void 0,approvalPromptFailedAt:void 0,lifecycle:"awaiting_plan_decision"});case"terminal.entered":return U({...n,lifecycle:e.suspended?"suspended":"terminal",runtimeState:"stopped"});case"worktree.decision_requested":return U({...n,lifecycle:"awaiting_worktree_decision",worktreeState:"pending_decision"});case"worktree.state_set":return U({...n,worktreeState:e.worktreeState,lifecycle:e.worktreeState==="pending_decision"?"awaiting_worktree_decision":Qi.has(e.worktreeState)?"terminal":n.lifecycle});case"delivery.state_set":return U({...n,deliveryState:e.deliveryState})}}function Yi(n,e){let t={...n,...O(e,"lifecycle")?{lifecycle:e.lifecycle}:{},...O(e,"approvalState")?{approvalState:e.approvalState}:{},...O(e,"approvalExecutionState")?{approvalExecutionState:e.approvalExecutionState}:{},...O(e,"worktreeState")?{worktreeState:e.worktreeState}:{},...O(e,"runtimeState")?{runtimeState:e.runtimeState}:{},...O(e,"deliveryState")?{deliveryState:e.deliveryState}:{},...O(e,"requestedPermissionMode")?{requestedPermissionMode:e.requestedPermissionMode}:{},...O(e,"currentPermissionMode")?{currentPermissionMode:e.currentPermissionMode}:{},...O(e,"pendingPlanApproval")?{pendingPlanApproval:e.pendingPlanApproval}:{},...O(e,"planApprovalContext")?{planApprovalContext:e.planApprovalContext}:{},...O(e,"planDecisionVersion")?{planDecisionVersion:e.planDecisionVersion}:{},...O(e,"actionablePlanDecisionVersion")?{actionablePlanDecisionVersion:e.actionablePlanDecisionVersion}:{},...O(e,"canonicalPlanPromptVersion")?{canonicalPlanPromptVersion:e.canonicalPlanPromptVersion}:{},...O(e,"approvalPromptRequiredVersion")?{approvalPromptRequiredVersion:e.approvalPromptRequiredVersion}:{},...O(e,"approvalPromptVersion")?{approvalPromptVersion:e.approvalPromptVersion}:{},...O(e,"approvalPromptStatus")?{approvalPromptStatus:e.approvalPromptStatus}:{},...O(e,"approvalPromptTransport")?{approvalPromptTransport:e.approvalPromptTransport}:{},...O(e,"approvalPromptMessageKind")?{approvalPromptMessageKind:e.approvalPromptMessageKind}:{},...O(e,"approvalPromptLastAttemptAt")?{approvalPromptLastAttemptAt:e.approvalPromptLastAttemptAt}:{},...O(e,"approvalPromptDeliveredAt")?{approvalPromptDeliveredAt:e.approvalPromptDeliveredAt}:{},...O(e,"approvalPromptFailedAt")?{approvalPromptFailedAt:e.approvalPromptFailedAt}:{},...O(e,"planModeApproved")?{planModeApproved:e.planModeApproved}:{}};return e.approvalState==="changes_requested"&&e.pendingPlanApproval===void 0&&(t={...t,pendingPlanApproval:!1}),t.pendingPlanApproval?t={...t,approvalState:"pending",actionablePlanDecisionVersion:t.actionablePlanDecisionVersion??t.planDecisionVersion,lifecycle:"awaiting_plan_decision"}:t.approvalState==="changes_requested"&&(t={...t,actionablePlanDecisionVersion:void 0,lifecycle:"awaiting_user_input"}),O(e,"pendingWorktreeDecisionSince")&&e.pendingWorktreeDecisionSince!==void 0||t.worktreeState==="pending_decision"?t=Jr(t,{type:"worktree.decision_requested"}):Qi.has(t.worktreeState)&&(t={...t,lifecycle:"terminal"}),U(t)}var gn=class{queue=[];resolve=null;done=!1;hasPending(){return this.queue.length>0}push(e){this.queue.push(e),this.resolve&&(this.resolve(),this.resolve=null)}end(){this.done=!0,this.resolve&&(this.resolve(),this.resolve=null)}async*[Symbol.asyncIterator](){for(;;){for(;this.queue.length>0;)yield this.queue.shift();if(this.done)return;await new Promise(e=>{this.resolve=e})}}};import{createHash as $d}from"crypto";import{appendFileSync as Ed}from"fs";import{tmpdir as Nd}from"os";import{join as Od}from"path";var Ji=2e3,Wd=/^[A-Za-z0-9_.-]+$/;function Fd(n){return Wd.test(n)?n:`hashed+${$d("sha256").update(n,"utf8").digest("hex")}`}function ve(n){return Od(Nd(),`openclaw-agent-${Fd(n)}.txt`)}function Ld(n,e){if(!e)return;let t=e.split(`
|
|
24
|
+
`),[r="",...o]=t;n.length===0?n.push(r):n[n.length-1]+=r;for(let i of o)n.push(i)}function Xi(n,e,t){Ld(n,t),n.length>Ji&&n.splice(0,n.length-Ji);try{Ed(ve(e),t,"utf-8")}catch{}return n}var hn=class{timers=new Map;get size(){return this.timers.size}set(e,t,r){this.clear(e);let o=setTimeout(()=>{this.timers.get(e)===o&&this.timers.delete(e),r()},t);o.unref?.(),this.timers.set(e,o)}clear(e){let t=this.timers.get(e);t&&(clearTimeout(t),this.timers.delete(e))}clearAll(){for(let e of this.timers.values())clearTimeout(e);this.timers.clear()}};var yn=class{constructor(e){this.deps=e}deps;waitingForInputFired=!1;lastTurnHadQuestion=!1;turnInProgress=!0;currentTurnText="";currentTurnPlanArtifact;pendingInputNotificationIdentity;beginUserTurn(){this.waitingForInputFired=!1,this.turnInProgress=!0,this.currentTurnText="",this.currentTurnPlanArtifact=void 0,this.lastTurnHadQuestion=!1}noteTextDelta(e,t){this.waitingForInputFired=!1,t||(this.lastTurnHadQuestion=!1),this.deps.appendOutput(e),this.currentTurnText+=this.currentTurnText?`
|
|
25
|
+
${e}`:e,this.deps.emitOutput(e)}noteToolCall(e){let{name:t,input:r,currentPermissionMode:o,permissionMode:i,planModeApproved:s}=e;if(t==="Write"){let a=r;typeof a?.file_path=="string"&&a.file_path.includes("/.claude/plans/")&&this.deps.setPlanFilePath(a.file_path)}t==="AskUserQuestion"?(this.lastTurnHadQuestion=!0,this.deps.applyInputRequested(),(o==="plan"||i==="plan")&&!s&&this.deps.markPendingPlanApproval("plan-mode")):(t==="ExitPlanMode"||t==="set_permission_mode")&&!s&&(this.lastTurnHadQuestion=!0,this.deps.markPendingPlanApproval("plan-mode")),this.deps.emitToolUse(t,r)}notePendingInput(e){this.lastTurnHadQuestion=!0,this.deps.applyInputRequested();let t=e?.activeQuestionIndex??0,r=e?.questions?.[t],o=e?.requestId?`${e.requestId}:${r?.id??(e.activeQuestionIndex!=null?`q${e.activeQuestionIndex}`:"request")}`:void 0;(!this.waitingForInputFired||o&&o!==this.pendingInputNotificationIdentity)&&(this.waitingForInputFired=!0,this.pendingInputNotificationIdentity=o,this.deps.emitTurnEnd(!0))}clearResolvedPendingInput(e,t){if(!e||t?.requestId===e){this.pendingInputNotificationIdentity=void 0;return}return t}notePlanArtifact(e,t){if(this.currentTurnPlanArtifact=e,this.deps.setLatestPlanArtifact(e),!t)return;let r=e.markdown.trim();!r||this.currentTurnText.trim()===r||(this.deps.appendOutput(r),this.currentTurnText=r,this.deps.emitOutput(r))}noteSettingsChanged(e){let{oldMode:t,permissionMode:r,planModeApproved:o}=e;if(r)return r!=="plan"&&t==="plan"&&!o&&(this.deps.markPendingPlanApproval("plan-mode"),this.lastTurnHadQuestion=!0),r}finishSuccessfulTurn(e){let{currentPermissionMode:t,permissionMode:r,pendingPlanApproval:o,planModeApproved:i,pendingInputState:s,hasPendingMessages:a}=e,l=o;(t==="plan"||r==="plan")&&!l&&!i&&(this.deps.markPendingPlanApproval("plan-mode"),l=!0);let d=l||this.lastTurnHadQuestion||!!s;if(this.turnInProgress=a,d&&!this.waitingForInputFired)this.waitingForInputFired=!0,l||this.deps.markAwaitingUserInput(),this.deps.emitTurnEnd(!0);else if(!a&&!d){if(this.deps.queueWorktreeFinalizationPrompt?.()){this.turnInProgress=!0;return}this.deps.completeTurn(),this.deps.emitTurnEnd(!1)}}finishTerminalTurn(){this.turnInProgress=!1}finishInterruptedTurn(e){this.turnInProgress=e,this.waitingForInputFired=!1,this.lastTurnHadQuestion=!1,this.currentTurnText="",this.currentTurnPlanArtifact=void 0}resetAfterRun(){this.lastTurnHadQuestion=!1,this.currentTurnText="",this.currentTurnPlanArtifact=void 0}};var Sn=class{constructor(e){this.deps=e}deps;applyMessage(e,t){if(e.type==="backend_ref"){this.deps.clearStartupTimer(),this.deps.assignBackendRef({...e.ref}),this.deps.transitionRunning();return}if(e.type==="run_started"){e.runId&&this.deps.noteRunStarted(e.runId);return}if(e.type==="text_delta"){this.deps.noteTextDelta(e.text,t.pendingPlanApproval);return}if(e.type==="tool_call"){this.deps.noteToolCall({name:e.name,input:e.input,currentPermissionMode:t.currentPermissionMode,permissionMode:t.permissionMode,planModeApproved:t.planModeApproved});return}if(e.type==="pending_input"){this.deps.setPendingInputState(e.state),this.deps.notePendingInput(e.state);return}if(e.type==="pending_input_resolved"){this.deps.setPendingInputState(this.deps.clearResolvedPendingInput(e.requestId,t.pendingInputState));return}if(e.type==="plan_artifact"){this.deps.notePlanArtifact(e);return}if(e.type==="settings_changed"){let r=this.deps.noteSettingsChanged({oldMode:t.currentPermissionMode,permissionMode:e.permissionMode,planModeApproved:t.planModeApproved});r&&this.deps.setCurrentPermissionMode(r);return}if(e.type==="run_completed"){this.deps.handleRunCompleted(e.data);return}e.type}};var Bd=[/\bfailed to authenticate\b/i,/\binvalid bearer token\b/i,/\bapi error:\s*401\b/i,/\b401\s+unauthori[sz]ed\b/i,/\binvalid api key\b/i,/\binvalid authentication\b/i,/\bauthentication failed\b/i,/\bauth(?:orization)? error\b/i];function Xr(n){return n?.trim()?Bd.some(e=>e.test(n)):!1}function Zi(n){if(!n?.trim())return;let e=n.split(/\r?\n/).map(t=>t.trim()).filter(Boolean);return e.find(t=>Xr(t))??e.at(-1)}var qd=120*1e3;function dt(n){return n instanceof Error?n.message:String(n)}function Vd(n,e){JSON.stringify({component:"Session",event:n,at:new Date().toISOString(),...e})}function Ud(n){return n?{backendRefKind:n.kind,hasBackendConversationId:!!n.conversationId,hasBackendRunId:!!n.runId,hasBackendWorktreeId:!!n.worktreeId,hasBackendWorktreePath:!!n.worktreePath}:{}}var kn=class extends Kd{id;name;harnessSessionId;backendRef;harness;harnessHandle;prompt;workdir;model;reasoningEffort;fastMode;systemPrompt;allowedTools;permissionMode;requestedPermissionMode;planApproval;codexApprovalPolicy;currentPermissionMode;pendingModeSwitch;resumeSessionId;resumedFromSessionName;forkSession;worktreePath;originalWorkdir;worktreeBranch;worktreeParentBranch;worktreeStrategy;repoIntegrationPolicy;repoIntegrationPolicySource;repoProvider;worktreeBaseBranch;worktreePrTargetRepo;autoMergeParentSessionId;autoMergeConflictResolutionAttemptCount;autoMergeResolverSessionId;worktreePushRemote;worktreeDisposition;worktreePrUrl;worktreePrNumber;worktreeMerged;worktreeMergedAt;worktreeLifecycle;worktreeState="none";multiTurn;goalTaskId;messageStream;_status="starting";error;startedAt;completedAt;abortController;teardownPromise;outputBuffer=[];result;costUsd=0;originChannel;originThreadId;originAgentId;originSessionKey;route;pendingInputState;lastPendingInputSubmissionRequiresMore=!1;pendingPlanApproval=!1;planApprovalContext;planDecisionVersion=0;actionablePlanDecisionVersion;canonicalPlanPromptVersion;approvalPromptRequiredVersion;approvalPromptVersion;approvalPromptStatus="not_sent";approvalPromptTransport="none";approvalPromptMessageKind="none";approvalPromptLastAttemptAt;approvalPromptDeliveredAt;approvalPromptFailedAt;planFilePath;killReason="unknown";planModeApproved=!1;turnRuntime;harnessEvents;worktreeFinalizationPromptIssued=!1;lifecycle="starting";approvalState="not_required";approvalExecutionState="not_plan_gated";approvalRationale;latestPlanArtifact;latestPlanArtifactVersion;runtimeState="live";taskFlowMirror;deliveryState="idle";canUseTool;autoRespondCount=0;timers=new hn;constructor(e,t){super(),this.id=e.sessionIdOverride??fn(8),this.name=t,this.harness=e.harness?ti(e.harness):ni(),this.prompt=e.prompt,this.workdir=e.workdir,this.model=e.model??Me(this.harness.name),this.reasoningEffort=e.reasoningEffort??ce(this.harness.name),this.fastMode=this.harness.name==="codex"?e.fastMode??_e(this.harness.name):void 0,this.systemPrompt=e.systemPrompt,this.allowedTools=e.allowedTools,this.permissionMode=e.permissionMode??_.permissionMode,this.requestedPermissionMode=e.requestedPermissionMode??this.permissionMode,this.planApproval=e.planApproval??_.planApproval,this.codexApprovalPolicy=this.harness.name==="codex"?"never":void 0,this.currentPermissionMode=this.permissionMode,this.originChannel=e.originChannel,this.originThreadId=e.originThreadId,this.originAgentId=e.originAgentId,this.originSessionKey=e.originSessionKey,this.route=e.route?{...e.route}:void 0,this.backendRef=e.backendRef?{...e.backendRef}:void 0,this.resumeSessionId=e.resumeSessionId,this.resumedFromSessionName=e.resumedFromSessionName,this.forkSession=e.forkSession,this.multiTurn=e.multiTurn??!0,this.goalTaskId=e.goalTaskId,this.worktreeStrategy=e.worktreeStrategy,this.repoIntegrationPolicy=e.repoIntegrationPolicy,this.repoIntegrationPolicySource=e.repoIntegrationPolicySource,this.repoProvider=e.repoProvider,this.worktreeBaseBranch=e.worktreeBaseBranch,this.worktreeParentBranch=e.worktreeParentBranch,e.worktreePrTargetRepo&&(this.worktreePrTargetRepo=e.worktreePrTargetRepo),e.autoMergeParentSessionId&&(this.autoMergeParentSessionId=e.autoMergeParentSessionId),e.autoMergeConflictResolutionAttemptCount!==void 0&&(this.autoMergeConflictResolutionAttemptCount=e.autoMergeConflictResolutionAttemptCount),e.autoMergeResolverSessionId&&(this.autoMergeResolverSessionId=e.autoMergeResolverSessionId),this.canUseTool=e.canUseTool,this.startedAt=Date.now(),this.abortController=new AbortController,this.turnRuntime=new yn({appendOutput:r=>Xi(this.outputBuffer,this.id,r),emitOutput:r=>this.emit("output",this,r),emitToolUse:(r,o)=>this.emit("toolUse",this,r,o),emitTurnEnd:r=>this.emit("turnEnd",this,r),markPendingPlanApproval:r=>this.markPendingPlanApproval(r),markAwaitingUserInput:()=>this.markAwaitingUserInput(),applyInputRequested:()=>this.applyControlEvent({type:"input.requested"}),completeTurn:()=>{this.applyControlEvent({type:"terminal.entered"}),this.complete("done")},queueWorktreeFinalizationPrompt:()=>this.queueWorktreeFinalizationPrompt(),setPlanFilePath:r=>{this.planFilePath=r},setLatestPlanArtifact:r=>{this.latestPlanArtifact=r;let o=this.pendingPlanApproval?this.actionablePlanDecisionVersion??this.planDecisionVersion:void 0;this.latestPlanArtifactVersion=o&&o>0?o:void 0}}),this.harnessEvents=new Sn({clearStartupTimer:()=>this.clearTimer("startup"),assignBackendRef:r=>{this.backendRef=r,this.harnessSessionId=r.conversationId,r.worktreePath&&(this.worktreePath=r.worktreePath,this.originalWorkdir??=this.workdir,this.worktreeBranch??=oe(r.worktreePath),this.worktreeStrategy&&this.worktreeStrategy!=="off"&&this.applyControlEvent({type:"worktree.state_set",worktreeState:"provisioned"}))},noteRunStarted:r=>{this.backendRef&&(this.backendRef={...this.backendRef,runId:r})},transitionRunning:()=>{this._status==="starting"&&this.transition("running")},noteTextDelta:(r,o)=>this.turnRuntime.noteTextDelta(r,o),noteToolCall:r=>this.turnRuntime.noteToolCall(r),setPendingInputState:r=>this.setPendingInputState(r),notePendingInput:r=>this.turnRuntime.notePendingInput(r),clearResolvedPendingInput:(r,o)=>this.turnRuntime.clearResolvedPendingInput(r,o),notePlanArtifact:r=>this.turnRuntime.notePlanArtifact(r.artifact,r.finalized),noteSettingsChanged:r=>this.turnRuntime.noteSettingsChanged(r),setCurrentPermissionMode:r=>{this.currentPermissionMode=r,this.applyControlEvent({type:"permission.mode_changed",currentPermissionMode:r})},handleRunCompleted:r=>{let o=r.outcome??(r.success?"completed":"failed"),i=r.num_turns===0?[r.result,...this.outputBuffer.slice(-5)].filter(m=>typeof m=="string").join(`
|
|
26
|
+
`):"",s=o==="completed"&&Xr(i),a=s?"failed":o,l=s?Zi(i)??r.result:r.result;this.result={subtype:a==="interrupted"?"interrupted":a==="completed"?"success":"error",duration_ms:r.duration_ms,total_cost_usd:r.total_cost_usd,num_turns:r.num_turns,result:l,is_error:a==="failed",session_id:r.session_id},this.costUsd=r.total_cost_usd;let d=this.multiTurn&&this.messageStream&&a==="interrupted",u=this.multiTurn&&this.messageStream&&a==="completed";d?(this.resetIdleTimer(),this.turnRuntime.finishInterruptedTurn(this.messageStream?.hasPending()===!0)):u?(this.resetIdleTimer(),this.turnRuntime.finishSuccessfulTurn({currentPermissionMode:this.currentPermissionMode,permissionMode:this.permissionMode,pendingPlanApproval:this.pendingPlanApproval,planModeApproved:this.planModeApproved,pendingInputState:this.pendingInputState,hasPendingMessages:this.messageStream?.hasPending()===!0})):(this.turnRuntime.finishTerminalTurn(),this.transitionToTerminal(a==="completed"?"completed":"failed",{...a==="failed"&&l?{error:l}:{}})),this.turnRuntime.resetAfterRun(),this.setPendingInputState(void 0)}}),this.applyControlEvent({type:"initialize",hasWorktree:!!(this.worktreeStrategy&&this.worktreeStrategy!=="off")}),(e.planModeApproved!==void 0||e.approvalState!==void 0||e.approvalExecutionState!==void 0||e.approvalRationale!==void 0||e.pendingPlanApproval!==void 0||e.planApprovalContext!==void 0||e.planDecisionVersion!==void 0||e.actionablePlanDecisionVersion!==void 0||e.canonicalPlanPromptVersion!==void 0||e.approvalPromptRequiredVersion!==void 0||e.approvalPromptVersion!==void 0||e.approvalPromptStatus!==void 0||e.approvalPromptTransport!==void 0||e.approvalPromptMessageKind!==void 0||e.approvalPromptLastAttemptAt!==void 0||e.approvalPromptDeliveredAt!==void 0||e.approvalPromptFailedAt!==void 0)&&this.applyControlPatch({...e.planModeApproved!==void 0?{planModeApproved:e.planModeApproved}:{},...e.approvalState!==void 0?{approvalState:e.approvalState}:{},...e.approvalExecutionState!==void 0?{approvalExecutionState:e.approvalExecutionState}:{},...e.pendingPlanApproval!==void 0?{pendingPlanApproval:e.pendingPlanApproval}:{},...e.planApprovalContext!==void 0?{planApprovalContext:e.planApprovalContext}:{},...e.planDecisionVersion!==void 0?{planDecisionVersion:e.planDecisionVersion}:{},...e.actionablePlanDecisionVersion!==void 0?{actionablePlanDecisionVersion:e.actionablePlanDecisionVersion}:{},...e.canonicalPlanPromptVersion!==void 0?{canonicalPlanPromptVersion:e.canonicalPlanPromptVersion}:{},...e.approvalPromptRequiredVersion!==void 0?{approvalPromptRequiredVersion:e.approvalPromptRequiredVersion}:{},...e.approvalPromptVersion!==void 0?{approvalPromptVersion:e.approvalPromptVersion}:{},...e.approvalPromptStatus!==void 0?{approvalPromptStatus:e.approvalPromptStatus}:{},...e.approvalPromptTransport!==void 0?{approvalPromptTransport:e.approvalPromptTransport}:{},...e.approvalPromptMessageKind!==void 0?{approvalPromptMessageKind:e.approvalPromptMessageKind}:{},...e.approvalPromptLastAttemptAt!==void 0?{approvalPromptLastAttemptAt:e.approvalPromptLastAttemptAt}:{},...e.approvalPromptDeliveredAt!==void 0?{approvalPromptDeliveredAt:e.approvalPromptDeliveredAt}:{},...e.approvalPromptFailedAt!==void 0?{approvalPromptFailedAt:e.approvalPromptFailedAt}:{}}),this.approvalRationale=e.approvalRationale}get status(){return this._status}get harnessName(){return this.harness.name}get backendKind(){return this.harness.backendKind}get backendCapabilities(){return this.harness.capabilities}get backendConversationId(){return C(this)}approvalSnapshot(){return{requestedPermissionMode:this.requestedPermissionMode,currentPermissionMode:this.currentPermissionMode,approvalExecutionState:this.approvalExecutionState,approvalRationale:this.approvalRationale,planModeApproved:this.controlStateSnapshot().planModeApproved,pendingPlanApproval:this.pendingPlanApproval,planApprovalContext:this.planApprovalContext,planDecisionVersion:this.planDecisionVersion,actionablePlanDecisionVersion:this.actionablePlanDecisionVersion,canonicalPlanPromptVersion:this.canonicalPlanPromptVersion,approvalPromptRequiredVersion:this.approvalPromptRequiredVersion,approvalPromptVersion:this.approvalPromptVersion,approvalPromptStatus:this.approvalPromptStatus,approvalPromptTransport:this.approvalPromptTransport,approvalPromptMessageKind:this.approvalPromptMessageKind,approvalPromptLastAttemptAt:this.approvalPromptLastAttemptAt,approvalPromptDeliveredAt:this.approvalPromptDeliveredAt,approvalPromptFailedAt:this.approvalPromptFailedAt,planApproval:this.planApproval,codexApprovalPolicy:this.codexApprovalPolicy}}worktreeSnapshot(){return{worktreePath:this.worktreePath,worktreeBranch:this.worktreeBranch,worktreeStrategy:this.worktreeStrategy,repoIntegrationPolicy:this.repoIntegrationPolicy,repoIntegrationPolicySource:this.repoIntegrationPolicySource,repoProvider:this.repoProvider,worktreeBaseBranch:this.worktreeBaseBranch,worktreeParentBranch:this.worktreeParentBranch,worktreePrTargetRepo:this.worktreePrTargetRepo,autoMergeParentSessionId:this.autoMergeParentSessionId,autoMergeConflictResolutionAttemptCount:this.autoMergeConflictResolutionAttemptCount,autoMergeResolverSessionId:this.autoMergeResolverSessionId,worktreeLifecycle:this.worktreeLifecycle}}backendSnapshot(){return{harnessSessionId:this.harnessSessionId,backendRef:this.backendRef,harness:this.harnessName,backendKind:this.backendKind,backendConversationId:this.backendConversationId}}routingSnapshot(){return{route:this.route,originAgentId:this.originAgentId,originChannel:this.originChannel,originThreadId:this.originThreadId,originSessionKey:this.originSessionKey}}get waitingForInputFired(){return this.turnRuntime.waitingForInputFired}set waitingForInputFired(e){this.turnRuntime.waitingForInputFired=e}get turnInProgress(){return this.turnRuntime.turnInProgress}set turnInProgress(e){this.turnRuntime.turnInProgress=e}get currentTurnPlanArtifact(){return this.turnRuntime.currentTurnPlanArtifact}set currentTurnPlanArtifact(e){this.turnRuntime.currentTurnPlanArtifact=e}get duration(){return(this.completedAt??Date.now())-this.startedAt}get phase(){return this.lifecycle}get isExplicitlyResumable(){return this.status!=="running"&&this.status!=="completed"&&this.killReason!=="done"&&!!this.backendConversationId}transition(e){if(!Hi[this._status].includes(e))throw new Error(`Session state error: cannot transition from ${this._status} to ${e}. This is an internal error — please report it.`);let t=this._status;this._status=e,this.applyControlEvent({type:"status.transition",status:e}),this.emit("statusChange",this,e,t)}setTimer(e,t,r){this.timers.set(e,t,r)}clearTimer(e){this.timers.clear(e)}clearAllTimers(){this.timers.clearAll()}markPendingPlanApproval(e){let t=this.latestPlanArtifactVersion;this.approvalRationale=void 0,this.applyControlEvent({type:"plan.requested",context:e});let r=this.actionablePlanDecisionVersion??this.planDecisionVersion;this.currentTurnPlanArtifact&&r>0?(this.latestPlanArtifact=this.currentTurnPlanArtifact,this.latestPlanArtifactVersion=r):t!==r&&(this.latestPlanArtifact=void 0,this.latestPlanArtifactVersion=void 0)}clearPendingPlanApproval(){this.applyControlEvent({type:"plan.cleared"})}markAwaitingUserInput(){this.applyControlEvent({type:"input.requested"})}queueWorktreeFinalizationPrompt(){if(!this.multiTurn||!this.messageStream||!this.worktreePath||!this.worktreeStrategy||this.worktreeStrategy==="off"||this.worktreeFinalizationPromptIssued)return!1;let e=nt(this.worktreePath);if(e.length===0)return!1;this.worktreeFinalizationPromptIssued=!0;let t=e.slice(0,20).map(i=>`- ${i}`).join(`
|
|
26
27
|
`),r=e.length>20?`
|
|
27
28
|
- ...and ${e.length-20} more`:"",o=["Your worktree still has uncommitted changes, so this session cannot finish yet.","",`Worktree: ${this.worktreePath}`,`Branch: ${this.worktreeBranch??"(unknown)"}`,"","Dirty entries:",`${t}${r}`,"","Before finishing, run `git status --short` in the worktree and do exactly one of these:","1. If these are real task changes, commit them on the worktree branch with `git add` and `git commit`.","2. If these are temporary or unrelated artifacts, remove or restore them so the worktree is clean.","3. If no repository changes were intended, leave the worktree clean and say that no changes were needed.","","Do not finish again until `git status --short` is clean or all real task changes are committed."].join(`
|
|
28
|
-
`);return this.messageStream.push(this.harness.buildUserMessage(o,this.backendConversationId??"")),!0}async start(){this.logDiagnostic("harness.launch.start",{model:this.model,hasWorkdir:!!this.workdir,hasResumeSessionId:!!this.resumeSessionId,forkSessionRequested:this.forkSession===!0,hasBackendRef:!!this.backendRef});try{let e;this.multiTurn?(this.messageStream=new
|
|
29
|
+
`);return this.messageStream.push(this.harness.buildUserMessage(o,this.backendConversationId??"")),!0}async start(){this.logDiagnostic("harness.launch.start",{model:this.model,hasWorkdir:!!this.workdir,hasResumeSessionId:!!this.resumeSessionId,forkSessionRequested:this.forkSession===!0,hasBackendRef:!!this.backendRef});try{let e;this.multiTurn?(this.messageStream=new gn,this.messageStream.push(this.harness.buildUserMessage(this.prompt,"")),e=this.messageStream):e=this.prompt;let t=this.harness.launch({prompt:e,cwd:this.workdir,model:this.model,reasoningEffort:this.reasoningEffort,fastMode:this.fastMode,permissionMode:this.permissionMode,codexApprovalPolicy:this.codexApprovalPolicy,systemPrompt:this.systemPrompt,allowedTools:this.allowedTools,resumeSessionId:this.resumeSessionId,forkSession:this.forkSession,backendRef:this.backendRef,worktreeStrategy:this.worktreeStrategy,originalWorkdir:this.originalWorkdir??this.workdir,abortController:this.abortController,mcpServers:Yo(),canUseTool:this.canUseTool});this.harnessHandle=t,this.logDiagnostic("harness.launch.created",{hasStreamInput:!!t.streamInput,hasInterrupt:!!t.interrupt,hasClose:!!t.close,hasPermissionModeSwitch:!!t.setPermissionMode}),this.setTimer("startup",qd,()=>{this._status==="starting"&&this.kill("startup-timeout")})}catch(e){let t=e instanceof Error?e.message:String(e);this.logDiagnostic("harness.launch.error",{error:t}),this.transitionToTerminal("failed",{error:t});return}this.consumeMessages(this.harnessHandle.messages).catch(e=>{let t=e instanceof Error?e.message:String(e),r=e instanceof Error?e.stack:void 0;this.logDiagnostic("harness.stream.error",{error:t}),console.error(`[Session ${this.id}] consumeMessages error: ${t}`,r),this.isActive&&this.transitionToTerminal("failed",{error:t})})}async sendMessage(e){if(this._status!=="running")throw new Error(`Session is not running (status: ${this._status})`);this.resetIdleTimer(),this.turnRuntime.beginUserTurn(),this.applyControlEvent({type:"turn.started"});let t=e;if(this.pendingModeSwitch){let r=this.pendingModeSwitch,o=!1,i=!1;if(this.harnessHandle?.setPermissionMode)try{await this.harnessHandle.setPermissionMode(r),this.currentPermissionMode=r,this.applyControlEvent({type:"permission.mode_changed",currentPermissionMode:r}),this.pendingModeSwitch=void 0,i=!0,o=!0}catch(s){throw console.error(`[Session ${this.id}] setPermissionMode(${r}) FAILED: ${dt(s)}`),this.markPendingPlanApproval(this.planApprovalContext??"plan-mode"),new Error(`Failed to switch permission mode to ${r}: ${dt(s)}`)}else this.pendingModeSwitch=void 0,i=!0,o=!0,`${this.id}${this.currentPermissionMode}`;i&&(this.clearPendingPlanApproval(),r!=="plan"&&this.applyControlEvent({type:"plan.approved"})),o&&(t=`[SYSTEM: The user has approved your plan. Exit plan mode immediately and implement the changes with full permissions. Do not ask for further confirmation.]
|
|
29
30
|
|
|
30
31
|
${e}`)}else if((this.pendingPlanApproval||this.approvalState==="changes_requested")&&!this.planModeApproved&&(this.approvalState!=="changes_requested"&&this.applyControlEvent({type:"plan.changes_requested"}),t=`[SYSTEM: The user wants changes to your plan. Revise the plan based on their feedback below, then re-submit your revised plan for approval. Do NOT start implementing yet.]
|
|
31
32
|
|
|
32
|
-
${e}`,this.harnessHandle?.setPermissionMode))try{await this.harnessHandle.setPermissionMode("plan"),this.currentPermissionMode="plan",this.applyControlEvent({type:"permission.mode_changed",currentPermissionMode:"plan"})}catch(r){`${this.id}${
|
|
33
|
-
`);return e.at(-1)===""&&e.pop(),e}function
|
|
34
|
-
`)}function
|
|
35
|
-
`)}function
|
|
33
|
+
${e}`,this.harnessHandle?.setPermissionMode))try{await this.harnessHandle.setPermissionMode("plan"),this.currentPermissionMode="plan",this.applyControlEvent({type:"permission.mode_changed",currentPermissionMode:"plan"})}catch(r){`${this.id}${dt(r)}`}if(this.multiTurn&&this.messageStream)this.messageStream.push(this.harness.buildUserMessage(t,this.backendConversationId??""));else if(this.harnessHandle?.streamInput){let r=this.harness.buildUserMessage(t,this.backendConversationId??"");async function*o(){yield r}await this.harnessHandle.streamInput(o())}else throw new Error("Session does not support follow-up messages (launched in single-turn mode).")}async interrupt(){return!this.turnInProgress||!this.harnessHandle?.interrupt?!1:(await this.harnessHandle.interrupt(),!0)}async submitPendingInputOption(e,t){if(!this.pendingInputState||!this.harnessHandle?.submitPendingInputOption)return!1;let r=this.pendingInputState.activeQuestionIndex,o=this.pendingInputState.questions?.length,i=await this.harnessHandle.submitPendingInputOption(e,t);return i&&(this.lastPendingInputSubmissionRequiresMore=r!=null&&o!=null&&r+1<o,this.waitingForInputFired=!1),i}canSubmitPendingInputOption(){return!!(this.pendingInputState&&this.harnessHandle?.submitPendingInputOption)}setPendingInputState(e){this.pendingInputState=e,e||(this.lastPendingInputSubmissionRequiresMore=!1)}pendingInputSubmissionRequiresMore(){return this.lastPendingInputSubmissionRequiresMore}async submitPendingInputText(e){if(!this.pendingInputState||!this.harnessHandle?.submitPendingInputText)return!1;let t=this.pendingInputState.activeQuestionIndex,r=this.pendingInputState.questions?.length,o=await this.harnessHandle.submitPendingInputText(e);return o&&(this.lastPendingInputSubmissionRequiresMore=t!=null&&r!=null&&t+1<r,this.waitingForInputFired=!1),o}switchPermissionMode(e){this.pendingModeSwitch=e}get isActive(){return this._status==="starting"||this._status==="running"}kill(e){this.transitionToTerminal("killed",{reason:e})}async waitForTeardown(){await(this.teardownPromise??Promise.resolve())}complete(e="done"){this.transitionToTerminal("completed",{reason:e})}incrementAutoRespond(){this.autoRespondCount++}resetAutoRespond(){this.autoRespondCount=0}getOutput(e){return e===void 0?this.outputBuffer.slice():this.outputBuffer.slice(-e)}resetIdleTimer(){if(!this.multiTurn)return;let e=(_.idleTimeoutMinutes??15)*60*1e3;this.setTimer("idle",e,()=>{this._status==="running"&&(this.logDiagnostic("idle_timeout.fire",{idleTimeoutMinutes:_.idleTimeoutMinutes??15}),this.applyControlEvent({type:"terminal.entered",suspended:!0}),this.kill("idle-timeout"))})}teardown(){if(this.teardownPromise)return;this.clearAllTimers(),this.completedAt||(this.completedAt=Date.now()),this.messageStream&&this.messageStream.end(),this.harnessHandle?.interrupt&&this.harnessHandle.interrupt().catch(t=>{`${this.id}${dt(t)}`});let e=this.harnessHandle?.close?this.harnessHandle.close().catch(t=>{`${this.id}${dt(t)}`}):Promise.resolve();this.teardownPromise=e,this.abortController.abort(),this.applyControlEvent({type:"terminal.entered",suspended:this.lifecycle==="suspended"})}transitionToTerminal(e,t={}){this.isActive&&(this.logDiagnostic("terminal.transition",{nextStatus:e,reason:t.reason,error:t.error,currentStatus:this._status,lifecycle:this.lifecycle,runtimeState:this.runtimeState}),this.turnInProgress=!1,t.reason&&(this.killReason=t.reason),t.error!==void 0&&(this.error=t.error),this.completedAt=Date.now(),this.applyControlEvent({type:"terminal.entered",suspended:e==="killed"&&t.reason==="idle-timeout"}),this.teardown(),this.transition(e))}async consumeMessages(e){let t=0;for await(let r of e){if(!this.isActive)break;this.resetIdleTimer(),t+=1,this.harnessEvents.applyMessage(r,{pendingPlanApproval:this.pendingPlanApproval,currentPermissionMode:this.currentPermissionMode,permissionMode:this.permissionMode,planModeApproved:this.planModeApproved,pendingInputState:this.pendingInputState})}this.logDiagnostic("harness.stream.end",{messageCount:t,activeAtEnd:this.isActive,status:this._status,lifecycle:this.lifecycle,runtimeState:this.runtimeState})}controlStateSnapshot(){return{status:this._status,lifecycle:this.lifecycle,approvalState:this.approvalState,approvalExecutionState:this.approvalExecutionState,worktreeState:this.worktreeState,runtimeState:this.runtimeState,deliveryState:this.deliveryState,requestedPermissionMode:this.requestedPermissionMode,currentPermissionMode:this.currentPermissionMode,pendingPlanApproval:this.pendingPlanApproval,planApprovalContext:this.planApprovalContext,planDecisionVersion:this.planDecisionVersion,actionablePlanDecisionVersion:this.actionablePlanDecisionVersion,canonicalPlanPromptVersion:this.canonicalPlanPromptVersion,approvalPromptRequiredVersion:this.approvalPromptRequiredVersion,approvalPromptVersion:this.approvalPromptVersion,approvalPromptStatus:this.approvalPromptStatus,approvalPromptTransport:this.approvalPromptTransport,approvalPromptMessageKind:this.approvalPromptMessageKind,approvalPromptLastAttemptAt:this.approvalPromptLastAttemptAt,approvalPromptDeliveredAt:this.approvalPromptDeliveredAt,approvalPromptFailedAt:this.approvalPromptFailedAt,planModeApproved:this.planModeApproved}}applyControlEvent(e){let t=Jr(this.controlStateSnapshot(),e);this.applyControlState(t)}applyControlPatch(e){let t=Yi(this.controlStateSnapshot(),e);this.applyControlState(t)}setControlField(e,t){this.applyControlPatch({[e]:t})}applyControlState(e){let t=this.lifecycle;this.lifecycle=e.lifecycle,this.approvalState=e.approvalState,this.approvalExecutionState=e.approvalExecutionState,this.worktreeState=e.worktreeState,this.runtimeState=e.runtimeState,this.deliveryState=e.deliveryState,this.pendingPlanApproval=e.pendingPlanApproval,this.planApprovalContext=e.planApprovalContext,this.planDecisionVersion=e.planDecisionVersion,this.actionablePlanDecisionVersion=e.actionablePlanDecisionVersion,this.canonicalPlanPromptVersion=e.canonicalPlanPromptVersion,this.approvalPromptRequiredVersion=e.approvalPromptRequiredVersion,this.approvalPromptVersion=e.approvalPromptVersion,this.approvalPromptStatus=e.approvalPromptStatus,this.approvalPromptTransport=e.approvalPromptTransport,this.approvalPromptMessageKind=e.approvalPromptMessageKind,this.approvalPromptLastAttemptAt=e.approvalPromptLastAttemptAt,this.approvalPromptDeliveredAt=e.approvalPromptDeliveredAt,this.approvalPromptFailedAt=e.approvalPromptFailedAt,this.planModeApproved=e.planModeApproved,t!==this.lifecycle&&this.emit("lifecycleChange",this,this.lifecycle,t)}logDiagnostic(e,t={}){let{backendRef:r,harnessSessionId:o,...i}=t;Vd(e,{sessionId:this.id,name:this.name,status:this._status,lifecycle:this.lifecycle,runtimeState:this.runtimeState,harness:this.harnessName,hasHarnessSessionId:!!this.harnessSessionId,...Ud(this.backendRef),...i})}};var Gd=50,jd=1,zd=new Set(["starting","running","completed","failed","killed"]),Hd=5,Qd=1440*60*1e3,vn=new Set(["merged","released","dismissed","no_change"]);function Yd(n){let e=Number(n);return!Number.isFinite(e)||e<jd?Gd:Math.floor(e)}function Jd(n){return typeof n=="string"&&zd.has(n)}function Xd(n){let e=n.split(`
|
|
34
|
+
`);return e.at(-1)===""&&e.pop(),e}function eo(n){return n.backendRef?.conversationId??n.harnessSessionId}function Zd(n,e){return e&&vn.has(e)&&(!n||!vn.has(n))?e:n??e}function eu(n,e){return e&&vn.has(e.state)&&(!n||!vn.has(n.state))?e:n??e}function ns(n,e,t){let r=ts(n,"utf-8"),o=Xd(r);return e.full?o:o.slice(-t)}function tu(n,e,t){if(n.status!=="starting"&&n.status!=="running")return null;let r=ve(n.id);if(!Zr(r))return null;try{return ns(r,e,t)}catch{return null}}function nu(n){let e=se(n.duration),t=` | Cost: $${n.costUsd.toFixed(4)}`,r=n.phase?` | Phase: ${n.phase}`:"",o=n.lifecycle&&n.lifecycle!==n.phase?` | Lifecycle: ${n.lifecycle}`:"";return[`Session: ${n.name} [${n.id}] | Status: ${n.status.toUpperCase()}${r}${o}${t} | Duration: ${e}`,`${"─".repeat(60)}`].join(`
|
|
35
|
+
`)}function rs(n,e){let t=n.name||n.sessionId||eo(n)||"unknown",r=n.lifecycle?` | Phase: ${n.lifecycle}`:"";return[`Session: ${t} | Status: ${n.status.toUpperCase()}${r} | Cost: $${n.costUsd.toFixed(4)}`,e,`${"─".repeat(60)}`].join(`
|
|
36
|
+
`)}function ru(n){return`(retrieved from ${n.outputPath} — evicted from runtime cache — showing persisted output)`}function ou(n){return n.outputPath?`(persisted session metadata recovered; output file is unavailable at ${n.outputPath})`:"(persisted session metadata recovered; no output file was recorded before the runtime was interrupted)"}function es(n,e){let t=rs(n,ou(n)),r=eo(n),o=r?`
|
|
36
37
|
Backend session: ${r}`:"";return`${t}
|
|
37
|
-
(no persisted output available: ${e})${o}`}function
|
|
38
|
+
(no persisted output available: ${e})${o}`}function iu(n){let e=[];return n.error&&e.push(`Error: ${n.error}`),n.result?.result&&e.push(`Result: ${n.result.result}`),n.result&&e.push(`Result status: ${n.result.subtype}`),e.length>0?`
|
|
38
39
|
(no output yet)
|
|
39
40
|
${e.join(`
|
|
40
41
|
`)}`:`
|
|
41
|
-
(no output yet)`}function
|
|
42
|
-
`),
|
|
43
|
-
${u}`:`${
|
|
44
|
-
(output file was empty)`}catch(u){let
|
|
42
|
+
(no output yet)`}function Pn(n,e,t={}){let r=Yd(t.lines),o=n.resolve(e);if(!o){let d=n.getPersistedSession(e);if(d?.outputPath&&Zr(d.outputPath))try{let u=ns(d.outputPath,t,r).join(`
|
|
43
|
+
`),m=rs(d,ru(d));return u?`${m}
|
|
44
|
+
${u}`:`${m}
|
|
45
|
+
(output file was empty)`}catch(u){let m=u instanceof Error?u.message:String(u);return`Error: Session "${e}" was cleaned up (expired) and output file could not be read: ${m}`}return d?.outputPath?es(d,`output file is missing at ${d.outputPath}`):d?es(d,"no outputPath is stored for this recovered session"):`Error: Session "${e}" not found.`}let i=tu(o,t,r),s=i&&i.length>0?i:t.full?o.getOutput():o.getOutput(r),a=nu(o),l=s.length===0?`${a}${iu(o)}`:`${a}
|
|
45
46
|
${s.join(`
|
|
46
|
-
`)}`;if(o.pendingPlanApproval&&o.planFilePath)try{if(
|
|
47
|
+
`)}`;if(o.pendingPlanApproval&&o.planFilePath)try{if(Zr(o.planFilePath)){let d=ts(o.planFilePath,"utf-8"),u="─".repeat(60);return`${l}
|
|
47
48
|
${u}
|
|
48
49
|
Plan file: ${o.planFilePath}
|
|
49
50
|
${u}
|
|
50
|
-
${d}`}}catch{}return l}function
|
|
51
|
+
${d}`}}catch{}return l}function wn(n,e="all",t,r={}){let o=n.listPersistedSessions()??[],s=su(n.list("all"),o);if(e!=="all"&&(s=s.filter(a=>a.status===e)),t&&(s=s.filter(a=>a.originChannel===t)),r.full){let a=Date.now()-Qd;s=s.filter(l=>(l.startedAt??0)>=a)}else s=s.slice(0,Hd);return s.length===0?"No sessions found.":s.map(a=>Ni(a)).join(`
|
|
51
52
|
|
|
52
|
-
`)}function
|
|
53
|
+
`)}function su(n,e){let t=new Map;for(let r of e){if(!Jd(r.status))continue;let o=r.completedAt??Date.now(),i=r.createdAt??o,s=eo(r),a=r.sessionId??`persisted:${s??r.harnessSessionId}`;t.set(a,{id:r.sessionId??s??r.harnessSessionId,name:r.name||r.sessionId||s||r.harnessSessionId,status:r.status,startedAt:r.createdAt??0,completedAt:r.completedAt,duration:Math.max(0,o-i),prompt:r.prompt??"",workdir:r.workdir??"(unknown)",costUsd:r.costUsd??0,multiTurn:!0,phase:r.lifecycle??r.status,lifecycle:r.lifecycle,resumable:r.resumable,harness:r.harness,model:r.model,reasoningEffort:r.reasoningEffort,backendRef:r.backendRef,harnessSessionId:r.harnessSessionId,requestedPermissionMode:r.requestedPermissionMode,currentPermissionMode:r.currentPermissionMode,approvalExecutionState:r.approvalExecutionState,originChannel:r.originChannel,originThreadId:r.originThreadId,worktreePath:r.worktreePath,worktreeBranch:r.worktreeBranch,worktreeStrategy:r.worktreeStrategy,worktreeState:r.worktreeState,worktreeLifecycle:r.worktreeLifecycle,worktreeMerged:r.worktreeMerged,worktreeMergedAt:r.worktreeMergedAt,worktreePrUrl:r.worktreePrUrl,recovered:!0,runtimeRecovery:r.runtimeRecovery})}for(let r of n){let o=t.get(r.id);t.set(r.id,{id:r.id,name:r.name,status:r.status,startedAt:r.startedAt,completedAt:r.completedAt,duration:r.duration,prompt:r.prompt,workdir:r.originalWorkdir??r.workdir,costUsd:r.costUsd,multiTurn:r.multiTurn,phase:r.phase,lifecycle:r.lifecycle,resumable:r.isExplicitlyResumable,harness:r.harnessName,model:r.model,reasoningEffort:r.reasoningEffort,backendRef:r.backendRef,harnessSessionId:r.harnessSessionId,requestedPermissionMode:r.requestedPermissionMode,currentPermissionMode:r.currentPermissionMode,approvalExecutionState:r.approvalExecutionState,originChannel:r.originChannel,originThreadId:r.originThreadId,worktreePath:r.worktreePath,worktreeBranch:r.worktreeBranch,worktreeStrategy:r.worktreeStrategy,worktreeState:Zd(r.worktreeState,o?.worktreeState),worktreeLifecycle:eu(r.worktreeLifecycle,o?.worktreeLifecycle),worktreeMerged:r.worktreeMerged??o?.worktreeMerged,worktreeMergedAt:r.worktreeMergedAt??o?.worktreeMergedAt,worktreePrUrl:r.worktreePrUrl??o?.worktreePrUrl,recovered:!1})}return[...t.values()].sort((r,o)=>(o.startedAt??0)-(r.startedAt??0))}function au(n){if(!n||typeof n!="object")return"all";let e=n.status;switch(e){case"running":case"completed":case"failed":case"killed":case"all":return e;default:return"all"}}function os(n){return{name:"agent_sessions",description:"List coding agent sessions with their status and progress. By default, shows the 5 most recent sessions; set `full` to show all sessions from the last 24 hours.",parameters:p.Object({status:p.Optional(p.Union([p.Literal("all"),p.Literal("running"),p.Literal("completed"),p.Literal("failed"),p.Literal("killed")],{description:'Filter by status (default "all")'})),full:p.Optional(p.Boolean({description:"Show all sessions from the last 24h instead of just the most recent 5"}))}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};let r=au(t),o=n?.workspaceDir?$e(n.workspaceDir):void 0,i=!!(t&&typeof t=="object"&&t.full===!0);return{content:[{type:"text",text:wn(k,r,o,{full:i})}]}}}}function bn(n,e,t){let r=n.resolve(e);if(!r){let o=n.getPersistedSession(e);if(!o)return`Error: Session "${e}" not found.`;if(o.status==="killed"&&o.lifecycle==="suspended"){let i=t==="completed";if(n.updatePersistedSession(e,{status:i?"completed":"killed",lifecycle:"terminal",runtimeState:"stopped",resumable:!1,killReason:i?"done":"user"}))return i?`Recovered session ${o.name} [${o.sessionId??o.harnessSessionId}] marked as completed. No live process was running.`:`Recovered session ${o.name} [${o.sessionId??o.harnessSessionId}] dismissed. No live process was running.`}return`Session ${o.name} [${o.sessionId??o.harnessSessionId}] is a persisted ${o.status} record with no live process to kill.`}return r.status==="completed"||r.status==="failed"||r.status==="killed"?`Session ${r.name} [${r.id}] is already ${r.status}. No action needed.`:t==="completed"?(r.complete(),`Session ${r.name} [${r.id}] marked as completed.`):(n.kill(r.id),`Session ${r.name} [${r.id}] has been terminated.`)}function lu(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.session!="string"?!1:e.reason===void 0?!0:e.reason==="completed"||e.reason==="killed"}function is(n){return{name:"agent_kill",description:"Terminate or complete a running coding agent session by name or ID. Use reason='completed' to mark a session as successfully completed instead of killed.",parameters:p.Object({session:p.String({description:"Session name or ID to terminate"}),reason:p.Optional(p.Union([p.Literal("completed"),p.Literal("killed")],{description:"Reason for closing the session. 'completed' marks it as successfully done (sends ✅ notification). 'killed' (default) terminates it."}))}),async execute(e,t){return k?lu(t)?{content:[{type:"text",text:bn(k,t.session,t.reason)}]}:{content:[{type:"text",text:"Error: Invalid parameters. Expected { session, reason? }."}]}:{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]}}}}function du(n){return!n||typeof n!="object"?!1:typeof n.session=="string"}function Rn(n){return{name:"agent_output",description:"Show recent output from a coding agent session (by name or ID).",parameters:p.Object({session:p.String({description:"Session name or ID to get output from"}),lines:p.Optional(p.Number({description:"Number of recent lines to show (default 50)"})),full:p.Optional(p.Boolean({description:"Show all available output"}))}),async execute(e,t){return k?du(t)?{content:[{type:"text",text:Pn(k,t.session,{full:t.full,lines:t.lines})}]}:{content:[{type:"text",text:"Error: Invalid parameters. Expected { session, lines?, full? }."}]}:{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]}}}}var uu=10;function no(n){return n instanceof Error?n.message:String(n)}function Pe(n){return D(n)??n.harnessSessionId??"unknown-session"}function cu(n){if(!(!n.worktreeStrategy||n.worktreeStrategy==="off"))return nn(n)??D(n)??C(n)}function ss(n){return n.status!=="running"&&!!n.pendingPlanApproval&&!!C(n)}function pu(n){let e=n.trim().toLowerCase().replace(/[.!?]+$/g,"").trim();if(e==="approve"||e==="approved")return"approve";if(e==="revise"||e==="request changes"||e==="changes requested")return"revise";if(e==="reject"||e==="rejected")return"reject"}function ut(n,e,t){let r=e==="completed"?`This session is closed by default. Launch a fresh session, or fork from prior context with agent_launch(resume_session_id='${Pe(n)}', fork_session=true, prompt='<new task>').`:e==="legacy_non_resumable"?`Persisted historical backend state is intentionally non-resumable here. Launch fresh, or fork with agent_launch(resume_session_id='${Pe(n)}', fork_session=true, prompt='<new task>').`:e==="missing_backend_state"?`No resumable backend state is available. Launch a fresh session, or fork from prior context with agent_launch(resume_session_id='${Pe(n)}', fork_session=true, prompt='<new task>').`:`Use agent_respond(session='${Pe(n)}', message='<next instruction>') to continue the running session.`,o=t?` ${t}`:"";return{text:`Resume unavailable for session ${n.name} [${Pe(n)}] (${e}).${o} ${r}`,isError:!0}}async function as(n,e,t){try{let r={prompt:e.prompt,sessionIdOverride:nn(e),workdir:e.workdir,name:e.name,model:e.model,reasoningEffort:e.reasoningEffort,fastMode:e.fastMode,worktreeStrategy:e.worktreeStrategy,multiTurn:!0,originChannel:e.originChannel,originThreadId:e.originThreadId,originAgentId:e.originAgentId,originSessionKey:e.originSessionKey,route:e.route,permissionMode:e.currentPermissionMode,requestedPermissionMode:e.requestedPermissionMode??e.currentPermissionMode,planApproval:e.planApproval,codexApprovalPolicy:e.codexApprovalPolicy,harness:"harnessName"in e?e.harnessName:e.harness},o=await n.spawnAndAwaitRunning(r,{notifyLaunch:!1});return n.notifySession(o,`▶️ [${o.name}] Relaunched fresh`,"notification",`agent-respond-relaunched:${o.id}:${o.startedAt}`),{text:`Session ${e.name} was relaunched fresh — it was killed during startup before the harness initialized. New session: ${o.name} [${o.id}].`}}catch(r){return{text:`Error relaunching session ${e.name} [${Pe(e)}]: ${no(r)}`,isError:!0}}}var mu=`[SYSTEM: The user has approved your plan. Exit plan mode immediately and implement the changes with full permissions. Do not ask for further confirmation.]
|
|
53
54
|
|
|
54
|
-
`;function
|
|
55
|
-
`)}}let
|
|
55
|
+
`;function ls(n){let e=n?.replace(/\s+/g," ").trim();if(e)return G(e,240)}function to(n){if(n.approvalState==="changes_requested"&&!An(n))return`Plan changes were already requested for session ${n.name}. Wait for the revised plan before approving.`;if(n.approvalState==="rejected")return`Plan for session ${n.name} was already rejected.`}function An(n){return n.pendingPlanApproval?(n.approvalState==="changes_requested"?n.actionablePlanDecisionVersion:n.actionablePlanDecisionVersion??n.planDecisionVersion)>0:!1}function fu(n){return to(n)?!1:An(n)||n.currentPermissionMode==="plan"}function gu(n,e){let t=n.updatePersistedSession;if(typeof t!="function")return;let r=e.controlStateSnapshot();t.call(n,e.id,{lifecycle:r.lifecycle,approvalState:r.approvalState,approvalExecutionState:r.approvalExecutionState,runtimeState:r.runtimeState,requestedPermissionMode:r.requestedPermissionMode,currentPermissionMode:r.currentPermissionMode,pendingPlanApproval:r.pendingPlanApproval,planApprovalContext:r.planApprovalContext,planDecisionVersion:r.planDecisionVersion,actionablePlanDecisionVersion:r.actionablePlanDecisionVersion,planModeApproved:r.planModeApproved,approvalRationale:e.approvalRationale})}function hu(n){return(n.planDecisionVersion??0)+1}function ds(n,e){let t={approvalState:e,lifecycle:e==="rejected"?"terminal":"awaiting_user_input",pendingPlanApproval:!1,planApprovalContext:void 0,planDecisionVersion:hu(n),actionablePlanDecisionVersion:void 0,canonicalPlanPromptVersion:void 0,approvalPromptRequiredVersion:void 0,approvalPromptVersion:void 0,approvalPromptStatus:"not_sent",approvalPromptTransport:"none",approvalPromptMessageKind:"none",approvalPromptLastAttemptAt:void 0,approvalPromptDeliveredAt:void 0,approvalPromptFailedAt:void 0};return e==="rejected"&&(t.runtimeState="stopped"),t}function us(n,e){let t=n;typeof t.applyControlPatch=="function"&&t.applyControlPatch(e),Object.assign(n,e)}function ro(n,e){let t=n.resolve(e),r=t?void 0:n.getPersistedSession(e),o=t??r,i=o?.name??e;if(n.clearPlanDecisionTokens?.(e),!o)return{text:`Plan rejected for [${i}].`,isError:!1};let s=ds(o,"rejected");return t?(us(t,s),n.updatePersistedSession?.(e,s),n.kill(t.id,"user"),{text:`Plan rejected for [${t.name}]. Session stopped.`}):(n.updatePersistedSession?.(e,s),{text:`Plan rejected for [${i}]. Session remains stopped.`})}function oo(n,e){let t=n.resolve(e),r=t?void 0:n.getPersistedSession(e),o=t??r,i=o?.name??e;if(n.clearPlanDecisionTokens?.(e),o){let s=ds(o,"changes_requested");t&&us(t,s),n.updatePersistedSession?.(e,s)}return{text:`Type your revision feedback for [${i}] and I'll forward it to the agent.`}}async function yu(n,e,t,r={}){let o=ke(e);if(!(o.kind==="resume"||ss(e)))return;let s=!!(r.approve&&fu(e)),a=s?ls(r.approvalRationale):void 0;try{if(o.kind!=="resume")return o.kind==="relaunch"?as(n,e,t):ut(e,o.reason);let l={prompt:s?mu+t:t,sessionIdOverride:o.stableSessionId,workdir:e.workdir,name:e.name,model:e.model,reasoningEffort:e.reasoningEffort,fastMode:e.fastMode,resumeSessionId:o.resumeSessionId,resumeWorktreeFrom:cu(e),worktreeStrategy:e.worktreeStrategy,worktreeBaseBranch:e.worktreeBaseBranch,worktreePrTargetRepo:e.worktreePrTargetRepo,multiTurn:!0,originChannel:e.originChannel,originThreadId:e.originThreadId,originAgentId:e.originAgentId,originSessionKey:e.originSessionKey,route:e.route,permissionMode:s?"bypassPermissions":e.currentPermissionMode,requestedPermissionMode:e.requestedPermissionMode??e.currentPermissionMode,planApproval:e.planApproval,codexApprovalPolicy:e.codexApprovalPolicy,pendingPlanApproval:s?!1:e.pendingPlanApproval,planApprovalContext:e.planApprovalContext,planDecisionVersion:s?(e.actionablePlanDecisionVersion??e.planDecisionVersion)+1:e.planDecisionVersion,actionablePlanDecisionVersion:s?void 0:e.actionablePlanDecisionVersion,canonicalPlanPromptVersion:e.canonicalPlanPromptVersion,approvalPromptRequiredVersion:e.approvalPromptRequiredVersion,approvalPromptVersion:e.approvalPromptVersion,approvalPromptStatus:e.approvalPromptStatus,approvalPromptTransport:e.approvalPromptTransport,approvalPromptMessageKind:e.approvalPromptMessageKind,approvalPromptLastAttemptAt:e.approvalPromptLastAttemptAt,approvalPromptDeliveredAt:e.approvalPromptDeliveredAt,approvalPromptFailedAt:e.approvalPromptFailedAt,...s?{approvalState:"approved",approvalExecutionState:"awaiting_plan_output",approvalRationale:a,planModeApproved:!0}:{},harness:"harnessName"in e?e.harnessName:e.harness},d=await n.spawnAndAwaitRunning(l,{notifyLaunch:!1});return s?(n.notifySession(d,`👍 [${d.name}] Plan approved (resumed)`,"plan-approved",`agent-respond-plan-approved-resumed:${d.id}:${d.startedAt}:${o.resumeSessionId}:v${e.planDecisionVersion??"unknown"}`),{text:`Plan approved for session ${d.name} [${d.id}]. Session resumed in bypassPermissions mode. Use agent_output to see the response.`}):(n.notifyResumedLaunch(d),{text:`Resume started for session ${d.name} [${d.id}]. Use agent_output to see the response.`})}catch(l){return ut(e,"missing_backend_state",`Backend resume failed: ${no(l)}`)}}async function Q(n,e){let t=n.resolve(e.session),r=t?void 0:n.getPersistedSession(e.session);if(!t&&!r)return{text:`Error: Session "${e.session}" not found.`,isError:!0};let o=t??r,i=o.status==="running"?{kind:"direct"}:ke(o),s=!e.approve&&o.lifecycle==="awaiting_plan_decision"&&o.pendingPlanApproval&&e.userInitiated?pu(e.message):void 0;if(s==="approve")return Q(n,{...e,message:"Approved. Go ahead.",approve:!0});if(s==="revise")return oo(n,t?.id??r?.sessionId??e.session);if(s==="reject")return ro(n,t?.id??r?.sessionId??e.session);if(e.approve){let l=to(o);if(l)return{text:l,isError:!0}}if(i.kind==="resume"||ss(o)){let l=await yu(n,o,e.message,{approve:e.approve,approvalRationale:e.approvalRationale});if(l)return l}else{if(i.kind==="relaunch")return as(n,o,e.message);if(i.kind==="unavailable")return ut(o,i.reason)}if(!t)return lt(r)?ut(r,"completed"):{text:`Error: Session ${r.name} [${Pe(r)}] is not running (status: ${r.status}). Cannot send a message to a non-running session.`,isError:!0};if(t.status!=="running")return lt(t)?ut(t,"completed"):{text:`Error: Session ${t.name} [${t.id}] is not running (status: ${t.status}). Cannot send a message to a non-running session.`,isError:!0};let a=_.maxAutoResponds??uu;if(e.userInitiated)t.resetAutoRespond();else if(t.autoRespondCount>=a)return{text:`⚠️ Auto-respond limit reached (${t.autoRespondCount}/${a}). Ask the user to provide the answer for session ${t.name}. Then call agent_respond with their answer and set userInitiated: true to reset the counter.`};try{if(e.approve){let h=to(t);if(h)return{text:h,isError:!0}}let l=t.pendingInputState?.activeQuestionIndex,d=t.pendingInputState?.questions?.length;if(!e.approve&&t.pendingInputState?.allowsFreeText&&await t.submitPendingInputText?.(e.message)===!0){if(e.userInitiated){let b=G(e.message,100);n.notifySession(t,`↪️ [${t.name}] "${b}"`,"agent-respond")}e.userInitiated||t.incrementAutoRespond();let h=G(e.message,80),P=l!=null&&d!=null&&l+1<d;return{text:[P?`Pending input question answered for session ${t.name} [${t.id}]; more input is required.`:`Pending input request submitted for session ${t.name} [${t.id}].`,` Message: "${h}"`,P?"The next question is being delivered to the user.":"Use agent_output to see the response."].join(`
|
|
56
|
+
`)}}let m=!1;e.interrupt&&(m=await t.interrupt());let g=!!(e.approve&&An(t)),S=g?ls(e.approvalRationale):void 0,y="";if(e.approve&&An(t)?(t.switchPermissionMode("bypassPermissions"),t.approvalRationale=S):e.approve&&t.currentPermissionMode==="default"?t.switchPermissionMode("bypassPermissions"):e.approve&&t.currentPermissionMode==="bypassPermissions"?y=`
|
|
56
57
|
ℹ️ approve=true was set but session is already in bypassPermissions mode.`:e.approve?y=`
|
|
57
58
|
⚠️ approve=true was set but session has no pending plan approval.`:(t.pendingPlanApproval||t.approvalState==="changes_requested")&&(y=`
|
|
58
|
-
ℹ️ Session has a pending plan — sending as revision feedback. The agent will revise and re-submit. Set approve=true to approve instead.`),await t.sendMessage(e.message),g&&
|
|
59
|
-
`)}}catch(l){return{text:`Error sending message to session ${t.name} [${t.id}]: ${Qr(l)}`,isError:!0}}}function uu(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.session=="string"&&typeof e.message=="string"&&(e.approval_rationale===void 0||typeof e.approval_rationale=="string")}function ts(n){return{name:"agent_respond",description:"Send a follow-up message to a running coding agent session. The session must be running. All sessions are multi-turn.",parameters:p.Object({session:p.String({description:"Session name or ID to respond to"}),message:p.String({description:"The message to send to the session"}),interrupt:p.Optional(p.Boolean({description:"If true, interrupt the current turn before sending the message. Useful to redirect the session mid-response."})),userInitiated:p.Optional(p.Boolean({description:"Set to true when the message comes from the user (not auto-generated). Resets the auto-respond counter and bypasses the auto-respond limit."})),approve:p.Optional(p.Boolean({description:"Set to true to escalate session permissions to bypassPermissions. Works in two scenarios: (1) approve a pending plan in plan mode, or (2) escalate a default-mode session to skip remaining OpenClaw approval checkpoints. No-op if already in bypassPermissions mode. In plan mode without a pending plan, this flag is ignored."})),approval_rationale:p.Optional(p.String({description:"Optional structured rationale for a direct delegated plan approval. Use this instead of embedding the rationale in message text when approve=true for a pending plan."}))}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};if(!uu(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected { session, message, interrupt?, userInitiated?, approve?, approval_rationale? }."}]};let r=await Q(k,{session:t.session,message:t.message,interrupt:t.interrupt,userInitiated:t.userInitiated,approve:t.approve,approvalRationale:t.approval_rationale});return{isError:r.isError??!1,content:[{type:"text",text:r.text}]}}}}function cu(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.session=="string"&&typeof e.summary=="string"}function ns(n){return{name:"agent_request_plan_approval",description:"Send a plan-approval decision prompt to the user for a session that is already waiting on plan approval. Reuses the plugin's Approve/Revise/Reject buttons so the user can decide directly from the message.",parameters:p.Object({session:p.String({description:"Session name or ID awaiting plan approval"}),summary:p.String({description:"Concise user-facing escalation rationale covering scope match, affected files/systems, destructive or external effects, material risks, verification, and unknowns or decisions"})}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};if(!cu(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected { session, summary }."}]};let r=k.requestPlanApprovalFromUser(t.session,t.summary);return{isError:r.startsWith("Error:"),content:[{type:"text",text:r}]}}}}function pu(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.session=="string"&&typeof e.summary=="string"}function rs(n){return{name:"agent_request_worktree_decision",description:"Send a worktree Merge/Open PR/Later/Discard decision prompt to the user for a delegated worktree session that is awaiting human choice.",parameters:p.Object({session:p.String({description:"Delegated session name or ID awaiting a worktree decision"}),summary:p.String({description:"Concise user-facing summary of scope, risk, and why human choice is needed"})}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};if(!pu(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected { session, summary }."}]};let r=k.requestWorktreeDecisionFromUser(t.session,t.summary);return{isError:r.startsWith("Error:"),content:[{type:"text",text:r}]}}}}var Xr=["off","manual","ask","delegate","auto-merge","auto-pr"],vn=new Set(Xr),mu=["pr-required","pr-allowed","never-pr","manual"],os=new Set(mu),fu=["low","medium","high","xhigh","max"],is=new Set(fu);function gu(n){return typeof n=="string"&&vn.has(n)}var hu=p.Union(Xr.map(n=>p.Literal(n)),{description:"Optional worktree strategy for the planning session. Use auto-pr when the follow-up should branch and open/update a PR after approved implementation."});function yu(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.offer_id=="string"&&typeof e.offer_text=="string"&&typeof e.plan_prompt=="string"&&typeof e.plan_workdir=="string"&&(e.plan_worktree_strategy==null||gu(e.plan_worktree_strategy))&&(e.plan_name==null||typeof e.plan_name=="string")&&(e.target_channel==null||typeof e.target_channel=="string")&&(e.target_thread_id==null||typeof e.target_thread_id=="string"||typeof e.target_thread_id=="number")&&(e.target_session_key==null||typeof e.target_session_key=="string")}function Su(n,e){let t=De(n,e.target_channel,e.target_session_key);if(t)return e.target_thread_id!=null&&(t.threadId=String(e.target_thread_id)),t}async function ku(n,e){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};let t=Su(n,e);return!t?.provider||!t.target?{content:[{type:"text",text:"Error: Could not resolve a direct delivery route for the plan offer."}]}:(k.sendPlanOffer({offerId:e.offer_id,route:t,text:e.offer_text,planName:e.plan_name??e.offer_id,planPrompt:e.plan_prompt,planWorkdir:e.plan_workdir,planWorktreeStrategy:e.plan_worktree_strategy}),{content:[{type:"text",text:`Interactive plan offer queued for ${t.provider}|${t.target}${t.threadId?`#${t.threadId}`:""}.`}]})}function ss(n){return{name:"agent_send_plan_offer",description:"Post a user-facing message to the current or explicit chat route with human-gated Start Plan and Dismiss inline buttons. Use this when an external workflow wants the user to open a plan-only code-agent session from the message.",parameters:p.Object({offer_id:p.String({description:"Stable offer identifier, for example 'plugin-readiness-v2026.5.5'."}),offer_text:p.String({description:"Final user-facing text to deliver."}),plan_prompt:p.String({description:"Prompt to seed into the plan-only session when the user clicks Start Plan."}),plan_workdir:p.String({description:"Working directory for the planning session."}),plan_worktree_strategy:p.Optional(hu),plan_name:p.Optional(p.String({description:"Optional explicit session name for the planning session."})),target_channel:p.Optional(p.String({description:"Optional explicit route like 'telegram|<chat-id>'."})),target_thread_id:p.Optional(p.Union([p.String(),p.Number()],{description:"Optional explicit topic/thread id."})),target_session_key:p.Optional(p.String({description:"Optional explicit session key for wake routing."}))}),async execute(e,t){return yu(t)?ku(n,t):{content:[{type:"text",text:"Error: Invalid parameters. Expected { offer_id, offer_text, plan_prompt, plan_workdir }."}]}}}}function as(n){return{name:"agent_stats",description:"Show OpenClaw Code Agent usage metrics: session counts by status, average duration, and notable sessions.",parameters:p.Object({}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};let r=k.getMetrics(),o=k.list("running").length;return{content:[{type:"text",text:Zt(r,o)}]}}}}import{execFileSync as vu}from"child_process";var eo=[{policy:"pr-required",label:"Require PR",title:"Require PR",description:"Direct merge is disabled; follow-through must use a pull request."},{policy:"pr-allowed",label:"Merge or PR",title:"Merge or PR",description:"Use the requested strategy; direct merge and pull requests are both allowed."},{policy:"never-pr",label:"No PR",title:"No PR",description:"Do not create pull requests; keep follow-through local/direct."},{policy:"manual",label:"Manual",title:"Manual",description:"Create isolated worktrees, but require an explicit human follow-up decision every time."}],Pu=new Map(eo.map(n=>[n.policy,n]));function at(n){return n?eo:eo.filter(e=>e.policy==="never-pr"||e.policy==="manual")}function ls(n){return Pu.get(n)??{policy:n,label:n,title:n,description:"Custom repo integration policy."}}function Ke(n,e){if(!(e||n!=="pr-required"&&n!=="pr-allowed"))return`Policy ${n} requires PR automation, but no supported PR provider is available. Choose never-pr or manual.`}function to(n){let{policy:e,prAvailable:t}=n;return{merge:e!=="pr-required"&&e!=="manual",pr:e!=="never-pr"&&e!=="manual"&&t}}function Zr(n,e){try{return vu("git",e,{cwd:n,timeout:5e3,encoding:"utf-8",stdio:["pipe","pipe","pipe"]}).trim()||void 0}catch{return}}function ds(n){if(!n)return;let e=n.trim().replace(/\/+$/,"").replace(/\.git$/i,""),t=e.match(/^git@([^:]+):(.+)$/);if(t)return`https://${t[1].toLowerCase()}/${t[2].toLowerCase()}`;let r=e.match(/^https?:\/\/([^/]+)\/(.+)$/i);return r?`https://${r[1].toLowerCase()}/${r[2].toLowerCase()}`:e.toLowerCase()}function wu(n){return ds(n)?.startsWith("https://github.com/")?"github":"unsupported"}function lt(n){let e=Zr(n,["rev-parse","--show-toplevel"]);if(!e)return;let t=Zr(e,["remote","get-url","origin"])??Zr(e,["remote","get-url","upstream"]),r=ds(t),o=wu(r);return{key:r?`${e}|${r}`:e,repoRoot:e,remoteUrl:r,provider:o}}function us(n){if(n?.remoteUrl)return n.remoteUrl==="https://github.com/goldmar/openclaw-code-agent"?"pr-required":void 0}function no(n,e,t="stored",r=new Date().toISOString()){return{key:n.key,policy:e,repoRoot:n.repoRoot,remoteUrl:n.remoteUrl,provider:n.provider,createdAt:r,updatedAt:r,source:t}}function ro(n){return n.provider==="github"&&Je()}function cs(n){let e=n.requestedStrategy,t=to(n);return!e||e==="off"||e==="manual"?{allowedActions:t}:n.policy?n.policy==="manual"?{strategy:e==="delegate"?"delegate":"ask",reason:"Repo policy requires manual follow-through.",allowedActions:{merge:!1,pr:!1}}:n.policy==="pr-required"?e==="auto-merge"?n.prAvailable?{strategy:"auto-pr",reason:"Repo policy requires a PR; auto-merge was downgraded to auto-pr.",allowedActions:t}:{strategy:"ask",blocked:!0,reason:"Repo policy requires a PR, but no supported PR provider is available.",allowedActions:t}:e==="auto-pr"?n.prAvailable?{strategy:"auto-pr",allowedActions:t}:{strategy:"ask",blocked:!0,reason:"Repo policy requires a PR, but no supported PR provider is available.",allowedActions:t}:{strategy:e,allowedActions:t}:n.policy==="never-pr"?e==="auto-pr"?n.existingOpenPr&&n.prAvailable?{strategy:"auto-pr",reason:"Repo policy forbids new PR creation; updating the existing open PR is allowed.",allowedActions:t}:{strategy:"ask",reason:"Repo policy forbids PR creation; auto-pr was downgraded to an explicit worktree decision.",allowedActions:t}:{strategy:e,allowedActions:t}:e==="auto-pr"&&!n.prAvailable?{strategy:"ask",reason:"No supported PR provider is available; auto-pr was downgraded to an explicit worktree decision.",allowedActions:t}:{strategy:e,allowedActions:t}:{strategy:e==="auto-merge"||e==="auto-pr"?"ask":e,blocked:!0,reason:"Repo integration policy is unknown.",allowedActions:{merge:!1,pr:!1}}}function oo(n,e,t=n.provider==="github"){let r=at(t);return[`Repo integration policy is not set for ${n.repoRoot}.`,"","OpenClaw Code Agent will create isolated worktrees, but it needs one repo policy before worktree follow-through can run.",`Requested worktree strategy: ${e}`,`Provider: ${n.provider}${t?"":" (PR automation unavailable)"}`,"","Choose one available policy:",...r.map(o=>`- ${o.title}: ${o.description}`),"","If buttons are unavailable, set it manually. OpenClaw Code Agent will continue the pending launch automatically when exactly one matching launch is waiting; otherwise run the intended launch again:",...r.map(o=>`- agent_repo_policy(workdir="${n.repoRoot}", policy="${o.policy}")`)].join(`
|
|
60
|
-
`)}function
|
|
61
|
-
`):"No stored repo policy found."}function
|
|
59
|
+
ℹ️ Session has a pending plan — sending as revision feedback. The agent will revise and re-submit. Set approve=true to approve instead.`),await t.sendMessage(e.message),g&&gu(n,t),g)n.notifySession(t,`👍 [${t.name}] Plan approved`,"plan-approved");else if(e.userInitiated){let h=G(e.message,100);n.notifySession(t,`↪️ [${t.name}] "${h}"`,"agent-respond")}e.userInitiated||t.incrementAutoRespond();let c=G(e.message,80);return g?{text:`Plan approved for session ${t.name} [${t.id}]. Use agent_output to see the response.`}:{text:[`Message sent to session ${t.name} [${t.id}].`,m?" (redirected active turn first)":e.interrupt?" (no active turn to interrupt)":"",` Message: "${c}"`,y,"Use agent_output to see the response."].filter(Boolean).join(`
|
|
60
|
+
`)}}catch(l){return{text:`Error sending message to session ${t.name} [${t.id}]: ${no(l)}`,isError:!0}}}function Su(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.session=="string"&&typeof e.message=="string"&&(e.approval_rationale===void 0||typeof e.approval_rationale=="string")}function cs(n){return{name:"agent_respond",description:"Send a follow-up message to a running coding agent session. The session must be running. All sessions are multi-turn.",parameters:p.Object({session:p.String({description:"Session name or ID to respond to"}),message:p.String({description:"The message to send to the session"}),interrupt:p.Optional(p.Boolean({description:"If true, interrupt the current turn before sending the message. Useful to redirect the session mid-response."})),userInitiated:p.Optional(p.Boolean({description:"Set to true when the message comes from the user (not auto-generated). Resets the auto-respond counter and bypasses the auto-respond limit."})),approve:p.Optional(p.Boolean({description:"Set to true to escalate session permissions to bypassPermissions. Works in two scenarios: (1) approve a pending plan in plan mode, or (2) escalate a default-mode session to skip remaining OpenClaw approval checkpoints. No-op if already in bypassPermissions mode. In plan mode without a pending plan, this flag is ignored."})),approval_rationale:p.Optional(p.String({description:"Optional structured rationale for a direct delegated plan approval. Use this instead of embedding the rationale in message text when approve=true for a pending plan."}))}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};if(!Su(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected { session, message, interrupt?, userInitiated?, approve?, approval_rationale? }."}]};let r=await Q(k,{session:t.session,message:t.message,interrupt:t.interrupt,userInitiated:t.userInitiated,approve:t.approve,approvalRationale:t.approval_rationale});return{isError:r.isError??!1,content:[{type:"text",text:r.text}]}}}}function ku(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.session=="string"&&typeof e.summary=="string"}function ps(n){return{name:"agent_request_plan_approval",description:"Send a plan-approval decision prompt to the user for a session that is already waiting on plan approval. Reuses the plugin's Approve/Revise/Reject buttons so the user can decide directly from the message.",parameters:p.Object({session:p.String({description:"Session name or ID awaiting plan approval"}),summary:p.String({description:"Concise user-facing escalation rationale covering scope match, affected files/systems, destructive or external effects, material risks, verification, and unknowns or decisions"})}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};if(!ku(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected { session, summary }."}]};let r=k.requestPlanApprovalFromUser(t.session,t.summary);return{isError:r.startsWith("Error:"),content:[{type:"text",text:r}]}}}}function vu(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.session=="string"&&typeof e.summary=="string"}function ms(n){return{name:"agent_request_worktree_decision",description:"Send a worktree Merge/Open PR/Later/Discard decision prompt to the user for a delegated worktree session that is awaiting human choice.",parameters:p.Object({session:p.String({description:"Delegated session name or ID awaiting a worktree decision"}),summary:p.String({description:"Concise user-facing summary of scope, risk, and why human choice is needed"})}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};if(!vu(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected { session, summary }."}]};let r=k.requestWorktreeDecisionFromUser(t.session,t.summary);return{isError:r.startsWith("Error:"),content:[{type:"text",text:r}]}}}}function Pu(n){return typeof n=="string"&&Zt.has(n)}var wu=p.Union(Vr.map(n=>p.Literal(n)),{description:"Optional worktree strategy for the planning session. Use auto-pr when the follow-up should branch and open/update a PR after approved implementation."});function bu(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.offer_id=="string"&&typeof e.offer_text=="string"&&typeof e.plan_prompt=="string"&&typeof e.plan_workdir=="string"&&(e.plan_worktree_strategy==null||Pu(e.plan_worktree_strategy))&&(e.plan_name==null||typeof e.plan_name=="string")&&(e.target_channel==null||typeof e.target_channel=="string")&&(e.target_thread_id==null||typeof e.target_thread_id=="string"||typeof e.target_thread_id=="number")&&(e.target_session_key==null||typeof e.target_session_key=="string")}function Ru(n,e){let t=De(n,e.target_channel,e.target_session_key);if(t)return e.target_thread_id!=null&&(t.threadId=String(e.target_thread_id)),t}async function Au(n,e){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};let t=Ru(n,e);return!t?.provider||!t.target?{content:[{type:"text",text:"Error: Could not resolve a direct delivery route for the plan offer."}]}:(k.sendPlanOffer({offerId:e.offer_id,route:t,text:e.offer_text,planName:e.plan_name??e.offer_id,planPrompt:e.plan_prompt,planWorkdir:e.plan_workdir,planWorktreeStrategy:e.plan_worktree_strategy}),{content:[{type:"text",text:`Interactive plan offer queued for ${t.provider}|${t.target}${t.threadId?`#${t.threadId}`:""}.`}]})}function fs(n){return{name:"agent_send_plan_offer",description:"Post a user-facing message to the current or explicit chat route with human-gated Start Plan and Dismiss inline buttons. Use this when an external workflow wants the user to open a plan-only code-agent session from the message.",parameters:p.Object({offer_id:p.String({description:"Stable offer identifier, for example 'plugin-readiness-v2026.5.5'."}),offer_text:p.String({description:"Final user-facing text to deliver."}),plan_prompt:p.String({description:"Prompt to seed into the plan-only session when the user clicks Start Plan."}),plan_workdir:p.String({description:"Working directory for the planning session."}),plan_worktree_strategy:p.Optional(wu),plan_name:p.Optional(p.String({description:"Optional explicit session name for the planning session."})),target_channel:p.Optional(p.String({description:"Optional explicit route like 'telegram|<chat-id>'."})),target_thread_id:p.Optional(p.Union([p.String(),p.Number()],{description:"Optional explicit topic/thread id."})),target_session_key:p.Optional(p.String({description:"Optional explicit session key for wake routing."}))}),async execute(e,t){return bu(t)?Au(n,t):{content:[{type:"text",text:"Error: Invalid parameters. Expected { offer_id, offer_text, plan_prompt, plan_workdir }."}]}}}}function gs(n){return{name:"agent_stats",description:"Show OpenClaw Code Agent usage metrics: session counts by status, average duration, and notable sessions.",parameters:p.Object({}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};let r=k.getMetrics(),o=k.list("running").length;return{content:[{type:"text",text:sn(r,o)}]}}}}import{execFileSync as Iu}from"child_process";var so=[{policy:"pr-required",label:"Require PR",title:"Require PR",description:"Direct merge is disabled; follow-through must use a pull request."},{policy:"pr-allowed",label:"Merge or PR",title:"Merge or PR",description:"Use the requested strategy; direct merge and pull requests are both allowed."},{policy:"never-pr",label:"No PR",title:"No PR",description:"Do not create pull requests; keep follow-through local/direct."},{policy:"manual",label:"Manual",title:"Manual",description:"Create isolated worktrees, but require an explicit human follow-up decision every time."}],Tu=new Map(so.map(n=>[n.policy,n]));function ct(n){return n?so:so.filter(e=>e.policy==="never-pr"||e.policy==="manual")}function hs(n){return Tu.get(n)??{policy:n,label:n,title:n,description:"Custom repo integration policy."}}function Ve(n,e){if(!(e||n!=="pr-required"&&n!=="pr-allowed"))return`Policy ${n} requires PR automation, but no supported PR provider is available. Choose never-pr or manual.`}function ao(n){let{policy:e,prAvailable:t}=n;return{merge:e!=="pr-required"&&e!=="manual",pr:e!=="never-pr"&&e!=="manual"&&t}}function io(n,e){try{return Iu("git",e,{cwd:n,timeout:5e3,encoding:"utf-8",stdio:["pipe","pipe","pipe"]}).trim()||void 0}catch{return}}function ys(n){if(!n)return;let e=n.trim().replace(/\/+$/,"").replace(/\.git$/i,""),t=e.match(/^git@([^:]+):(.+)$/);if(t)return`https://${t[1].toLowerCase()}/${t[2].toLowerCase()}`;let r=e.match(/^https?:\/\/([^/]+)\/(.+)$/i);return r?`https://${r[1].toLowerCase()}/${r[2].toLowerCase()}`:e.toLowerCase()}function xu(n){return ys(n)?.startsWith("https://github.com/")?"github":"unsupported"}function pt(n){let e=io(n,["rev-parse","--show-toplevel"]);if(!e)return;let t=io(e,["remote","get-url","origin"])??io(e,["remote","get-url","upstream"]),r=ys(t),o=xu(r);return{key:r?`${e}|${r}`:e,repoRoot:e,remoteUrl:r,provider:o}}function Ss(n){if(n?.remoteUrl)return n.remoteUrl==="https://github.com/goldmar/openclaw-code-agent"?"pr-required":void 0}function lo(n,e,t="stored",r=new Date().toISOString()){return{key:n.key,policy:e,repoRoot:n.repoRoot,remoteUrl:n.remoteUrl,provider:n.provider,createdAt:r,updatedAt:r,source:t}}function uo(n){return n.provider==="github"&&et()}function ks(n){let e=n.requestedStrategy,t=ao(n);return!e||e==="off"||e==="manual"?{allowedActions:t}:n.policy?n.policy==="manual"?{strategy:e==="delegate"?"delegate":"ask",reason:"Repo policy requires manual follow-through.",allowedActions:{merge:!1,pr:!1}}:n.policy==="pr-required"?e==="auto-merge"?n.prAvailable?{strategy:"auto-pr",reason:"Repo policy requires a PR; auto-merge was downgraded to auto-pr.",allowedActions:t}:{strategy:"ask",blocked:!0,reason:"Repo policy requires a PR, but no supported PR provider is available.",allowedActions:t}:e==="auto-pr"?n.prAvailable?{strategy:"auto-pr",allowedActions:t}:{strategy:"ask",blocked:!0,reason:"Repo policy requires a PR, but no supported PR provider is available.",allowedActions:t}:{strategy:e,allowedActions:t}:n.policy==="never-pr"?e==="auto-pr"?n.existingOpenPr&&n.prAvailable?{strategy:"auto-pr",reason:"Repo policy forbids new PR creation; updating the existing open PR is allowed.",allowedActions:t}:{strategy:"ask",reason:"Repo policy forbids PR creation; auto-pr was downgraded to an explicit worktree decision.",allowedActions:t}:{strategy:e,allowedActions:t}:e==="auto-pr"&&!n.prAvailable?{strategy:"ask",reason:"No supported PR provider is available; auto-pr was downgraded to an explicit worktree decision.",allowedActions:t}:{strategy:e,allowedActions:t}:{strategy:e==="auto-merge"||e==="auto-pr"?"ask":e,blocked:!0,reason:"Repo integration policy is unknown.",allowedActions:{merge:!1,pr:!1}}}function co(n,e,t=n.provider==="github"){let r=ct(t);return[`Repo integration policy is not set for ${n.repoRoot}.`,"","OpenClaw Code Agent will create isolated worktrees, but it needs one repo policy before worktree follow-through can run.",`Requested worktree strategy: ${e}`,`Provider: ${n.provider}${t?"":" (PR automation unavailable)"}`,"","Choose one available policy:",...r.map(o=>`- ${o.title}: ${o.description}`),"","If buttons are unavailable, set it manually. OpenClaw Code Agent will continue the pending launch automatically when exactly one matching launch is waiting; otherwise run the intended launch again:",...r.map(o=>`- agent_repo_policy(workdir="${n.repoRoot}", policy="${o.policy}")`)].join(`
|
|
61
|
+
`)}function Cu(n){return n==="pr-required"||n==="pr-allowed"||n==="never-pr"||n==="manual"}function mt(n){return n?[`Repo policy: ${n.policy}`,`Repo: ${n.repoRoot}`,`Provider: ${n.provider}`,...n.remoteUrl?[`Remote: ${n.remoteUrl}`]:[],`Updated: ${n.updatedAt}`].join(`
|
|
62
|
+
`):"No stored repo policy found."}function vs(n){return{name:"agent_repo_policy",description:"Inspect or set the repository integration policy that governs OpenClaw Code Agent worktree merge/PR follow-through.",parameters:p.Object({workdir:p.Optional(p.String({description:"Repository workdir. Defaults to the current workspace directory."})),policy:p.Optional(p.Union([p.Literal("pr-required"),p.Literal("pr-allowed"),p.Literal("never-pr"),p.Literal("manual")],{description:"Policy to set for this repo."})),reset:p.Optional(p.Boolean({description:"Remove the stored policy for this repo."})),list:p.Optional(p.Boolean({description:"List all stored repo policies."})),cleanup:p.Optional(p.Boolean({description:"Remove stored repo policies whose repoRoot no longer exists on disk."}))}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};let r=t&&typeof t=="object"?t:{};if(r.cleanup===!0){let a=k.cleanupRepoPolicies();return{content:[{type:"text",text:a.length===0?"No stale repo policies found.":[`Removed ${a.length} stale repo ${a.length===1?"policy":"policies"}.`,...a.map(d=>`${d.policy} | ${d.provider} | ${d.repoRoot}`)].join(`
|
|
62
63
|
`)}]}}if(r.list===!0){let a=k.listRepoPolicies();return{content:[{type:"text",text:a.length===0?"No stored repo policies.":a.map(d=>`${d.policy} | ${d.provider} | ${d.repoRoot}${d.remoteUrl?` | ${d.remoteUrl}`:""}`).join(`
|
|
63
|
-
`)}]}}let o=r.workdir??n?.workspaceDir;if(!o)return{content:[{type:"text",text:"Error: workdir is required when no workspace directory is available."}]};if(r.reset===!0)return{content:[{type:"text",text:k.resetRepoPolicy(o)?`Repo policy reset for ${o}.`:`No stored repo policy found for ${o}.`}]};if(r.policy!==void 0){if(!
|
|
64
|
-
`)}]}}return l.kind==="launched"?{content:[{type:"text",text:[
|
|
65
|
-
`)}]}:l.kind==="ambiguous"?{content:[{type:"text",text:[
|
|
66
|
-
`)}]}:{content:[{type:"text",text:
|
|
67
|
-
`)}]}}}}import{existsSync as
|
|
68
|
-
`)}]};let
|
|
69
|
-
⚠️ Pre-merge stash pop conflicted — run \`git stash show ${I.stashRef??"stash@{0}"}\` in ${d} to review stashed changes.`:I.stashed&&(
|
|
70
|
-
(Pre-existing changes on ${
|
|
71
|
-
|
|
72
|
-
${I.error}`,I)}]};else if(I.conflictFiles&&I.conflictFiles.length>0){let
|
|
73
|
-
`);try{let
|
|
74
|
-
`)}]}}}}function
|
|
75
|
-
`).trim()}]}}}}import{existsSync as
|
|
76
|
-
`)}function
|
|
77
|
-
`).map(t=>`${e}${t}`)}function
|
|
78
|
-
`);return o.length<=t?o:`...${o.slice(-(t-3))}`}function
|
|
79
|
-
`)}function
|
|
80
|
-
`)}var X="Error: GoalController not initialized. The code-agent service must be running.";function
|
|
81
|
-
|
|
82
|
-
`)}function
|
|
83
|
-
`):void 0}async function
|
|
84
|
-
|
|
85
|
-
Approval is still pending. Try again below.`:e,...o?{buttons:o}:{}})}function Is(n="telegram",e={}){let t=e.makeAgentMergeTool??wn,r=e.makeAgentPrTool??Vt;return M("callback_handler_registered",{channel:n,namespace:W}),{channel:n,namespace:W,handler:async o=>{let i=await ju(o);if(!o.auth.isAuthorizedSender)return await x(o,"⛔ Unauthorized."),{handled:!0};let s=Uu(o),a=Bu(s);if(M("callback_received",{channel:o.channel,namespace:W,payloadByteLength:Buffer.byteLength(s,"utf8"),tokenHash:K(a),isAuthorizedSender:o.auth.isAuthorizedSender}),!a)return await x(o,"⚠️ Unrecognized callback payload."),{handled:!0};if(!k)return await x(o,"⚠️ Code agent service not running."),{handled:!0};let l=k.getActionToken(a);if(M("callback_token_lookup_completed",{channel:o.channel,namespace:W,tokenHash:K(a),tokenFound:!!l,actionKind:l?.kind,sessionId:l?.sessionId,planDecisionVersion:l?.planDecisionVersion}),!l)return await qe(o,()=>N(o,{alreadyAcknowledged:i})),{handled:!0};if(l.kind==="question-answer"&&l.consumedAt!=null)return await N(o,{alreadyAcknowledged:i}),await x(o,"⚠️ That question button is no longer active. Use the latest question prompt."),{handled:!0};let d=l.sessionId,u=k.resolve?.(d)??k.getPersistedSession?.(d),f=u?.name??d;if(u&&qr(l,u))return k.consumePlanDecisionTokens?.(d,l.planDecisionVersion),await ne(o,i),await x(o,`✅ Plan v${l.planDecisionVersion} was already approved; resume is in progress or running.`),{handled:!0};let g=uo(l,u);if(M("callback_plan_validation_completed",{channel:o.channel,namespace:W,tokenHash:K(a),actionKind:l.kind,sessionId:d,sessionName:f,planDecisionVersion:l.planDecisionVersion,valid:!g}),g)return $n(l.kind)?await ne(o,i):await N(o,{alreadyAcknowledged:i}),await x(o,`⚠️ ${g}`),{handled:!0};if(l.kind==="question-answer"){if(l.optionIndex==null)return await N(o,{alreadyAcknowledged:i}),await x(o,"⚠️ Invalid question-answer action."),{handled:!0};let c=Wu(l);if(lo.has(c))return await x(o,"⚠️ That answer is already being submitted. If the question remains active, try again."),{handled:!0};lo.add(c);let h=!1,v=!1;try{if(h=await k.resolvePendingInputOption(d,l.optionIndex,{requestId:l.pendingInputRequestId,questionId:l.pendingInputQuestionId}),!h&&!(k.canSubmitPendingInputOption?.(d)??!1)){let R=k.getPersistedSession?.(d),m=Lu(l);m&&Fu(R)&&(h=!(await Q(k,{session:d,message:m,userInitiated:!0})).isError,v=h)}}catch(R){let m=R instanceof Error?R.message:String(R);return`${m}`,await x(o,vs),{handled:!0}}finally{lo.delete(c)}if(!h)return await x(o,vs),{handled:!0};let A=(l.pendingInputRequestId?k.consumeQuestionAnswerTokens(d,l.pendingInputRequestId,l.pendingInputQuestionId):[]).find(R=>R.id===a)??k.consumeActionToken(a);if(M("callback_token_consume_completed",{channel:o.channel,namespace:W,tokenHash:K(a),consumed:!!A,actionKind:A?.kind,sessionId:A?.sessionId,planDecisionVersion:A?.planDecisionVersion}),!A){await N(o,{alreadyAcknowledged:i});let R=!v&&(k.pendingInputSubmissionRequiresMore?.(d)??!1);return await x(o,v?"✅ Answer forwarded to the resumed session.":R?"✅ Question step answered; more input is required.":"✅ Pending input request submitted."),{handled:!0}}await N(o,{alreadyAcknowledged:i});let I=!v&&(k.pendingInputSubmissionRequiresMore?.(d)??!1);return await x(o,v?"✅ Answer forwarded to the resumed session.":I?"✅ Question step answered; more input is required.":"✅ Pending input request submitted."),{handled:!0}}let S=qu(l);if(S)for(;;){let c=ut.get(S);if(!c)break;if(c.tokenId===a)return await ne(o,i),await x(o,"⚠️ This plan decision is already being processed."),{handled:!0};await Rs(c.operation);let h=k.getActionToken(a);if(!h)return await qe(o,()=>ne(o,i)),{handled:!0};if(d=h.sessionId,u=k.resolve?.(d)??k.getPersistedSession?.(d),f=u?.name??d,u&&qr(h,u))return k.consumePlanDecisionTokens?.(d,h.planDecisionVersion),await ne(o,i),await x(o,`✅ Plan v${h.planDecisionVersion} was already approved; resume is in progress or running.`),{handled:!0};if(g=uo(h,u),M("callback_plan_validation_completed",{channel:o.channel,namespace:W,tokenHash:K(a),actionKind:h.kind,sessionId:d,sessionName:f,planDecisionVersion:h.planDecisionVersion,valid:!g,afterPlanDecisionLock:!0}),g)return await ne(o,i),await x(o,`⚠️ ${g}`),{handled:!0};l=h}if(l.kind==="plan-approve"){let c=l,h=!1,v=async(I=!1)=>{h||!I&&o.channel!=="telegram"||(await ne(o,i),h=!0)},b=await Ps(S,a,async()=>(await v(),Q(k,{session:c.sessionId,message:"Approved. Go ahead.",approve:!0,userInitiated:!0})));if(b.isError){let I=k.resolve?.(c.sessionId)??k.getPersistedSession?.(c.sessionId),R=Vu(I);if(R){let m=k.consumeActionToken(a);M("callback_token_consume_completed",{channel:o.channel,namespace:W,tokenHash:K(a),consumed:!!m,actionKind:m?.kind,sessionId:m?.sessionId??c.sessionId,planDecisionVersion:m?.planDecisionVersion,approvalAppliedAfterError:!0}),await v(!0)}return R?await x(o,`⚠️ ${b.text}`):await Ju(o,`⚠️ ${b.text}`,k,c),{handled:!0}}let A=k.consumeActionToken(a);return M("callback_token_consume_completed",{channel:o.channel,namespace:W,tokenHash:K(a),consumed:!!A,actionKind:A?.kind,sessionId:A?.sessionId??c.sessionId,planDecisionVersion:A?.planDecisionVersion}),A?(await v(!0),{handled:!0}):(await qe(o,()=>v(!0)),{handled:!0})}if(l.kind==="plan-reject"||l.kind==="plan-request-changes")return await Ps(S,a,async()=>{let c=k.getActionToken(a);if(!c)return await qe(o,()=>ne(o,i)),{handled:!0};d=c.sessionId,u=k.resolve?.(d)??k.getPersistedSession?.(d),f=u?.name??d;let h=uo(c,u);if(M("callback_plan_validation_completed",{channel:o.channel,namespace:W,tokenHash:K(a),actionKind:c.kind,sessionId:d,sessionName:f,planDecisionVersion:c.planDecisionVersion,valid:!h,afterPlanDecisionLock:!0}),h)return await ne(o,i),await x(o,`⚠️ ${h}`),{handled:!0};let v=k.consumeActionToken(a);if(M("callback_token_consume_completed",{channel:o.channel,namespace:W,tokenHash:K(a),consumed:!!v,actionKind:v?.kind,sessionId:v?.sessionId,planDecisionVersion:v?.planDecisionVersion}),!v)return await qe(o,()=>ne(o,i)),{handled:!0};if(await ne(o,i),v.kind==="plan-reject"){let b=Yr(k,d);await x(o,`❌ ${b.text}`)}else{let b=Jr(k,d);await x(o,`✏️ ${b.text}`)}return{handled:!0}});let y=k.consumeActionToken(a);if(M("callback_token_consume_completed",{channel:o.channel,namespace:W,tokenHash:K(a),consumed:!!y,actionKind:y?.kind,sessionId:y?.sessionId,planDecisionVersion:y?.planDecisionVersion}),!y)return await qe(o,()=>N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:l.kind==="plan-offer-start"||l.kind==="plan-offer-dismiss"})),{handled:!0};switch(y.kind){case"plugin-update-install":{if(await _n(o,i),M("callback_update_action_started",{channel:o.channel,tokenHash:K(a),actionKind:y.kind,approvedVersion:y.pluginUpdateVersion}),!de){M("callback_update_action_failed",{channel:o.channel,tokenHash:K(a),reason:"service_unavailable"}),await x(o,"⚠️ OpenClaw Code Agent update service is not running.");break}let c;try{c=await de.installConfirmed(y.pluginUpdateVersion,{route:y.route})}catch(h){M("callback_update_action_failed",{channel:o.channel,tokenHash:K(a),reason:"install_failed"}),await x(o,`⚠️ OpenClaw Code Agent update failed: ${h instanceof Error?h.message:String(h)}`);break}M("callback_update_action_completed",{channel:o.channel,tokenHash:K(a),approvedVersion:y.pluginUpdateVersion});try{await x(o,`✅ ${c}`)}catch(h){M("callback_update_confirmation_failed",{channel:o.channel,tokenHash:K(a),reason:"reply_failed"}),`${h instanceof Error?h.message:String(h)}`}break}case"plugin-update-restart":{if(await _n(o,i),!de){await x(o,"⚠️ OpenClaw Code Agent update service is not running.");break}try{let c=await de.restartConfirmed(y.pluginUpdateVersion);await x(o,`▶️ ${c}`)}catch(c){await x(o,`⚠️ Gateway restart failed: ${c instanceof Error?c.message:String(c)}`)}break}case"plugin-update-dismiss":{await _n(o,i);let c=de?de.dismiss(y.pluginUpdateVersion):"Dismissed OpenClaw Code Agent update reminder.";await x(o,`✅ ${c}`);break}case"plugin-update-remind-later":{await _n(o,i);let c=de?de.remindLater(y.pluginUpdateVersion):"Will remind later about OpenClaw Code Agent update reminder.";await x(o,`✅ ${c}`);break}case"worktree-merge":{let c=await t().execute("callback",{session:d}),h=Dn(c);if(ws(c)){await Cn(o,i);break}await x(o,h);break}case"worktree-decide-later":{let c=k.snoozeWorktreeDecision(d,{notifyUser:!1});if(po(c)){let v=`⏭️ Snoozed 24h for [${f}]`;await Cn(o,i),await x(o,v)}else await x(o,c);break}case"worktree-dismiss":{let c=await k.dismissWorktree(d),h=po(c);h&&await Cn(o,i),await x(o,h?"✅ Discarded":c);break}case"worktree-create-pr":case"worktree-update-pr":{let c=await r().execute("callback",{session:d}),h=Dn(c);if(ws(c)){await Cn(o,i);break}await x(o,h);break}case"worktree-view-pr":{await N(o,{alreadyAcknowledged:i});let c=k.getPersistedSession?.(d),h=l.targetUrl??c?.worktreePrUrl;await x(o,h?`PR: ${h}`:"⚠️ PR URL is no longer available.");break}case"plan-offer-start":{if(!y.launchPrompt||!y.launchWorkdir){await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,"⚠️ This action is missing the plan launch context.");break}let c;try{c=k.launchPlanOffer({route:y.route,prompt:y.launchPrompt,workdir:y.launchWorkdir,name:y.launchName,worktreeStrategy:y.launchWorktreeStrategy})}catch(h){let v=h instanceof Error?h.message:String(h);await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,`⚠️ Failed to start planning session: ${v}`);break}await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,`▶️ Planning session started: ${c.name} [${c.id}]`);break}case"plan-offer-dismiss":{await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,"✅ Dismissed.");break}case"repo-policy-set":{if(!y.repoPolicy||!y.repoPolicyWorkdir){await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,"⚠️ This action is missing the repo policy context.");break}if(!y.launchPrompt||!y.launchWorkdir){await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,"⚠️ This action is missing the launch context.");break}if(typeof k.resolveRepoPolicy=="function"){let v=k.resolveRepoPolicy(y.repoPolicyWorkdir);if(v.identity){let b=Ke(y.repoPolicy,v.prAvailable);if(b){await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,`⚠️ ${b}`);break}}}let c=k.setRepoPolicy(y.repoPolicyWorkdir,y.repoPolicy);if(!c){await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,`⚠️ Could not resolve a git repository for ${y.repoPolicyWorkdir}.`);break}k.clearRepoPolicyChoiceTokens(y.sessionId);let h;try{h=k.launchAfterRepoPolicyChoice({route:y.route,prompt:y.launchPrompt,workdir:y.launchWorkdir,name:y.launchName,model:y.launchModel,reasoningEffort:y.launchReasoningEffort,fastMode:y.launchFastMode,systemPrompt:y.launchSystemPrompt,allowedTools:y.launchAllowedTools,resumeSessionId:y.launchResumeSessionId,resumedFromSessionName:y.launchResumedFromSessionName,resumeWorktreeFrom:y.launchResumeWorktreeFrom,sessionIdOverride:y.launchSessionIdOverride,clearedPersistedCodexResume:y.launchClearedPersistedCodexResume,forkSession:y.launchForkSession,forceNewSession:y.launchForceNewSession,permissionMode:y.launchPermissionMode,planApproval:y.launchPlanApproval,harness:y.launchHarness,worktreeStrategy:y.launchWorktreeStrategy,worktreeBaseBranch:y.launchWorktreeBaseBranch,worktreePrTargetRepo:y.launchWorktreePrTargetRepo,originAgentId:y.launchOriginAgentId}).text}catch(v){let b=v instanceof Error?v.message:String(v);await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,`⚠️ Repo policy saved, but launch failed: ${b}`);break}await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,[`✅ Repo policy saved: ${ls(c.policy).title}.`,"",h].join(`
|
|
86
|
-
`));break}case"session-restart":case"session-resume":{await N(o,{alreadyAcknowledged:i});let c=await Q(k,{session:d,message:"Continue where you left off.",userInitiated:!0});await x(o,c.isError?`⚠️ ${c.text}`:`▶️ ${c.text}`);break}case"view-output":{await N(o,{alreadyAcknowledged:i});let c=await
|
|
87
|
-
|
|
88
|
-
Use /agent_sessions to see active sessions.`;return{text:`Error launching session: ${s}${a}`}}}})}function
|
|
89
|
-
/agent_respond --interrupt <id-or-name> <message>`};let r=!1,o=t,i=Re(o);i?.value==="--interrupt"&&(r=!0,o=i.rest);let s=Re(o);if(!s)return{text:"Error: Missing message. Usage: /agent_respond <id-or-name> <message>"};let a=s.value,l=s.rest;return l.trim()?{text:(await Q(k,{session:a,message:l,interrupt:r,userInitiated:!0})).text}:{text:"Error: Empty message. Usage: /agent_respond <id-or-name> <message>"}}})}function
|
|
64
|
+
`)}]}}let o=r.workdir??n?.workspaceDir;if(!o)return{content:[{type:"text",text:"Error: workdir is required when no workspace directory is available."}]};if(r.reset===!0)return{content:[{type:"text",text:k.resetRepoPolicy(o)?`Repo policy reset for ${o}.`:`No stored repo policy found for ${o}.`}]};if(r.policy!==void 0){if(!Cu(r.policy))return{content:[{type:"text",text:"Error: policy must be one of pr-required, pr-allowed, never-pr, manual."}]};if(typeof k.resolveRepoPolicy=="function"){let d=k.resolveRepoPolicy(o);if(d.identity){let u=Ve(r.policy,d.prAvailable);if(u)return{content:[{type:"text",text:`Error: ${u}`}]}}}let a=k.setRepoPolicy(o,r.policy);if(!a)return{content:[{type:"text",text:`Error: ${o} is not a git repository.`}]};let l;try{l=typeof k.continueLaunchAfterManualRepoPolicy=="function"?k.continueLaunchAfterManualRepoPolicy(a.repoRoot,r.policy):{kind:"none"}}catch(d){let u=d instanceof Error?d.message:String(d);return{content:[{type:"text",text:[mt(a),"",`Repo policy saved, but the deferred launch failed: ${u}`,"The pending launch context was kept so you can retry the same agent_repo_policy call or run the intended launch again."].join(`
|
|
65
|
+
`)}]}}return l.kind==="launched"?{content:[{type:"text",text:[mt(a),"",l.text].join(`
|
|
66
|
+
`)}]}:l.kind==="ambiguous"?{content:[{type:"text",text:[mt(a),"",`Repo policy saved, but ${l.count} pending launches match this policy. Run the intended launch again to avoid starting the wrong session.`].join(`
|
|
67
|
+
`)}]}:{content:[{type:"text",text:mt(a)}]}}let i=k.resolveRepoPolicy(o);if(!i.identity)return{content:[{type:"text",text:`No git repository found for ${o}.`}]};let s=i.record;return s?{content:[{type:"text",text:mt(s)}]}:{content:[{type:"text",text:["Repo policy: unknown",`Repo: ${i.identity.repoRoot}`,`Provider: ${i.provider}${i.prAvailable?"":" (PR automation unavailable)"}`,...i.identity.remoteUrl?[`Remote: ${i.identity.remoteUrl}`]:[]].join(`
|
|
68
|
+
`)}]}}}}import{existsSync as In}from"fs";function Mu(n){return!n||typeof n!="object"?!1:typeof n.session=="string"}function _u(n){return n.mergeResult.stashPopConflict?[`Pre-merge stash pop conflicted; run git stash show ${n.mergeResult.stashRef??"stash@{0}"} in ${n.repoDir} to review stashed changes.`]:n.mergeResult.stashed?[`Pre-existing changes on ${n.baseBranch} were auto-stashed and restored.`]:[]}function Du(n){return n.deleteBranchRequested?n.branchDeleted&&n.worktreeCleanedUp&&!n.worktreeAlreadyAbsent?{detailLine:"Branch and worktree cleaned up.",summaryFragment:" Branch and worktree cleaned up."}:n.branchDeleted&&n.worktreeAlreadyAbsent?{detailLine:"Branch deleted; worktree was already absent.",summaryFragment:" Branch deleted; worktree was already absent."}:n.branchDeleted?{detailLine:"Branch deleted; worktree cleanup failed.",summaryFragment:" Branch deleted; worktree cleanup failed."}:n.worktreeAlreadyAbsent?{detailLine:"Worktree was already absent; branch deletion failed.",summaryFragment:" Worktree was already absent; branch deletion failed."}:n.worktreeCleanedUp?{detailLine:"Worktree cleaned up; branch deletion failed.",summaryFragment:" Worktree cleaned up; branch deletion failed."}:{detailLine:"Branch deletion and worktree cleanup failed.",summaryFragment:" Branch deletion and worktree cleanup failed."}:n.worktreeCleanedUp&&!n.worktreeAlreadyAbsent?{detailLine:"Worktree cleaned up; branch deletion was not requested.",summaryFragment:" Worktree cleaned up; branch kept."}:n.worktreeAlreadyAbsent?{detailLine:"Worktree was already absent; branch deletion was not requested.",summaryFragment:" Worktree was already absent; branch kept."}:{detailLine:"Worktree cleanup failed; branch deletion was not requested.",summaryFragment:" Worktree cleanup failed; branch kept."}}function Tn(n){return{name:"agent_merge",description:"Merge a worktree branch back to the base branch. Resolves session (active or persisted), gets worktree path, and performs the merge. On conflict, spawns a conflict-resolver session using the configured default harness.",parameters:p.Object({session:p.String({description:"Session name or ID to merge"}),base_branch:p.Optional(p.String({description:"Literal Git branch name to merge into; options and revision expressions are rejected (default: main)"})),strategy:p.Optional(p.Union([p.Literal("merge"),p.Literal("squash")],{description:"Merge strategy: 'merge' (default, fast-forward if possible; merge commit if branches have diverged) or 'squash' (squashes all commits into one)"})),push:p.Optional(p.Boolean({description:"Push the base branch after successful merge (default: false)"})),delete_branch:p.Optional(p.Boolean({description:"Delete the worktree branch after successful merge (default: true)"}))}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};if(!Mu(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected { session, base_branch?, strategy?, push?, delete_branch? }."}]};if(t.base_branch!==void 0){let b=Ee(t.base_branch);if(b)return{content:[{type:"text",text:`Error: ${b}`}]}}let r=Vt(k,t.session),o=r.activeSession,i=r.persistedSession;if(!o&&!i)return{content:[{type:"text",text:`Error: Session "${t.session}" not found.`}]};let{worktreePath:s,originalWorkdir:a,branchName:l}=r;if(!s||!a)return{content:[{type:"text",text:`Error: Session "${t.session}" does not have a worktree.`}]};if(!l)return{content:[{type:"text",text:`Error: Cannot determine branch name for worktree at ${s}. The worktree may have been removed and no persisted branch name is available.`}]};In(s)||`${s}${a}`;let d=a;if(!In(a))return{content:[{type:"text",text:`Error: originalWorkdir "${a}" does not exist.`}]};let u=t.base_branch??Ne(d),m=u,g=t.strategy??"merge",S=t.push===!0,y=t.delete_branch!==!1,c=k.resolveRepoPolicy(d);if(c?.policy==="pr-required")return{content:[{type:"text",text:`❌ Merge blocked: repo policy requires a pull request for ${c.identity?.repoRoot??d}. Use agent_pr if PR automation is available.`}]};if(i?.worktreeLifecycle?.state==="merged"||i?.worktreeMerged)return{content:[{type:"text",text:`ℹ️ Session "${t.session}" is already merged.`}]};if((In(s)?tt(d,l,m):void 0)===0&&rt(s))return{content:[{type:"text",text:[`❌ Merge blocked: session "${t.session}" has uncommitted worktree changes but branch ${l} has no commits ahead of ${m}.`,`Worktree: ${s}`,"Resume or inspect the session, then commit real task changes or clean temporary files before retrying agent_merge."].join(`
|
|
69
|
+
`)}]};let P={content:[{type:"text",text:"❌ Merge did not run (internal error)"}]};return await k.enqueueMerge(d,async()=>{let b=k.getPersistedSession(t.session);if(b?.worktreeLifecycle?.state==="merged"||b?.worktreeMerged){P={content:[{type:"text",text:`ℹ️ Session "${t.session}" was already merged while waiting in queue.`}]};return}let A=Oe(d,l,u),I=Ft(d,l,m,g,s),R=ot(I);if(I.success){let f=_u({mergeResult:I,repoDir:d,baseBranch:m});if(S&&!di(d,m)){let Ie=`⚠️ Merged ${l} → ${m} locally, but failed to push ${m}`;k.notifyWorktreeOutcome(r.notificationTarget,Ie,{detailLines:[`Branch ${l} was merged into ${m} in the local repository.`,`Pushing ${m} failed; remote state may not include the merge.`,"Cleanup was skipped so the branch/worktree remains available for follow-up.",...f,...R]}),P={content:[{type:"text",text:ie(Ie,I)}]};return}let v=!1,T=!1,F=!In(s);T=F?!0:B(d,s),Wt(d),y&&(v=H(d,l));let V=Du({deleteBranchRequested:y,branchDeleted:v,worktreeCleanedUp:T,worktreeAlreadyAbsent:F});if(b){let Ie=new Date().toISOString();for(let kl of ci({...r,persistedSession:b}))k.updatePersistedSession(kl,{...qt({worktreeBaseBranch:u,worktreePrTargetRepo:b.worktreePrTargetRepo,worktreePushRemote:b.worktreePushRemote},{mergedAt:Ie,updatedAt:Ie,resolvedAt:Ie,resolutionSource:"agent_merge"}),worktreeDisposition:"merged"})}let Ho=Bt({kind:"merge",branch:l,base:u,filesChanged:A?.filesChanged,insertions:A?.insertions,deletions:A?.deletions});k.notifyWorktreeOutcome(r.notificationTarget,Ho,{detailLines:[I.fastForward?"Merge type: fast-forward.":"Merge type: merge commit.",S?`Pushed ${m}.`:`Did not push ${m}; push was not requested.`,V.detailLine,...f,...R]});let Xe=`✅ ${I.fastForward?"⚡ Fast-forward":"🔀 Merge commit"}: ${l} → ${m}.${S?" Pushed.":""}${V.summaryFragment}`;I.stashPopConflict?Xe+=`
|
|
70
|
+
⚠️ Pre-merge stash pop conflicted — run \`git stash show ${I.stashRef??"stash@{0}"}\` in ${d} to review stashed changes.`:I.stashed&&(Xe+=`
|
|
71
|
+
(Pre-existing changes on ${m} were auto-stashed and restored.)`),Xe=ie(Xe,I),P={content:[{type:"text",text:Xe}]}}else if(I.rebaseConflict)P={content:[{type:"text",text:ie(`⚠️ Rebase conflicts — manual resolution required:
|
|
72
|
+
|
|
73
|
+
${I.error}`,I)}]};else if(I.conflictFiles&&I.conflictFiles.length>0){let f=["Resolve merge conflicts in the following files and commit the resolution:","",...I.conflictFiles.map(v=>`- ${v}`),"",`After resolving, commit with message: "Resolve merge conflicts from ${l}"`].join(`
|
|
74
|
+
`);try{let v=k.spawn({prompt:f,workdir:d,name:`${t.session}-conflict-resolver`,harness:Y(),permissionMode:"bypassPermissions",multiTurn:!0,route:o?.route??i?.route,originChannel:o?.originChannel??i?.originChannel,originThreadId:o?.originThreadId??i?.originThreadId,originAgentId:o?.originAgentId??i?.originAgentId,originSessionKey:o?.originSessionKey??i?.originSessionKey});P={content:[{type:"text",text:ie(`⚠️ Merge conflicts in ${I.conflictFiles.length} file(s) — spawned conflict resolver session: ${v.name}`,I)}]}}catch(v){P={content:[{type:"text",text:ie(`❌ Merge conflicts detected, but failed to spawn resolver: ${v instanceof Error?v.message:String(v)}`,I)}]}}}else{let f=I.dirtyError?`❌ Merge blocked: ${I.error}`:`❌ Merge failed: ${I.error??"unknown error"}`;P={content:[{type:"text",text:ie(f,I)}]}}}),P}}}function $u(n){return!!n&&typeof n=="object"}function Ps(n){return{name:"agent_worktree_cleanup",description:"Manage worktree cleanup with lifecycle-aware safety rules. Use preview_safe to list what Clean all safe would remove, clean_safe to execute it, or preview_all to see both safe and retained sandboxes with kept reasons.",parameters:p.Object({workdir:p.Optional(p.String({description:"Restrict cleanup to sessions rooted in this repository"})),base_branch:p.Optional(p.String({description:"Override literal Git branch name for lifecycle resolution; options and revision expressions are rejected"})),mode:p.Optional(p.Union([p.Literal("preview_safe"),p.Literal("clean_safe"),p.Literal("preview_all")],{description:"Cleanup mode. preview_safe shows what Clean all safe would remove, clean_safe performs that cleanup, preview_all shows both safe and retained worktrees. Defaults to preview_safe when dry_run=true, otherwise clean_safe."})),skip_session_check:p.Optional(p.Boolean({description:"Deprecated. Safe cleanup never removes live sessions."})),force:p.Optional(p.Boolean({description:"Deprecated alias for skip_session_check."})),dry_run:p.Optional(p.Boolean({description:"Show what would be cleaned without deleting anything."})),session:p.Optional(p.String({description:"Session name or ID to clean or dismiss."})),dismiss_session:p.Optional(p.Boolean({description:"When true and session is provided, permanently dismiss the worktree."}))}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};if(!$u(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected { workdir?, base_branch?, mode?, dry_run?, session?, dismiss_session? }."}]};if(t.base_branch!==void 0){let S=Ee(t.base_branch);if(S)return{content:[{type:"text",text:`Error: ${S}`}]}}let r=t.session,o=t.mode??(t.dry_run===!0?"preview_safe":"clean_safe"),i=o!=="clean_safe",s=o==="preview_all"||o==="clean_safe";if(r&&t.dismiss_session===!0)return{content:[{type:"text",text:await k.dismissWorktree(r)}]};let a=Gt(k);if(r&&(a=a.filter(S=>jt(S,r)),a.length===0)){let S=Vt(k,r);if(!S.persistedSession&&!S.activeSession)return{content:[{type:"text",text:`Error: Session "${r}" not found.`}]}}if(t.workdir&&(a=a.filter(S=>S.workdir===t.workdir)),a.length===0)return{content:[{type:"text",text:"No managed worktrees matched the requested scope."}]};let l=[],d=[],u=[],m=[];for(let S of a){let{persistedSession:y,activeSession:c,resolvedLifecycle:h}=Ut(k,S,{baseBranch:t.base_branch});if(!h.cleanupSafe){if(s){let P=h.reasons.length>0?h.reasons.map(zt).join(", "):Se(h.derivedState);d.push(`${S.name} [kept: ${P}]`)}continue}if(l.push(`${S.name} (${Se(h.derivedState)})`),!i)try{let P=S.workdir;if(!((y??c)&&L(y??c))&&S.worktreePath&&B(P,S.worktreePath,{destructive:!1}),S.worktreeBranch&&H(P,S.worktreeBranch),y){let A=h.derivedState==="merged"||h.derivedState==="released"?h.derivedState:h.lifecycle.state,I=new Date().toISOString(),R=A==="merged"?y.worktreeMergedAt:A==="dismissed"?y.worktreeDismissedAt:void 0;k.updatePersistedSession(S.id,{worktreePath:void 0,worktreeBranch:void 0,lifecycle:"terminal",worktreeState:"none",pendingWorktreeDecisionSince:void 0,lastWorktreeReminderAt:void 0,worktreeDecisionSnoozedUntil:void 0,worktreeMerged:A==="merged"?!0:y.worktreeMerged,worktreeMergedAt:A==="merged"?y.worktreeMergedAt??I:y.worktreeMergedAt,worktreeLifecycle:{...y.worktreeLifecycle??h.lifecycle,state:A,updatedAt:I,resolvedAt:y.worktreeLifecycle?.resolvedAt??R??I,resolutionSource:y.worktreeLifecycle?.resolutionSource??"maintenance",baseBranch:t.base_branch??h.lifecycle.baseBranch??y.worktreeBaseBranch,targetRepo:y.worktreePrTargetRepo,pushRemote:y.worktreePushRemote,notes:h.reasons}})}u.push(`${S.name} (${Se(h.derivedState)})`)}catch(P){m.push(`${S.name}: ${P instanceof Error?P.message:String(P)}`)}}let g=[o==="clean_safe"?"Clean all safe:":o==="preview_all"?"Worktree lifecycle review:":"Clean all safe preview:"];return g.push(` SAFE ${i?"NOW":"FOUND"} (${l.length}): ${l.join(", ")||"(none)"}`),s&&g.push(` KEPT (${d.length}): ${d.join(", ")||"(none)"}`),i||(g.push(` CLEANED (${u.length}): ${u.join(", ")||"(none)"}`),g.push(` FAILURES (${m.length}): ${m.join(", ")||"(none)"}`)),{content:[{type:"text",text:g.join(`
|
|
75
|
+
`)}]}}}}function Eu(n){if(n==null)return!0;if(Array.isArray(n)||typeof n!="object")return!1;let e=n.session;return e===void 0||typeof e=="string"}function ws(n){return{name:"agent_worktree_status",description:"Show lifecycle-first worktree status for coding agent sessions. Displays product-facing lifecycle state, released handling, cleanup safety, and retained reasons.",parameters:p.Object({session:p.Optional(p.String({description:"Session name or ID to show status for (optional, shows all if omitted)"}))}),async execute(e,t){if(!k)return{content:[{type:"text",text:"Error: SessionManager not initialized. The code-agent service must be running."}]};if(!Eu(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected { session? }."}]};let r=t&&typeof t=="object"?t.session:void 0,o=Gt(k);if(r&&(o=o.filter(s=>jt(s,r)),o.length===0))return{content:[{type:"text",text:`Error: Session "${r}" not found or has no worktree.`}]};if(o.length===0)return{content:[{type:"text",text:"No sessions with worktrees found."}]};let i=[];for(let s of o){let{persistedSession:a,resolvedLifecycle:l}=Ut(k,s),d=l.cleanupSafe?"safe now":l.preserve?"preserve":"blocked";i.push(`Session: ${s.name} [${s.id}]`),i.push(` Branch: ${s.worktreeBranch??"(unknown)"} → ${l.lifecycle.baseBranch??a?.worktreeBaseBranch??"main"}`),i.push(` Repo: ${s.workdir}`),i.push(` Lifecycle:${Se(l.lifecycle.state)}`),l.derivedState!==l.lifecycle.state&&i.push(` Derived: ${Se(l.derivedState)}`),i.push(` Cleanup: ${d}`),l.evidence.prUrl&&i.push(` PR: ${l.evidence.prUrl} (${l.evidence.prState??"unknown"})`),(l.evidence.branchAheadCount!=null||l.evidence.baseAheadCount!=null)&&i.push(` Ahead: ${l.evidence.branchAheadCount??0} ahead / ${l.evidence.baseAheadCount??0} behind`),i.push(` Reasons: ${l.reasons.length>0?l.reasons.map(zt).join(", "):"none"}`),i.push("")}return{content:[{type:"text",text:i.join(`
|
|
76
|
+
`).trim()}]}}}}import{existsSync as Nu}from"fs";function Ou(n=[]){return n.map((e,t)=>({label:`check-${t+1}`,command:e.trim()})).filter(e=>e.command.length>0)}function xn(n,e){let t=n.goal.trim();if(!t)return{kind:"error",text:"Error: goal must not be empty."};let r=n.workdir||e.workspaceDir||_.defaultWorkdir||process.cwd();if(!Nu(r))return{kind:"error",text:`Error: Working directory does not exist: ${r}`};let o=Ou(n.verifierCommands),i=n.goalMode??(o.length>0?"verifier":"ralph");if(i==="verifier"&&o.length===0)return{kind:"error",text:"Error: verifier mode requires at least one non-empty verifier command."};let s=n.harness??Y(),a=n.model??Me(s),l=Ct(s,a),d=It(s);if(!l&&(s!=="opencode"||d&&d.length>0))return{kind:"error",text:`Error: No default model configured for harness "${s}". Set plugins.entries["openclaw-code-agent"].config.harnesses.${s}.defaultModel or pass model explicitly.`};if(l&&!_t(s,l))return{kind:"error",text:`Error: Model "${a}" is not supported for harness "${s}". Use a bare Codex model id such as "gpt-6-astra" or "gpt-5.6-sol".`};if(l&&!Mt(s,l,d))return{kind:"error",text:`Error: Model "${a}" is not allowed. Permitted models: ${d?.join(", ")}`};let u=Dt(s,l,d),m=e.sessionKey||void 0,g=Tt(e),S=xt(e,g||$e(r)),y=De(e,S,m),c=rn({ctx:e,route:y,operation:"goal task"});if(c.kind==="error")return c;let h=c.route;return{kind:"resolved",goal:t,name:n.name,workdir:r,model:u,reasoningEffort:ce(s),fastMode:_e(s),systemPrompt:n.systemPrompt,allowedTools:n.allowedTools,maxIterations:n.maxIterations,permissionMode:n.permissionMode??"bypassPermissions",harness:s,loopMode:i,completionPromise:n.completionPromise,originChannel:S,originThreadId:y?.threadId,originAgentId:e.agentId||void 0,originSessionKey:m,route:h,verifierCommands:o}}function Cn(n,e){return["Goal task launched.",` Name: ${n.name}`,` ID: ${n.id}`,` Dir: ${n.workdir}`,` Session: ${n.sessionName} [${n.sessionId}]`,` Harness: ${e.harness}`,` Model: ${e.model??"default"}`,...e.fastMode?[" Fast mode: enabled"]:[],` Loop mode: ${n.loopMode}`,` Max controller iterations: ${n.maxIterations}`,` Goal: "${e.goal.length>100?`${e.goal.slice(0,100)}...`:e.goal}"`,...n.loopMode==="ralph"?[` Completion promise: ${n.completionPromise}`]:[" Verifiers:",...e.verifierCommands.map(r=>` - ${r.command}`)],"","Controller iteration progress advances only when the goal controller starts another agent turn; internal agent review passes are reported in the completion summary.","","Use /agent_goal_status to follow progress or /agent_goal_stop to terminate the task in chat."].join(`
|
|
77
|
+
`)}function Wu(n){return!n||typeof n!="object"?!1:typeof n.goal=="string"}function bs(n){return{name:"agent_goal_launch",description:"Launch an explicit goal loop. Use this only when the user specifically asks for a goal task, Ralph-style autonomous loop, iterative loop, autonomous repair loop, or keep-going-until-checks-pass workflow. This is not the default session path.",parameters:p.Object({goal:p.String({description:"End goal for the autonomous task"}),verifier_commands:p.Optional(p.Array(p.String({description:"Shell command that must pass for the goal to be considered complete"}),{minItems:1,description:"Verifier commands run after each coding turn"})),name:p.Optional(p.String({description:"Short task name (kebab-case preferred)"})),workdir:p.Optional(p.String({description:"Working directory (defaults to cwd / configured workspace)"})),model:p.Optional(p.String({description:"Model name to use"})),system_prompt:p.Optional(p.String({description:"Additional system prompt"})),allowed_tools:p.Optional(p.Array(p.String(),{description:"Allowed tools for the underlying agent session"})),max_iterations:p.Optional(p.Number({description:"Maximum verifier-driven repair iterations",minimum:1})),goal_mode:p.Optional(p.Union([p.Literal("ralph"),p.Literal("verifier")],{description:"Loop strategy. 'ralph' repeats until a completion promise appears. 'verifier' repeats until verifier commands pass. Defaults to 'ralph' when no verifiers are provided, otherwise 'verifier'."})),completion_promise:p.Optional(p.String({description:"Exact completion promise for Ralph-mode loops. Defaults to 'DONE'."})),permission_mode:p.Optional(p.Union([p.Literal("default"),p.Literal("plan"),p.Literal("bypassPermissions")],{description:"Permission mode for the underlying agent session. Defaults to bypassPermissions for autonomous goal loops."})),harness:p.Optional(p.String({description:"Agent harness to use ('claude-code', 'codex', or experimental 'opencode')."}))}),async execute(e,t){if(!$)return{content:[{type:"text",text:"Error: GoalController not initialized. The code-agent service must be running."}]};if(!Wu(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected at least { goal }."}]};let r=xn({goal:t.goal,verifierCommands:t.verifier_commands,name:t.name,workdir:t.workdir,model:t.model,systemPrompt:t.system_prompt,allowedTools:t.allowed_tools,maxIterations:t.max_iterations,permissionMode:t.permission_mode,harness:t.harness,goalMode:t.goal_mode,completionPromise:t.completion_promise},n);if(r.kind!=="resolved")return{content:[{type:"text",text:r.text}]};try{let o=await $.launchTask({goal:r.goal,name:r.name,workdir:r.workdir,model:r.model,reasoningEffort:r.reasoningEffort,fastMode:r.fastMode,systemPrompt:r.systemPrompt,allowedTools:r.allowedTools,maxIterations:r.maxIterations,permissionMode:r.permissionMode,loopMode:r.loopMode,completionPromise:r.completionPromise,originChannel:r.originChannel,originThreadId:r.originThreadId,originAgentId:r.originAgentId,originSessionKey:r.originSessionKey,route:r.route,harness:r.harness,verifierCommands:r.verifierCommands});return{content:[{type:"text",text:Cn(o,r)}]}}catch(o){return{content:[{type:"text",text:`Error launching goal task: ${o instanceof Error?o.message:String(o)}`}]}}}}}function po(n,e){return n.split(`
|
|
78
|
+
`).map(t=>`${e}${t}`)}function Rs(n,e){return n.length<=e?n:`${n.slice(0,e-3).trimEnd()}...`}function Fu(n,e=8,t=600){let r=n.map(i=>i.trimEnd()).filter(i=>i.trim().length>0);if(r.length===0)return;let o=r.slice(-e).join(`
|
|
79
|
+
`);return o.length<=t?o:`...${o.slice(-(t-3))}`}function Lu(n,e){let t=e?.trim(),r=(n??"").split(/\r?\n/).map(o=>o.trim().replace(/^[-*]\s+/,"")).filter(o=>o.length>0).filter(o=>!(t&&o===t)).filter(o=>!/^<promise>.*<\/promise>$/.test(o));return r.length===0?[]:r.slice(-3).map(o=>Rs(o,180))}function Bu(n){let e=(n??"").split(/\r?\n/).map(t=>t.trim()).filter(t=>t.length>0);return e.length===0?[]:e.slice(0,2).map(t=>Rs(t,180))}function Mn(n){let e=[],t=Bu(n.verifierSummary),r=Lu(n.output,n.completionPromise);if(t.length===0&&r.length===0)return;n.completionDetected===!0&&e.push("Completion was claimed, but the loop is continuing after verification.");for(let i of t)e.push(`Verifier: ${i}`);let o=Math.max(0,3-e.length);if(o>0)for(let i of r.slice(-o))e.push(`Agent: ${i}`);return["Iteration summary:",...e.map(i=>`- ${i}`)].join(`
|
|
80
|
+
`)}function mo(n){if(n)return{phase:n.phase,awaitingInput:n.isAwaitingInput??!!n.pendingInputState,latestOutput:Fu(n.getOutput(20))}}function fo(n,e,t){let r=n.loopMode??"verifier",o=[`${n.name} [${n.id}]`,` Status: ${n.status}`,...r==="ralph"?[" Loop mode: ralph",` Iteration: ${n.iteration}/${n.maxIterations}`]:[` Repair iteration: ${n.iteration}/${n.maxIterations}`],` Workdir: ${n.workdir}`];return r==="ralph"&&n.completionPromise&&o.push(` Completion promise: ${n.completionPromise}`),n.sessionId&&o.push(` Session: ${n.sessionName??"(unknown)"} [${n.sessionId}]`),e?.phase&&o.push(` Phase: ${e.phase}`),e?.awaitingInput!==void 0&&o.push(` Awaiting input: ${e.awaitingInput?"yes":"no"}`),t&&o.push(...po(t," ")),n.lastVerifierSummary&&(o.push(" Last verifier:"),o.push(...po(n.lastVerifierSummary," "))),e?.latestOutput&&(o.push(" Latest activity:"),o.push(...po(e.latestOutput," "))),n.waitingForUserReason&&o.push(` Waiting on user: ${n.waitingForUserReason}`),n.failureReason&&o.push(` Failure: ${n.failureReason}`),o.join(`
|
|
81
|
+
`)}var X="Error: GoalController not initialized. The code-agent service must be running.";function _n(n,e,t){let r=t?.trim();if(r){let i=n.getTask(r);if(!i)return`Error: Goal task "${r}" not found.`;let s=i.sessionId?e(i.sessionId):void 0;return fo(i,mo(s))}let o=n.listTasks();return o.length===0?"No goal tasks found.":o.map(i=>{let s=i.sessionId?e(i.sessionId):void 0;return fo(i,mo(s))}).join(`
|
|
82
|
+
|
|
83
|
+
`)}function Dn(n,e){return n?n.action==="already_terminal"?`Task is already ${n.task.status}.`:`Task "${n.task.name}" (${n.task.id}) stopped.`:`Error: Goal task "${e}" not found.`}function $n(n,e){switch(n.action){case"updated":return`Task "${n.task.name}" (${n.task.id}) goal updated.`;case"not_found":return`Error: Goal task "${e}" not found.`;case"invalid_goal":return"Error: replacement goal must not be empty.";case"not_editable":return n.task.status!=="waiting_for_user"?`Task is already ${n.task.status}.`:`Error: Goal task "${n.task.name}" is ${n.task.status} and cannot be edited.`}}function As(n){return{name:"agent_goal_status",description:"Show status for explicit goal tasks managed by the goal controller. Use this to inspect one goal task or list all goal tasks.",parameters:p.Object({task:p.Optional(p.String({description:"Task name or ID. Omit to list all tasks."}))}),async execute(e,t){if(!$)return{content:[{type:"text",text:X}]};let r=t&&typeof t=="object"&&typeof t.task=="string"?t.task:void 0;return{content:[{type:"text",text:_n($,o=>k?.resolve(o),r)}]}}}}function Ku(n){return!!n&&typeof n=="object"&&typeof n.task=="string"}function Is(n){return{name:"agent_goal_stop",description:"Stop an explicit goal task and kill its underlying agent session.",parameters:p.Object({task:p.String({description:"Goal task name or ID"})}),async execute(e,t){if(!$)return{content:[{type:"text",text:X}]};if(!Ku(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected { task }."}]};let r=$.stopTask(t.task);return{content:[{type:"text",text:Dn(r,t.task)}]}}}}function qu(n){if(!n||typeof n!="object")return!1;let e=n;return typeof e.task=="string"&&typeof e.goal=="string"}function Ts(n){return{name:"agent_goal_edit",description:"Edit the goal text for an active goal task without stopping or relaunching its session.",parameters:p.Object({task:p.String({description:"Goal task name or ID"}),goal:p.String({description:"Replacement goal text"})}),async execute(e,t){if(!$)return{content:[{type:"text",text:X}]};if(!qu(t))return{content:[{type:"text",text:"Error: Invalid parameters. Expected { task, goal }."}]};let r=$.editTask(t.task,t.goal);return{content:[{type:"text",text:$n(r,t.task)}]}}}}var go=new Set,xs="⚠️ Could not submit that answer. The question prompt is still active; try again or reply with the answer.",ft=new Map;function Vu(n){return`${n.sessionId}:${n.pendingInputRequestId??n.id}`}function Uu(n){return!!(n&&n.status!=="running"&&ke(n).kind==="resume")}function Gu(n){return n.label?.trim()?["[SYSTEM: The gateway restarted while a user question was pending. Treat the user's selection below as the answer to that interrupted question and continue without asking it again.]","",n.pendingInputQuestionId?`Question ID: ${n.pendingInputQuestionId}`:"Question: the interrupted pending question",`Selected answer: ${n.label.trim()}`].join(`
|
|
84
|
+
`):void 0}async function Ds(n){try{await n}catch(e){let t=e instanceof Error?e.message:String(e);`${t}`}}async function Cs(n,e,t){if(!n)return t();for(;;){let o=ft.get(n)?.operation;if(!o)break;await Ds(o)}let r=Promise.resolve().then(t);ft.set(n,{operation:r,tokenId:e});try{return await r}finally{ft.get(n)?.operation===r&&ft.delete(n)}}function ju(n){let e=n.trim().replace(new RegExp(`^${W}:`),"");return e||null}function zu(n){return n.trim().startsWith(`${W}:`)}async function En(n,e=!1){if(n.channel==="telegram")try{return(await N(n,{alreadyAcknowledged:e,forceTelegramMarkupEdit:!0})).textDelivered}catch(r){let o=r instanceof Error?r.message:String(r);return`${o}`,!1}let t=n.respond;if(typeof t.clearComponents=="function")try{return await t.clearComponents(),!1}catch(r){if(be(r))return!1;let o=r instanceof Error?r.message:String(r);`${o}`}if(typeof t.clearButtons=="function")try{await t.clearButtons()}catch(r){let o=r instanceof Error?r.message:String(r);`${o}`}else!e&&typeof t.acknowledge=="function"&&await t.acknowledge();return!1}async function ne(n,e=!1){await N(n,{alreadyAcknowledged:e,forceTelegramMarkupEdit:n.channel==="telegram"})}function Wn(n){if(n&&typeof n=="object"&&"content"in n&&Array.isArray(n.content)){let e=n.content[0];return typeof e?.text=="string"?e.text:"(done)"}return"(done)"}function Ms(n){if(n&&typeof n=="object"&&"meta"in n){let e=n.meta;if(typeof e?.success=="boolean")return e.success}return So(Wn(n))}function So(n){return!/^\s*(?:Error\b:?|❌|⚠️)/.test(n)}function Fn(n){return n==="plan-approve"||n==="plan-request-changes"||n==="plan-reject"}function Hu(n){if(Fn(n.kind))return`${n.sessionId}:v${n.planDecisionVersion??"unknown"}`}function Qu(n){return n?n.approvalState==="approved"||!n.pendingPlanApproval:!1}function ho(n,e){if(!Fn(n.kind))return;if(!e)return"This plan decision is stale because the session is no longer available.";let t=Hr(e);if(n.planDecisionVersion!=null&&t!=null&&n.planDecisionVersion!==t)return"This plan decision is stale because a newer plan review state already exists.";if(!e.pendingPlanApproval)return"This plan is no longer awaiting approval.";if(n.kind==="plan-approve"&&e.approvalState==="changes_requested"&&!e.pendingPlanApproval)return"Changes were already requested for this plan. Wait for the revised plan before approving.";if(n.kind==="plan-request-changes"&&e.approvalState==="changes_requested")return"Changes were already requested for this plan. Send your feedback to the agent instead."}function $s(n,...e){return e.find(t=>typeof t=="string"&&n(t))}function Nn(...n){return $s(e=>e.trim().length>0,...n)}function _s(...n){return $s(zu,...n)}function Yu(n){let e="callback"in n?_s(n.callback?.data,n.callback?.callback_data,n.callback?.callbackData):void 0;if(e)return e;let t="interaction"in n?n.interaction:void 0,r=_s(t?.data,t?.callback_data,t?.callbackData);if(r)return r;let o="callback"in n?Nn(n.callback?.payload):void 0;if(o)return o;let i=Nn(t?.payload);return i||(("callback"in n?Nn(n.callback?.data,n.callback?.callback_data,n.callback?.callbackData):void 0)??Nn(t?.data,t?.callback_data,t?.callbackData)??"")}function gt(n,e=new Set){if(n==null)return"";if(typeof n=="string")return n;if(typeof n=="number"||typeof n=="boolean"||typeof n=="bigint")return String(n);if(n instanceof Error)return[n.name,n.message,gt(n.cause,e)].filter(Boolean).join(" ");if(typeof n!="object")return String(n);if(e.has(n))return"";e.add(n);let t=n;return[t.message,t.description,t.error,t.error_description,gt(t.cause,e),gt(t.response,e),gt(t.payload,e)].filter(Boolean).join(" ")}function be(n){return/message is not modified/i.test(gt(n))}function Ju(n){let e=n instanceof Error?n.message:String(n);return/empty message/i.test(e)}async function we(n){if(typeof n.clearButtons=="function")try{await n.clearButtons()}catch(e){if(be(e))return;throw e}}async function N(n,e={}){let t=n.respond,{alreadyAcknowledged:r=!1,forceTelegramMarkupEdit:o=!1,text:i}=e;if(n.channel==="telegram"){if(typeof i=="string"&&typeof t.editMessage=="function")try{return await t.editMessage({text:i,buttons:[]}),await we(t),{textDelivered:!0}}catch(a){if(be(a))return await we(t),{textDelivered:!0};let l=a instanceof Error?a.message:String(a);`${l}`}if(o&&typeof t.editButtons=="function")try{return await t.editButtons({buttons:[]}),await we(t),{textDelivered:!1}}catch(a){if(be(a))return await we(t),{textDelivered:!1};let l=a instanceof Error?a.message:String(a);`${l}`}let s="callback"in n&&typeof n.callback?.messageText=="string"?n.callback.messageText:void 0;if(o&&typeof s=="string"&&typeof t.editMessage=="function")try{return await t.editMessage({text:s,buttons:[]}),await we(t),{textDelivered:!1}}catch(a){if(be(a))return await we(t),{textDelivered:!1};let l=a instanceof Error?a.message:String(a);`${l}`}return await we(t),{textDelivered:!1}}if(typeof t.clearComponents=="function")try{return await t.clearComponents(typeof i=="string"?{text:i}:void 0),{textDelivered:typeof i=="string"}}catch(s){if(be(s))return{textDelivered:typeof i=="string"};if(Ju(s)){if(typeof i!="string"&&r)return{textDelivered:!1};if(typeof i!="string"&&typeof t.acknowledge=="function")return await t.acknowledge(),{textDelivered:!1};typeof i!="string"&&t.acknowledge}else{if(typeof i!="string")throw s;{let a=s instanceof Error?s.message:String(s);`${a}`}}}if(typeof i=="string"&&typeof t.editMessage=="function"){try{await t.editMessage({text:i})}catch(s){if(be(s))return typeof t.clearButtons=="function"&&await t.clearButtons(),{textDelivered:!0};let a=s instanceof Error?s.message:String(s);return`${a}`,typeof t.clearButtons=="function"&&await t.clearButtons(),{textDelivered:!1}}return typeof t.clearButtons=="function"&&await t.clearButtons(),{textDelivered:!0}}return typeof t.clearButtons=="function"&&await t.clearButtons(),{textDelivered:!1}}async function Xu(n){let e=n.respond;if(typeof e.acknowledge!="function")return!1;try{return await e.acknowledge(),!0}catch(t){let r=t instanceof Error?t.message:String(t);return`${r}`,!1}}async function x(n,e){if(n.channel==="telegram"){await n.respond.reply({text:e});return}await n.respond.reply({text:e,ephemeral:!0})}var Zu="⚠️ This action is stale or has already been used.";async function Ue(n,e){try{await e()}catch(t){let r=t instanceof Error?t.message:String(t);`${r}`}try{await x(n,Zu)}catch(t){let r=t instanceof Error?t.message:String(t);`${r}`}}async function On(n,e){try{await N(n,{alreadyAcknowledged:e,forceTelegramMarkupEdit:!0})}catch(t){let r=t instanceof Error?t.message:String(t);`${r}`}}var yo=["plan-approve","plan-request-changes","plan-reject"];function ec(n){switch(n){case"plan-approve":return"Approve";case"plan-request-changes":return"Revise";case"plan-reject":return"Reject";default:return"Continue"}}function tc(n){switch(n){case"plan-approve":return"primary";case"plan-reject":return"danger";default:return"secondary"}}function nc(n,e){let t=n,o=(typeof t?.listActiveActionTokens=="function"?yo.flatMap(a=>t.listActiveActionTokens?.(a)??[]):[e]).filter(a=>Fn(a.kind)&&a.sessionId===e.sessionId&&(e.planDecisionVersion==null||a.planDecisionVersion==null||a.planDecisionVersion===e.planDecisionVersion)).sort((a,l)=>yo.indexOf(a.kind)-yo.indexOf(l.kind)),i=new Set,s=o.filter(a=>i.has(a.kind)?!1:(i.add(a.kind),!0)).map(a=>({label:typeof a.label=="string"&&a.label.trim()?a.label:ec(a.kind),callbackData:`${W}:${a.id}`,style:tc(a.kind)}));return s.length>0?[s]:void 0}async function rc(n,e,t,r){if(n.channel!=="telegram"){await x(n,e);return}let o=nc(t,r);await n.respond.reply({text:o?`${e}
|
|
85
|
+
|
|
86
|
+
Approval is still pending. Try again below.`:e,...o?{buttons:o}:{}})}function Es(n="telegram",e={}){let t=e.makeAgentMergeTool??Tn,r=e.makeAgentPrTool??Ht;return M("callback_handler_registered",{channel:n,namespace:W}),{channel:n,namespace:W,handler:async o=>{let i=await Xu(o);if(!o.auth.isAuthorizedSender)return await x(o,"⛔ Unauthorized."),{handled:!0};let s=Yu(o),a=ju(s);if(M("callback_received",{channel:o.channel,namespace:W,payloadByteLength:Buffer.byteLength(s,"utf8"),tokenHash:K(a),isAuthorizedSender:o.auth.isAuthorizedSender}),!a)return await x(o,"⚠️ Unrecognized callback payload."),{handled:!0};if(!k)return await x(o,"⚠️ Code agent service not running."),{handled:!0};let l=k.getActionToken(a);if(M("callback_token_lookup_completed",{channel:o.channel,namespace:W,tokenHash:K(a),tokenFound:!!l,actionKind:l?.kind,sessionId:l?.sessionId,planDecisionVersion:l?.planDecisionVersion}),!l)return await Ue(o,()=>N(o,{alreadyAcknowledged:i})),{handled:!0};if(l.kind==="question-answer"&&l.consumedAt!=null)return await N(o,{alreadyAcknowledged:i}),await x(o,"⚠️ That question button is no longer active. Use the latest question prompt."),{handled:!0};let d=l.sessionId,u=k.resolve?.(d)??k.getPersistedSession?.(d),m=u?.name??d;if(u&&Qr(l,u))return k.consumePlanDecisionTokens?.(d,l.planDecisionVersion),await ne(o,i),await x(o,`✅ Plan v${l.planDecisionVersion} was already approved; resume is in progress or running.`),{handled:!0};let g=ho(l,u);if(M("callback_plan_validation_completed",{channel:o.channel,namespace:W,tokenHash:K(a),actionKind:l.kind,sessionId:d,sessionName:m,planDecisionVersion:l.planDecisionVersion,valid:!g}),g)return Fn(l.kind)?await ne(o,i):await N(o,{alreadyAcknowledged:i}),await x(o,`⚠️ ${g}`),{handled:!0};if(l.kind==="question-answer"){if(l.optionIndex==null)return await N(o,{alreadyAcknowledged:i}),await x(o,"⚠️ Invalid question-answer action."),{handled:!0};let c=Vu(l);if(go.has(c))return await x(o,"⚠️ That answer is already being submitted. If the question remains active, try again."),{handled:!0};go.add(c);let h=!1,P=!1;try{if(h=await k.resolvePendingInputOption(d,l.optionIndex,{requestId:l.pendingInputRequestId,questionId:l.pendingInputQuestionId}),!h&&!(k.canSubmitPendingInputOption?.(d)??!1)){let R=k.getPersistedSession?.(d),f=Gu(l);f&&Uu(R)&&(h=!(await Q(k,{session:d,message:f,userInitiated:!0})).isError,P=h)}}catch(R){let f=R instanceof Error?R.message:String(R);return`${f}`,await x(o,xs),{handled:!0}}finally{go.delete(c)}if(!h)return await x(o,xs),{handled:!0};let A=(l.pendingInputRequestId?k.consumeQuestionAnswerTokens(d,l.pendingInputRequestId,l.pendingInputQuestionId):[]).find(R=>R.id===a)??k.consumeActionToken(a);if(M("callback_token_consume_completed",{channel:o.channel,namespace:W,tokenHash:K(a),consumed:!!A,actionKind:A?.kind,sessionId:A?.sessionId,planDecisionVersion:A?.planDecisionVersion}),!A){await N(o,{alreadyAcknowledged:i});let R=!P&&(k.pendingInputSubmissionRequiresMore?.(d)??!1);return await x(o,P?"✅ Answer forwarded to the resumed session.":R?"✅ Question step answered; more input is required.":"✅ Pending input request submitted."),{handled:!0}}await N(o,{alreadyAcknowledged:i});let I=!P&&(k.pendingInputSubmissionRequiresMore?.(d)??!1);return await x(o,P?"✅ Answer forwarded to the resumed session.":I?"✅ Question step answered; more input is required.":"✅ Pending input request submitted."),{handled:!0}}let S=Hu(l);if(S)for(;;){let c=ft.get(S);if(!c)break;if(c.tokenId===a)return await ne(o,i),await x(o,"⚠️ This plan decision is already being processed."),{handled:!0};await Ds(c.operation);let h=k.getActionToken(a);if(!h)return await Ue(o,()=>ne(o,i)),{handled:!0};if(d=h.sessionId,u=k.resolve?.(d)??k.getPersistedSession?.(d),m=u?.name??d,u&&Qr(h,u))return k.consumePlanDecisionTokens?.(d,h.planDecisionVersion),await ne(o,i),await x(o,`✅ Plan v${h.planDecisionVersion} was already approved; resume is in progress or running.`),{handled:!0};if(g=ho(h,u),M("callback_plan_validation_completed",{channel:o.channel,namespace:W,tokenHash:K(a),actionKind:h.kind,sessionId:d,sessionName:m,planDecisionVersion:h.planDecisionVersion,valid:!g,afterPlanDecisionLock:!0}),g)return await ne(o,i),await x(o,`⚠️ ${g}`),{handled:!0};l=h}if(l.kind==="plan-approve"){let c=l,h=!1,P=async(I=!1)=>{h||!I&&o.channel!=="telegram"||(await ne(o,i),h=!0)},b=await Cs(S,a,async()=>(await P(),Q(k,{session:c.sessionId,message:"Approved. Go ahead.",approve:!0,userInitiated:!0})));if(b.isError){let I=k.resolve?.(c.sessionId)??k.getPersistedSession?.(c.sessionId),R=Qu(I);if(R){let f=k.consumeActionToken(a);M("callback_token_consume_completed",{channel:o.channel,namespace:W,tokenHash:K(a),consumed:!!f,actionKind:f?.kind,sessionId:f?.sessionId??c.sessionId,planDecisionVersion:f?.planDecisionVersion,approvalAppliedAfterError:!0}),await P(!0)}return R?await x(o,`⚠️ ${b.text}`):await rc(o,`⚠️ ${b.text}`,k,c),{handled:!0}}let A=k.consumeActionToken(a);return M("callback_token_consume_completed",{channel:o.channel,namespace:W,tokenHash:K(a),consumed:!!A,actionKind:A?.kind,sessionId:A?.sessionId??c.sessionId,planDecisionVersion:A?.planDecisionVersion}),A?(await P(!0),{handled:!0}):(await Ue(o,()=>P(!0)),{handled:!0})}if(l.kind==="plan-reject"||l.kind==="plan-request-changes")return await Cs(S,a,async()=>{let c=k.getActionToken(a);if(!c)return await Ue(o,()=>ne(o,i)),{handled:!0};d=c.sessionId,u=k.resolve?.(d)??k.getPersistedSession?.(d),m=u?.name??d;let h=ho(c,u);if(M("callback_plan_validation_completed",{channel:o.channel,namespace:W,tokenHash:K(a),actionKind:c.kind,sessionId:d,sessionName:m,planDecisionVersion:c.planDecisionVersion,valid:!h,afterPlanDecisionLock:!0}),h)return await ne(o,i),await x(o,`⚠️ ${h}`),{handled:!0};let P=k.consumeActionToken(a);if(M("callback_token_consume_completed",{channel:o.channel,namespace:W,tokenHash:K(a),consumed:!!P,actionKind:P?.kind,sessionId:P?.sessionId,planDecisionVersion:P?.planDecisionVersion}),!P)return await Ue(o,()=>ne(o,i)),{handled:!0};if(await ne(o,i),P.kind==="plan-reject"){let b=ro(k,d);await x(o,`❌ ${b.text}`)}else{let b=oo(k,d);await x(o,`✏️ ${b.text}`)}return{handled:!0}});let y=k.consumeActionToken(a);if(M("callback_token_consume_completed",{channel:o.channel,namespace:W,tokenHash:K(a),consumed:!!y,actionKind:y?.kind,sessionId:y?.sessionId,planDecisionVersion:y?.planDecisionVersion}),!y)return await Ue(o,()=>N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:l.kind==="plan-offer-start"||l.kind==="plan-offer-dismiss"})),{handled:!0};switch(y.kind){case"plugin-update-install":{if(await On(o,i),M("callback_update_action_started",{channel:o.channel,tokenHash:K(a),actionKind:y.kind,approvedVersion:y.pluginUpdateVersion}),!de){M("callback_update_action_failed",{channel:o.channel,tokenHash:K(a),reason:"service_unavailable"}),await x(o,"⚠️ OpenClaw Code Agent update service is not running.");break}let c;try{c=await de.installConfirmed(y.pluginUpdateVersion,{route:y.route})}catch(h){M("callback_update_action_failed",{channel:o.channel,tokenHash:K(a),reason:"install_failed"}),await x(o,`⚠️ OpenClaw Code Agent update failed: ${h instanceof Error?h.message:String(h)}`);break}M("callback_update_action_completed",{channel:o.channel,tokenHash:K(a),approvedVersion:y.pluginUpdateVersion});try{await x(o,`✅ ${c}`)}catch(h){M("callback_update_confirmation_failed",{channel:o.channel,tokenHash:K(a),reason:"reply_failed"}),`${h instanceof Error?h.message:String(h)}`}break}case"plugin-update-restart":{if(await On(o,i),!de){await x(o,"⚠️ OpenClaw Code Agent update service is not running.");break}try{let c=await de.restartConfirmed(y.pluginUpdateVersion);await x(o,`▶️ ${c}`)}catch(c){await x(o,`⚠️ Gateway restart failed: ${c instanceof Error?c.message:String(c)}`)}break}case"plugin-update-dismiss":{await On(o,i);let c=de?de.dismiss(y.pluginUpdateVersion):"Dismissed OpenClaw Code Agent update reminder.";await x(o,`✅ ${c}`);break}case"plugin-update-remind-later":{await On(o,i);let c=de?de.remindLater(y.pluginUpdateVersion):"Will remind later about OpenClaw Code Agent update reminder.";await x(o,`✅ ${c}`);break}case"worktree-merge":{let c=await t().execute("callback",{session:d}),h=Wn(c);if(Ms(c)){await En(o,i);break}await x(o,h);break}case"worktree-decide-later":{let c=k.snoozeWorktreeDecision(d,{notifyUser:!1});if(So(c)){let P=`⏭️ Snoozed 24h for [${m}]`;await En(o,i),await x(o,P)}else await x(o,c);break}case"worktree-dismiss":{let c=await k.dismissWorktree(d),h=So(c);h&&await En(o,i),await x(o,h?"✅ Discarded":c);break}case"worktree-create-pr":case"worktree-update-pr":{let c=await r().execute("callback",{session:d}),h=Wn(c);if(Ms(c)){await En(o,i);break}await x(o,h);break}case"worktree-view-pr":{await N(o,{alreadyAcknowledged:i});let c=k.getPersistedSession?.(d),h=l.targetUrl??c?.worktreePrUrl;await x(o,h?`PR: ${h}`:"⚠️ PR URL is no longer available.");break}case"plan-offer-start":{if(!y.launchPrompt||!y.launchWorkdir){await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,"⚠️ This action is missing the plan launch context.");break}let c;try{c=k.launchPlanOffer({route:y.route,prompt:y.launchPrompt,workdir:y.launchWorkdir,name:y.launchName,worktreeStrategy:y.launchWorktreeStrategy})}catch(h){let P=h instanceof Error?h.message:String(h);await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,`⚠️ Failed to start planning session: ${P}`);break}await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,`▶️ Planning session started: ${c.name} [${c.id}]`);break}case"plan-offer-dismiss":{await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,"✅ Dismissed.");break}case"repo-policy-set":{if(!y.repoPolicy||!y.repoPolicyWorkdir){await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,"⚠️ This action is missing the repo policy context.");break}if(!y.launchPrompt||!y.launchWorkdir){await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,"⚠️ This action is missing the launch context.");break}if(typeof k.resolveRepoPolicy=="function"){let P=k.resolveRepoPolicy(y.repoPolicyWorkdir);if(P.identity){let b=Ve(y.repoPolicy,P.prAvailable);if(b){await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,`⚠️ ${b}`);break}}}let c=k.setRepoPolicy(y.repoPolicyWorkdir,y.repoPolicy);if(!c){await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,`⚠️ Could not resolve a git repository for ${y.repoPolicyWorkdir}.`);break}k.clearRepoPolicyChoiceTokens(y.sessionId);let h;try{h=k.launchAfterRepoPolicyChoice({route:y.route,prompt:y.launchPrompt,workdir:y.launchWorkdir,name:y.launchName,model:y.launchModel,reasoningEffort:y.launchReasoningEffort,fastMode:y.launchFastMode,systemPrompt:y.launchSystemPrompt,allowedTools:y.launchAllowedTools,resumeSessionId:y.launchResumeSessionId,resumedFromSessionName:y.launchResumedFromSessionName,resumeWorktreeFrom:y.launchResumeWorktreeFrom,sessionIdOverride:y.launchSessionIdOverride,clearedPersistedCodexResume:y.launchClearedPersistedCodexResume,forkSession:y.launchForkSession,forceNewSession:y.launchForceNewSession,permissionMode:y.launchPermissionMode,planApproval:y.launchPlanApproval,harness:y.launchHarness,worktreeStrategy:y.launchWorktreeStrategy,worktreeBaseBranch:y.launchWorktreeBaseBranch,worktreePrTargetRepo:y.launchWorktreePrTargetRepo,originAgentId:y.launchOriginAgentId}).text}catch(P){let b=P instanceof Error?P.message:String(P);await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,`⚠️ Repo policy saved, but launch failed: ${b}`);break}await N(o,{alreadyAcknowledged:i,forceTelegramMarkupEdit:!0}),await x(o,[`✅ Repo policy saved: ${hs(c.policy).title}.`,"",h].join(`
|
|
87
|
+
`));break}case"session-restart":case"session-resume":{await N(o,{alreadyAcknowledged:i});let c=await Q(k,{session:d,message:"Continue where you left off.",userInitiated:!0});await x(o,c.isError?`⚠️ ${c.text}`:`▶️ ${c.text}`);break}case"view-output":{await N(o,{alreadyAcknowledged:i});let c=await Rn().execute("callback",{session:d,lines:50});await x(o,Wn(c));break}default:{await N(o,{alreadyAcknowledged:i}),await x(o,"⚠️ Unknown callback action.");break}}return{handled:!0}}}}function Ge(n){return Ns(n).map(e=>e.value)}function Ns(n){return Array.from(n.matchAll(/"[^"]*"|'[^']*'|\S+/g),e=>{let t=e[0],r=e.index??0;return{value:oc(t),raw:t,start:r,end:r+t.length}})}function Re(n){let[e]=Ns(n);if(e)return{value:e.value,rest:n.slice(e.end).trimStart()}}function oc(n){return n.length>1&&(n.startsWith('"')&&n.endsWith('"')||n.startsWith("'")&&n.endsWith("'"))?n.slice(1,-1):n}function ic(n){return n instanceof Error?n.message:String(n)}function sc(n){let e=Ge(n),t,r=e;return e[0]==="--name"&&e[1]&&(t=e[1],r=e.slice(2)),{name:t,prompt:r.join(" ").trim()}}function Os(n){n.registerCommand({name:"agent",description:"Launch a coding agent session. Usage: /agent [--name <name>] <prompt>",acceptsArgs:!0,requireAuth:!0,handler:e=>{if(!k)return{text:"Error: SessionManager not initialized. The code-agent service must be running."};let t=(e.args??"").trim();if(!t)return{text:"Usage: /agent [--name <name>] <prompt>"};let{name:r,prompt:o}=sc(t);if(!o)return{text:"Usage: /agent [--name <name>] <prompt>"};try{let i=on({prompt:o,name:r},e,k);if(i.kind!=="resolved")return{text:i.text};let s=k.spawn({prompt:o,name:r,workdir:i.workdir,model:i.resolvedModel,reasoningEffort:i.reasoningEffort,fastMode:i.fastMode,codexApprovalPolicy:i.harness==="codex"?"never":void 0,originChannel:i.originChannel,originThreadId:i.originThreadId,originAgentId:e.agentId||void 0,originSessionKey:i.originSessionKey,route:i.route,harness:i.harness,permissionMode:i.permissionMode,planApproval:i.planApproval,taskLifecycle:pn(e)});return{text:k.formatLaunchResult({prompt:o,workdir:i.workdir,harness:i.harness,permissionMode:i.permissionMode??_.permissionMode,planApproval:i.planApproval},s)}}catch(i){let s=ic(i),a=s.includes("Max sessions")?"":`
|
|
88
|
+
|
|
89
|
+
Use /agent_sessions to see active sessions.`;return{text:`Error launching session: ${s}${a}`}}}})}function Ws(n){n.registerCommand({name:"agent_sessions",description:"List coding agent sessions. Usage: /agent_sessions [--full]",acceptsArgs:!0,requireAuth:!0,handler:e=>{if(!k)return{text:"Error: SessionManager not initialized. The code-agent service must be running."};let t=(e.args??"").split(/\s+/).includes("--full");return{text:wn(k,"all",void 0,{full:t})}}})}function Fs(n){n.registerCommand({name:"agent_kill",description:"Kill a coding agent session by name or ID",acceptsArgs:!0,requireAuth:!0,handler:e=>{if(!k)return{text:"Error: SessionManager not initialized. The code-agent service must be running."};let t=e.args?.trim();return t?{text:bn(k,t,"killed")}:{text:"Usage: /agent_kill <name-or-id>"}}})}function Ls(n){n.registerCommand({name:"agent_respond",description:"Send a follow-up message to a running coding agent session. Usage: /agent_respond <id-or-name> <message>",acceptsArgs:!0,requireAuth:!0,handler:async e=>{if(!k)return{text:"Error: SessionManager not initialized. The code-agent service must be running."};let t=(e.args??"").trim();if(!t)return{text:`Usage: /agent_respond <id-or-name> <message>
|
|
90
|
+
/agent_respond --interrupt <id-or-name> <message>`};let r=!1,o=t,i=Re(o);i?.value==="--interrupt"&&(r=!0,o=i.rest);let s=Re(o);if(!s)return{text:"Error: Missing message. Usage: /agent_respond <id-or-name> <message>"};let a=s.value,l=s.rest;return l.trim()?{text:(await Q(k,{session:a,message:l,interrupt:r,userInitiated:!0})).text}:{text:"Error: Empty message. Usage: /agent_respond <id-or-name> <message>"}}})}function Bs(n){n.registerCommand({name:"agent_stats",description:"Show OpenClaw Code Agent usage metrics",acceptsArgs:!1,requireAuth:!0,handler:()=>{if(!k)return{text:"Error: SessionManager not initialized. The code-agent service must be running."};let e=k.getMetrics(),t=k.list("running").length;return{text:sn(e,t)}}})}function ac(n){return n==="pr-required"||n==="pr-allowed"||n==="never-pr"||n==="manual"}function Ks(n){n.registerCommand({name:"agent_policy",description:"Inspect or set the current repo integration policy. Usage: /agent_policy [pr-required|pr-allowed|never-pr|manual|reset|list|cleanup]",acceptsArgs:!0,requireAuth:!0,handler:e=>{if(!k)return{text:"Error: SessionManager not initialized. The code-agent service must be running."};let r=Re((e.args??"").trim())?.value;if(r==="list"){let a=k.listRepoPolicies();return{text:a.length===0?"No stored repo policies.":a.map(l=>`${l.policy} | ${l.provider} | ${l.repoRoot}`).join(`
|
|
90
91
|
`)}}if(r==="cleanup"){let a=k.cleanupRepoPolicies();return{text:a.length===0?"No stale repo policies found.":[`Removed ${a.length} stale repo ${a.length===1?"policy":"policies"}.`,...a.map(l=>`${l.policy} | ${l.provider} | ${l.repoRoot}`)].join(`
|
|
91
|
-
`)}}let o=e.workspaceDir;if(!o)return{text:"Error: workspaceDir is required."};if(r==="reset")return{text:k.resetRepoPolicy(o)?`Repo policy reset for ${o}.`:`No stored repo policy found for ${o}.`};if(r&&
|
|
92
|
+
`)}}let o=e.workspaceDir;if(!o)return{text:"Error: workspaceDir is required."};if(r==="reset")return{text:k.resetRepoPolicy(o)?`Repo policy reset for ${o}.`:`No stored repo policy found for ${o}.`};if(r&&ac(r)){if(typeof k.resolveRepoPolicy=="function"){let d=k.resolveRepoPolicy(o);if(d.identity){let u=Ve(r,d.prAvailable);if(u)return{text:`Error: ${u}`}}}let a=k.setRepoPolicy(o,r);if(!a)return{text:`Error: ${o} is not a git repository.`};let l=`Repo policy set to ${a.policy} for ${a.repoRoot}.`;try{if(typeof k.continueLaunchAfterManualRepoPolicy!="function")return{text:l};let d=k.continueLaunchAfterManualRepoPolicy(a.repoRoot,r);if(d.kind==="launched")return{text:[l,"",d.text].join(`
|
|
92
93
|
`)};if(d.kind==="ambiguous")return{text:[l,"",`Repo policy saved, but ${d.count} pending launches match this policy. Run the intended launch again to avoid starting the wrong session.`].join(`
|
|
93
94
|
`)}}catch(d){let u=d instanceof Error?d.message:String(d);return{text:[l,"",`Repo policy saved, but the deferred launch failed: ${u}`,"The pending launch context was kept so you can retry the same /agent_policy command or run the intended launch again."].join(`
|
|
94
|
-
`)}}return{text:l}}let i=k.resolveRepoPolicy(o);if(!i.identity)return{text:`No git repository found for ${o}.`};let s=
|
|
95
|
-
`)}}})}var
|
|
95
|
+
`)}}return{text:l}}let i=k.resolveRepoPolicy(o);if(!i.identity)return{text:`No git repository found for ${o}.`};let s=ct(i.prAvailable).map(a=>a.policy).join(", ");return{text:[`Repo policy: ${i.policy??"unknown"}`,`Repo: ${i.identity.repoRoot}`,`Provider: ${i.provider}${i.prAvailable?"":" (PR automation unavailable)"}`,...i.identity.remoteUrl?[`Remote: ${i.identity.remoteUrl}`]:[],"",`Set with /agent_policy ${s}.`].join(`
|
|
96
|
+
`)}}})}var lc=50;function qs(n){n.registerCommand({name:"agent_output",description:"Show recent output from a coding agent session. Usage: /agent_output <id-or-name> [--full] [--lines N]",acceptsArgs:!0,requireAuth:!0,handler:e=>{if(!k)return{text:"Error: SessionManager not initialized. The code-agent service must be running."};let t=(e.args??"").trim();if(!t)return{text:"Usage: /agent_output <id-or-name> [--full] [--lines N]"};let r=Ge(t),o="",i=!1,s=lc;for(let l=0;l<r.length;l++)if(r[l]==="--full")i=!0;else if(r[l]==="--lines"&&l+1<r.length){let d=parseInt(r[l+1],10);!isNaN(d)&&d>0&&(s=d),l++}else o||(o=r[l]);return o?{text:Pn(k,o,{full:i,lines:s})}:{text:"Usage: /agent_output <id-or-name> [--full] [--lines N]"}}})}var Vs="Usage: /agent_goal [--name <name>] [--workdir <dir>] [--model <model>] [--harness <name>] [--mode <ralph|verifier>] [--completion-promise <text>] [--max-iterations N] [--verify <cmd> ...] <goal>";function Us(n){n.registerCommand({name:"agent_goal",description:"Launch an explicit goal task (Ralph-style completion loop or verifier-driven loop)",acceptsArgs:!0,requireAuth:!0,handler:async e=>{if(!$)return{text:"Error: GoalController not initialized. The code-agent service must be running."};let t=(e.args??"").trim();if(!t)return{text:Vs};let r=Ge(t),o,i,s,a,l="bypassPermissions",d,u,m,g=[],S=[];for(let h=0;h<r.length;h++){let P=r[h];if(P==="--name"&&h+1<r.length)o=r[++h];else if(P==="--workdir"&&h+1<r.length)i=r[++h];else if(P==="--model"&&h+1<r.length)s=r[++h];else if(P==="--max-iterations"&&h+1<r.length){let b=parseInt(r[++h],10);!Number.isNaN(b)&&b>0&&(a=b)}else if(P==="--permission-mode"&&h+1<r.length){let b=r[++h];if(b==="default"||b==="plan"||b==="bypassPermissions")l=b;else return{text:`Error: Invalid permission mode "${b}".`}}else if(P==="--harness"&&h+1<r.length)d=r[++h];else if(P==="--mode"&&h+1<r.length){let b=r[++h];if(b==="ralph"||b==="verifier")u=b;else return{text:`Error: Invalid goal mode "${b}". Use ralph or verifier.`}}else if(P==="--completion-promise"&&h+1<r.length)m=r[++h];else if(P==="--verify"&&h+1<r.length){let b=r[++h].trim();if(!b)return{text:"Error: --verify commands must not be empty."};g.push(b)}else S.push(P)}let y=S.join(" ").trim();if(!y)return{text:Vs};let c=xn({goal:y,verifierCommands:g,name:o,workdir:i,model:s,maxIterations:a,permissionMode:l,harness:d,goalMode:u,completionPromise:m},e);if(c.kind!=="resolved")return{text:c.text};try{let h=await $.launchTask({goal:c.goal,name:c.name,workdir:c.workdir,model:c.model,reasoningEffort:c.reasoningEffort,fastMode:c.fastMode,maxIterations:c.maxIterations,permissionMode:c.permissionMode,harness:c.harness,loopMode:c.loopMode,completionPromise:c.completionPromise,originChannel:c.originChannel,originThreadId:c.originThreadId,originAgentId:c.originAgentId,originSessionKey:c.originSessionKey,route:c.route,verifierCommands:c.verifierCommands});return{text:Cn(h,c)}}catch(h){return{text:`Error launching goal task: ${h instanceof Error?h.message:String(h)}`}}}})}function Gs(n){n.registerCommand({name:"agent_goal_status",description:"Show one goal task or list all explicit goal tasks. Usage: /agent_goal_status [<task-id-or-name>]",acceptsArgs:!0,requireAuth:!0,handler:e=>{if(!$)return{text:X};let t=(e.args??"").trim();return{text:_n($,r=>k?.resolve(r),t)}}})}function js(n){n.registerCommand({name:"agent_goal_stop",description:"Stop a goal task. Usage: /agent_goal_stop <task-id-or-name>",acceptsArgs:!0,requireAuth:!0,handler:e=>{if(!$)return{text:X};let t=(e.args??"").trim();if(!t)return{text:"Usage: /agent_goal_stop <task-id-or-name>"};let r=$.stopTask(t);return{text:Dn(r,t)}}})}var zs="Usage: /agent_goal_edit <task-id-or-name> <replacement-goal>";function Hs(n){n.registerCommand({name:"agent_goal_edit",description:"Edit a running goal task. Usage: /agent_goal_edit <task-id-or-name> <replacement-goal>",acceptsArgs:!0,requireAuth:!0,handler:e=>{if(!$)return{text:X};let t=(e.args??"").trim();if(!t)return{text:zs};let r=Re(t),o=r?.value.trim(),i=r?.rest.trim();if(!o||!i)return{text:zs};let s=$.editTask(o,i);return{text:$n(s,o)}}})}import{execFile as wc}from"child_process";import{createHash as da}from"crypto";import{existsSync as ht,mkdirSync as dc,readFileSync as uc,renameSync as Js,writeFileSync as cc}from"fs";import{homedir as pc}from"os";import{dirname as mc,join as Xs}from"path";var fc=new Set(["running","waiting_for_session","waiting_for_user","succeeded","failed","stopped"]);function gc(n){let e=n.OPENCLAW_HOME?.trim();return e||Xs(pc(),".openclaw")}function hc(n){let e=n.OPENCLAW_CODE_AGENT_GOAL_TASKS_PATH?.trim();return e||Xs(gc(n),"code-agent-goal-tasks.json")}function Zs(n){return n instanceof Error?n.message:String(n)}function ea(n){return!!n&&typeof n=="object"&&!Array.isArray(n)}var yc=100;function Qs(n,e,t){let r=t==null?"":`-${t}`;return`${n}.invalid-${e}${r}.json`}function Sc(n){if(!ht(n))return;let e=Date.now(),t=Qs(n,e);if(!ht(t))return t;for(let r=1;r<=yc;r+=1){let o=Qs(n,e,r);if(!ht(o))return o}}function Ys(n,e){try{let t=Sc(n);return t?(Js(n,t),`${e}${t}`,!0):(ht(n),!1)}catch(t){return`${Zs(t)}`,!1}}function kc(n){if(!ea(n))return;let e=n;if(!(typeof e.provider!="string"||typeof e.target!="string"))return{provider:e.provider,target:e.target,accountId:typeof e.accountId=="string"?e.accountId:void 0,threadId:typeof e.threadId=="string"?e.threadId:void 0,sessionKey:typeof e.sessionKey=="string"?e.sessionKey:void 0}}function vc(n){if(!ea(n))return;let e=n;if(typeof e.id!="string"||typeof e.name!="string"||typeof e.goal!="string"||typeof e.workdir!="string"||typeof e.status!="string"||!fc.has(e.status))return;let t=e.status,r=t==="running"||t==="waiting_for_session"?"waiting_for_session":t;return{id:e.id,name:e.name,goal:e.goal,workdir:e.workdir,status:r,createdAt:typeof e.createdAt=="number"?e.createdAt:Date.now(),updatedAt:typeof e.updatedAt=="number"?e.updatedAt:Date.now(),iteration:typeof e.iteration=="number"?e.iteration:0,maxIterations:typeof e.maxIterations=="number"?e.maxIterations:8,sessionId:typeof e.sessionId=="string"?e.sessionId:void 0,sessionName:typeof e.sessionName=="string"?e.sessionName:void 0,harnessSessionId:typeof e.harnessSessionId=="string"?e.harnessSessionId:void 0,model:typeof e.model=="string"?e.model:void 0,reasoningEffort:typeof e.reasoningEffort=="string"?e.reasoningEffort:void 0,fastMode:e.fastMode===!0?!0:void 0,systemPrompt:typeof e.systemPrompt=="string"?e.systemPrompt:void 0,allowedTools:Array.isArray(e.allowedTools)?e.allowedTools.filter(o=>typeof o=="string"):void 0,originChannel:typeof e.originChannel=="string"?e.originChannel:void 0,originThreadId:typeof e.originThreadId=="string"||typeof e.originThreadId=="number"?e.originThreadId:void 0,originAgentId:typeof e.originAgentId=="string"?e.originAgentId:void 0,originSessionKey:typeof e.originSessionKey=="string"?e.originSessionKey:void 0,route:kc(e.route),harness:typeof e.harness=="string"?e.harness:void 0,permissionMode:typeof e.permissionMode=="string"?e.permissionMode:void 0,loopMode:e.loopMode==="ralph"?"ralph":"verifier",completionPromise:typeof e.completionPromise=="string"?e.completionPromise:void 0,verifierCommands:Array.isArray(e.verifierCommands)?e.verifierCommands.filter(o=>!!o&&typeof o=="object"&&typeof o.label=="string"&&typeof o.command=="string"):[],lastVerifierSummary:typeof e.lastVerifierSummary=="string"?e.lastVerifierSummary:void 0,lastVerifierFingerprint:typeof e.lastVerifierFingerprint=="string"?e.lastVerifierFingerprint:void 0,repeatedFailureCount:typeof e.repeatedFailureCount=="number"?e.repeatedFailureCount:0,waitingForUserReason:typeof e.waitingForUserReason=="string"?e.waitingForUserReason:void 0,failureReason:typeof e.failureReason=="string"?e.failureReason:void 0}}function Pc(n){if(!Array.isArray(n))return;let e=[];for(let t of n){let r=vc(t);if(!r)return;e.push(r)}return e}var Ln=class{path;tasks=new Map;constructor(e=process.env){this.path=hc(e),this.load()}load(){try{if(!ht(this.path))return;let e=uc(this.path,"utf8"),t=JSON.parse(e),r=Pc(t);if(!r){Ys(this.path,"invalid")&&this.save();return}this.tasks.clear();for(let o of r)this.tasks.set(o.id,o);this.save()}catch(e){if(e.code==="ENOENT")return;Ys(this.path,"corrupt or unreadable")&&this.save()}}save(){try{dc(mc(this.path),{recursive:!0});let e=`${this.path}.tmp`;cc(e,JSON.stringify([...this.tasks.values()],null,2),"utf8"),Js(e,this.path)}catch(e){`${this.path}${Zs(e)}`}}upsert(e){this.tasks.set(e.id,e),this.save()}get(e){let t=this.tasks.get(e);return t||[...this.tasks.values()].find(r=>r.name===e)}list(){return[...this.tasks.values()].sort((e,t)=>t.createdAt-e.createdAt)}};var bc=8,ua=600*1e3,Rc=4e3,ko=1200,Ac="DONE",Ic=["BASH_ENV","ENV"];function Tc(n){return n.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,48)||"goal-task"}function je(n,e){return n.length<=e?n:`${n.slice(0,e-3)}...`}function he(n,e=20){let t=n.split(/\r?\n/).map(r=>r.trimEnd()).filter(r=>r.trim().length>0);return t.length<=e?t.join(`
|
|
96
97
|
`):t.slice(-e).join(`
|
|
97
|
-
`)}function
|
|
98
|
-
`):void 0}function
|
|
99
|
-
`);return
|
|
98
|
+
`)}function xc(n){let e=(n??"").split(/\r?\n/).map(t=>t.trim()).filter(t=>t.length>0).filter(t=>!/^Iteration summary:$/i.test(t)).map(t=>t.replace(/^[-*]\s+/,""));return e.length>0?e.join(`
|
|
99
|
+
`):void 0}function ta(n){return n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function na(n){let e=n?.trim();return e&&e.length>0?e:Ac}function Cc(n){return n.route??ue(n.originChannel,n.originThreadId,n.originSessionKey)}function vo(n){return n.map((e,t)=>({label:e.label.trim()||`check-${t+1}`,command:e.command.trim(),timeoutMs:e.timeoutMs??ua})).filter(e=>e.command.length>0)}function Mc(n){return n.loopMode==="verifier"}function _c(n){return vo(n.verifierCommands).length>0}function ra(n){return Mc(n)&&!_c(n)}function Bn(){return"Verifier-mode goal tasks require at least one verifier command."}function oa(n){let e=n.steps.filter(t=>!t.ok).map(t=>`${t.label}:${t.exitCode}:${he(t.output,12)}`).join(`
|
|
100
|
+
`);return da("sha1").update(e).digest("hex")}function Dc(n){return da("sha1").update(he(n,24)).digest("hex")}function ia(n){let e=n.toLowerCase().replace(/\s+/g," ").trim();if(!e)return;if(/\b(can i|may i|should i|do you want me to|would you like me to)\b/.test(e)&&/\b(read|write|edit|modify|change|inspect|search|run|execute|install|delete|move|rename|open)\b/.test(e))return"Yes, proceed.";if(/\b(should i continue|shall i continue|want me to continue|should i proceed|shall i proceed|can i proceed|go ahead)\b/.test(e))return"Yes, continue."}function $c(n){if(n.loopMode==="ralph")return["You are working inside a Ralph Wiggum-style autonomous loop.","","Goal:",n.goal,"","Loop rules:","- Keep making concrete progress toward the goal in this repository.","- An external controller will continue looping until you emit the completion promise or the iteration budget is exhausted.",`- When the goal is truly complete, output this exact marker on its own line: <promise>${n.completionPromise}</promise>`,"- Do not output that completion marker early, approximately, or with altered spelling.","- If you are not done yet, do not emit the completion marker.","- Do not ask to stop or wait for confirmation unless blocked by a real product, architecture, credential, or approval decision.","- Re-run useful local checks before ending each turn.",n.verifierCommands.length>0?"- External verifier commands will also be run after you claim completion.":"- There may not be external verifiers, so your completion promise is the success gate."].join(`
|
|
100
101
|
`);let e=n.verifierCommands.map(t=>`- ${t.label}: ${t.command}`).join(`
|
|
101
102
|
`);return["You are working on an autonomous goal-driven task.","","Goal:",n.goal,"","Working rules:","- Make concrete progress toward the goal in this repository.","- An external controller will run these verifier commands after your turn:",e,"- If any verifier fails, you will receive the exact failures and must continue fixing the remaining gaps.","- Do not stop early just because you think the task is probably done.","- Do not ask for confirmation unless you are blocked by a real product, architecture, or credential decision.","- Before ending a turn, run any local checks you think are useful."].join(`
|
|
102
|
-
`)}function
|
|
103
|
+
`)}function sa(n){return n.loopMode==="ralph"?["The OpenClaw gateway restarted while this Ralph-style goal task was running.","Resume from the prior session context and continue immediately.","","Goal:",n.goal,"","Instructions:","- Continue from the current repo state and prior session context without restarting from scratch.",`- Only emit <promise>${n.completionPromise}</promise> when the goal is actually complete.`,"- The external controller will keep looping until that marker appears or the iteration budget is exhausted.","- Do not ask for confirmation just because the gateway restarted."].join(`
|
|
103
104
|
`):["The OpenClaw gateway restarted while this autonomous goal task was running.","Resume from the prior session context and continue toward the same goal immediately.","","Goal:",n.goal,"","Instructions:","- Re-orient yourself from the existing repo state and prior session context.","- Continue the task without restarting from scratch unless the prior approach is clearly invalid.","- The external controller will continue running verifier commands after your turns.","- Do not ask for confirmation just because the gateway restarted."].join(`
|
|
104
|
-
`)}function
|
|
105
|
+
`)}function Ec(n,e){let t=e.steps.filter(o=>!o.ok).map(o=>[`- ${o.label} failed (exit ${o.exitCode})`,he(o.output,18)].join(`
|
|
105
106
|
`)).join(`
|
|
106
107
|
|
|
107
108
|
`);return["The external verifier did not pass.",n.repeatedFailureCount>=2?"The same verifier fingerprint has repeated. Use a different debugging strategy than your previous attempt.":"Fix only the remaining issues from the verifier output below.","","Goal:",n.goal,"","Verifier failures:",t,"","Instructions:","- Continue from the current code state and prior session context.","- Make the minimum necessary changes to satisfy the remaining verifier failures.","- Re-run relevant checks yourself before ending the turn.","- Do not ask for confirmation unless you are truly blocked on a human decision."].join(`
|
|
108
|
-
`)}function
|
|
109
|
-
`)}function
|
|
109
|
+
`)}function Nc(n,e){let t=he(e,18)||"(no meaningful output)",r=n.repeatedFailureCount>=2?"The loop has repeated without converging. Use a different strategy than the previous attempt.":"Continue from the current repo state and prior session context.";return["Continue the same Ralph-style goal task.","","Goal:",n.goal,"","Status:",`- The last turn ended without the completion promise <promise>${n.completionPromise}</promise>.`,`- Current iteration: ${n.iteration}/${n.maxIterations}.`,`- ${r}`,"","Latest output / blockers:",t,"","Instructions:","- Keep working until the goal is actually complete.",`- Only emit <promise>${n.completionPromise}</promise> when all requested work is done.`,"- If you are not done, do not emit the completion promise.",n.verifierCommands.length>0?"- If you believe you are done, make sure the expected verifiers are likely to pass before emitting the completion promise.":"- There may not be external verifiers, so your completion promise is the success signal."].join(`
|
|
110
|
+
`)}function Oc(n,e){let t=e.steps.filter(r=>!r.ok).map(r=>[`- ${r.label} failed (exit ${r.exitCode})`,he(r.output,18)].join(`
|
|
110
111
|
`)).join(`
|
|
111
112
|
|
|
112
113
|
`);return["You emitted the completion promise, but the external verifiers did not pass.","","Goal:",n.goal,"","Verifier failures:",t,"","Instructions:","- Continue from the current repo state and fix only the remaining gaps.",`- Do not emit <promise>${n.completionPromise}</promise> again until the goal is fully complete and the failing checks are addressed.`,"- Re-run relevant checks yourself before ending the turn."].join(`
|
|
113
|
-
`)}function
|
|
114
|
-
`)}function Ae(n){return n instanceof Error?n.message:String(n)}function
|
|
115
|
-
${s}`:""}`.trim();t({label:e.label,command:e.command,ok:!o,exitCode:l,durationMs:a,output:
|
|
114
|
+
`)}function Wc(n,e){let t=e.trim();if(!t)return!1;let r=new RegExp(`(^|\\n)\\s*${ta(t)}\\s*(\\n|$)`,"i"),o=new RegExp(`<promise>\\s*${ta(t)}\\s*<\\/promise>`,"i");return r.test(n)||o.test(n)}function aa(n){return n.steps.map(e=>`${e.ok?"PASS":"FAIL"} ${e.label} (exit ${e.exitCode}, ${Math.round(e.durationMs)}ms)`).join(`
|
|
115
|
+
`)}function Ae(n){return n instanceof Error?n.message:String(n)}function la(n){return n==="succeeded"||n==="failed"||n==="stopped"}function Fc(n){return je(n.error?.trim()||"Underlying session failed.",ko)}function Lc(n=process.env){let e={...n};for(let t of Ic)delete e[t];return e}function Bc(n,e){return new Promise(t=>{let r=Date.now();wc("bash",["-lc",e.command],{cwd:n,timeout:e.timeoutMs??ua,maxBuffer:1024*1024,env:Lc()},(o,i,s)=>{let a=Date.now()-r,l=typeof o?.code=="number"?o.code:o?1:0,d=`${i??""}${s?`
|
|
116
|
+
${s}`:""}`.trim();t({label:e.label,command:e.command,ok:!o,exitCode:l,durationMs:a,output:je(d||"(no output)",Rc)})})})}var Kn=class{store;sessionManager;restorePromise=null;inFlight=new Set;observerDisposers=new Map;scheduledEvaluations=new Map;dirtyEvaluations=new Set;dirtyEvaluationSessionIds=new Map;started=!1;constructor(e){this.sessionManager=e,this.store=new Ln}start(){this.started||(this.started=!0,!this.restorePromise&&(this.restorePromise=this.restoreRecoverableTasks().catch(e=>{`${Ae(e)}`}).finally(()=>{this.restorePromise=null})))}stop(){this.started=!1,this.clearScheduledEvaluations(),this.detachSessionObservers(),this.captureRecoverableTasks(),this.store.save()}listTasks(){return this.store.list()}getTask(e){return this.store.get(e)}async launchTask(e){let t=e.loopMode??"verifier",r=vo(e.verifierCommands);if(t==="verifier"&&r.length===0)throw new Error(Bn());let i={id:fn(8),name:Tc(e.name??e.goal),goal:e.goal.trim(),workdir:e.workdir,status:"waiting_for_session",createdAt:Date.now(),updatedAt:Date.now(),iteration:0,maxIterations:e.maxIterations??bc,model:e.model,reasoningEffort:e.reasoningEffort,fastMode:e.fastMode,systemPrompt:e.systemPrompt,allowedTools:e.allowedTools,originChannel:e.originChannel,originThreadId:e.originThreadId,originAgentId:e.originAgentId,originSessionKey:e.originSessionKey,route:e.route,harness:e.harness,permissionMode:e.permissionMode??"bypassPermissions",loopMode:t,completionPromise:na(e.completionPromise),verifierCommands:r,repeatedFailureCount:0},s=await this.spawnTaskSession(i,$c(i));return this.attachSessionObservers(i,s),i.sessionId=s.id,i.sessionName=s.name,i.harnessSessionId=s.harnessSessionId,i.status="running",i.updatedAt=Date.now(),this.store.upsert(i),this.scheduleTaskEvaluation(i.id,"launch",s.id),this.notify(i,`🎯 [${i.name}] Goal task started
|
|
116
117
|
|
|
117
118
|
Goal:
|
|
118
|
-
${
|
|
119
|
+
${je(i.goal,500)}`,"goal-task-started"),i}stopTask(e){let t=this.store.get(e);if(t)return la(t.status)?{task:t,action:"already_terminal"}:(t.sessionId&&this.sessionManager.kill(t.sessionId,"user"),this.markTaskStopped(t,"Stopped by user."),{task:t,action:"stopped"})}editTask(e,t){let r=t.trim();if(!r)return{action:"invalid_goal"};let o=this.store.get(e);if(!o)return{action:"not_found"};if(la(o.status)||o.status==="waiting_for_user")return{action:"not_editable",task:o};let i=o.goal;return o.goal=r,o.updatedAt=Date.now(),this.store.upsert(o),this.notify(o,`✏️ [${o.name}] Goal task edited
|
|
119
120
|
|
|
120
121
|
Goal:
|
|
121
|
-
${
|
|
122
|
-
`));return this.attachSessionObservers(e,i),i}let o=await this.spawnManagedTaskSession(e,t,r.harnessSessionId);return this.attachSessionObservers(e,o),o}resolveResumeSessionId(e){if(e.harnessSessionId)return e.harnessSessionId;if(e.sessionId){let r=this.sessionManager.resolveHarnessSessionId(e.sessionId);if(r)return r}let t=this.sessionManager.resolveHarnessSessionId(e.name);if(t)return t}captureRecoverableTasks(){for(let e of this.store.list()){if(e.status!=="running"&&e.status!=="waiting_for_session")continue;let t=e.sessionId?this.sessionManager.resolve(e.sessionId):void 0;t&&(e.sessionId=t.id,e.sessionName=t.name,e.harnessSessionId=t.harnessSessionId??e.harnessSessionId,e.route=t.route??e.route,e.updatedAt=Date.now(),e.status="waiting_for_session",this.store.upsert(e))}}async restoreRecoverableTasks(){for(let e of this.store.list()){if(!this.started)break;if(e.status==="waiting_for_user"){this.markTaskFailed(e,"Goal task was waiting for user input and cannot continue autonomously");continue}if(e.status!=="waiting_for_session"&&e.status!=="running")continue;if(
|
|
122
|
+
${je(o.goal,500)}`,"goal-task-edited"),{action:"updated",task:o,previousGoal:i}}async spawnTaskSession(e,t){return this.spawnManagedTaskSession(e,t)}async spawnManagedTaskSession(e,t,r){let o=r?this.sessionManager.resolveHarnessSessionId(r)??r:void 0,i=r?this.sessionManager.resolve(r):void 0,s=o?this.sessionManager.getPersistedSession(o)??this.sessionManager.getPersistedSession(r??o):void 0,{resumeSessionId:a}=qe({requestedResumeSessionId:o,activeSession:i?{harnessSessionId:i.harnessSessionId}:void 0,persistedSession:s?{harness:s.harness,backendRef:s.backendRef}:void 0}),l={prompt:t,name:e.name,workdir:e.workdir,model:e.model,reasoningEffort:e.reasoningEffort,fastMode:e.fastMode,systemPrompt:e.systemPrompt,allowedTools:e.allowedTools,originChannel:e.originChannel,originThreadId:e.originThreadId,originAgentId:e.originAgentId,originSessionKey:e.originSessionKey,route:Cc(e),permissionMode:e.permissionMode,multiTurn:!0,goalTaskId:e.id,harness:e.harness,resumeSessionId:a,resumeWorktreeFrom:o,worktreeStrategy:"off"},d=await this.sessionManager.spawnAndAwaitRunning(l,{notifyLaunch:!1});return e.harness=d.harnessName??e.harness,e.model=d.model??e.model,e.reasoningEffort=d.reasoningEffort??e.reasoningEffort,d}async resumeTaskSession(e,t,r){if(!r.harnessSessionId){let i=await this.spawnTaskSession(e,["The previous session ended without a resumable harness session id.","Continue working on the same goal.","",t].join(`
|
|
123
|
+
`));return this.attachSessionObservers(e,i),i}let o=await this.spawnManagedTaskSession(e,t,r.harnessSessionId);return this.attachSessionObservers(e,o),o}resolveResumeSessionId(e){if(e.harnessSessionId)return e.harnessSessionId;if(e.sessionId){let r=this.sessionManager.resolveHarnessSessionId(e.sessionId);if(r)return r}let t=this.sessionManager.resolveHarnessSessionId(e.name);if(t)return t}captureRecoverableTasks(){for(let e of this.store.list()){if(e.status!=="running"&&e.status!=="waiting_for_session")continue;let t=e.sessionId?this.sessionManager.resolve(e.sessionId):void 0;t&&(e.sessionId=t.id,e.sessionName=t.name,e.harnessSessionId=t.harnessSessionId??e.harnessSessionId,e.route=t.route??e.route,e.updatedAt=Date.now(),e.status="waiting_for_session",this.store.upsert(e))}}async restoreRecoverableTasks(){for(let e of this.store.list()){if(!this.started)break;if(e.status==="waiting_for_user"){this.markTaskFailed(e,"Goal task was waiting for user input and cannot continue autonomously");continue}if(e.status!=="waiting_for_session"&&e.status!=="running")continue;if(ra(e)){this.markTaskFailed(e,Bn());continue}if(e.sessionId){let r=this.sessionManager.resolve(e.sessionId);if(r&&(r.status==="starting"||r.status==="running"))continue}let t=this.resolveResumeSessionId(e);if(!t){this.markTaskFailed(e,"Goal task could not be resumed after gateway restart because no resumable session id was available.");continue}try{let r=await this.spawnManagedTaskSession(e,sa(e),t);if(!this.started){e.sessionId=r.id,e.sessionName=r.name,e.harnessSessionId=r.harnessSessionId??t,e.route=r.route??e.route,e.status="waiting_for_session",e.updatedAt=Date.now(),this.store.upsert(e),this.sessionManager.kill(r.id,"shutdown");break}this.attachSessionObservers(e,r),e.sessionId=r.id,e.sessionName=r.name,e.harnessSessionId=r.harnessSessionId??t,e.route=r.route??e.route,e.status="running",e.updatedAt=Date.now(),this.store.upsert(e),this.notifyIterationStatus(e,`🔄 [${e.name}] Goal task resumed after gateway restart`,r),this.scheduleTaskEvaluation(e.id,"restore",r.id)}catch(r){this.markTaskFailed(e,`Failed to resume the goal task after gateway restart: ${Ae(r)}`)}}}async runVerifiers(e){let t=vo(e.verifierCommands);if(t.length===0){let s={status:"fail",steps:[{label:"verifier-config",command:"(none configured)",ok:!1,exitCode:1,durationMs:0,output:Bn()}],summary:"",fingerprint:""};return s.fingerprint=oa(s),s.summary=aa(s),s}let r=[];for(let s of t)r.push(await Bc(e.workdir,s));let i={status:r.every(s=>s.ok)?"pass":"fail",steps:r,summary:"",fingerprint:""};return i.fingerprint=oa(i),i.summary=aa(i),i}notify(e,t,r){this.sessionManager.emitGoalTaskUpdate(e,t,r)}notifyIterationStatus(e,t,r,o){let i=e.loopMode==="ralph"?"iteration":"repair iteration",s=/\b(?:repair\s+)?iteration\s+\d+\/\d+\b/i.test(t)?t:`${t} ${i} ${e.iteration}/${e.maxIterations}`,a=xc(o),l=a?`${s}
|
|
123
124
|
|
|
124
|
-
${a}`:s;this.notify(e,l,"goal-task-progress")}setTaskRunningWithSession(e,t){e.sessionId=t.id,e.sessionName=t.name,e.harnessSessionId=t.harnessSessionId,e.route=t.route??e.route,e.status="running",e.waitingForUserReason=void 0,e.updatedAt=Date.now(),this.store.upsert(e)}attachSessionObservers(e,t){if(this.observerDisposers.has(t.id))return;let r=(i,s)=>{(s==="completed"||s==="failed"||s==="killed")&&(this.removeSessionObserver(t.id),this.scheduleTaskEvaluation(e.id,`status:${s}`,t.id))},o=()=>{let i=this.store.get(e.id);i&&(i.sessionId=t.id,i.sessionName=t.name,i.harnessSessionId=t.harnessSessionId,i.route=t.route??i.route,i.updatedAt=Date.now(),i.status==="waiting_for_session"&&(i.status="running"),this.store.upsert(i),this.notifyIterationStatus(i,`🔄 [${i.name}] Coding turn complete`,t),this.scheduleTaskEvaluation(e.id,"turnEnd",t.id))};t.on("statusChange",r),t.on("turnEnd",o),this.observerDisposers.set(t.id,()=>{t.off?.("statusChange",r),t.off?.("turnEnd",o),t.removeListener?.("statusChange",r),t.removeListener?.("turnEnd",o)})}scheduleTaskEvaluation(e,t,r){if(!this.started)return;let o=this.scheduledEvaluations.get(e);if(o){!o.sessionId&&r&&(o.sessionId=r);return}let i={timer:setTimeout(()=>{this.scheduledEvaluations.delete(e),this.evaluateTask(e,t,i.sessionId).catch(s=>{`${t}${Ae(s)}`})},0),sessionId:r};i.timer.unref?.(),this.scheduledEvaluations.set(e,i)}clearScheduledEvaluations(){for(let e of this.scheduledEvaluations.values())clearTimeout(e.timer);this.scheduledEvaluations.clear(),this.dirtyEvaluations.clear(),this.dirtyEvaluationSessionIds.clear()}detachSessionObservers(){for(let e of[...this.observerDisposers.keys()])this.removeSessionObserver(e)}removeSessionObserver(e){let t=this.observerDisposers.get(e);t&&(this.observerDisposers.delete(e),t())}markTaskFailed(e,t){e.status="failed",e.failureReason=
|
|
125
|
+
${a}`:s;this.notify(e,l,"goal-task-progress")}setTaskRunningWithSession(e,t){e.sessionId=t.id,e.sessionName=t.name,e.harnessSessionId=t.harnessSessionId,e.route=t.route??e.route,e.status="running",e.waitingForUserReason=void 0,e.updatedAt=Date.now(),this.store.upsert(e)}attachSessionObservers(e,t){if(this.observerDisposers.has(t.id))return;let r=(i,s)=>{(s==="completed"||s==="failed"||s==="killed")&&(this.removeSessionObserver(t.id),this.scheduleTaskEvaluation(e.id,`status:${s}`,t.id))},o=()=>{let i=this.store.get(e.id);i&&(i.sessionId=t.id,i.sessionName=t.name,i.harnessSessionId=t.harnessSessionId,i.route=t.route??i.route,i.updatedAt=Date.now(),i.status==="waiting_for_session"&&(i.status="running"),this.store.upsert(i),this.notifyIterationStatus(i,`🔄 [${i.name}] Coding turn complete`,t),this.scheduleTaskEvaluation(e.id,"turnEnd",t.id))};t.on("statusChange",r),t.on("turnEnd",o),this.observerDisposers.set(t.id,()=>{t.off?.("statusChange",r),t.off?.("turnEnd",o),t.removeListener?.("statusChange",r),t.removeListener?.("turnEnd",o)})}scheduleTaskEvaluation(e,t,r){if(!this.started)return;let o=this.scheduledEvaluations.get(e);if(o){!o.sessionId&&r&&(o.sessionId=r);return}let i={timer:setTimeout(()=>{this.scheduledEvaluations.delete(e),this.evaluateTask(e,t,i.sessionId).catch(s=>{`${t}${Ae(s)}`})},0),sessionId:r};i.timer.unref?.(),this.scheduledEvaluations.set(e,i)}clearScheduledEvaluations(){for(let e of this.scheduledEvaluations.values())clearTimeout(e.timer);this.scheduledEvaluations.clear(),this.dirtyEvaluations.clear(),this.dirtyEvaluationSessionIds.clear()}detachSessionObservers(){for(let e of[...this.observerDisposers.keys()])this.removeSessionObserver(e)}removeSessionObserver(e){let t=this.observerDisposers.get(e);t&&(this.observerDisposers.delete(e),t())}markTaskFailed(e,t){e.status="failed",e.failureReason=je(t,ko),e.updatedAt=Date.now(),this.store.upsert(e),this.notify(e,`❌ [${e.name}] Goal task failed
|
|
125
126
|
|
|
126
127
|
${e.failureReason}`,"goal-task-failed")}markTaskFailedWaitingForUser(e,t){this.markTaskFailed(e,`Goal task was waiting for user input and cannot continue autonomously: ${t}`)}markTaskSucceeded(e,t){e.status="succeeded",e.lastVerifierSummary=t,e.updatedAt=Date.now(),this.store.upsert(e),this.notify(e,`✅ [${e.name}] Goal task succeeded
|
|
127
128
|
|
|
128
|
-
${t}`,"goal-task-succeeded")}markTaskStopped(e,t){e.status="stopped",e.failureReason=
|
|
129
|
+
${t}`,"goal-task-succeeded")}markTaskStopped(e,t){e.status="stopped",e.failureReason=je(t,ko),e.updatedAt=Date.now(),this.store.upsert(e),this.notify(e,`⛔ [${e.name}] Goal task stopped
|
|
129
130
|
|
|
130
131
|
${e.failureReason}`,"goal-task-stopped")}async handleRunningSession(e,t){if(t.pendingPlanApproval){let s=await Q(this.sessionManager,{session:t.id,message:"Approved. Implement the plan.",approve:!0,userInitiated:!1});s.isError&&this.markTaskFailed(e,s.text);return}if(!t.pendingInputState){this.setTaskRunningWithSession(e,t);return}let r=t.getOutput(30).join(`
|
|
131
|
-
`),o=
|
|
132
|
-
`);if(t.pendingPlanApproval){let a=await Q(this.sessionManager,{session:t.id,message:"Approved. Implement the plan.",approve:!0,userInitiated:!1});if(a.isError){this.markTaskFailed(e,a.text);return}let l=this.sessionManager.resolve(t.id);if(!l){this.markTaskFailed(e,"Failed to resolve the resumed goal task session after idle timeout plan approval.");return}this.attachSessionObservers(e,l),this.setTaskRunningWithSession(e,l),this.notifyIterationStatus(e,`🔄 [${e.name}] Goal task resumed after idle timeout`,l),this.scheduleTaskEvaluation(e.id,"idle-timeout-plan-resume",l.id);return}if(t.pendingInputState){let a=
|
|
132
|
+
`),o=ia(r);if(!o){this.markTaskFailedWaitingForUser(e,he(r,24)||"The session is waiting for user input.");return}let i=await Q(this.sessionManager,{session:t.id,message:o,userInitiated:!1});if(i.isError||i.text.includes("Auto-respond limit reached")){this.markTaskFailedWaitingForUser(e,i.text);return}e.status="running",e.waitingForUserReason=void 0,e.updatedAt=Date.now(),this.store.upsert(e)}async resumeAfterIdleTimeout(e,t,r){try{let o=await this.resumeTaskSession(e,r,t);this.setTaskRunningWithSession(e,o),this.notifyIterationStatus(e,`🔄 [${e.name}] Goal task resumed after idle timeout`,o),this.scheduleTaskEvaluation(e.id,"idle-timeout-resume",o.id)}catch(o){this.markTaskFailed(e,`Failed to resume the goal task after idle timeout: ${Ae(o)}`)}}async handleTerminalSession(e,t){if(t.status==="failed"){this.markTaskFailed(e,Fc(t));return}if(t.status==="killed"&&t.killReason==="user"){this.markTaskStopped(e,"Stopped by user.");return}if(t.status==="killed"&&t.killReason==="idle-timeout"){let s=t.getOutput(60).join(`
|
|
133
|
+
`);if(t.pendingPlanApproval){let a=await Q(this.sessionManager,{session:t.id,message:"Approved. Implement the plan.",approve:!0,userInitiated:!1});if(a.isError){this.markTaskFailed(e,a.text);return}let l=this.sessionManager.resolve(t.id);if(!l){this.markTaskFailed(e,"Failed to resolve the resumed goal task session after idle timeout plan approval.");return}this.attachSessionObservers(e,l),this.setTaskRunningWithSession(e,l),this.notifyIterationStatus(e,`🔄 [${e.name}] Goal task resumed after idle timeout`,l),this.scheduleTaskEvaluation(e.id,"idle-timeout-plan-resume",l.id);return}if(t.pendingInputState){let a=ia(s);if(!a){this.markTaskFailed(e,`Goal task was waiting for user input and cannot continue autonomously: ${he(s,24)||"The goal task hit idle timeout while waiting for user input."}`);return}await this.resumeAfterIdleTimeout(e,t,["The previous session hit idle timeout while waiting for a response.",`Use this response and continue the goal: ${a}`].join(`
|
|
133
134
|
|
|
134
|
-
`));return}await this.resumeAfterIdleTimeout(e,t,
|
|
135
|
-
`),a=
|
|
135
|
+
`));return}await this.resumeAfterIdleTimeout(e,t,sa(e));return}if(t.status==="killed"&&t.killReason!=="done"){this.markTaskFailed(e,`Underlying session was killed (${t.killReason}).`);return}if(e.loopMode==="ralph"){let s=t.getOutput(200).join(`
|
|
136
|
+
`),a=na(e.completionPromise);if(Wc(s,a)){if(e.verifierCommands.length===0){this.markTaskSucceeded(e,`Completion promise "${a}" detected in agent output.`);return}let g=await this.runVerifiers(e);if(e.lastVerifierSummary=g.summary,e.updatedAt=Date.now(),g.status==="pass"){this.markTaskSucceeded(e,[`Completion promise "${a}" detected.`,g.summary].join(`
|
|
136
137
|
`));return}if(e.lastVerifierFingerprint===g.fingerprint?e.repeatedFailureCount+=1:e.repeatedFailureCount=1,e.lastVerifierFingerprint=g.fingerprint,e.iteration+1>=e.maxIterations){this.markTaskFailed(e,[`Goal task emitted completion promise but verifiers still failed before hitting the iteration budget (${e.maxIterations}).`,g.summary].join(`
|
|
137
|
-
`));return}e.iteration+=1;let S=
|
|
138
|
-
`));return}e.iteration+=1;let u=
|
|
139
|
-
`));return}e.iteration+=1;let o=
|
|
140
|
-
`);return(n??"")+o}function Uc(n,e){let t=n.resumeWorktreeFrom??n.resumeSessionId;if(!t)return{};let r=e(t);if(!r)return{};let o=!!r.harness||!!r.backendRef,i=(()=>{if(r.workdir&&r.workdir!==r.worktreePath)return r.workdir;if(r.worktreePath){let a=Ct(r.worktreePath);return r.workdir&&a&&Wn(r.workdir,a)?r.workdir:a??r.workdir}return r.workdir})();if(n.worktreeStrategy||(r.worktreeLifecycle?.state==="pr_open"||r.worktreePrUrl?n.worktreeStrategy="auto-pr":r.worktreeStrategy&&(n.worktreeStrategy=r.worktreeStrategy)),!n.planApproval&&r.planApproval&&(n.planApproval=r.planApproval),!r.worktreePath){let a=o&&Ir(xt({persistedHarness:r.harness,backendRef:r.backendRef}));return{originalWorkdir:i,canCreateManagedWorktreeForResumeWithoutPersistedPath:a}}if(!r.worktreeBranch)throw new Error(`Cannot resume session "${t}": persisted worktree metadata is missing worktreeBranch.`);let s=Ir(xt({persistedHarness:r.harness,backendRef:r.backendRef}))&&!!r.backendRef?.worktreePath;if(Fc(r.worktreePath))return`${r.worktreePath}`,{actualWorkdir:r.worktreePath,originalWorkdir:i??r.worktreePath,worktreePath:r.worktreePath,worktreeBranchName:r.worktreeBranch,worktreeParentBranch:r.worktreeParentBranch};if(!i)return`${r.worktreePath}`,{clearedResumeSessionId:!!n.resumeSessionId,clearedResumeWorktreeFrom:!!n.resumeWorktreeFrom};if(s)return`${r.worktreePath}`,{actualWorkdir:i,originalWorkdir:i,worktreeBranchName:r.worktreeBranch,worktreeParentBranch:r.worktreeParentBranch,clearedResumeWorktreeFrom:!!n.resumeWorktreeFrom,restoredMissingNativeBackendWorktree:!0};try{Dt(i);let a=xr(i,r.worktreeBranch.replace(/^agent\//,""),{allowExistingBranch:!0});return`${r.worktreeBranch}${a}`,{actualWorkdir:a,originalWorkdir:i,worktreePath:a,worktreeBranchName:r.worktreeBranch,worktreeParentBranch:r.worktreeParentBranch}}catch(a){return`${oa(a)}`,{actualWorkdir:i,originalWorkdir:i,clearedResumeSessionId:!!n.resumeSessionId,clearedResumeWorktreeFrom:!!n.resumeWorktreeFrom,failedResumeWorktreeRestore:!0}}}function ia(n,e,t){Kc(n,t);let{actualWorkdir:r,originalWorkdir:o,worktreePath:i,worktreeBranchName:s,worktreeParentBranch:a,clearedResumeSessionId:l,clearedResumeWorktreeFrom:d,restoredMissingNativeBackendWorktree:u,canCreateManagedWorktreeForResumeWithoutPersistedPath:f,failedResumeWorktreeRestore:g}=Uc(n,t);l&&(n.resumeSessionId=void 0),d&&(n.resumeWorktreeFrom=void 0),r??=n.workdir,o??=n.workdir;let S=!!(n.resumeSessionId??n.resumeWorktreeFrom),y=n.worktreeStrategy??_.defaultWorktreeStrategy;y&&(n.worktreeStrategy=y),!S&&y&&y!=="off"&&Mt(o)&&(a??=oe(o));let c=qc(n),v=(!S||!!f)&&!u&&!i&&y&&y!=="off"&&!c;if(g&&y&&y!=="off")throw new Error(`Cannot launch session "${e}": worktree strategy "${y}" was requested, but no isolated worktree was prepared. Launch with worktree_strategy "off" only when running in the base checkout is intentional.`);if(c&&!Mt(o))throw new Error(`Cannot launch session "${e}": worktree strategy "${y}" requires a git worktree, but "${o}" is not a git repository.`);if(v&&Mt(o)){if(!Jo(o))throw new Error(`Cannot launch session "${e}": insufficient space for worktree creation.`);try{if(i=xr(o,e),r=i,s=oe(i),!s)throw new Error(`created worktree at ${i} but failed to resolve branch name`);`${i}`}catch(b){throw new Error(`Cannot launch session "${e}": worktree creation failed: ${oa(b)}`)}}else if(v)throw new Error(`Cannot launch session "${e}": worktree strategy "${y}" requires a git worktree, but "${o}" is not a git repository.`);if(S&&i&&(r=i),y&&y!=="off"&&!i)throw new Error(`Cannot launch session "${e}": worktree strategy "${y}" was requested, but no isolated worktree was prepared. Launch with worktree_strategy "off" only when running in the base checkout is intentional.`);return{actualWorkdir:r,originalWorkdir:o,effectiveSystemPrompt:i&&s?Vc(n.systemPrompt,o,i,s):n.systemPrompt,worktreePath:i,worktreeBranchName:s,worktreeParentBranch:a,clearedResumeSessionId:l,clearedResumeWorktreeFrom:d,restoredMissingNativeBackendWorktree:u,failedResumeWorktreeRestore:g}}var Fn=class{constructor(e){this.getPersistedSession=e}getPersistedSession;prepareSpawn(e,t){return ia(e,t,this.getPersistedSession)}hydrateSpawnedSession(e,t,r){r.worktreeStrategy&&r.worktreeStrategy!=="off"&&(e.originalWorkdir=t.originalWorkdir),e.worktreeParentBranch=t.worktreeParentBranch,t.worktreePath&&(e.worktreePath=t.worktreePath,e.worktreeBranch=t.worktreeBranchName,e.worktreeState="provisioned"),r.worktreePrTargetRepo&&(e.worktreePrTargetRepo=r.worktreePrTargetRepo)}};var Ln=class{constructor(e){this.deps=e}deps;applySessionPatch(e,t){let r=this.normalizeCompletionWakePatch(t),o=this.deps.store.getPersistedSession(e);o&&(Object.assign(o,r),this.deps.store.assertPersistedEntry(o));let i=this.findActiveSessionForRef(e,o);return i&&this.applyPatchToActiveSession(i,r),!o&&!i?!1:(o&&this.deps.store.saveIndex(),!0)}normalizeCompletionWakePatch(e){return"completionWakeSucceededAt"in e&&e.completionWakeSucceededAt||"completionWakeSkippedAt"in e&&e.completionWakeSkippedAt?{...e,completionWakeSummaryRequired:void 0}:e}matchesExistingSession(e,t){if(!t)return!1;let r=C(e),o=C(t);return!!(t.sessionId&&e.id===t.sessionId||o&&o===r||t?.harnessSessionId&&e.harnessSessionId===t.harnessSessionId||t?.name&&e.name===t.name)}findActiveSessionForRef(e,t){let r=this.deps.resolveSession(e);if(r)return r;for(let o of this.deps.sessions.values())if(D(o)===e||C(o)===e||o.harnessSessionId===e||this.matchesExistingSession(o,t))return o}applyPatchToActiveSession(e,t){this.applyControlStatePatch(e,t),this.applySessionMetadataPatch(e,t),this.applyWorktreeMetadataPatch(e,t)}applyControlStatePatch(e,t){let r={};if(this.copyControlPatchField(r,t,"lifecycle"),this.copyControlPatchField(r,t,"approvalState"),this.copyControlPatchField(r,t,"approvalExecutionState"),this.copyControlPatchField(r,t,"worktreeState"),this.copyControlPatchField(r,t,"runtimeState"),this.copyControlPatchField(r,t,"deliveryState"),this.copyControlPatchField(r,t,"requestedPermissionMode"),this.copyControlPatchField(r,t,"currentPermissionMode"),this.copyControlPatchField(r,t,"pendingPlanApproval"),this.copyControlPatchField(r,t,"planApprovalContext"),this.copyControlPatchField(r,t,"planDecisionVersion"),this.copyControlPatchField(r,t,"actionablePlanDecisionVersion"),this.copyControlPatchField(r,t,"canonicalPlanPromptVersion"),this.copyControlPatchField(r,t,"approvalPromptRequiredVersion"),this.copyControlPatchField(r,t,"approvalPromptVersion"),this.copyControlPatchField(r,t,"approvalPromptStatus"),this.copyControlPatchField(r,t,"approvalPromptTransport"),this.copyControlPatchField(r,t,"approvalPromptMessageKind"),this.copyControlPatchField(r,t,"approvalPromptLastAttemptAt"),this.copyControlPatchField(r,t,"approvalPromptDeliveredAt"),this.copyControlPatchField(r,t,"approvalPromptFailedAt"),this.copyControlPatchField(r,t,"planModeApproved"),this.copyControlPatchField(r,t,"pendingWorktreeDecisionSince"),typeof e.applyControlPatch=="function"){e.applyControlPatch(r);return}this.assignIfPresent(e,"lifecycle",t,"lifecycle"),this.assignIfPresent(e,"approvalState",t,"approvalState"),this.assignIfPresent(e,"approvalExecutionState",t,"approvalExecutionState"),this.assignIfPresent(e,"worktreeState",t,"worktreeState"),this.assignIfPresent(e,"runtimeState",t,"runtimeState"),this.assignIfPresent(e,"deliveryState",t,"deliveryState"),this.assignIfPresent(e,"requestedPermissionMode",t,"requestedPermissionMode"),this.assignIfPresent(e,"currentPermissionMode",t,"currentPermissionMode"),this.assignIfPresent(e,"pendingPlanApproval",t,"pendingPlanApproval"),this.assignIfPresent(e,"planApprovalContext",t,"planApprovalContext"),this.assignIfPresent(e,"planDecisionVersion",t,"planDecisionVersion"),this.assignIfPresent(e,"actionablePlanDecisionVersion",t,"actionablePlanDecisionVersion"),this.assignIfPresent(e,"canonicalPlanPromptVersion",t,"canonicalPlanPromptVersion"),this.assignIfPresent(e,"approvalPromptRequiredVersion",t,"approvalPromptRequiredVersion"),this.assignIfPresent(e,"approvalPromptVersion",t,"approvalPromptVersion"),this.assignIfPresent(e,"approvalPromptStatus",t,"approvalPromptStatus"),this.assignIfPresent(e,"approvalPromptTransport",t,"approvalPromptTransport"),this.assignIfPresent(e,"approvalPromptMessageKind",t,"approvalPromptMessageKind"),this.assignIfPresent(e,"approvalPromptLastAttemptAt",t,"approvalPromptLastAttemptAt"),this.assignIfPresent(e,"approvalPromptDeliveredAt",t,"approvalPromptDeliveredAt"),this.assignIfPresent(e,"approvalPromptFailedAt",t,"approvalPromptFailedAt"),this.setControlFieldIfPresent(e,"planModeApproved",t)}applySessionMetadataPatch(e,t){this.assignIfDefined(e,"approvalRationale",t.approvalRationale),this.assignIfDefined(e,"taskFlowMirror",t.taskFlowMirror)}applyWorktreeMetadataPatch(e,t){this.assignIfDefined(e,"worktreePath",t.worktreePath),this.assignIfDefined(e,"worktreeBranch",t.worktreeBranch),this.assignIfDefined(e,"worktreePrUrl",t.worktreePrUrl),this.assignIfDefined(e,"worktreePrNumber",t.worktreePrNumber),this.assignIfDefined(e,"worktreeMerged",t.worktreeMerged),this.assignIfDefined(e,"worktreeMergedAt",t.worktreeMergedAt),this.assignIfDefined(e,"worktreeDisposition",t.worktreeDisposition),this.assignIfDefined(e,"worktreePrTargetRepo",t.worktreePrTargetRepo),this.assignIfDefined(e,"worktreePushRemote",t.worktreePushRemote),this.assignIfDefined(e,"worktreeLifecycle",t.worktreeLifecycle),"autoMergeParentSessionId"in t&&(e.autoMergeParentSessionId=t.autoMergeParentSessionId),"autoMergeConflictResolutionAttemptCount"in t&&(e.autoMergeConflictResolutionAttemptCount=t.autoMergeConflictResolutionAttemptCount),"autoMergeResolverSessionId"in t&&(e.autoMergeResolverSessionId=t.autoMergeResolverSessionId)}assignIfDefined(e,t,r){r!==void 0&&(e[t]=r)}assignIfPresent(e,t,r,o){Object.hasOwn(r,o)&&(e[t]=r[o])}copyControlPatchField(e,t,r){Object.hasOwn(t,r)&&(e[r]=t[r])}setControlFieldIfPresent(e,t,r){Object.hasOwn(r,t)&&e.setControlField?.(t,r[t])}};var Gc=new Set(["starting","running"]),Bn=class{constructor(e,t){this.sessions=e;this.store=t}sessions;store;resolveActive(e){let t=this.sessions.get(e);return t||(this.findPreferredMatch(e,r=>r.name===e)??this.findPreferredMatch(e,r=>C(r)===e)??this.findPreferredMatch(e,r=>r.harnessSessionId===e))}getPersistedSession(e){return this.store.getPersistedSession(e)}resolveBackendConversationId(e){let t=this.resolveActive(e);return this.store.resolveBackendConversationId(e,t?C(t)??t.harnessSessionId:void 0)}findPreferredMatch(e,t){let r,o;for(let i of this.sessions.values())t(i,e)&&((!o||i.startedAt>o.startedAt)&&(o=i),Gc.has(i.status)&&(!r||i.startedAt>r.startedAt)&&(r=i));return r??o}};import{existsSync as jc}from"fs";var sa=new Set(["merged","released","pr_open","dismissed","cleanup_failed"]),Kn=class{constructor(e){this.deps=e}deps;async plan(e){let t=D(e)??e.harnessSessionId;if(this.deps.isAlreadyMerged(t))return`${e.name}`,{kind:"skip",result:{notificationSent:!0,worktreeRemoved:!1}};let r=sa.has(e.worktreeState)?e.worktreeState:e.worktreeLifecycle?.state&&sa.has(e.worktreeLifecycle.state)?e.worktreeLifecycle.state:void 0;if(r&&!(r==="pr_open"&&e.worktreeStrategy==="auto-pr"))return`${e.name}${e.worktreeLifecycle?.state??e.worktreeState}`,{kind:"skip",result:{notificationSent:!0,worktreeRemoved:!1}};if(e.status!=="completed")return{kind:"skip",result:{notificationSent:!1,worktreeRemoved:!1}};if(!this.deps.shouldRunWorktreeStrategy(e))return`${e.name}${e.phase}`,{kind:"skip",result:{notificationSent:!1,worktreeRemoved:!1}};let o=e.worktreeStrategy;if(!o||o==="off"||o==="manual")return{kind:"skip",result:{notificationSent:!1,worktreeRemoved:!1}};let i=e.worktreePath,s=this.deps.resolveWorktreeRepoDir(e.originalWorkdir,i),a=e.worktreeBranch;if(!s)return{kind:"notify",label:"worktree-missing-repo-dir",message:`⚠️ [${e.name}] Cannot determine the original repo for worktree ${i}. Manual inspection is required.`};if(!a)return{kind:"notify",label:"worktree-no-branch-name",message:`⚠️ [${e.name}] Cannot determine branch name for worktree ${i}. The worktree may have been removed or is in detached HEAD state. Manual cleanup may be needed.`};let l=L(e);if(l&&!jc(i))return{kind:"no-change",repoDir:s,worktreePath:i,branchName:a,nativeBackendWorktree:l};let d=e.worktreeBaseBranch??Ee(s),u=this.deps.getWorktreeCompletionState(s,i,a,d);if(u==="no-change")return{kind:"no-change",repoDir:s,worktreePath:i,branchName:a,nativeBackendWorktree:l};if(u==="merged")return{kind:"merged",repoDir:s,worktreePath:i,branchName:a,nativeBackendWorktree:l};if(u==="released")return{kind:"released",repoDir:s,worktreePath:i,branchName:a,nativeBackendWorktree:l,reasons:["merge_noop_content_already_on_base"]};if(u==="base-advanced")return{kind:"notify",label:"worktree-no-commits-ahead",message:`⚠️ [${e.name}] Auto-merge: branch '${a}' has no commits ahead of '${d}', but '${d}' has new commits — commits likely landed outside the worktree branch. Verify that commits were not made directly to '${d}' instead of the worktree branch. Worktree: ${i}`};if(u==="dirty-uncommitted")return{kind:"dirty-uncommitted",worktreePath:i,branchName:a,baseBranch:d};let f=Ne(s,a,d);if(!f)return`${a}`,{kind:"skip",result:{notificationSent:!1,worktreeRemoved:!1}};let g=e.repoIntegrationPolicy?void 0:this.deps.resolveRepoPolicy?.(s),S=e.repoIntegrationPolicy??g?.policy,y=e.repoIntegrationPolicy?this.deps.isPrAvailable(s):g?.prAvailable??this.deps.isPrAvailable(s),c=cs({requestedStrategy:o,policy:S,prAvailable:y,existingOpenPr:!1});return c.strategy?{kind:"decision",strategy:c.strategy,policy:S,policyReason:c.reason,policyBlocked:c.blocked,allowedActions:c.allowedActions,repoDir:s,worktreePath:i,branchName:a,baseBranch:d,diffSummary:f,sessionRef:t??void 0}:{kind:"skip",result:{notificationSent:!1,worktreeRemoved:!1}}}};function ye(n){let e=[];typeof n.costUsd=="number"&&Number.isFinite(n.costUsd)&&e.push(`$${n.costUsd.toFixed(2)}`);let t=zc(n);t!==void 0&&e.push(se(t));let r=ge({harness:n.harnessName??n.harness,model:n.model});return r&&e.push(...r.split(" | ")),e.length>0?` | ${e.join(" | ")}`:""}function zc(n){if(typeof n.duration=="number"&&Number.isFinite(n.duration)&&n.duration>=0)return n.duration;if(typeof n.createdAt=="number"&&Number.isFinite(n.createdAt)&&typeof n.completedAt=="number"&&Number.isFinite(n.completedAt)&&n.completedAt>=n.createdAt)return n.completedAt-n.createdAt}function Hc(n){if(n.requestedPermissionMode==="plan"){if(n.approvalExecutionState==="approved_then_implemented"){if(n.planApproval==="ask"&&n.approvalPromptStatus==="delivered"&&n.approvalPromptMessageKind==="canonical_buttons"&&n.approvalPromptDeliveredAt)return"Approval interpretation: canonical Approve/Revise/Reject buttons were delivered, and implementation after approval was expected.";if(n.approvalState==="approved")return"Approval interpretation: explicit plan approval was recorded before implementation, so leaving plan-only mode was expected."}if(n.approvalExecutionState==="implemented_without_required_approval")return"Approval interpretation: implementation left plan-only mode without a recorded approval."}}function Qc(){return/https?:\/\/github\.com\/[^\s)]+\/[^\s)]+\/pull\/(\d+)(?=[\s).,;:]|$)/gi}function Yc(){return/https?:\/\/\S+/gi}function qn(n){let e=Hc(n);return[`Requested permission mode: ${n.requestedPermissionMode??"unknown"}`,`Effective permission mode: ${n.currentPermissionMode??"unknown"}`,`Deterministic approval/execution state: ${n.approvalExecutionState??"unknown"}`,...e?[e]:[]]}function go(n){switch(n){case"user":return"Stopped by user";case"shutdown":return"Stopped by shutdown";case"startup-timeout":return"Stopped by startup timeout";case"unknown":case void 0:return"Stopped unexpectedly";default:return"Stopped"}}function Jc(){return{requiresShortFactualSummary:!0,owner:"agent",appliesToOrdinaryTerminalCompletions:!0}}function Xc(){return{requiresShortFactualSummary:!0,owner:"agent",appliesToWorktreeTerminalOutcomes:!0}}function Zc(){return{requiresShortFactualSummary:!0,owner:"agent",appliesToGoalTaskCompletions:!0}}function ho(n){let{sessionId:e,canonicalStatusDetail:t,canonicalStatusDelivered:r=!0,hasOriginRouteBlock:o=!1}=n,i=o?["7. Before sending that follow-up, honor the Session origin route block above. If originRoute differs from the current chat, do NOT use a plain final assistant reply; use a routed send path that preserves provider/target/threadId."]:[],s=o?8:7,a=o?9:8;return["[ACTION REQUIRED] Follow your autonomy rules for session completion:",`1. Use agent_output(session='${e}', full=true) to read the full result.`,"2. If this is part of a multi-phase pipeline, launch the next phase NOW — do not wait for user input.",`3. ${t??(r?"The plugin already sent the canonical completion status to the user.":"The plugin did not confirm delivery of the canonical completion status to the user.")}`,"4. Treat the completed session output as source material, not visible delivery. A meaningful final summary inside agent_output is not enough to skip, because the user may only have seen the plugin's terse status line.","5. If the visible result is only the plugin's terse status line, use the full output to send the user one short factual completion summary for this completed session. Do this even when agent_output already contains a good final summary.","6. This requirement applies to ordinary terminal/manual completions too, not just delegated worktree decisions.",...i,`${s}. That follow-up belongs to you alone; keep it brief, concrete, and grounded in reliable result data. Send at most one orchestrator-owned human summary for this terminal/worktree outcome.`,...r?[`${a}. Do NOT repeat the plugin's status line, and do NOT rely on the plugin to summarize the completed work for you.`]:[`${a}. Because canonical status delivery was not confirmed, account for that gap yourself when you follow up; do NOT assume the plugin already reached the user.`],`${a+1}. Send a normal concise final response after any routed follow-up is visibly sent. Do not use marker text, do not answer NO_REPLY, and do not ask the user whether they want the summary.`]}function ep(n){let{contract:e,canonicalStatusDelivered:t}=n;return["Completion diagnostics:",`- Canonical completion status delivered to user: ${t?"yes":"no"}`,`- Plugin requested short factual follow-up summary: ${e.requiresShortFactualSummary?"yes":"no"}`,`- Contract applies to ordinary terminal/manual completions: ${e.appliesToOrdinaryTerminalCompletions?"yes":"no"}`]}function yo(n){let{session:e,originThreadLine:t,preview:r}=n,o=!!t.trim(),i=Jc(),s=a=>["Coding agent session completed.",`Name: ${e.name} | ID: ${e.id}`,`Status: ${e.status}`,...o?[t]:[],...qn(e),"","Output preview:",r,"",...ep({contract:i,canonicalStatusDelivered:a}),"",...ho({sessionId:e.id,canonicalStatusDelivered:a,hasOriginRouteBlock:o})].join(`
|
|
141
|
-
`);return{userMessage:`✅ [${e.name}] Completed${ye(e)}`,wakeMessageOnNotifySuccess:s(!0),wakeMessageOnNotifyFailed:s(!1),followupContract:i}}function
|
|
142
|
-
`)}function
|
|
143
|
-
`)}function
|
|
144
|
-
`),wakeMessage:["Coding agent session failed.",`Name: ${e.name} | ID: ${e.id}`,`Status: ${e.status}`,t,...
|
|
145
|
-
`),buttons:s}}function
|
|
146
|
-
`)}}function
|
|
147
|
-
`)}function
|
|
148
|
-
`)}function
|
|
149
|
-
`)}var
|
|
150
|
-
`)){let
|
|
151
|
-
`)
|
|
152
|
-
|
|
153
|
-
|
|
138
|
+
`));return}e.iteration+=1;let S=Oc(e,g),y=Mn({output:s,verifierSummary:g.summary,completionPromise:a,completionDetected:!0});try{let c=await this.resumeTaskSession(e,S,t);this.setTaskRunningWithSession(e,c),this.notifyIterationStatus(e,`🔁 [${e.name}] Completion claimed but verifiers still failed`,void 0,y),this.scheduleTaskEvaluation(e.id,"ralph-verifier-resume",c.id)}catch(c){this.markTaskFailed(e,`Failed to resume the Ralph goal task after verifier failure: ${Ae(c)}`)}return}let d=Dc(s);if(e.lastVerifierFingerprint===d?e.repeatedFailureCount+=1:e.repeatedFailureCount=1,e.lastVerifierFingerprint=d,e.iteration+1>=e.maxIterations){this.markTaskFailed(e,[`Completion promise "${a}" was not emitted before hitting the iteration budget (${e.maxIterations}).`,"Latest output:",he(s,20)||"(no output)"].join(`
|
|
139
|
+
`));return}e.iteration+=1;let u=Nc(e,s),m=Mn({output:s,completionPromise:a,completionDetected:!1});try{let g=await this.resumeTaskSession(e,u,t);this.setTaskRunningWithSession(e,g),this.notifyIterationStatus(e,`🔁 [${e.name}] Continued`,void 0,m),this.scheduleTaskEvaluation(e.id,"ralph-continue",g.id)}catch(g){this.markTaskFailed(e,`Failed to continue the Ralph goal task: ${Ae(g)}`)}return}let r=await this.runVerifiers(e);if(e.lastVerifierSummary=r.summary,e.updatedAt=Date.now(),r.status==="pass"){this.markTaskSucceeded(e,r.summary);return}if(e.lastVerifierFingerprint===r.fingerprint?e.repeatedFailureCount+=1:e.repeatedFailureCount=1,e.lastVerifierFingerprint=r.fingerprint,e.iteration+1>=e.maxIterations){this.markTaskFailed(e,[`Verifier did not pass before hitting the iteration budget (${e.maxIterations}).`,r.summary].join(`
|
|
140
|
+
`));return}e.iteration+=1;let o=Ec(e,r),i=Mn({verifierSummary:r.summary});try{let s=await this.resumeTaskSession(e,o,t);this.setTaskRunningWithSession(e,s),this.notifyIterationStatus(e,`🔁 [${e.name}] Repair iteration started after verifier failure`,void 0,i),this.scheduleTaskEvaluation(e.id,"repair-resume",s.id)}catch(s){this.markTaskFailed(e,`Failed to resume the goal task: ${Ae(s)}`)}}async evaluateTask(e,t,r){this.restorePromise&&await this.restorePromise;let o=this.store.get(e);o&&await this.reconcileTask(o,t,r)}async reconcileTask(e,t="manual",r){if(!(e.status==="succeeded"||e.status==="failed"||e.status==="stopped")){if(this.inFlight.has(e.id)){this.dirtyEvaluations.add(e.id),!this.dirtyEvaluationSessionIds.get(e.id)&&r?this.dirtyEvaluationSessionIds.set(e.id,r):this.dirtyEvaluationSessionIds.has(e.id)||this.dirtyEvaluationSessionIds.set(e.id,r);return}this.inFlight.add(e.id);try{if(r&&e.sessionId&&e.sessionId!==r){let i=this.sessionManager.resolve(r);if(i&&(i.status==="completed"||i.status==="failed"||i.status==="killed"))return}let o=r?this.sessionManager.resolve(r)??(e.sessionId?this.sessionManager.resolve(e.sessionId):void 0):e.sessionId?this.sessionManager.resolve(e.sessionId):void 0;if(e.status==="waiting_for_user"){this.markTaskFailed(e,"Goal task was waiting for user input and cannot continue autonomously");return}if(ra(e)){this.markTaskFailed(e,Bn());return}if(!o){this.markTaskFailed(e,"Underlying session could not be found.");return}if(e.sessionName=o.name,e.harnessSessionId=o.harnessSessionId,e.route=o.route??e.route,e.updatedAt=Date.now(),this.store.upsert(e),o.status==="starting"||o.status==="running"){await this.handleRunningSession(e,o);return}await this.handleTerminalSession(e,o)}finally{if(this.inFlight.delete(e.id),this.dirtyEvaluations.has(e.id)){this.dirtyEvaluations.delete(e.id);let o=this.dirtyEvaluationSessionIds.get(e.id);this.dirtyEvaluationSessionIds.delete(e.id),this.scheduleTaskEvaluation(e.id,`${t}:dirty`,o)}}}}};import{createHash as hl}from"crypto";import{existsSync as Kc,realpathSync as qc}from"fs";import{resolve as Vc}from"path";function ca(n){if(!n)return;let e=Vc(n);try{if(Kc(e))return qc.native(e)}catch{}return e}function qn(n,e){return!n||!e?!1:ca(n)===ca(e)}import{existsSync as Uc}from"fs";function Gc(n){if(!(!n.provider||!n.target))return n.accountId?`${n.provider}|${n.accountId}|${n.target}`:`${n.provider}|${n.target}`}function jc(n){return Ce(Z({route:n.route,originChannel:n.originChannel,originThreadId:n.originThreadId,originSessionKey:n.originSessionKey}))}function zc(n,e){if(jc(n))return;let t=[n.resumeSessionId,n.resumeWorktreeFrom].filter(r=>!!r?.trim());for(let r of t){let o=e(r);if(!o)continue;let i=Z({route:o.route,originChannel:o.originChannel,originThreadId:o.originThreadId,originSessionKey:o.originSessionKey});if(Ce(i)){n.route={...i},n.originChannel=o.originChannel??Gc(i),n.originThreadId=o.originThreadId??i.threadId,n.originSessionKey=o.originSessionKey??i.sessionKey,n.originAgentId??=o.originAgentId;return}}}function pa(n){return n instanceof Error?n.message:String(n)}function Hc(n){let e=n.worktreeStrategy??_.defaultWorktreeStrategy;return!!e&&e!=="off"&&oi($t({harnessName:n.harness??Y(),backendRef:n.backendRef}))}function Qc(n,e,t,r){let o=["","You are working in a git worktree.",`Worktree path: ${t}`,`Branch: ${r}`,"",`IMPORTANT: ALL file edits must be made within this worktree at ${t}.`,`Do NOT edit files directly in ${e} (the original workspace).`,`If your task references files by absolute path under ${e}, rewrite those`,"paths relative to your current working directory. For example:",` "${e}/src/file.py" → use relative path "src/file.py"`,"","Before finishing, run `git status --short` in this worktree.","If you made actual task changes, commit all of them to this branch before finishing.","Use `git add` and `git commit`. If no repository changes are needed, leave the worktree clean before finishing.","Do NOT run `git checkout`, `git switch`, or `git reset --hard` as these will detach or corrupt the worktree HEAD.","","When making changes, please note:","- Do NOT commit planning documents, investigation notes, or analysis artifacts to this branch","- Only commit actual code, configuration, tests, and documentation changes that were explicitly requested as part of the task"].join(`
|
|
141
|
+
`);return(n??"")+o}function Yc(n,e){let t=n.resumeWorktreeFrom??n.resumeSessionId;if(!t)return{};let r=e(t);if(!r)return{};let o=!!r.harness||!!r.backendRef,i=(()=>{if(r.workdir&&r.workdir!==r.worktreePath)return r.workdir;if(r.worktreePath){let a=Et(r.worktreePath);return r.workdir&&a&&qn(r.workdir,a)?r.workdir:a??r.workdir}return r.workdir})();if(n.worktreeStrategy||(r.worktreeLifecycle?.state==="pr_open"||r.worktreePrUrl?n.worktreeStrategy="auto-pr":r.worktreeStrategy&&(n.worktreeStrategy=r.worktreeStrategy)),!n.planApproval&&r.planApproval&&(n.planApproval=r.planApproval),!r.worktreePath){let a=o&&_r($t({persistedHarness:r.harness,backendRef:r.backendRef}));return{originalWorkdir:i,canCreateManagedWorktreeForResumeWithoutPersistedPath:a}}if(!r.worktreeBranch)throw new Error(`Cannot resume session "${t}": persisted worktree metadata is missing worktreeBranch.`);let s=_r($t({persistedHarness:r.harness,backendRef:r.backendRef}))&&!!r.backendRef?.worktreePath;if(Uc(r.worktreePath))return`${r.worktreePath}`,{actualWorkdir:r.worktreePath,originalWorkdir:i??r.worktreePath,worktreePath:r.worktreePath,worktreeBranchName:r.worktreeBranch,worktreeParentBranch:r.worktreeParentBranch};if(!i)return`${r.worktreePath}`,{clearedResumeSessionId:!!n.resumeSessionId,clearedResumeWorktreeFrom:!!n.resumeWorktreeFrom};if(s)return`${r.worktreePath}`,{actualWorkdir:i,originalWorkdir:i,worktreeBranchName:r.worktreeBranch,worktreeParentBranch:r.worktreeParentBranch,clearedResumeWorktreeFrom:!!n.resumeWorktreeFrom,restoredMissingNativeBackendWorktree:!0};try{Wt(i);let a=$r(i,r.worktreeBranch.replace(/^agent\//,""),{allowExistingBranch:!0});return`${r.worktreeBranch}${a}`,{actualWorkdir:a,originalWorkdir:i,worktreePath:a,worktreeBranchName:r.worktreeBranch,worktreeParentBranch:r.worktreeParentBranch}}catch(a){return`${pa(a)}`,{actualWorkdir:i,originalWorkdir:i,clearedResumeSessionId:!!n.resumeSessionId,clearedResumeWorktreeFrom:!!n.resumeWorktreeFrom,failedResumeWorktreeRestore:!0}}}function ma(n,e,t){n.worktreeBaseBranch!==void 0&&ei(n.worktreeBaseBranch),zc(n,t);let{actualWorkdir:r,originalWorkdir:o,worktreePath:i,worktreeBranchName:s,worktreeParentBranch:a,clearedResumeSessionId:l,clearedResumeWorktreeFrom:d,restoredMissingNativeBackendWorktree:u,canCreateManagedWorktreeForResumeWithoutPersistedPath:m,failedResumeWorktreeRestore:g}=Yc(n,t);l&&(n.resumeSessionId=void 0),d&&(n.resumeWorktreeFrom=void 0),r??=n.workdir,o??=n.workdir;let S=!!(n.resumeSessionId??n.resumeWorktreeFrom),y=n.worktreeStrategy??_.defaultWorktreeStrategy;y&&(n.worktreeStrategy=y),!S&&y&&y!=="off"&&Nt(o)&&(a??=oe(o));let c=Hc(n),P=(!S||!!m)&&!u&&!i&&y&&y!=="off"&&!c;if(g&&y&&y!=="off")throw new Error(`Cannot launch session "${e}": worktree strategy "${y}" was requested, but no isolated worktree was prepared. Launch with worktree_strategy "off" only when running in the base checkout is intentional.`);if(c&&!Nt(o))throw new Error(`Cannot launch session "${e}": worktree strategy "${y}" requires a git worktree, but "${o}" is not a git repository.`);if(P&&Nt(o)){if(!ii(o))throw new Error(`Cannot launch session "${e}": insufficient space for worktree creation.`);try{if(i=$r(o,e),r=i,s=oe(i),!s)throw new Error(`created worktree at ${i} but failed to resolve branch name`);`${i}`}catch(b){throw new Error(`Cannot launch session "${e}": worktree creation failed: ${pa(b)}`)}}else if(P)throw new Error(`Cannot launch session "${e}": worktree strategy "${y}" requires a git worktree, but "${o}" is not a git repository.`);if(S&&i&&(r=i),y&&y!=="off"&&!i)throw new Error(`Cannot launch session "${e}": worktree strategy "${y}" was requested, but no isolated worktree was prepared. Launch with worktree_strategy "off" only when running in the base checkout is intentional.`);return{actualWorkdir:r,originalWorkdir:o,effectiveSystemPrompt:i&&s?Qc(n.systemPrompt,o,i,s):n.systemPrompt,worktreePath:i,worktreeBranchName:s,worktreeParentBranch:a,clearedResumeSessionId:l,clearedResumeWorktreeFrom:d,restoredMissingNativeBackendWorktree:u,failedResumeWorktreeRestore:g}}var Vn=class{constructor(e){this.getPersistedSession=e}getPersistedSession;prepareSpawn(e,t){return ma(e,t,this.getPersistedSession)}hydrateSpawnedSession(e,t,r){r.worktreeStrategy&&r.worktreeStrategy!=="off"&&(e.originalWorkdir=t.originalWorkdir),e.worktreeParentBranch=t.worktreeParentBranch,t.worktreePath&&(e.worktreePath=t.worktreePath,e.worktreeBranch=t.worktreeBranchName,e.worktreeState="provisioned"),r.worktreePrTargetRepo&&(e.worktreePrTargetRepo=r.worktreePrTargetRepo)}};var Un=class{constructor(e){this.deps=e}deps;applySessionPatch(e,t){let r=this.normalizeCompletionWakePatch(t),o=this.deps.store.getPersistedSession(e);o&&(Object.assign(o,r),this.deps.store.assertPersistedEntry(o));let i=this.findActiveSessionForRef(e,o);return i&&this.applyPatchToActiveSession(i,r),!o&&!i?!1:(o&&this.deps.store.saveIndex(),!0)}normalizeCompletionWakePatch(e){return"completionWakeSucceededAt"in e&&e.completionWakeSucceededAt||"completionWakeSkippedAt"in e&&e.completionWakeSkippedAt?{...e,completionWakeSummaryRequired:void 0}:e}matchesExistingSession(e,t){if(!t)return!1;let r=C(e),o=C(t);return!!(t.sessionId&&e.id===t.sessionId||o&&o===r||t?.harnessSessionId&&e.harnessSessionId===t.harnessSessionId||t?.name&&e.name===t.name)}findActiveSessionForRef(e,t){let r=this.deps.resolveSession(e);if(r)return r;for(let o of this.deps.sessions.values())if(D(o)===e||C(o)===e||o.harnessSessionId===e||this.matchesExistingSession(o,t))return o}applyPatchToActiveSession(e,t){this.applyControlStatePatch(e,t),this.applySessionMetadataPatch(e,t),this.applyWorktreeMetadataPatch(e,t)}applyControlStatePatch(e,t){let r={};if(this.copyControlPatchField(r,t,"lifecycle"),this.copyControlPatchField(r,t,"approvalState"),this.copyControlPatchField(r,t,"approvalExecutionState"),this.copyControlPatchField(r,t,"worktreeState"),this.copyControlPatchField(r,t,"runtimeState"),this.copyControlPatchField(r,t,"deliveryState"),this.copyControlPatchField(r,t,"requestedPermissionMode"),this.copyControlPatchField(r,t,"currentPermissionMode"),this.copyControlPatchField(r,t,"pendingPlanApproval"),this.copyControlPatchField(r,t,"planApprovalContext"),this.copyControlPatchField(r,t,"planDecisionVersion"),this.copyControlPatchField(r,t,"actionablePlanDecisionVersion"),this.copyControlPatchField(r,t,"canonicalPlanPromptVersion"),this.copyControlPatchField(r,t,"approvalPromptRequiredVersion"),this.copyControlPatchField(r,t,"approvalPromptVersion"),this.copyControlPatchField(r,t,"approvalPromptStatus"),this.copyControlPatchField(r,t,"approvalPromptTransport"),this.copyControlPatchField(r,t,"approvalPromptMessageKind"),this.copyControlPatchField(r,t,"approvalPromptLastAttemptAt"),this.copyControlPatchField(r,t,"approvalPromptDeliveredAt"),this.copyControlPatchField(r,t,"approvalPromptFailedAt"),this.copyControlPatchField(r,t,"planModeApproved"),this.copyControlPatchField(r,t,"pendingWorktreeDecisionSince"),typeof e.applyControlPatch=="function"){e.applyControlPatch(r);return}this.assignIfPresent(e,"lifecycle",t,"lifecycle"),this.assignIfPresent(e,"approvalState",t,"approvalState"),this.assignIfPresent(e,"approvalExecutionState",t,"approvalExecutionState"),this.assignIfPresent(e,"worktreeState",t,"worktreeState"),this.assignIfPresent(e,"runtimeState",t,"runtimeState"),this.assignIfPresent(e,"deliveryState",t,"deliveryState"),this.assignIfPresent(e,"requestedPermissionMode",t,"requestedPermissionMode"),this.assignIfPresent(e,"currentPermissionMode",t,"currentPermissionMode"),this.assignIfPresent(e,"pendingPlanApproval",t,"pendingPlanApproval"),this.assignIfPresent(e,"planApprovalContext",t,"planApprovalContext"),this.assignIfPresent(e,"planDecisionVersion",t,"planDecisionVersion"),this.assignIfPresent(e,"actionablePlanDecisionVersion",t,"actionablePlanDecisionVersion"),this.assignIfPresent(e,"canonicalPlanPromptVersion",t,"canonicalPlanPromptVersion"),this.assignIfPresent(e,"approvalPromptRequiredVersion",t,"approvalPromptRequiredVersion"),this.assignIfPresent(e,"approvalPromptVersion",t,"approvalPromptVersion"),this.assignIfPresent(e,"approvalPromptStatus",t,"approvalPromptStatus"),this.assignIfPresent(e,"approvalPromptTransport",t,"approvalPromptTransport"),this.assignIfPresent(e,"approvalPromptMessageKind",t,"approvalPromptMessageKind"),this.assignIfPresent(e,"approvalPromptLastAttemptAt",t,"approvalPromptLastAttemptAt"),this.assignIfPresent(e,"approvalPromptDeliveredAt",t,"approvalPromptDeliveredAt"),this.assignIfPresent(e,"approvalPromptFailedAt",t,"approvalPromptFailedAt"),this.setControlFieldIfPresent(e,"planModeApproved",t)}applySessionMetadataPatch(e,t){this.assignIfDefined(e,"approvalRationale",t.approvalRationale),this.assignIfDefined(e,"taskFlowMirror",t.taskFlowMirror)}applyWorktreeMetadataPatch(e,t){this.assignIfDefined(e,"worktreePath",t.worktreePath),this.assignIfDefined(e,"worktreeBranch",t.worktreeBranch),this.assignIfDefined(e,"worktreePrUrl",t.worktreePrUrl),this.assignIfDefined(e,"worktreePrNumber",t.worktreePrNumber),this.assignIfDefined(e,"worktreeMerged",t.worktreeMerged),this.assignIfDefined(e,"worktreeMergedAt",t.worktreeMergedAt),this.assignIfDefined(e,"worktreeDisposition",t.worktreeDisposition),this.assignIfDefined(e,"worktreePrTargetRepo",t.worktreePrTargetRepo),this.assignIfDefined(e,"worktreePushRemote",t.worktreePushRemote),this.assignIfDefined(e,"worktreeLifecycle",t.worktreeLifecycle),"autoMergeParentSessionId"in t&&(e.autoMergeParentSessionId=t.autoMergeParentSessionId),"autoMergeConflictResolutionAttemptCount"in t&&(e.autoMergeConflictResolutionAttemptCount=t.autoMergeConflictResolutionAttemptCount),"autoMergeResolverSessionId"in t&&(e.autoMergeResolverSessionId=t.autoMergeResolverSessionId)}assignIfDefined(e,t,r){r!==void 0&&(e[t]=r)}assignIfPresent(e,t,r,o){Object.hasOwn(r,o)&&(e[t]=r[o])}copyControlPatchField(e,t,r){Object.hasOwn(t,r)&&(e[r]=t[r])}setControlFieldIfPresent(e,t,r){Object.hasOwn(r,t)&&e.setControlField?.(t,r[t])}};var Jc=new Set(["starting","running"]),Gn=class{constructor(e,t){this.sessions=e;this.store=t}sessions;store;resolveActive(e){let t=this.sessions.get(e);return t||(this.findPreferredMatch(e,r=>r.name===e)??this.findPreferredMatch(e,r=>C(r)===e)??this.findPreferredMatch(e,r=>r.harnessSessionId===e))}getPersistedSession(e){return this.store.getPersistedSession(e)}resolveBackendConversationId(e){let t=this.resolveActive(e);return this.store.resolveBackendConversationId(e,t?C(t)??t.harnessSessionId:void 0)}findPreferredMatch(e,t){let r,o;for(let i of this.sessions.values())t(i,e)&&((!o||i.startedAt>o.startedAt)&&(o=i),Jc.has(i.status)&&(!r||i.startedAt>r.startedAt)&&(r=i));return r??o}};import{existsSync as Xc}from"fs";var fa=new Set(["merged","released","pr_open","dismissed","cleanup_failed"]),jn=class{constructor(e){this.deps=e}deps;async plan(e){let t=D(e)??e.harnessSessionId;if(this.deps.isAlreadyMerged(t))return`${e.name}`,{kind:"skip",result:{notificationSent:!0,worktreeRemoved:!1}};let r=fa.has(e.worktreeState)?e.worktreeState:e.worktreeLifecycle?.state&&fa.has(e.worktreeLifecycle.state)?e.worktreeLifecycle.state:void 0;if(r&&!(r==="pr_open"&&e.worktreeStrategy==="auto-pr"))return`${e.name}${e.worktreeLifecycle?.state??e.worktreeState}`,{kind:"skip",result:{notificationSent:!0,worktreeRemoved:!1}};if(e.status!=="completed")return{kind:"skip",result:{notificationSent:!1,worktreeRemoved:!1}};if(!this.deps.shouldRunWorktreeStrategy(e))return`${e.name}${e.phase}`,{kind:"skip",result:{notificationSent:!1,worktreeRemoved:!1}};let o=e.worktreeStrategy;if(!o||o==="off"||o==="manual")return{kind:"skip",result:{notificationSent:!1,worktreeRemoved:!1}};let i=e.worktreePath,s=this.deps.resolveWorktreeRepoDir(e.originalWorkdir,i),a=e.worktreeBranch;if(!s)return{kind:"notify",label:"worktree-missing-repo-dir",message:`⚠️ [${e.name}] Cannot determine the original repo for worktree ${i}. Manual inspection is required.`};if(!a)return{kind:"notify",label:"worktree-no-branch-name",message:`⚠️ [${e.name}] Cannot determine branch name for worktree ${i}. The worktree may have been removed or is in detached HEAD state. Manual cleanup may be needed.`};let l=L(e);if(l&&!Xc(i))return{kind:"no-change",repoDir:s,worktreePath:i,branchName:a,nativeBackendWorktree:l};let d=e.worktreeBaseBranch??Ne(s),u=this.deps.getWorktreeCompletionState(s,i,a,d);if(u==="no-change")return{kind:"no-change",repoDir:s,worktreePath:i,branchName:a,nativeBackendWorktree:l};if(u==="merged")return{kind:"merged",repoDir:s,worktreePath:i,branchName:a,nativeBackendWorktree:l};if(u==="released")return{kind:"released",repoDir:s,worktreePath:i,branchName:a,nativeBackendWorktree:l,reasons:["merge_noop_content_already_on_base"]};if(u==="base-advanced")return{kind:"notify",label:"worktree-no-commits-ahead",message:`⚠️ [${e.name}] Auto-merge: branch '${a}' has no commits ahead of '${d}', but '${d}' has new commits — commits likely landed outside the worktree branch. Verify that commits were not made directly to '${d}' instead of the worktree branch. Worktree: ${i}`};if(u==="dirty-uncommitted")return{kind:"dirty-uncommitted",worktreePath:i,branchName:a,baseBranch:d};let m=Oe(s,a,d);if(!m)return`${a}`,{kind:"skip",result:{notificationSent:!1,worktreeRemoved:!1}};let g=e.repoIntegrationPolicy?void 0:this.deps.resolveRepoPolicy?.(s),S=e.repoIntegrationPolicy??g?.policy,y=e.repoIntegrationPolicy?this.deps.isPrAvailable(s):g?.prAvailable??this.deps.isPrAvailable(s),c=ks({requestedStrategy:o,policy:S,prAvailable:y,existingOpenPr:!1});return c.strategy?{kind:"decision",strategy:c.strategy,policy:S,policyReason:c.reason,policyBlocked:c.blocked,allowedActions:c.allowedActions,repoDir:s,worktreePath:i,branchName:a,baseBranch:d,diffSummary:m,sessionRef:t??void 0}:{kind:"skip",result:{notificationSent:!1,worktreeRemoved:!1}}}};function ye(n){let e=[];typeof n.costUsd=="number"&&Number.isFinite(n.costUsd)&&e.push(`$${n.costUsd.toFixed(2)}`);let t=Zc(n);t!==void 0&&e.push(se(t));let r=ge({harness:n.harnessName??n.harness,model:n.model,reasoningEffort:n.reasoningEffort});return r&&e.push(...r.split(" | ")),e.length>0?` | ${e.join(" | ")}`:""}function Zc(n){if(typeof n.duration=="number"&&Number.isFinite(n.duration)&&n.duration>=0)return n.duration;if(typeof n.createdAt=="number"&&Number.isFinite(n.createdAt)&&typeof n.completedAt=="number"&&Number.isFinite(n.completedAt)&&n.completedAt>=n.createdAt)return n.completedAt-n.createdAt}function ep(n){if(n.requestedPermissionMode==="plan"){if(n.approvalExecutionState==="approved_then_implemented"){if(n.planApproval==="ask"&&n.approvalPromptStatus==="delivered"&&n.approvalPromptMessageKind==="canonical_buttons"&&n.approvalPromptDeliveredAt)return"Approval interpretation: canonical Approve/Revise/Reject buttons were delivered, and implementation after approval was expected.";if(n.approvalState==="approved")return"Approval interpretation: explicit plan approval was recorded before implementation, so leaving plan-only mode was expected."}if(n.approvalExecutionState==="implemented_without_required_approval")return"Approval interpretation: implementation left plan-only mode without a recorded approval."}}function tp(){return/https?:\/\/github\.com\/[^\s)]+\/[^\s)]+\/pull\/(\d+)(?=[\s).,;:]|$)/gi}function np(){return/https?:\/\/\S+/gi}function zn(n){let e=ep(n);return[`Requested permission mode: ${n.requestedPermissionMode??"unknown"}`,`Effective permission mode: ${n.currentPermissionMode??"unknown"}`,`Deterministic approval/execution state: ${n.approvalExecutionState??"unknown"}`,...e?[e]:[]]}function Po(n){switch(n){case"user":return"Stopped by user";case"shutdown":return"Stopped by shutdown";case"startup-timeout":return"Stopped by startup timeout";case"unknown":case void 0:return"Stopped unexpectedly";default:return"Stopped"}}function rp(){return{requiresShortFactualSummary:!0,owner:"agent",appliesToOrdinaryTerminalCompletions:!0}}function op(){return{requiresShortFactualSummary:!0,owner:"agent",appliesToWorktreeTerminalOutcomes:!0}}function ip(){return{requiresShortFactualSummary:!0,owner:"agent",appliesToGoalTaskCompletions:!0}}function wo(n){let{sessionId:e,canonicalStatusDetail:t,canonicalStatusDelivered:r=!0,hasOriginRouteBlock:o=!1}=n,i=o?["7. Before sending that follow-up, honor the Session origin route block above. If originRoute differs from the current chat, do NOT use a plain final assistant reply; use a routed send path that preserves provider/target/threadId."]:[],s=o?8:7,a=o?9:8;return["[ACTION REQUIRED] Follow your autonomy rules for session completion:",`1. Use agent_output(session='${e}', full=true) to read the full result.`,"2. If this is part of a multi-phase pipeline, launch the next phase NOW — do not wait for user input.",`3. ${t??(r?"The plugin already sent the canonical completion status to the user.":"The plugin did not confirm delivery of the canonical completion status to the user.")}`,"4. Treat the completed session output as source material, not visible delivery. A meaningful final summary inside agent_output is not enough to skip, because the user may only have seen the plugin's terse status line.","5. If the visible result is only the plugin's terse status line, use the full output to send the user one short factual completion summary for this completed session. Do this even when agent_output already contains a good final summary.","6. This requirement applies to ordinary terminal/manual completions too, not just delegated worktree decisions.",...i,`${s}. That follow-up belongs to you alone; keep it brief, concrete, and grounded in reliable result data. Send at most one orchestrator-owned human summary for this terminal/worktree outcome.`,...r?[`${a}. Do NOT repeat the plugin's status line, and do NOT rely on the plugin to summarize the completed work for you.`]:[`${a}. Because canonical status delivery was not confirmed, account for that gap yourself when you follow up; do NOT assume the plugin already reached the user.`],`${a+1}. Send a normal concise final response after any routed follow-up is visibly sent. Do not use marker text, do not answer NO_REPLY, and do not ask the user whether they want the summary.`]}function sp(n){let{contract:e,canonicalStatusDelivered:t}=n;return["Completion diagnostics:",`- Canonical completion status delivered to user: ${t?"yes":"no"}`,`- Plugin requested short factual follow-up summary: ${e.requiresShortFactualSummary?"yes":"no"}`,`- Contract applies to ordinary terminal/manual completions: ${e.appliesToOrdinaryTerminalCompletions?"yes":"no"}`]}function bo(n){let{session:e,originThreadLine:t,preview:r}=n,o=!!t.trim(),i=rp(),s=a=>["Coding agent session completed.",`Name: ${e.name} | ID: ${e.id}`,`Status: ${e.status}`,...o?[t]:[],...zn(e),"","Output preview:",r,"",...sp({contract:i,canonicalStatusDelivered:a}),"",...wo({sessionId:e.id,canonicalStatusDelivered:a,hasOriginRouteBlock:o})].join(`
|
|
142
|
+
`);return{userMessage:`✅ [${e.name}] Completed${ye(e)}`,wakeMessageOnNotifySuccess:s(!0),wakeMessageOnNotifyFailed:s(!1),followupContract:i}}function ze(n){let e=op(),t=!!n.originThreadLine.trim(),r=(n.detailLines??[]).map(i=>ga(i).trim()).filter(i=>i.length>0),o=ga(n.outcomeLine);return["Worktree follow-through outcome recorded.",`Name: ${n.sessionName??"unknown"} | ID: ${n.sessionId}`,...t?[n.originThreadLine]:[],"","Canonical outcome status:",o,...r.length>0?["","Outcome details:",...r.map(i=>`- ${i}`)]:[],"","Completion diagnostics:",`- Canonical worktree status delivered to user: ${n.canonicalStatusDelivered?"yes":"no"}`,`- Plugin requested short factual follow-up summary: ${e.requiresShortFactualSummary?"yes":"no"}`,`- Contract applies to worktree terminal outcomes: ${e.appliesToWorktreeTerminalOutcomes?"yes":"no"}`,"","[ACTION REQUIRED] Follow your autonomy rules for worktree follow-through:",`1. Use agent_output(session='${n.sessionId}', full=true) to read the full result if output is available.`,"2. Treat the completed session output as source material, not visible delivery. A meaningful final summary inside agent_output is not enough to skip, because the user may only have seen the plugin's terse status line.","3. Use the full output plus the canonical outcome facts above to send the user one short factual outcome summary. Do this even when agent_output already contains a good final summary.","4. If full output is unavailable or not meaningful, say only what is proven by the merge/PR facts above; do not invent task details.","5. Mention blockers such as push failure when present; do not describe a local-only merge as pushed.","6. Do not include raw PR URLs in the follow-up summary. The plugin already posted the PR link in the canonical status; refer to PRs by number, repo, and branch instead.",...t?["7. Before sending that follow-up, honor the Session origin route block above. If originRoute differs from the current chat, do NOT use a plain final assistant reply; use a routed send path that preserves provider/target/threadId."]:[],`${t?8:7}. Do NOT repeat only the plugin status line; keep the follow-up brief, concrete, and non-duplicative. Send at most one human-visible summary for this terminal/worktree outcome, counting summaries already sent by the foreground assistant turn or routed message tools.`,`${t?9:8}. Send a normal concise final response after any routed follow-up is visibly sent. Do not use marker text, do not answer NO_REPLY, and do not ask the user whether they want the summary.`].join(`
|
|
143
|
+
`)}function Ro(n){let e=ip(),t=!!n.originThreadLine.trim();return["Goal task succeeded.",`Task: ${n.taskName}`,`Name: ${n.sessionName??n.taskName} | ID: ${n.sessionId}`,...t?[n.originThreadLine]:[],"","Canonical goal status:",n.summary,"","Completion diagnostics:",`- Canonical goal success status delivered to user: ${n.canonicalStatusDelivered?"yes":"no"}`,`- Plugin requested short factual follow-up summary: ${e.requiresShortFactualSummary?"yes":"no"}`,`- Contract applies to goal task completions: ${e.appliesToGoalTaskCompletions?"yes":"no"}`,"","[ACTION REQUIRED] Follow your autonomy rules for goal task completion:",`1. Use agent_output(session='${n.sessionId}', full=true) to read the full result if output is available.`,"2. Treat the completed session output as source material, not visible delivery. A meaningful final summary inside agent_output is not enough to skip, because the user may only have seen the plugin's terse goal status line.","3. Send the user one short factual completion summary grounded in the full output plus the canonical goal status above. Do this even when agent_output already contains a good final summary.","4. If full output is unavailable or not meaningful, say only what is proven by the goal status above; do not invent task details.",...t?["5. Before sending that follow-up, honor the Session origin route block above. If originRoute differs from the current chat, do NOT use a plain final assistant reply; use a routed send path that preserves provider/target/threadId."]:[],`${t?6:5}. Do NOT repeat only the plugin status line; keep the follow-up brief, concrete, and non-duplicative. Send at most one human-visible summary for this goal success outcome, counting summaries already sent by the foreground assistant turn or routed message tools.`,`${t?7:6}. Send a normal concise final response after any routed follow-up is visibly sent. Do not use marker text, do not answer NO_REPLY, and do not ask the user whether they want the summary.`].join(`
|
|
144
|
+
`)}function ga(n){return n.replace(tp(),"PR #$1").replace(np(),"[link omitted]")}function Ao(n){let{session:e,originThreadLine:t,errorSummary:r,preview:o,worktreeAutoCleaned:i,failedButtons:s}=n,a=o.trim()?["","Output preview:",o]:[],l=i?["","Note: Worktree and branch were auto-removed (zero cost, startup failure)."]:[];return{userMessage:[`❌ [${e.name}] Failed${ye(e)}`,` ⚠️ ${r}`].join(`
|
|
145
|
+
`),wakeMessage:["Coding agent session failed.",`Name: ${e.name} | ID: ${e.id}`,`Status: ${e.status}`,t,...zn(e),...e.harnessSessionId?[`Backend conversation ID: ${e.harnessSessionId}`]:[],"","Failure summary:",r,...a,...l,"","[ACTION REQUIRED] Follow your autonomy rules for session failure:",`1. Use agent_output(session='${e.id}', full=true) to inspect the full failure context.`,`2. Continue the same session with agent_respond(session='${e.id}', message='<next instruction>').`,` If you intentionally want to fork or switch harnesses, launch a new session with agent_launch(resume_session_id='${e.id}', fork_session=true, ...)`," If the failure is a launch/config issue, relaunch fresh with agent_launch(prompt=...).","3. Notify the user with the failure cause and the next action you are taking."].join(`
|
|
146
|
+
`),buttons:s}}function Io(n){let{session:e,originThreadLine:t,preview:r}=n;return{userMessage:`⏸️ [${e.name}] Turn completed${ye(e)}`,wakeMessage:["Coding agent session turn ended.",`Name: ${e.name}`,`ID: ${e.id}`,`Status: ${e.status}`,`Lifecycle: ${e.lifecycle}`,"","Last output (~20 lines):",r,...t?["",t]:[]].join(`
|
|
147
|
+
`)}}function To(n){let{sessionName:e,sessionId:t,branchName:r,baseBranch:o,promptSnippet:i,commitLines:s,moreNote:a,originThreadLine:l,diffSummary:d,allowedActions:u,policyReason:m}=n,g=!!l?.trim();return[`[DELEGATED WORKTREE DECISION] Session "${e}" completed with changes.`,"",`Session ID: ${t}`,`Branch: ${r} → ${o}`,`Commits: ${d.commits} | Files: ${d.filesChanged} | +${d.insertions} / -${d.deletions}`,...g?[l]:[],"",...s,...a?[a]:[],"","Original task prompt (first 500 chars):",i,"",...m?[`Policy constraint: ${m}`,""]:[],"You own the next step for this worktree.",`- First call agent_output(session='${t}', full=true) to inspect the full session output and use it as source material for your decision.`,...u?.merge===!1?["- Do not call agent_merge(); repo policy does not allow direct merge for this session."]:[`- Merge immediately with agent_merge(session="${e}", base_branch="${o}") if the changes are clearly in-scope and low-risk.`],...u?.pr===!1?["- Do not call agent_pr(); PR creation is unavailable or forbidden by repo policy."]:[`- If a PR is safer or human choice is needed, call agent_request_worktree_decision(session="${e}", summary="...") so the user gets the canonical Merge/Open PR/Later/Discard buttons.`],`- If scope or risk is unclear, call agent_request_worktree_decision(session="${e}", summary="...") with a concise risk summary instead of sending a plain-text-only question.`,"- Never call agent_pr() autonomously in delegate mode.","- After deciding, notify the user briefly with what you did and why.",...g?["- Send any human follow-up to the Session origin route above; if it differs from the current chat, do not use a plain final assistant reply."]:[]].join(`
|
|
148
|
+
`)}function xo(n,e){return[`[DELEGATED WORKTREE DECISION REMINDER] Session "${n.name}" still has an unresolved worktree decision.`,"",`Session ID: ${n.sessionId??n.harnessSessionId}`,`Branch: ${n.worktreeBranch??"unknown"}`,`Pending: ${e}h`,"","Resolve it now:",`- agent_merge(session="${n.name}") if the diff is clearly safe and in scope`,"- If a PR is safer, ask the user before agent_pr()","- If scope or risk is unclear, ask the user for guidance","- Never call agent_pr() autonomously in delegate mode"].join(`
|
|
149
|
+
`)}function Co(n){let{sessionName:e,sessionId:t,headline:r,cleanupSummary:o,preview:i,originThreadLine:s,requestedPermissionMode:a,currentPermissionMode:l,approvalExecutionState:d,approvalState:u,planApproval:m,approvalPromptStatus:g,approvalPromptMessageKind:S,approvalPromptDeliveredAt:y}=n,c=i.trim()?["","Output preview:",i]:[],h=!!s?.trim();return[r??"Coding agent session completed with no worktree changes to merge.",`Name: ${e} | ID: ${t}`,`Worktree outcome: ${o}`,...h?[s]:[],...zn({requestedPermissionMode:a,currentPermissionMode:l,approvalExecutionState:d,approvalState:u,planApproval:m,approvalPromptStatus:g,approvalPromptMessageKind:S,approvalPromptDeliveredAt:y}),...c,"",...wo({sessionId:t,canonicalStatusDetail:"The plugin already sent the canonical completion status to the user, including the no-worktree-changes outcome.",hasOriginRouteBlock:h})].join(`
|
|
150
|
+
`)}var ap=3200,ha=3e3,ka=2400,lp=120,ya=6,dp={objective:"Objective / scope",approach:"Implementation approach",affected:"Files / systems affected",verification:"Tests / verification",effects:"Destructive / external effects",risks:"Material risks",unknowns:"Unknowns / decisions",costs:"Costs / resources",rollback:"Rollback / recovery"};function Mo(n){let e=n.toLowerCase();if(/\b(rollback|roll back|revert|restore|recovery)\b/.test(e))return"rollback";if(/\?\s*$/.test(e))return"unknowns";if(/^budget:|\b(costs?|billing|paid|pricing|spend|charges?)\b|[$€£]\s*\d/.test(e))return"costs";if(/^(unknowns?|omissions?|assumptions?|open questions?|decisions?|choices?|alternatives?|options?)(?:\s*\/[^:]*)?:/.test(e))return"unknowns";if(/^(material\s+)?risks?(?:\s*\/[^:]*)?:/.test(e))return"risks";if(/^(destructive|irreversible|external)(?:\s*\/[^:]*)?\s*(?:effects?|actions?)?:/.test(e))return"effects";if(/^(tests?|verification|validation)(?:\s*\/[^:]*)?:/.test(e))return"verification";if(/^(affected\s+)?(?:files?|components?|systems?)(?:\s*\/[^:]*)?:/.test(e))return"affected";if(/^(objective|scope|goal|purpose)(?:\s*\/[^:]*)?:/.test(e))return"objective";if(/\b(unknown|uncertain|assumption|omission|open question|choose|choices?|alternatives?|options?|decision needed|tbd|not specified)\b/.test(e))return"unknowns";if(/\b(delete|deletion|remove|drop|overwrite|force[- ]?push|rm|truncate|credentials?|secrets?|migrate|destructive|irreversible|deploy|publish|release|restart|production|external effect|send|notify|purchase|trade)\b/.test(e))return"effects";if(/\b(risk|hazard|failure mode|danger|caveat)\b/.test(e))return"risks";if(/\b(objective|scope|goal|purpose|outcome|intent)\b/.test(e))return"objective";if(/\b(tests?|verify|verification|validation|lint|typecheck|build|checks?|proof)\b/.test(e))return"verification";if(/\b(affected|files?\/systems?|components?\/files?)\b/.test(e)||/`[^`]+(?:\/[^`]*)?`/.test(n)||/\b[\w.-]+\.(?:ts|tsx|js|jsx|py|md|json|ya?ml|toml|sql)\b/.test(n))return"affected";if(/^(implementation(?: approach| steps)?|approach|steps?(?: \d+)?)(?:\s*\/[^:]*)?:/.test(e)||/\b(approach|implementation|step|change|update|add|create|refactor|modify|wire|use)\b/.test(e))return"approach"}function St(n){return n.replace(/^#{1,6}\s+/,"").replace(/^[-*+]\s+/,"").replace(/^\d+[.)]\s+/,"").trim()}function _o(n){return n.split(/(`+[^`]*`+)/g).map((e,t)=>t%2?e:e.replace(/\*\*([^*]+)\*\*/g,"$1").replace(/(^|\s)__([^_]+)__(?=\s|$|:)/g,"$1$2")).join("").trim()}function $o(n){let e=_o(St(n));return/^#{1,6}\s+/.test(n)||/^[A-Za-z][^.!?]{0,80}:\s*$/.test(e)}function va(n){let e=n.split(`
|
|
151
|
+
`),t=[],r=o=>_o(St(o)).replace(/^•\s*/,"").replace(/^\|/,"").replace(/\|$/,"").split(/(?<!\\)\|/).map(i=>i.trim().replace(/\\\|/g,"|"));for(let o=0;o<e.length;o+=1){let i=e[o],s=e[o+1];if(i.includes("|")&&s&&r(s).every(a=>/^:?-+:?$/.test(a))){let a=r(i);for(o+=1;o+1<e.length&&e[o+1].includes("|");){let d=r(e[++o]).flatMap((u,m)=>u?[`${a[m]||`Field ${m+1}`}: ${u}`]:[]);d.length&&t.push(`- ${d.join("; ")}`)}}else t.push(_o(i))}return t}function yt(n,e){let t=e.replace(/\s+/g," ").trim();t&&(n.some(r=>r.toLowerCase()===t.toLowerCase())||n.push(t))}function Pa(n){let e=n.artifact?.markdown?.trim()||n.preview.trim(),t={objective:[],approach:[],affected:[],verification:[],effects:[],risks:[],unknowns:[],costs:[],rollback:[]},r,o=0;n.artifact?.explanation?.trim()&&yt(t.objective,He(n.artifact.explanation));for(let u of n.artifact?.steps??[]){let m=He(u.step),g=Mo(m)??"approach";yt(t[g],m)}for(let u of va(e)){let m=u.trim();if(!m)continue;let g=St(m);if(!g||/^(plan|proposed plan|implementation plan|decision brief):?$/i.test(g)||/^(thinking|checking|considering|analyzing)\b/i.test(g)||/^(should|can|could|would|will) (?:i|we) (?:proceed|continue|start)\?$/i.test(g))continue;if($o(m)){r=Mo(`${g.replace(/:\s*$/,"")}:`);continue}let S=Mo(g),c=r&&["objective","effects","risks","unknowns","costs","rollback"].includes(r)?r:S??r;c?yt(t[c],g):t.objective.length===0?yt(t.objective,g):(yt(t.approach,g),o+=1)}let i=0,s=0;t.approach=t.approach.filter(u=>u.includes("; ")||++i<=ya?!0:(s+=1,!1));let a=u=>{let m=t[u];return m.length?[`${dp[u]}: ${m[0]}`,...m.slice(1).map(g=>`- ${g}`)]:[]},l=[];s>0&&l.push(`${s} additional routine implementation step(s)`),n.artifact||l.push("a version-matched structured plan artifact was unavailable; this brief uses the available plan preview"),o>ya&&l.push("unclassified plan detail was compacted into the implementation section");let d=n.detailRef&&/^[a-zA-Z0-9_-]+$/.test(n.detailRef)?`Inspect available full output before deciding: /agent_output ${n.detailRef} --full. Request the complete plan if it is unavailable there.`:"To inspect these details before deciding, reply asking for the complete plan for this version.";return[...a("objective"),"",...a("approach"),"",...a("affected"),"",...a("verification"),"",...a("effects"),"",...a("risks"),"",...a("unknowns"),"",...a("costs"),"",...a("rollback"),...l.length>0?["","Full-plan detail:",`- ${l.join("; ")}. ${d}`]:[]].join(`
|
|
152
|
+
`).replace(/\n{3,}/g,`
|
|
153
|
+
|
|
154
|
+
`).trim()||"Plan context: No concrete plan content was available. Request the complete plan before deciding."}function He(n){let e=[],t;for(let r of va(n))$o(r)?t=St(r).replace(/:$/,""):r.trim()?(e.push(t?`${t}: ${St(r)}`:r),t=void 0):!t&&e.length&&e.push("");return e.join(`
|
|
155
|
+
`).trim()}function up(n,e){let t=[],r=n.trim();for(;r.length>e;){let o=r.lastIndexOf(" ",e);o<Math.floor(e*.6)&&(o=e),t.push(r.slice(0,o).trimEnd()),r=r.slice(o).trimStart()}return r.length>0&&t.push(r),t}function Do(n,e){let t=n.split(`
|
|
156
|
+
`),r=[],o=[];for(let l of t)$o(l.trim())||l.trim()==="Decision brief"?o.push(l):l.trim()?(r.push([...o,l].join(`
|
|
157
|
+
`)),o=[]):!o.length&&r.length&&(r[r.length-1]+=`
|
|
158
|
+
`);let i=[],s="",a=()=>{s.trim()&&i.push(s.trim()),s=""};for(let l of r){let d=l.length>e?up(l,e):[l];for(let u of d)(s?`${s}
|
|
159
|
+
${u}`:u).length>e&&a(),s=s?`${s}
|
|
160
|
+
${u}`:u}return a(),i}function wa(n){return Do(n.trim(),ka)}function ba(n){return G(n.trim(),lp)}function Sa(n,e){return e?n?`
|
|
154
161
|
|
|
155
162
|
Choose Approve, Revise, or Reject below.`:`
|
|
156
163
|
|
|
157
164
|
Approval is still pending for this plan version.`:`
|
|
158
165
|
|
|
159
|
-
Continued in next message.`}function
|
|
160
|
-
`),
|
|
161
|
-
${
|
|
162
|
-
${d}${pa(o,f)}`})}function To(n){return fa(n)}function ft(n){let{sessionName:e,actionableVersion:t,preview:r,artifact:o,hasButtons:i,escalationRationale:s}=n,a=n.heading??"ready for approval",l=ha(e),d=fa({preview:r,artifact:o}),u=s?.trim()?`Why this was escalated:
|
|
163
|
-
${s.trim()}
|
|
166
|
+
Continued in next message.`}function cp(n){let{sessionName:e,actionableVersion:t,fullPlanText:r,hasButtons:o,heading:i}=n,s=ba(e),a=ka;for(;a>0;){let d=Do(r,a),u=d.map((S,y)=>{let c=d.length,h=`📋 [${s}] Plan v${t??"?"} ${i} (${y+1}/${c})`,P=Sa(o,y===c-1);return`${h}
|
|
167
|
+
${S}${P}`}),m=u.reduce((S,y)=>Math.max(S,y.length),0);if(m<=ha)return u;let g=m-ha;a-=Math.max(g,50)}let l=Do(r,100);return l.map((d,u)=>{let m=u===l.length-1;return`📋 [${s}] Plan v${t??"?"} ${i} (${u+1}/${l.length}):
|
|
168
|
+
${d}${Sa(o,m)}`})}function Eo(n){return Pa(n)}function kt(n){let{sessionName:e,actionableVersion:t,preview:r,artifact:o,hasButtons:i,escalationRationale:s}=n,a=n.heading??"ready for approval",l=ba(e),d=Pa({preview:r,artifact:o,detailRef:e}),u=He(s??""),m=u?`Why this was escalated: ${u}
|
|
164
169
|
|
|
165
|
-
Decision brief
|
|
166
|
-
${d}`:`Decision brief
|
|
167
|
-
${d}`,
|
|
170
|
+
Decision brief
|
|
171
|
+
${d}`:`Decision brief
|
|
172
|
+
${d}`,g=`📋 [${l}] Plan v${t??"?"} ${a}
|
|
168
173
|
|
|
169
|
-
${
|
|
174
|
+
${m}
|
|
170
175
|
|
|
171
|
-
${i?"Choose Approve, Revise, or Reject below.":"Approval is still pending for this plan version."}`;return
|
|
176
|
+
${i?"Choose Approve, Revise, or Reject below.":"Approval is still pending for this plan version."}`;return g.length>ap?{displayMode:"chunked-summary",userMessages:cp({sessionName:e,actionableVersion:t,fullPlanText:m,hasButtons:i,heading:a}),reviewSummary:m}:{displayMode:"summary",userMessages:[g],reviewSummary:m}}function Ra(n){let e=n?.trim();return e||void 0}function pp(n){let e=Ra(n.questionText)??"The session is waiting for your reply.",t=Ra(n.contextSummary);return t?[`❓ [${n.sessionName}] Question waiting for reply:`,"",e,"",`Why: ${t}`].join(`
|
|
172
177
|
`):`❓ [${n.sessionName}] Question waiting for reply:
|
|
173
178
|
|
|
174
|
-
${e}`}function
|
|
175
|
-
`)}function
|
|
179
|
+
${e}`}function mp(n,e){return e!=null&&n.approvalPromptRequiredVersion===e&&(n.approvalPromptStatus==="delivered"||n.approvalPromptStatus==="fallback_delivered")}function Aa(n){let{session:e,summary:t}=n,r=e.actionablePlanDecisionVersion??e.planDecisionVersion;return[`📋 [${e.name}] Plan v${r??"?"} needs your decision.`,"","Interactive Approve / Revise / Reject buttons could not be delivered, so reply here instead:",'- Reply "approve" to approve and start implementation','- Reply "reject" to reject and stop the session',"- Any other reply will be sent back as revision feedback","",t].join(`
|
|
180
|
+
`)}function vt(n){let e=wa(Aa(n));return e.map((t,r)=>({text:e.length===1?t:`${t}
|
|
176
181
|
|
|
177
|
-
${r===e.length-1?"End of decision context.":"Continued in next message."}`,requiredForSequenceSuccess:!0}))}function
|
|
178
|
-
`)}:A==="ask"?{label:"plan-approval",userMessage:h,userMessages:
|
|
179
|
-
`),buttons:
|
|
182
|
+
${r===e.length-1?"End of decision context.":"Continued in next message."}`,requiredForSequenceSuccess:!0}))}function No(n){let{session:e,preview:t,questionText:r,questionContextSummary:o,planArtifact:i,originThreadLine:s,planApprovalMode:a,planApprovalButtons:l,questionButtons:d}=n,u=e.pendingPlanApproval,m=e.actionablePlanDecisionVersion??e.planDecisionVersion,g=mp(e,m),S=a??"delegate",y=u&&S==="ask"&&!g?kt({sessionName:e.name,actionableVersion:m,preview:t,artifact:i,hasButtons:!!l}):void 0,c=u?y?.reviewSummary??Eo({preview:t,artifact:i}):void 0,h=u?y&&y.userMessages.length===1?y.userMessages[0]:void 0:pp({sessionName:e.name,questionText:r??t,contextSummary:o,hasButtons:!!d}),P=u&&y&&y.userMessages.length>1?y.userMessages.map((A,I,R)=>({text:A,buttons:I===R.length-1?l:void 0,requiredForSequenceSuccess:!0})):void 0;if(u){let A=S,I="Permission mode: plan → will switch to bypassPermissions on approval";return A==="delegate"?{label:"plan-approval",userMessage:h,userMessages:P,planReviewSummary:c,wakeMessage:["[DELEGATED PLAN APPROVAL] Coding agent session has finished its plan and is requesting approval to implement.",`Name: ${e.name} | ID: ${e.id} | Plan v${m??"?"}`,s,I,"","⚠️ YOU MUST COMPLETE THESE STEPS IN ORDER. Do NOT skip any step.","","━━━ STEP 1 (MANDATORY): Read the full plan ━━━",`Call agent_output(session='${e.id}', full=true) to read the FULL plan output.`,"The preview below is truncated — you MUST read the full output before making any decision.","","Preview (truncated):",t,"","━━━ STEP 2 (MANDATORY): Review privately ━━━","You are the delegated decision-maker. Review the plan yourself before involving the user.","If you approve directly, you own the user-facing explanation of what was approved and why.","Keep it short and user-facing: one sentence is usually enough.","","━━━ STEP 3 (ONLY AFTER steps 1 and 2): Decide ━━━","Choose ONE:","","APPROVE the plan directly if ALL of the following are true:","- You have read the FULL plan (not just the preview)","- The plan scope matches the original task request","- The changes are low-risk (no destructive operations, no credential handling, no production deployments)","- The plan is clear and well-scoped (no ambiguous requirements or open design questions)","- No architectural decisions that the user should weigh in on","- The working directory and codebase are correct","","ESCALATE to the user and WAIT if ANY of the following are true:","- The plan involves destructive operations (deleting files, dropping tables, force-pushing)","- The plan involves credentials, secrets, or production environments","- The plan requires architectural decisions not covered by the original task","- The scope has expanded beyond the original request","- The requirements are ambiguous or the plan makes assumptions the user should confirm","- You are unsure — when in doubt, always escalate","",`If approving: call agent_respond(session='${e.id}', message='Approved. Go ahead.', approve=true, approval_rationale='<brief reason>').`,"That rationale should say why approval was safe: for example scope match, low risk, or no meaningful ambiguity.","After approving directly, send the user a short plain-text follow-up explaining what was approved and why.","The plugin's thumbs-up line is only the minimal approval acknowledgment, not the explanation.",`If escalating: call agent_request_plan_approval(session='${e.id}', summary='...') exactly once so the plugin posts the single canonical Approve / Revise / Reject prompt.`,"That summary must concisely explain why this was escalated, plus changed files/components, risk level and why, scope match/expansion, and any concerns or assumptions.","After the canonical prompt exists, WAIT for the user's button click or explicit response and do NOT send a second plain-text recap.",`To request changes without user escalation: call agent_respond(session='${e.id}', message='<your feedback>') and do NOT set approve=true. The agent will revise the plan.`].join(`
|
|
183
|
+
`)}:A==="ask"?{label:"plan-approval",userMessage:h,userMessages:P,planReviewSummary:c,wakeMessage:["[USER APPROVAL REQUESTED] Coding agent session has finished its plan. The user has been notified via Telegram and must approve directly.",`Name: ${e.name} | ID: ${e.id} | Plan v${m??"?"}`,s,I,"","DO NOT approve this plan yourself. Wait for the user's explicit approval or rejection.","Once the user responds, forward their decision:",` To approve: agent_respond(session='${e.id}', message='Approved. Go ahead.', approve=true)`,` To request changes: agent_respond(session='${e.id}', message='<user feedback>')`,"","Preview (truncated):",t].join(`
|
|
184
|
+
`),buttons:P?void 0:l}:{label:"plan-approval",userMessage:h,planReviewSummary:c,wakeMessage:["[AUTO-APPROVE] Session has a plan ready. Approve it now:",`agent_respond(session='${e.id}', message='Approved. Go ahead.', approve=true)`].join(`
|
|
180
185
|
`)}}let b=e.multiTurn?"Multi-turn session":"Session";return{label:"waiting",userMessage:h,wakeMessage:[`[SYSTEM INSTRUCTION: Follow your auto-respond rules strictly. If this is a permission request or "should I continue?" → auto-respond. For ALL other questions → forward the agent's EXACT question to the user. Do NOT add your own analysis, commentary, or interpretation. Do NOT "nudge" or "poke" the session.]`,"",`${b} is waiting for a genuine user reply.`,`Name: ${e.name} | ID: ${e.id}`,s,"","Last output:",t,"",`Use agent_respond(session='${e.id}', message='...') to send a reply, or agent_output(session='${e.id}', full: true) to see full context before deciding.`].join(`
|
|
181
|
-
`),buttons:d}}var
|
|
182
|
-
`).map(t=>t.replace(/^[-*•]\s+/,"").trim()).filter(Boolean).slice(0,
|
|
183
|
-
`)}function
|
|
186
|
+
`),buttons:d}}var Hn=3,Oo=180,fp=4e3,Ia=32;function Ta(){let n=xe(),e=wp(n);if(e)return{async generateWorktreeDecisionSummary(t){if(typeof e.generateWorktreeDecisionSummary=="function")return await e.generateWorktreeDecisionSummary(t);if(typeof e.summarizeWorktreeDecision=="function")return await e.summarizeWorktreeDecision(t);let r=bp(t);if(typeof e.generateObject=="function")return await e.generateObject({task:"openclaw-code-agent.worktree-decision-summary",prompt:r,input:t});if(typeof e.generateText=="function")return await e.generateText({task:"openclaw-code-agent.worktree-decision-summary",prompt:r});if(typeof e.complete=="function")return await e.complete({prompt:r})}}}function gp(n,e){let t=Rp(e);if(t.length>0)return t;let r=hp(n);if(r.length>0)return r;let o=[],i=n.changedFiles.slice(0,3).map(a=>`\`${a}\``);if(i.length>0){let a=n.changedFiles.length-i.length;o.push(a>0?`Touches ${i.join(", ")} and ${a} more file${a===1?"":"s"}`:`Touches ${i.join(", ")}`)}let s=[...new Set(n.commitMessages.map(a=>a.message.trim()).filter(Boolean))].slice(0,2);return s.length>0&&o.push(`Recent work: ${s.join("; ")}`),o}function hp(n){let e=new Set,t=n.commitMessages.length===1?n.changedFiles:[];return n.commitMessages.map(r=>yp(r.message,t)).filter(r=>!!r).filter(r=>{let o=r.toLowerCase();return e.has(o)?!1:(e.add(o),!0)}).slice(0,Hn)}function yp(n,e){let t=Sp(n);if(!t)return;let r=e.length===1?` in \`${e[0]}\``:e.length>1&&e.length<=3?` across ${e.map(o=>`\`${o}\``).join(", ")}`:"";return Wo(`${t}${r}.`,Oo)}function Sp(n){let e=Qn(Yn(n)).replace(/^(?:feat|fix|docs|style|refactor|perf|test|tests|build|ci|chore|revert)(?:\([^)]+\))?!?:\s*/i,"").replace(/\.$/,"").trim();if(!e||Fo(e))return;let t=e.match(/^([a-z]+)\b(.*)$/i);if(!t)return e;let r=t[1].toLowerCase(),o=t[2]??"",s=new Map([["add","Adds"],["address","Addresses"],["block","Blocks"],["change","Changes"],["clarify","Clarifies"],["clean","Cleans"],["cover","Covers"],["create","Creates"],["deduplicate","Deduplicates"],["delete","Deletes"],["disable","Disables"],["document","Documents"],["enable","Enables"],["extract","Extracts"],["fix","Fixes"],["guard","Guards"],["harden","Hardens"],["implement","Implements"],["improve","Improves"],["init","Initializes"],["initialize","Initializes"],["migrate","Migrates"],["move","Moves"],["normalize","Normalizes"],["preserve","Preserves"],["prevent","Prevents"],["refactor","Refactors"],["remove","Removes"],["rename","Renames"],["restore","Restores"],["simplify","Simplifies"],["tighten","Tightens"],["update","Updates"],["use","Uses"],["wire","Wires"],["wrap","Wraps"]]).get(r);return s?`${s}${o}`:`${r[0].toUpperCase()}${r.slice(1)}${o}`}async function xa(n){let e=kp(n),t=gp(n.diffSummary??{changedFiles:e.changedFiles,commitMessages:e.commitSubjects.map(r=>({message:r}))},n.outputPreview);if(!n.provider)return{source:"fallback",lines:t,evidence:e,error:"LLM summary provider unavailable."};try{let r=await n.provider.generateWorktreeDecisionSummary(e),o=vp(r);return o.length>0?{source:"llm",lines:o,evidence:e}:{source:"fallback",lines:t,evidence:e,error:"LLM-generated worktree summary failed schema validation."}}catch(r){let o=r instanceof Error?r.message:String(r);return`${o}`,{source:"fallback",lines:t,evidence:e,error:"LLM summary provider failed."}}}function kp(n){let e=n.diffSummary;return{sessionName:n.sessionName,objective:Tp(n.prompt),stats:e?{commits:e.commits,filesChanged:e.filesChanged,insertions:e.insertions,deletions:e.deletions}:void 0,changedFiles:e?.changedFiles??[],commitSubjects:e?.commitMessages.map(t=>Qn(t.message)).filter(Boolean).slice(0,8)??[],outputPreview:Wo(Yn(n.outputPreview??""),fp)}}function vp(n){let e=Pp(n),t=Array.isArray(e)?e:e&&typeof e=="object"&&Array.isArray(e.summary)?e.summary:void 0;if(!t||t.length===0||t.length>Hn)return[];let r=t.map(o=>typeof o=="string"?Qn(o):"").filter(Boolean);return r.length!==t.length?[]:r.some(o=>o.length>Oo)?[]:r.some(Fo)?[]:r}function Pp(n){if(typeof n!="string")return n;let e=n.trim();if(e)try{return JSON.parse(e)}catch{return{summary:e.split(`
|
|
187
|
+
`).map(t=>t.replace(/^[-*•]\s+/,"").trim()).filter(Boolean).slice(0,Hn)}}}function wp(n){return[n?.worktreeDecisionSummary,n?.llm,n?.ai,n?.model,n?.models].find(t=>!!(t&&typeof t=="object"&&(typeof t.generateWorktreeDecisionSummary=="function"||typeof t.summarizeWorktreeDecision=="function"||typeof t.generateObject=="function"||typeof t.generateText=="function"||typeof t.complete=="function")))}function bp(n){return["Summarize the completed OpenClaw Code Agent work for a worktree decision notification.",'Return only JSON with shape {"summary":["..."]}.',"Write 1-3 concise, user-facing bullets that help a human choose Merge, Open PR, Later, or Discard.","Do not mention that you are summarizing. Do not invent changes not supported by the evidence.","","Evidence:",JSON.stringify(n,null,2)].join(`
|
|
188
|
+
`)}function Rp(n){let e=new Set;return Yn(n??"").replace(/\r\n/g,`
|
|
184
189
|
`).split(`
|
|
185
|
-
`).map(r=>
|
|
190
|
+
`).map(r=>Qn(r)).map(r=>Ap(r)).filter(Ip).filter(r=>{let o=r.toLowerCase();return e.has(o)?!1:(e.add(o),!0)}).slice(0,Hn)}function Ap(n){return n.replace(/^(?:summary|done|completed|changes|validation|verified)\s*:\s*/i,"").trim()}function Ip(n){return n.length<12||n.length>Oo||Fo(n)||/^(```|>|command\b|exit code\b|\$|[a-z0-9_-]+@[a-z0-9_-]+:)/i.test(n)||/^(?:running|ran|pnpm|npm|node|git|tsc|vitest|jest)\b/i.test(n)||/^(?:pass|fail|ok|error)\b[:\s]/i.test(n)||/^(?:changed files?|files changed|recent commits?|commits?)\b/i.test(n)?!1:/^(?:implemented|updated|added|fixed|changed|removed|verified|covered|refactored|improved|created|documented|hardened|restored|simplified|renamed|moved|wired|handled|blocked|reduced|deduplicated|normalized|addressed|prevented)\b/i.test(n)||/\b(?:so that|coverage|validation|regression|cleanup|summary|notification|callback|button|merge|worktree|policy)\b/i.test(n)&&/\b(?:preserv(?:e|es|ed|ing)|clear(?:s|ed|ing)?|reuse(?:s|d|ing)?|cover(?:s|ed|ing)?|pass(?:es|ed|ing)?|fail(?:s|ed|ing)?|complete(?:s|d)?|fall(?:s|ing)? back)\b/i.test(n)}function Tp(n){let e=n?.replace(/\r\n/g,`
|
|
186
191
|
`).split(`
|
|
187
|
-
`).map(o=>o.trim()).filter(Boolean).join(" ");if(!e)return;let t=e.match(/^[^.!?]+[.!?]/)?.[0]??e,r=
|
|
192
|
+
`).map(o=>o.trim()).filter(Boolean).join(" ");if(!e)return;let t=e.match(/^[^.!?]+[.!?]/)?.[0]??e,r=Yn(t).replace(/\s+/g," ").trim();return r?Wo(r,180):void 0}function Qn(n){return n.replace(/^[-*•]\s+/,"").replace(/\s+/g," ").trim()}function Wo(n,e){return n.length<=e?n:`${n.slice(0,Math.max(0,e-3)).trimEnd()}...`}function Yn(n){return n.replace(/\b[A-Za-z0-9_-]*(?:api[_-]?key|token|secret|password|passwd|pwd|credential|credentials|authorization|auth)[A-Za-z0-9_-]*\b\s*[:=]\s*["']?[^"'\s,;)]+/gi,"[redacted credential]").replace(/\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9_]{16,}\b/g,"[redacted token]").replace(/\bgithub_pat_[A-Za-z0-9_]{20,}\b/g,"[redacted token]").replace(/\bsk-[A-Za-z0-9_-]{20,}\b/g,"[redacted token]").replace(/\bhttps?:\/\/\S+/gi,"[redacted link]").replace(new RegExp(`\\b[A-Za-z0-9_-]{${Ia},}\\b`,"g"),"[redacted token]")}function Fo(n){return/\b[A-Za-z0-9_-]*(?:api[_-]?key|token|secret|password|passwd|pwd|credential|credentials|authorization|auth)[A-Za-z0-9_-]*\b\s*[:=]\s*["']?[^"'\s,;)]+/i.test(n)||/\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9_]{16,}\b/.test(n)||/\bgithub_pat_[A-Za-z0-9_]{20,}\b/.test(n)||/\bsk-[A-Za-z0-9_-]{20,}\b/.test(n)||/\bhttps?:\/\/\S+/i.test(n)||new RegExp(`\\b[A-Za-z0-9_-]{${Ia},}\\b`).test(n)}function re(n){return[n.startedAt,n.worktreeBranch??"unknown-branch",n.worktreePath??"unknown-worktree"].join(":")}var Jn=class{constructor(e){this.deps=e;this.actions=new jn({shouldRunWorktreeStrategy:e.shouldRunWorktreeStrategy,isAlreadyMerged:e.isAlreadyMerged,resolveWorktreeRepoDir:e.resolveWorktreeRepoDir,getWorktreeCompletionState:e.getWorktreeCompletionState,isPrAvailable:e.isPrAvailable??(()=>!0),resolveRepoPolicy:e.resolveRepoPolicy})}deps;actions;buildConflictResolverPrompt(e){return["Resolve the git rebase conflict for the auto-merge worktree and finish the rebase cleanly.","",`Original session: ${e.session.name} [${e.session.id}]`,`Repository root: ${e.repoDir}`,`Conflicted worktree: ${e.worktreePath}`,`Branch: ${e.branchName}`,`Base branch: ${e.baseBranch}`,"","Requirements:","- Work only inside the conflicted worktree.","- Inspect the current rebase state and resolve only the necessary conflict hunks.","- Make only minimal follow-up edits needed to keep the rebased branch correct.","- Continue the rebase until it completes successfully.","- Run relevant local verification before you finish.","- Do not broaden scope or start unrelated refactors.",`- Stop only when the branch is cleanly rebased onto ${e.baseBranch}.`,e.mergeError?"":void 0,e.mergeError?"Rebase failure details:":void 0,e.mergeError].filter(t=>typeof t=="string").join(`
|
|
188
193
|
`)}notifyAutoMergeConflictEscalation(e,t,r,o,i=[]){this.deps.dispatchSessionNotification(e,{label:"worktree-merge-conflict-escalated",idempotencyKey:`worktree-merge-conflict-escalated:${e.id}:${t}:${re(e)}`,userMessage:[`⚠️ [${e.name}] Auto-merge could not finish after one conflict-resolution attempt.`,`Branch \`${t}\` was preserved for manual follow-up.`,"",r,...i.map(s=>`⚠️ ${s}`)].join(`
|
|
189
|
-
`),buttons:this.getPolicyAwareWorktreeDecisionButtons(e.id,o,{},[[this.deps.makeOpenPrButton(e.id)]])})}updatePersistedSessionFor(e,t){for(let r of z(e))this.deps.updatePersistedSession(r,t)}markPendingDecision(e,t={}){this.updatePersistedSessionFor(e,
|
|
190
|
-
`)}),{notificationSent:!0,worktreeRemoved:!1}}handleAutoMergeSuccess(e,t,r,o,i,s,a){let l=L(e),d=l||!
|
|
191
|
-
⚠️ Pre-merge stash pop conflicted — run \`git stash show ${a.stashRef??"stash@{0}"}\` in ${t} to review stashed changes.`:a.stashed&&(
|
|
192
|
-
(Pre-existing changes on ${i} were auto-stashed and restored.)`),
|
|
194
|
+
`),buttons:this.getPolicyAwareWorktreeDecisionButtons(e.id,o,{},[[this.deps.makeOpenPrButton(e.id)]])})}updatePersistedSessionFor(e,t){for(let r of z(e))this.deps.updatePersistedSession(r,t)}markPendingDecision(e,t={}){this.updatePersistedSessionFor(e,Kt(e,t))}markAutoMergeConflictResolving(e,t,r){this.updatePersistedSessionFor(e,ui(e,t,r,{notes:[`resolver_session:${t}`]}))}markMerged(e){this.updatePersistedSessionFor(e,qt(e,{clearResolverSessionId:!0}))}markReleased(e,t=[]){let r=new Date().toISOString();this.updatePersistedSessionFor(e,{lifecycle:"terminal",worktreeState:"released",pendingWorktreeDecisionSince:void 0,lastWorktreeReminderAt:void 0,worktreeDecisionSnoozedUntil:void 0,worktreeLifecycle:{state:"released",updatedAt:r,resolvedAt:r,resolutionSource:"lifecycle_resolver",baseBranch:e.worktreeBaseBranch,targetRepo:e.worktreePrTargetRepo,pushRemote:e.worktreePushRemote,notes:t}})}async handleWorktreeStrategy(e){let t=await this.actions.plan(e);if(t.kind==="skip")return t.result;if(t.kind==="notify")return this.deps.dispatchSessionNotification(e,{label:t.label,idempotencyKey:["worktree-action",e.id,t.label,e.startedAt,e.worktreeBranch??"unknown",e.worktreePath??"unknown"].join(":"),userMessage:t.message}),{notificationSent:!0,worktreeRemoved:!1};if(t.kind==="dirty-uncommitted")return this.handleDirtyUncommittedCompletion(e,t.worktreePath,t.branchName,t.baseBranch);if(t.kind==="no-change")return this.handleNoChange(e,t.repoDir,t.worktreePath,t.branchName,t.nativeBackendWorktree);if(t.kind==="merged"){let r=t.nativeBackendWorktree?!0:B(t.repoDir,t.worktreePath);return H(t.repoDir,t.branchName),this.markMerged(e),{notificationSent:!1,worktreeRemoved:r}}if(t.kind==="released"){let r=t.nativeBackendWorktree?!0:B(t.repoDir,t.worktreePath);return H(t.repoDir,t.branchName),this.markReleased(e,t.reasons),{notificationSent:!1,worktreeRemoved:r}}return t.policyBlocked?this.shouldUpdateExistingOpenPr(e,t.repoDir,t.branchName,t.baseBranch)?this.handleAutoPrStrategy(e,t.repoDir,t.worktreePath,t.branchName,t.baseBranch,{merge:!1,pr:!0}):(this.markPendingDecision(e,{notes:t.policyReason?[t.policyReason]:void 0}),this.deps.dispatchSessionNotification(e,{label:"worktree-policy-blocked",idempotencyKey:`worktree-policy-blocked:${e.id}:${t.branchName}:${t.baseBranch}:${re(e)}`,userMessage:`⚠️ [${e.name}] ${t.policyReason??"Repo policy blocked automatic follow-through."}`,buttons:this.getPolicyAwareWorktreeDecisionButtons(e.id,t.allowedActions,{allowDelegate:!0})}),{notificationSent:!0,worktreeRemoved:!1}):t.strategy==="ask"?this.shouldUpdateExistingOpenPr(e,t.repoDir,t.branchName,t.baseBranch)?this.handleAutoPrStrategy(e,t.repoDir,t.worktreePath,t.branchName,t.baseBranch,t.allowedActions):await this.handleAskStrategy(e,t.branchName,t.baseBranch,t.diffSummary,t.allowedActions,t.policyReason):t.strategy==="delegate"?this.handleDelegateStrategy(e,t.branchName,t.baseBranch,t.diffSummary,t.allowedActions,t.policyReason):t.strategy==="auto-merge"?{notificationSent:!0,worktreeRemoved:await this.handleAutoMergeStrategy(e,t.repoDir,t.worktreePath,t.branchName,t.baseBranch,t.diffSummary,t.sessionRef,t.allowedActions)}:t.strategy==="auto-pr"?this.handleAutoPrStrategy(e,t.repoDir,t.worktreePath,t.branchName,t.baseBranch,t.allowedActions):{notificationSent:!1,worktreeRemoved:!1}}async handleNoChange(e,t,r,o,i=L(e)){if(this.hasCurrentlyOpenPrForBranch(e,t,o)){let l=new Date().toISOString();return this.updatePersistedSessionFor(e,{lifecycle:"terminal",worktreeState:"pr_open",pendingWorktreeDecisionSince:void 0,lastWorktreeReminderAt:void 0,worktreeDecisionSnoozedUntil:void 0,worktreeLifecycle:{state:"pr_open",updatedAt:l,resolutionSource:e.worktreeLifecycle?.resolutionSource??"agent_pr",baseBranch:e.worktreeBaseBranch,targetRepo:e.worktreePrTargetRepo,pushRemote:e.worktreePushRemote,notes:["no_new_worktree_commits_preserved_open_pr"]}}),this.deps.dispatchSessionNotification(e,this.deps.worktreeMessages.buildNoChangeNotification({session:e,nativeBackendWorktree:i,cleanupSucceeded:!0,worktreePath:r,worktreeBranch:o,preview:this.deps.getOutputPreview(e),originThreadLine:this.deps.originThreadLine(e),preservedSummary:"existing PR worktree preserved until merge"})),{notificationSent:!0,worktreeRemoved:!1}}let s=this.getDeliveredRemoteOutcome(e),a=i?!0:B(t,r);return a?(e.worktreePath=void 0,this.updatePersistedSessionFor(e,{worktreePath:void 0,worktreeDisposition:"no-change-cleaned",worktreeState:"none",worktreeLifecycle:{state:"no_change",updatedAt:new Date().toISOString(),resolvedAt:new Date().toISOString(),resolutionSource:"strategy_no_change",baseBranch:e.worktreeBaseBranch,targetRepo:e.worktreePrTargetRepo,pushRemote:e.worktreePushRemote}}),this.deps.dispatchSessionNotification(e,this.deps.worktreeMessages.buildNoChangeNotification({session:e,nativeBackendWorktree:i,cleanupSucceeded:!0,worktreePath:r,worktreeBranch:o,preview:this.deps.getOutputPreview(e),originThreadLine:this.deps.originThreadLine(e),remoteOutcome:s}))):this.deps.dispatchSessionNotification(e,this.deps.worktreeMessages.buildNoChangeNotification({session:e,nativeBackendWorktree:i,cleanupSucceeded:!1,worktreePath:r,worktreeBranch:o,preview:this.deps.getOutputPreview(e),originThreadLine:this.deps.originThreadLine(e),remoteOutcome:s})),{notificationSent:!0,worktreeRemoved:a}}hasCurrentlyOpenPrForBranch(e,t,r){return this.deps.hasOpenPrForBranch?.(t,r,e.worktreePrTargetRepo)===!0}getDeliveredRemoteOutcome(e){return z(e).map(r=>this.deps.getPersistedSession?.(r)).find(r=>r?.worktreeRemoteOutcome==="pr-updated"||r?.worktreeRemoteOutcome==="pr-opened")?.worktreeRemoteOutcome}resolveExistingTargetPr(e,t,r,o){let i=Dr(t,e.worktreePrTargetRepo);if(e.worktreePrUrl){let l=this.getPrStatusForUrl(t,e.worktreePrUrl,i);if(l.exists&&l.baseRefName===o)return l}let s=e.worktreeParentBranch;if(!s||oe(t)!==s||s===r||s===o)return;let a=this.deps.getPrStatusForBranch?.(t,s,i)??it(t,s,i);return a.exists&&a.state==="open"&&a.headRefName===s&&(!a.baseRefName||a.baseRefName===o)?a:void 0}shouldUpdateExistingOpenPr(e,t,r,o){return e.worktreeStrategy==="auto-pr"&&(this.hasCurrentlyOpenPrForBranch(e,t,r)||this.resolveExistingTargetPr(e,t,r,o)?.state==="open")}async handleAskStrategy(e,t,r,o,i,s){let a=await xa({sessionName:e.name,prompt:e.prompt,diffSummary:o,outputPreview:this.deps.getOutputPreview(e,4e3),provider:this.deps.worktreeSummaryProvider});return this.deps.dispatchSessionNotification(e,this.deps.worktreeMessages.buildAskNotification({session:e,branchName:t,baseBranch:r,diffSummary:o,summaryLines:a.lines,policyReason:s,buttons:this.getPolicyAwareWorktreeDecisionButtons(e.id,i)})),this.markPendingDecision(e),{notificationSent:!0,worktreeRemoved:!1}}handleDelegateStrategy(e,t,r,o,i,s){return this.deps.dispatchSessionNotification(e,this.deps.worktreeMessages.buildDelegateNotification({session:e,branchName:t,baseBranch:r,diffSummary:o,policyReason:s,allowedActions:i,originThreadLine:this.deps.originThreadLine(e)})),this.markPendingDecision(e),{notificationSent:!0,worktreeRemoved:!1}}getPolicyAwareWorktreeDecisionButtons(e,t,r={},o){if(this.deps.getPolicyAwareWorktreeDecisionButtons)return this.deps.getPolicyAwareWorktreeDecisionButtons(e,r,t);if(o&&t.pr)return o;if(!(o&&!t.pr&&!t.merge))return this.deps.getWorktreeDecisionButtons(e)}handleDirtyUncommittedCompletion(e,t,r,o){this.markPendingDecision(e,{notes:["dirty_uncommitted_completion"]});let i=nt(t),s=i.slice(0,20).map(l=>`- ${l}`),a=i.length>20?[`- ...and ${i.length-20} more`]:[];return this.deps.dispatchSessionNotification(e,{label:"worktree-dirty-uncommitted",idempotencyKey:`worktree-dirty-uncommitted:${e.id}:${r}:${o}:${re(e)}`,userMessage:[`⚠️ [${e.name}] Session completed with uncommitted worktree changes.`,"",`Branch \`${r}\` has no commits ahead of \`${o}\`, so merge/PR follow-through is blocked until the worktree is fixed.`,`Worktree: ${t}`,"",...s.length>0?["Dirty entries:",...s,...a,""]:[],"Resume the session or inspect the worktree, then commit real task changes or clean temporary files. Discard only if these local changes should be permanently removed."].join(`
|
|
195
|
+
`)}),{notificationSent:!0,worktreeRemoved:!1}}handleAutoMergeSuccess(e,t,r,o,i,s,a){let l=L(e),d=l||!li(r)||B(t,r);d&&(e.worktreePath=void 0,this.updatePersistedSessionFor(e,{worktreePath:void 0}),l||H(t,o)),this.markMerged(e);let u=Bt({kind:"merge",branch:o,base:i,filesChanged:s.filesChanged,insertions:s.insertions,deletions:s.deletions}),m=u;a.stashPopConflict?m+=`
|
|
196
|
+
⚠️ Pre-merge stash pop conflicted — run \`git stash show ${a.stashRef??"stash@{0}"}\` in ${t} to review stashed changes.`:a.stashed&&(m+=`
|
|
197
|
+
(Pre-existing changes on ${i} were auto-stashed and restored.)`),m=ie(m,a);let g=ot(a),S=[a.fastForward?"Merge type: fast-forward.":"Merge type: merge commit.",`Auto-merge landed ${o} into ${i}.`,"Local worktree branch cleanup was requested.",...a.stashPopConflict?[`Pre-merge stash pop conflicted; run git stash show ${a.stashRef??"stash@{0}"} in ${t} to review stashed changes.`]:[],...!a.stashPopConflict&&a.stashed?[`Pre-existing changes on ${i} were auto-stashed and restored.`]:[],...g];return this.deps.dispatchSessionNotification(e,{label:"worktree-merge-success",idempotencyKey:`worktree-merge-success:${e.id}:${o}:${i}:${re(e)}`,userMessage:m,notifyUser:"always",completionSummary:{required:!0,producer:"worktree",outcomeKey:`worktree-merge:${e.id}:${o}:${i}:${re(e)}`},completionWakeSummaryRequired:!0,completionWakeOutcomeKey:`worktree-merge:${e.id}:${o}:${i}:${re(e)}`,deferConditionalWakeUntilNextTick:!0,wakeMessageOnNotifySuccess:ze({sessionId:e.id,sessionName:e.name,outcomeLine:u,originThreadLine:this.deps.originThreadLine(e),detailLines:S,canonicalStatusDelivered:!0}),wakeMessageOnNotifyFailed:ze({sessionId:e.id,sessionName:e.name,outcomeLine:u,originThreadLine:this.deps.originThreadLine(e),detailLines:S,canonicalStatusDelivered:!1})}),d}async handleInitialAutoMergeConflict(e,t,r,o,i,s,a,l){let d=l?ot(l):[],u=e.autoMergeConflictResolutionAttemptCount??0;if(u>=1){this.markPendingDecision(e,{notes:["auto_merge_conflict_retry_exhausted"],clearResolverSessionId:!0}),this.notifyAutoMergeConflictEscalation(e,o,s.pr?`The rebased branch still conflicts with \`${i}\`. Open a PR or resolve manually in ${r}.`:`The rebased branch still conflicts with \`${i}\`. Resolve manually in ${r}.`,s,d);return}let m=this.buildConflictResolverPrompt({session:e,repoDir:t,worktreePath:r,branchName:o,baseBranch:i,mergeError:a});try{let g=await this.deps.spawnConflictResolver({session:e,repoDir:t,worktreePath:r,branchName:o,baseBranch:i,prompt:m});this.markAutoMergeConflictResolving(e,g.id,u+1),this.deps.dispatchSessionNotification(e,{label:"worktree-merge-conflict-resolving",idempotencyKey:`worktree-merge-conflict-resolving:${e.id}:${o}:${g.id}`,userMessage:[`⚠️ [${e.name}] Auto-merge hit a rebase conflict. Started resolver session ${g.name} and will retry automatically if it succeeds.`,...d.map(S=>`⚠️ ${S}`)].join(`
|
|
193
198
|
`)})}catch(g){this.markPendingDecision(e,{notes:["auto_merge_conflict_resolver_spawn_failed"]}),this.deps.dispatchSessionNotification(e,{label:"worktree-merge-conflict-spawn-failed",idempotencyKey:`worktree-merge-conflict-spawn-failed:${e.id}:${o}:${re(e)}`,userMessage:[`❌ [${e.name}] Auto-merge hit a rebase conflict and failed to start the resolver: ${g instanceof Error?g.message:String(g)}`,...d.map(S=>`⚠️ ${S}`)].join(`
|
|
194
199
|
`),buttons:this.getPolicyAwareWorktreeDecisionButtons(e.id,s,{},[[this.deps.makeOpenPrButton(e.id)]])})}}handleAutoMergeRetryFailure(e,t,r,o,i){this.markPendingDecision(e,{notes:["auto_merge_conflict_retry_failed"],clearResolverSessionId:!0}),this.deps.dispatchSessionNotification(e,{label:"worktree-merge-error",idempotencyKey:`worktree-merge-error:${e.id}:${t}:${re(e)}`,userMessage:[o,"","Auto-merge retry did not complete after conflict resolution.",`Branch \`${t}\` was preserved for manual follow-up in ${r}.`].join(`
|
|
195
|
-
`),buttons:this.getPolicyAwareWorktreeDecisionButtons(e.id,i,{},[[this.deps.makeOpenPrButton(e.id)]])})}async handleAutoMergeStrategy(e,t,r,o,i,s,a=D(e)??e.harnessSessionId,l={merge:!0,pr:!0}){if(this.deps.isAlreadyMerged(a)||e.autoMergeResolverSessionId)return!1;let d=!1;return await this.deps.enqueueMerge(t,async()=>{if(this.deps.isAlreadyMerged(a))return;let u=this.deps.mergeBranch(t,o,i,"merge",r);if(u.success){d=this.handleAutoMergeSuccess(e,t,r,o,i,s,u);return}if(u.rebaseConflict){await this.handleInitialAutoMergeConflict(e,t,r,o,i,l,u.error,u);return}let f=ie(u.dirtyError?`❌ [${e.name}] Merge blocked: ${u.error}`:`❌ [${e.name}] Merge failed: ${u.error??"unknown error"}`,u);if(e.worktreeState==="merge_conflict_resolving"||e.worktreeLifecycle?.state==="merge_conflict_resolving"){this.handleAutoMergeRetryFailure(e,o,r,f,l);return}this.deps.dispatchSessionNotification(e,{label:"worktree-merge-error",idempotencyKey:`worktree-merge-error:${e.id}:${o}:${re(e)}`,userMessage:f})},()=>{this.deps.dispatchSessionNotification(e,{label:"worktree-merge-queued",idempotencyKey:`worktree-merge-queued:${e.id}:${o}:${re(e)}`,userMessage:`🕐 [${e.name}] Merge queued — another merge for this repo is in progress. Will notify when complete.`})}),d}async handleAutoPrStrategy(e,t,r,o,i,s={merge:!0,pr:!0}){let a=this.releaseIfRepresentedByTargetPrBranch(e,t,r,o,i);if(a)return a;if(this.updatePersistedSessionFor(e,{lifecycle:"terminal",worktreeState:"pr_in_progress"}),!(await this.deps.runAutoPr(e,i)).success){let d=this.releaseIfRepresentedByTargetPrBranch(e,t,r,o,i);if(d)return d;this.markPendingDecision(e),this.deps.dispatchSessionNotification(e,{label:"worktree-auto-pr-failed",idempotencyKey:`worktree-auto-pr-failed:${e.id}:${i}:${re(e)}`,userMessage:`⚠️ [${e.name}] Auto-PR did not complete. The worktree is preserved for an explicit merge or PR decision.`,buttons:this.getPolicyAwareWorktreeDecisionButtons(e.id,s)})}return{notificationSent:!0,worktreeRemoved:!1}}getPrStatusForUrl(e,t,r){return this.deps.getPrStatusForUrl?.(e,t,r)??Et(e,t,r)}releaseIfRepresentedByTargetPrBranch(e,t,r,o,i){if(Ze(r).length>0)return;let s=this.resolveExistingTargetPr(e,t,o,i),a=s?.headRefName;if(!a||a===o||a===i)return;let l=this.deps.fetchRemoteBranch?this.deps.fetchRemoteBranch(t,a):Xo(t,a);if(!l||!!!((s?.state==="open"||s?.state==="merged")&&s?.baseRefName===i&&_t(t,o,l)))return;let u=L(e)?!0:B(t,r);return u?(e.worktreePath=void 0,e.worktreePrUrl=s.url,e.worktreePrNumber=s.number,e.worktreePrTargetRepo=Tr(t,e.worktreePrTargetRepo),this.updatePersistedSessionFor(e,{worktreePath:void 0,worktreePrUrl:s.url,worktreePrNumber:s.number,worktreePrTargetRepo:e.worktreePrTargetRepo,worktreeRemoteOutcome:"pr-updated"}),H(t,o),this.markReleased(e,[`released_by_branch:${a}`]),{notificationSent:!1,worktreeRemoved:u}):(this.markPendingDecision(e,{notes:[`represented_by_branch:${a}`,"represented_worktree_cleanup_failed"]}),{notificationSent:!1,worktreeRemoved:!1})}};import{existsSync as dm,statSync as um,writeFileSync as cm}from"fs";import{randomUUID as Ap}from"crypto";function wa(n){return n==="plan-approve"||n==="plan-request-changes"||n==="plan-reject"}var zn=class{constructor(e,t){this.onChange=e;this.retentionMs=t}onChange;retentionMs;tokens=new Map;afterChange;replaceAll(e){this.tokens.clear();for(let t of e)this.tokens.set(t.id,t)}clear(){this.tokens.clear(),this.notifyChanged()}listForPersistence(){return[...this.tokens.values()]}createActionToken(e,t,r={}){let o={id:Ap(),sessionId:e,kind:t,createdAt:Date.now(),...r};return this.tokens.set(o.id,o),this.notifyChanged(),o}getActionToken(e){let t=this.tokens.get(e);if(t){if(t.expiresAt!=null&&t.expiresAt<=Date.now()){this.tokens.delete(e),this.notifyChanged();return}return t}}listActiveActionTokens(e){let t=[];for(let r of[...this.tokens.keys()]){let o=this.getActionToken(r);!o||o.consumedAt!=null||e&&o.kind!==e||t.push(o)}return t}consumeActionToken(e){let t=this.getActionToken(e);if(!(!t||t.consumedAt!=null))return t.consumedAt=Date.now(),this.notifyChanged(),t}consumeQuestionAnswerTokens(e,t,r){let o=[],i=Date.now();for(let s of this.tokens.values())s.sessionId!==e||s.kind!=="question-answer"||s.pendingInputRequestId!==t||r!=null&&s.pendingInputQuestionId!==r||s.consumedAt!=null||(s.consumedAt=i,o.push(s));return o.length>0&&this.notifyChanged(),o}consumePlanDecisionTokens(e,t){let r=[],o=Date.now();for(let i of this.tokens.values())i.sessionId!==e||!wa(i.kind)||i.planDecisionVersion!==t||i.consumedAt!=null||(i.consumedAt=o,r.push(i));return r.length>0&&this.notifyChanged(),r}deleteActionTokensForSession(e){let t=!1;for(let[r,o]of this.tokens)o.sessionId===e&&(this.tokens.delete(r),t=!0);t&&this.notifyChanged()}deleteActionTokensForSessionByKind(e,t){let r=!1;for(let[o,i]of this.tokens)i.sessionId===e&&i.kind===t&&(this.tokens.delete(o),r=!0);r&&this.notifyChanged()}deletePlanDecisionTokensForSession(e,t){let r=!1;for(let[o,i]of this.tokens)i.sessionId!==e||!wa(i.kind)||t!=null&&i.planDecisionVersion===t||(this.tokens.delete(o),r=!0);r&&this.notifyChanged()}purgeExpiredActionTokens(e=Date.now()){let t=!1;for(let[r,o]of this.tokens){let i=o.expiresAt!=null&&o.expiresAt<=e,s=o.consumedAt!=null&&e-o.consumedAt>=this.retentionMs;(i||s)&&(this.tokens.delete(r),t=!0)}return t&&this.notifyChanged(),t}nextExpiryAt(){let e;for(let t of this.tokens.values()){let r=[t.expiresAt,t.consumedAt!=null?t.consumedAt+this.retentionMs:void 0].filter(o=>o!=null&&Number.isFinite(o));for(let o of r)(e==null||o<e)&&(e=o)}return e}setAfterChangeListener(e){this.afterChange=e}notifyChanged(){this.onChange(),this.afterChange?.()}};var Hn=class{constructor(e){this.indexes=e}indexes;hasRecordedSession(e){return this.indexes.idIndex.has(e)}getLatestPersistedByName(e){let t,r=Number.NEGATIVE_INFINITY,o=Number.NEGATIVE_INFINITY,i=Number.NEGATIVE_INFINITY,s=0;for(let a of this.indexes.persisted.values()){if(a.name!==e){s++;continue}let l=a.createdAt??Number.NEGATIVE_INFINITY,d=a.completedAt??Number.NEGATIVE_INFINITY;(l>r||l===r&&d>o||l===r&&d===o&&s>i)&&(t=a,r=l,o=d,i=s),s++}return t}resolveBackendConversationId(e,t){if(t)return t;let r=this.indexes.idIndex.get(e);if(r){let s=this.indexes.persisted.get(r);return(s?C(s):void 0)??r}let o=this.getLatestPersistedByName(e);if(o)return C(o)??o.harnessSessionId;let i=this.indexes.backendIdIndex.get(e);if(i){let s=this.indexes.persisted.get(i);return(s?C(s):void 0)??i}if(this.indexes.persisted.has(e)){let s=this.indexes.persisted.get(e);return s?C(s)??e:e}if(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(e))return e}resolveHarnessSessionId(e,t){return this.resolveBackendConversationId(e,t)}getPersistedSession(e){let t=this.indexes.idIndex.get(e);if(t)return this.indexes.persisted.get(t);let r=this.getLatestPersistedByName(e);if(r)return r;let o=this.indexes.backendIdIndex.get(e);return o?this.indexes.persisted.get(o):this.indexes.persisted.get(e)}listPersistedSessions(){return[...this.indexes.persisted.values()].sort((e,t)=>(t.completedAt??0)-(e.completedAt??0))}};import{existsSync as $a,mkdirSync as Zp,readFileSync as em,readdirSync as tm,renameSync as Ea,statSync as Na,unlinkSync as Oa,writeFileSync as Wa}from"fs";import{tmpdir as nm}from"os";import{dirname as rm,join as Fa}from"path";import{homedir as Ip}from"node:os";import{join as Tp}from"node:path";function Qn(n){let e=n.OPENCLAW_HOME?.trim();if(e)return e;let t=n.HOME?.trim()||Ip();return Tp(t,".openclaw")}var Yn=7,xp=new Set(["running","completed","failed","killed"]),Cp=new Set(["queued","running","waiting","blocked","succeeded","failed","cancelled","lost"]);function ae(n){return!!n&&typeof n=="object"}function je(n,e=""){return typeof n=="string"&&n.trim().length>0?n:e}function P(n){return typeof n=="string"&&n.trim().length>0?n:void 0}function q(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function Mp(n){if(!ae(n))return;let e=P(n.flowId),t=q(n.revision);if(!e||t===void 0)return;let r=typeof n.status=="string"&&Cp.has(n.status)?n.status:void 0;return{flowId:e,revision:t,...r?{status:r}:{}}}function ba(n){return typeof n=="string"&&is.has(n)?n:void 0}function Do(n){return n==="default"||n==="plan"||n==="bypassPermissions"?n:void 0}function _p(n){return n==="never"||n==="on-request"?"never":void 0}function Ra(n){return n==="approve"||n==="ask"||n==="delegate"?n:void 0}function Dp(n){if(n==="plan-mode"||n==="codex-first-turn-plan"||n==="soft-plan")return"plan-mode"}function Aa(n){return typeof n=="string"&&vn.has(n)?n:void 0}function $o(n){return typeof n=="string"&&os.has(n)?n:void 0}function Ia(n){return n==="github"||n==="unsupported"?n:void 0}function $p(n){return n==="stored"||n==="seeded"||n==="unknown"?n:void 0}function Ep(n){return n==="user"||n==="idle-timeout"||n==="startup-timeout"||n==="shutdown"||n==="done"||n==="unknown"?n:void 0}function Ta(n){return n==="starting"||n==="active"||n==="awaiting_plan_decision"||n==="awaiting_user_input"||n==="awaiting_worktree_decision"||n==="suspended"||n==="terminal"?n:void 0}function Np(n){return n==="not_required"||n==="pending"||n==="approved"||n==="changes_requested"||n==="rejected"?n:void 0}function Op(n){return n==="none"||n==="provisioned"||n==="pending_decision"||n==="merge_conflict_resolving"||n==="merge_in_progress"||n==="pr_in_progress"||n==="merged"||n==="released"||n==="pr_open"||n==="dismissed"||n==="cleanup_failed"?n:void 0}function Wp(n){return n==="none"||n==="provisioned"||n==="pending_decision"||n==="merge_conflict_resolving"||n==="pr_open"||n==="merged"||n==="released"||n==="dismissed"||n==="no_change"||n==="cleanup_failed"?n:void 0}function xa(n){return n==="live"||n==="stopped"?n:void 0}function Fp(n){if(!ae(n)||n.reason!=="persisted-running-without-runtime")return;let e=P(n.recoveredAt);if(!e||!Number.isFinite(Date.parse(e)))return;let t=n.normalizedStatus==="killed"?"killed":void 0;if(t)return{reason:"persisted-running-without-runtime",recoveredAt:e,rawStatus:P(n.rawStatus),rawLifecycle:P(n.rawLifecycle),rawRuntimeState:P(n.rawRuntimeState),rawResumable:typeof n.rawResumable=="boolean"?n.rawResumable:void 0,rawCompletedAt:q(n.rawCompletedAt),rawOutputPath:P(n.rawOutputPath),normalizedStatus:t,normalizedLifecycle:Ta(n.normalizedLifecycle),normalizedRuntimeState:xa(n.normalizedRuntimeState)}}function Lp(n){return{reason:"persisted-running-without-runtime",recoveredAt:new Date().toISOString(),rawStatus:P(n.status),rawLifecycle:P(n.lifecycle),rawRuntimeState:P(n.runtimeState),rawResumable:typeof n.resumable=="boolean"?n.resumable:void 0,rawCompletedAt:q(n.completedAt),rawOutputPath:P(n.outputPath),normalizedStatus:"killed",normalizedLifecycle:"suspended",normalizedRuntimeState:"stopped"}}function Bp(n){return n==="not_sent"||n==="sending"||n==="delivered"||n==="fallback_delivered"||n==="failed"?n:void 0}function Kp(n){return n==="direct-telegram"?"direct-message":n==="none"||n==="direct-message"||n==="wake-only"?n:void 0}function qp(n){return n==="none"||n==="canonical_buttons"||n==="explicit_fallback_text"?n:void 0}function Vp(n){return n==="idle"||n==="notifying"||n==="wake_pending"||n==="failed"?n:void 0}function Up(n){return n==="in_flight"||n==="delivered"?n:void 0}function Gp(n){if(!Array.isArray(n))return;let e=n.map(t=>{if(!ae(t))return;let r=P(t.key),o=Up(t.status),i=P(t.recordedAt);if(!(!r||!o||!i||!Number.isFinite(Date.parse(i))))return{key:r,status:o,recordedAt:i,label:P(t.label)}}).filter(t=>!!t);return e.length>0?e.slice(-64):void 0}function jp(n){if(!Array.isArray(n))return;let e=n.map(t=>{if(!ae(t))return;let r=P(t.key),o=P(t.recordedAt);if(!r||!o||!Number.isFinite(Date.parse(o)))return;let i=Array.isArray(t.linkedKeys)?[...new Set(t.linkedKeys.map(P).filter(s=>!!(s&&s!==r)))]:void 0;return{key:r,linkedKeys:i&&i.length>0?i:void 0,recordedAt:o,label:P(t.label),skipReason:P(t.skipReason)}}).filter(t=>!!t);return e.length>0?e.slice(-1024):void 0}function zp(n){return n==="awaiting_plan_output"||n==="awaiting_approval"||n==="approved_then_implemented"||n==="implemented_without_required_approval"||n==="not_plan_gated"?n:void 0}function Hp(n){return n==="plan-approve"||n==="plan-request-changes"||n==="plan-reject"||n==="plan-offer-start"||n==="plan-offer-dismiss"||n==="repo-policy-set"||n==="worktree-merge"||n==="worktree-create-pr"||n==="worktree-update-pr"||n==="worktree-view-pr"||n==="worktree-decide-later"||n==="worktree-dismiss"||n==="session-resume"||n==="session-restart"||n==="view-output"||n==="question-answer"?n:void 0}function Ca(n){if(!ae(n))return;let e=P(n.provider),t=P(n.accountId),r=P(n.target),o=P(n.threadId),i=P(n.sessionKey);if(!(!e||!r))return{provider:e,accountId:t,target:r,threadId:o,sessionKey:i}}function Qp(n,e,t){if(ae(n)){let r=P(n.kind),o=P(n.conversationId);if(o&&(r==="claude-code"||r==="codex-app-server"||r==="opencode-server"))return{kind:r,conversationId:o,runId:P(n.runId),worktreeId:P(n.worktreeId),worktreePath:P(n.worktreePath)}}if(e==="claude-code")return{kind:"claude-code",conversationId:t}}function Yp(n){if(!ae(n))return;let e=Wp(n.state),t=P(n.updatedAt);if(!e||!t)return;let r=P(n.resolutionSource);return{state:e,updatedAt:t,resolvedAt:P(n.resolvedAt),resolutionSource:r==="agent_merge"||r==="agent_pr"||r==="strategy_no_change"||r==="lifecycle_resolver"||r==="dismiss"||r==="maintenance"?r:void 0,baseBranch:P(n.baseBranch),targetRepo:P(n.targetRepo),pushRemote:P(n.pushRemote),notes:Array.isArray(n.notes)?n.notes.filter(o=>typeof o=="string"&&o.length>0):void 0}}function Jp(n){let e=new Date(0).toISOString(),t=P(n.worktreeMergedAt)??P(n.worktreeDismissedAt)??P(n.pendingWorktreeDecisionSince)??e;if(n.worktreeMerged===!0||n.worktreeDisposition==="merged")return{state:"merged",updatedAt:t,resolvedAt:P(n.worktreeMergedAt)??t,resolutionSource:"agent_merge",baseBranch:P(n.worktreeBaseBranch),targetRepo:P(n.worktreePrTargetRepo),pushRemote:P(n.worktreePushRemote)};if(n.worktreeDisposition==="dismissed")return{state:"dismissed",updatedAt:t,resolvedAt:P(n.worktreeDismissedAt)??t,resolutionSource:"dismiss",baseBranch:P(n.worktreeBaseBranch),targetRepo:P(n.worktreePrTargetRepo),pushRemote:P(n.worktreePushRemote)};if(n.worktreeDisposition==="no-change-cleaned")return{state:"no_change",updatedAt:t,resolvedAt:t,resolutionSource:"strategy_no_change",baseBranch:P(n.worktreeBaseBranch),targetRepo:P(n.worktreePrTargetRepo),pushRemote:P(n.worktreePushRemote)};if(n.worktreeDisposition==="pr-opened"||n.worktreeState==="pr_open")return{state:"pr_open",updatedAt:t,baseBranch:P(n.worktreeBaseBranch),targetRepo:P(n.worktreePrTargetRepo),pushRemote:P(n.worktreePushRemote)};if(n.pendingWorktreeDecisionSince||n.worktreeState==="pending_decision")return{state:"pending_decision",updatedAt:t,baseBranch:P(n.worktreeBaseBranch),targetRepo:P(n.worktreePrTargetRepo),pushRemote:P(n.worktreePushRemote)};if(n.worktreeState==="merge_conflict_resolving")return{state:"merge_conflict_resolving",updatedAt:t,baseBranch:P(n.worktreeBaseBranch),targetRepo:P(n.worktreePrTargetRepo),pushRemote:P(n.worktreePushRemote)};if(P(n.worktreePath)||P(n.worktreeBranch))return{state:"provisioned",updatedAt:t,baseBranch:P(n.worktreeBaseBranch),targetRepo:P(n.worktreePrTargetRepo),pushRemote:P(n.worktreePushRemote)}}function Xp(n){if(typeof n=="string"&&xp.has(n))return n==="running"?"killed":n}function Ma(n){if(!ae(n))return;let e=je(n.harnessSessionId);if(!e)return;let t=Xp(n.status);if(!t)return;let r=n.status==="running",o=P(n.worktreePath),i=P(n.worktreeBranch),s=P(n.originChannel),a=typeof n.originThreadId=="string"||typeof n.originThreadId=="number"?n.originThreadId:void 0,l=P(n.originSessionKey),d=Ca(n.route);if(!d)return;let u=Z({route:d,originChannel:s,originThreadId:a,originSessionKey:l});if(!u||o&&!i)return;let f=P(n.harness),g=Qp(n.backendRef,f,e),S=Yp(n.worktreeLifecycle)??Jp(n),y=P(n.completionWakeSucceededAt),c=P(n.completionWakeSkippedAt),h=n.completionWakeSummaryRequired===!0&&!c?!0:void 0;return{sessionId:P(n.sessionId),harnessSessionId:e,backendRef:g,name:je(n.name,e),prompt:je(n.prompt),workdir:je(n.workdir,"(unknown)"),model:P(n.model),reasoningEffort:ba(n.reasoningEffort),fastMode:n.fastMode===!0?!0:void 0,createdAt:q(n.createdAt),completedAt:q(n.completedAt),status:t,lifecycle:r?"suspended":Ta(n.lifecycle),approvalState:Np(n.approvalState),worktreeState:Op(n.worktreeState),runtimeState:r?"stopped":xa(n.runtimeState),runtimeRecovery:r?Lp(n):Fp(n.runtimeRecovery),taskFlowMirror:Mp(n.taskFlowMirror),deliveryState:Vp(n.deliveryState),notificationDedupe:Gp(n.notificationDedupe),completionSummaryDedupe:jp(n.completionSummaryDedupe),completionWakeIssuedAt:P(n.completionWakeIssuedAt),completionWakeSucceededAt:y,completionWakeFailedAt:P(n.completionWakeFailedAt),completionWakeSkippedAt:c,completionWakeSkipReason:P(n.completionWakeSkipReason),completionWakeSummaryRequired:h,killReason:Ep(n.killReason),costUsd:typeof n.costUsd=="number"&&Number.isFinite(n.costUsd)?n.costUsd:0,originAgentId:P(n.originAgentId),originChannel:s,originThreadId:a,originSessionKey:l,route:u,outputPath:P(n.outputPath),harness:f,resumedFromSessionName:P(n.resumedFromSessionName),goalTaskId:P(n.goalTaskId),requestedPermissionMode:Do(n.requestedPermissionMode),currentPermissionMode:Do(n.currentPermissionMode),approvalExecutionState:zp(n.approvalExecutionState),approvalRationale:P(n.approvalRationale),planModeApproved:n.planModeApproved===!0,pendingPlanApproval:n.pendingPlanApproval===!0,planApprovalContext:Dp(n.planApprovalContext),planDecisionVersion:q(n.planDecisionVersion),actionablePlanDecisionVersion:q(n.actionablePlanDecisionVersion),canonicalPlanPromptVersion:q(n.canonicalPlanPromptVersion),approvalPromptRequiredVersion:q(n.approvalPromptRequiredVersion),approvalPromptVersion:q(n.approvalPromptVersion),approvalPromptStatus:Bp(n.approvalPromptStatus),approvalPromptTransport:Kp(n.approvalPromptTransport),approvalPromptMessageKind:qp(n.approvalPromptMessageKind),approvalPromptLastAttemptAt:P(n.approvalPromptLastAttemptAt),approvalPromptDeliveredAt:P(n.approvalPromptDeliveredAt),approvalPromptFailedAt:P(n.approvalPromptFailedAt),planApproval:Ra(n.planApproval),codexApprovalPolicy:_p(n.codexApprovalPolicy),worktreePath:o,worktreeBranch:i,worktreeStrategy:Aa(n.worktreeStrategy),repoIntegrationPolicy:$o(n.repoIntegrationPolicy),repoIntegrationPolicySource:$p(n.repoIntegrationPolicySource),repoProvider:Ia(n.repoProvider),worktreeMerged:typeof n.worktreeMerged=="boolean"?n.worktreeMerged:void 0,worktreeMergedAt:P(n.worktreeMergedAt),worktreePrUrl:P(n.worktreePrUrl),worktreePrNumber:q(n.worktreePrNumber),pendingWorktreeDecisionSince:P(n.pendingWorktreeDecisionSince),lastWorktreeReminderAt:P(n.lastWorktreeReminderAt),worktreeBaseBranch:P(n.worktreeBaseBranch),worktreeParentBranch:P(n.worktreeParentBranch),worktreePrTargetRepo:P(n.worktreePrTargetRepo),autoMergeParentSessionId:P(n.autoMergeParentSessionId),autoMergeConflictResolutionAttemptCount:q(n.autoMergeConflictResolutionAttemptCount),autoMergeResolverSessionId:P(n.autoMergeResolverSessionId),worktreePushRemote:P(n.worktreePushRemote),worktreeDecisionSnoozedUntil:P(n.worktreeDecisionSnoozedUntil),worktreeDisposition:n.worktreeDisposition==="active"||n.worktreeDisposition==="pr-opened"||n.worktreeDisposition==="merged"||n.worktreeDisposition==="dismissed"||n.worktreeDisposition==="no-change-cleaned"?n.worktreeDisposition:void 0,worktreeRemoteOutcome:n.worktreeRemoteOutcome==="pr-opened"||n.worktreeRemoteOutcome==="pr-updated"?n.worktreeRemoteOutcome:void 0,worktreeDismissedAt:P(n.worktreeDismissedAt),worktreeLifecycle:S,resumable:r?!0:n.resumable===!0}}function _a(n){if(!ae(n))return;let e=P(n.key),t=P(n.repoRoot),r=$o(n.policy),o=Ia(n.provider)??"unsupported",i=P(n.createdAt),s=P(n.updatedAt),a=n.source==="seeded"?"seeded":"stored";if(!(!e||!t||!r||!i||!s)&&!(!Number.isFinite(Date.parse(i))||!Number.isFinite(Date.parse(s))))return{key:e,policy:r,repoRoot:t,remoteUrl:P(n.remoteUrl),provider:o,createdAt:i,updatedAt:s,source:a}}function Da(n){if(!ae(n))return;let e=Hp(n.kind),t=je(n.id),r=je(n.sessionId),o=q(n.createdAt);if(!(!t||!r||!e||o==null))return{id:t,sessionId:r,kind:e,createdAt:o,planDecisionVersion:q(n.planDecisionVersion),expiresAt:q(n.expiresAt),consumedAt:q(n.consumedAt),optionIndex:q(n.optionIndex),pendingInputRequestId:P(n.pendingInputRequestId),pendingInputQuestionId:P(n.pendingInputQuestionId),label:P(n.label),targetUrl:P(n.targetUrl),route:Ca(n.route),launchName:P(n.launchName),launchPrompt:P(n.launchPrompt),launchWorkdir:P(n.launchWorkdir),launchModel:P(n.launchModel),launchReasoningEffort:ba(n.launchReasoningEffort),launchFastMode:n.launchFastMode===!0?!0:void 0,launchSystemPrompt:P(n.launchSystemPrompt),launchAllowedTools:Array.isArray(n.launchAllowedTools)?n.launchAllowedTools.filter(i=>typeof i=="string"):void 0,launchResumeSessionId:P(n.launchResumeSessionId),launchResumedFromSessionName:P(n.launchResumedFromSessionName),launchResumeWorktreeFrom:P(n.launchResumeWorktreeFrom),launchSessionIdOverride:P(n.launchSessionIdOverride),launchClearedPersistedCodexResume:n.launchClearedPersistedCodexResume===!0?!0:void 0,launchForkSession:typeof n.launchForkSession=="boolean"?n.launchForkSession:void 0,launchForceNewSession:typeof n.launchForceNewSession=="boolean"?n.launchForceNewSession:void 0,launchPermissionMode:Do(n.launchPermissionMode),launchPlanApproval:Ra(n.launchPlanApproval),launchHarness:P(n.launchHarness),launchWorktreeStrategy:Aa(n.launchWorktreeStrategy),launchWorktreeBaseBranch:P(n.launchWorktreeBaseBranch),launchWorktreePrTargetRepo:P(n.launchWorktreePrTargetRepo),launchOriginAgentId:P(n.launchOriginAgentId),repoPolicy:$o(n.repoPolicy),repoPolicyWorkdir:P(n.repoPolicyWorkdir)}}function Jn(n){if(!n.backendRef?.kind||!n.backendRef.conversationId)throw new Error(`Persisted session ${n.harnessSessionId} is missing required backend ref metadata.`);if(!n.route?.provider||!n.route.target)throw new Error(`Persisted session ${n.harnessSessionId} is missing required route metadata.`);if(n.worktreePath&&!n.worktreeBranch)throw new Error(`Persisted session ${n.harnessSessionId} is missing required worktreeBranch metadata.`)}function ht(n){return!!n&&typeof n=="object"}function Eo(n){return n instanceof Error?n.message:String(n)}function om(n,e){JSON.stringify({component:"SessionStore",event:n,at:new Date().toISOString(),...e})}function im(n){return ht(n)?{backendRefKind:typeof n.kind=="string"?n.kind:void 0,hasBackendConversationId:typeof n.conversationId=="string"&&n.conversationId.length>0,hasBackendRunId:typeof n.runId=="string"&&n.runId.length>0,hasBackendWorktreeId:typeof n.worktreeId=="string"&&n.worktreeId.length>0,hasBackendWorktreePath:typeof n.worktreePath=="string"&&n.worktreePath.length>0}:{}}function La(n,e,t=Date.now()){let r=`${n}.${e}-${t}`;for(let o=0;o<100;o+=1){let i=o===0?"":`-${o}`,s=`${r}${i}.json`;if(!$a(s))return s}}function No(){let n=nm();return tm(n).filter(e=>e.startsWith("openclaw-agent-")&&e.endsWith(".txt")).map(e=>Fa(n,e))}function Ba(n){let e=n.OPENCLAW_CODE_AGENT_SESSIONS_PATH?.trim();return e||Fa(Qn(n),"code-agent-sessions.json")}function Ka(n,e,t,r=[]){try{Zp(rm(n),{recursive:!0});let o=n+".tmp";Wa(o,JSON.stringify({schemaVersion:Yn,sessions:e,actionTokens:t,repoPolicies:r},null,2),"utf-8"),Ea(o,n)}catch(o){`${Eo(o)}`}}function sm(n,e){try{if(!$a(n))return!1;let t=La(n,"legacy");return t?(Ea(n,t),`${e}${t}`,!0):!1}catch(t){return`${Eo(t)}`,!1}}var am={archiveLegacySessionIndex:sm};function lm(n,e){try{if(e.length===0)return;let t=La(n,"codex-sdk-legacy");if(!t)return;Wa(t,JSON.stringify(e,null,2),"utf-8"),`${e.length}${t}`}catch(t){`${Eo(t)}`}}function qa(n,e,t=[]){try{let r=new Set(t);for(let o of No())if(!r.has(o))try{let i=Na(o).mtimeMs;n-i>e&&Oa(o)}catch{}}catch{}}function Va(n,e,t=[]){try{let r=new Set(t),o;for(let i of No())if(!r.has(i))try{let s=Na(i).mtimeMs+e;if(s<=n)return n;o=o==null?s:Math.min(o,s)}catch{}return o}catch{return}}function Ua(n){try{let e=new Set(n);for(let t of No())if(!e.has(t))try{Oa(t)}catch{}}catch{}}function Ga(n){let{indexPath:e,clearAll:t,indexPersistedEntry:r,setActionToken:o,setRepoPolicy:i,purgeExpiredActionTokens:s,saveIndex:a}=n,l=d=>am.archiveLegacySessionIndex(e,d)?(t(),!0):!1;try{let d=em(e,"utf-8"),u=JSON.parse(d);if(Array.isArray(u)){if(!l("legacy array store"))return;a();return}if(!ht(u)||u.schemaVersion!==Yn&&u.schemaVersion!==6&&u.schemaVersion!==4){if(!l(`schema mismatch (expected v${Yn})`))return;a();return}let f=(R,m)=>{if(!Object.prototype.hasOwnProperty.call(u,R))return[];let w=u[R];if(Array.isArray(w))return w;l(m)&&a()},g=f("sessions","invalid sessions collection");if(g===void 0)return;let S=[],y=[],c=!1;for(let R of g){if(ht(R)&&R.harness==="codex"){let w=ht(R.backendRef)?R.backendRef:void 0;if((typeof w?.kind=="string"?w.kind:void 0)!=="codex-app-server"){S.push(R);continue}}let m=Ma(R);if(!m){if(!l("invalid v4 session entry"))return;a();return}ht(R)&&R.status==="running"&&(c=!0,om("session.recovered_from_running_persisted_row",{sessionId:typeof R.sessionId=="string"?R.sessionId:void 0,hasHarnessSessionId:typeof R.harnessSessionId=="string"&&R.harnessSessionId.length>0,...im(R.backendRef),rawStatus:R.status,rawLifecycle:R.lifecycle,rawRuntimeState:R.runtimeState,normalizedStatus:m.status,normalizedLifecycle:m.lifecycle,normalizedRuntimeState:m.runtimeState,reason:m.runtimeRecovery?.reason})),y.push(m)}S.length>0&&lm(e,S);let h=f("actionTokens","invalid action token collection");if(h===void 0)return;let v=[];for(let R of h){let m=Da(R);if(!m){if(!l("invalid v4 action token"))return;a();return}v.push(m)}let b=f("repoPolicies","invalid repo policy collection");if(b===void 0)return;let A=[],I=!1;for(let R of b){let m=_a(R);if(!m){I=!0;continue}A.push(m)}for(let R of y)r(R);for(let R of v)o(R);for(let R of A)i(R);(S.length>0||c)&&a(),I&&a(),s()}catch(d){if(d.code==="ENOENT"||!l("corrupt or unreadable"))return;a()}}var pm=new Set(["completed","failed","killed"]),Oo=1440*60*1e3;function mm(n){return n instanceof Error?n.message:String(n)}var fm={statSync:um};function gm(n){try{return fm.statSync(n).isDirectory()}catch(e){let t=e.code;return t!=="ENOENT"&&t!=="ENOTDIR"}}var Xn=class{persisted=new Map;repoPolicies=new Map;idIndex=new Map;nameIndex=new Map;backendIdIndex=new Map;actionTokens;actionTokenStore;indexPath;queries;constructor(e={}){let t=e.env??process.env;this.indexPath=e.indexPath??Ba(t),this.actionTokenStore=new zn(()=>this.saveIndex(),Oo),this.actionTokens=this.actionTokenStore.tokens,this.queries=new Hn({persisted:this.persisted,idIndex:this.idIndex,nameIndex:this.nameIndex,backendIdIndex:this.backendIdIndex}),t.OPENCLAW_DEBUG_SESSION_STORE==="1"&&`${this.indexPath}`,this.loadIndex()}loadIndex(){Ga({indexPath:this.indexPath,clearAll:()=>{this.persisted.clear(),this.repoPolicies.clear(),this.idIndex.clear(),this.nameIndex.clear(),this.backendIdIndex.clear(),this.actionTokenStore.clear()},indexPersistedEntry:e=>this.indexPersistedEntry(e),setActionToken:e=>{this.actionTokens.set(e.id,e)},setRepoPolicy:e=>{this.repoPolicies.set(e.key,e)},purgeExpiredActionTokens:()=>this.actionTokenStore.purgeExpiredActionTokens(),saveIndex:()=>this.saveIndex()})}saveIndex(){Ka(this.indexPath,[...this.persisted.values()],this.actionTokenStore.listForPersistence(),[...this.repoPolicies.values()])}assertPersistedEntry(e){Jn(e)}getEntryStorageKey(e){return e.harnessSessionId}buildPersistedBackendRef(e){let t=Qo(e),r=t==="codex"?"codex-app-server":t==="opencode"?"opencode-server":"claude-code";return e.backendRef??{kind:e.backendKind??r,conversationId:e.harnessSessionId}}getSessionApprovalSnapshot(e){if(typeof e.approvalSnapshot=="function")return e.approvalSnapshot();let t=typeof e.controlStateSnapshot=="function"?e.controlStateSnapshot():{planModeApproved:!1};return{requestedPermissionMode:e.requestedPermissionMode,currentPermissionMode:e.currentPermissionMode,approvalExecutionState:e.approvalExecutionState,approvalRationale:e.approvalRationale,planModeApproved:t.planModeApproved,pendingPlanApproval:e.pendingPlanApproval,planApprovalContext:e.planApprovalContext,planDecisionVersion:e.planDecisionVersion,actionablePlanDecisionVersion:e.actionablePlanDecisionVersion,canonicalPlanPromptVersion:e.canonicalPlanPromptVersion,approvalPromptRequiredVersion:e.approvalPromptRequiredVersion,approvalPromptVersion:e.approvalPromptVersion,approvalPromptStatus:e.approvalPromptStatus,approvalPromptTransport:e.approvalPromptTransport,approvalPromptMessageKind:e.approvalPromptMessageKind,approvalPromptLastAttemptAt:e.approvalPromptLastAttemptAt,approvalPromptDeliveredAt:e.approvalPromptDeliveredAt,approvalPromptFailedAt:e.approvalPromptFailedAt,planApproval:e.planApproval,codexApprovalPolicy:e.codexApprovalPolicy}}getSessionRoutingSnapshot(e){return typeof e.routingSnapshot=="function"?e.routingSnapshot():{route:e.route,originAgentId:e.originAgentId,originChannel:e.originChannel,originThreadId:e.originThreadId,originSessionKey:e.originSessionKey}}getSessionWorktreeSnapshot(e){return typeof e.worktreeSnapshot=="function"?e.worktreeSnapshot():{worktreePath:e.worktreePath,worktreeBranch:e.worktreeBranch,worktreeStrategy:e.worktreeStrategy,repoIntegrationPolicy:e.repoIntegrationPolicy,repoIntegrationPolicySource:e.repoIntegrationPolicySource,repoProvider:e.repoProvider,worktreeBaseBranch:e.worktreeBaseBranch,worktreeParentBranch:e.worktreeParentBranch,worktreePrTargetRepo:e.worktreePrTargetRepo,autoMergeParentSessionId:e.autoMergeParentSessionId,autoMergeConflictResolutionAttemptCount:e.autoMergeConflictResolutionAttemptCount,autoMergeResolverSessionId:e.autoMergeResolverSessionId,worktreeLifecycle:e.worktreeLifecycle}}indexPersistedEntry(e){let t=this.getEntryStorageKey(e);if(e.sessionId){let o=this.idIndex.get(e.sessionId);if(o&&o!==t){let i=this.persisted.get(o);i&&this.removePersistedIndexes(i)}}this.persisted.set(t,e),e.sessionId&&this.idIndex.set(e.sessionId,t),e.name&&this.nameIndex.set(e.name,t);let r=C(e);r&&this.backendIdIndex.set(r,t)}removePersistedIndexes(e){let t=this.getEntryStorageKey(e);this.persisted.delete(t);for(let[r,o]of this.idIndex)o===t&&this.idIndex.delete(r);for(let[r,o]of this.nameIndex)o===t&&this.nameIndex.delete(r);for(let[r,o]of this.backendIdIndex)o===t&&this.backendIdIndex.delete(r)}markRunning(e){if(!e.harnessSessionId)return;let t=this.getSessionApprovalSnapshot(e),r=this.getSessionRoutingSnapshot(e),o=this.getSessionWorktreeSnapshot(e),i=Z({route:r.route,originChannel:r.originChannel,originThreadId:r.originThreadId,originSessionKey:r.originSessionKey});if(!i)throw new Error(`Cannot persist running session ${e.id}: canonical lifecycle route is missing.`);let s={sessionId:e.id,harnessSessionId:e.harnessSessionId,backendRef:this.buildPersistedBackendRef(e),name:e.name,prompt:e.prompt,workdir:e.originalWorkdir??e.workdir,model:e.model,reasoningEffort:e.reasoningEffort,fastMode:e.fastMode,createdAt:e.startedAt,status:"running",lifecycle:e.lifecycle,approvalState:e.approvalState,worktreeState:e.worktreeState,runtimeState:e.runtimeState,taskFlowMirror:e.taskFlowMirror,deliveryState:e.deliveryState,notificationDedupe:this.getExistingNotificationDedupe(e),completionSummaryDedupe:this.getExistingCompletionSummaryDedupe(e),costUsd:0,originAgentId:r.originAgentId,originChannel:r.originChannel,originThreadId:r.originThreadId,originSessionKey:r.originSessionKey,route:i,outputPath:ve(e.id),harness:e.harnessName,resumedFromSessionName:e.resumedFromSessionName,requestedPermissionMode:t.requestedPermissionMode,currentPermissionMode:t.currentPermissionMode,approvalExecutionState:t.approvalExecutionState,approvalRationale:t.approvalRationale,planModeApproved:t.planModeApproved,pendingPlanApproval:t.pendingPlanApproval,planApprovalContext:t.planApprovalContext,planDecisionVersion:t.planDecisionVersion,actionablePlanDecisionVersion:t.actionablePlanDecisionVersion,canonicalPlanPromptVersion:t.canonicalPlanPromptVersion,approvalPromptRequiredVersion:t.approvalPromptRequiredVersion,approvalPromptVersion:t.approvalPromptVersion,approvalPromptStatus:t.approvalPromptStatus,approvalPromptTransport:t.approvalPromptTransport,approvalPromptMessageKind:t.approvalPromptMessageKind,approvalPromptLastAttemptAt:t.approvalPromptLastAttemptAt,approvalPromptDeliveredAt:t.approvalPromptDeliveredAt,approvalPromptFailedAt:t.approvalPromptFailedAt,planApproval:t.planApproval,codexApprovalPolicy:t.codexApprovalPolicy,worktreePath:o.worktreePath,worktreeBranch:o.worktreeBranch,worktreeStrategy:o.worktreeStrategy,repoIntegrationPolicy:o.repoIntegrationPolicy,repoIntegrationPolicySource:o.repoIntegrationPolicySource,repoProvider:o.repoProvider,worktreeBaseBranch:o.worktreeBaseBranch,worktreeParentBranch:o.worktreeParentBranch,worktreePrTargetRepo:o.worktreePrTargetRepo,autoMergeParentSessionId:o.autoMergeParentSessionId,autoMergeConflictResolutionAttemptCount:o.autoMergeConflictResolutionAttemptCount,autoMergeResolverSessionId:o.autoMergeResolverSessionId,worktreeLifecycle:o.worktreeLifecycle,resumable:e.isExplicitlyResumable};Jn(s),this.indexPersistedEntry(s),this.saveIndex()}hasRecordedSession(e){return this.queries.hasRecordedSession(e)}persistTerminal(e){if(!e.harnessSessionId)return;let t=this.getSessionApprovalSnapshot(e),r=this.getSessionRoutingSnapshot(e),o=this.getSessionWorktreeSnapshot(e),i=Z({route:r.route,originChannel:r.originChannel,originThreadId:r.originThreadId,originSessionKey:r.originSessionKey});if(!i)throw new Error(`Cannot persist terminal session ${e.id}: canonical lifecycle route is missing.`);let s;try{let l=ve(e.id);if(dm(l))s=l;else{let d=e.getOutput().join(`
|
|
196
|
-
`);d.length>0&&(cm(l,d,"utf-8"),s=l)}}catch(l){`${e.id}${mm(l)}`}let a={sessionId:e.id,harnessSessionId:e.harnessSessionId,backendRef:this.buildPersistedBackendRef(e),name:e.name,prompt:e.prompt,workdir:e.originalWorkdir??e.workdir,model:e.model,reasoningEffort:e.reasoningEffort,fastMode:e.fastMode,createdAt:e.startedAt,completedAt:e.completedAt,status:e.status,lifecycle:e.lifecycle,approvalState:e.approvalState,worktreeState:e.worktreeState,runtimeState:e.runtimeState,taskFlowMirror:e.taskFlowMirror,deliveryState:e.deliveryState,notificationDedupe:this.getExistingNotificationDedupe(e),completionSummaryDedupe:this.getExistingCompletionSummaryDedupe(e),killReason:e.killReason,costUsd:e.costUsd,originAgentId:r.originAgentId,originChannel:r.originChannel,originThreadId:r.originThreadId,originSessionKey:r.originSessionKey,route:i,outputPath:s,harness:e.harnessName,resumedFromSessionName:e.resumedFromSessionName,goalTaskId:e.goalTaskId,requestedPermissionMode:t.requestedPermissionMode,currentPermissionMode:t.currentPermissionMode,approvalExecutionState:t.approvalExecutionState,approvalRationale:t.approvalRationale,planModeApproved:t.planModeApproved,pendingPlanApproval:t.pendingPlanApproval,planApprovalContext:t.planApprovalContext,planDecisionVersion:t.planDecisionVersion,actionablePlanDecisionVersion:t.actionablePlanDecisionVersion,canonicalPlanPromptVersion:t.canonicalPlanPromptVersion,approvalPromptRequiredVersion:t.approvalPromptRequiredVersion,approvalPromptVersion:t.approvalPromptVersion,approvalPromptStatus:t.approvalPromptStatus,approvalPromptTransport:t.approvalPromptTransport,approvalPromptMessageKind:t.approvalPromptMessageKind,approvalPromptLastAttemptAt:t.approvalPromptLastAttemptAt,approvalPromptDeliveredAt:t.approvalPromptDeliveredAt,approvalPromptFailedAt:t.approvalPromptFailedAt,planApproval:t.planApproval,codexApprovalPolicy:t.codexApprovalPolicy,worktreePath:o.worktreePath,worktreeBranch:o.worktreeBranch,worktreeStrategy:o.worktreeStrategy,worktreeBaseBranch:o.worktreeBaseBranch,worktreeParentBranch:o.worktreeParentBranch,worktreePrTargetRepo:o.worktreePrTargetRepo,autoMergeParentSessionId:o.autoMergeParentSessionId,autoMergeConflictResolutionAttemptCount:o.autoMergeConflictResolutionAttemptCount,autoMergeResolverSessionId:o.autoMergeResolverSessionId,worktreeLifecycle:o.worktreeLifecycle,resumable:e.isExplicitlyResumable};Jn(a),this.indexPersistedEntry(a),this.saveIndex()}getExistingNotificationDedupe(e){return this.getPersistedSession(e.id)?.notificationDedupe??(e.harnessSessionId?this.getPersistedSession(e.harnessSessionId)?.notificationDedupe:void 0)??(C(e)?this.getPersistedSession(C(e))?.notificationDedupe:void 0)}getExistingCompletionSummaryDedupe(e){return this.getPersistedSession(e.id)?.completionSummaryDedupe??(e.harnessSessionId?this.getPersistedSession(e.harnessSessionId)?.completionSummaryDedupe:void 0)??(C(e)?this.getPersistedSession(C(e))?.completionSummaryDedupe:void 0)}getLatestPersistedByName(e){return this.queries.getLatestPersistedByName(e)}resolveBackendConversationId(e,t){return this.queries.resolveBackendConversationId(e,t)}resolveHarnessSessionId(e,t){return this.queries.resolveHarnessSessionId(e,t)}getPersistedSession(e){return this.queries.getPersistedSession(e)}replacePersistedSession(e){let t=e.sessionId?this.getPersistedSession(e.sessionId):void 0;t&&this.removePersistedIndexes(t),this.indexPersistedEntry(e),this.saveIndex()}listPersistedSessions(){return this.queries.listPersistedSessions()}getRepoPolicy(e){return this.repoPolicies.get(e)}listRepoPolicies(){return[...this.repoPolicies.values()].sort((e,t)=>e.repoRoot.localeCompare(t.repoRoot)||e.key.localeCompare(t.key))}setRepoPolicy(e){let t=this.repoPolicies.get(e.key),r={...e,createdAt:t?.createdAt??e.createdAt,updatedAt:new Date().toISOString(),source:"stored"};return this.repoPolicies.set(r.key,r),this.saveIndex(),r}updateRepoPolicy(e,t){let r=this.repoPolicies.get(e);if(r)return this.setRepoPolicy({...r,policy:t})}resetRepoPolicy(e){let t=this.repoPolicies.delete(e);return t&&this.saveIndex(),t}removeRepoPolicies(e){let t=[];for(let r of e){let o=this.repoPolicies.get(r);o&&(this.repoPolicies.delete(r),t.push(o))}return t.length>0&&this.saveIndex(),t.sort((r,o)=>r.repoRoot.localeCompare(o.repoRoot)||r.key.localeCompare(o.key))}cleanupRepoPolicies(){let e=[];for(let[t,r]of this.repoPolicies)gm(r.repoRoot)||(this.repoPolicies.delete(t),e.push(r));return e.length>0&&this.saveIndex(),e.sort((t,r)=>t.repoRoot.localeCompare(r.repoRoot)||t.key.localeCompare(r.key))}cleanupTmpOutputFiles(e){this.actionTokenStore.purgeExpiredActionTokens(e),qa(e,Oo,this.getReferencedOutputPaths())}getNextTmpOutputCleanupAt(e){return Va(e,Oo,this.getReferencedOutputPaths())}evictOldestPersisted(e){let t=this.listPersistedSessions();if(t.length<=e)return[];let r=t.slice(e);for(let o of r)this.removePersistedIndexes(o);return this.saveIndex(),r}shouldGcActiveSession(e,t,r){return!e.completedAt||!pm.has(e.status)?!1:t-e.completedAt>r}getActionToken(e){return this.actionTokenStore.getActionToken(e)}consumeActionToken(e){return this.actionTokenStore.consumeActionToken(e)}deleteActionTokensForSession(e){this.actionTokenStore.deleteActionTokensForSession(e)}purgeExpiredActionTokens(e=Date.now()){return this.actionTokenStore.purgeExpiredActionTokens(e)}getNextActionTokenExpiry(){return this.actionTokenStore.nextExpiryAt()}onActionTokensChanged(e){this.actionTokenStore.setAfterChangeListener(e)}hasOutputPathReference(e){for(let t of this.persisted.values())if(t.outputPath===e)return!0;return!1}getReferencedOutputPaths(){return[...new Set([...this.persisted.values()].map(e=>e.outputPath).filter(e=>typeof e=="string"&&e.length>0))]}cleanupOrphanOutputFiles(){Ua(this.getReferencedOutputPaths())}};var hm=new Set(["completed","failed","killed"]),Zn=class{metrics={totalCostUsd:0,costPerDay:new Map,sessionsByStatus:{completed:0,failed:0,killed:0},totalLaunched:0,totalDurationMs:0,sessionsWithDuration:0,mostExpensive:null};incrementLaunched(){this.metrics.totalLaunched++}recordSession(e){let t=e.costUsd??0,r=e.status;this.metrics.totalCostUsd+=t;let o=new Date(e.completedAt??e.startedAt).toISOString().slice(0,10);if(this.metrics.costPerDay.set(o,(this.metrics.costPerDay.get(o)??0)+t),hm.has(r)&&this.metrics.sessionsByStatus[r]++,e.completedAt){let i=e.completedAt-e.startedAt;this.metrics.totalDurationMs+=i,this.metrics.sessionsWithDuration++}(!this.metrics.mostExpensive||t>this.metrics.mostExpensive.costUsd)&&(this.metrics.mostExpensive={id:e.id,name:e.name,costUsd:t,prompt:G(e.prompt,80)})}getMetrics(){return{totalCostUsd:this.metrics.totalCostUsd,costPerDay:new Map(this.metrics.costPerDay),sessionsByStatus:{...this.metrics.sessionsByStatus},totalLaunched:this.metrics.totalLaunched,totalDurationMs:this.metrics.totalDurationMs,sessionsWithDuration:this.metrics.sessionsWithDuration,mostExpensive:this.metrics.mostExpensive?{...this.metrics.mostExpensive}:null}}};import*as ja from"child_process";var ze=class{tails=new Map;enqueue(e,t,r){let o=this.tails.get(e);o!==void 0&&r?.();let s=(o?.catch(()=>{})??Promise.resolve()).then(()=>t()),a=s.catch(()=>{});return this.tails.set(e,a),a.finally(()=>{this.tails.get(e)===a&&this.tails.delete(e)}),s}clear(){this.tails.clear()}};var Wo=3e4,ym=2e3,Sm=2e4,j=4;function yt(n){return n instanceof Error?n.message:String(n)}function km(){return new Error(`Dispatch timed out after ${Wo}ms`)}function vm(n){if(!(n instanceof Error))return!1;let e=n;return e.killed===!0&&e.signal==="SIGKILL"}var Pm={execFile:ja.execFile},er=class{pendingRetryTimers=new Map;orderedDispatches=new ze;disposed=!1;clearPendingRetries(){for(let e of this.pendingRetryTimers.values())for(let t of e)clearTimeout(t.timer),t.onCleared?.();this.pendingRetryTimers.clear()}clearRetryTimersForSession(e){let t=this.pendingRetryTimers.get(e);if(t){for(let r of t)clearTimeout(r.timer),r.onCleared?.();this.pendingRetryTimers.delete(e)}}dispose(){this.disposed=!0,this.clearPendingRetries(),this.orderedDispatches.clear()}execute(e,t,r=1){if(!this.disposed){if(r===1&&t.orderingKey){this.enqueueOrderedDispatch(t.orderingKey,o=>this.executeNow(e,t,o,r));return}this.executeNow(e,t,void 0,r)}}executePromise(e,t,r=1){if(!this.disposed){if(r===1&&t.orderingKey){this.enqueueOrderedDispatch(t.orderingKey,o=>this.executePromiseNow(e,t,o,r));return}this.executePromiseNow(e,t,void 0,r)}}executeNow(e,t,r,o=1){if(this.disposed||t.shouldContinue?.()===!1){r?.();return}let i=Date.now();o===1&&t.onStarted?.(),this.log("info","dispatch_started",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j}),Pm.execFile("openclaw",e,{timeout:Wo,killSignal:"SIGKILL"},(s,a,l)=>{if(this.disposed){r?.();return}let d=Date.now()-i;if(!s){let y=t.successValidator?.(a??"");if(y?.outcome==="failure"){this.log("error","dispatch_success_validation_failed",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:d,error:y.reason,terminal:!0}),t.onFinalFailure?.(),r?.();return}if(y?.outcome==="skipped"){this.log("info","dispatch_skipped",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:d,reason:y.reason}),t.shouldContinue?.()!==!1&&t.onSkipped?.(y.reason),r?.();return}this.log("info","dispatch_succeeded",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:d}),t.shouldContinue?.()!==!1&&t.onSuccess?.(),r?.();return}let u=l?.trim()?` | stderr: ${l.trim()}`:"";if(t.shouldContinue?.()===!1){r?.();return}if(t.target==="message.send"&&vm(s)){this.log("error","dispatch_failed",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:d,error:`${yt(s)}${u}`,terminal:!0,ambiguousResult:!0}),t.onAmbiguousResult?.(),r?.();return}if(o>=j||t.terminalOnFailure===!0){this.log("error","dispatch_failed",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:d,error:`${yt(s)}${u}`,terminal:!0}),t.onFinalFailure?.(),r?.();return}let g=this.retryDelayMs(o);this.log("error","dispatch_retry_scheduled",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:d,retryDelayMs:g,error:`${yt(s)}${u}`});let S={timer:setTimeout(()=>{let y=this.pendingRetryTimers.get(t.sessionId);if(y&&(y.delete(S),y.size===0&&this.pendingRetryTimers.delete(t.sessionId)),t.shouldContinue?.()===!1){r?.();return}this.executeNow(e,t,r,o+1)},g),onCleared:r};S.timer.unref?.(),this.pendingRetryTimers.has(t.sessionId)||this.pendingRetryTimers.set(t.sessionId,new Set),this.pendingRetryTimers.get(t.sessionId).add(S)})}executePromiseNow(e,t,r,o=1){if(this.disposed||t.shouldContinue?.()===!1){r?.();return}let i=Date.now();o===1&&t.onStarted?.(),this.log("info","dispatch_started",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j}),this.executePromiseWithTimeout(e).then(()=>{if(this.disposed){r?.();return}let s=Date.now()-i;this.log("info","dispatch_succeeded",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:s}),t.shouldContinue?.()!==!1&&t.onSuccess?.(),r?.()}).catch(s=>{if(this.disposed){r?.();return}let a=Date.now()-i;if(t.shouldContinue?.()===!1){r?.();return}if(o>=j||t.terminalOnFailure===!0){this.log("error","dispatch_failed",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:a,error:yt(s),terminal:!0,...t.terminalOnFailure===!0&&o<j?{terminalReason:"non_retryable"}:{}}),t.onFinalFailure?.(),r?.();return}let l=this.retryDelayMs(o);this.log("error","dispatch_retry_scheduled",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:a,retryDelayMs:l,error:yt(s)});let d={timer:setTimeout(()=>{let u=this.pendingRetryTimers.get(t.sessionId);if(u&&(u.delete(d),u.size===0&&this.pendingRetryTimers.delete(t.sessionId)),t.shouldContinue?.()===!1){r?.();return}this.executePromiseNow(e,t,r,o+1)},l),onCleared:r};d.timer.unref?.(),this.pendingRetryTimers.has(t.sessionId)||this.pendingRetryTimers.set(t.sessionId,new Set),this.pendingRetryTimers.get(t.sessionId).add(d)})}executePromiseWithTimeout(e){return new Promise((t,r)=>{let o=!1,i=setTimeout(()=>{o||(o=!0,r(km()))},Wo);i.unref?.(),Promise.resolve().then(e).then(()=>{o||(o=!0,clearTimeout(i),t())}).catch(s=>{o||(o=!0,clearTimeout(i),r(s))})})}retryDelayMs(e){let t=Math.max(0,e-1),r=ym*2**t;return Math.min(r,Sm)}enqueueOrderedDispatch(e,t){this.orderedDispatches.enqueue(e,async()=>{this.disposed||await new Promise(r=>{let o=!1;t(()=>{o||(o=!0,r())})})})}log(e,t,r){let o=`[WakeDispatcher] ${JSON.stringify({event:t,...r})}`;if(e==="error"){console.error(o);return}}};var tr=class{resolve(e){let t=le(e);if(!(!t?.provider||!t.target))return{channel:t.provider,target:t.target,accountId:t.accountId,threadId:t.threadId,sessionKey:t.sessionKey}}summary(e){if(!e)return"system";let t=e.accountId?`|${e.accountId}`:"",r=e.threadId?`#${e.threadId}`:"";return`${e.channel}${t}|${e.target}${r}`}};import{randomUUID as wm}from"crypto";var nr=class{constructor(e={}){}buildChatSendArgs(e,t,r,o=wm()){return["gateway","call","chat.send","--expect-final","--timeout","30000","--params",JSON.stringify({sessionKey:e,message:t,deliver:r,idempotencyKey:o})]}buildDirectNotificationArgs(e,t,r){let o=jt(r),i=["message","send","--channel",e.channel,"--target",e.target,"--message",t];return e.accountId&&i.push("--account",e.accountId),e.threadId&&i.push("--thread-id",e.threadId),o&&i.push("--presentation",JSON.stringify(o)),i}buildSystemEventArgs(e){return["system","event","--text",e,"--mode","now"]}};function bm(n){let e=Rm(n).trim();return e?/^NO_REPLY$/i.test(e)?{outcome:"failure",reason:"completion follow-up wake ended with NO_REPLY"}:{outcome:"success"}:{outcome:"failure",reason:"completion follow-up wake produced no final response"}}function Rm(n){let e=n.trim();if(!e)return"";try{return Fo(JSON.parse(e)).trim()}catch{return e}}function Fo(n){if(typeof n=="string")return n;if(Array.isArray(n))return n.map(r=>Fo(r)).filter(Boolean).join(`
|
|
197
|
-
`);if(!n||typeof n!="object")return"";let e=n,t=["final","finalResponse","final_response","assistantFinal","assistant_final","response","text","content"];for(let r of t){let o=e[r];if(typeof o=="string"&&o.trim())return o}for(let r of["result","message","data"]){let o=Fo(e[r]);if(o.trim())return o}return""}var rr=class{routes=new tr;transport;directNotifications;executor=new er;constructor(e={}){this.transport=e.transport??new nr(e.transportOptions),this.directNotifications=e.directNotifications===void 0?new We:e.directNotifications}clearPendingRetries(){this.executor.clearPendingRetries()}clearRetryTimersForSession(e){this.executor.clearRetryTimersForSession(e)}dispose(){this.executor.dispose()}buildDispatchContext(e){let t=e.buttons??[],r=t.flat();return{transportRoute:e.routeSummary,transportChannel:e.route?.channel??"system",transportTarget:e.route?.target??"system",transportAccountId:e.route?.accountId,transportThreadId:e.route?.threadId,transportSessionKey:e.route?.sessionKey,messageTextLength:e.text.length,buttonsPresent:r.length>0,buttonRows:t.length||void 0,buttonCount:r.length||void 0,buttonLabels:r.length>0?r.map(o=>o.label):void 0,maxCallbackDataLength:r.length>0?Math.max(...r.map(o=>o.callbackData.length)):void 0}}sendWake(e,t,r,o,i,s,a,l,d,u){let f=this.routes.resolve(e),g=a;if(g?.()===!1)return;let S=f?.sessionKey?.trim();if(!S){this.executor.execute(this.transport.buildSystemEventArgs(t),{label:`${r}-system`,sessionId:e.id,target:"system.event",phase:o,routeSummary:"system",messageKind:"wake",dispatchContext:this.buildDispatchContext({routeSummary:"system",text:t}),onSuccess:s,onSkipped:d,onFinalFailure:i,successValidator:l,shouldContinue:g});return}this.executor.execute(this.transport.buildChatSendArgs(S,t,!0,u),{label:r,sessionId:e.id,target:"chat.send",phase:o,routeSummary:`session:${S}`,messageKind:"wake",dispatchContext:this.buildDispatchContext({routeSummary:`session:${S}`,route:f,text:t}),onSuccess:s,onSkipped:d,successValidator:l,shouldContinue:g,onFinalFailure:()=>{g?.()!==!1&&this.executor.execute(this.transport.buildSystemEventArgs(t),{label:`${r}-fallback`,sessionId:e.id,target:"system.event",phase:o,routeSummary:"system",messageKind:"wake",dispatchContext:this.buildDispatchContext({routeSummary:"system",text:t}),onSuccess:s,onSkipped:d,onFinalFailure:i,successValidator:l,shouldContinue:g})}})}sendUserNotification(e,t,r,o,i,s,a=!1,l){if(l?.()===!1)return;let d=!!o?.some(y=>Array.isArray(y)&&y.length>0),u=this.routes.resolve(e);M("wake_notify_selected",{sessionId:e.id,sessionName:e.name,label:r,messageTextLength:t.length,requireDirectDelivery:a,hasDirectNotificationTransport:!!this.directNotifications,routeSummary:u?this.routes.summary(u):"system",channel:u?.channel,target:u?.target,accountId:u?.accountId,threadId:u?.threadId,sessionKey:u?.sessionKey,...E(o)});let f=u?`notify:${u.channel}|${u.accountId??""}|${u.target}|${u.threadId??""}`:`notify:system:${e.id}`;if(!u){if(M("wake_notify_no_direct_route",{sessionId:e.id,sessionName:e.name,label:r,requireDirectDelivery:a,hasInteractiveButtons:d,...E(o)}),a){`${r}${e.id}`,i?.();return}if(d){`${r}${e.id}`,i?.();return}this.executor.execute(this.transport.buildSystemEventArgs(t),{label:`${r}-notify-system`,sessionId:e.id,target:"system.event",phase:"notify",routeSummary:"system",messageKind:"notify",dispatchContext:this.buildDispatchContext({routeSummary:"system",text:t,buttons:o}),orderingKey:f,onSuccess:s,onFinalFailure:i,shouldContinue:l});return}let g=()=>{if(M("wake_notify_direct_failed",{sessionId:e.id,sessionName:e.name,label:r,requireDirectDelivery:a,hasInteractiveButtons:d,channel:u.channel,target:u.target,accountId:u.accountId,threadId:u.threadId,sessionKey:u.sessionKey,...E(o)}),a){`${r}${e.id}`,i?.();return}if(d){`${r}${e.id}`,i?.();return}this.executor.execute(this.transport.buildSystemEventArgs(t),{label:`${r}-notify-fallback`,sessionId:e.id,target:"system.event",phase:"notify",routeSummary:"system",messageKind:"notify",dispatchContext:this.buildDispatchContext({routeSummary:"system",text:t}),orderingKey:f,onSuccess:s,onFinalFailure:i,shouldContinue:l})},S={label:`${r}-notify`,sessionId:e.id,target:"message.send",phase:"notify",routeSummary:this.routes.summary(u),messageKind:"notify",dispatchContext:this.buildDispatchContext({routeSummary:this.routes.summary(u),route:u,text:t,buttons:o}),orderingKey:f,onSuccess:s,onAmbiguousResult:g,onFinalFailure:g,terminalOnFailure:this.directNotifications?!0:void 0,shouldContinue:l};if(this.directNotifications){M("wake_notify_dispatching_direct_runtime",{sessionId:e.id,sessionName:e.name,label:r,channel:u.channel,target:u.target,accountId:u.accountId,threadId:u.threadId,sessionKey:u.sessionKey,...E(o)}),this.executor.executePromise(()=>this.directNotifications.send(u,t,o),S);return}M("wake_notify_dispatching_cli_message_send",{sessionId:e.id,sessionName:e.name,label:r,channel:u.channel,target:u.target,accountId:u.accountId,threadId:u.threadId,sessionKey:u.sessionKey,...E(o)}),this.executor.execute(this.transport.buildDirectNotificationArgs(u,t,o),S)}sendUserNotificationSequence(e,t,r,o,i,s=!1,a){let l=t.map(u=>({text:u.text.trim(),buttons:u.buttons,requiredForSequenceSuccess:u.requiredForSequenceSuccess})).filter(u=>u.text.length>0);if(l.length===0){o?.();return}M("wake_notify_sequence_started",{sessionId:e.id,sessionName:e.name,label:r,chunkCount:l.length,buttonChunkIndexes:l.map((u,f)=>u.buttons?.some(g=>Array.isArray(g)&&g.length>0)?f+1:void 0).filter(u=>typeof u=="number"),requireDirectDelivery:s});let d=u=>{if(a?.()===!1)return;let f=l[u];if(!f){M("wake_notify_sequence_succeeded",{sessionId:e.id,sessionName:e.name,label:r,chunkCount:l.length}),i?.();return}let g=u===0||f.requiredForSequenceSuccess===!0,S=g?o:i;M("wake_notify_sequence_chunk_selected",{sessionId:e.id,sessionName:e.name,label:r,chunkIndex:u+1,chunkCount:l.length,messageTextLength:f.text.length,failureHandler:g?"sequence-failed":"partial-success",...E(f.buttons)}),this.sendUserNotification(e,f.text,`${r}-part-${u+1}`,f.buttons,S,()=>d(u+1),s,a)};d(0)}dispatchSessionNotification(e,t){let r=t.hooks,o=t.wakeMessageOnNotifySuccess!=null||t.wakeMessageOnNotifyFailed!=null,i=t.notifyUser??(t.wakeMessage?"on-wake-fallback":"always"),s=(t.userMessages?.length?t.userMessages:t.userMessage?.trim()?[{text:t.userMessage.trim(),buttons:t.buttons}]:[]).map(u=>({text:u.text.trim(),buttons:u.buttons,requiredForSequenceSuccess:u.requiredForSequenceSuccess})).filter(u=>u.text.length>0),a=t.wakeMessage?.trim(),l=t.shouldDispatch,d=t.completionWakeSummaryRequired===!0?bm:void 0;if(o){let u=t.wakeMessageOnNotifySuccess?.trim(),f=t.wakeMessageOnNotifyFailed?.trim(),g=h=>{h&&l?.()!==!1&&(r?.onWakeStarted?.(),this.sendWake(e,h,`${t.label}-wake`,"wake",r?.onWakeFailed,r?.onWakeSucceeded,l,d,r?.onWakeSkipped,t.idempotencyKey))},S=h=>{if(h){if(t.deferConditionalWakeUntilNextTick===!0||t.deferConditionalWakeMs!==void 0){let v=Math.max(0,Math.floor(t.deferConditionalWakeMs??0));setTimeout(()=>g(h),v).unref?.();return}g(h)}},y=()=>{l?.()!==!1&&(r?.onNotifySucceeded?.(),u&&S(u))},c=f?()=>{l?.()!==!1&&(r?.onNotifyFailed?.(),t.onUserNotifyFailed?.(),S(f))}:()=>{l?.()!==!1&&(r?.onNotifyFailed?.(),t.onUserNotifyFailed?.())};if(s.length>0){if(l?.()===!1)return;r?.onNotifyStarted?.(),this.sendUserNotificationSequence(e,s,t.label,c,y,t.requireDirectUserNotification===!0,l)}else c();return}if(i==="always"&&s.length>0){if(l?.()===!1)return;r?.onNotifyStarted?.(),this.sendUserNotificationSequence(e,s,t.label,()=>{l?.()!==!1&&(r?.onNotifyFailed?.(),t.onUserNotifyFailed?.())},()=>{l?.()!==!1&&r?.onNotifySucceeded?.()},t.requireDirectUserNotification===!0,l)}if(a&&l?.()!==!1){if(r?.onWakeStarted?.(),i==="on-wake-fallback"&&s.length>0&&!this.routes.resolve(e)?.sessionKey){if(l?.()===!1)return;r?.onNotifyStarted?.(),this.sendUserNotificationSequence(e,s,t.label,()=>{l?.()!==!1&&(r?.onNotifyFailed?.(),t.onUserNotifyFailed?.())},()=>{l?.()!==!1&&r?.onNotifySucceeded?.()},!1,l)}this.sendWake(e,a,`${t.label}-wake`,"wake",r?.onWakeFailed,r?.onWakeSucceeded,l,d,r?.onWakeSkipped,t.idempotencyKey)}}};var za=36,Ha=2,Qa="...";function Am(n){let e=Array.from(n);return e.length<=za?n:`${e.slice(0,za-Qa.length).join("")}${Qa}`}var or=class{constructor(e,t){this.actionTokens=e;this.isGitHubCliAvailable=t}actionTokens;isGitHubCliAvailable;createActionToken(e,t,r={}){return this.actionTokens.createActionToken(e,t,{expiresAt:Date.now()+10080*60*1e3,...r})}consumeActionToken(e){return this.actionTokens.consumeActionToken(e)}consumeQuestionAnswerTokens(e,t,r){return this.actionTokens.consumeQuestionAnswerTokens(e,t,r)}consumePlanDecisionTokens(e,t){return this.actionTokens.consumePlanDecisionTokens(e,t)}getActionToken(e){return this.actionTokens.getActionToken(e)}listActiveActionTokens(e){return this.actionTokens.listActiveActionTokens(e)}clearRepoPolicyChoiceTokens(e){this.actionTokens.deleteActionTokensForSessionByKind(e,"repo-policy-set")}clearPlanDecisionTokens(e,t){this.actionTokens.deletePlanDecisionTokensForSession(e,t)}makeActionButton(e,t,r,o={}){let i=this.createActionToken(e,t,{label:r,...o});return{label:r,callbackData:i.id,style:this.resolveButtonStyle(t)}}resolveButtonStyle(e){switch(e){case"plan-approve":case"worktree-create-pr":case"worktree-update-pr":case"plan-offer-start":case"repo-policy-set":return"primary";case"worktree-merge":case"session-resume":case"session-restart":case"plugin-update-install":case"plugin-update-restart":return"success";case"plugin-update-remind-later":return"secondary";case"plan-reject":case"worktree-dismiss":case"plugin-update-dismiss":return"danger";default:return"secondary"}}getWorktreeDecisionButtons(e,t,r={merge:!0,pr:!0}){if(!t)return[];let o=[],i=[];if(r.merge&&i.push(this.makeActionButton(e,"worktree-merge","Merge")),r.pr&&this.isGitHubCliAvailable())t.worktreePrUrl?(i.push(this.makeActionButton(e,"worktree-view-pr","View PR",{targetUrl:t.worktreePrUrl})),i.length>0&&o.push(i),o.push([this.makeActionButton(e,"worktree-update-pr","Sync PR"),this.makeActionButton(e,"worktree-decide-later","Later")])):(i.push(this.makeActionButton(e,"worktree-create-pr","Open PR")),i.length>0&&o.push(i),o.push([this.makeActionButton(e,"worktree-decide-later","Later"),this.makeActionButton(e,"worktree-dismiss","Discard")]));else return o.push([...i,this.makeActionButton(e,"worktree-decide-later","Later")]),o.push([this.makeActionButton(e,"worktree-dismiss","Discard")]),o;return t.worktreePrUrl&&o.push([this.makeActionButton(e,"worktree-dismiss","Discard")]),o}getPlanApprovalButtons(e,t){let r=t?.actionablePlanDecisionVersion??t?.planDecisionVersion;this.clearPlanDecisionTokens(e,r);let o=[[this.makeActionButton(e,"plan-approve","Approve",{planDecisionVersion:r}),this.makeActionButton(e,"plan-request-changes","Revise",{planDecisionVersion:r}),this.makeActionButton(e,"plan-reject","Reject",{planDecisionVersion:r})]];return M("plan_approval_buttons_created",{sessionId:e,planDecisionVersion:r,...E(o)}),o}getResumeButtons(e,t){let r=[];return t.isExplicitlyResumable&&r.push(this.makeActionButton(e,"session-resume","Resume")),r.push(this.makeActionButton(e,"view-output","View output")),[r]}getQuestionButtons(e,t,r={}){if(t.length===0)return;let o=t.map((s,a)=>{let l=this.createActionToken(e,"question-answer",{label:s.label,optionIndex:a,pendingInputRequestId:r.requestId,pendingInputQuestionId:r.questionId});return{label:Am(s.label),callbackData:l.id,style:this.resolveButtonStyle("question-answer")}}),i=[];for(let s=0;s<o.length;s+=Ha)i.push(o.slice(s,s+Ha));return i}getPlanOfferButtons(e){let{offerId:t,route:r,planName:o,planPrompt:i,planWorkdir:s,planWorktreeStrategy:a}=e,l=[[this.makeActionButton(t,"plan-offer-start","Start Plan",{route:r,launchName:o,launchPrompt:i,launchWorkdir:s,launchWorktreeStrategy:a}),this.makeActionButton(t,"plan-offer-dismiss","Dismiss",{route:r})]];return M("plan_offer_buttons_created",{offerId:t,planName:o,channel:r.provider,target:r.target,accountId:r.accountId,threadId:r.threadId,sessionKey:r.sessionKey,planPromptLength:i.length,planWorkdirLength:s.length,planWorktreeStrategy:a,...E(l)}),l}getRepoPolicyChoiceButtons(e){let t=at(e.prAvailable??!0),r=Array.from({length:Math.ceil(t.length/2)},(o,i)=>t.slice(i*2,i*2+2)).map(o=>o.map(i=>this.makeActionButton(e.choiceId,"repo-policy-set",i.label,{route:e.route,repoPolicy:i.policy,repoPolicyWorkdir:e.repoRoot,launchName:e.launchName,launchPrompt:e.launchPrompt,launchWorkdir:e.launchWorkdir,launchModel:e.launchModel,launchReasoningEffort:e.launchReasoningEffort,launchFastMode:e.launchFastMode,launchSystemPrompt:e.launchSystemPrompt,launchAllowedTools:e.launchAllowedTools,launchResumeSessionId:e.launchResumeSessionId,launchResumedFromSessionName:e.launchResumedFromSessionName,launchResumeWorktreeFrom:e.launchResumeWorktreeFrom,launchSessionIdOverride:e.launchSessionIdOverride,launchClearedPersistedCodexResume:e.launchClearedPersistedCodexResume,launchForkSession:e.launchForkSession,launchForceNewSession:e.launchForceNewSession,launchPermissionMode:e.launchPermissionMode,launchPlanApproval:e.launchPlanApproval,launchHarness:e.launchHarness,launchWorktreeStrategy:e.launchWorktreeStrategy,launchWorktreeBaseBranch:e.launchWorktreeBaseBranch,launchWorktreePrTargetRepo:e.launchWorktreePrTargetRepo,launchOriginAgentId:e.launchOriginAgentId})));return M("repo_policy_choice_buttons_created",{choiceId:e.choiceId,repoRoot:e.repoRoot,channel:e.route.provider,target:e.route.target,accountId:e.route.accountId,threadId:e.route.threadId,sessionKey:e.route.sessionKey,labels:t.map(o=>o.label),...E(r)}),r}};import{createHash as Cm}from"crypto";var Im="duplicate notification already delivered or in flight",ir=class{maxRecords;inFlightTtlMs;now;inFlight=new Set;delivered=new Set;constructor(e={}){this.maxRecords=Math.max(1,Math.floor(e.maxRecords??64)),this.inFlightTtlMs=Math.max(1,Math.floor(e.inFlightTtlMs??6e5)),this.now=e.now??(()=>new Date)}claim(e,t,r){if(!e)return{allowed:!0};let o=this.now(),i=this.prune(t??[],o),s=i.find(a=>a.key===e);return this.delivered.has(e)||this.inFlight.has(e)||s?.status==="delivered"||s?.status==="in_flight"&&this.isFreshInFlight(s,o)?{allowed:!1,records:i,reason:Im}:(this.inFlight.add(e),this.delivered.delete(e),{allowed:!0,records:this.upsert(i,{key:e,status:"in_flight",recordedAt:o.toISOString(),label:r})})}deliveredRecords(e,t,r){if(!e)return;let o=this.now();return this.inFlight.delete(e),this.delivered.add(e),this.upsert(this.prune(t??[],o),{key:e,status:"delivered",recordedAt:o.toISOString(),label:r})}releasedRecords(e,t){if(!e)return;let r=this.now();return this.inFlight.delete(e),this.prune(t??[],r).filter(o=>!(o.key===e&&o.status==="in_flight"))}prune(e,t){let r=e.filter(o=>o.status==="delivered"?!0:this.isFreshInFlight(o,t));return r.slice(Math.max(0,r.length-this.maxRecords))}isFreshInFlight(e,t){let r=Date.parse(e.recordedAt);return Number.isFinite(r)?t.getTime()-r<this.inFlightTtlMs:!1}upsert(e,t){let o=[...e.filter(i=>i.key!==t.key),t];return o.slice(Math.max(0,o.length-this.maxRecords))}};import{createHash as Tm}from"crypto";var xm=1024,St="duplicate completion follow-up wake already handled",sr="prior human-visible summary already delivered",ar=class{inFlight=new Set;completed=new Map;claimKeysByPrimary=new Map;maxCompletedKeys;constructor(e={}){this.maxCompletedKeys=Math.max(1,Math.floor(e.maxCompletedKeys??xm))}decide(e,t,r){if(t?.required!==!0)return{required:!1,allowed:!1,explicit:!1};let o=this.buildKeySet(e,t);if(!o)return{required:!0,allowed:!0,explicit:!1};let i=this.normalizeRecords(r),s=this.buildPersistedKeyMap(i),a=o.decisionKeys.find(u=>this.inFlight.has(u)||!!this.getCompletedRecord(u)||s.has(u)),l=a?this.getCompletedRecord(a):void 0,d=a?s.get(a):void 0;return a?{required:!0,allowed:!1,explicit:o.explicit,dedupeKey:o.primary,skipReason:l?.skipReason??d?.skipReason??St}:(this.registerClaim(o.primary,o.claimKeys),{required:!0,allowed:!0,key:o.primary,explicit:o.explicit})}recordVisibleDelivery(e,t,r,o){if(t.required!==!0)return{required:!1,allowed:!1,explicit:!1};let i=this.buildKeySet(e,t);if(!i)return{required:!0,allowed:!0,explicit:!1};let s=this.normalizeRecords(r),a=this.buildPersistedKeyMap(s),l=i.decisionKeys.find(g=>!!this.getCompletedRecord(g)||a.has(g)),d=l?this.getCompletedRecord(l):void 0,u=l?a.get(l):void 0;if(d||u)return{required:!0,allowed:!1,explicit:i.explicit,dedupeKey:i.primary,skipReason:d?.skipReason??u?.skipReason};this.finishKeys(i.primary,i.claimKeys,!0,sr);let f=this.deliveredRecords(i.claimKeys,s,o,sr);return{required:!0,allowed:!0,key:i.primary,explicit:i.explicit,records:f}}finish(e,t,r=St){if(!e)return;let o=this.claimKeysByPrimary.get(e)??[e];this.finishKeys(e,o,t,r)}completionRecordsAfterDelivery(e,t,r,o=St){if(!e)return;let i=this.claimKeysByPrimary.get(e)??[e];return this.deliveredRecords(i,t,r,o)}registerClaim(e,t){for(let r of t)this.inFlight.add(r);for(this.claimKeysByPrimary.delete(e),this.claimKeysByPrimary.set(e,t);this.claimKeysByPrimary.size>this.maxCompletedKeys;){let r=this.claimKeysByPrimary.keys().next().value;if(r===void 0)break;let o=this.claimKeysByPrimary.get(r)??[r];for(let i of o)this.inFlight.delete(i);this.claimKeysByPrimary.delete(r)}}finishKeys(e,t,r,o=St){for(let i of t)this.inFlight.delete(i);if(this.claimKeysByPrimary.delete(e),!!r){for(let i of t)this.completed.get(i)?.skipReason===sr&&o!==sr||(this.completed.delete(i),this.completed.set(i,{skipReason:o,linkedKeys:t.filter(a=>a!==i)}));for(;this.completed.size>this.maxCompletedKeys;){let i=this.completed.keys().next().value;if(i===void 0)break;this.completed.delete(i)}}}buildKeySet(e,t){let r=this.getDeliveryRef(e);if(!r)return;let o=this.normalizeOutcomeKey(e,t.outcomeKey?.trim());if(o){let a=`${this.buildVisibleScope(r,e)}:outcome:${this.digest(o)}`,l=this.isGoalLike(e,t,o),d=this.buildSessionAliasKeys(r,e,"visible-summary"),u=this.buildSessionAliasKeys(r,e,"goal-summary"),f=this.isTerminalLike(t,o),g=o.startsWith("worktree-pr:")?this.normalizeWorktreePrOutcome(t.outcomeKey?.trim()):void 0,S=g?.aliases.map(y=>`${this.buildVisibleScope(r,e)}:outcome:${this.digest(y)}`)??[];return{primary:a,explicit:!0,claimKeys:[...d,...l?u:[],...S,a],decisionKeys:[a,...g?.blocksAliases?S:[],...l||f?d:[],...u]}}let i=t.fallbackFingerprint?.trim();if(!i)return;let s=`${r}:${t.producer}:${this.digest(i)}`;return{primary:s,explicit:!1,claimKeys:[s],decisionKeys:[s]}}isGoalLike(e,t,r){return!!e.goalTaskId?.trim()||t.producer==="goal"||/^goal:/i.test(r)}isTerminalLike(e,t){return e.producer==="terminal"||/^terminal:/i.test(t)}normalizeOutcomeKey(e,t){let r=e.goalTaskId?.trim();if(r)return`goal:${r}`;let o=this.normalizeWorktreePrOutcome(t);return o?o.primary:t}normalizeWorktreePrOutcome(e){if(!e)return;let t=e.split(":");if(t[0]!=="worktree-pr"||t.length<5)return;let r=t[1]?.trim().toLowerCase();if(!r||!/^(?:draft-)?(?:opened|updated)$/.test(r))return;let o=t[2]?.trim()||"default-repo",i=t[3]?.trim();if(!i)return;let s=this.normalizePrIdentity(i),a=t[4]?.trim();if(!s||!a)return;let l=["worktree-pr",o.toLowerCase(),s,a].join(":"),d=`${l}:created`;if(r.endsWith("updated")){let u=t.slice(5).join(":").trim();return{primary:u?`${l}:updated:${u}`:`${l}:updated`,aliases:[d,l],blocksAliases:!1}}return{primary:d,aliases:[l],blocksAliases:!0}}normalizePrIdentity(e){let t=e.match(/#?(\d+)$/);if(t?.[1])return`#${t[1]}`;let r=e.match(/\/pull\/(\d+)(?:\b|\/|$)/);if(r?.[1])return`#${r[1]}`;let o=e.trim();return o||void 0}buildVisibleScope(e,t){let r=le(t);return r?`route:${this.digest(JSON.stringify({provider:r.provider,accountId:r.accountId,target:r.target,threadId:r.threadId}))}`:e}buildSessionAliasKeys(e,t,r){let o=this.buildVisibleScope(e,t),i="startedAt"in t?t.startedAt:t.createdAt,s=Number.isFinite(i)?`${o}:cycle:${this.digest(String(i))}`:o,a=["id"in t?t.id:void 0,"sessionId"in t?t.sessionId:void 0,t.name,C(t),t.harnessSessionId,e].map(l=>l?.trim()).filter(l=>!!l);return[...new Set(a)].map(l=>`${s}:session:${this.digest(l)}:${r}`)}getDeliveryRef(e){return D(e)??C(e)??""}digest(e){return Tm("sha256").update(e).digest("hex").slice(0,16)}normalizeRecords(e){return e?.length?e.filter(t=>t.key.trim()&&Number.isFinite(Date.parse(t.recordedAt))).slice(-this.maxCompletedKeys):[]}buildPersistedKeyMap(e){let t=new Map;for(let r of e){t.set(r.key,r);for(let o of r.linkedKeys??[])o.trim()&&t.set(o,r)}return t}getCompletedRecord(e){let t=this.completed.get(e);if(t)return t;for(let r of this.completed.values())if(r.linkedKeys?.includes(e))return r}deliveredRecords(e,t,r,o=St){let i=new Date().toISOString(),s=this.normalizeRecords(t),a=new Set(e),d=[...s.filter(u=>!a.has(u.key)&&!(u.linkedKeys??[]).some(f=>a.has(f))),...e.map(u=>{let f=e.filter(g=>g!==u);return{key:u,linkedKeys:f.length>0?f:void 0,recordedAt:i,label:r,skipReason:o}})];return d.slice(Math.max(0,d.length-this.maxCompletedKeys))}};var Mm=2e3,_m=new Set(["1","true","yes","on"]);function Dm(){let n=process.env.OPENCLAW_CODE_AGENT_NOTIFICATION_DIAGNOSTICS?.trim().toLowerCase();return!!(n&&_m.has(n))||Mr()}function $m(n){Dm()&&process.stderr.write(`[SessionNotification] ${JSON.stringify(n)}
|
|
198
|
-
`)}function
|
|
200
|
+
`),buttons:this.getPolicyAwareWorktreeDecisionButtons(e.id,i,{},[[this.deps.makeOpenPrButton(e.id)]])})}async handleAutoMergeStrategy(e,t,r,o,i,s,a=D(e)??e.harnessSessionId,l={merge:!0,pr:!0}){if(this.deps.isAlreadyMerged(a)||e.autoMergeResolverSessionId)return!1;let d=!1;return await this.deps.enqueueMerge(t,async()=>{if(this.deps.isAlreadyMerged(a))return;let u=this.deps.mergeBranch(t,o,i,"merge",r);if(u.success){d=this.handleAutoMergeSuccess(e,t,r,o,i,s,u);return}if(u.rebaseConflict){await this.handleInitialAutoMergeConflict(e,t,r,o,i,l,u.error,u);return}let m=ie(u.dirtyError?`❌ [${e.name}] Merge blocked: ${u.error}`:`❌ [${e.name}] Merge failed: ${u.error??"unknown error"}`,u);if(e.worktreeState==="merge_conflict_resolving"||e.worktreeLifecycle?.state==="merge_conflict_resolving"){this.handleAutoMergeRetryFailure(e,o,r,m,l);return}this.deps.dispatchSessionNotification(e,{label:"worktree-merge-error",idempotencyKey:`worktree-merge-error:${e.id}:${o}:${re(e)}`,userMessage:m})},()=>{this.deps.dispatchSessionNotification(e,{label:"worktree-merge-queued",idempotencyKey:`worktree-merge-queued:${e.id}:${o}:${re(e)}`,userMessage:`🕐 [${e.name}] Merge queued — another merge for this repo is in progress. Will notify when complete.`})}),d}async handleAutoPrStrategy(e,t,r,o,i,s={merge:!0,pr:!0}){let a=this.releaseIfRepresentedByTargetPrBranch(e,t,r,o,i);if(a)return a;if(this.updatePersistedSessionFor(e,{lifecycle:"terminal",worktreeState:"pr_in_progress"}),!(await this.deps.runAutoPr(e,i)).success){let d=this.releaseIfRepresentedByTargetPrBranch(e,t,r,o,i);if(d)return d;this.markPendingDecision(e),this.deps.dispatchSessionNotification(e,{label:"worktree-auto-pr-failed",idempotencyKey:`worktree-auto-pr-failed:${e.id}:${i}:${re(e)}`,userMessage:`⚠️ [${e.name}] Auto-PR did not complete. The worktree is preserved for an explicit merge or PR decision.`,buttons:this.getPolicyAwareWorktreeDecisionButtons(e.id,s)})}return{notificationSent:!0,worktreeRemoved:!1}}getPrStatusForUrl(e,t,r){return this.deps.getPrStatusForUrl?.(e,t,r)??Lt(e,t,r)}releaseIfRepresentedByTargetPrBranch(e,t,r,o,i){if(nt(r).length>0)return;let s=this.resolveExistingTargetPr(e,t,o,i),a=s?.headRefName;if(!a||a===o||a===i)return;let l=this.deps.fetchRemoteBranch?this.deps.fetchRemoteBranch(t,a):si(t,a);if(!l||!!!((s?.state==="open"||s?.state==="merged")&&s?.baseRefName===i&&Ot(t,o,l)))return;let u=L(e)?!0:B(t,r);return u?(e.worktreePath=void 0,e.worktreePrUrl=s.url,e.worktreePrNumber=s.number,e.worktreePrTargetRepo=Dr(t,e.worktreePrTargetRepo),this.updatePersistedSessionFor(e,{worktreePath:void 0,worktreePrUrl:s.url,worktreePrNumber:s.number,worktreePrTargetRepo:e.worktreePrTargetRepo,worktreeRemoteOutcome:"pr-updated"}),H(t,o),this.markReleased(e,[`released_by_branch:${a}`]),{notificationSent:!1,worktreeRemoved:u}):(this.markPendingDecision(e,{notes:[`represented_by_branch:${a}`,"represented_worktree_cleanup_failed"]}),{notificationSent:!1,worktreeRemoved:!1})}};import{existsSync as pm,statSync as mm,writeFileSync as fm}from"fs";import{randomUUID as xp}from"crypto";function Ca(n){return n==="plan-approve"||n==="plan-request-changes"||n==="plan-reject"}var Xn=class{constructor(e,t){this.onChange=e;this.retentionMs=t}onChange;retentionMs;tokens=new Map;afterChange;replaceAll(e){this.tokens.clear();for(let t of e)this.tokens.set(t.id,t)}clear(){this.tokens.clear(),this.notifyChanged()}listForPersistence(){return[...this.tokens.values()]}createActionToken(e,t,r={}){let o={id:xp(),sessionId:e,kind:t,createdAt:Date.now(),...r};return this.tokens.set(o.id,o),this.notifyChanged(),o}getActionToken(e){let t=this.tokens.get(e);if(t){if(t.expiresAt!=null&&t.expiresAt<=Date.now()){this.tokens.delete(e),this.notifyChanged();return}return t}}listActiveActionTokens(e){let t=[];for(let r of[...this.tokens.keys()]){let o=this.getActionToken(r);!o||o.consumedAt!=null||e&&o.kind!==e||t.push(o)}return t}consumeActionToken(e){let t=this.getActionToken(e);if(!(!t||t.consumedAt!=null))return t.consumedAt=Date.now(),this.notifyChanged(),t}consumeQuestionAnswerTokens(e,t,r){let o=[],i=Date.now();for(let s of this.tokens.values())s.sessionId!==e||s.kind!=="question-answer"||s.pendingInputRequestId!==t||r!=null&&s.pendingInputQuestionId!==r||s.consumedAt!=null||(s.consumedAt=i,o.push(s));return o.length>0&&this.notifyChanged(),o}consumePlanDecisionTokens(e,t){let r=[],o=Date.now();for(let i of this.tokens.values())i.sessionId!==e||!Ca(i.kind)||i.planDecisionVersion!==t||i.consumedAt!=null||(i.consumedAt=o,r.push(i));return r.length>0&&this.notifyChanged(),r}deleteActionTokensForSession(e){let t=!1;for(let[r,o]of this.tokens)o.sessionId===e&&(this.tokens.delete(r),t=!0);t&&this.notifyChanged()}deleteActionTokensForSessionByKind(e,t){let r=!1;for(let[o,i]of this.tokens)i.sessionId===e&&i.kind===t&&(this.tokens.delete(o),r=!0);r&&this.notifyChanged()}deletePlanDecisionTokensForSession(e,t){let r=!1;for(let[o,i]of this.tokens)i.sessionId!==e||!Ca(i.kind)||t!=null&&i.planDecisionVersion===t||(this.tokens.delete(o),r=!0);r&&this.notifyChanged()}purgeExpiredActionTokens(e=Date.now()){let t=!1;for(let[r,o]of this.tokens){let i=o.expiresAt!=null&&o.expiresAt<=e,s=o.consumedAt!=null&&e-o.consumedAt>=this.retentionMs;(i||s)&&(this.tokens.delete(r),t=!0)}return t&&this.notifyChanged(),t}nextExpiryAt(){let e;for(let t of this.tokens.values()){let r=[t.expiresAt,t.consumedAt!=null?t.consumedAt+this.retentionMs:void 0].filter(o=>o!=null&&Number.isFinite(o));for(let o of r)(e==null||o<e)&&(e=o)}return e}setAfterChangeListener(e){this.afterChange=e}notifyChanged(){this.onChange(),this.afterChange?.()}};var Zn=class{constructor(e){this.indexes=e}indexes;hasRecordedSession(e){return this.indexes.idIndex.has(e)}getLatestPersistedByName(e){let t,r=Number.NEGATIVE_INFINITY,o=Number.NEGATIVE_INFINITY,i=Number.NEGATIVE_INFINITY,s=0;for(let a of this.indexes.persisted.values()){if(a.name!==e){s++;continue}let l=a.createdAt??Number.NEGATIVE_INFINITY,d=a.completedAt??Number.NEGATIVE_INFINITY;(l>r||l===r&&d>o||l===r&&d===o&&s>i)&&(t=a,r=l,o=d,i=s),s++}return t}resolveBackendConversationId(e,t){if(t)return t;let r=this.indexes.idIndex.get(e);if(r){let s=this.indexes.persisted.get(r);return(s?C(s):void 0)??r}let o=this.getLatestPersistedByName(e);if(o)return C(o)??o.harnessSessionId;let i=this.indexes.backendIdIndex.get(e);if(i){let s=this.indexes.persisted.get(i);return(s?C(s):void 0)??i}if(this.indexes.persisted.has(e)){let s=this.indexes.persisted.get(e);return s?C(s)??e:e}if(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(e))return e}resolveHarnessSessionId(e,t){return this.resolveBackendConversationId(e,t)}getPersistedSession(e){let t=this.indexes.idIndex.get(e);if(t)return this.indexes.persisted.get(t);let r=this.getLatestPersistedByName(e);if(r)return r;let o=this.indexes.backendIdIndex.get(e);return o?this.indexes.persisted.get(o):this.indexes.persisted.get(e)}listPersistedSessions(){return[...this.indexes.persisted.values()].sort((e,t)=>(t.completedAt??0)-(e.completedAt??0))}};import{existsSync as Ba,mkdirSync as nm,readFileSync as rm,readdirSync as om,renameSync as Ka,statSync as qa,unlinkSync as Va,writeFileSync as Ua}from"fs";import{tmpdir as im}from"os";import{dirname as sm,join as Ga}from"path";import{homedir as Cp}from"node:os";import{join as Mp}from"node:path";function er(n){let e=n.OPENCLAW_HOME?.trim();if(e)return e;let t=n.HOME?.trim()||Cp();return Mp(t,".openclaw")}var tr=7,_p=new Set(["running","completed","failed","killed"]),Dp=new Set(["queued","running","waiting","blocked","succeeded","failed","cancelled","lost"]);function ae(n){return!!n&&typeof n=="object"}function Qe(n,e=""){return typeof n=="string"&&n.trim().length>0?n:e}function w(n){return typeof n=="string"&&n.trim().length>0?n:void 0}function q(n){return typeof n=="number"&&Number.isFinite(n)?n:void 0}function $p(n){if(!ae(n))return;let e=w(n.flowId),t=q(n.revision);if(!e||t===void 0)return;let r=typeof n.status=="string"&&Dp.has(n.status)?n.status:void 0;return{flowId:e,revision:t,...r?{status:r}:{}}}function Ma(n){return typeof n=="string"&&xi.has(n)?n:void 0}function Lo(n){return n==="default"||n==="plan"||n==="bypassPermissions"?n:void 0}function Ep(n){return n==="never"||n==="on-request"?"never":void 0}function _a(n){return n==="approve"||n==="ask"||n==="delegate"?n:void 0}function Np(n){if(n==="plan-mode"||n==="codex-first-turn-plan"||n==="soft-plan")return"plan-mode"}function Da(n){return typeof n=="string"&&Zt.has(n)?n:void 0}function Bo(n){return typeof n=="string"&&Ti.has(n)?n:void 0}function $a(n){return n==="github"||n==="unsupported"?n:void 0}function Op(n){return n==="stored"||n==="seeded"||n==="unknown"?n:void 0}function Wp(n){return n==="user"||n==="idle-timeout"||n==="startup-timeout"||n==="shutdown"||n==="done"||n==="unknown"?n:void 0}function Ea(n){return n==="starting"||n==="active"||n==="awaiting_plan_decision"||n==="awaiting_user_input"||n==="awaiting_worktree_decision"||n==="suspended"||n==="terminal"?n:void 0}function Fp(n){return n==="not_required"||n==="pending"||n==="approved"||n==="changes_requested"||n==="rejected"?n:void 0}function Lp(n){return n==="none"||n==="provisioned"||n==="pending_decision"||n==="merge_conflict_resolving"||n==="merge_in_progress"||n==="pr_in_progress"||n==="merged"||n==="released"||n==="pr_open"||n==="dismissed"||n==="cleanup_failed"?n:void 0}function Bp(n){return n==="none"||n==="provisioned"||n==="pending_decision"||n==="merge_conflict_resolving"||n==="pr_open"||n==="merged"||n==="released"||n==="dismissed"||n==="no_change"||n==="cleanup_failed"?n:void 0}function Na(n){return n==="live"||n==="stopped"?n:void 0}function Kp(n){if(!ae(n)||n.reason!=="persisted-running-without-runtime")return;let e=w(n.recoveredAt);if(!e||!Number.isFinite(Date.parse(e)))return;let t=n.normalizedStatus==="killed"?"killed":void 0;if(t)return{reason:"persisted-running-without-runtime",recoveredAt:e,rawStatus:w(n.rawStatus),rawLifecycle:w(n.rawLifecycle),rawRuntimeState:w(n.rawRuntimeState),rawResumable:typeof n.rawResumable=="boolean"?n.rawResumable:void 0,rawCompletedAt:q(n.rawCompletedAt),rawOutputPath:w(n.rawOutputPath),normalizedStatus:t,normalizedLifecycle:Ea(n.normalizedLifecycle),normalizedRuntimeState:Na(n.normalizedRuntimeState)}}function qp(n){return{reason:"persisted-running-without-runtime",recoveredAt:new Date().toISOString(),rawStatus:w(n.status),rawLifecycle:w(n.lifecycle),rawRuntimeState:w(n.runtimeState),rawResumable:typeof n.resumable=="boolean"?n.resumable:void 0,rawCompletedAt:q(n.completedAt),rawOutputPath:w(n.outputPath),normalizedStatus:"killed",normalizedLifecycle:"suspended",normalizedRuntimeState:"stopped"}}function Vp(n){return n==="not_sent"||n==="sending"||n==="delivered"||n==="fallback_delivered"||n==="failed"?n:void 0}function Up(n){return n==="direct-telegram"?"direct-message":n==="none"||n==="direct-message"||n==="wake-only"?n:void 0}function Gp(n){return n==="none"||n==="canonical_buttons"||n==="explicit_fallback_text"?n:void 0}function jp(n){return n==="idle"||n==="notifying"||n==="wake_pending"||n==="failed"?n:void 0}function zp(n){return n==="in_flight"||n==="delivered"?n:void 0}function Hp(n){if(!Array.isArray(n))return;let e=n.map(t=>{if(!ae(t))return;let r=w(t.key),o=zp(t.status),i=w(t.recordedAt);if(!(!r||!o||!i||!Number.isFinite(Date.parse(i))))return{key:r,status:o,recordedAt:i,label:w(t.label)}}).filter(t=>!!t);return e.length>0?e.slice(-64):void 0}function Qp(n){if(!Array.isArray(n))return;let e=n.map(t=>{if(!ae(t))return;let r=w(t.key),o=w(t.recordedAt);if(!r||!o||!Number.isFinite(Date.parse(o)))return;let i=Array.isArray(t.linkedKeys)?[...new Set(t.linkedKeys.map(w).filter(s=>!!(s&&s!==r)))]:void 0;return{key:r,linkedKeys:i&&i.length>0?i:void 0,recordedAt:o,label:w(t.label),skipReason:w(t.skipReason)}}).filter(t=>!!t);return e.length>0?e.slice(-1024):void 0}function Yp(n){return n==="awaiting_plan_output"||n==="awaiting_approval"||n==="approved_then_implemented"||n==="implemented_without_required_approval"||n==="not_plan_gated"?n:void 0}function Jp(n){return n==="plan-approve"||n==="plan-request-changes"||n==="plan-reject"||n==="plan-offer-start"||n==="plan-offer-dismiss"||n==="repo-policy-set"||n==="worktree-merge"||n==="worktree-create-pr"||n==="worktree-update-pr"||n==="worktree-view-pr"||n==="worktree-decide-later"||n==="worktree-dismiss"||n==="session-resume"||n==="session-restart"||n==="view-output"||n==="question-answer"?n:void 0}function Oa(n){if(!ae(n))return;let e=w(n.provider),t=w(n.accountId),r=w(n.target),o=w(n.threadId),i=w(n.sessionKey);if(!(!e||!r))return{provider:e,accountId:t,target:r,threadId:o,sessionKey:i}}function Xp(n,e,t){if(ae(n)){let r=w(n.kind),o=w(n.conversationId);if(o&&(r==="claude-code"||r==="codex-app-server"||r==="opencode-server"))return{kind:r,conversationId:o,runId:w(n.runId),worktreeId:w(n.worktreeId),worktreePath:w(n.worktreePath)}}if(e==="claude-code")return{kind:"claude-code",conversationId:t}}function Zp(n){if(!ae(n))return;let e=Bp(n.state),t=w(n.updatedAt);if(!e||!t)return;let r=w(n.resolutionSource);return{state:e,updatedAt:t,resolvedAt:w(n.resolvedAt),resolutionSource:r==="agent_merge"||r==="agent_pr"||r==="strategy_no_change"||r==="lifecycle_resolver"||r==="dismiss"||r==="maintenance"?r:void 0,baseBranch:w(n.baseBranch),targetRepo:w(n.targetRepo),pushRemote:w(n.pushRemote),notes:Array.isArray(n.notes)?n.notes.filter(o=>typeof o=="string"&&o.length>0):void 0}}function em(n){let e=new Date(0).toISOString(),t=w(n.worktreeMergedAt)??w(n.worktreeDismissedAt)??w(n.pendingWorktreeDecisionSince)??e;if(n.worktreeMerged===!0||n.worktreeDisposition==="merged")return{state:"merged",updatedAt:t,resolvedAt:w(n.worktreeMergedAt)??t,resolutionSource:"agent_merge",baseBranch:w(n.worktreeBaseBranch),targetRepo:w(n.worktreePrTargetRepo),pushRemote:w(n.worktreePushRemote)};if(n.worktreeDisposition==="dismissed")return{state:"dismissed",updatedAt:t,resolvedAt:w(n.worktreeDismissedAt)??t,resolutionSource:"dismiss",baseBranch:w(n.worktreeBaseBranch),targetRepo:w(n.worktreePrTargetRepo),pushRemote:w(n.worktreePushRemote)};if(n.worktreeDisposition==="no-change-cleaned")return{state:"no_change",updatedAt:t,resolvedAt:t,resolutionSource:"strategy_no_change",baseBranch:w(n.worktreeBaseBranch),targetRepo:w(n.worktreePrTargetRepo),pushRemote:w(n.worktreePushRemote)};if(n.worktreeDisposition==="pr-opened"||n.worktreeState==="pr_open")return{state:"pr_open",updatedAt:t,baseBranch:w(n.worktreeBaseBranch),targetRepo:w(n.worktreePrTargetRepo),pushRemote:w(n.worktreePushRemote)};if(n.pendingWorktreeDecisionSince||n.worktreeState==="pending_decision")return{state:"pending_decision",updatedAt:t,baseBranch:w(n.worktreeBaseBranch),targetRepo:w(n.worktreePrTargetRepo),pushRemote:w(n.worktreePushRemote)};if(n.worktreeState==="merge_conflict_resolving")return{state:"merge_conflict_resolving",updatedAt:t,baseBranch:w(n.worktreeBaseBranch),targetRepo:w(n.worktreePrTargetRepo),pushRemote:w(n.worktreePushRemote)};if(w(n.worktreePath)||w(n.worktreeBranch))return{state:"provisioned",updatedAt:t,baseBranch:w(n.worktreeBaseBranch),targetRepo:w(n.worktreePrTargetRepo),pushRemote:w(n.worktreePushRemote)}}function tm(n){if(typeof n=="string"&&_p.has(n))return n==="running"?"killed":n}function Wa(n){if(!ae(n))return;let e=Qe(n.harnessSessionId);if(!e)return;let t=tm(n.status);if(!t)return;let r=n.status==="running",o=w(n.worktreePath),i=w(n.worktreeBranch),s=w(n.originChannel),a=typeof n.originThreadId=="string"||typeof n.originThreadId=="number"?n.originThreadId:void 0,l=w(n.originSessionKey),d=Oa(n.route);if(!d)return;let u=Z({route:d,originChannel:s,originThreadId:a,originSessionKey:l});if(!u||o&&!i)return;let m=w(n.harness),g=Xp(n.backendRef,m,e),S=Zp(n.worktreeLifecycle)??em(n),y=w(n.completionWakeSucceededAt),c=w(n.completionWakeSkippedAt),h=n.completionWakeSummaryRequired===!0&&!c?!0:void 0;return{sessionId:w(n.sessionId),harnessSessionId:e,backendRef:g,name:Qe(n.name,e),prompt:Qe(n.prompt),workdir:Qe(n.workdir,"(unknown)"),model:w(n.model),reasoningEffort:Ma(n.reasoningEffort),fastMode:n.fastMode===!0?!0:void 0,createdAt:q(n.createdAt),completedAt:q(n.completedAt),status:t,lifecycle:r?"suspended":Ea(n.lifecycle),approvalState:Fp(n.approvalState),worktreeState:Lp(n.worktreeState),runtimeState:r?"stopped":Na(n.runtimeState),runtimeRecovery:r?qp(n):Kp(n.runtimeRecovery),taskFlowMirror:$p(n.taskFlowMirror),deliveryState:jp(n.deliveryState),notificationDedupe:Hp(n.notificationDedupe),completionSummaryDedupe:Qp(n.completionSummaryDedupe),completionWakeIssuedAt:w(n.completionWakeIssuedAt),completionWakeSucceededAt:y,completionWakeFailedAt:w(n.completionWakeFailedAt),completionWakeSkippedAt:c,completionWakeSkipReason:w(n.completionWakeSkipReason),completionWakeSummaryRequired:h,killReason:Wp(n.killReason),costUsd:typeof n.costUsd=="number"&&Number.isFinite(n.costUsd)?n.costUsd:0,originAgentId:w(n.originAgentId),originChannel:s,originThreadId:a,originSessionKey:l,route:u,outputPath:w(n.outputPath),harness:m,resumedFromSessionName:w(n.resumedFromSessionName),goalTaskId:w(n.goalTaskId),requestedPermissionMode:Lo(n.requestedPermissionMode),currentPermissionMode:Lo(n.currentPermissionMode),approvalExecutionState:Yp(n.approvalExecutionState),approvalRationale:w(n.approvalRationale),planModeApproved:n.planModeApproved===!0,pendingPlanApproval:n.pendingPlanApproval===!0,planApprovalContext:Np(n.planApprovalContext),planDecisionVersion:q(n.planDecisionVersion),actionablePlanDecisionVersion:q(n.actionablePlanDecisionVersion),canonicalPlanPromptVersion:q(n.canonicalPlanPromptVersion),approvalPromptRequiredVersion:q(n.approvalPromptRequiredVersion),approvalPromptVersion:q(n.approvalPromptVersion),approvalPromptStatus:Vp(n.approvalPromptStatus),approvalPromptTransport:Up(n.approvalPromptTransport),approvalPromptMessageKind:Gp(n.approvalPromptMessageKind),approvalPromptLastAttemptAt:w(n.approvalPromptLastAttemptAt),approvalPromptDeliveredAt:w(n.approvalPromptDeliveredAt),approvalPromptFailedAt:w(n.approvalPromptFailedAt),planApproval:_a(n.planApproval),codexApprovalPolicy:Ep(n.codexApprovalPolicy),worktreePath:o,worktreeBranch:i,worktreeStrategy:Da(n.worktreeStrategy),repoIntegrationPolicy:Bo(n.repoIntegrationPolicy),repoIntegrationPolicySource:Op(n.repoIntegrationPolicySource),repoProvider:$a(n.repoProvider),worktreeMerged:typeof n.worktreeMerged=="boolean"?n.worktreeMerged:void 0,worktreeMergedAt:w(n.worktreeMergedAt),worktreePrUrl:w(n.worktreePrUrl),worktreePrNumber:q(n.worktreePrNumber),pendingWorktreeDecisionSince:w(n.pendingWorktreeDecisionSince),lastWorktreeReminderAt:w(n.lastWorktreeReminderAt),worktreeBaseBranch:w(n.worktreeBaseBranch),worktreeParentBranch:w(n.worktreeParentBranch),worktreePrTargetRepo:w(n.worktreePrTargetRepo),autoMergeParentSessionId:w(n.autoMergeParentSessionId),autoMergeConflictResolutionAttemptCount:q(n.autoMergeConflictResolutionAttemptCount),autoMergeResolverSessionId:w(n.autoMergeResolverSessionId),worktreePushRemote:w(n.worktreePushRemote),worktreeDecisionSnoozedUntil:w(n.worktreeDecisionSnoozedUntil),worktreeDisposition:n.worktreeDisposition==="active"||n.worktreeDisposition==="pr-opened"||n.worktreeDisposition==="merged"||n.worktreeDisposition==="dismissed"||n.worktreeDisposition==="no-change-cleaned"?n.worktreeDisposition:void 0,worktreeRemoteOutcome:n.worktreeRemoteOutcome==="pr-opened"||n.worktreeRemoteOutcome==="pr-updated"?n.worktreeRemoteOutcome:void 0,worktreeDismissedAt:w(n.worktreeDismissedAt),worktreeLifecycle:S,resumable:r?!0:n.resumable===!0}}function Fa(n){if(!ae(n))return;let e=w(n.key),t=w(n.repoRoot),r=Bo(n.policy),o=$a(n.provider)??"unsupported",i=w(n.createdAt),s=w(n.updatedAt),a=n.source==="seeded"?"seeded":"stored";if(!(!e||!t||!r||!i||!s)&&!(!Number.isFinite(Date.parse(i))||!Number.isFinite(Date.parse(s))))return{key:e,policy:r,repoRoot:t,remoteUrl:w(n.remoteUrl),provider:o,createdAt:i,updatedAt:s,source:a}}function La(n){if(!ae(n))return;let e=Jp(n.kind),t=Qe(n.id),r=Qe(n.sessionId),o=q(n.createdAt);if(!(!t||!r||!e||o==null))return{id:t,sessionId:r,kind:e,createdAt:o,planDecisionVersion:q(n.planDecisionVersion),expiresAt:q(n.expiresAt),consumedAt:q(n.consumedAt),optionIndex:q(n.optionIndex),pendingInputRequestId:w(n.pendingInputRequestId),pendingInputQuestionId:w(n.pendingInputQuestionId),label:w(n.label),targetUrl:w(n.targetUrl),route:Oa(n.route),launchName:w(n.launchName),launchPrompt:w(n.launchPrompt),launchWorkdir:w(n.launchWorkdir),launchModel:w(n.launchModel),launchReasoningEffort:Ma(n.launchReasoningEffort),launchFastMode:n.launchFastMode===!0?!0:void 0,launchSystemPrompt:w(n.launchSystemPrompt),launchAllowedTools:Array.isArray(n.launchAllowedTools)?n.launchAllowedTools.filter(i=>typeof i=="string"):void 0,launchResumeSessionId:w(n.launchResumeSessionId),launchResumedFromSessionName:w(n.launchResumedFromSessionName),launchResumeWorktreeFrom:w(n.launchResumeWorktreeFrom),launchSessionIdOverride:w(n.launchSessionIdOverride),launchClearedPersistedCodexResume:n.launchClearedPersistedCodexResume===!0?!0:void 0,launchForkSession:typeof n.launchForkSession=="boolean"?n.launchForkSession:void 0,launchForceNewSession:typeof n.launchForceNewSession=="boolean"?n.launchForceNewSession:void 0,launchPermissionMode:Lo(n.launchPermissionMode),launchPlanApproval:_a(n.launchPlanApproval),launchHarness:w(n.launchHarness),launchWorktreeStrategy:Da(n.launchWorktreeStrategy),launchWorktreeBaseBranch:w(n.launchWorktreeBaseBranch),launchWorktreePrTargetRepo:w(n.launchWorktreePrTargetRepo),launchOriginAgentId:w(n.launchOriginAgentId),repoPolicy:Bo(n.repoPolicy),repoPolicyWorkdir:w(n.repoPolicyWorkdir)}}function nr(n){if(!n.backendRef?.kind||!n.backendRef.conversationId)throw new Error(`Persisted session ${n.harnessSessionId} is missing required backend ref metadata.`);if(!n.route?.provider||!n.route.target)throw new Error(`Persisted session ${n.harnessSessionId} is missing required route metadata.`);if(n.worktreePath&&!n.worktreeBranch)throw new Error(`Persisted session ${n.harnessSessionId} is missing required worktreeBranch metadata.`)}function Pt(n){return!!n&&typeof n=="object"}function Ko(n){return n instanceof Error?n.message:String(n)}function am(n,e){JSON.stringify({component:"SessionStore",event:n,at:new Date().toISOString(),...e})}function lm(n){return Pt(n)?{backendRefKind:typeof n.kind=="string"?n.kind:void 0,hasBackendConversationId:typeof n.conversationId=="string"&&n.conversationId.length>0,hasBackendRunId:typeof n.runId=="string"&&n.runId.length>0,hasBackendWorktreeId:typeof n.worktreeId=="string"&&n.worktreeId.length>0,hasBackendWorktreePath:typeof n.worktreePath=="string"&&n.worktreePath.length>0}:{}}function ja(n,e,t=Date.now()){let r=`${n}.${e}-${t}`;for(let o=0;o<100;o+=1){let i=o===0?"":`-${o}`,s=`${r}${i}.json`;if(!Ba(s))return s}}function qo(){let n=im();return om(n).filter(e=>e.startsWith("openclaw-agent-")&&e.endsWith(".txt")).map(e=>Ga(n,e))}function za(n){let e=n.OPENCLAW_CODE_AGENT_SESSIONS_PATH?.trim();return e||Ga(er(n),"code-agent-sessions.json")}function Ha(n,e,t,r=[]){try{nm(sm(n),{recursive:!0});let o=n+".tmp";Ua(o,JSON.stringify({schemaVersion:tr,sessions:e,actionTokens:t,repoPolicies:r},null,2),"utf-8"),Ka(o,n)}catch(o){`${Ko(o)}`}}function dm(n,e){try{if(!Ba(n))return!1;let t=ja(n,"legacy");return t?(Ka(n,t),`${e}${t}`,!0):!1}catch(t){return`${Ko(t)}`,!1}}var um={archiveLegacySessionIndex:dm};function cm(n,e){try{if(e.length===0)return;let t=ja(n,"codex-sdk-legacy");if(!t)return;Ua(t,JSON.stringify(e,null,2),"utf-8"),`${e.length}${t}`}catch(t){`${Ko(t)}`}}function Qa(n,e,t=[]){try{let r=new Set(t);for(let o of qo())if(!r.has(o))try{let i=qa(o).mtimeMs;n-i>e&&Va(o)}catch{}}catch{}}function Ya(n,e,t=[]){try{let r=new Set(t),o;for(let i of qo())if(!r.has(i))try{let s=qa(i).mtimeMs+e;if(s<=n)return n;o=o==null?s:Math.min(o,s)}catch{}return o}catch{return}}function Ja(n){try{let e=new Set(n);for(let t of qo())if(!e.has(t))try{Va(t)}catch{}}catch{}}function Xa(n){let{indexPath:e,clearAll:t,indexPersistedEntry:r,setActionToken:o,setRepoPolicy:i,purgeExpiredActionTokens:s,saveIndex:a}=n,l=d=>um.archiveLegacySessionIndex(e,d)?(t(),!0):!1;try{let d=rm(e,"utf-8"),u=JSON.parse(d);if(Array.isArray(u)){if(!l("legacy array store"))return;a();return}if(!Pt(u)||u.schemaVersion!==tr&&u.schemaVersion!==6&&u.schemaVersion!==4){if(!l(`schema mismatch (expected v${tr})`))return;a();return}let m=(R,f)=>{if(!Object.prototype.hasOwnProperty.call(u,R))return[];let v=u[R];if(Array.isArray(v))return v;l(f)&&a()},g=m("sessions","invalid sessions collection");if(g===void 0)return;let S=[],y=[],c=!1;for(let R of g){if(Pt(R)&&R.harness==="codex"){let v=Pt(R.backendRef)?R.backendRef:void 0;if((typeof v?.kind=="string"?v.kind:void 0)!=="codex-app-server"){S.push(R);continue}}let f=Wa(R);if(!f){if(!l("invalid v4 session entry"))return;a();return}Pt(R)&&R.status==="running"&&(c=!0,am("session.recovered_from_running_persisted_row",{sessionId:typeof R.sessionId=="string"?R.sessionId:void 0,hasHarnessSessionId:typeof R.harnessSessionId=="string"&&R.harnessSessionId.length>0,...lm(R.backendRef),rawStatus:R.status,rawLifecycle:R.lifecycle,rawRuntimeState:R.runtimeState,normalizedStatus:f.status,normalizedLifecycle:f.lifecycle,normalizedRuntimeState:f.runtimeState,reason:f.runtimeRecovery?.reason})),y.push(f)}S.length>0&&cm(e,S);let h=m("actionTokens","invalid action token collection");if(h===void 0)return;let P=[];for(let R of h){let f=La(R);if(!f){if(!l("invalid v4 action token"))return;a();return}P.push(f)}let b=m("repoPolicies","invalid repo policy collection");if(b===void 0)return;let A=[],I=!1;for(let R of b){let f=Fa(R);if(!f){I=!0;continue}A.push(f)}for(let R of y)r(R);for(let R of P)o(R);for(let R of A)i(R);(S.length>0||c)&&a(),I&&a(),s()}catch(d){if(d.code==="ENOENT"||!l("corrupt or unreadable"))return;a()}}var gm=new Set(["completed","failed","killed"]),Vo=1440*60*1e3;function hm(n){return n instanceof Error?n.message:String(n)}var ym={statSync:mm};function Sm(n){try{return ym.statSync(n).isDirectory()}catch(e){let t=e.code;return t!=="ENOENT"&&t!=="ENOTDIR"}}var rr=class{persisted=new Map;repoPolicies=new Map;idIndex=new Map;nameIndex=new Map;backendIdIndex=new Map;actionTokens;actionTokenStore;indexPath;queries;constructor(e={}){let t=e.env??process.env;this.indexPath=e.indexPath??za(t),this.actionTokenStore=new Xn(()=>this.saveIndex(),Vo),this.actionTokens=this.actionTokenStore.tokens,this.queries=new Zn({persisted:this.persisted,idIndex:this.idIndex,nameIndex:this.nameIndex,backendIdIndex:this.backendIdIndex}),t.OPENCLAW_DEBUG_SESSION_STORE==="1"&&`${this.indexPath}`,this.loadIndex()}loadIndex(){Xa({indexPath:this.indexPath,clearAll:()=>{this.persisted.clear(),this.repoPolicies.clear(),this.idIndex.clear(),this.nameIndex.clear(),this.backendIdIndex.clear(),this.actionTokenStore.clear()},indexPersistedEntry:e=>this.indexPersistedEntry(e),setActionToken:e=>{this.actionTokens.set(e.id,e)},setRepoPolicy:e=>{this.repoPolicies.set(e.key,e)},purgeExpiredActionTokens:()=>this.actionTokenStore.purgeExpiredActionTokens(),saveIndex:()=>this.saveIndex()})}saveIndex(){Ha(this.indexPath,[...this.persisted.values()],this.actionTokenStore.listForPersistence(),[...this.repoPolicies.values()])}assertPersistedEntry(e){nr(e)}getEntryStorageKey(e){return e.harnessSessionId}buildPersistedBackendRef(e){let t=ri(e),r=t==="codex"?"codex-app-server":t==="opencode"?"opencode-server":"claude-code";return e.backendRef??{kind:e.backendKind??r,conversationId:e.harnessSessionId}}getSessionApprovalSnapshot(e){if(typeof e.approvalSnapshot=="function")return e.approvalSnapshot();let t=typeof e.controlStateSnapshot=="function"?e.controlStateSnapshot():{planModeApproved:!1};return{requestedPermissionMode:e.requestedPermissionMode,currentPermissionMode:e.currentPermissionMode,approvalExecutionState:e.approvalExecutionState,approvalRationale:e.approvalRationale,planModeApproved:t.planModeApproved,pendingPlanApproval:e.pendingPlanApproval,planApprovalContext:e.planApprovalContext,planDecisionVersion:e.planDecisionVersion,actionablePlanDecisionVersion:e.actionablePlanDecisionVersion,canonicalPlanPromptVersion:e.canonicalPlanPromptVersion,approvalPromptRequiredVersion:e.approvalPromptRequiredVersion,approvalPromptVersion:e.approvalPromptVersion,approvalPromptStatus:e.approvalPromptStatus,approvalPromptTransport:e.approvalPromptTransport,approvalPromptMessageKind:e.approvalPromptMessageKind,approvalPromptLastAttemptAt:e.approvalPromptLastAttemptAt,approvalPromptDeliveredAt:e.approvalPromptDeliveredAt,approvalPromptFailedAt:e.approvalPromptFailedAt,planApproval:e.planApproval,codexApprovalPolicy:e.codexApprovalPolicy}}getSessionRoutingSnapshot(e){return typeof e.routingSnapshot=="function"?e.routingSnapshot():{route:e.route,originAgentId:e.originAgentId,originChannel:e.originChannel,originThreadId:e.originThreadId,originSessionKey:e.originSessionKey}}getSessionWorktreeSnapshot(e){return typeof e.worktreeSnapshot=="function"?e.worktreeSnapshot():{worktreePath:e.worktreePath,worktreeBranch:e.worktreeBranch,worktreeStrategy:e.worktreeStrategy,repoIntegrationPolicy:e.repoIntegrationPolicy,repoIntegrationPolicySource:e.repoIntegrationPolicySource,repoProvider:e.repoProvider,worktreeBaseBranch:e.worktreeBaseBranch,worktreeParentBranch:e.worktreeParentBranch,worktreePrTargetRepo:e.worktreePrTargetRepo,autoMergeParentSessionId:e.autoMergeParentSessionId,autoMergeConflictResolutionAttemptCount:e.autoMergeConflictResolutionAttemptCount,autoMergeResolverSessionId:e.autoMergeResolverSessionId,worktreeLifecycle:e.worktreeLifecycle}}indexPersistedEntry(e){let t=this.getEntryStorageKey(e);if(e.sessionId){let o=this.idIndex.get(e.sessionId);if(o&&o!==t){let i=this.persisted.get(o);i&&this.removePersistedIndexes(i)}}this.persisted.set(t,e),e.sessionId&&this.idIndex.set(e.sessionId,t),e.name&&this.nameIndex.set(e.name,t);let r=C(e);r&&this.backendIdIndex.set(r,t)}removePersistedIndexes(e){let t=this.getEntryStorageKey(e);this.persisted.delete(t);for(let[r,o]of this.idIndex)o===t&&this.idIndex.delete(r);for(let[r,o]of this.nameIndex)o===t&&this.nameIndex.delete(r);for(let[r,o]of this.backendIdIndex)o===t&&this.backendIdIndex.delete(r)}markRunning(e){if(!e.harnessSessionId)return;let t=this.getSessionApprovalSnapshot(e),r=this.getSessionRoutingSnapshot(e),o=this.getSessionWorktreeSnapshot(e),i=Z({route:r.route,originChannel:r.originChannel,originThreadId:r.originThreadId,originSessionKey:r.originSessionKey});if(!i)throw new Error(`Cannot persist running session ${e.id}: canonical lifecycle route is missing.`);let s={sessionId:e.id,harnessSessionId:e.harnessSessionId,backendRef:this.buildPersistedBackendRef(e),name:e.name,prompt:e.prompt,workdir:e.originalWorkdir??e.workdir,model:e.model,reasoningEffort:e.reasoningEffort,fastMode:e.fastMode,createdAt:e.startedAt,status:"running",lifecycle:e.lifecycle,approvalState:e.approvalState,worktreeState:e.worktreeState,runtimeState:e.runtimeState,taskFlowMirror:e.taskFlowMirror,deliveryState:e.deliveryState,notificationDedupe:this.getExistingNotificationDedupe(e),completionSummaryDedupe:this.getExistingCompletionSummaryDedupe(e),costUsd:0,originAgentId:r.originAgentId,originChannel:r.originChannel,originThreadId:r.originThreadId,originSessionKey:r.originSessionKey,route:i,outputPath:ve(e.id),harness:e.harnessName,resumedFromSessionName:e.resumedFromSessionName,requestedPermissionMode:t.requestedPermissionMode,currentPermissionMode:t.currentPermissionMode,approvalExecutionState:t.approvalExecutionState,approvalRationale:t.approvalRationale,planModeApproved:t.planModeApproved,pendingPlanApproval:t.pendingPlanApproval,planApprovalContext:t.planApprovalContext,planDecisionVersion:t.planDecisionVersion,actionablePlanDecisionVersion:t.actionablePlanDecisionVersion,canonicalPlanPromptVersion:t.canonicalPlanPromptVersion,approvalPromptRequiredVersion:t.approvalPromptRequiredVersion,approvalPromptVersion:t.approvalPromptVersion,approvalPromptStatus:t.approvalPromptStatus,approvalPromptTransport:t.approvalPromptTransport,approvalPromptMessageKind:t.approvalPromptMessageKind,approvalPromptLastAttemptAt:t.approvalPromptLastAttemptAt,approvalPromptDeliveredAt:t.approvalPromptDeliveredAt,approvalPromptFailedAt:t.approvalPromptFailedAt,planApproval:t.planApproval,codexApprovalPolicy:t.codexApprovalPolicy,worktreePath:o.worktreePath,worktreeBranch:o.worktreeBranch,worktreeStrategy:o.worktreeStrategy,repoIntegrationPolicy:o.repoIntegrationPolicy,repoIntegrationPolicySource:o.repoIntegrationPolicySource,repoProvider:o.repoProvider,worktreeBaseBranch:o.worktreeBaseBranch,worktreeParentBranch:o.worktreeParentBranch,worktreePrTargetRepo:o.worktreePrTargetRepo,autoMergeParentSessionId:o.autoMergeParentSessionId,autoMergeConflictResolutionAttemptCount:o.autoMergeConflictResolutionAttemptCount,autoMergeResolverSessionId:o.autoMergeResolverSessionId,worktreeLifecycle:o.worktreeLifecycle,resumable:e.isExplicitlyResumable};nr(s),this.indexPersistedEntry(s),this.saveIndex()}hasRecordedSession(e){return this.queries.hasRecordedSession(e)}persistTerminal(e){if(!e.harnessSessionId)return;let t=this.getSessionApprovalSnapshot(e),r=this.getSessionRoutingSnapshot(e),o=this.getSessionWorktreeSnapshot(e),i=Z({route:r.route,originChannel:r.originChannel,originThreadId:r.originThreadId,originSessionKey:r.originSessionKey});if(!i)throw new Error(`Cannot persist terminal session ${e.id}: canonical lifecycle route is missing.`);let s;try{let l=ve(e.id);if(pm(l))s=l;else{let d=e.getOutput().join(`
|
|
201
|
+
`);d.length>0&&(fm(l,d,"utf-8"),s=l)}}catch(l){`${e.id}${hm(l)}`}let a={sessionId:e.id,harnessSessionId:e.harnessSessionId,backendRef:this.buildPersistedBackendRef(e),name:e.name,prompt:e.prompt,workdir:e.originalWorkdir??e.workdir,model:e.model,reasoningEffort:e.reasoningEffort,fastMode:e.fastMode,createdAt:e.startedAt,completedAt:e.completedAt,status:e.status,lifecycle:e.lifecycle,approvalState:e.approvalState,worktreeState:e.worktreeState,runtimeState:e.runtimeState,taskFlowMirror:e.taskFlowMirror,deliveryState:e.deliveryState,notificationDedupe:this.getExistingNotificationDedupe(e),completionSummaryDedupe:this.getExistingCompletionSummaryDedupe(e),killReason:e.killReason,costUsd:e.costUsd,originAgentId:r.originAgentId,originChannel:r.originChannel,originThreadId:r.originThreadId,originSessionKey:r.originSessionKey,route:i,outputPath:s,harness:e.harnessName,resumedFromSessionName:e.resumedFromSessionName,goalTaskId:e.goalTaskId,requestedPermissionMode:t.requestedPermissionMode,currentPermissionMode:t.currentPermissionMode,approvalExecutionState:t.approvalExecutionState,approvalRationale:t.approvalRationale,planModeApproved:t.planModeApproved,pendingPlanApproval:t.pendingPlanApproval,planApprovalContext:t.planApprovalContext,planDecisionVersion:t.planDecisionVersion,actionablePlanDecisionVersion:t.actionablePlanDecisionVersion,canonicalPlanPromptVersion:t.canonicalPlanPromptVersion,approvalPromptRequiredVersion:t.approvalPromptRequiredVersion,approvalPromptVersion:t.approvalPromptVersion,approvalPromptStatus:t.approvalPromptStatus,approvalPromptTransport:t.approvalPromptTransport,approvalPromptMessageKind:t.approvalPromptMessageKind,approvalPromptLastAttemptAt:t.approvalPromptLastAttemptAt,approvalPromptDeliveredAt:t.approvalPromptDeliveredAt,approvalPromptFailedAt:t.approvalPromptFailedAt,planApproval:t.planApproval,codexApprovalPolicy:t.codexApprovalPolicy,worktreePath:o.worktreePath,worktreeBranch:o.worktreeBranch,worktreeStrategy:o.worktreeStrategy,worktreeBaseBranch:o.worktreeBaseBranch,worktreeParentBranch:o.worktreeParentBranch,worktreePrTargetRepo:o.worktreePrTargetRepo,autoMergeParentSessionId:o.autoMergeParentSessionId,autoMergeConflictResolutionAttemptCount:o.autoMergeConflictResolutionAttemptCount,autoMergeResolverSessionId:o.autoMergeResolverSessionId,worktreeLifecycle:o.worktreeLifecycle,resumable:e.isExplicitlyResumable};nr(a),this.indexPersistedEntry(a),this.saveIndex()}getExistingNotificationDedupe(e){return this.getPersistedSession(e.id)?.notificationDedupe??(e.harnessSessionId?this.getPersistedSession(e.harnessSessionId)?.notificationDedupe:void 0)??(C(e)?this.getPersistedSession(C(e))?.notificationDedupe:void 0)}getExistingCompletionSummaryDedupe(e){return this.getPersistedSession(e.id)?.completionSummaryDedupe??(e.harnessSessionId?this.getPersistedSession(e.harnessSessionId)?.completionSummaryDedupe:void 0)??(C(e)?this.getPersistedSession(C(e))?.completionSummaryDedupe:void 0)}getLatestPersistedByName(e){return this.queries.getLatestPersistedByName(e)}resolveBackendConversationId(e,t){return this.queries.resolveBackendConversationId(e,t)}resolveHarnessSessionId(e,t){return this.queries.resolveHarnessSessionId(e,t)}getPersistedSession(e){return this.queries.getPersistedSession(e)}replacePersistedSession(e){let t=e.sessionId?this.getPersistedSession(e.sessionId):void 0;t&&this.removePersistedIndexes(t),this.indexPersistedEntry(e),this.saveIndex()}listPersistedSessions(){return this.queries.listPersistedSessions()}getRepoPolicy(e){return this.repoPolicies.get(e)}listRepoPolicies(){return[...this.repoPolicies.values()].sort((e,t)=>e.repoRoot.localeCompare(t.repoRoot)||e.key.localeCompare(t.key))}setRepoPolicy(e){let t=this.repoPolicies.get(e.key),r={...e,createdAt:t?.createdAt??e.createdAt,updatedAt:new Date().toISOString(),source:"stored"};return this.repoPolicies.set(r.key,r),this.saveIndex(),r}updateRepoPolicy(e,t){let r=this.repoPolicies.get(e);if(r)return this.setRepoPolicy({...r,policy:t})}resetRepoPolicy(e){let t=this.repoPolicies.delete(e);return t&&this.saveIndex(),t}removeRepoPolicies(e){let t=[];for(let r of e){let o=this.repoPolicies.get(r);o&&(this.repoPolicies.delete(r),t.push(o))}return t.length>0&&this.saveIndex(),t.sort((r,o)=>r.repoRoot.localeCompare(o.repoRoot)||r.key.localeCompare(o.key))}cleanupRepoPolicies(){let e=[];for(let[t,r]of this.repoPolicies)Sm(r.repoRoot)||(this.repoPolicies.delete(t),e.push(r));return e.length>0&&this.saveIndex(),e.sort((t,r)=>t.repoRoot.localeCompare(r.repoRoot)||t.key.localeCompare(r.key))}cleanupTmpOutputFiles(e){this.actionTokenStore.purgeExpiredActionTokens(e),Qa(e,Vo,this.getReferencedOutputPaths())}getNextTmpOutputCleanupAt(e){return Ya(e,Vo,this.getReferencedOutputPaths())}evictOldestPersisted(e){let t=this.listPersistedSessions();if(t.length<=e)return[];let r=t.slice(e);for(let o of r)this.removePersistedIndexes(o);return this.saveIndex(),r}shouldGcActiveSession(e,t,r){return!e.completedAt||!gm.has(e.status)?!1:t-e.completedAt>r}getActionToken(e){return this.actionTokenStore.getActionToken(e)}consumeActionToken(e){return this.actionTokenStore.consumeActionToken(e)}deleteActionTokensForSession(e){this.actionTokenStore.deleteActionTokensForSession(e)}purgeExpiredActionTokens(e=Date.now()){return this.actionTokenStore.purgeExpiredActionTokens(e)}getNextActionTokenExpiry(){return this.actionTokenStore.nextExpiryAt()}onActionTokensChanged(e){this.actionTokenStore.setAfterChangeListener(e)}hasOutputPathReference(e){for(let t of this.persisted.values())if(t.outputPath===e)return!0;return!1}getReferencedOutputPaths(){return[...new Set([...this.persisted.values()].map(e=>e.outputPath).filter(e=>typeof e=="string"&&e.length>0))]}cleanupOrphanOutputFiles(){Ja(this.getReferencedOutputPaths())}};var km=new Set(["completed","failed","killed"]),or=class{metrics={totalCostUsd:0,costPerDay:new Map,sessionsByStatus:{completed:0,failed:0,killed:0},totalLaunched:0,totalDurationMs:0,sessionsWithDuration:0,mostExpensive:null};incrementLaunched(){this.metrics.totalLaunched++}recordSession(e){let t=e.costUsd??0,r=e.status;this.metrics.totalCostUsd+=t;let o=new Date(e.completedAt??e.startedAt).toISOString().slice(0,10);if(this.metrics.costPerDay.set(o,(this.metrics.costPerDay.get(o)??0)+t),km.has(r)&&this.metrics.sessionsByStatus[r]++,e.completedAt){let i=e.completedAt-e.startedAt;this.metrics.totalDurationMs+=i,this.metrics.sessionsWithDuration++}(!this.metrics.mostExpensive||t>this.metrics.mostExpensive.costUsd)&&(this.metrics.mostExpensive={id:e.id,name:e.name,costUsd:t,prompt:G(e.prompt,80)})}getMetrics(){return{totalCostUsd:this.metrics.totalCostUsd,costPerDay:new Map(this.metrics.costPerDay),sessionsByStatus:{...this.metrics.sessionsByStatus},totalLaunched:this.metrics.totalLaunched,totalDurationMs:this.metrics.totalDurationMs,sessionsWithDuration:this.metrics.sessionsWithDuration,mostExpensive:this.metrics.mostExpensive?{...this.metrics.mostExpensive}:null}}};import*as Za from"child_process";var Ye=class{tails=new Map;enqueue(e,t,r){let o=this.tails.get(e);o!==void 0&&r?.();let s=(o?.catch(()=>{})??Promise.resolve()).then(()=>t()),a=s.catch(()=>{});return this.tails.set(e,a),a.finally(()=>{this.tails.get(e)===a&&this.tails.delete(e)}),s}clear(){this.tails.clear()}};var Uo=3e4,vm=2e3,Pm=2e4,j=4;function wt(n){return n instanceof Error?n.message:String(n)}function wm(){return new Error(`Dispatch timed out after ${Uo}ms`)}function bm(n){if(!(n instanceof Error))return!1;let e=n;return e.killed===!0&&e.signal==="SIGKILL"}var Rm={execFile:Za.execFile},ir=class{pendingRetryTimers=new Map;orderedDispatches=new Ye;disposed=!1;clearPendingRetries(){for(let e of this.pendingRetryTimers.values())for(let t of e)clearTimeout(t.timer),t.onCleared?.();this.pendingRetryTimers.clear()}clearRetryTimersForSession(e){let t=this.pendingRetryTimers.get(e);if(t){for(let r of t)clearTimeout(r.timer),r.onCleared?.();this.pendingRetryTimers.delete(e)}}dispose(){this.disposed=!0,this.clearPendingRetries(),this.orderedDispatches.clear()}execute(e,t,r=1){if(!this.disposed){if(r===1&&t.orderingKey){this.enqueueOrderedDispatch(t.orderingKey,o=>this.executeNow(e,t,o,r));return}this.executeNow(e,t,void 0,r)}}executePromise(e,t,r=1){if(!this.disposed){if(r===1&&t.orderingKey){this.enqueueOrderedDispatch(t.orderingKey,o=>this.executePromiseNow(e,t,o,r));return}this.executePromiseNow(e,t,void 0,r)}}executeNow(e,t,r,o=1){if(this.disposed||t.shouldContinue?.()===!1){r?.();return}let i=Date.now();o===1&&t.onStarted?.(),this.log("info","dispatch_started",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j}),Rm.execFile("openclaw",e,{timeout:Uo,killSignal:"SIGKILL"},(s,a,l)=>{if(this.disposed){r?.();return}let d=Date.now()-i;if(!s){let y=t.successValidator?.(a??"");if(y?.outcome==="failure"){this.log("error","dispatch_success_validation_failed",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:d,error:y.reason,terminal:!0}),t.onFinalFailure?.(),r?.();return}if(y?.outcome==="skipped"){this.log("info","dispatch_skipped",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:d,reason:y.reason}),t.shouldContinue?.()!==!1&&t.onSkipped?.(y.reason),r?.();return}this.log("info","dispatch_succeeded",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:d}),t.shouldContinue?.()!==!1&&t.onSuccess?.(),r?.();return}let u=l?.trim()?` | stderr: ${l.trim()}`:"";if(t.shouldContinue?.()===!1){r?.();return}if(t.target==="message.send"&&bm(s)){this.log("error","dispatch_failed",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:d,error:`${wt(s)}${u}`,terminal:!0,ambiguousResult:!0}),t.onAmbiguousResult?.(),r?.();return}if(o>=j||t.terminalOnFailure===!0){this.log("error","dispatch_failed",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:d,error:`${wt(s)}${u}`,terminal:!0}),t.onFinalFailure?.(),r?.();return}let g=this.retryDelayMs(o);this.log("error","dispatch_retry_scheduled",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:d,retryDelayMs:g,error:`${wt(s)}${u}`});let S={timer:setTimeout(()=>{let y=this.pendingRetryTimers.get(t.sessionId);if(y&&(y.delete(S),y.size===0&&this.pendingRetryTimers.delete(t.sessionId)),t.shouldContinue?.()===!1){r?.();return}this.executeNow(e,t,r,o+1)},g),onCleared:r};S.timer.unref?.(),this.pendingRetryTimers.has(t.sessionId)||this.pendingRetryTimers.set(t.sessionId,new Set),this.pendingRetryTimers.get(t.sessionId).add(S)})}executePromiseNow(e,t,r,o=1){if(this.disposed||t.shouldContinue?.()===!1){r?.();return}let i=Date.now();o===1&&t.onStarted?.(),this.log("info","dispatch_started",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j}),this.executePromiseWithTimeout(e).then(()=>{if(this.disposed){r?.();return}let s=Date.now()-i;this.log("info","dispatch_succeeded",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:s}),t.shouldContinue?.()!==!1&&t.onSuccess?.(),r?.()}).catch(s=>{if(this.disposed){r?.();return}let a=Date.now()-i;if(t.shouldContinue?.()===!1){r?.();return}if(o>=j||t.terminalOnFailure===!0){this.log("error","dispatch_failed",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:a,error:wt(s),terminal:!0,...t.terminalOnFailure===!0&&o<j?{terminalReason:"non_retryable"}:{}}),t.onFinalFailure?.(),r?.();return}let l=this.retryDelayMs(o);this.log("error","dispatch_retry_scheduled",{label:t.label,sessionId:t.sessionId,target:t.target,phase:t.phase,messageKind:t.messageKind,route:t.routeSummary,...t.dispatchContext,attempt:o,maxAttempts:j,elapsedMs:a,retryDelayMs:l,error:wt(s)});let d={timer:setTimeout(()=>{let u=this.pendingRetryTimers.get(t.sessionId);if(u&&(u.delete(d),u.size===0&&this.pendingRetryTimers.delete(t.sessionId)),t.shouldContinue?.()===!1){r?.();return}this.executePromiseNow(e,t,r,o+1)},l),onCleared:r};d.timer.unref?.(),this.pendingRetryTimers.has(t.sessionId)||this.pendingRetryTimers.set(t.sessionId,new Set),this.pendingRetryTimers.get(t.sessionId).add(d)})}executePromiseWithTimeout(e){return new Promise((t,r)=>{let o=!1,i=setTimeout(()=>{o||(o=!0,r(wm()))},Uo);i.unref?.(),Promise.resolve().then(e).then(()=>{o||(o=!0,clearTimeout(i),t())}).catch(s=>{o||(o=!0,clearTimeout(i),r(s))})})}retryDelayMs(e){let t=Math.max(0,e-1),r=vm*2**t;return Math.min(r,Pm)}enqueueOrderedDispatch(e,t){this.orderedDispatches.enqueue(e,async()=>{this.disposed||await new Promise(r=>{let o=!1;t(()=>{o||(o=!0,r())})})})}log(e,t,r){let o=`[WakeDispatcher] ${JSON.stringify({event:t,...r})}`;if(e==="error"){console.error(o);return}}};var sr=class{resolve(e){let t=le(e);if(!(!t?.provider||!t.target))return{channel:t.provider,target:t.target,accountId:t.accountId,threadId:t.threadId,sessionKey:t.sessionKey}}summary(e){if(!e)return"system";let t=e.accountId?`|${e.accountId}`:"",r=e.threadId?`#${e.threadId}`:"";return`${e.channel}${t}|${e.target}${r}`}};import{randomUUID as Am}from"crypto";var ar=class{constructor(e={}){}buildChatSendArgs(e,t,r,o=Am()){return["gateway","call","chat.send","--expect-final","--timeout","30000","--params",JSON.stringify({sessionKey:e,message:t,deliver:r,idempotencyKey:o})]}buildDirectNotificationArgs(e,t,r){let o=Jt(r),i=["message","send","--channel",e.channel,"--target",e.target,"--message",t];return e.accountId&&i.push("--account",e.accountId),e.threadId&&i.push("--thread-id",e.threadId),o&&i.push("--presentation",JSON.stringify(o)),i}buildSystemEventArgs(e){return["system","event","--text",e,"--mode","now"]}};function Im(n){let e=Tm(n).trim();return e?/^NO_REPLY$/i.test(e)?{outcome:"failure",reason:"completion follow-up wake ended with NO_REPLY"}:{outcome:"success"}:{outcome:"failure",reason:"completion follow-up wake produced no final response"}}function Tm(n){let e=n.trim();if(!e)return"";try{return Go(JSON.parse(e)).trim()}catch{return e}}function Go(n){if(typeof n=="string")return n;if(Array.isArray(n))return n.map(r=>Go(r)).filter(Boolean).join(`
|
|
202
|
+
`);if(!n||typeof n!="object")return"";let e=n,t=["final","finalResponse","final_response","assistantFinal","assistant_final","response","text","content"];for(let r of t){let o=e[r];if(typeof o=="string"&&o.trim())return o}for(let r of["result","message","data"]){let o=Go(e[r]);if(o.trim())return o}return""}var lr=class{routes=new sr;transport;directNotifications;executor=new ir;constructor(e={}){this.transport=e.transport??new ar(e.transportOptions),this.directNotifications=e.directNotifications===void 0?new Fe:e.directNotifications}clearPendingRetries(){this.executor.clearPendingRetries()}clearRetryTimersForSession(e){this.executor.clearRetryTimersForSession(e)}dispose(){this.executor.dispose()}buildDispatchContext(e){let t=e.buttons??[],r=t.flat();return{transportRoute:e.routeSummary,transportChannel:e.route?.channel??"system",transportTarget:e.route?.target??"system",transportAccountId:e.route?.accountId,transportThreadId:e.route?.threadId,transportSessionKey:e.route?.sessionKey,messageTextLength:e.text.length,buttonsPresent:r.length>0,buttonRows:t.length||void 0,buttonCount:r.length||void 0,buttonLabels:r.length>0?r.map(o=>o.label):void 0,maxCallbackDataLength:r.length>0?Math.max(...r.map(o=>o.callbackData.length)):void 0}}sendWake(e,t,r,o,i,s,a,l,d,u){let m=this.routes.resolve(e),g=a;if(g?.()===!1)return;let S=m?.sessionKey?.trim();if(!S){this.executor.execute(this.transport.buildSystemEventArgs(t),{label:`${r}-system`,sessionId:e.id,target:"system.event",phase:o,routeSummary:"system",messageKind:"wake",dispatchContext:this.buildDispatchContext({routeSummary:"system",text:t}),onSuccess:s,onSkipped:d,onFinalFailure:i,successValidator:l,shouldContinue:g});return}this.executor.execute(this.transport.buildChatSendArgs(S,t,!0,u),{label:r,sessionId:e.id,target:"chat.send",phase:o,routeSummary:`session:${S}`,messageKind:"wake",dispatchContext:this.buildDispatchContext({routeSummary:`session:${S}`,route:m,text:t}),onSuccess:s,onSkipped:d,successValidator:l,shouldContinue:g,onFinalFailure:()=>{g?.()!==!1&&this.executor.execute(this.transport.buildSystemEventArgs(t),{label:`${r}-fallback`,sessionId:e.id,target:"system.event",phase:o,routeSummary:"system",messageKind:"wake",dispatchContext:this.buildDispatchContext({routeSummary:"system",text:t}),onSuccess:s,onSkipped:d,onFinalFailure:i,successValidator:l,shouldContinue:g})}})}sendUserNotification(e,t,r,o,i,s,a=!1,l){if(l?.()===!1)return;let d=!!o?.some(y=>Array.isArray(y)&&y.length>0),u=this.routes.resolve(e);M("wake_notify_selected",{sessionId:e.id,sessionName:e.name,label:r,messageTextLength:t.length,requireDirectDelivery:a,hasDirectNotificationTransport:!!this.directNotifications,routeSummary:u?this.routes.summary(u):"system",channel:u?.channel,target:u?.target,accountId:u?.accountId,threadId:u?.threadId,sessionKey:u?.sessionKey,...E(o)});let m=u?`notify:${u.channel}|${u.accountId??""}|${u.target}|${u.threadId??""}`:`notify:system:${e.id}`;if(!u){if(M("wake_notify_no_direct_route",{sessionId:e.id,sessionName:e.name,label:r,requireDirectDelivery:a,hasInteractiveButtons:d,...E(o)}),a){`${r}${e.id}`,i?.();return}if(d){`${r}${e.id}`,i?.();return}this.executor.execute(this.transport.buildSystemEventArgs(t),{label:`${r}-notify-system`,sessionId:e.id,target:"system.event",phase:"notify",routeSummary:"system",messageKind:"notify",dispatchContext:this.buildDispatchContext({routeSummary:"system",text:t,buttons:o}),orderingKey:m,onSuccess:s,onFinalFailure:i,shouldContinue:l});return}let g=()=>{if(M("wake_notify_direct_failed",{sessionId:e.id,sessionName:e.name,label:r,requireDirectDelivery:a,hasInteractiveButtons:d,channel:u.channel,target:u.target,accountId:u.accountId,threadId:u.threadId,sessionKey:u.sessionKey,...E(o)}),a){`${r}${e.id}`,i?.();return}if(d){`${r}${e.id}`,i?.();return}this.executor.execute(this.transport.buildSystemEventArgs(t),{label:`${r}-notify-fallback`,sessionId:e.id,target:"system.event",phase:"notify",routeSummary:"system",messageKind:"notify",dispatchContext:this.buildDispatchContext({routeSummary:"system",text:t}),orderingKey:m,onSuccess:s,onFinalFailure:i,shouldContinue:l})},S={label:`${r}-notify`,sessionId:e.id,target:"message.send",phase:"notify",routeSummary:this.routes.summary(u),messageKind:"notify",dispatchContext:this.buildDispatchContext({routeSummary:this.routes.summary(u),route:u,text:t,buttons:o}),orderingKey:m,onSuccess:s,onAmbiguousResult:g,onFinalFailure:g,terminalOnFailure:this.directNotifications?!0:void 0,shouldContinue:l};if(this.directNotifications){M("wake_notify_dispatching_direct_runtime",{sessionId:e.id,sessionName:e.name,label:r,channel:u.channel,target:u.target,accountId:u.accountId,threadId:u.threadId,sessionKey:u.sessionKey,...E(o)}),this.executor.executePromise(()=>this.directNotifications.send(u,t,o),S);return}M("wake_notify_dispatching_cli_message_send",{sessionId:e.id,sessionName:e.name,label:r,channel:u.channel,target:u.target,accountId:u.accountId,threadId:u.threadId,sessionKey:u.sessionKey,...E(o)}),this.executor.execute(this.transport.buildDirectNotificationArgs(u,t,o),S)}sendUserNotificationSequence(e,t,r,o,i,s=!1,a){let l=t.map(u=>({text:u.text.trim(),buttons:u.buttons,requiredForSequenceSuccess:u.requiredForSequenceSuccess})).filter(u=>u.text.length>0);if(l.length===0){o?.();return}M("wake_notify_sequence_started",{sessionId:e.id,sessionName:e.name,label:r,chunkCount:l.length,buttonChunkIndexes:l.map((u,m)=>u.buttons?.some(g=>Array.isArray(g)&&g.length>0)?m+1:void 0).filter(u=>typeof u=="number"),requireDirectDelivery:s});let d=u=>{if(a?.()===!1)return;let m=l[u];if(!m){M("wake_notify_sequence_succeeded",{sessionId:e.id,sessionName:e.name,label:r,chunkCount:l.length}),i?.();return}let g=u===0||m.requiredForSequenceSuccess===!0,S=g?o:i;M("wake_notify_sequence_chunk_selected",{sessionId:e.id,sessionName:e.name,label:r,chunkIndex:u+1,chunkCount:l.length,messageTextLength:m.text.length,failureHandler:g?"sequence-failed":"partial-success",...E(m.buttons)}),this.sendUserNotification(e,m.text,`${r}-part-${u+1}`,m.buttons,S,()=>d(u+1),s,a)};d(0)}dispatchSessionNotification(e,t){let r=t.hooks,o=t.wakeMessageOnNotifySuccess!=null||t.wakeMessageOnNotifyFailed!=null,i=t.notifyUser??(t.wakeMessage?"on-wake-fallback":"always"),s=(t.userMessages?.length?t.userMessages:t.userMessage?.trim()?[{text:t.userMessage.trim(),buttons:t.buttons}]:[]).map(u=>({text:u.text.trim(),buttons:u.buttons,requiredForSequenceSuccess:u.requiredForSequenceSuccess})).filter(u=>u.text.length>0),a=t.wakeMessage?.trim(),l=t.shouldDispatch,d=t.completionWakeSummaryRequired===!0?Im:void 0;if(o){let u=t.wakeMessageOnNotifySuccess?.trim(),m=t.wakeMessageOnNotifyFailed?.trim(),g=h=>{h&&l?.()!==!1&&(r?.onWakeStarted?.(),this.sendWake(e,h,`${t.label}-wake`,"wake",r?.onWakeFailed,r?.onWakeSucceeded,l,d,r?.onWakeSkipped,t.idempotencyKey))},S=h=>{if(h){if(t.deferConditionalWakeUntilNextTick===!0||t.deferConditionalWakeMs!==void 0){let P=Math.max(0,Math.floor(t.deferConditionalWakeMs??0));setTimeout(()=>g(h),P).unref?.();return}g(h)}},y=()=>{l?.()!==!1&&(r?.onNotifySucceeded?.(),u&&S(u))},c=m?()=>{l?.()!==!1&&(r?.onNotifyFailed?.(),t.onUserNotifyFailed?.(),S(m))}:()=>{l?.()!==!1&&(r?.onNotifyFailed?.(),t.onUserNotifyFailed?.())};if(s.length>0){if(l?.()===!1)return;r?.onNotifyStarted?.(),this.sendUserNotificationSequence(e,s,t.label,c,y,t.requireDirectUserNotification===!0,l)}else c();return}if(i==="always"&&s.length>0){if(l?.()===!1)return;r?.onNotifyStarted?.(),this.sendUserNotificationSequence(e,s,t.label,()=>{l?.()!==!1&&(r?.onNotifyFailed?.(),t.onUserNotifyFailed?.())},()=>{l?.()!==!1&&r?.onNotifySucceeded?.()},t.requireDirectUserNotification===!0,l)}if(a&&l?.()!==!1){if(r?.onWakeStarted?.(),i==="on-wake-fallback"&&s.length>0&&!this.routes.resolve(e)?.sessionKey){if(l?.()===!1)return;r?.onNotifyStarted?.(),this.sendUserNotificationSequence(e,s,t.label,()=>{l?.()!==!1&&(r?.onNotifyFailed?.(),t.onUserNotifyFailed?.())},()=>{l?.()!==!1&&r?.onNotifySucceeded?.()},!1,l)}this.sendWake(e,a,`${t.label}-wake`,"wake",r?.onWakeFailed,r?.onWakeSucceeded,l,d,r?.onWakeSkipped,t.idempotencyKey)}}};var el=36,tl=2,nl="...";function xm(n){let e=Array.from(n);return e.length<=el?n:`${e.slice(0,el-nl.length).join("")}${nl}`}var dr=class{constructor(e,t){this.actionTokens=e;this.isGitHubCliAvailable=t}actionTokens;isGitHubCliAvailable;createActionToken(e,t,r={}){return this.actionTokens.createActionToken(e,t,{expiresAt:Date.now()+10080*60*1e3,...r})}consumeActionToken(e){return this.actionTokens.consumeActionToken(e)}consumeQuestionAnswerTokens(e,t,r){return this.actionTokens.consumeQuestionAnswerTokens(e,t,r)}consumePlanDecisionTokens(e,t){return this.actionTokens.consumePlanDecisionTokens(e,t)}getActionToken(e){return this.actionTokens.getActionToken(e)}listActiveActionTokens(e){return this.actionTokens.listActiveActionTokens(e)}clearRepoPolicyChoiceTokens(e){this.actionTokens.deleteActionTokensForSessionByKind(e,"repo-policy-set")}clearPlanDecisionTokens(e,t){this.actionTokens.deletePlanDecisionTokensForSession(e,t)}makeActionButton(e,t,r,o={}){let i=this.createActionToken(e,t,{label:r,...o});return{label:r,callbackData:i.id,style:this.resolveButtonStyle(t)}}resolveButtonStyle(e){switch(e){case"plan-approve":case"worktree-create-pr":case"worktree-update-pr":case"plan-offer-start":case"repo-policy-set":return"primary";case"worktree-merge":case"session-resume":case"session-restart":case"plugin-update-install":case"plugin-update-restart":return"success";case"plugin-update-remind-later":return"secondary";case"plan-reject":case"worktree-dismiss":case"plugin-update-dismiss":return"danger";default:return"secondary"}}getWorktreeDecisionButtons(e,t,r={merge:!0,pr:!0}){if(!t)return[];let o=[],i=[];if(r.merge&&i.push(this.makeActionButton(e,"worktree-merge","Merge")),r.pr&&this.isGitHubCliAvailable())t.worktreePrUrl?(i.push(this.makeActionButton(e,"worktree-view-pr","View PR",{targetUrl:t.worktreePrUrl})),i.length>0&&o.push(i),o.push([this.makeActionButton(e,"worktree-update-pr","Sync PR"),this.makeActionButton(e,"worktree-decide-later","Later")])):(i.push(this.makeActionButton(e,"worktree-create-pr","Open PR")),i.length>0&&o.push(i),o.push([this.makeActionButton(e,"worktree-decide-later","Later"),this.makeActionButton(e,"worktree-dismiss","Discard")]));else return o.push([...i,this.makeActionButton(e,"worktree-decide-later","Later")]),o.push([this.makeActionButton(e,"worktree-dismiss","Discard")]),o;return t.worktreePrUrl&&o.push([this.makeActionButton(e,"worktree-dismiss","Discard")]),o}getPlanApprovalButtons(e,t){let r=t?.actionablePlanDecisionVersion??t?.planDecisionVersion;this.clearPlanDecisionTokens(e,r);let o=[[this.makeActionButton(e,"plan-approve","Approve",{planDecisionVersion:r}),this.makeActionButton(e,"plan-request-changes","Revise",{planDecisionVersion:r}),this.makeActionButton(e,"plan-reject","Reject",{planDecisionVersion:r})]];return M("plan_approval_buttons_created",{sessionId:e,planDecisionVersion:r,...E(o)}),o}getResumeButtons(e,t){let r=[];return t.isExplicitlyResumable&&r.push(this.makeActionButton(e,"session-resume","Resume")),r.push(this.makeActionButton(e,"view-output","View output")),[r]}getQuestionButtons(e,t,r={}){if(t.length===0)return;let o=t.map((s,a)=>{let l=this.createActionToken(e,"question-answer",{label:s.label,optionIndex:a,pendingInputRequestId:r.requestId,pendingInputQuestionId:r.questionId});return{label:xm(s.label),callbackData:l.id,style:this.resolveButtonStyle("question-answer")}}),i=[];for(let s=0;s<o.length;s+=tl)i.push(o.slice(s,s+tl));return i}getPlanOfferButtons(e){let{offerId:t,route:r,planName:o,planPrompt:i,planWorkdir:s,planWorktreeStrategy:a}=e,l=[[this.makeActionButton(t,"plan-offer-start","Start Plan",{route:r,launchName:o,launchPrompt:i,launchWorkdir:s,launchWorktreeStrategy:a}),this.makeActionButton(t,"plan-offer-dismiss","Dismiss",{route:r})]];return M("plan_offer_buttons_created",{offerId:t,planName:o,channel:r.provider,target:r.target,accountId:r.accountId,threadId:r.threadId,sessionKey:r.sessionKey,planPromptLength:i.length,planWorkdirLength:s.length,planWorktreeStrategy:a,...E(l)}),l}getRepoPolicyChoiceButtons(e){let t=ct(e.prAvailable??!0),r=Array.from({length:Math.ceil(t.length/2)},(o,i)=>t.slice(i*2,i*2+2)).map(o=>o.map(i=>this.makeActionButton(e.choiceId,"repo-policy-set",i.label,{route:e.route,repoPolicy:i.policy,repoPolicyWorkdir:e.repoRoot,launchName:e.launchName,launchPrompt:e.launchPrompt,launchWorkdir:e.launchWorkdir,launchModel:e.launchModel,launchReasoningEffort:e.launchReasoningEffort,launchFastMode:e.launchFastMode,launchSystemPrompt:e.launchSystemPrompt,launchAllowedTools:e.launchAllowedTools,launchResumeSessionId:e.launchResumeSessionId,launchResumedFromSessionName:e.launchResumedFromSessionName,launchResumeWorktreeFrom:e.launchResumeWorktreeFrom,launchSessionIdOverride:e.launchSessionIdOverride,launchClearedPersistedCodexResume:e.launchClearedPersistedCodexResume,launchForkSession:e.launchForkSession,launchForceNewSession:e.launchForceNewSession,launchPermissionMode:e.launchPermissionMode,launchPlanApproval:e.launchPlanApproval,launchHarness:e.launchHarness,launchWorktreeStrategy:e.launchWorktreeStrategy,launchWorktreeBaseBranch:e.launchWorktreeBaseBranch,launchWorktreePrTargetRepo:e.launchWorktreePrTargetRepo,launchOriginAgentId:e.launchOriginAgentId})));return M("repo_policy_choice_buttons_created",{choiceId:e.choiceId,repoRoot:e.repoRoot,channel:e.route.provider,target:e.route.target,accountId:e.route.accountId,threadId:e.route.threadId,sessionKey:e.route.sessionKey,labels:t.map(o=>o.label),...E(r)}),r}};import{createHash as Dm}from"crypto";var Cm="duplicate notification already delivered or in flight",ur=class{maxRecords;inFlightTtlMs;now;inFlight=new Set;delivered=new Set;constructor(e={}){this.maxRecords=Math.max(1,Math.floor(e.maxRecords??64)),this.inFlightTtlMs=Math.max(1,Math.floor(e.inFlightTtlMs??6e5)),this.now=e.now??(()=>new Date)}claim(e,t,r){if(!e)return{allowed:!0};let o=this.now(),i=this.prune(t??[],o),s=i.find(a=>a.key===e);return this.delivered.has(e)||this.inFlight.has(e)||s?.status==="delivered"||s?.status==="in_flight"&&this.isFreshInFlight(s,o)?{allowed:!1,records:i,reason:Cm}:(this.inFlight.add(e),this.delivered.delete(e),{allowed:!0,records:this.upsert(i,{key:e,status:"in_flight",recordedAt:o.toISOString(),label:r})})}deliveredRecords(e,t,r){if(!e)return;let o=this.now();return this.inFlight.delete(e),this.delivered.add(e),this.upsert(this.prune(t??[],o),{key:e,status:"delivered",recordedAt:o.toISOString(),label:r})}releasedRecords(e,t){if(!e)return;let r=this.now();return this.inFlight.delete(e),this.prune(t??[],r).filter(o=>!(o.key===e&&o.status==="in_flight"))}prune(e,t){let r=e.filter(o=>o.status==="delivered"?!0:this.isFreshInFlight(o,t));return r.slice(Math.max(0,r.length-this.maxRecords))}isFreshInFlight(e,t){let r=Date.parse(e.recordedAt);return Number.isFinite(r)?t.getTime()-r<this.inFlightTtlMs:!1}upsert(e,t){let o=[...e.filter(i=>i.key!==t.key),t];return o.slice(Math.max(0,o.length-this.maxRecords))}};import{createHash as Mm}from"crypto";var _m=1024,bt="duplicate completion follow-up wake already handled",cr="prior human-visible summary already delivered",pr=class{inFlight=new Set;completed=new Map;claimKeysByPrimary=new Map;maxCompletedKeys;constructor(e={}){this.maxCompletedKeys=Math.max(1,Math.floor(e.maxCompletedKeys??_m))}decide(e,t,r){if(t?.required!==!0)return{required:!1,allowed:!1,explicit:!1};let o=this.buildKeySet(e,t);if(!o)return{required:!0,allowed:!0,explicit:!1};let i=this.normalizeRecords(r),s=this.buildPersistedKeyMap(i),a=o.decisionKeys.find(u=>this.inFlight.has(u)||!!this.getCompletedRecord(u)||s.has(u)),l=a?this.getCompletedRecord(a):void 0,d=a?s.get(a):void 0;return a?{required:!0,allowed:!1,explicit:o.explicit,dedupeKey:o.primary,skipReason:l?.skipReason??d?.skipReason??bt}:(this.registerClaim(o.primary,o.claimKeys),{required:!0,allowed:!0,key:o.primary,explicit:o.explicit})}recordVisibleDelivery(e,t,r,o){if(t.required!==!0)return{required:!1,allowed:!1,explicit:!1};let i=this.buildKeySet(e,t);if(!i)return{required:!0,allowed:!0,explicit:!1};let s=this.normalizeRecords(r),a=this.buildPersistedKeyMap(s),l=i.decisionKeys.find(g=>!!this.getCompletedRecord(g)||a.has(g)),d=l?this.getCompletedRecord(l):void 0,u=l?a.get(l):void 0;if(d||u)return{required:!0,allowed:!1,explicit:i.explicit,dedupeKey:i.primary,skipReason:d?.skipReason??u?.skipReason};this.finishKeys(i.primary,i.claimKeys,!0,cr);let m=this.deliveredRecords(i.claimKeys,s,o,cr);return{required:!0,allowed:!0,key:i.primary,explicit:i.explicit,records:m}}finish(e,t,r=bt){if(!e)return;let o=this.claimKeysByPrimary.get(e)??[e];this.finishKeys(e,o,t,r)}completionRecordsAfterDelivery(e,t,r,o=bt){if(!e)return;let i=this.claimKeysByPrimary.get(e)??[e];return this.deliveredRecords(i,t,r,o)}registerClaim(e,t){for(let r of t)this.inFlight.add(r);for(this.claimKeysByPrimary.delete(e),this.claimKeysByPrimary.set(e,t);this.claimKeysByPrimary.size>this.maxCompletedKeys;){let r=this.claimKeysByPrimary.keys().next().value;if(r===void 0)break;let o=this.claimKeysByPrimary.get(r)??[r];for(let i of o)this.inFlight.delete(i);this.claimKeysByPrimary.delete(r)}}finishKeys(e,t,r,o=bt){for(let i of t)this.inFlight.delete(i);if(this.claimKeysByPrimary.delete(e),!!r){for(let i of t)this.completed.get(i)?.skipReason===cr&&o!==cr||(this.completed.delete(i),this.completed.set(i,{skipReason:o,linkedKeys:t.filter(a=>a!==i)}));for(;this.completed.size>this.maxCompletedKeys;){let i=this.completed.keys().next().value;if(i===void 0)break;this.completed.delete(i)}}}buildKeySet(e,t){let r=this.getDeliveryRef(e);if(!r)return;let o=this.normalizeOutcomeKey(e,t.outcomeKey?.trim());if(o){let a=`${this.buildVisibleScope(r,e)}:outcome:${this.digest(o)}`,l=this.isGoalLike(e,t,o),d=this.buildSessionAliasKeys(r,e,"visible-summary"),u=this.buildSessionAliasKeys(r,e,"goal-summary"),m=this.isTerminalLike(t,o),g=o.startsWith("worktree-pr:")?this.normalizeWorktreePrOutcome(t.outcomeKey?.trim()):void 0,S=g?.aliases.map(y=>`${this.buildVisibleScope(r,e)}:outcome:${this.digest(y)}`)??[];return{primary:a,explicit:!0,claimKeys:[...d,...l?u:[],...S,a],decisionKeys:[a,...g?.blocksAliases?S:[],...l||m?d:[],...u]}}let i=t.fallbackFingerprint?.trim();if(!i)return;let s=`${r}:${t.producer}:${this.digest(i)}`;return{primary:s,explicit:!1,claimKeys:[s],decisionKeys:[s]}}isGoalLike(e,t,r){return!!e.goalTaskId?.trim()||t.producer==="goal"||/^goal:/i.test(r)}isTerminalLike(e,t){return e.producer==="terminal"||/^terminal:/i.test(t)}normalizeOutcomeKey(e,t){let r=e.goalTaskId?.trim();if(r)return`goal:${r}`;let o=this.normalizeWorktreePrOutcome(t);return o?o.primary:t}normalizeWorktreePrOutcome(e){if(!e)return;let t=e.split(":");if(t[0]!=="worktree-pr"||t.length<5)return;let r=t[1]?.trim().toLowerCase();if(!r||!/^(?:draft-)?(?:opened|updated)$/.test(r))return;let o=t[2]?.trim()||"default-repo",i=t[3]?.trim();if(!i)return;let s=this.normalizePrIdentity(i),a=t[4]?.trim();if(!s||!a)return;let l=["worktree-pr",o.toLowerCase(),s,a].join(":"),d=`${l}:created`;if(r.endsWith("updated")){let u=t.slice(5).join(":").trim();return{primary:u?`${l}:updated:${u}`:`${l}:updated`,aliases:[d,l],blocksAliases:!1}}return{primary:d,aliases:[l],blocksAliases:!0}}normalizePrIdentity(e){let t=e.match(/#?(\d+)$/);if(t?.[1])return`#${t[1]}`;let r=e.match(/\/pull\/(\d+)(?:\b|\/|$)/);if(r?.[1])return`#${r[1]}`;let o=e.trim();return o||void 0}buildVisibleScope(e,t){let r=le(t);return r?`route:${this.digest(JSON.stringify({provider:r.provider,accountId:r.accountId,target:r.target,threadId:r.threadId}))}`:e}buildSessionAliasKeys(e,t,r){let o=this.buildVisibleScope(e,t),i="startedAt"in t?t.startedAt:t.createdAt,s=Number.isFinite(i)?`${o}:cycle:${this.digest(String(i))}`:o,a=["id"in t?t.id:void 0,"sessionId"in t?t.sessionId:void 0,t.name,C(t),t.harnessSessionId,e].map(l=>l?.trim()).filter(l=>!!l);return[...new Set(a)].map(l=>`${s}:session:${this.digest(l)}:${r}`)}getDeliveryRef(e){return D(e)??C(e)??""}digest(e){return Mm("sha256").update(e).digest("hex").slice(0,16)}normalizeRecords(e){return e?.length?e.filter(t=>t.key.trim()&&Number.isFinite(Date.parse(t.recordedAt))).slice(-this.maxCompletedKeys):[]}buildPersistedKeyMap(e){let t=new Map;for(let r of e){t.set(r.key,r);for(let o of r.linkedKeys??[])o.trim()&&t.set(o,r)}return t}getCompletedRecord(e){let t=this.completed.get(e);if(t)return t;for(let r of this.completed.values())if(r.linkedKeys?.includes(e))return r}deliveredRecords(e,t,r,o=bt){let i=new Date().toISOString(),s=this.normalizeRecords(t),a=new Set(e),d=[...s.filter(u=>!a.has(u.key)&&!(u.linkedKeys??[]).some(m=>a.has(m))),...e.map(u=>{let m=e.filter(g=>g!==u);return{key:u,linkedKeys:m.length>0?m:void 0,recordedAt:i,label:r,skipReason:o}})];return d.slice(Math.max(0,d.length-this.maxCompletedKeys))}};var $m=2e3,Em=new Set(["1","true","yes","on"]);function Nm(){let n=process.env.OPENCLAW_CODE_AGENT_NOTIFICATION_DIAGNOSTICS?.trim().toLowerCase();return!!(n&&Em.has(n))||Nr()}function Om(n){Nm()&&process.stderr.write(`[SessionNotification] ${JSON.stringify(n)}
|
|
203
|
+
`)}function Wm(n){let e=n?.trim().split(":")??[];if(e[0]!=="worktree-pr")return;let t=e[1]?.toLowerCase();if(t==="updated"||t==="draft-updated")return"pr-updated";if(t==="opened"||t==="draft-opened")return"pr-opened"}var mr=class{constructor(e,t,r={}){this.wakeDispatcher=e;this.applyPersistedPatch=t;this.completionSummaries=new pr({maxCompletedKeys:r.maxCompletedCompletionWakeKeys}),this.notificationDedupe=new ur({maxRecords:r.maxNotificationDedupeRecords}),this.getPersistedSession=r.getPersistedSession}wakeDispatcher;applyPersistedPatch;completionSummaries;notificationDedupe;getPersistedSession;dispatch(e,t){let r=this.getDeliveryRef(e),o=this.getPersistedSession?.(r),i=this.buildNotificationDedupeKey(e,t),s=this.notificationDedupe.claim(i,o?.notificationDedupe,t.label);if(s.records&&this.applyNotificationDedupePatch(r,s.records),!s.allowed){this.logNotificationDecision({session:e,request:t,deliveryRef:r,dedupeKey:i,decision:"skip",reason:s.reason??"duplicate notification skipped",deliveryPath:"notify"}),t.hooks?.onDuplicateSkipped?.(s.reason??"duplicate notification skipped");return}let a=this.buildCompletionSummaryFact(t),l=t.completionSummaryOwner==="foreground"&&a?.required===!0,d=l?this.completionSummaries.recordVisibleDelivery(e,a,o?.completionSummaryDedupe,t.label):this.completionSummaries.decide(e,a,o?.completionSummaryDedupe);d.records&&this.applyCompletionSummaryDedupePatch(r,d.records);let u=l?this.withoutCompletionWake(t):t;if(d.required&&!d.allowed)if(this.logNotificationDecision({session:e,request:t,deliveryRef:r,dedupeKey:d.dedupeKey??d.key,outcomeKey:a?.outcomeKey,decision:l?"skip":d.explicit?"send_without_followup":"skip",reason:d.skipReason??"completion follow-up wake suppressed",deliveryPath:l?"foreground-summary":"wake",followupKind:a?.producer}),t.hooks?.onWakeSkipped?.(d.skipReason??"completion follow-up wake suppressed"),l){this.releaseNotificationDedupe(r,i);return}else if(d.explicit)u=this.withoutCompletionWake(t);else{this.releaseNotificationDedupe(r,i);return}a?.required===!0&&this.logNotificationDecision({session:e,request:u,deliveryRef:r,dedupeKey:d.dedupeKey??d.key,outcomeKey:a.outcomeKey,decision:l?"record_visible":u.completionWakeSummaryRequired===!0?"send_with_followup":"send_without_followup",reason:l?"foreground summary owns completion follow-up":void 0,deliveryPath:l?"foreground-summary":"wake",followupKind:a.producer});let m=this.buildCompletionWakePatch(u),g=!!(u.wakeMessage?.trim()||u.wakeMessageOnNotifySuccess?.trim()),S=!!(u.wakeMessage?.trim()||u.wakeMessageOnNotifyFailed?.trim()),y=u.failureWakeConfirmsNotificationDelivery!==!1,c=!1,h=!1,P=!1,b=()=>{P||(P=!0,c||(this.releaseNotificationDedupe(r,i),c=!0),this.completionSummaries.finish(d.key,!1))},A=u.shouldDispatch?()=>{let v=u.shouldDispatch?.()!==!1;return v||b(),v}:void 0,I={onNotifyStarted:()=>{this.applyDeliveryState(r,"notifying"),u.hooks?.onNotifyStarted?.()},onNotifySucceeded:()=>{c=!0,this.markNotificationDedupeDelivered(r,i,u.label),this.applyNotifyDeliveryState(r,g?"wake_pending":"idle",g?m:void 0),g||this.completionSummaries.finish(d.key,!1),u.hooks?.onNotifySucceeded?.()},onNotifyFailed:()=>{h=!0,this.applyNotifyDeliveryState(r,S?"wake_pending":"failed",S?m:void 0),S||(this.completionSummaries.finish(d.key,!1),this.releaseNotificationDedupe(r,i),c=!0),u.hooks?.onNotifyFailed?.()},onWakeStarted:()=>{this.applyPersistedPatchWithCompletionWake(r,"wake_pending",m,{completionWakeIssuedAt:new Date().toISOString(),completionWakeSucceededAt:void 0,completionWakeFailedAt:void 0,completionWakeSkippedAt:void 0,completionWakeSkipReason:void 0}),u.hooks?.onWakeStarted?.()},onWakeSucceeded:()=>{c=!0,h&&!y?this.releaseNotificationDedupe(r,i):this.markNotificationDedupeDelivered(r,i,u.label),this.markCompletionSummaryDelivered(r,d.key,u.label),this.applyPersistedPatchWithCompletionWake(r,"idle",this.buildCompletionWakeSucceededPatch(m),{completionWakeSucceededAt:new Date().toISOString(),completionWakeFailedAt:void 0,completionWakeSkippedAt:void 0,completionWakeSkipReason:void 0}),this.completionSummaries.finish(d.key,!0),u.hooks?.onWakeSucceeded?.()},onWakeSkipped:v=>{if(P){u.hooks?.onWakeSkipped?.(v);return}c=!0,h&&!y?this.releaseNotificationDedupe(r,i):this.markNotificationDedupeDelivered(r,i,u.label),this.markCompletionSummaryDelivered(r,d.key,u.label,v),this.applyPersistedPatchWithCompletionWake(r,"idle",this.buildCompletionWakeSucceededPatch(m),{completionWakeSucceededAt:void 0,completionWakeFailedAt:void 0,completionWakeSkippedAt:new Date().toISOString(),completionWakeSkipReason:v}),this.completionSummaries.finish(d.key,!0),u.hooks?.onWakeSkipped?.(v)},onWakeFailed:()=>{this.applyPersistedPatchWithCompletionWake(r,"failed",m,{completionWakeFailedAt:new Date().toISOString()}),this.completionSummaries.finish(d.key,!1),this.releaseNotificationDedupe(r,i),c=!0,u.hooks?.onWakeFailed?.()}},R="model"in e?e:o,f=at({harness:R&&("harnessName"in R?R.harnessName:R.harness),model:R?.model,reasoningEffort:R?.reasoningEffort});this.wakeDispatcher.dispatchSessionNotification(e,{...u,userMessage:u.userMessage===void 0?void 0:Ur(u.userMessage,f),userMessages:u.userMessages?.map((v,T)=>({...v,text:T===0||v.text.startsWith("📋 [")?Ur(v.text,f):v.text})),idempotencyKey:i??u.idempotencyKey,shouldDispatch:A,hooks:I})}notifyWorktreeOutcome(e,t,r={}){let o=this.getDeliveryRef(e),i=Wm(r.completionWakeOutcomeKey);o&&i&&this.applyPersistedPatch(o,{worktreeRemoteOutcome:i});let s=Fm(t,e),a=m=>ze({sessionId:o,sessionName:e.name,outcomeLine:s,originThreadLine:Ze(e),detailLines:r.detailLines,canonicalStatusDelivered:m}),l=r.summaryWakeRequired??!0,d={required:l,producer:"worktree-pr",outcomeKey:r.completionWakeOutcomeKey??`terminal:${o}`},u=r.completionSummaryOwner!=="foreground";if(l&&!u){let m=this.getDeliveryRef(e),g=this.completionSummaries.recordVisibleDelivery(e,d,m?this.getPersistedSession?.(m)?.completionSummaryDedupe:void 0,"worktree-outcome");g.records&&this.applyCompletionSummaryDedupePatch(m,g.records)}this.dispatch(e,{label:"worktree-outcome",userMessage:s,notifyUser:"always",requireDirectUserNotification:!0,completionSummary:{...d,required:l&&u},completionWakeSummaryRequired:l&&u,completionWakeOutcomeKey:r.completionWakeOutcomeKey??`terminal:${o}`,idempotencyKey:`worktree-outcome:${r.completionWakeOutcomeKey??t}`,deferConditionalWakeUntilNextTick:!0,deferConditionalWakeMs:$m,wakeMessageOnNotifySuccess:l&&u?a(!0):void 0,wakeMessageOnNotifyFailed:l&&u?a(!1):void 0})}dispose(){this.wakeDispatcher.dispose()}getDeliveryRef(e){return D(e)??C(e)??""}applyDeliveryState(e,t){!e||!t||this.applyPersistedPatch(e,{deliveryState:t})}applyNotifyDeliveryState(e,t,r){!e||!t||this.applyPersistedPatch(e,r?{deliveryState:t,...r}:{deliveryState:t})}applyPersistedPatchWithCompletionWake(e,t,r,o){!e||!t||this.applyPersistedPatch(e,r?{deliveryState:t,...r,...o}:{deliveryState:t})}buildCompletionWakePatch(e){if(e.completionWakeSummaryRequired===!0)return{completionWakeSummaryRequired:!0}}buildCompletionWakeSucceededPatch(e){if(e)return{completionWakeSummaryRequired:void 0}}withoutCompletionWake(e){return{...e,completionSummary:e.completionSummary?{...e.completionSummary,required:!1}:void 0,wakeMessage:void 0,wakeMessageOnNotifySuccess:void 0,wakeMessageOnNotifyFailed:void 0,completionWakeSummaryRequired:!1}}buildCompletionSummaryFact(e){if(e.completionSummary)return e.completionSummary;if(e.completionWakeSummaryRequired!==!0)return;let t=[e.wakeMessage,e.wakeMessageOnNotifySuccess,e.wakeMessageOnNotifyFailed].map(r=>r?.trim()).filter(r=>!!r);return{required:!0,producer:"legacy",outcomeKey:e.completionWakeOutcomeKey,fallbackFingerprint:t.length>0?`${e.label}
|
|
199
204
|
${t.join(`
|
|
200
205
|
---completion-wake---
|
|
201
|
-
`)}`:void 0}}buildNotificationDedupeKey(e,t){let r=t.idempotencyKey?.trim();if(!r)return;let o=this.getDeliveryRef(e);if(!o)return;let i=le(e),s=i?{provider:i.provider,accountId:i.accountId,target:i.target,threadId:i.threadId}:{deliveryRef:o};return`notification:${this.digest(JSON.stringify({scope:s,semanticKey:r}))}`}markNotificationDedupeDelivered(e,t,r){let o=this.notificationDedupe.deliveredRecords(t,e?this.getPersistedSession?.(e)?.notificationDedupe:void 0,r);o&&this.applyNotificationDedupePatch(e,o)}releaseNotificationDedupe(e,t){let r=this.notificationDedupe.releasedRecords(t,e?this.getPersistedSession?.(e)?.notificationDedupe:void 0);r&&this.applyNotificationDedupePatch(e,r)}applyNotificationDedupePatch(e,t){this.getPersistedSession&&e&&this.applyPersistedPatch(e,{notificationDedupe:t})}markCompletionSummaryDelivered(e,t,r,o){let i=this.completionSummaries.completionRecordsAfterDelivery(t,e?this.getPersistedSession?.(e)?.completionSummaryDedupe:void 0,r,o);i&&this.applyCompletionSummaryDedupePatch(e,i)}applyCompletionSummaryDedupePatch(e,t){this.getPersistedSession&&e&&this.applyPersistedPatch(e,{completionSummaryDedupe:t})}logNotificationDecision(e){let t=le(e.session)
|
|
202
|
-
`);return new Promise((S,y)=>{let c=this.pendingQuestions.get(e.id);c&&(clearTimeout(c.timeoutHandle),c.reject(new Error(`AskUserQuestion superseded by a newer question for session "${e.name}".`)));let h=setTimeout(()=>{this.pendingQuestions.delete(e.id),y(new Error(`AskUserQuestion timed out after ${6e5/1e3}s for session "${e.name}"`))},6e5);h.unref?.(),this.pendingQuestions.set(e.id,{resolve:S,reject:y,questions:i,timeoutHandle:h,requestId:u,questionId:d}),this.dispatchSessionNotification(e,{label:"ask-user-question",idempotencyKey:`ask-user-question:${e.id}:${u}`,userMessage:l,notifyUser:"always",buttons:
|
|
203
|
-
`),wakeMessageOnNotifyFailed:g})})}resolveAskUserQuestion(e,t,r={}){let o=this.pendingQuestions.get(e);if(!o)return`${e}`,!1;if(r.requestId&&r.requestId!==o.requestId)return`${e}${o.requestId}${r.requestId}`,!1;if(r.questionId&&r.questionId!==o.questionId)return`${e}${o.questionId??""}${r.questionId}`,!1;let i=o.questions[0],s=i.options??[],a=s[t];return a?(clearTimeout(o.timeoutHandle),this.pendingQuestions.delete(e),this.clearWaitingTimestamp(e),o.resolve({behavior:"allow",updatedInput:{questions:o.questions,answers:{[i.question]:a.label}}}),!0):(`${t}${e}${s.length}`,!1)}dispose(){for(let e of this.pendingQuestions.values())clearTimeout(e.timeoutHandle),e.reject(new Error("SessionManager disposed before AskUserQuestion resolved."));this.pendingQuestions.clear()}};var
|
|
204
|
-
`);this.dispatchNotification(r,{label:`worktree-stale-reminder-${e.name}`,userMessage:o,notifyUser:"always",buttons:this.getWorktreeDecisionButtons(D(e)??e.harnessSessionId,e)})}clearResolvedReminderState(e){if(!e.pendingWorktreeDecisionSince&&!e.lastWorktreeReminderAt&&!e.worktreeDecisionSnoozedUntil||!this.isResolvedWorktreeDecision(e))return!1;let t=!1;for(let r of z(e))t=this.updatePersistedSession(r,{pendingWorktreeDecisionSince:void 0,lastWorktreeReminderAt:void 0,worktreeDecisionSnoozedUntil:void 0})||t;return t}getWorktreeDecisionReminderStatus(e,t){if(this.hasResolvedWorktreeDecisionMarker(e))return"resolved";if(!e.pendingWorktreeDecisionSince)return"inactive";let r=t??new Date(e.pendingWorktreeDecisionSince).getTime();if(!Number.isFinite(r))return"inactive";let o=e.worktreeState==="pending_decision"||e.lifecycle==="awaiting_worktree_decision"||e.worktreeLifecycle?.state==="pending_decision",i=e.worktreeState==null&&e.lifecycle==null&&e.worktreeLifecycle?.state==null&&!e.worktreeMerged&&!e.worktreePrUrl;if(!o&&!i)return"inactive";let s=
|
|
205
|
-
`)}function
|
|
206
|
-
`)}import{existsSync as
|
|
207
|
-
`)}function
|
|
208
|
-
`):e}function
|
|
209
|
-
`)}function
|
|
206
|
+
`)}`:void 0}}buildNotificationDedupeKey(e,t){let r=t.idempotencyKey?.trim();if(!r)return;let o=this.getDeliveryRef(e);if(!o)return;let i=le(e),s=i?{provider:i.provider,accountId:i.accountId,target:i.target,threadId:i.threadId}:{deliveryRef:o};return`notification:${this.digest(JSON.stringify({scope:s,semanticKey:r}))}`}markNotificationDedupeDelivered(e,t,r){let o=this.notificationDedupe.deliveredRecords(t,e?this.getPersistedSession?.(e)?.notificationDedupe:void 0,r);o&&this.applyNotificationDedupePatch(e,o)}releaseNotificationDedupe(e,t){let r=this.notificationDedupe.releasedRecords(t,e?this.getPersistedSession?.(e)?.notificationDedupe:void 0);r&&this.applyNotificationDedupePatch(e,r)}applyNotificationDedupePatch(e,t){this.getPersistedSession&&e&&this.applyPersistedPatch(e,{notificationDedupe:t})}markCompletionSummaryDelivered(e,t,r,o){let i=this.completionSummaries.completionRecordsAfterDelivery(t,e?this.getPersistedSession?.(e)?.completionSummaryDedupe:void 0,r,o);i&&this.applyCompletionSummaryDedupePatch(e,i)}applyCompletionSummaryDedupePatch(e,t){this.getPersistedSession&&e&&this.applyPersistedPatch(e,{completionSummaryDedupe:t})}logNotificationDecision(e){let t=le(e.session);Om({event:"oca_notification_decision",sessionId:"id"in e.session?e.session.id:e.session.sessionId,sessionName:e.session.name,harnessSessionId:e.session.harnessSessionId,originRoute:t?{provider:t.provider,accountId:t.accountId,target:t.target,threadId:t.threadId,sessionKey:t.sessionKey}:void 0,outcomeType:e.outcomeKey?.split(":").slice(0,2).join(":")??e.request.label,notificationKind:e.request.label,followupKind:e.followupKind,dedupeKey:e.dedupeKey,deliveryPath:e.deliveryPath,decision:e.decision,reason:e.reason,completionWakeSummaryRequired:e.request.completionWakeSummaryRequired,deliveryRef:e.deliveryRef})}digest(e){return Dm("sha256").update(e).digest("hex").slice(0,16)}};function Fm(n,e){let t=ye(e);return t?`${n}${t}`:n}import{existsSync as Lm}from"fs";var fr=class{getCompletionState(e,t,r,o){if(oe(t)!==r)return"has-commits";let i=tt(e,r,o);if(i===void 0)return"has-commits";if(i===0){let s=tt(e,o,r);return s===void 0?"has-commits":s>0?Ot(e,r,o)?"merged":"base-advanced":rt(t)?"dirty-uncommitted":"no-change"}return rt(t)?"dirty-uncommitted":ai(e,r,o)?"released":"has-commits"}isResolvedWorktreeEligibleForCleanup(e,t,r){if(!e.worktreePath||!e.workdir||!Lm(e.worktreePath)||e.pendingWorktreeDecisionSince||e.worktreeState==="pending_decision"||e.pendingPlanApproval||e.resumable||e.worktreeBranch&&oe(e.worktreePath)!==e.worktreeBranch)return!1;let o=e.worktreeMergedAt??e.worktreeDismissedAt??e.completedAt??e.createdAt,i=typeof o=="string"?new Date(o).getTime():Number(o??0);return!!i&&t-i>=r}};import{randomUUID as Bm}from"node:crypto";function Km(n){let e=n.pendingInputState,t=e?.activeQuestionIndex??0;return e?.questions?.[t]?.id??(e?.activeQuestionIndex!=null?`q${e.activeQuestionIndex}`:void 0)}var gr=class{constructor(e,t,r,o){this.pendingQuestions=e;this.dispatchSessionNotification=t;this.clearWaitingTimestamp=r;this.getQuestionButtons=o}pendingQuestions;dispatchSessionNotification;clearWaitingTimestamp;getQuestionButtons;async handleAskUserQuestion(e,t){let i=t?.questions??[];if(i.length===0)throw new Error("AskUserQuestion: no questions in input");let s=i[0],a=s.options??[],l=`❓ [${e.name}] ${s.question}`,d=Km(e),u=e.pendingInputState?.requestId??`legacy:${Bm()}`,m=this.getQuestionButtons(e.id,a,{requestId:u,questionId:d}),g=[`[ASK USER QUESTION] Session "${e.name}" has a question requiring user input.`,"",`Question: ${s.question}`,...a.length>0?["Options:",...a.map((S,y)=>` ${y+1}. ${S.label}`)]:[],"",`Send the question to the user and call agent_respond(session="${e.id}", message="<answer>") with their answer.`].join(`
|
|
207
|
+
`);return new Promise((S,y)=>{let c=this.pendingQuestions.get(e.id);c&&(clearTimeout(c.timeoutHandle),c.reject(new Error(`AskUserQuestion superseded by a newer question for session "${e.name}".`)));let h=setTimeout(()=>{this.pendingQuestions.delete(e.id),y(new Error(`AskUserQuestion timed out after ${6e5/1e3}s for session "${e.name}"`))},6e5);h.unref?.(),this.pendingQuestions.set(e.id,{resolve:S,reject:y,questions:i,timeoutHandle:h,requestId:u,questionId:d}),this.dispatchSessionNotification(e,{label:"ask-user-question",idempotencyKey:`ask-user-question:${e.id}:${u}`,userMessage:l,notifyUser:"always",buttons:m,wakeMessageOnNotifySuccess:["AskUserQuestion delivered to the user.",`Session: ${e.name} | ID: ${e.id}`,`Question: ${s.question}`,"Await their selection — do NOT answer this question yourself."].join(`
|
|
208
|
+
`),wakeMessageOnNotifyFailed:g})})}resolveAskUserQuestion(e,t,r={}){let o=this.pendingQuestions.get(e);if(!o)return`${e}`,!1;if(r.requestId&&r.requestId!==o.requestId)return`${e}${o.requestId}${r.requestId}`,!1;if(r.questionId&&r.questionId!==o.questionId)return`${e}${o.questionId??""}${r.questionId}`,!1;let i=o.questions[0],s=i.options??[],a=s[t];return a?(clearTimeout(o.timeoutHandle),this.pendingQuestions.delete(e),this.clearWaitingTimestamp(e),o.resolve({behavior:"allow",updatedInput:{questions:o.questions,answers:{[i.question]:a.label}}}),!0):(`${t}${e}${s.length}`,!1)}dispose(){for(let e of this.pendingQuestions.values())clearTimeout(e.timeoutHandle),e.reject(new Error("SessionManager disposed before AskUserQuestion resolved."));this.pendingQuestions.clear()}};var qm=new Set(["merged","released","dismissed","none","cleanup_failed"]),Vm=new Set(["merged","released","dismissed","no_change","none","cleanup_failed"]),hr=class n{constructor(e,t,r,o){this.buildRoutingProxy=e;this.dispatchNotification=t;this.updatePersistedSession=r;this.getWorktreeDecisionButtons=o}buildRoutingProxy;dispatchNotification;updatePersistedSession;getWorktreeDecisionButtons;static REMINDER_THRESHOLD_MS=10800*1e3;static REMINDER_INTERVAL_MS=10800*1e3;getNextReminderAt(e){let t=new Date(e.pendingWorktreeDecisionSince).getTime();if(this.getWorktreeDecisionReminderStatus(e,t)!=="pending")return;let r=[t+n.REMINDER_THRESHOLD_MS];if(e.worktreeDecisionSnoozedUntil){let o=new Date(e.worktreeDecisionSnoozedUntil).getTime();Number.isFinite(o)&&r.push(o)}if(e.lastWorktreeReminderAt){let o=new Date(e.lastWorktreeReminderAt).getTime();Number.isFinite(o)&&r.push(o+n.REMINDER_INTERVAL_MS)}return Math.max(...r)}sendReminderIfDue(e,t=Date.now()){this.clearResolvedReminderState(e);let r=this.getNextReminderAt(e);if(r==null||r>t)return!1;let o=t-new Date(e.pendingWorktreeDecisionSince).getTime(),i=Math.floor(Math.max(0,o)/(3600*1e3));try{this.sendReminderNotification(e,i)}catch(s){return`${e.name}${s instanceof Error?s.message:String(s)}`,!1}for(let s of z(e))this.updatePersistedSession(s,{lastWorktreeReminderAt:new Date(t).toISOString()});return!0}sendReminderNotification(e,t){let r=this.buildRoutingProxy({id:e.sessionId??e.name??C(e)??e.harnessSessionId,sessionId:e.sessionId,harnessSessionId:e.harnessSessionId,backendRef:e.backendRef,route:e.route});if(e.worktreeStrategy==="delegate"){this.dispatchNotification(r,{label:`worktree-stale-reminder-${e.name}`,wakeMessage:xo(e,t),notifyUser:"never"});return}let o=[`⏰ Reminder: branch \`${e.worktreeBranch??"unknown"}\` is still waiting for a merge decision.`,`Session: ${e.name} | Pending: ${t}h`,"",`agent_merge(session="${e.name}") or agent_pr(session="${e.name}") or agent_worktree_cleanup() to resolve.`].join(`
|
|
209
|
+
`);this.dispatchNotification(r,{label:`worktree-stale-reminder-${e.name}`,userMessage:o,notifyUser:"always",buttons:this.getWorktreeDecisionButtons(D(e)??e.harnessSessionId,e)})}clearResolvedReminderState(e){if(!e.pendingWorktreeDecisionSince&&!e.lastWorktreeReminderAt&&!e.worktreeDecisionSnoozedUntil||!this.isResolvedWorktreeDecision(e))return!1;let t=!1;for(let r of z(e))t=this.updatePersistedSession(r,{pendingWorktreeDecisionSince:void 0,lastWorktreeReminderAt:void 0,worktreeDecisionSnoozedUntil:void 0})||t;return t}getWorktreeDecisionReminderStatus(e,t){if(this.hasResolvedWorktreeDecisionMarker(e))return"resolved";if(!e.pendingWorktreeDecisionSince)return"inactive";let r=t??new Date(e.pendingWorktreeDecisionSince).getTime();if(!Number.isFinite(r))return"inactive";let o=e.worktreeState==="pending_decision"||e.lifecycle==="awaiting_worktree_decision"||e.worktreeLifecycle?.state==="pending_decision",i=e.worktreeState==null&&e.lifecycle==null&&e.worktreeLifecycle?.state==null&&!e.worktreeMerged&&!e.worktreePrUrl;if(!o&&!i)return"inactive";let s=We(e,{activeSession:!1,includePrSync:!1});return this.isResolvedDerivedWorktreeState(s.derivedState)?"resolved":"pending"}isResolvedWorktreeDecision(e){if(this.hasResolvedWorktreeDecisionMarker(e))return!0;let t=We(e,{activeSession:!1,includePrSync:!1});return this.isResolvedDerivedWorktreeState(t.derivedState)}hasResolvedWorktreeDecisionMarker(e){return!!(e.worktreeMerged||e.worktreePrUrl||e.lifecycle==="terminal"||e.worktreeState&&qm.has(e.worktreeState)||e.worktreeLifecycle?.state&&Vm.has(e.worktreeLifecycle.state))}isResolvedDerivedWorktreeState(e){return e==="merged"||e==="released"||e==="no_change"||e==="cleanup_failed"}};function Rt(n,e){return e!=null&&n.approvalPromptRequiredVersion===e&&Um(n.approvalPromptStatus)}function Um(n){return n==="delivered"||n==="fallback_delivered"}function At(n,e){return!n||e==null?!1:!!(n.pendingPlanApproval&&n.approvalState==="pending"&&n.lifecycle==="awaiting_plan_decision"&&(n.actionablePlanDecisionVersion??n.planDecisionVersion)===e)}function Je(n,e,t=!1){let r="id"in n?n.id:n.sessionId??"unknown-session";return[t?"Plan review fallback text delivered to the user because interactive buttons could not be delivered.":"Plan approval buttons delivered to the user.",`Session: ${n.name??"unknown"} | ID: ${r} | Plan v${e??"?"}`,`Wait for their ${t?"explicit reply":"button callback"} — do NOT approve or reject this plan yourself.`].join(`
|
|
210
|
+
`)}function yr(n){let e="id"in n.session?n.session.id:n.session.sessionId??"unknown-session",t=n.originThreadLine??(n.session.originThreadId!=null?`Origin thread: ${n.session.originThreadId}`:"");return["[PLAN APPROVAL DELIVERY FAILED] The plugin could not deliver the canonical plan review buttons or the explicit fallback text to the user.",`Name: ${n.session.name??"unknown"} | ID: ${e} | Plan v${n.planDecisionVersion??"?"}`,t,"","No user-visible actionable review prompt is confirmed for this plan version.","Intervene manually before assuming the user saw the plan review request."].filter(Boolean).join(`
|
|
211
|
+
`)}import{existsSync as ef,readFileSync as tf}from"fs";var Gm=4e3,rl=180,jm=300;function ol(){let n=xe(),e=Qm(n);if(e)return{async generateQuestionContextSummary(t){if(typeof e.generateQuestionContextSummary=="function")return await e.generateQuestionContextSummary(t);if(typeof e.summarizeQuestionContext=="function")return await e.summarizeQuestionContext(t);let r=Ym(t);if(typeof e.generateObject=="function")return await e.generateObject({task:"openclaw-code-agent.question-context-summary",prompt:r,input:t});if(typeof e.generateText=="function")return await e.generateText({task:"openclaw-code-agent.question-context-summary",prompt:r});if(typeof e.complete=="function")return await e.complete({prompt:r})}}}async function il(n){let e=Sr(n.question??""),t=Sr(Jm(e,n.context??""));if(!n.provider||!e||!t)return;let r={sessionName:n.sessionName,question:e,context:Xm(t,Gm)};try{let o=await Zm(n.provider.generateQuestionContextSummary(r),n.timeoutMs??jm);return zm(o)}catch(o){let i=o instanceof Error?o.message:String(o);i!=="question-context-summary timed out"&&`${i}`;return}}function zm(n){let e=Hm(n),t=typeof e=="string"?e:e&&typeof e=="object"&&typeof e.summary=="string"?e.summary:void 0,r=Sr(t??"").replace(/^[-*•]\s+/,"").trim();if(!(!r||r.length>rl)&&!(r.split(/[.!?]+/).filter(o=>o.trim()).length>1))return r}function Hm(n){if(typeof n!="string")return n;let e=n.trim();if(e)try{return JSON.parse(e)}catch{return e}}function Qm(n){return[n?.questionContextSummary,n?.llm,n?.ai,n?.model,n?.models].find(t=>!!(t&&typeof t=="object"&&(typeof t.generateQuestionContextSummary=="function"||typeof t.summarizeQuestionContext=="function"||typeof t.generateObject=="function"||typeof t.generateText=="function"||typeof t.complete=="function")))}function Ym(n){return["Summarize why an OpenClaw Code Agent question is being asked.",'Return only JSON with shape {"summary":"..."}.',`Write one concise user-facing sentence under ${rl} characters.`,"Do not answer the question. Do not invent choices. Do not alter option labels or semantics.","","Evidence:",JSON.stringify(n,null,2)].join(`
|
|
212
|
+
`)}function Jm(n,e){return n?e.split(/\r?\n/).filter(t=>Sr(t)!==n).join(`
|
|
213
|
+
`):e}function Sr(n){return n.replace(/\s+/g," ").trim()}function Xm(n,e){return n.length<=e?n:`${n.slice(0,Math.max(0,e-3)).trimEnd()}...`}function Zm(n,e){return new Promise((t,r)=>{let o=setTimeout(()=>r(new Error("question-context-summary timed out")),e);o.unref?.(),n.then(i=>{clearTimeout(o),t(i)},i=>{clearTimeout(o),r(i)})})}var nf=280;function jo(n,e){if(n.latestPlanArtifactVersion===e&&n.latestPlanArtifact)return n.latestPlanArtifact;let t=n.planFilePath?.trim();if(!(!t||!ef(t)))try{let r=tf(t,"utf-8").trim();return r?{markdown:r,steps:[]}:void 0}catch{return}}function rf(n){let e=[n.total>1?`Question ${n.index+1}`:void 0,n.question.header].filter(Boolean).join(" - "),t=[...e?[e]:[],n.question.question];return n.optionDescriptions.length>0&&t.push("","Options:",...n.optionDescriptions.map(r=>`${r.label} - ${r.description}`)),t.join(`
|
|
214
|
+
`)}function of(n){return n.options.map(e=>({label:e.label,description:e.description?.trim()??""})).filter(e=>e.description&&e.description.length<=nf)}function al(n){let e=n?.activeQuestionIndex??0;return n?.questions?.[e]?.id??(n?.activeQuestionIndex!=null?`q${n.activeQuestionIndex}`:void 0)}function sf(n,e,t){let r=n.pendingInputState;return!!(r&&r.requestId===e&&al(r)===t)}function af(n){return[n.startedAt??"unknown-started-at",n.result?.session_id??"unknown-backend-session",n.result?.num_turns??0].join(":")}function sl(n){return[n.status,n.startedAt??"unknown-started-at",n.result?.session_id??"unknown-backend-session",n.result?.num_turns??0,n.killReason??"unknown"].join(":")}var kr=class{constructor(e){this.deps=e}deps;dispatchPlanApprovalFallback(e,t,r){let o=new Date().toISOString();this.deps.dispatchSessionNotification(e,{label:"plan-approval-fallback",idempotencyKey:`plan-approval:${e.id}:v${t??"unknown"}:fallback`,userMessages:vt({session:e,summary:r}),notifyUser:"always",shouldDispatch:()=>At(e,t),hooks:{onNotifyStarted:()=>{this.deps.updatePersistedSession(e.id,{approvalPromptRequiredVersion:t,approvalPromptVersion:t,approvalPromptStatus:"sending",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"explicit_fallback_text",approvalPromptLastAttemptAt:o})},onNotifySucceeded:()=>{this.deps.updatePersistedSession(e.id,{approvalPromptRequiredVersion:t,approvalPromptVersion:t,approvalPromptStatus:"fallback_delivered",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"explicit_fallback_text",approvalPromptLastAttemptAt:o,approvalPromptDeliveredAt:new Date().toISOString(),approvalPromptFailedAt:void 0})},onNotifyFailed:()=>{this.deps.updatePersistedSession(e.id,{approvalPromptRequiredVersion:t,approvalPromptVersion:t,approvalPromptStatus:"failed",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"explicit_fallback_text",approvalPromptLastAttemptAt:o,approvalPromptFailedAt:new Date().toISOString()})}},wakeMessageOnNotifySuccess:Je(e,t,!0),wakeMessageOnNotifyFailed:yr({session:e,planDecisionVersion:t,originThreadLine:this.deps.originThreadLine(e)}),failureWakeConfirmsNotificationDelivery:!1})}logCompletionWakeDiagnostic(e){JSON.stringify({event:e.event,sessionId:e.session.id,sessionName:e.session.name,canonicalStatusDelivered:e.canonicalStatusDelivered,requestedShortFactualSummary:e.followupSummaryRequired,completionKind:"terminal"})}shouldRequestCompletionFollowup(e){return e.originSessionKey?.trim()?.startsWith("agent:main:cron:")?!1:!!le(e)}async handleTurnEnd(e,t){if(e.status!=="running"){`${e.id}${e.status}`;return}if(!e.goalTaskId){if(t||e.pendingPlanApproval){await this.emitWaitingForInput(e);return}if(e.worktreeStrategy==="ask"||e.worktreeStrategy==="delegate"){`${e.id}${e.worktreeStrategy}`;return}this.deps.shouldEmitTurnCompleteWake(e)&&this.emitTurnComplete(e)}}async handleSessionTerminal(e){if(this.deps.persistSession(e),this.deps.clearWaitingTimestamp(e.id),e.goalTaskId){this.deps.clearRetryTimersForSession(e.id);return}if(e.pendingPlanApproval){e.killReason==="idle-timeout"&&e.pendingPlanApproval?this.emitIdleTimeoutPlanApproval(e):await this.emitWaitingForInput(e),this.deps.clearRetryTimersForSession(e.id);return}let t={notificationSent:!1,worktreeRemoved:!1};e.worktreePath&&e.originalWorkdir&&(t=await this.deps.handleWorktreeStrategy(e));let r=!1;if(e.worktreePath&&e.originalWorkdir&&e.status==="failed"&&e.costUsd===0&&e.duration<3e4){let a=this.deps.resolveWorktreeRepoDir(e.originalWorkdir,e.worktreePath),l=e.worktreeBranch,d=L(e);`${e.name}${e.costUsd.toFixed(2)}${e.duration}`;let u=!1;if(a&&!d&&(u=B(a,e.worktreePath)),a&&l&&!d&&u&&H(a,l),u){for(let m of z(e))this.deps.updatePersistedSession(m,{worktreePath:void 0,worktreeBranch:void 0});r=!0}}let o=e.worktreeStrategy&&e.worktreeStrategy!=="off"&&e.worktreeStrategy!=="manual";if(!r&&e.worktreePath&&e.originalWorkdir){let a=this.deps.resolveWorktreeRepoDir(e.originalWorkdir,e.worktreePath),l=L(e);t.worktreeRemoved?`${e.name}`:o?`${e.name}${e.worktreeStrategy}`:a&&!l&&B(a,e.worktreePath)}if(t.notificationSent){`${e.id}`;return}if(e.killReason==="done"){if(this.deps.hasTurnCompleteWakeMarker(e.id)||!this.deps.shouldEmitTerminalWake(e))return;this.emitCompleted(e);return}if(e.status==="completed"){if(!this.deps.shouldEmitTerminalWake(e))return;this.emitCompleted(e);return}if(e.status==="failed"){if(!this.deps.shouldEmitTerminalWake(e))return;let a=e.error||e.result?.is_error&&e.result.result||e.result?.result||this.deps.extractLastOutputLine(e)||`Session failed with no error details (session=${e.id}, subtype=${e.result?.subtype??"none"}, turns=${e.result?.num_turns??0})`;this.emitFailed(e,G(a,200),r);return}let i=`$${(e.costUsd??0).toFixed(2)}`,s=e.duration;if(e.killReason==="idle-timeout"){if(e.pendingPlanApproval){this.emitIdleTimeoutPlanApproval(e),this.deps.clearRetryTimersForSession(e.id);return}this.deps.dispatchSessionNotification(e,{label:"suspended",idempotencyKey:`suspended:${e.id}:${e.killReason??"idle-timeout"}:${e.completedAt??"unknown"}`,userMessage:`💤 [${e.name}] Suspended after idle timeout | ${i} | ${se(s)}`,notifyUser:"always",buttons:this.deps.getResumeButtons(e.id,e)}),this.deps.clearRetryTimersForSession(e.id);return}this.deps.notifySession(e,`⛔ [${e.name}] ${Po(e.killReason)} | ${i} | ${se(s)}`),this.deps.clearRetryTimersForSession(e.id)}emitIdleTimeoutPlanApproval(e){let t=this.deps.resolvePlanApprovalMode(e),r=e.actionablePlanDecisionVersion??e.planDecisionVersion,o=Rt(e,r);if(t==="delegate"){this.deps.dispatchSessionNotification(e,{label:"plan-approval-timeout",idempotencyKey:`plan-approval-timeout:${e.id}:v${r??"unknown"}:delegate`,wakeMessage:["[DELEGATED PLAN APPROVAL REMINDER] Plan review is still pending after the session hit idle timeout.",`Name: ${e.name} | ID: ${e.id}`,this.deps.originThreadLine(e),"The agent already produced a plan and is waiting for a delegated decision.","Review privately first. Approve directly with agent_respond(..., approve=true, approval_rationale='...') if the plan is clearly within scope and low risk.","Escalate only if needed via agent_request_plan_approval(summary='...').","If you approve directly, follow up with a short user-facing explanation; the plugin's thumbs-up line is only the minimal approval acknowledgment.","If a canonical approval prompt was already posted for this plan version, do not restate it in plain text."].join(`
|
|
210
215
|
`),notifyUser:"never"});return}if(t==="ask"&&o){this.deps.dispatchSessionNotification(e,{label:"plan-approval-timeout",idempotencyKey:`plan-approval-timeout:${e.id}:v${r??"unknown"}:already-delivered`,notifyUser:"never",wakeMessage:["[PLAN APPROVAL REMINDER] The user already has an actionable plan review prompt for this plan version.",`Name: ${e.name} | ID: ${e.id} | Plan v${r??"?"}`,this.deps.originThreadLine(e),"Do NOT post another approval summary unless canonical delivery is known to be missing."].join(`
|
|
211
216
|
`)});return}this.deps.dispatchSessionNotification(e,{label:"plan-approval-timeout",idempotencyKey:`plan-approval-timeout:${e.id}:v${r??"unknown"}:user-prompt`,userMessage:[`📋 [${e.name}] Plan v${r??"?"} still awaiting approval after idle timeout | $${(e.costUsd??0).toFixed(2)} | ${se(e.duration)}`,"","The agent already produced a plan and is waiting for your decision.","Approve resumes the session and starts implementation.","Revise resumes it in plan mode so it can update the plan first.","Reject keeps the session stopped."].join(`
|
|
212
|
-
`),notifyUser:"always",buttons:t==="ask"&&!o?this.deps.getPlanApprovalButtons(e.id,{...e,planDecisionVersion:r}):void 0})}async emitWaitingForInput(e){let t=e.pendingInputState?.questions,r=t?.[e.pendingInputState?.activeQuestionIndex??0],o=e.pendingInputState?.requestId,i=el(e.pendingInputState),s=o?[o,i].filter(Boolean).join(":"):void 0,a=s?`pending-input:${s}`:void 0;if(!this.deps.debounceWaitingEvent(e.id,a))return;let l=e.pendingPlanApproval?this.deps.resolvePlanApprovalMode(e):void 0,d=e.actionablePlanDecisionVersion??e.planDecisionVersion,u=e.pendingPlanApproval&&l==="ask"&&kt(e,d),f=e.pendingInputState?.promptText?.trim()||void 0,g=e.pendingPlanApproval?void 0:this.deps.getOutputPreview(e),S=!e.pendingPlanApproval&&f?f:!e.pendingPlanApproval&&g!==void 0?g:this.deps.getOutputPreview(e,e.pendingPlanApproval&&l!=="delegate"?Number.POSITIVE_INFINITY:void 0),y=e.pendingInputState?.options.map(w=>({label:w}))??[],c=!t||t.length===0?y:r&&r.options.length>0&&r.options.length<=6&&!r.options.some(w=>w.isOther)&&!r.multiSelect?r.options:t.length===1&&r&&r.options.length===0?y:[],h=e.pendingPlanApproval&&l==="ask"&&!u?this.deps.getPlanApprovalButtons(e.id,{...e,planDecisionVersion:d}):!e.pendingPlanApproval&&c.length?this.deps.getQuestionButtons(e.id,c,{requestId:e.pendingInputState?.requestId,questionId:r?.id}):void 0,v=Lo(e,d),b=r?tf(r):[],A=r?ef({question:r,index:e.pendingInputState?.activeQuestionIndex??0,total:t?.length??1,optionDescriptions:b}):f,I=!e.pendingPlanApproval&&this.deps.questionContextSummaryProvider?await Xa({sessionName:e.name,question:A??S,context:g,provider:this.deps.questionContextSummaryProvider}):void 0,R=xo({session:e,preview:S,questionText:e.pendingPlanApproval?void 0:A,questionContextPreview:g,questionContextSummary:I,planArtifact:v,originThreadLine:this.deps.originThreadLine(e),planApprovalMode:l,planApprovalButtons:h,questionButtons:e.pendingPlanApproval?void 0:h}),m=R.planReviewSummary??S;if(R.label==="plan-approval"&&l==="ask"&&u){this.deps.dispatchSessionNotification(e,{label:R.label,idempotencyKey:`plan-approval:${e.id}:v${d??"unknown"}:canonical`,userMessage:R.userMessage,userMessages:R.userMessages,notifyUser:"never",buttons:R.buttons,wakeMessage:R.wakeMessage});return}if(R.label==="plan-approval"&&l==="ask"){this.deps.dispatchSessionNotification(e,{label:R.label,idempotencyKey:`plan-approval:${e.id}:v${d??"unknown"}:canonical`,userMessage:R.userMessage,userMessages:R.userMessages,notifyUser:"always",buttons:R.buttons,hooks:{onNotifyStarted:()=>{this.deps.updatePersistedSession(e.id,{approvalPromptRequiredVersion:d,approvalPromptVersion:d,approvalPromptStatus:"sending",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"canonical_buttons",approvalPromptLastAttemptAt:new Date().toISOString()})},onNotifySucceeded:()=>{this.deps.updatePersistedSession(e.id,{canonicalPlanPromptVersion:d,approvalPromptRequiredVersion:d,approvalPromptVersion:d,approvalPromptStatus:"delivered",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"canonical_buttons",approvalPromptDeliveredAt:new Date().toISOString(),approvalPromptFailedAt:void 0})},onNotifyFailed:()=>{this.deps.updatePersistedSession(e.id,{approvalPromptRequiredVersion:d,approvalPromptVersion:d,approvalPromptStatus:"failed",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"canonical_buttons",approvalPromptFailedAt:new Date().toISOString()})}},shouldDispatch:()=>vt(e,d),onUserNotifyFailed:()=>this.dispatchPlanApprovalFallback(e,d,m),wakeMessageOnNotifySuccess:He(e,d)});return}if(R.label==="plan-approval"){this.deps.dispatchSessionNotification(e,{label:R.label,idempotencyKey:`plan-approval:${e.id}:v${d??"unknown"}:wake-only`,userMessage:R.userMessage,wakeMessage:R.wakeMessage,notifyUser:"never",buttons:R.buttons});return}this.deps.dispatchSessionNotification(e,{label:R.label,idempotencyKey:`waiting:${e.id}:${s??`${R.label}:${R.userMessage}`}`,userMessage:R.userMessage,notifyUser:"always",buttons:R.buttons,shouldDispatch:o?()=>nf(e,o,i):void 0,wakeMessageOnNotifyFailed:R.wakeMessage})}emitTurnComplete(e){`${e.id}${e.result?.num_turns??0}${e.worktreeStrategy??"none"}`;let t=vo({session:e,originThreadLine:this.deps.originThreadLine(e),preview:this.deps.getOutputPreview(e)});this.deps.dispatchSessionNotification(e,{label:"turn-complete",idempotencyKey:`turn-complete:${e.id}:${rf(e)}`,userMessage:t.userMessage,wakeMessage:t.wakeMessage,notifyUser:"always",onUserNotifyFailed:()=>{`${e.id}`,this.deps.shouldEmitTerminalWake(e)&&this.emitCompleted(e)}})}emitCompleted(e){let t=this.deps.getOutputPreview(e),r=this.shouldRequestCompletionFollowup(e),o=yo({session:e,originThreadLine:this.deps.originThreadLine(e),preview:t}),i=Za(e),s=`terminal:${e.id}:${i}`,a;this.deps.dispatchSessionNotification(e,{label:"completed",idempotencyKey:`terminal-completed:${e.id}:${i}`,userMessage:o.userMessage,notifyUser:"always",completionSummary:{required:r,producer:"terminal",outcomeKey:s},completionWakeSummaryRequired:r,completionWakeOutcomeKey:s,requireDirectUserNotification:!0,wakeMessageOnNotifySuccess:r?o.wakeMessageOnNotifySuccess:void 0,wakeMessageOnNotifyFailed:r?o.wakeMessageOnNotifyFailed:void 0,hooks:{onNotifySucceeded:()=>{a=!0,this.logCompletionWakeDiagnostic({session:e,event:"completion_notify_succeeded",canonicalStatusDelivered:a,followupSummaryRequired:r})},onNotifyFailed:()=>{a=!1,this.logCompletionWakeDiagnostic({session:e,event:"completion_notify_failed",canonicalStatusDelivered:a,followupSummaryRequired:r})},onWakeSucceeded:()=>{this.logCompletionWakeDiagnostic({session:e,event:"completion_wake_succeeded",canonicalStatusDelivered:a,followupSummaryRequired:r})}}})}emitFailed(e,t,r){let o=ko({session:e,originThreadLine:this.deps.originThreadLine(e),errorSummary:t,preview:this.deps.getOutputPreview(e),worktreeAutoCleaned:r,failedButtons:this.deps.getResumeButtons(e.id,e)});this.deps.dispatchSessionNotification(e,{label:"failed",idempotencyKey:`terminal-failed:${e.id}:${Za(e)}:${t}`,userMessage:o.userMessage,wakeMessage:o.wakeMessage,notifyUser:"always",buttons:o.buttons})}};import{existsSync as of}from"fs";var gr=class{constructor(e){this.deps=e}deps;async dismissWorktree(e){let t=this.deps.getPersistedSession(e),r=this.deps.resolveActiveSession(e),o=r??t;if(!o)return`Error: Session "${e}" not found.`;let i=r?.worktreePath??t?.worktreePath,s=this.deps.resolveWorktreeRepoDir(r?.originalWorkdir??t?.workdir,i),a=r?.worktreeBranch??t?.worktreeBranch,l=r?.name??t?.name??e;if(!s)return`Error: No workdir found for session "${e}".`;let d=L(o);!d&&i&&of(i)&&B(s,i,{destructive:!0}),a&&H(s,a);for(let f of z(r??t))this.deps.updatePersistedSession(f,{worktreeDisposition:"dismissed",worktreeDismissedAt:new Date().toISOString(),pendingWorktreeDecisionSince:void 0,worktreeState:"dismissed",lifecycle:"terminal",worktreePath:void 0,worktreeBranch:void 0,worktreeLifecycle:{state:"dismissed",updatedAt:new Date().toISOString(),resolvedAt:new Date().toISOString(),resolutionSource:"dismiss",baseBranch:t?.worktreeLifecycle?.baseBranch,targetRepo:t?.worktreePrTargetRepo,pushRemote:t?.worktreePushRemote}});let u=d?`🗑️ [${l}] Branch \`${a??"unknown"}\` dismissed. Native backend worktree released for backend cleanup.`:`🗑️ [${l}] Branch \`${a??"unknown"}\` dismissed and permanently deleted.`;return this.deps.dispatchNotification(this.deps.buildRoutingProxy({id:D(r??t??{id:e})??e,sessionId:t?.sessionId,harnessSessionId:r?.harnessSessionId??t?.harnessSessionId,backendRef:r?.backendRef??t?.backendRef,route:r?.route??t?.route}),{label:"worktree-dismissed",idempotencyKey:`worktree-dismissed:${D(r??t??{id:e})??e}:${a??"unknown"}`,userMessage:u,notifyUser:"always"}),u}snoozeWorktreeDecision(e,t={}){let r=this.deps.getPersistedSession(e);if(!r)return`Error: Session "${e}" not found.`;let o=Date.now(),i=new Date(o+1440*60*1e3).toISOString();for(let l of z(r))this.deps.updatePersistedSession(l,{worktreeDecisionSnoozedUntil:i,lastWorktreeReminderAt:new Date(o).toISOString()});let a=`⏭️ Reminder snoozed 24h for \`${r.worktreeBranch??"unknown"}\` (session: ${r.name})`;return t.notifyUser!==!1&&this.deps.dispatchNotification(this.deps.buildRoutingProxy({id:D(r)??C(r)??r.harnessSessionId,sessionId:r.sessionId,harnessSessionId:r.harnessSessionId,backendRef:r.backendRef,route:r.route}),{label:"worktree-snoozed",idempotencyKey:`worktree-snoozed:${D(r)??C(r)??r.harnessSessionId}:${i}`,userMessage:a,notifyUser:"always"}),a}};var tl=new Set(["starting","running"]);function nl(n,e){JSON.stringify({component:"SessionRuntimeRegistry",event:n,at:new Date().toISOString(),...e})}function rl(n){return{sessionId:n.id,name:n.name,status:n.status,lifecycle:n.lifecycle,runtimeState:n.runtimeState,harness:n.harnessName,hasHarnessSessionId:!!n.harnessSessionId,backendRefKind:n.backendRef?.kind,hasBackendConversationId:!!n.backendRef?.conversationId,hasBackendRunId:!!n.backendRef?.runId,hasBackendWorktreeId:!!n.backendRef?.worktreeId,hasBackendWorktreePath:!!n.backendRef?.worktreePath}}var hr=class{sessions=new Map;activeSessionCount(){return[...this.sessions.values()].filter(e=>tl.has(e.status)).length}uniqueName(e){let t=new Set([...this.sessions.values()].filter(o=>tl.has(o.status)).map(o=>o.name));if(!t.has(e))return e;let r=2;for(;t.has(`${e}-${r}`);)r+=1;return`${e}-${r}`}add(e,t="spawn"){nl("runtime.add",{reason:t,...rl(e),activeCountBefore:this.activeSessionCount()}),this.sessions.set(e.id,e)}remove(e,t="unknown"){let r=this.sessions.get(e);nl("runtime.remove",{reason:t,sessionId:e,existed:!!r,...r?rl(r):{},activeCountBefore:this.activeSessionCount()}),this.sessions.delete(e)}get(e){return this.sessions.get(e)}list(){return[...this.sessions.values()]}};var yr=class{constructor(e){this.deps=e}deps;initializeSession(e,t,r,o={}){if(this.deps.hydrateSpawnedSession(e,t,r),r.taskLifecycle?.create(e),e.on("statusChange",(i,s)=>{s==="running"?(e.harnessSessionId&&this.deps.markRunning(e),r.taskLifecycle?.progress(e)):(s==="completed"||s==="failed"||s==="killed")&&(r.taskLifecycle?.finalize(e),this.deps.handleTerminal(e).catch(a=>{console.error(`[SessionRuntimeBootstrap] handleTerminal threw for session ${e.id}:`,a)}))}),e.on("lifecycleChange",(i,s)=>{r.taskLifecycle?.progress(e)}),e.on("turnEnd",(i,s)=>{this.deps.handleTurnEnd(e,s).catch(a=>{console.error(`[SessionRuntimeBootstrap] handleTurnEnd threw for session ${e.id}:`,a)})}),o.startAfter?o.startAfter.then(()=>e.start()).catch(i=>{console.error(`[SessionRuntimeBootstrap] deferred start threw for session ${e.id}:`,i)}):e.start(),o.notifyLaunch!==!1){let i=this.buildLaunchNotification(e);this.deps.notifySession(e,i.text,i.label,i.idempotencyKey)}return e}buildLaunchNotification(e){let t=this.deps.formatLaunchWorkdirLabel(e),r=ge({harness:e.harnessName,model:e.model})??"default";return e.resumeSessionId?{text:Qt({sessionName:e.name,resumedFromSessionName:e.resumedFromSessionName,workdirLabel:t,harnessLabel:r}),label:"resumed-launch",idempotencyKey:`resumed-launch:${e.id}:${e.startedAt}:${e.resumeSessionId}`}:{text:`🚀 [${e.name}] Launched | ${t} | ${r}`,label:"launch"}}};var Sr=class{buildNoChangeNotification(e){let{session:t,nativeBackendWorktree:r,cleanupSucceeded:o,worktreePath:i,worktreeBranch:s,preview:a,originThreadLine:l,preservedSummary:d,remoteOutcome:u}=e,f=d?"preserved":o?"cleaned":"cleanup-failed",g=[s?.trim()||"unknown-branch",i.trim()||"unknown-worktree",t.startedAt].join(":"),S=d??(o?r?"native backend worktree released for backend cleanup":"worktree cleaned up":`cleanup failed; worktree still exists at ${i}`),y=ye({costUsd:t.costUsd,duration:typeof t.completedAt=="number"?t.completedAt-t.startedAt:void 0,harnessName:t.harnessName,model:t.model}),c=u==="pr-updated"?"PR updated; no local worktree changes remained to merge":u==="pr-opened"?"PR opened; no local worktree changes remained to merge":"Session completed with no worktree changes to merge",h=u==="pr-updated"?`PR updated; no local worktree changes remained to merge — ${S}`:u==="pr-opened"?`PR opened; no local worktree changes remained to merge — ${S}`:void 0,v=u==="pr-updated"?"PR updated; no local worktree changes remained to merge":u==="pr-opened"?"PR opened; no local worktree changes remained to merge":"Session completed with no worktree changes to merge",b=u==="pr-updated"?"Coding agent session updated a PR; no local worktree changes remained to merge.":u==="pr-opened"?"Coding agent session opened a PR; no local worktree changes remained to merge.":void 0;return{label:d?"worktree-no-changes-preserved":o?"worktree-no-changes":"worktree-no-changes-cleanup-failed",idempotencyKey:`worktree-no-change:${t.id}:${f}:${g}`,userMessage:d?`ℹ️ [${t.name}] ${c} — ${d}${y}`:o?r?`ℹ️ [${t.name}] ${c} — native backend worktree released for backend cleanup${y}`:`ℹ️ [${t.name}] ${c} — worktree cleaned up${y}`:`⚠️ [${t.name}] ${v}, but worktree cleanup failed. Worktree still exists at ${i}${y}`,wakeMessage:bo({sessionName:t.name,sessionId:t.id,headline:b,cleanupSummary:h??S,preview:a,originThreadLine:l,requestedPermissionMode:t.requestedPermissionMode,currentPermissionMode:t.currentPermissionMode,approvalExecutionState:t.approvalExecutionState,approvalState:t.approvalState,planApproval:t.planApproval,approvalPromptStatus:t.approvalPromptStatus,approvalPromptMessageKind:t.approvalPromptMessageKind,approvalPromptDeliveredAt:t.approvalPromptDeliveredAt}),notifyUser:"always"}}buildAskNotification(e){let{session:t,branchName:r,baseBranch:o,diffSummary:i,buttons:s,summaryLines:a=[],policyReason:l}=e,d=i.commitMessages.slice(0,5).map(g=>`• ${g.hash} ${g.message} (${g.author})`),u=i.commits>5?`...and ${i.commits-5} more`:"",f=t.worktreePrTargetRepo?`Branch: \`${r}\` → \`${o}\` | PR target: ${t.worktreePrTargetRepo}`:`Branch: \`${r}\` → \`${o}\``;return{label:"worktree-merge-ask",idempotencyKey:["worktree-decision",t.id,r,o,i.commits,i.commitMessages.map(g=>g.hash).join(",")].join(":"),userMessage:[`🔀 Worktree decision required for session \`${t.name}\``,"",f,`Commits: ${i.commits} | Files: ${i.filesChanged} | +${i.insertions} / -${i.deletions}`,"",...a.length>0?["Summary:",...a.map(g=>`- ${g}`),""]:[],...l?[`Policy: ${l}`,""]:[],"Recent commits:",...d,...u?[u]:[],"",`⚠️ Discard will permanently delete branch \`${r}\` and all local changes. This cannot be undone.`].join(`
|
|
213
|
-
`),notifyUser:"always",buttons:s,wakeMessageOnNotifySuccess:["Worktree decision buttons delivered to the user.",`Session: ${t.name} | ID: ${t.id}`,
|
|
214
|
-
`),wakeMessageOnNotifyFailed:[`🔀 Worktree decision required for session \`${t.name}\``,"",
|
|
215
|
-
`)}}buildDelegateNotification(e){let{session:t,branchName:r,baseBranch:o,diffSummary:i,policyReason:s,allowedActions:a,originThreadLine:l}=e,d=i.commitMessages.slice(0,5).map(g=>`• ${g.hash} ${g.message} (${g.author})`),u=i.commits>5?`...and ${i.commits-5} more`:void 0,
|
|
216
|
-
`):
|
|
217
|
-
`).trim();return t?
|
|
218
|
-
`):""}function
|
|
219
|
-
`)?t:void 0}import{unlinkSync as lf}from"fs";var kr=class n{timers=new Map;disposed=!1;static MAX_DELAY_MS=2147483647;schedule(e,t,r){if(this.disposed||(this.cancel(e),!Number.isFinite(t)))return;let o=Math.max(0,t-Date.now()),i=setTimeout(()=>{if(this.timers.delete(e),!this.disposed){if(t-Date.now()>0){this.schedule(e,t,r);return}r()}},Math.min(o,n.MAX_DELAY_MS));i.unref?.(),this.timers.set(e,i)}cancel(e){let t=this.timers.get(e);t&&(clearTimeout(t),this.timers.delete(e))}cancelPrefix(e){for(let t of this.timers.keys())t.startsWith(e)&&this.cancel(t)}dispose(){this.disposed=!0;for(let e of this.timers.values())clearTimeout(e);this.timers.clear()}};var sl=10080*60*1e3,df=300*1e3,al="tmp-output:cleanup",uf=60*1e3;function cf(n){return n instanceof Error?n.message:String(n)}var vr=class{constructor(e){this.deps=e}deps;scheduler=new kr;lastTmpOutputCleanupAttemptAt;schedule(e,t,r){this.scheduler.schedule(e,t,r)}cancel(e){this.scheduler.cancel(e)}cancelPrefix(e){this.scheduler.cancelPrefix(e)}bootstrapMaintenanceSchedules(){let e=Date.now();this.runTmpOutputCleanup(e);for(let t of this.deps.store.listPersistedSessions())this.syncPersistedSessionMaintenance(t);this.syncActionTokenExpiryDeadline(),this.deps.store.cleanupOrphanOutputFiles(),this.syncTmpOutputCleanupDeadline(e)}syncRuntimeGcDeadline(e){if(!e.completedAt)return;let t=this.runtimeGcKey(e.id);this.schedule(t,e.completedAt+this.runtimeGcMaxAgeMs(),()=>{let r=this.deps.sessions.get(e.id);if(!r||!r.completedAt)return;let o=this.runtimeGcMaxAgeMs();if(!this.deps.store.shouldGcActiveSession(r,Date.now(),o)){this.syncRuntimeGcDeadline(r);return}this.deps.removeRuntimeSession(e.id,"runtime-gc"),this.deps.persistSession(r,{scheduleRuntimeGc:!1}),this.deps.clearRuntimeSessionState(e.id)})}cancelRuntimeGc(e){this.cancel(this.runtimeGcKey(e))}cancelPersistedMaintenance(e){let t=this.persistedMaintenanceRef(e);t&&this.cancelPrefix(`persisted:${t}:`)}syncPersistedSessionMaintenance(e){let t=this.persistedMaintenanceRef(e);if(!t)return;this.cancel(this.persistedMaintenanceKey(t,"worktree-reminder"));let r=this.deps.reminders.getNextReminderAt(e);r!=null?this.schedulePersistedWorktreeReminder(t,r):this.deps.reminders.clearResolvedReminderState(e),this.cancel(this.persistedMaintenanceKey(t,"worktree-retention"));let o=Oe(e,{activeSession:!1,includePrSync:e.worktreeLifecycle?.state==="pr_open"||!!e.worktreePrUrl}),i=this.resolvedAtIso(e),s=this.isLegacyResolvedWorktree(e);if((o.cleanupSafe||s)&&typeof i=="string"){let a=new Date(i).getTime();Number.isFinite(a)&&this.schedule(this.persistedMaintenanceKey(t,"worktree-retention"),a+sl,()=>{let l=this.deps.store.getPersistedSession(t);l&&this.reconcileResolvedWorktreeRetention(l,Date.now())})}}reconcileResolvedWorktreeRetention(e,t){let r=Oe(e,{activeSession:!1,includePrSync:e.worktreeLifecycle?.state==="pr_open"||!!e.worktreePrUrl}),o=this.resolvedAtIso(e),i=o?new Date(o).getTime():0,s=this.legacyResolvedWorktreeState(e);if(!(!r.cleanupSafe&&!s||!o||!Number.isFinite(i)||t-i<sl))try{if(!e.worktreePath&&!L(e))return;let a=this.deps.resolveWorktreeRepoDir(e.workdir,e.worktreePath);if(!a)return;let l=L(e)?!1:B(a,e.worktreePath);if(!L(e)&&!l)return;for(let d of z(e))this.deps.updatePersistedSession(d,{worktreePath:void 0,worktreeBranch:void 0,worktreeState:"none",pendingWorktreeDecisionSince:void 0,lastWorktreeReminderAt:void 0,worktreeDecisionSnoozedUntil:void 0,worktreeLifecycle:{...e.worktreeLifecycle??r.lifecycle,state:r.cleanupSafe?r.derivedState:s??r.derivedState,updatedAt:new Date(t).toISOString(),resolvedAt:e.worktreeLifecycle?.resolvedAt??o,resolutionSource:e.worktreeLifecycle?.resolutionSource??"maintenance",notes:r.reasons}})}catch(a){`${e.worktreePath}${cf(a)}`}}syncActionTokenExpiryDeadline(){let e="tokens:expiry";this.cancel(e);let t=this.deps.store.getNextActionTokenExpiry();t!=null&&this.schedule(e,t,()=>{this.deps.store.purgeExpiredActionTokens(Date.now())||this.syncActionTokenExpiryDeadline()})}syncTmpOutputCleanupDeadline(e=Date.now()){this.cancel(al);let t=this.deps.store.getNextTmpOutputCleanupAt(e);if(t==null){this.lastTmpOutputCleanupAttemptAt=void 0;return}t>e&&(this.lastTmpOutputCleanupAttemptAt=void 0),this.schedule(al,this.getTmpOutputCleanupScheduleAt(t,e),()=>{let r=Date.now();this.runTmpOutputCleanup(r),this.syncTmpOutputCleanupDeadline(r)})}enforcePersistedRetention(){let e=this.deps.store.evictOldestPersisted(this.deps.getMaxPersistedSessions());for(let t of e)this.cancelPersistedMaintenance(t),t.sessionId&&this.cancelRuntimeGc(t.sessionId),this.cleanupOutputPathIfUnreferenced(t.outputPath)}dispose(){this.scheduler.dispose()}persistedMaintenanceRef(e){return e.sessionId??C(e)??e.harnessSessionId}runtimeGcKey(e){return`runtime-gc:${e}`}persistedMaintenanceKey(e,t){return`persisted:${e}:${t}`}runtimeGcMaxAgeMs(){return(_.sessionGcAgeMinutes??1440)*6e4}runTmpOutputCleanup(e){this.lastTmpOutputCleanupAttemptAt=e,this.deps.store.cleanupTmpOutputFiles(e)}getTmpOutputCleanupScheduleAt(e,t){if(e>t||this.lastTmpOutputCleanupAttemptAt==null)return e;let r=this.lastTmpOutputCleanupAttemptAt+uf;return r>t?r:e}schedulePersistedWorktreeReminder(e,t){let r=this.persistedMaintenanceKey(e,"worktree-reminder");this.schedule(r,t,()=>{let o=this.deps.store.getPersistedSession(e);if(!o||this.deps.reminders.sendReminderIfDue(o,Date.now()))return;let s=this.deps.reminders.getNextReminderAt(o);s!=null&&this.schedulePersistedWorktreeReminder(e,Math.max(s,Date.now()+df))})}cleanupOutputPathIfUnreferenced(e){if(!(!e||this.deps.store.hasOutputPathReference(e)))try{lf(e)}catch{}}resolvedAtIso(e){return e.worktreeLifecycle?.resolvedAt??e.worktreeMergedAt??e.worktreeDismissedAt??(e.completedAt?new Date(e.completedAt).toISOString():void 0)}isLegacyResolvedWorktree(e){return this.legacyResolvedWorktreeState(e)!=null}legacyResolvedWorktreeState(e){if(e.worktreeMerged===!0||e.worktreeDisposition==="merged"||e.worktreeState==="merged")return"merged";if(e.worktreeDisposition==="dismissed"||e.worktreeState==="dismissed")return"dismissed";if(e.worktreeDisposition==="no-change-cleaned")return"no_change"}};var ll=new Set(["completed","failed","killed"]),pf=new Set(["starting","running"]),mf=5e3;function ff(n,e){return dl("sha256").update(JSON.stringify({route:n.route?{provider:n.route.provider,target:n.route.target,accountId:n.route.accountId,threadId:n.route.threadId,sessionKey:n.route.sessionKey}:void 0,prompt:n.prompt,workdir:n.workdir,name:n.name,model:n.model,reasoningEffort:n.reasoningEffort,fastMode:n.fastMode,systemPrompt:n.systemPrompt,allowedTools:n.allowedTools?[...n.allowedTools].sort():n.allowedTools,resumeSessionId:n.resumeSessionId,resumedFromSessionName:n.resumedFromSessionName,resumeWorktreeFrom:n.resumeWorktreeFrom,sessionIdOverride:n.sessionIdOverride,clearedPersistedCodexResume:n.clearedPersistedCodexResume,forkSession:n.forkSession,forceNewSession:n.forceNewSession,permissionMode:n.permissionMode,planApproval:n.planApproval,harness:n.harness,worktreeStrategy:n.worktreeStrategy,effectiveWorktreeStrategy:e,worktreeBaseBranch:n.worktreeBaseBranch,worktreePrTargetRepo:n.worktreePrTargetRepo,originAgentId:n.originAgentId})).digest("hex").slice(0,16)}function gf(n){return dl("sha256").update(JSON.stringify({route:n.route?{provider:n.route.provider,target:n.route.target,accountId:n.route.accountId,threadId:n.route.threadId,sessionKey:n.route.sessionKey}:void 0,prompt:n.launchPrompt,workdir:n.launchWorkdir,name:n.launchName,model:n.launchModel,reasoningEffort:n.launchReasoningEffort,fastMode:n.launchFastMode,systemPrompt:n.launchSystemPrompt,allowedTools:n.launchAllowedTools?[...n.launchAllowedTools].sort():n.launchAllowedTools,resumeSessionId:n.launchResumeSessionId,resumedFromSessionName:n.launchResumedFromSessionName,resumeWorktreeFrom:n.launchResumeWorktreeFrom,sessionIdOverride:n.launchSessionIdOverride,clearedPersistedCodexResume:n.launchClearedPersistedCodexResume,forkSession:n.launchForkSession,forceNewSession:n.launchForceNewSession,permissionMode:n.launchPermissionMode,planApproval:n.launchPlanApproval,harness:n.launchHarness,worktreeStrategy:n.launchWorktreeStrategy,worktreeBaseBranch:n.launchWorktreeBaseBranch,worktreePrTargetRepo:n.launchWorktreePrTargetRepo,originAgentId:n.launchOriginAgentId})).digest("hex").slice(0,16)}var Pr=class n{registry;sessions;maxSessions;maxPersistedSessions;lastWaitingEventTimestamps=new Map;lastTurnCompleteMarkers=new Map;lastTerminalWakeMarkers=new Map;mergeQueue=new ze;pendingAskUserQuestions=new Map;store;metrics;wakeDispatcher;interactions;notifications;worktrees;questions;lifecycle;restore;stateSync;references;worktreeStrategy;worktreeDecisions;runtimeBootstrap;worktreeMessages;maintenance;pendingPlanResumeClaims=new Map;constructor(e=20,t=50,r={}){this.maxSessions=e,this.maxPersistedSessions=t;let o=n.createServiceBundle(this,r);this.registry=o.registry,this.sessions=o.sessions,this.store=o.store,this.metrics=o.metrics,this.wakeDispatcher=o.wakeDispatcher,this.interactions=o.interactions,this.notifications=o.notifications,this.worktrees=o.worktrees,this.questions=o.questions,this.lifecycle=o.lifecycle,this.restore=o.restore,this.stateSync=o.stateSync,this.references=o.references,this.worktreeStrategy=o.worktreeStrategy,this.worktreeDecisions=o.worktreeDecisions,this.runtimeBootstrap=o.runtimeBootstrap,this.worktreeMessages=o.worktreeMessages,this.maintenance=o.maintenance,this.reconcilePersistedTaskFlowMirrors()}static createServiceBundle(e,t){let r=new hr,o=r.sessions,i=new Xn(t.store),s=new Zn,a=new rr,l=new or(i.actionTokenStore,Je),d=new Bn(o,i),u=new Ln({store:i,sessions:o,resolveSession:m=>d.resolveActive(m)}),f=new lr(a,(m,w)=>u.applySessionPatch(m,w),{getPersistedSession:m=>i.getPersistedSession(m)}),g=new dr,S=new Fn(m=>i.getPersistedSession(m)),y=new Sr,c=new jn({shouldRunWorktreeStrategy:m=>e.shouldRunWorktreeStrategy(m),isAlreadyMerged:m=>e.isAlreadyMerged(m),resolveWorktreeRepoDir:(m,w)=>e.resolveWorktreeRepoDir(m,w),getWorktreeCompletionState:(m,w,T,F)=>e.getWorktreeCompletionState(m,w,T,F),updatePersistedSession:(m,w)=>e.updatePersistedSession(m,w),getPersistedSession:m=>i.getPersistedSession(m),dispatchSessionNotification:(m,w)=>e.dispatchSessionNotification(m,w),getOutputPreview:(m,w)=>e.getOutputPreview(m,w),originThreadLine:m=>e.originThreadLine(m),getWorktreeDecisionButtons:m=>e.getWorktreeDecisionButtons(m),getPolicyAwareWorktreeDecisionButtons:(m,w,T)=>e.getWorktreeDecisionButtons(m,w,T),makeOpenPrButton:m=>e.makeActionButton(m,"worktree-create-pr","Open PR"),isPrAvailable:m=>e.resolveRepoPolicy(m).prAvailable,hasOpenPrForBranch:(m,w,T)=>{let F=nt(m,w,T);return F.exists&&F.state==="open"},getPrStatusForBranch:(m,w,T)=>nt(m,w,T),getPrStatusForUrl:(m,w,T)=>Et(m,w,T),resolveRepoPolicy:m=>e.resolveRepoPolicy(m),worktreeSummaryProvider:t.worktreeSummaryProvider,worktreeMessages:y,enqueueMerge:(m,w,T)=>e.enqueueMerge(m,w,T),mergeBranch:$t,spawnConflictResolver:async({session:m,worktreePath:w,prompt:T})=>e.spawn({prompt:T,workdir:w,name:`${m.name}-conflict-resolver`,harness:m.harnessName||Y(),model:m.model,reasoningEffort:m.reasoningEffort,fastMode:m.fastMode,permissionMode:"bypassPermissions",multiTurn:!0,worktreeStrategy:"off",autoMergeParentSessionId:m.id,route:m.route,originChannel:m.originChannel,originThreadId:m.originThreadId,originAgentId:m.originAgentId,originSessionKey:m.originSessionKey},{notifyLaunch:!1}),runAutoPr:async(m,w)=>{let{makeAgentPrTool:T}=await import("./chunks/agent-pr.js");return{success:(await T().execute("auto-pr",{session:m.id,base_branch:w}))?.meta?.success===!0}}}),h=new ur(e.pendingAskUserQuestions,(m,w)=>e.dispatchSessionNotification(m,w),m=>{e.clearWaitingTimestampsForSession(m)},(m,w,T)=>l.getQuestionButtons(m,w,T)),v=new cr(m=>e.buildRoutingProxy(m),(m,w)=>f.dispatch(m,w),(m,w)=>e.updatePersistedSession(m,w),(m,w)=>e.getPolicyAwareWorktreeDecisionButtons(m,{},void 0,w)),b=new vr({store:i,sessions:o,reminders:v,removeRuntimeSession:(m,w)=>r.remove(m,w),persistSession:(m,w)=>e.persistSession(m,w),clearRuntimeSessionState:m=>{e.clearWaitingTimestampsForSession(m),e.lastTurnCompleteMarkers.delete(m),e.lastTerminalWakeMarkers.delete(m)},resolveWorktreeRepoDir:(m,w)=>e.resolveWorktreeRepoDir(m,w),updatePersistedSession:(m,w)=>e.updatePersistedSession(m,w),getMaxPersistedSessions:()=>e.maxPersistedSessions});i.onActionTokensChanged(()=>e.syncActionTokenExpiryDeadline());let A=new fr({persistSession:m=>e.persistSession(m),clearWaitingTimestamp:m=>{e.clearWaitingTimestampsForSession(m)},handleWorktreeStrategy:m=>e.handleWorktreeStrategy(m),resolveWorktreeRepoDir:(m,w)=>e.resolveWorktreeRepoDir(m,w),updatePersistedSession:(m,w)=>e.updatePersistedSession(m,w),dispatchSessionNotification:(m,w)=>e.dispatchSessionNotification(m,w),notifySession:(m,w,T,F)=>e.notifySession(m,w,T,F),clearRetryTimersForSession:m=>a.clearRetryTimersForSession(m),hasTurnCompleteWakeMarker:m=>e.lastTurnCompleteMarkers.has(m),shouldEmitTurnCompleteWake:m=>e.shouldEmitTurnCompleteWake(m),shouldEmitTerminalWake:m=>e.shouldEmitTerminalWake(m),resolvePlanApprovalMode:m=>e.resolvePlanApprovalMode(m),getPlanApprovalButtons:(m,w)=>l.getPlanApprovalButtons(m,w),getResumeButtons:(m,w)=>l.getResumeButtons(m,w),getQuestionButtons:(m,w,T)=>l.getQuestionButtons(m,w,T),extractLastOutputLine:m=>e.extractLastOutputLine(m),getOutputPreview:(m,w)=>e.getOutputPreview(m,w),originThreadLine:m=>e.originThreadLine(m),debounceWaitingEvent:(m,w)=>e.debounceWaitingEvent(m,w),isAlreadyMerged:m=>e.isAlreadyMerged(m),questionContextSummaryProvider:t.questionContextSummaryProvider??Ja()}),I=new gr({getPersistedSession:m=>i.getPersistedSession(m),resolveActiveSession:m=>d.resolveActive(m),resolveWorktreeRepoDir:(m,w)=>e.resolveWorktreeRepoDir(m,w),updatePersistedSession:(m,w)=>e.updatePersistedSession(m,w),dispatchNotification:(m,w)=>f.dispatch(m,w),buildRoutingProxy:m=>e.buildRoutingProxy(m)}),R=new yr({hydrateSpawnedSession:(m,w,T)=>{S.hydrateSpawnedSession(m,w,T)},markRunning:m=>{i.markRunning(m),m.approvalState==="approved"&&m.planDecisionVersion>0&&l.consumePlanDecisionTokens(m.id,m.planDecisionVersion-1),e.pendingPlanResumeClaims.delete(m.id),e.onPersistedSessionChanged(i.getPersistedSession(m.id))},handleTerminal:async m=>{let w=e.pendingPlanResumeClaims.get(m.id);try{await e.onSessionTerminal(m)}finally{if(w){let T=_i(w,i.getPersistedSession(m.id));i.replacePersistedSession(T),e.pendingPlanResumeClaims.delete(m.id),e.onPersistedSessionChanged(T)}}},handleTurnEnd:(m,w)=>A.handleTurnEnd(m,w),formatLaunchWorkdirLabel:m=>e.formatLaunchWorkdirLabel(m),notifySession:(m,w,T,F)=>e.notifySession(m,w,T,F)});return{registry:r,sessions:o,store:i,metrics:s,wakeDispatcher:a,interactions:l,notifications:f,worktrees:g,questions:h,lifecycle:A,restore:S,stateSync:u,references:d,worktreeStrategy:c,worktreeDecisions:I,runtimeBootstrap:R,worktreeMessages:y,maintenance:b}}get persisted(){return this.store.persisted}get idIndex(){return this.store.idIndex}get nameIndex(){return this.store.nameIndex}uniqueName(e){return this.registry.uniqueName(e)}syncRuntimeGcDeadline(e){this.maintenance.syncRuntimeGcDeadline(e)}onPersistedSessionChanged(e){e&&(this.syncPersistedSessionMaintenance(e),this.enforcePersistedRetention())}syncPersistedSessionMaintenance(e){this.maintenance.syncPersistedSessionMaintenance(e)}syncActionTokenExpiryDeadline(){this.maintenance.syncActionTokenExpiryDeadline()}syncTmpOutputCleanupDeadline(e=Date.now()){this.maintenance.syncTmpOutputCleanupDeadline(e)}enforcePersistedRetention(){this.maintenance.enforcePersistedRetention()}bootstrapMaintenanceSchedules(){this.maintenance.bootstrapMaintenanceSchedules()}disposeMaintenance(){this.maintenance.dispose()}spawn(e,t={}){if(this.registry.activeSessionCount()>=this.maxSessions)throw new Error(`Max sessions reached (${this.maxSessions}). Use agent_sessions to list active sessions and agent_kill to end one.`);let o,i,s=h=>{i=i?Promise.all([i,h]).then(()=>{}):h};if(e.resumeSessionId&&!e.forkSession){let h=this.registry.list().filter(b=>b.backendKind==="codex-app-server"&&(b.harnessSessionId===e.resumeSessionId||b.backendRef?.conversationId===e.resumeSessionId||!b.harnessSessionId&&!b.backendRef?.conversationId&&b.resumeSessionId===e.resumeSessionId)),v=h.find(b=>b.status==="starting"||b.status==="running");if(v)throw new Error(`Cannot resume backend thread ${e.resumeSessionId}: session ${v.id} still owns its active writer.`);for(let b of h)typeof b.waitForTeardown=="function"&&s(b.waitForTeardown())}if(e.sessionIdOverride){let h=this.getPersistedSession(e.sessionIdOverride);if(e.resumeSessionId&&h?.pendingPlanApproval){let b=an(h,e.permissionMode??_.permissionMode);e={...e,permissionMode:b.permissionMode,...b.patch},b.approvalApplied&&(o=h)}let v=this.registry.get(e.sessionIdOverride);if(v?.status==="starting"||v?.status==="running")throw new Error(`Cannot reuse session ID ${e.sessionIdOverride}: that session is still ${v.status}.`);v&&(typeof v.waitForTeardown=="function"&&s(v.waitForTeardown()),this.registry.remove(v.id,"session-id-override-replacement")),this.clearWaitingTimestampsForSession(e.sessionIdOverride),this.lastTurnCompleteMarkers.delete(e.sessionIdOverride),this.lastTerminalWakeMarkers.delete(e.sessionIdOverride),this.maintenance.cancelRuntimeGc(e.sessionIdOverride)}let a=e.name||Ri(e.prompt),l=this.uniqueName(a);l!==a&&`${a}${l}`;let d=this.checkRepoPolicyForLaunch(e.workdir,e.worktreeStrategy);if(!d.ok){let h=d;throw new Error(h.text)}e.repoIntegrationPolicy=d.resolution.policy,e.repoIntegrationPolicySource=d.resolution.source==="none"?void 0:d.resolution.source,e.repoProvider=d.resolution.provider;let u=this.restore.prepareSpawn(e,l);if(!e.route?.provider||!e.route.target)throw new Error(`Cannot launch session "${l}": missing explicit route metadata.`);let f=e.harness??"claude-code",g=this,S,y=f==="claude-code"&&!e.canUseTool?async(h,v)=>{if(!S)throw new Error("canUseTool called before session ID was set");return g.handleAskUserQuestion(S,v)}:e.canUseTool,c=new mn({...e,workdir:u.actualWorkdir,systemPrompt:u.effectiveSystemPrompt,canUseTool:y},l);S=c.id,o&&this.pendingPlanResumeClaims.set(c.id,o),this.registry.add(c),this.metrics.incrementLaunched();try{return this.runtimeBootstrap.initializeSession(c,u,e,{...t,startAfter:i})}catch(h){throw this.pendingPlanResumeClaims.delete(c.id),h}}async spawnAndAwaitRunning(e,t={}){let r=this.spawn(e,t);return await this.waitForRunningSession(r),r}async waitForRunningSession(e){if(e.status==="running")return;if(ll.has(e.status))throw new Error(this.describeLaunchFailure(e));let t=e.on?.bind(e),r=e.off?.bind(e)??e.removeListener?.bind(e);if(!t||!r)throw new Error(`Session ${e.name} [${e.id}] did not expose lifecycle events during startup.`);await new Promise((o,i)=>{let s=(l,d)=>{if(d==="running"){a(),o();return}ll.has(d)&&(a(),i(new Error(this.describeLaunchFailure(e))))},a=()=>{r("statusChange",s)};t("statusChange",s)})}describeLaunchFailure(e){let t=e.killReason?` (reason: ${e.killReason})`:"",r=e.error||e.result?.result||`status=${e.status}${t}`;return`Session ${e.name} [${e.id}] failed to start: ${r}`}formatLaunchResult(e,t){return Ht({prompt:e.prompt,workdir:e.workdir,harness:e.harness,permissionMode:e.permissionMode??_.permissionMode,planApproval:e.planApproval,resumeSessionId:e.resumeSessionId,resumeSessionName:e.resumeSessionName,forkSession:e.forkSession,forceNewSession:e.forceNewSession,clearedPersistedCodexResume:e.clearedPersistedCodexResume},t)}shouldRunWorktreeStrategy(e){let t=e.lifecycle;return!(t==="starting"||t==="awaiting_plan_decision"||t==="awaiting_user_input"||e.pendingPlanApproval)}resolveRepoPolicy(e){let t=lt(e);if(!t)return{source:"none",provider:"unsupported",prAvailable:!1};let r=this.store.getRepoPolicy(t.key);if(r)return{identity:t,policy:r.policy,source:"stored",provider:t.provider,prAvailable:t.provider==="github"&&Je(),record:r};let o=us(t);if(o){let i=no(t,o,"seeded");return{identity:t,policy:o,source:"seeded",provider:t.provider,prAvailable:ro({provider:t.provider}),record:i}}return{identity:t,source:"unknown",provider:t.provider,prAvailable:ro({provider:t.provider})}}checkRepoPolicyForLaunch(e,t){let r=t??_.defaultWorktreeStrategy??"off",o=this.resolveRepoPolicy(e);return r==="off"?{ok:!0,resolution:o}:o.source==="none"?{ok:!0,resolution:o}:o.source==="unknown"&&o.identity?{ok:!1,text:oo(o.identity,r,o.prAvailable)}:{ok:!0,resolution:o}}getRepoPolicyRecordForWorkdir(e){return this.resolveRepoPolicy(e).record}listRepoPolicies(){return this.store.listRepoPolicies()}cleanupRepoPolicies(){let e=[...this.store.cleanupRepoPolicies()],t=[];for(let r of this.store.listRepoPolicies()){let o=lt(r.repoRoot);!o||o.key===r.key||t.push(r.key)}return e.push(...this.store.removeRepoPolicies(t)),e.sort((r,o)=>r.repoRoot.localeCompare(o.repoRoot)||r.key.localeCompare(o.key))}setRepoPolicy(e,t){let r=lt(e);if(r)return this.store.setRepoPolicy(no(r,t,"stored"))}resetRepoPolicy(e){let t=lt(e);return t?this.store.resetRepoPolicy(t.key):!1}requestRepoPolicyForLaunch(e){let t=e.worktreeStrategy??_.defaultWorktreeStrategy??"off",r=this.resolveRepoPolicy(e.workdir);if(!r.identity)return`Error: ${e.workdir} is not a git repository.`;let o=oo(r.identity,t,r.prAvailable);if(!e.route?.provider||!e.route.target)return o;let i=`repo-policy:${r.identity.key}`,s=this.interactions.getRepoPolicyChoiceButtons({choiceId:i,route:e.route,repoRoot:r.identity.repoRoot,launchPrompt:e.prompt,launchWorkdir:e.workdir,launchName:e.name,launchModel:e.model,launchReasoningEffort:e.reasoningEffort,launchFastMode:e.fastMode,launchSystemPrompt:e.systemPrompt,launchAllowedTools:e.allowedTools,launchResumeSessionId:e.resumeSessionId,launchResumedFromSessionName:e.resumedFromSessionName,launchResumeWorktreeFrom:e.resumeWorktreeFrom,launchSessionIdOverride:e.sessionIdOverride,launchClearedPersistedCodexResume:e.clearedPersistedCodexResume,launchForkSession:e.forkSession,launchForceNewSession:e.forceNewSession,launchPermissionMode:e.permissionMode,launchPlanApproval:e.planApproval,launchHarness:e.harness,launchWorktreeStrategy:t,launchWorktreeBaseBranch:e.worktreeBaseBranch,launchWorktreePrTargetRepo:e.worktreePrTargetRepo,launchOriginAgentId:e.originAgentId,prAvailable:r.prAvailable}),a=ff(e,t);return this.notifications.dispatch(this.buildRoutingProxy({id:i,name:"repo-policy",route:e.route}),{label:"repo-policy-choice",idempotencyKey:`repo-policy-choice:${r.identity.key}:${t}:${a}`,userMessage:[o,"","After you choose a policy, OpenClaw Code Agent will continue this launch automatically."].join(`
|
|
217
|
+
`),notifyUser:"always",buttons:t==="ask"&&!o?this.deps.getPlanApprovalButtons(e.id,{...e,planDecisionVersion:r}):void 0})}async emitWaitingForInput(e){let t=e.pendingInputState?.questions,r=t?.[e.pendingInputState?.activeQuestionIndex??0],o=e.pendingInputState?.requestId,i=al(e.pendingInputState),s=o?[o,i].filter(Boolean).join(":"):void 0,a=s?`pending-input:${s}`:void 0;if(!this.deps.debounceWaitingEvent(e.id,a))return;let l=e.pendingPlanApproval?this.deps.resolvePlanApprovalMode(e):void 0,d=e.actionablePlanDecisionVersion??e.planDecisionVersion,u=e.pendingPlanApproval&&l==="ask"&&Rt(e,d),m=e.pendingInputState?.promptText?.trim()||void 0,g=e.pendingPlanApproval?void 0:this.deps.getOutputPreview(e),S=!e.pendingPlanApproval&&m?m:!e.pendingPlanApproval&&g!==void 0?g:this.deps.getOutputPreview(e,e.pendingPlanApproval&&l!=="delegate"?Number.POSITIVE_INFINITY:void 0),y=e.pendingInputState?.options.map(v=>({label:v}))??[],c=!t||t.length===0?y:r&&r.options.length>0&&r.options.length<=6&&!r.options.some(v=>v.isOther)&&!r.multiSelect?r.options:t.length===1&&r&&r.options.length===0?y:[],h=e.pendingPlanApproval&&l==="ask"&&!u?this.deps.getPlanApprovalButtons(e.id,{...e,planDecisionVersion:d}):!e.pendingPlanApproval&&c.length?this.deps.getQuestionButtons(e.id,c,{requestId:e.pendingInputState?.requestId,questionId:r?.id}):void 0,P=jo(e,d),b=r?of(r):[],A=r?rf({question:r,index:e.pendingInputState?.activeQuestionIndex??0,total:t?.length??1,optionDescriptions:b}):m,I=!e.pendingPlanApproval&&this.deps.questionContextSummaryProvider?await il({sessionName:e.name,question:A??S,context:g,provider:this.deps.questionContextSummaryProvider}):void 0,R=No({session:e,preview:S,questionText:e.pendingPlanApproval?void 0:A,questionContextPreview:g,questionContextSummary:I,planArtifact:P,originThreadLine:this.deps.originThreadLine(e),planApprovalMode:l,planApprovalButtons:h,questionButtons:e.pendingPlanApproval?void 0:h}),f=R.planReviewSummary??S;if(R.label==="plan-approval"&&l==="ask"&&u){this.deps.dispatchSessionNotification(e,{label:R.label,idempotencyKey:`plan-approval:${e.id}:v${d??"unknown"}:canonical`,userMessage:R.userMessage,userMessages:R.userMessages,notifyUser:"never",buttons:R.buttons,wakeMessage:R.wakeMessage});return}if(R.label==="plan-approval"&&l==="ask"){this.deps.dispatchSessionNotification(e,{label:R.label,idempotencyKey:`plan-approval:${e.id}:v${d??"unknown"}:canonical`,userMessage:R.userMessage,userMessages:R.userMessages,notifyUser:"always",buttons:R.buttons,hooks:{onNotifyStarted:()=>{this.deps.updatePersistedSession(e.id,{approvalPromptRequiredVersion:d,approvalPromptVersion:d,approvalPromptStatus:"sending",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"canonical_buttons",approvalPromptLastAttemptAt:new Date().toISOString()})},onNotifySucceeded:()=>{this.deps.updatePersistedSession(e.id,{canonicalPlanPromptVersion:d,approvalPromptRequiredVersion:d,approvalPromptVersion:d,approvalPromptStatus:"delivered",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"canonical_buttons",approvalPromptDeliveredAt:new Date().toISOString(),approvalPromptFailedAt:void 0})},onNotifyFailed:()=>{this.deps.updatePersistedSession(e.id,{approvalPromptRequiredVersion:d,approvalPromptVersion:d,approvalPromptStatus:"failed",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"canonical_buttons",approvalPromptFailedAt:new Date().toISOString()})}},shouldDispatch:()=>At(e,d),onUserNotifyFailed:()=>this.dispatchPlanApprovalFallback(e,d,f),wakeMessageOnNotifySuccess:Je(e,d)});return}if(R.label==="plan-approval"){this.deps.dispatchSessionNotification(e,{label:R.label,idempotencyKey:`plan-approval:${e.id}:v${d??"unknown"}:wake-only`,userMessage:R.userMessage,wakeMessage:R.wakeMessage,notifyUser:"never",buttons:R.buttons});return}this.deps.dispatchSessionNotification(e,{label:R.label,idempotencyKey:`waiting:${e.id}:${s??`${R.label}:${R.userMessage}`}`,userMessage:R.userMessage,notifyUser:"always",buttons:R.buttons,shouldDispatch:o?()=>sf(e,o,i):void 0,wakeMessageOnNotifyFailed:R.wakeMessage})}emitTurnComplete(e){`${e.id}${e.result?.num_turns??0}${e.worktreeStrategy??"none"}`;let t=Io({session:e,originThreadLine:this.deps.originThreadLine(e),preview:this.deps.getOutputPreview(e)});this.deps.dispatchSessionNotification(e,{label:"turn-complete",idempotencyKey:`turn-complete:${e.id}:${af(e)}`,userMessage:t.userMessage,wakeMessage:t.wakeMessage,notifyUser:"always",onUserNotifyFailed:()=>{`${e.id}`,this.deps.shouldEmitTerminalWake(e)&&this.emitCompleted(e)}})}emitCompleted(e){let t=this.deps.getOutputPreview(e),r=this.shouldRequestCompletionFollowup(e),o=bo({session:e,originThreadLine:this.deps.originThreadLine(e),preview:t}),i=sl(e),s=`terminal:${e.id}:${i}`,a;this.deps.dispatchSessionNotification(e,{label:"completed",idempotencyKey:`terminal-completed:${e.id}:${i}`,userMessage:o.userMessage,notifyUser:"always",completionSummary:{required:r,producer:"terminal",outcomeKey:s},completionWakeSummaryRequired:r,completionWakeOutcomeKey:s,requireDirectUserNotification:!0,wakeMessageOnNotifySuccess:r?o.wakeMessageOnNotifySuccess:void 0,wakeMessageOnNotifyFailed:r?o.wakeMessageOnNotifyFailed:void 0,hooks:{onNotifySucceeded:()=>{a=!0,this.logCompletionWakeDiagnostic({session:e,event:"completion_notify_succeeded",canonicalStatusDelivered:a,followupSummaryRequired:r})},onNotifyFailed:()=>{a=!1,this.logCompletionWakeDiagnostic({session:e,event:"completion_notify_failed",canonicalStatusDelivered:a,followupSummaryRequired:r})},onWakeSucceeded:()=>{this.logCompletionWakeDiagnostic({session:e,event:"completion_wake_succeeded",canonicalStatusDelivered:a,followupSummaryRequired:r})}}})}emitFailed(e,t,r){let o=Ao({session:e,originThreadLine:this.deps.originThreadLine(e),errorSummary:t,preview:this.deps.getOutputPreview(e),worktreeAutoCleaned:r,failedButtons:this.deps.getResumeButtons(e.id,e)});this.deps.dispatchSessionNotification(e,{label:"failed",idempotencyKey:`terminal-failed:${e.id}:${sl(e)}:${t}`,userMessage:o.userMessage,wakeMessage:o.wakeMessage,notifyUser:"always",buttons:o.buttons})}};import{existsSync as lf}from"fs";var vr=class{constructor(e){this.deps=e}deps;async dismissWorktree(e){let t=this.deps.getPersistedSession(e),r=this.deps.resolveActiveSession(e),o=r??t;if(!o)return`Error: Session "${e}" not found.`;let i=r?.worktreePath??t?.worktreePath,s=this.deps.resolveWorktreeRepoDir(r?.originalWorkdir??t?.workdir,i),a=r?.worktreeBranch??t?.worktreeBranch,l=r?.name??t?.name??e;if(!s)return`Error: No workdir found for session "${e}".`;let d=L(o);!d&&i&&lf(i)&&B(s,i,{destructive:!0}),a&&H(s,a);for(let m of z(r??t))this.deps.updatePersistedSession(m,{worktreeDisposition:"dismissed",worktreeDismissedAt:new Date().toISOString(),pendingWorktreeDecisionSince:void 0,worktreeState:"dismissed",lifecycle:"terminal",worktreePath:void 0,worktreeBranch:void 0,worktreeLifecycle:{state:"dismissed",updatedAt:new Date().toISOString(),resolvedAt:new Date().toISOString(),resolutionSource:"dismiss",baseBranch:t?.worktreeLifecycle?.baseBranch,targetRepo:t?.worktreePrTargetRepo,pushRemote:t?.worktreePushRemote}});let u=d?`🗑️ [${l}] Branch \`${a??"unknown"}\` dismissed. Native backend worktree released for backend cleanup.`:`🗑️ [${l}] Branch \`${a??"unknown"}\` dismissed and permanently deleted.`;return this.deps.dispatchNotification(this.deps.buildRoutingProxy({id:D(r??t??{id:e})??e,sessionId:t?.sessionId,harnessSessionId:r?.harnessSessionId??t?.harnessSessionId,backendRef:r?.backendRef??t?.backendRef,route:r?.route??t?.route}),{label:"worktree-dismissed",idempotencyKey:`worktree-dismissed:${D(r??t??{id:e})??e}:${a??"unknown"}`,userMessage:u,notifyUser:"always"}),u}snoozeWorktreeDecision(e,t={}){let r=this.deps.getPersistedSession(e);if(!r)return`Error: Session "${e}" not found.`;let o=Date.now(),i=new Date(o+1440*60*1e3).toISOString();for(let l of z(r))this.deps.updatePersistedSession(l,{worktreeDecisionSnoozedUntil:i,lastWorktreeReminderAt:new Date(o).toISOString()});let a=`⏭️ Reminder snoozed 24h for \`${r.worktreeBranch??"unknown"}\` (session: ${r.name})`;return t.notifyUser!==!1&&this.deps.dispatchNotification(this.deps.buildRoutingProxy({id:D(r)??C(r)??r.harnessSessionId,sessionId:r.sessionId,harnessSessionId:r.harnessSessionId,backendRef:r.backendRef,route:r.route}),{label:"worktree-snoozed",idempotencyKey:`worktree-snoozed:${D(r)??C(r)??r.harnessSessionId}:${i}`,userMessage:a,notifyUser:"always"}),a}};var ll=new Set(["starting","running"]);function dl(n,e){JSON.stringify({component:"SessionRuntimeRegistry",event:n,at:new Date().toISOString(),...e})}function ul(n){return{sessionId:n.id,name:n.name,status:n.status,lifecycle:n.lifecycle,runtimeState:n.runtimeState,harness:n.harnessName,hasHarnessSessionId:!!n.harnessSessionId,backendRefKind:n.backendRef?.kind,hasBackendConversationId:!!n.backendRef?.conversationId,hasBackendRunId:!!n.backendRef?.runId,hasBackendWorktreeId:!!n.backendRef?.worktreeId,hasBackendWorktreePath:!!n.backendRef?.worktreePath}}var Pr=class{sessions=new Map;activeSessionCount(){return[...this.sessions.values()].filter(e=>ll.has(e.status)).length}uniqueName(e){let t=new Set([...this.sessions.values()].filter(o=>ll.has(o.status)).map(o=>o.name));if(!t.has(e))return e;let r=2;for(;t.has(`${e}-${r}`);)r+=1;return`${e}-${r}`}add(e,t="spawn"){dl("runtime.add",{reason:t,...ul(e),activeCountBefore:this.activeSessionCount()}),this.sessions.set(e.id,e)}remove(e,t="unknown"){let r=this.sessions.get(e);dl("runtime.remove",{reason:t,sessionId:e,existed:!!r,...r?ul(r):{},activeCountBefore:this.activeSessionCount()}),this.sessions.delete(e)}get(e){return this.sessions.get(e)}list(){return[...this.sessions.values()]}};var wr=class{constructor(e){this.deps=e}deps;initializeSession(e,t,r,o={}){if(this.deps.hydrateSpawnedSession(e,t,r),r.taskLifecycle?.create(e),e.on("statusChange",(i,s)=>{s==="running"?(e.harnessSessionId&&this.deps.markRunning(e),r.taskLifecycle?.progress(e)):(s==="completed"||s==="failed"||s==="killed")&&(r.taskLifecycle?.finalize(e),this.deps.handleTerminal(e).catch(a=>{console.error(`[SessionRuntimeBootstrap] handleTerminal threw for session ${e.id}:`,a)}))}),e.on("lifecycleChange",(i,s)=>{r.taskLifecycle?.progress(e)}),e.on("turnEnd",(i,s)=>{this.deps.handleTurnEnd(e,s).catch(a=>{console.error(`[SessionRuntimeBootstrap] handleTurnEnd threw for session ${e.id}:`,a)})}),o.startAfter?o.startAfter.then(()=>e.start()).catch(i=>{console.error(`[SessionRuntimeBootstrap] deferred start threw for session ${e.id}:`,i)}):e.start(),o.notifyLaunch!==!1){let i=this.buildLaunchNotification(e);this.deps.notifySession(e,i.text,i.label,i.idempotencyKey)}return e}buildLaunchNotification(e){let t=this.deps.formatLaunchWorkdirLabel(e),r=ge({harness:e.harnessName,model:e.model,reasoningEffort:e.reasoningEffort})??"default";return e.resumeSessionId?{text:tn({sessionName:e.name,resumedFromSessionName:e.resumedFromSessionName,workdirLabel:t,harnessLabel:r}),label:"resumed-launch",idempotencyKey:`resumed-launch:${e.id}:${e.startedAt}:${e.resumeSessionId}`}:{text:`🚀 [${e.name}] Launched | ${t} | ${r}`,label:"launch"}}};var br=class{buildNoChangeNotification(e){let{session:t,nativeBackendWorktree:r,cleanupSucceeded:o,worktreePath:i,worktreeBranch:s,preview:a,originThreadLine:l,preservedSummary:d,remoteOutcome:u}=e,m=d?"preserved":o?"cleaned":"cleanup-failed",g=[s?.trim()||"unknown-branch",i.trim()||"unknown-worktree",t.startedAt].join(":"),S=d??(o?r?"native backend worktree released for backend cleanup":"worktree cleaned up":`cleanup failed; worktree still exists at ${i}`),y=ye({costUsd:t.costUsd,duration:typeof t.completedAt=="number"?t.completedAt-t.startedAt:void 0,harnessName:t.harnessName,model:t.model,reasoningEffort:t.reasoningEffort}),c=u==="pr-updated"?"PR updated; no local worktree changes remained to merge":u==="pr-opened"?"PR opened; no local worktree changes remained to merge":"Session completed with no worktree changes to merge",h=u==="pr-updated"?`PR updated; no local worktree changes remained to merge — ${S}`:u==="pr-opened"?`PR opened; no local worktree changes remained to merge — ${S}`:void 0,P=u==="pr-updated"?"PR updated; no local worktree changes remained to merge":u==="pr-opened"?"PR opened; no local worktree changes remained to merge":"Session completed with no worktree changes to merge",b=u==="pr-updated"?"Coding agent session updated a PR; no local worktree changes remained to merge.":u==="pr-opened"?"Coding agent session opened a PR; no local worktree changes remained to merge.":void 0;return{label:d?"worktree-no-changes-preserved":o?"worktree-no-changes":"worktree-no-changes-cleanup-failed",idempotencyKey:`worktree-no-change:${t.id}:${m}:${g}`,userMessage:d?`ℹ️ [${t.name}] ${c} — ${d}${y}`:o?r?`ℹ️ [${t.name}] ${c} — native backend worktree released for backend cleanup${y}`:`ℹ️ [${t.name}] ${c} — worktree cleaned up${y}`:`⚠️ [${t.name}] ${P}, but worktree cleanup failed. Worktree still exists at ${i}${y}`,wakeMessage:Co({sessionName:t.name,sessionId:t.id,headline:b,cleanupSummary:h??S,preview:a,originThreadLine:l,requestedPermissionMode:t.requestedPermissionMode,currentPermissionMode:t.currentPermissionMode,approvalExecutionState:t.approvalExecutionState,approvalState:t.approvalState,planApproval:t.planApproval,approvalPromptStatus:t.approvalPromptStatus,approvalPromptMessageKind:t.approvalPromptMessageKind,approvalPromptDeliveredAt:t.approvalPromptDeliveredAt}),notifyUser:"always"}}buildAskNotification(e){let{session:t,branchName:r,baseBranch:o,diffSummary:i,buttons:s,summaryLines:a=[],policyReason:l}=e,d=i.commitMessages.slice(0,5).map(g=>`• ${g.hash} ${g.message} (${g.author})`),u=i.commits>5?`...and ${i.commits-5} more`:"",m=t.worktreePrTargetRepo?`Branch: \`${r}\` → \`${o}\` | PR target: ${t.worktreePrTargetRepo}`:`Branch: \`${r}\` → \`${o}\``;return{label:"worktree-merge-ask",idempotencyKey:["worktree-decision",t.id,r,o,i.commits,i.commitMessages.map(g=>g.hash).join(",")].join(":"),userMessage:[`🔀 Worktree decision required for session \`${t.name}\``,"",m,`Commits: ${i.commits} | Files: ${i.filesChanged} | +${i.insertions} / -${i.deletions}`,"",...a.length>0?["Summary:",...a.map(g=>`- ${g}`),""]:[],...l?[`Policy: ${l}`,""]:[],"Recent commits:",...d,...u?[u]:[],"",`⚠️ Discard will permanently delete branch \`${r}\` and all local changes. This cannot be undone.`].join(`
|
|
218
|
+
`),notifyUser:"always",buttons:s,wakeMessageOnNotifySuccess:["Worktree decision buttons delivered to the user.",`Session: ${t.name} | ID: ${t.id}`,m,"Wait for their button callback — do NOT act on this worktree yourself."].join(`
|
|
219
|
+
`),wakeMessageOnNotifyFailed:[`🔀 Worktree decision required for session \`${t.name}\``,"",m,`Commits: ${i.commits} | Files: ${i.filesChanged} | +${i.insertions} / -${i.deletions}`].join(`
|
|
220
|
+
`)}}buildDelegateNotification(e){let{session:t,branchName:r,baseBranch:o,diffSummary:i,policyReason:s,allowedActions:a,originThreadLine:l}=e,d=i.commitMessages.slice(0,5).map(g=>`• ${g.hash} ${g.message} (${g.author})`),u=i.commits>5?`...and ${i.commits-5} more`:void 0,m=t.prompt?t.prompt.slice(0,500):"(no prompt)";return{label:"worktree-delegate",idempotencyKey:["worktree-delegate",t.id,r,o,i.commits,i.commitMessages.map(g=>g.hash).join(",")].join(":"),wakeMessage:To({sessionName:t.name,sessionId:t.id,branchName:r,baseBranch:o,promptSnippet:m,commitLines:d,moreNote:u,originThreadLine:l,diffSummary:i,allowedActions:a,policyReason:s}),notifyUser:"never"}}};function cl(n,e=1e3){let t=!Number.isFinite(e),r=typeof n.getOutput=="function"?n.getOutput():[],o=t?r.join(`
|
|
221
|
+
`):df(n);return t?o:o.length>e?pl(n)?Oi(o,e):Wi(o,e):o}function df(n){let e=typeof n.getOutput=="function"?n.getOutput():[],t=e.join(`
|
|
222
|
+
`).trim();return t?pl(n)?uf(t)??t:e.slice(-20).join(`
|
|
223
|
+
`):""}function pl(n){if(n.status==="completed"||n.killReason==="done")return!0;let e=n;return e.approvalState==="approved"||e.planModeApproved===!0||(e.planDecisionVersion??0)>0}function uf(n){let t=n.split(/\n\s*\n+/).map(r=>r.trim()).filter(r=>/[A-Za-z0-9]/.test(r)).at(-1);if(t)return t.length>=120||t.includes(`
|
|
224
|
+
`)?t:void 0}import{unlinkSync as cf}from"fs";var Rr=class n{timers=new Map;disposed=!1;static MAX_DELAY_MS=2147483647;schedule(e,t,r){if(this.disposed||(this.cancel(e),!Number.isFinite(t)))return;let o=Math.max(0,t-Date.now()),i=setTimeout(()=>{if(this.timers.delete(e),!this.disposed){if(t-Date.now()>0){this.schedule(e,t,r);return}r()}},Math.min(o,n.MAX_DELAY_MS));i.unref?.(),this.timers.set(e,i)}cancel(e){let t=this.timers.get(e);t&&(clearTimeout(t),this.timers.delete(e))}cancelPrefix(e){for(let t of this.timers.keys())t.startsWith(e)&&this.cancel(t)}dispose(){this.disposed=!0;for(let e of this.timers.values())clearTimeout(e);this.timers.clear()}};var ml=10080*60*1e3,pf=300*1e3,fl="tmp-output:cleanup",mf=60*1e3;function ff(n){return n instanceof Error?n.message:String(n)}var Ar=class{constructor(e){this.deps=e}deps;scheduler=new Rr;lastTmpOutputCleanupAttemptAt;schedule(e,t,r){this.scheduler.schedule(e,t,r)}cancel(e){this.scheduler.cancel(e)}cancelPrefix(e){this.scheduler.cancelPrefix(e)}bootstrapMaintenanceSchedules(){let e=Date.now();this.runTmpOutputCleanup(e);for(let t of this.deps.store.listPersistedSessions())this.syncPersistedSessionMaintenance(t);this.syncActionTokenExpiryDeadline(),this.deps.store.cleanupOrphanOutputFiles(),this.syncTmpOutputCleanupDeadline(e)}syncRuntimeGcDeadline(e){if(!e.completedAt)return;let t=this.runtimeGcKey(e.id);this.schedule(t,e.completedAt+this.runtimeGcMaxAgeMs(),()=>{let r=this.deps.sessions.get(e.id);if(!r||!r.completedAt)return;let o=this.runtimeGcMaxAgeMs();if(!this.deps.store.shouldGcActiveSession(r,Date.now(),o)){this.syncRuntimeGcDeadline(r);return}this.deps.removeRuntimeSession(e.id,"runtime-gc"),this.deps.persistSession(r,{scheduleRuntimeGc:!1}),this.deps.clearRuntimeSessionState(e.id)})}cancelRuntimeGc(e){this.cancel(this.runtimeGcKey(e))}cancelPersistedMaintenance(e){let t=this.persistedMaintenanceRef(e);t&&this.cancelPrefix(`persisted:${t}:`)}syncPersistedSessionMaintenance(e){let t=this.persistedMaintenanceRef(e);if(!t)return;this.cancel(this.persistedMaintenanceKey(t,"worktree-reminder"));let r=this.deps.reminders.getNextReminderAt(e);r!=null?this.schedulePersistedWorktreeReminder(t,r):this.deps.reminders.clearResolvedReminderState(e),this.cancel(this.persistedMaintenanceKey(t,"worktree-retention"));let o=We(e,{activeSession:!1,includePrSync:e.worktreeLifecycle?.state==="pr_open"||!!e.worktreePrUrl}),i=this.resolvedAtIso(e),s=this.isLegacyResolvedWorktree(e);if((o.cleanupSafe||s)&&typeof i=="string"){let a=new Date(i).getTime();Number.isFinite(a)&&this.schedule(this.persistedMaintenanceKey(t,"worktree-retention"),a+ml,()=>{let l=this.deps.store.getPersistedSession(t);l&&this.reconcileResolvedWorktreeRetention(l,Date.now())})}}reconcileResolvedWorktreeRetention(e,t){let r=We(e,{activeSession:!1,includePrSync:e.worktreeLifecycle?.state==="pr_open"||!!e.worktreePrUrl}),o=this.resolvedAtIso(e),i=o?new Date(o).getTime():0,s=this.legacyResolvedWorktreeState(e);if(!(!r.cleanupSafe&&!s||!o||!Number.isFinite(i)||t-i<ml))try{if(!e.worktreePath&&!L(e))return;let a=this.deps.resolveWorktreeRepoDir(e.workdir,e.worktreePath);if(!a)return;let l=L(e)?!1:B(a,e.worktreePath);if(!L(e)&&!l)return;for(let d of z(e))this.deps.updatePersistedSession(d,{worktreePath:void 0,worktreeBranch:void 0,worktreeState:"none",pendingWorktreeDecisionSince:void 0,lastWorktreeReminderAt:void 0,worktreeDecisionSnoozedUntil:void 0,worktreeLifecycle:{...e.worktreeLifecycle??r.lifecycle,state:r.cleanupSafe?r.derivedState:s??r.derivedState,updatedAt:new Date(t).toISOString(),resolvedAt:e.worktreeLifecycle?.resolvedAt??o,resolutionSource:e.worktreeLifecycle?.resolutionSource??"maintenance",notes:r.reasons}})}catch(a){`${e.worktreePath}${ff(a)}`}}syncActionTokenExpiryDeadline(){let e="tokens:expiry";this.cancel(e);let t=this.deps.store.getNextActionTokenExpiry();t!=null&&this.schedule(e,t,()=>{this.deps.store.purgeExpiredActionTokens(Date.now())||this.syncActionTokenExpiryDeadline()})}syncTmpOutputCleanupDeadline(e=Date.now()){this.cancel(fl);let t=this.deps.store.getNextTmpOutputCleanupAt(e);if(t==null){this.lastTmpOutputCleanupAttemptAt=void 0;return}t>e&&(this.lastTmpOutputCleanupAttemptAt=void 0),this.schedule(fl,this.getTmpOutputCleanupScheduleAt(t,e),()=>{let r=Date.now();this.runTmpOutputCleanup(r),this.syncTmpOutputCleanupDeadline(r)})}enforcePersistedRetention(){let e=this.deps.store.evictOldestPersisted(this.deps.getMaxPersistedSessions());for(let t of e)this.cancelPersistedMaintenance(t),t.sessionId&&this.cancelRuntimeGc(t.sessionId),this.cleanupOutputPathIfUnreferenced(t.outputPath)}dispose(){this.scheduler.dispose()}persistedMaintenanceRef(e){return e.sessionId??C(e)??e.harnessSessionId}runtimeGcKey(e){return`runtime-gc:${e}`}persistedMaintenanceKey(e,t){return`persisted:${e}:${t}`}runtimeGcMaxAgeMs(){return(_.sessionGcAgeMinutes??1440)*6e4}runTmpOutputCleanup(e){this.lastTmpOutputCleanupAttemptAt=e,this.deps.store.cleanupTmpOutputFiles(e)}getTmpOutputCleanupScheduleAt(e,t){if(e>t||this.lastTmpOutputCleanupAttemptAt==null)return e;let r=this.lastTmpOutputCleanupAttemptAt+mf;return r>t?r:e}schedulePersistedWorktreeReminder(e,t){let r=this.persistedMaintenanceKey(e,"worktree-reminder");this.schedule(r,t,()=>{let o=this.deps.store.getPersistedSession(e);if(!o||this.deps.reminders.sendReminderIfDue(o,Date.now()))return;let s=this.deps.reminders.getNextReminderAt(o);s!=null&&this.schedulePersistedWorktreeReminder(e,Math.max(s,Date.now()+pf))})}cleanupOutputPathIfUnreferenced(e){if(!(!e||this.deps.store.hasOutputPathReference(e)))try{cf(e)}catch{}}resolvedAtIso(e){return e.worktreeLifecycle?.resolvedAt??e.worktreeMergedAt??e.worktreeDismissedAt??(e.completedAt?new Date(e.completedAt).toISOString():void 0)}isLegacyResolvedWorktree(e){return this.legacyResolvedWorktreeState(e)!=null}legacyResolvedWorktreeState(e){if(e.worktreeMerged===!0||e.worktreeDisposition==="merged"||e.worktreeState==="merged")return"merged";if(e.worktreeDisposition==="dismissed"||e.worktreeState==="dismissed")return"dismissed";if(e.worktreeDisposition==="no-change-cleaned")return"no_change"}};var gl=new Set(["completed","failed","killed"]),gf=new Set(["starting","running"]),hf=5e3;function yf(n,e){return hl("sha256").update(JSON.stringify({route:n.route?{provider:n.route.provider,target:n.route.target,accountId:n.route.accountId,threadId:n.route.threadId,sessionKey:n.route.sessionKey}:void 0,prompt:n.prompt,workdir:n.workdir,name:n.name,model:n.model,reasoningEffort:n.reasoningEffort,fastMode:n.fastMode,systemPrompt:n.systemPrompt,allowedTools:n.allowedTools?[...n.allowedTools].sort():n.allowedTools,resumeSessionId:n.resumeSessionId,resumedFromSessionName:n.resumedFromSessionName,resumeWorktreeFrom:n.resumeWorktreeFrom,sessionIdOverride:n.sessionIdOverride,clearedPersistedCodexResume:n.clearedPersistedCodexResume,forkSession:n.forkSession,forceNewSession:n.forceNewSession,permissionMode:n.permissionMode,planApproval:n.planApproval,harness:n.harness,worktreeStrategy:n.worktreeStrategy,effectiveWorktreeStrategy:e,worktreeBaseBranch:n.worktreeBaseBranch,worktreePrTargetRepo:n.worktreePrTargetRepo,originAgentId:n.originAgentId})).digest("hex").slice(0,16)}function Sf(n){return hl("sha256").update(JSON.stringify({route:n.route?{provider:n.route.provider,target:n.route.target,accountId:n.route.accountId,threadId:n.route.threadId,sessionKey:n.route.sessionKey}:void 0,prompt:n.launchPrompt,workdir:n.launchWorkdir,name:n.launchName,model:n.launchModel,reasoningEffort:n.launchReasoningEffort,fastMode:n.launchFastMode,systemPrompt:n.launchSystemPrompt,allowedTools:n.launchAllowedTools?[...n.launchAllowedTools].sort():n.launchAllowedTools,resumeSessionId:n.launchResumeSessionId,resumedFromSessionName:n.launchResumedFromSessionName,resumeWorktreeFrom:n.launchResumeWorktreeFrom,sessionIdOverride:n.launchSessionIdOverride,clearedPersistedCodexResume:n.launchClearedPersistedCodexResume,forkSession:n.launchForkSession,forceNewSession:n.launchForceNewSession,permissionMode:n.launchPermissionMode,planApproval:n.launchPlanApproval,harness:n.launchHarness,worktreeStrategy:n.launchWorktreeStrategy,worktreeBaseBranch:n.launchWorktreeBaseBranch,worktreePrTargetRepo:n.launchWorktreePrTargetRepo,originAgentId:n.launchOriginAgentId})).digest("hex").slice(0,16)}var Ir=class n{registry;sessions;maxSessions;maxPersistedSessions;lastWaitingEventTimestamps=new Map;lastTurnCompleteMarkers=new Map;lastTerminalWakeMarkers=new Map;mergeQueue=new Ye;pendingAskUserQuestions=new Map;store;metrics;wakeDispatcher;interactions;notifications;worktrees;questions;lifecycle;restore;stateSync;references;worktreeStrategy;worktreeDecisions;runtimeBootstrap;worktreeMessages;maintenance;pendingPlanResumeClaims=new Map;constructor(e=20,t=50,r={}){this.maxSessions=e,this.maxPersistedSessions=t;let o=n.createServiceBundle(this,r);this.registry=o.registry,this.sessions=o.sessions,this.store=o.store,this.metrics=o.metrics,this.wakeDispatcher=o.wakeDispatcher,this.interactions=o.interactions,this.notifications=o.notifications,this.worktrees=o.worktrees,this.questions=o.questions,this.lifecycle=o.lifecycle,this.restore=o.restore,this.stateSync=o.stateSync,this.references=o.references,this.worktreeStrategy=o.worktreeStrategy,this.worktreeDecisions=o.worktreeDecisions,this.runtimeBootstrap=o.runtimeBootstrap,this.worktreeMessages=o.worktreeMessages,this.maintenance=o.maintenance,this.reconcilePersistedTaskFlowMirrors()}static createServiceBundle(e,t){let r=new Pr,o=r.sessions,i=new rr(t.store),s=new or,a=new lr,l=new dr(i.actionTokenStore,et),d=new Gn(o,i),u=new Un({store:i,sessions:o,resolveSession:f=>d.resolveActive(f)}),m=new mr(a,(f,v)=>u.applySessionPatch(f,v),{getPersistedSession:f=>i.getPersistedSession(f)}),g=new fr,S=new Vn(f=>i.getPersistedSession(f)),y=new br,c=new Jn({shouldRunWorktreeStrategy:f=>e.shouldRunWorktreeStrategy(f),isAlreadyMerged:f=>e.isAlreadyMerged(f),resolveWorktreeRepoDir:(f,v)=>e.resolveWorktreeRepoDir(f,v),getWorktreeCompletionState:(f,v,T,F)=>e.getWorktreeCompletionState(f,v,T,F),updatePersistedSession:(f,v)=>e.updatePersistedSession(f,v),getPersistedSession:f=>i.getPersistedSession(f),dispatchSessionNotification:(f,v)=>e.dispatchSessionNotification(f,v),getOutputPreview:(f,v)=>e.getOutputPreview(f,v),originThreadLine:f=>e.originThreadLine(f),getWorktreeDecisionButtons:f=>e.getWorktreeDecisionButtons(f),getPolicyAwareWorktreeDecisionButtons:(f,v,T)=>e.getWorktreeDecisionButtons(f,v,T),makeOpenPrButton:f=>e.makeActionButton(f,"worktree-create-pr","Open PR"),isPrAvailable:f=>e.resolveRepoPolicy(f).prAvailable,hasOpenPrForBranch:(f,v,T)=>{let F=it(f,v,T);return F.exists&&F.state==="open"},getPrStatusForBranch:(f,v,T)=>it(f,v,T),getPrStatusForUrl:(f,v,T)=>Lt(f,v,T),resolveRepoPolicy:f=>e.resolveRepoPolicy(f),worktreeSummaryProvider:t.worktreeSummaryProvider,worktreeMessages:y,enqueueMerge:(f,v,T)=>e.enqueueMerge(f,v,T),mergeBranch:Ft,spawnConflictResolver:async({session:f,worktreePath:v,prompt:T})=>e.spawn({prompt:T,workdir:v,name:`${f.name}-conflict-resolver`,harness:f.harnessName||Y(),model:f.model,reasoningEffort:f.reasoningEffort,fastMode:f.fastMode,permissionMode:"bypassPermissions",multiTurn:!0,worktreeStrategy:"off",autoMergeParentSessionId:f.id,route:f.route,originChannel:f.originChannel,originThreadId:f.originThreadId,originAgentId:f.originAgentId,originSessionKey:f.originSessionKey},{notifyLaunch:!1}),runAutoPr:async(f,v)=>{let{makeAgentPrTool:T}=await import("./chunks/agent-pr.js");return{success:(await T().execute("auto-pr",{session:f.id,base_branch:v}))?.meta?.success===!0}}}),h=new gr(e.pendingAskUserQuestions,(f,v)=>e.dispatchSessionNotification(f,v),f=>{e.clearWaitingTimestampsForSession(f)},(f,v,T)=>l.getQuestionButtons(f,v,T)),P=new hr(f=>e.buildRoutingProxy(f),(f,v)=>m.dispatch(f,v),(f,v)=>e.updatePersistedSession(f,v),(f,v)=>e.getPolicyAwareWorktreeDecisionButtons(f,{},void 0,v)),b=new Ar({store:i,sessions:o,reminders:P,removeRuntimeSession:(f,v)=>r.remove(f,v),persistSession:(f,v)=>e.persistSession(f,v),clearRuntimeSessionState:f=>{e.clearWaitingTimestampsForSession(f),e.lastTurnCompleteMarkers.delete(f),e.lastTerminalWakeMarkers.delete(f)},resolveWorktreeRepoDir:(f,v)=>e.resolveWorktreeRepoDir(f,v),updatePersistedSession:(f,v)=>e.updatePersistedSession(f,v),getMaxPersistedSessions:()=>e.maxPersistedSessions});i.onActionTokensChanged(()=>e.syncActionTokenExpiryDeadline());let A=new kr({persistSession:f=>e.persistSession(f),clearWaitingTimestamp:f=>{e.clearWaitingTimestampsForSession(f)},handleWorktreeStrategy:f=>e.handleWorktreeStrategy(f),resolveWorktreeRepoDir:(f,v)=>e.resolveWorktreeRepoDir(f,v),updatePersistedSession:(f,v)=>e.updatePersistedSession(f,v),dispatchSessionNotification:(f,v)=>e.dispatchSessionNotification(f,v),notifySession:(f,v,T,F)=>e.notifySession(f,v,T,F),clearRetryTimersForSession:f=>a.clearRetryTimersForSession(f),hasTurnCompleteWakeMarker:f=>e.lastTurnCompleteMarkers.has(f),shouldEmitTurnCompleteWake:f=>e.shouldEmitTurnCompleteWake(f),shouldEmitTerminalWake:f=>e.shouldEmitTerminalWake(f),resolvePlanApprovalMode:f=>e.resolvePlanApprovalMode(f),getPlanApprovalButtons:(f,v)=>l.getPlanApprovalButtons(f,v),getResumeButtons:(f,v)=>l.getResumeButtons(f,v),getQuestionButtons:(f,v,T)=>l.getQuestionButtons(f,v,T),extractLastOutputLine:f=>e.extractLastOutputLine(f),getOutputPreview:(f,v)=>e.getOutputPreview(f,v),originThreadLine:f=>e.originThreadLine(f),debounceWaitingEvent:(f,v)=>e.debounceWaitingEvent(f,v),isAlreadyMerged:f=>e.isAlreadyMerged(f),questionContextSummaryProvider:t.questionContextSummaryProvider??ol()}),I=new vr({getPersistedSession:f=>i.getPersistedSession(f),resolveActiveSession:f=>d.resolveActive(f),resolveWorktreeRepoDir:(f,v)=>e.resolveWorktreeRepoDir(f,v),updatePersistedSession:(f,v)=>e.updatePersistedSession(f,v),dispatchNotification:(f,v)=>m.dispatch(f,v),buildRoutingProxy:f=>e.buildRoutingProxy(f)}),R=new wr({hydrateSpawnedSession:(f,v,T)=>{S.hydrateSpawnedSession(f,v,T)},markRunning:f=>{i.markRunning(f),f.approvalState==="approved"&&f.planDecisionVersion>0&&l.consumePlanDecisionTokens(f.id,f.planDecisionVersion-1),e.pendingPlanResumeClaims.delete(f.id),e.onPersistedSessionChanged(i.getPersistedSession(f.id))},handleTerminal:async f=>{let v=e.pendingPlanResumeClaims.get(f.id);try{await e.onSessionTerminal(f)}finally{if(v){let T=Ki(v,i.getPersistedSession(f.id));i.replacePersistedSession(T),e.pendingPlanResumeClaims.delete(f.id),e.onPersistedSessionChanged(T)}}},handleTurnEnd:(f,v)=>A.handleTurnEnd(f,v),formatLaunchWorkdirLabel:f=>e.formatLaunchWorkdirLabel(f),notifySession:(f,v,T,F)=>e.notifySession(f,v,T,F)});return{registry:r,sessions:o,store:i,metrics:s,wakeDispatcher:a,interactions:l,notifications:m,worktrees:g,questions:h,lifecycle:A,restore:S,stateSync:u,references:d,worktreeStrategy:c,worktreeDecisions:I,runtimeBootstrap:R,worktreeMessages:y,maintenance:b}}get persisted(){return this.store.persisted}get idIndex(){return this.store.idIndex}get nameIndex(){return this.store.nameIndex}uniqueName(e){return this.registry.uniqueName(e)}syncRuntimeGcDeadline(e){this.maintenance.syncRuntimeGcDeadline(e)}onPersistedSessionChanged(e){e&&(this.syncPersistedSessionMaintenance(e),this.enforcePersistedRetention())}syncPersistedSessionMaintenance(e){this.maintenance.syncPersistedSessionMaintenance(e)}syncActionTokenExpiryDeadline(){this.maintenance.syncActionTokenExpiryDeadline()}syncTmpOutputCleanupDeadline(e=Date.now()){this.maintenance.syncTmpOutputCleanupDeadline(e)}enforcePersistedRetention(){this.maintenance.enforcePersistedRetention()}bootstrapMaintenanceSchedules(){this.maintenance.bootstrapMaintenanceSchedules()}disposeMaintenance(){this.maintenance.dispose()}spawn(e,t={}){if(this.registry.activeSessionCount()>=this.maxSessions)throw new Error(`Max sessions reached (${this.maxSessions}). Use agent_sessions to list active sessions and agent_kill to end one.`);let o,i,s=h=>{i=i?Promise.all([i,h]).then(()=>{}):h};if(e.resumeSessionId&&!e.forkSession){let h=this.registry.list().filter(b=>b.backendKind==="codex-app-server"&&(b.harnessSessionId===e.resumeSessionId||b.backendRef?.conversationId===e.resumeSessionId||!b.harnessSessionId&&!b.backendRef?.conversationId&&b.resumeSessionId===e.resumeSessionId)),P=h.find(b=>b.status==="starting"||b.status==="running");if(P)throw new Error(`Cannot resume backend thread ${e.resumeSessionId}: session ${P.id} still owns its active writer.`);for(let b of h)typeof b.waitForTeardown=="function"&&s(b.waitForTeardown())}if(e.sessionIdOverride){let h=this.getPersistedSession(e.sessionIdOverride);if(e.resumeSessionId&&h?.pendingPlanApproval){let b=mn(h,e.permissionMode??_.permissionMode);e={...e,permissionMode:b.permissionMode,...b.patch},b.approvalApplied&&(o=h)}let P=this.registry.get(e.sessionIdOverride);if(P?.status==="starting"||P?.status==="running")throw new Error(`Cannot reuse session ID ${e.sessionIdOverride}: that session is still ${P.status}.`);P&&(typeof P.waitForTeardown=="function"&&s(P.waitForTeardown()),this.registry.remove(P.id,"session-id-override-replacement")),this.clearWaitingTimestampsForSession(e.sessionIdOverride),this.lastTurnCompleteMarkers.delete(e.sessionIdOverride),this.lastTerminalWakeMarkers.delete(e.sessionIdOverride),this.maintenance.cancelRuntimeGc(e.sessionIdOverride)}let a=e.name||Ei(e.prompt),l=this.uniqueName(a);l!==a&&`${a}${l}`;let d=this.checkRepoPolicyForLaunch(e.workdir,e.worktreeStrategy);if(!d.ok){let h=d;throw new Error(h.text)}e.repoIntegrationPolicy=d.resolution.policy,e.repoIntegrationPolicySource=d.resolution.source==="none"?void 0:d.resolution.source,e.repoProvider=d.resolution.provider;let u=this.restore.prepareSpawn(e,l);if(!e.route?.provider||!e.route.target)throw new Error(`Cannot launch session "${l}": missing explicit route metadata.`);let m=e.harness??"claude-code",g=this,S,y=m==="claude-code"&&!e.canUseTool?async(h,P)=>{if(!S)throw new Error("canUseTool called before session ID was set");return g.handleAskUserQuestion(S,P)}:e.canUseTool,c=new kn({...e,workdir:u.actualWorkdir,systemPrompt:u.effectiveSystemPrompt,canUseTool:y},l);S=c.id,o&&this.pendingPlanResumeClaims.set(c.id,o),this.registry.add(c),this.metrics.incrementLaunched();try{return this.runtimeBootstrap.initializeSession(c,u,e,{...t,startAfter:i})}catch(h){throw this.pendingPlanResumeClaims.delete(c.id),h}}async spawnAndAwaitRunning(e,t={}){let r=this.spawn(e,t);return await this.waitForRunningSession(r),r}async waitForRunningSession(e){if(e.status==="running")return;if(gl.has(e.status))throw new Error(this.describeLaunchFailure(e));let t=e.on?.bind(e),r=e.off?.bind(e)??e.removeListener?.bind(e);if(!t||!r)throw new Error(`Session ${e.name} [${e.id}] did not expose lifecycle events during startup.`);await new Promise((o,i)=>{let s=(l,d)=>{if(d==="running"){a(),o();return}gl.has(d)&&(a(),i(new Error(this.describeLaunchFailure(e))))},a=()=>{r("statusChange",s)};t("statusChange",s)})}describeLaunchFailure(e){let t=e.killReason?` (reason: ${e.killReason})`:"",r=e.error||e.result?.result||`status=${e.status}${t}`;return`Session ${e.name} [${e.id}] failed to start: ${r}`}formatLaunchResult(e,t){return en({prompt:e.prompt,workdir:e.workdir,harness:e.harness,permissionMode:e.permissionMode??_.permissionMode,planApproval:e.planApproval,resumeSessionId:e.resumeSessionId,resumeSessionName:e.resumeSessionName,forkSession:e.forkSession,forceNewSession:e.forceNewSession,clearedPersistedCodexResume:e.clearedPersistedCodexResume},t)}shouldRunWorktreeStrategy(e){let t=e.lifecycle;return!(t==="starting"||t==="awaiting_plan_decision"||t==="awaiting_user_input"||e.pendingPlanApproval)}resolveRepoPolicy(e){let t=pt(e);if(!t)return{source:"none",provider:"unsupported",prAvailable:!1};let r=this.store.getRepoPolicy(t.key);if(r)return{identity:t,policy:r.policy,source:"stored",provider:t.provider,prAvailable:t.provider==="github"&&et(),record:r};let o=Ss(t);if(o){let i=lo(t,o,"seeded");return{identity:t,policy:o,source:"seeded",provider:t.provider,prAvailable:uo({provider:t.provider}),record:i}}return{identity:t,source:"unknown",provider:t.provider,prAvailable:uo({provider:t.provider})}}checkRepoPolicyForLaunch(e,t){let r=t??_.defaultWorktreeStrategy??"off",o=this.resolveRepoPolicy(e);return r==="off"?{ok:!0,resolution:o}:o.source==="none"?{ok:!0,resolution:o}:o.source==="unknown"&&o.identity?{ok:!1,text:co(o.identity,r,o.prAvailable)}:{ok:!0,resolution:o}}getRepoPolicyRecordForWorkdir(e){return this.resolveRepoPolicy(e).record}listRepoPolicies(){return this.store.listRepoPolicies()}cleanupRepoPolicies(){let e=[...this.store.cleanupRepoPolicies()],t=[];for(let r of this.store.listRepoPolicies()){let o=pt(r.repoRoot);!o||o.key===r.key||t.push(r.key)}return e.push(...this.store.removeRepoPolicies(t)),e.sort((r,o)=>r.repoRoot.localeCompare(o.repoRoot)||r.key.localeCompare(o.key))}setRepoPolicy(e,t){let r=pt(e);if(r)return this.store.setRepoPolicy(lo(r,t,"stored"))}resetRepoPolicy(e){let t=pt(e);return t?this.store.resetRepoPolicy(t.key):!1}requestRepoPolicyForLaunch(e){let t=e.worktreeStrategy??_.defaultWorktreeStrategy??"off",r=this.resolveRepoPolicy(e.workdir);if(!r.identity)return`Error: ${e.workdir} is not a git repository.`;let o=co(r.identity,t,r.prAvailable);if(!e.route?.provider||!e.route.target)return o;let i=`repo-policy:${r.identity.key}`,s=this.interactions.getRepoPolicyChoiceButtons({choiceId:i,route:e.route,repoRoot:r.identity.repoRoot,launchPrompt:e.prompt,launchWorkdir:e.workdir,launchName:e.name,launchModel:e.model,launchReasoningEffort:e.reasoningEffort,launchFastMode:e.fastMode,launchSystemPrompt:e.systemPrompt,launchAllowedTools:e.allowedTools,launchResumeSessionId:e.resumeSessionId,launchResumedFromSessionName:e.resumedFromSessionName,launchResumeWorktreeFrom:e.resumeWorktreeFrom,launchSessionIdOverride:e.sessionIdOverride,launchClearedPersistedCodexResume:e.clearedPersistedCodexResume,launchForkSession:e.forkSession,launchForceNewSession:e.forceNewSession,launchPermissionMode:e.permissionMode,launchPlanApproval:e.planApproval,launchHarness:e.harness,launchWorktreeStrategy:t,launchWorktreeBaseBranch:e.worktreeBaseBranch,launchWorktreePrTargetRepo:e.worktreePrTargetRepo,launchOriginAgentId:e.originAgentId,prAvailable:r.prAvailable}),a=yf(e,t);return this.notifications.dispatch(this.buildRoutingProxy({id:i,name:"repo-policy",route:e.route}),{label:"repo-policy-choice",idempotencyKey:`repo-policy-choice:${r.identity.key}:${t}:${a}`,userMessage:[o,"","After you choose a policy, OpenClaw Code Agent will continue this launch automatically."].join(`
|
|
220
225
|
`),notifyUser:"always",requireDirectUserNotification:!0,buttons:s,wakeMessageOnNotifySuccess:["Repo policy choice buttons delivered to the user.",`Repo: ${r.identity.repoRoot}`,"Wait for their policy choice; do not set the policy or relaunch separately."].join(`
|
|
221
|
-
`),wakeMessageOnNotifyFailed:o}),[`Repo policy choice prompt sent for ${r.identity.repoRoot}.`,r.prAvailable?"Wait for the user's Require PR, Merge or PR, No PR, or Manual response.":"Wait for the user's No PR or Manual response.","Do not send a separate plain-text policy question."].join(" ")}launchAfterRepoPolicyChoice(e){let t=e.route;if(!t?.provider||!t.target)throw new Error("missing route metadata for stored launch");let r=e.harness??Y(),o=e.permissionMode??_.permissionMode,i=e.planApproval??_.planApproval,s=this.spawn({prompt:e.prompt,workdir:e.workdir,sessionIdOverride:e.sessionIdOverride,name:e.name,model:e.model,reasoningEffort:e.reasoningEffort,fastMode:e.fastMode,systemPrompt:e.systemPrompt,allowedTools:e.allowedTools,resumeSessionId:e.resumeSessionId,resumedFromSessionName:e.resumedFromSessionName,resumeWorktreeFrom:e.resumeWorktreeFrom,forkSession:e.resumeSessionId?e.forkSession:!1,multiTurn:!0,permissionMode:o,planApproval:i,codexApprovalPolicy:r==="codex"?"never":void 0,originChannel:this.originChannelFromRoute(t),originThreadId:t.threadId,originAgentId:e.originAgentId,originSessionKey:t.sessionKey,route:t,harness:r,worktreeStrategy:e.worktreeStrategy,worktreeBaseBranch:e.worktreeBaseBranch,worktreePrTargetRepo:e.worktreePrTargetRepo});return{session:s,text:this.formatLaunchResult({prompt:e.prompt,workdir:e.workdir,harness:r,permissionMode:o,planApproval:i,forceNewSession:e.forceNewSession,resumeSessionId:e.resumeSessionId,resumeSessionName:e.resumedFromSessionName,forkSession:e.forkSession,clearedPersistedCodexResume:e.clearedPersistedCodexResume},s)}}continueLaunchAfterManualRepoPolicy(e,t){let r=this.resolveRepoPolicy(e);if(!r.identity)return{kind:"none"};let o=this.interactions.listActiveActionTokens("repo-policy-set").filter(u=>u.repoPolicyWorkdir===r.identity?.repoRoot),i=()=>{for(let u of new Set(o.map(
|
|
222
|
-
`)??"",artifact:
|
|
223
|
-
`),buttons:this.getPolicyAwareWorktreeDecisionButtons(t,{allowDelegate:!0},r,o)})}persistSession(e,t={}){let r=t.scheduleRuntimeGc??!0;this.store.hasRecordedSession(e.id)||this.metrics.recordSession(e),this.store.persistTerminal(e),r&&this.syncRuntimeGcDeadline(e),this.onPersistedSessionChanged(this.store.getPersistedSession(e.id)),this.syncTmpOutputCleanupDeadline()}reconcilePersistedTaskFlowMirrors(){let e=!1;for(let t of this.store.listPersistedSessions()){let r=
|
|
224
|
-
`),
|
|
226
|
+
`),wakeMessageOnNotifyFailed:o}),[`Repo policy choice prompt sent for ${r.identity.repoRoot}.`,r.prAvailable?"Wait for the user's Require PR, Merge or PR, No PR, or Manual response.":"Wait for the user's No PR or Manual response.","Do not send a separate plain-text policy question."].join(" ")}launchAfterRepoPolicyChoice(e){let t=e.route;if(!t?.provider||!t.target)throw new Error("missing route metadata for stored launch");let r=e.harness??Y(),o=e.permissionMode??_.permissionMode,i=e.planApproval??_.planApproval,s=this.spawn({prompt:e.prompt,workdir:e.workdir,sessionIdOverride:e.sessionIdOverride,name:e.name,model:e.model,reasoningEffort:e.reasoningEffort,fastMode:e.fastMode,systemPrompt:e.systemPrompt,allowedTools:e.allowedTools,resumeSessionId:e.resumeSessionId,resumedFromSessionName:e.resumedFromSessionName,resumeWorktreeFrom:e.resumeWorktreeFrom,forkSession:e.resumeSessionId?e.forkSession:!1,multiTurn:!0,permissionMode:o,planApproval:i,codexApprovalPolicy:r==="codex"?"never":void 0,originChannel:this.originChannelFromRoute(t),originThreadId:t.threadId,originAgentId:e.originAgentId,originSessionKey:t.sessionKey,route:t,harness:r,worktreeStrategy:e.worktreeStrategy,worktreeBaseBranch:e.worktreeBaseBranch,worktreePrTargetRepo:e.worktreePrTargetRepo});return{session:s,text:this.formatLaunchResult({prompt:e.prompt,workdir:e.workdir,harness:r,permissionMode:o,planApproval:i,forceNewSession:e.forceNewSession,resumeSessionId:e.resumeSessionId,resumeSessionName:e.resumedFromSessionName,forkSession:e.forkSession,clearedPersistedCodexResume:e.clearedPersistedCodexResume},s)}}continueLaunchAfterManualRepoPolicy(e,t){let r=this.resolveRepoPolicy(e);if(!r.identity)return{kind:"none"};let o=this.interactions.listActiveActionTokens("repo-policy-set").filter(u=>u.repoPolicyWorkdir===r.identity?.repoRoot),i=()=>{for(let u of new Set(o.map(m=>m.sessionId)))this.clearRepoPolicyChoiceTokens(u)},s=o.filter(u=>u.repoPolicy===t&&!!u.launchPrompt&&!!u.launchWorkdir&&!!u.route?.provider&&!!u.route?.target);if(s.length===0)return i(),{kind:"none"};let a=new Map;for(let u of s){let m=Sf(u);a.has(m)||a.set(m,u)}if(a.size>1)return i(),{kind:"ambiguous",count:a.size};let l=[...a.values()][0];if(!l.launchPrompt||!l.launchWorkdir)return{kind:"none"};let d=this.launchAfterRepoPolicyChoice({route:l.route,prompt:l.launchPrompt,workdir:l.launchWorkdir,name:l.launchName,model:l.launchModel,reasoningEffort:l.launchReasoningEffort,fastMode:l.launchFastMode,systemPrompt:l.launchSystemPrompt,allowedTools:l.launchAllowedTools,resumeSessionId:l.launchResumeSessionId,resumedFromSessionName:l.launchResumedFromSessionName,resumeWorktreeFrom:l.launchResumeWorktreeFrom,sessionIdOverride:l.launchSessionIdOverride,clearedPersistedCodexResume:l.launchClearedPersistedCodexResume,forkSession:l.launchForkSession,forceNewSession:l.launchForceNewSession,permissionMode:l.launchPermissionMode,planApproval:l.launchPlanApproval,harness:l.launchHarness,worktreeStrategy:l.launchWorktreeStrategy,worktreeBaseBranch:l.launchWorktreeBaseBranch,worktreePrTargetRepo:l.launchWorktreePrTargetRepo,originAgentId:l.launchOriginAgentId});return this.consumeActionToken(l.id),this.clearRepoPolicyChoiceTokens(l.sessionId),{kind:"launched",...d}}makeActionButton(e,t,r,o={}){return this.interactions.makeActionButton(e,t,r,o)}makePluginActionButton(e,t,r,o={}){return this.makeActionButton(e,t,r,o)}consumeActionToken(e){return this.interactions.consumeActionToken(e)}getActionToken(e){return this.interactions.getActionToken(e)}clearRepoPolicyChoiceTokens(e){this.interactions.clearRepoPolicyChoiceTokens(e)}clearPlanDecisionTokens(e,t){this.interactions.clearPlanDecisionTokens(e,t)}isCurrentPendingPlanDecision(e,t){let r=this.resolve(e)??this.getPersistedSession(e);return At(r,t)}dispatchPlanApprovalFallback(e,t,r){let o=new Date().toISOString();this.notifications.dispatch(e,{label:"plan-approval-fallback",idempotencyKey:`plan-approval:${e.id}:v${t??"unknown"}:fallback`,userMessages:vt({session:e,summary:r}),notifyUser:"always",shouldDispatch:()=>this.isCurrentPendingPlanDecision(e.id,t),hooks:{onNotifyStarted:()=>{this.updatePersistedSession(e.id,{approvalPromptRequiredVersion:t,approvalPromptVersion:t,approvalPromptStatus:"sending",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"explicit_fallback_text",approvalPromptLastAttemptAt:o})},onNotifySucceeded:()=>{this.updatePersistedSession(e.id,{approvalPromptRequiredVersion:t,approvalPromptVersion:t,approvalPromptStatus:"fallback_delivered",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"explicit_fallback_text",approvalPromptLastAttemptAt:o,approvalPromptDeliveredAt:new Date().toISOString(),approvalPromptFailedAt:void 0})},onNotifyFailed:()=>{this.updatePersistedSession(e.id,{approvalPromptRequiredVersion:t,approvalPromptVersion:t,approvalPromptStatus:"failed",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"explicit_fallback_text",approvalPromptLastAttemptAt:o,approvalPromptFailedAt:new Date().toISOString()})}},wakeMessageOnNotifySuccess:Je(e,t,!0),wakeMessageOnNotifyFailed:yr({session:e,planDecisionVersion:t}),failureWakeConfirmsNotificationDelivery:!1})}getWorktreeDecisionButtons(e,t={},r={merge:!0,pr:!0}){let o=this.resolve(e)??this.getPersistedSession(e);if(!(!o||o.worktreeStrategy==="delegate"&&t.allowDelegate!==!0))return this.interactions.getWorktreeDecisionButtons(e,o,r)}getPolicyAwareWorktreeDecisionButtons(e,t={},r,o){let i=r??this.resolve(e),s=o??this.getPersistedSession(e),a=this.resolveWorktreeRepoDir(i?.originalWorkdir??s?.workdir,i?.worktreePath??s?.worktreePath),l=a?this.resolveRepoPolicy(a):void 0,d=i?.repoIntegrationPolicy??s?.repoIntegrationPolicy,u=d??l?.policy,m=l?.prAvailable??!!(d&&d!=="never-pr"&&d!=="manual"),g=u?ao({policy:u,prAvailable:m}):{merge:!0,pr:!0};return this.getWorktreeDecisionButtons(e,t,g)}getWorktreeCompletionState(e,t,r,o){return this.worktrees.getCompletionState(e,t,r,o)}notifyWorktreeOutcome(e,t,r){this.notifications.notifyWorktreeOutcome(e,t,r)}requestPlanApprovalFromUser(e,t){let r=t.trim();if(!r)return"Error: summary must not be empty.";let o=He(r),i=this.resolve(e),s=i?void 0:this.getPersistedSession(e),a=i??s;if(!a)return`Error: Session "${e}" not found.`;if(!a.pendingPlanApproval)return`Error: Session "${e}" is not awaiting plan approval.`;let l=D(i??s??{id:e})??e;if(this.resolvePlanApprovalMode(a)!=="delegate")return`Error: Session "${e}" already uses direct user plan approval. Do not send a duplicate approval prompt.`;let d=a.actionablePlanDecisionVersion??a.planDecisionVersion;if(Rt(a,d))return[`An actionable plan review prompt already exists for session ${a.name} [${l}].`,"Wait for the user's Approve, Revise, or Reject response.","Do not send a separate plain-text approval message."].join(" ");if(a.deliveryState==="notifying")return[`A plan approval prompt is already being delivered for session ${a.name} [${l}].`,"Wait for delivery to finish before retrying."].join(" ");let u=this.interactions.getPlanApprovalButtons(l,{...a,planDecisionVersion:d}),m=i?jo(i,d):void 0,g=kt({sessionName:a.name,actionableVersion:d,preview:i?.getOutput().join(`
|
|
227
|
+
`)??"",artifact:m,escalationRationale:o,hasButtons:!0,heading:"needs your decision"}),S=g.userMessages.map((y,c,h)=>({text:y,buttons:c===h.length-1?u:void 0,requiredForSequenceSuccess:!0}));return this.notifications.dispatch(this.buildRoutingProxy({id:l,name:a.name,sessionId:s?.sessionId,harnessSessionId:i?.harnessSessionId??s?.harnessSessionId,backendRef:i?.backendRef??s?.backendRef,route:i?.route??s?.route}),{label:"plan-approval",idempotencyKey:`plan-approval:${l}:v${d??"unknown"}:canonical`,userMessage:S.length===1?S[0]?.text:void 0,userMessages:S.length>1?S:void 0,notifyUser:"always",buttons:S.length===1?u:void 0,hooks:{onNotifyStarted:()=>{this.updatePersistedSession(l,{approvalPromptRequiredVersion:d,approvalPromptStatus:"sending",approvalPromptVersion:d,approvalPromptTransport:"direct-message",approvalPromptMessageKind:"canonical_buttons",approvalPromptLastAttemptAt:new Date().toISOString()})},onNotifySucceeded:()=>{this.updatePersistedSession(l,{canonicalPlanPromptVersion:d,approvalPromptRequiredVersion:d,approvalPromptVersion:d,approvalPromptStatus:"delivered",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"canonical_buttons",approvalPromptDeliveredAt:new Date().toISOString(),approvalPromptFailedAt:void 0})},onNotifyFailed:()=>{this.updatePersistedSession(l,{approvalPromptRequiredVersion:d,approvalPromptVersion:d,approvalPromptStatus:"failed",approvalPromptTransport:"direct-message",approvalPromptMessageKind:"canonical_buttons",approvalPromptFailedAt:new Date().toISOString()})}},shouldDispatch:()=>this.isCurrentPendingPlanDecision(l,d),onUserNotifyFailed:()=>this.dispatchPlanApprovalFallback(this.buildRoutingProxy({id:l,name:a.name,sessionId:s?.sessionId,harnessSessionId:i?.harnessSessionId??s?.harnessSessionId,backendRef:i?.backendRef??s?.backendRef,route:i?.route??s?.route}),d,g.reviewSummary),wakeMessageOnNotifySuccess:Je({id:l,name:a.name},d)}),[`Canonical plan approval prompt queued for session ${a.name} [${l}].`,"Wait for the user's Approve, Revise, or Reject response.","Do not send a separate plain-text approval message."].join(" ")}requestWorktreeDecisionFromUser(e,t){let r=t.trim();if(!r)return"Error: summary must not be empty.";let o=this.resolve(e),i=this.getPersistedSession(e),s=o??i;if(!s)return`Error: Session "${e}" not found.`;if(s.worktreeStrategy!=="delegate")return`Error: Session "${e}" already uses direct user worktree decisions. Do not send a duplicate decision prompt.`;if(!(!!("pendingWorktreeDecisionSince"in s?s.pendingWorktreeDecisionSince:void 0)||s.worktreeState==="pending_decision"||s.worktreeLifecycle?.state==="pending_decision"))return`Error: Session "${e}" is not awaiting a delegated worktree decision.`;let d=D(o??i??{id:e})??e,u=o?.worktreePath??i?.worktreePath,m=o?.worktreeBranch??i?.worktreeBranch,g=this.resolveWorktreeRepoDir(o?.originalWorkdir??i?.workdir,u);if(!u)return`Error: Session "${e}" has no managed worktree path.`;if(!m)return`Error: Session "${e}" has no managed worktree branch.`;if(!g)return`Error: Session "${e}" has no resolvable repository root for worktree ${u}.`;let S=o?.worktreeBaseBranch??i?.worktreeBaseBranch??Ne(g),y=Oe(g,m,S);if(!y)return`Error: Could not compute worktree diff summary for session "${e}".`;let c=this.getPolicyAwareWorktreeDecisionButtons(d,{allowDelegate:!0},o,i);if(!c||c.length===0)return`Error: Could not create worktree decision buttons for session "${e}".`;let h=r.split(/\r?\n/).map(P=>P.replace(/^[-*]\s+/,"").trim()).filter(P=>P.length>0);return this.notifications.dispatch(this.buildRoutingProxy({id:d,name:s.name,sessionId:i?.sessionId,harnessSessionId:o?.harnessSessionId??i?.harnessSessionId,backendRef:o?.backendRef??i?.backendRef,route:o?.route??i?.route}),this.worktreeMessages.buildAskNotification({session:{id:d,name:s.name,worktreePrTargetRepo:o?.worktreePrTargetRepo??i?.worktreePrTargetRepo},branchName:m,baseBranch:S,diffSummary:y,summaryLines:h,buttons:c})),[`Canonical worktree decision prompt sent for session ${s.name} [${d}].`,"Wait for the user's Merge, Open PR, Later, or Discard response.","Do not send a separate plain-text worktree decision message."].join(" ")}buildRoutingProxy(e){return{id:D(e)??C(e)??e.harnessSessionId??"unknown-session",name:e.name,harnessSessionId:e.harnessSessionId,backendRef:e.backendRef?{...e.backendRef}:void 0,route:e.route}}resolveWorktreeRepoDir(e,t){return e&&(!t||!qn(e,t))||!t?e:Et(t)??e}formatLaunchWorkdirLabel(e){if(!e.worktreePath)return e.workdir;let t=this.resolveWorktreeRepoDir(e.originalWorkdir,e.worktreePath);return!t||t===e.worktreePath?e.worktreePath:`${e.worktreePath} (worktree of ${t})`}async dismissWorktree(e){return this.worktreeDecisions.dismissWorktree(e)}snoozeWorktreeDecision(e,t){return this.worktreeDecisions.snoozeWorktreeDecision(e,t)}async handleWorktreeStrategy(e){return this.worktreeStrategy.handleWorktreeStrategy(e)}async onSessionTerminal(e){if(e.autoMergeParentSessionId){await this.handleAutoMergeResolverTerminal(e);return}return this.lifecycle.handleSessionTerminal(e)}async handleAutoMergeResolverTerminal(e){this.persistSession(e),this.clearWaitingTimestampsForSession(e.id),this.wakeDispatcher.clearRetryTimersForSession(e.id);let t=e.autoMergeParentSessionId;if(!t)return;let r=this.resolve(t),o=this.getPersistedSession(t),i=r??(o?this.buildRoutingProxy({id:o.sessionId,name:o.name,sessionId:o.sessionId,harnessSessionId:o.harnessSessionId,backendRef:o.backendRef,route:o.route}):void 0);if(!i){`${e.id}${t}`;return}if(e.status==="completed"&&r){this.updatePersistedSession(t,{autoMergeResolverSessionId:void 0}),await this.handleWorktreeStrategy(r);return}let s=r?.worktreeBranch??o?.worktreeBranch??"unknown",a=r?.worktreePath??o?.worktreePath??"(unknown worktree)",l=r?.worktreeBaseBranch??o?.worktreeBaseBranch,d=r?.worktreePrTargetRepo??o?.worktreePrTargetRepo,u=r?.worktreePushRemote??o?.worktreePushRemote;this.updatePersistedSession(t,Kt({worktreeBaseBranch:l,worktreePrTargetRepo:d,worktreePushRemote:u},{clearResolverSessionId:!0,notes:[e.status==="completed"?"auto_merge_conflict_resolver_completed_without_retry_target":"auto_merge_conflict_resolver_failed"]})),this.dispatchSessionNotification(i,{label:"worktree-merge-conflict-resolver-failed",idempotencyKey:`worktree-merge-conflict-resolver-failed:${t}:${e.id}`,userMessage:[`⚠️ [${i.name}] Auto-merge conflict resolution did not complete successfully.`,`Branch \`${s}\` was preserved for manual follow-up in ${a}.`,e.status==="completed"?"The resolver finished, but the original session could not be resumed for the merge retry.":`Resolver session ${e.name} ended with status=${e.status}.`].join(`
|
|
228
|
+
`),buttons:this.getPolicyAwareWorktreeDecisionButtons(t,{allowDelegate:!0},r,o)})}persistSession(e,t={}){let r=t.scheduleRuntimeGc??!0;this.store.hasRecordedSession(e.id)||this.metrics.recordSession(e),this.store.persistTerminal(e),r&&this.syncRuntimeGcDeadline(e),this.onPersistedSessionChanged(this.store.getPersistedSession(e.id)),this.syncTmpOutputCleanupDeadline()}reconcilePersistedTaskFlowMirrors(){let e=!1;for(let t of this.store.listPersistedSessions()){let r=Bi(t);r&&(t.taskFlowMirror=r,e=!0)}e&&this.store.saveIndex()}getMetrics(){return this.metrics.getMetrics()}notifySession(e,t,r="notification",o){this.dispatchSessionNotification(e,{label:r,idempotencyKey:r==="agent-respond"?void 0:o??`notify:${e.id}:${r}:${t}`,userMessage:t,notifyUser:"always"})}notifyResumedLaunch(e){if(!e.resumeSessionId)return;let t=this.formatLaunchWorkdirLabel(e),r=ge({harness:e.harnessName,model:e.model,reasoningEffort:e.reasoningEffort})??"default";this.notifySession(e,tn({sessionName:e.name,resumedFromSessionName:e.resumedFromSessionName,workdirLabel:t,harnessLabel:r}),"resumed-launch",`resumed-launch:${e.id}:${e.startedAt}:${e.resumeSessionId}`)}sendPlanOffer(e){let t=this.interactions.getPlanOfferButtons({offerId:e.offerId,route:e.route,planName:e.planName,planPrompt:e.planPrompt,planWorkdir:e.planWorkdir,planWorktreeStrategy:e.planWorktreeStrategy});this.dispatchSessionNotification(this.buildRoutingProxy({id:e.offerId,route:e.route}),{label:"plan-offer",idempotencyKey:`plan-offer:${e.offerId}`,userMessage:e.text,notifyUser:"always",buttons:t})}emitGoalTaskUpdate(e,t,r="goal-task"){let o=e.sessionId??e.id,i=this.buildRoutingProxy({id:o,name:e.sessionName??e.name,route:e.route}),s=e.sessionId?this.resolve(e.sessionId):void 0,a=e.sessionId?this.getPersistedSession(e.sessionId):void 0,l=s??a??e;Object.assign(i,{harnessName:"harnessName"in l?l.harnessName:l.harness,model:l.model,reasoningEffort:l.reasoningEffort}),i.originChannel=e.originChannel,i.originThreadId=e.originThreadId,i.originSessionKey=e.originSessionKey;let d=r==="goal-task-succeeded",u=[`✅ [${e.name}] Goal task succeeded`,e.sessionId?`Session: ${e.sessionName??e.name} [${e.sessionId}]`:void 0].filter(g=>!!g).join(`
|
|
229
|
+
`),m=g=>Ro({sessionId:o,sessionName:e.sessionName,taskName:e.name,summary:t,originThreadLine:Ze(i),canonicalStatusDelivered:g});this.dispatchSessionNotification(i,{label:r,idempotencyKey:`goal:${e.id}:${r}:${d?"success":t}`,userMessage:d?u:t,notifyUser:"always",completionSummary:d?{required:!0,producer:"goal",outcomeKey:`goal:${e.id}`}:void 0,completionWakeSummaryRequired:d,completionWakeOutcomeKey:d?`goal:${e.id}`:void 0,wakeMessageOnNotifySuccess:d?m(!0):void 0,wakeMessageOnNotifyFailed:d?m(!1):void 0})}launchPlanOffer(e){let t=e.route??{provider:"system",target:"system"};return this.spawn({prompt:e.prompt,workdir:e.workdir,name:e.name,harness:Y(),permissionMode:"plan",planApproval:"ask",worktreeStrategy:e.worktreeStrategy??"off",multiTurn:!0,route:t,originChannel:this.originChannelFromRoute(t),originThreadId:t.threadId,originSessionKey:t.sessionKey})}dispatchSessionNotification(e,t){this.notifications.dispatch(e,t)}originChannelFromRoute(e){return e.accountId?`${e.provider}|${e.accountId}|${e.target}`:`${e.provider}|${e.target}`}debounceWaitingEvent(e,t){let r=Date.now(),o=t?`${e}:${t}`:e,i=this.lastWaitingEventTimestamps.get(o);return i&&r-i<hf?!1:(this.lastWaitingEventTimestamps.set(o,r),!0)}clearWaitingTimestampsForSession(e){this.lastWaitingEventTimestamps.delete(e);let t=`${e}:`;for(let r of this.lastWaitingEventTimestamps.keys())r.startsWith(t)&&this.lastWaitingEventTimestamps.delete(r)}originThreadLine(e){return Ze(e)}extractLastOutputLine(e){return e.getOutput(3).filter(o=>o.trim()).pop()?.trim()||void 0}getOutputPreview(e,t=1e3){return cl(e,t)}resolvePlanApprovalMode(e){return e.planApproval??_.planApproval??"delegate"}shouldEmitTurnCompleteWake(e){let t=`${e.startedAt??0}|${e.result?.session_id??""}|${e.result?.num_turns??0}`;return this.lastTurnCompleteMarkers.get(e.id)===t?(`${e.id}${t}`,!1):(this.lastTurnCompleteMarkers.set(e.id,t),!0)}shouldEmitTerminalWake(e){let t=`${e.status}|${e.startedAt??0}|${e.result?.session_id??""}|${e.result?.num_turns??0}|${e.killReason}`;return this.lastTerminalWakeMarkers.get(e.id)===t?!1:(this.lastTerminalWakeMarkers.set(e.id,t),!0)}resolve(e){return this.references.resolveActive(e)}get(e){return this.registry.get(e)}list(e){let t=this.registry.list();return e&&e!=="all"&&(t=t.filter(r=>r.status===e)),t.sort((r,o)=>o.startedAt-r.startedAt)}kill(e,t){let r=this.registry.get(e);if(!r)return!1;if(r.pendingPlanApproval){this.clearPlanDecisionTokens(r.id);let o={lifecycle:"terminal",runtimeState:"stopped",pendingPlanApproval:!1,planApprovalContext:void 0,approvalState:r.approvalState==="pending"?"rejected":r.approvalState,planDecisionVersion:(r.planDecisionVersion??0)+1,actionablePlanDecisionVersion:void 0,canonicalPlanPromptVersion:void 0,approvalPromptRequiredVersion:void 0,approvalPromptVersion:void 0,approvalPromptStatus:"not_sent",approvalPromptTransport:"none",approvalPromptMessageKind:"none",approvalPromptLastAttemptAt:void 0,approvalPromptDeliveredAt:void 0,approvalPromptFailedAt:void 0};r.applyControlPatch(o),Object.assign(r,o),this.updatePersistedSession(r.id,o)}return r.kill(t??"user"),!0}killAll(e="user"){for(let t of this.sessions.values())gf.has(t.status)&&this.kill(t.id,e)}resolveBackendConversationId(e){return this.references.resolveBackendConversationId(e)}resolveHarnessSessionId(e){return this.resolveBackendConversationId(e)}getPersistedSession(e){return this.references.getPersistedSession(e)}isAlreadyMerged(e){if(!e)return!1;let t=this.store.getPersistedSession(e);return t?.worktreeMerged===!0||t?.worktreeLifecycle?.state==="merged"||t?.worktreeLifecycle?.state==="released"}async enqueueMerge(e,t,r){return this.mergeQueue.enqueue(e,t,r)}updatePersistedSession(e,t){let r=this.stateSync.applySessionPatch(e,t);return r&&this.onPersistedSessionChanged(this.store.getPersistedSession(e)),r}listPersistedSessions(){return this.store.listPersistedSessions()}async handleAskUserQuestion(e,t){let r=this.sessions.get(e);if(!r)throw new Error(`Session "${e}" not found for AskUserQuestion intercept`);return this.questions.handleAskUserQuestion(r,t)}resolveAskUserQuestion(e,t,r={}){return this.questions.resolveAskUserQuestion(e,t,r)}async resolvePendingInputOption(e,t,r={}){let o=this.sessions.get(e);return o?.canSubmitPendingInputOption?.()?await o.submitPendingInputOption(t,r)?(this.clearWaitingTimestampsForSession(e),!0):!1:this.questions.resolveAskUserQuestion(e,t,r)}canSubmitPendingInputOption(e){return this.sessions.get(e)?.canSubmitPendingInputOption?.()===!0}pendingInputSubmissionRequiresMore(e){return this.sessions.get(e)?.pendingInputSubmissionRequiresMore()===!0}consumeQuestionAnswerTokens(e,t,r){return this.interactions.consumeQuestionAnswerTokens(e,t,r)}consumePlanDecisionTokens(e,t){return this.interactions.consumePlanDecisionTokens(e,t)}dispose(){this.disposeMaintenance(),this.questions.dispose(),this.notifications.dispose()}};import{definePluginEntry as yl}from"openclaw/plugin-sdk/plugin-entry";function bf(n){return[...new Set(n.map(e=>e.workdir).filter(e=>typeof e=="string"&&e.length>0))]}function Rf(n){let e=parseInt(process.env.OPENCLAW_WORKTREE_CLEANUP_AGE_HOURS??"168",10)||168,t=e*60*60*1e3,r=Date.now()-t,o=new Set(n.listPersistedSessions().map(l=>l.worktreePath).filter(l=>typeof l=="string"&&l.length>0)),i=new Set,s=process.env.OPENCLAW_WORKTREE_DIR??_.worktreeDir;if(s)i.add(s);else for(let l of bf(n.listPersistedSessions()))try{let d=wf("git",["rev-parse","--show-toplevel"],{cwd:l,timeout:5e3,encoding:"utf-8",stdio:["pipe","pipe","pipe"]}).trim();d&&i.add(zo(d,".worktrees"))}catch{}if(i.size===0)return;let a=0;for(let l of i)try{let d=kf(l);for(let u of d){if(!u.startsWith("openclaw-worktree-"))continue;let m=zo(l,u);try{let g=vf(m);if(!g.isDirectory()||g.mtimeMs>r||o.has(m))continue;Pf(m,{recursive:!0,force:!0}),a++}catch(g){`${m}${g instanceof Error?g.message:String(g)}`}}}catch{}a>0&&`${a}${e}`}function Af(n){if(!n||typeof n!="object")return;let e=n,t=e.channel?.trim().toLowerCase();if(!t)return;let r=t==="telegram"?e.callback?.chatId??e.parentConversationId??e.conversationId:e.parentConversationId??e.conversationId??e.callback?.chatId;if(r)return ue(e.accountId?`${t}|${e.accountId}|${r}`:`${t}|${r}`,e.threadId,e.sessionKey)}function If(n){let e=null,t=null,r=null,o=!1,i=!1,s=n.registerTool;Te(n.runtime);let a=()=>zo(er(process.env),"plugin-state","openclaw-code-agent"),l=c=>{let h=c.deliveryContext;return h?.channel&&h.to?ue(h.accountId?`${h.channel}|${h.accountId}|${h.to}`:`${h.channel}|${h.to}`,h.threadId,c.sessionKey):ue(c.messageChannel,void 0,c.sessionKey)},d=c=>{r?.maybeCheckForUpdate({route:c})},u=c=>{if(o){c&&!i&&(Te(n.runtime,c.config),i=!0);return}let h=n.pluginConfig??{};Jo(h),c?(Te(n.runtime,c.config),i=!0):(Te(n.runtime),i=!1),e=new Ir(_.maxSessions,_.maxPersistedSessions,{worktreeSummaryProvider:Ta()}),t=new Kn(e),r=new Xt({stateDir:c?.stateDir??a(),currentVersion:n.version??pi.version??"0.0.0",actionButtonFactory:(P,b,A,I)=>e.makePluginActionButton(P,b,A,I)}),xr(e),Cr(t),Mr(r),t.start(),Rf(e),e.bootstrapMaintenanceSchedules(),o=!0,d()},m=()=>{t&&t.stop(),e&&e.killAll("shutdown"),e&&e.dispose(),t=null,e=null,r=null,o=!1,i=!1,Te(void 0),Cr(null),xr(null),Mr(null)},g=(c,h)=>{s(P=>{let b=c(P);return{...b,execute(A,I){return u(),d(l(P)),b.execute(A,I)}}},h)},S={...n,registerCommand(c){n.registerCommand({...c,handler:h=>(u(),d(l(h)),c.handler(h))})}},y=c=>{let h=Es(c);n.registerInteractiveHandler({...h,handler:async P=>(u(),d(Af(P)),h.handler(P))})};g(c=>Vi(c),{optional:!1,name:"agent_launch"}),g(c=>os(c),{optional:!1,name:"agent_sessions"}),g(c=>is(c),{optional:!1,name:"agent_kill"}),g(c=>Rn(c),{optional:!1,name:"agent_output"}),g(c=>cs(c),{optional:!1,name:"agent_respond"}),g(c=>ps(c),{optional:!1,name:"agent_request_plan_approval"}),g(c=>ms(c),{optional:!1,name:"agent_request_worktree_decision"}),g(c=>fs(c),{optional:!1,name:"agent_send_plan_offer"}),g(c=>gs(c),{optional:!1,name:"agent_stats"}),g(c=>vs(c),{optional:!1,name:"agent_repo_policy"}),g(c=>Tn(c),{optional:!1,name:"agent_merge"}),g(c=>Ht(c),{optional:!1,name:"agent_pr"}),g(c=>Ps(c),{optional:!1,name:"agent_worktree_cleanup"}),g(c=>ws(c),{optional:!1,name:"agent_worktree_status"}),g(c=>bs(c),{optional:!1,name:"agent_goal_launch"}),g(c=>As(c),{optional:!1,name:"agent_goal_status"}),g(c=>Is(c),{optional:!1,name:"agent_goal_stop"}),g(c=>Ts(c),{optional:!1,name:"agent_goal_edit"}),y("telegram"),y("discord"),Os(S),Ws(S),Fs(S),Ls(S),Bs(S),Ks(S),qs(S),Us(S),Gs(S),js(S),Hs(S),n.registerService({id:"openclaw-code-agent",start:u,stop:m})}var Rb=yl({id:"openclaw-code-agent",name:"Code Agent",description:"Multi-session coding-agent orchestration from OpenClaw chat",register:If});export{Rb as default,If as register,Af as routeFromInteractiveContext,bf as uniquePersistedWorkdirs};
|