@@ -8,7 +8,11 @@ import { runExec } from "openclaw/plugin-sdk/process-runtime";
88import { CODEX_ACP_PACKAGE , LEGACY_CODEX_ACP_PACKAGE } from "./codex-adapter.js" ;
99import { splitCommandParts } from "./command-line.js" ;
1010import { resolveAcpxPluginRoot } from "./config.js" ;
11- import { OPENCLAW_ACPX_LEASE_ID_ARG , OPENCLAW_GATEWAY_INSTANCE_ID_ARG } from "./process-lease.js" ;
11+ import {
12+ OPENCLAW_ACPX_LEASE_ID_ARG ,
13+ OPENCLAW_GATEWAY_INSTANCE_ID_ARG ,
14+ readAcpxProcessLeaseIdentity ,
15+ } from "./process-lease.js" ;
1216
1317const requireFromHere = createRequire ( import . meta. url ) ;
1418const GENERATED_WRAPPER_BASENAMES = new Set ( [
@@ -59,6 +63,7 @@ type AcpxProcessInfo = {
5963export type AcpxProcessCleanupDeps = {
6064 listProcesses ?: ( ) => Promise < AcpxProcessInfo [ ] > ;
6165 killProcess ?: ( pid : number , signal : NodeJS . Signals ) => void ;
66+ platform ?: NodeJS . Platform ;
6267 sleep ?: ( ms : number ) => Promise < void > ;
6368} ;
6469
@@ -68,8 +73,10 @@ type AcpxProcessCleanupResult = {
6873 terminatedPids : number [ ] ;
6974 skippedReason ?:
7075 | "missing-root"
76+ | "ambiguous-root"
7177 | "not-openclaw-owned"
7278 | "process-list-unavailable"
79+ | "unsupported-platform"
7380 | "unverified-root" ;
7481} ;
7582
@@ -136,6 +143,20 @@ function commandWrapperBelongsToRoot(command: string, wrapperRoot: string | unde
136143 ) ;
137144}
138145
146+ function commandContainsExactWrapperPath ( command : string , wrapperPath : string ) : boolean {
147+ const expectedPath = normalizePathLike ( wrapperPath ) ;
148+ return splitCommandParts ( command ) . some ( ( part ) => normalizePathLike ( part ) === expectedPath ) ;
149+ }
150+
151+ function wrapperPathBelongsToRoot ( wrapperPath : string , wrapperRoot : string ) : boolean {
152+ const normalizedPath = normalizePathLike ( wrapperPath ) ;
153+ const normalizedRoot = normalizePathLike ( wrapperRoot ) . replace ( / \/ + $ / , "" ) ;
154+ return (
155+ GENERATED_WRAPPER_BASENAMES . has ( path . posix . basename ( normalizedPath ) ) &&
156+ normalizedPath . startsWith ( `${ normalizedRoot } /` )
157+ ) ;
158+ }
159+
139160/** Check whether a command references an OpenClaw-generated ACPX wrapper path. */
140161export function isOpenClawLeaseAwareAcpxProcessCommand ( params : {
141162 command : string | undefined ;
@@ -342,6 +363,9 @@ export async function cleanupOpenClawOwnedAcpxProcessTree(params: {
342363 if ( ! rootPid || rootPid <= 0 || rootPid === process . pid ) {
343364 return { inspectedPids : [ ] , terminatedPids : [ ] , skippedReason : "missing-root" } ;
344365 }
366+ if ( ( params . deps ?. platform ?? process . platform ) === "win32" ) {
367+ return { inspectedPids : [ ] , terminatedPids : [ ] , skippedReason : "unsupported-platform" } ;
368+ }
345369
346370 let processes : AcpxProcessInfo [ ] ;
347371 try {
@@ -418,12 +442,66 @@ export async function cleanupOpenClawOwnedAcpxProcessTree(params: {
418442 } ;
419443}
420444
445+ /** Recover a pending lease by matching its exact live wrapper identity. */
446+ export async function cleanupOpenClawOwnedAcpxPendingLease ( params : {
447+ leaseId : string ;
448+ gatewayInstanceId : string ;
449+ wrapperRoot : string ;
450+ wrapperPath : string ;
451+ deps ?: AcpxProcessCleanupDeps ;
452+ } ) : Promise < AcpxProcessCleanupResult > {
453+ if ( ( params . deps ?. platform ?? process . platform ) === "win32" ) {
454+ return { inspectedPids : [ ] , terminatedPids : [ ] , skippedReason : "unsupported-platform" } ;
455+ }
456+ if ( ! params . wrapperPath || ! wrapperPathBelongsToRoot ( params . wrapperPath , params . wrapperRoot ) ) {
457+ return { inspectedPids : [ ] , terminatedPids : [ ] , skippedReason : "unverified-root" } ;
458+ }
459+
460+ let processes : AcpxProcessInfo [ ] ;
461+ try {
462+ processes = await ( params . deps ?. listProcesses ?? listPlatformProcesses ) ( ) ;
463+ } catch {
464+ return {
465+ inspectedPids : [ ] ,
466+ terminatedPids : [ ] ,
467+ skippedReason : "process-list-unavailable" ,
468+ } ;
469+ }
470+
471+ const matchingRoots = processes . filter (
472+ ( processInfo ) =>
473+ commandContainsExactWrapperPath ( processInfo . command , params . wrapperPath ) &&
474+ liveCommandMatchesLeaseIdentity ( {
475+ command : processInfo . command ,
476+ expectedLeaseId : params . leaseId ,
477+ expectedGatewayInstanceId : params . gatewayInstanceId ,
478+ } ) ,
479+ ) ;
480+ if ( matchingRoots . length === 0 ) {
481+ return { inspectedPids : [ ] , terminatedPids : [ ] , skippedReason : "missing-root" } ;
482+ }
483+ if ( matchingRoots . length > 1 ) {
484+ return {
485+ inspectedPids : uniquePids ( matchingRoots ) ,
486+ terminatedPids : [ ] ,
487+ skippedReason : "ambiguous-root" ,
488+ } ;
489+ }
490+
491+ const listedTree = collectProcessTree ( processes , matchingRoots [ 0 ] ! . pid ) ;
492+ const pids = uniquePids ( listedTree . toReversed ( ) ) ;
493+ return {
494+ inspectedPids : uniquePids ( listedTree ) ,
495+ terminatedPids : await terminatePids ( pids , params . deps ) ,
496+ } ;
497+ }
498+
421499/** Reap orphaned OpenClaw-owned ACPX wrapper trees during runtime startup. */
422500export async function reapStaleOpenClawOwnedAcpxOrphans ( params : {
423501 wrapperRoot : string ;
424502 deps ?: AcpxProcessCleanupDeps ;
425503} ) : Promise < AcpxStartupReapResult > {
426- if ( process . platform === "win32" ) {
504+ if ( ( params . deps ?. platform ?? process . platform ) === "win32" ) {
427505 return { inspectedPids : [ ] , terminatedPids : [ ] , skippedReason : "unsupported-platform" } ;
428506 }
429507
@@ -437,6 +515,10 @@ export async function reapStaleOpenClawOwnedAcpxOrphans(params: {
437515 const orphans = processes . filter (
438516 ( processInfo ) =>
439517 processInfo . ppid === 1 &&
518+ // Lease-aware wrapper roots are recovered one lease at a time. This
519+ // temporary marker fallback remains only for direct agents and
520+ // reparented descendants that upstream acpx cannot identify yet.
521+ ! readAcpxProcessLeaseIdentity ( processInfo . command ) &&
440522 isOpenClawOwnedAcpxProcessCommand ( {
441523 command : processInfo . command ,
442524 wrapperRoot : params . wrapperRoot ,
0 commit comments