Skip to content

Commit b6adb8e

Browse files
committed
fix: preserve retryable ACPX lease ownership (#115247)
1 parent 4b0d01b commit b6adb8e

6 files changed

Lines changed: 169 additions & 10 deletions

File tree

‎extensions/acpx/src/process-reaper.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe("process reaper", () => {
9393
expect(result).toEqual({
9494
inspectedPids: [],
9595
terminatedPids: [],
96-
skippedReason: "unverified-root",
96+
skippedReason: "process-list-unavailable",
9797
});
9898
expect(killSpy).not.toHaveBeenCalled();
9999
});
@@ -214,7 +214,7 @@ describe("process reaper", () => {
214214
expect(result).toEqual({
215215
inspectedPids: [],
216216
terminatedPids: [],
217-
skippedReason: "unverified-root",
217+
skippedReason: "process-list-unavailable",
218218
});
219219
expect(killed).toStrictEqual([]);
220220
});

‎extensions/acpx/src/process-reaper.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ export type AcpxProcessCleanupDeps = {
6666
type AcpxProcessCleanupResult = {
6767
inspectedPids: number[];
6868
terminatedPids: number[];
69-
skippedReason?: "missing-root" | "not-openclaw-owned" | "unverified-root";
69+
skippedReason?:
70+
| "missing-root"
71+
| "not-openclaw-owned"
72+
| "process-list-unavailable"
73+
| "unverified-root";
7074
};
7175

7276
/** Result from startup orphan reaping. */
@@ -343,7 +347,11 @@ export async function cleanupOpenClawOwnedAcpxProcessTree(params: {
343347
try {
344348
processes = await (params.deps?.listProcesses ?? listPlatformProcesses)();
345349
} catch {
346-
processes = [];
350+
return {
351+
inspectedPids: [],
352+
terminatedPids: [],
353+
skippedReason: "process-list-unavailable",
354+
};
347355
}
348356

349357
const listedTree = collectProcessTree(processes, rootPid);

‎extensions/acpx/src/runtime.test.ts‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,6 +2475,57 @@ describe("AcpxRuntime fresh reset wrapper", () => {
24752475
expect(leaseStore.leases.size).toBe(0);
24762476
});
24772477

2478+
it("preserves a promoted PID when the session record save fails", async () => {
2479+
const leasedCommand = `${CODEX_ACP_WRAPPER_COMMAND} ${OPENCLAW_ACPX_LEASE_ID_ARG} lease-partial-save ${OPENCLAW_GATEWAY_INSTANCE_ID_ARG} gateway-test`;
2480+
const savedRecord: Record<string, unknown> = {
2481+
name: "agent:codex:acp:binding:test",
2482+
agentCommand: leasedCommand,
2483+
pid: 777,
2484+
};
2485+
const baseStore: TestSessionStore = {
2486+
load: vi.fn(async () => savedRecord),
2487+
save: vi.fn(async () => {
2488+
throw new Error("session save failed");
2489+
}),
2490+
};
2491+
const leaseStore = makeLeaseStore();
2492+
leaseStore.leases.set("lease-partial-save", {
2493+
leaseId: "lease-partial-save",
2494+
gatewayInstanceId: "gateway-test",
2495+
sessionKey: "agent:codex:acp:binding:test",
2496+
wrapperRoot: "/tmp/openclaw/acpx",
2497+
wrapperPath: "/tmp/openclaw/acpx/codex-acp-wrapper.mjs",
2498+
rootPid: 777,
2499+
commandHash: "hash",
2500+
startedAt: 1,
2501+
state: "open",
2502+
});
2503+
const { runtime, delegate, wrappedStore } = makeRuntime(baseStore, {
2504+
openclawGatewayInstanceId: "gateway-test",
2505+
openclawProcessLeaseStore: leaseStore.store,
2506+
openclawWrapperRoot: "/tmp/openclaw/acpx",
2507+
});
2508+
vi.spyOn(delegate, "setMode").mockImplementation(async () => {
2509+
await wrappedStore.save({ ...savedRecord, pid: 888 });
2510+
});
2511+
2512+
await expect(
2513+
runtime.setMode({
2514+
handle: {
2515+
sessionKey: "agent:codex:acp:binding:test",
2516+
backend: "acpx",
2517+
runtimeSessionName: "agent:codex:acp:binding:test",
2518+
},
2519+
mode: "plan",
2520+
}),
2521+
).rejects.toThrow("session save failed");
2522+
2523+
expect(leaseStore.leases.get("lease-partial-save")).toMatchObject({
2524+
rootPid: 888,
2525+
state: "open",
2526+
});
2527+
});
2528+
24782529
it("keeps a shared pending lease until the last concurrent operation finishes", async () => {
24792530
const leasedCommand = `${CODEX_ACP_WRAPPER_COMMAND} ${OPENCLAW_ACPX_LEASE_ID_ARG} lease-concurrent-operations ${OPENCLAW_GATEWAY_INSTANCE_ID_ARG} gateway-test`;
24802531
const baseStore: TestSessionStore = {
@@ -2933,6 +2984,61 @@ describe("AcpxRuntime fresh reset wrapper", () => {
29332984
});
29342985
});
29352986

2987+
it("keeps close leases retryable when process listing is unavailable", async () => {
2988+
const leasedCommand = `${CODEX_ACP_WRAPPER_COMMAND} ${OPENCLAW_ACPX_LEASE_ID_ARG} lease-close-process-list ${OPENCLAW_GATEWAY_INSTANCE_ID_ARG} gateway-test`;
2989+
const baseStore: TestSessionStore = {
2990+
load: vi.fn(async () => ({
2991+
name: "agent:codex:acp:binding:test",
2992+
agentCommand: leasedCommand,
2993+
pid: 777,
2994+
})),
2995+
save: vi.fn(async () => {}),
2996+
};
2997+
const leaseStore = makeLeaseStore();
2998+
leaseStore.leases.set("lease-close-process-list", {
2999+
leaseId: "lease-close-process-list",
3000+
gatewayInstanceId: "gateway-test",
3001+
sessionKey: "agent:codex:acp:binding:test",
3002+
wrapperRoot: "/tmp/openclaw/acpx",
3003+
wrapperPath: "/tmp/openclaw/acpx/codex-acp-wrapper.mjs",
3004+
rootPid: 777,
3005+
commandHash: "hash",
3006+
startedAt: 1,
3007+
state: "open",
3008+
});
3009+
const { runtime, delegate } = makeRuntime(
3010+
baseStore,
3011+
{
3012+
openclawGatewayInstanceId: "gateway-test",
3013+
openclawProcessLeaseStore: leaseStore.store,
3014+
openclawWrapperRoot: "/tmp/openclaw/acpx",
3015+
},
3016+
{
3017+
openclawProcessCleanup: {
3018+
listProcesses: vi.fn(async () => {
3019+
throw new Error("process listing unavailable");
3020+
}),
3021+
sleep: vi.fn(async () => {}),
3022+
},
3023+
},
3024+
);
3025+
vi.spyOn(delegate, "close").mockResolvedValue(undefined);
3026+
3027+
await runtime.close({
3028+
handle: {
3029+
sessionKey: "agent:codex:acp:binding:test",
3030+
backend: "acpx",
3031+
runtimeSessionName: "agent:codex:acp:binding:test",
3032+
},
3033+
reason: "user-close",
3034+
});
3035+
3036+
expect(leaseStore.leases.get("lease-close-process-list")).toMatchObject({
3037+
rootPid: 777,
3038+
state: "open",
3039+
});
3040+
});
3041+
29363042
it("merges sidecar lease ids into loaded ACPX session records", async () => {
29373043
const leaseStore = makeLeaseStore();
29383044
leaseStore.leases.set("lease-loaded", {

‎extensions/acpx/src/runtime.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,8 +1234,7 @@ export class AcpxRuntime implements AcpRuntime {
12341234
const recordIdentity = readAcpxProcessLeaseIdentity(readAgentCommandFromRecord(record));
12351235
if (
12361236
recordIdentity?.leaseId !== identity.leaseId ||
1237-
recordIdentity.gatewayInstanceId !== identity.gatewayInstanceId ||
1238-
readRecordAgentPid(record) !== lease.rootPid
1237+
recordIdentity.gatewayInstanceId !== identity.gatewayInstanceId
12391238
) {
12401239
await processLeaseStore.markState(identity.leaseId, "lost");
12411240
}
@@ -1379,9 +1378,11 @@ export class AcpxRuntime implements AcpRuntime {
13791378
});
13801379
await this.processLeaseStore?.markState(
13811380
lease.leaseId,
1382-
result.terminatedPids.length > 0 || result.skippedReason === "missing-root"
1383-
? "closed"
1384-
: "lost",
1381+
result.skippedReason === "process-list-unavailable"
1382+
? "open"
1383+
: result.terminatedPids.length > 0 || result.skippedReason === "missing-root"
1384+
? "closed"
1385+
: "lost",
13851386
);
13861387
return;
13871388
}

‎extensions/acpx/src/service.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,46 @@ describe("createAcpxRuntimeService", () => {
421421
await service.stop?.(ctx);
422422
});
423423

424+
it("keeps PID-bearing leases when startup process listing is unavailable", async () => {
425+
const workspaceDir = await makeTempDir();
426+
const ctx = createServiceContext(workspaceDir);
427+
const runtime = createMockRuntime();
428+
const wrapperRoot = path.join(ctx.stateDir, "acpx");
429+
await openGatewayInstanceStore(ctx).register(ACPX_GATEWAY_INSTANCE_KEY, {
430+
instanceId: "gw-test",
431+
createdAt: 1,
432+
});
433+
const lease: AcpxProcessLease = {
434+
leaseId: "lease-process-list-unavailable",
435+
gatewayInstanceId: "gw-test",
436+
sessionKey: "agent:codex:acp:test",
437+
wrapperRoot,
438+
wrapperPath: path.join(wrapperRoot, "codex-acp-wrapper.mjs"),
439+
rootPid: 101,
440+
commandHash: "hash",
441+
startedAt: 1,
442+
state: "open",
443+
};
444+
await openProcessLeaseStore(ctx).register(lease.leaseId, lease);
445+
cleanupOpenClawOwnedAcpxProcessTreeMock.mockResolvedValueOnce({
446+
inspectedPids: [],
447+
terminatedPids: [],
448+
skippedReason: "process-list-unavailable",
449+
});
450+
const service = createAcpxRuntimeService(ctx, {
451+
runtimeFactory: () => runtime as never,
452+
});
453+
454+
await service.start(ctx);
455+
456+
await expect(openProcessLeaseStore(ctx).lookup(lease.leaseId)).resolves.toMatchObject({
457+
leaseId: lease.leaseId,
458+
rootPid: 101,
459+
state: "open",
460+
});
461+
await service.stop?.(ctx);
462+
});
463+
424464
it("runs wrapper-root orphan cleanup before dropping pending ACPX leases", async () => {
425465
const workspaceDir = await makeTempDir();
426466
const ctx = createServiceContext(workspaceDir);

‎extensions/acpx/src/service.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,11 @@ async function reapOpenAcpxProcessLeases(params: {
312312
terminatedPids.push(...result.terminatedPids);
313313
await params.leaseStore.markState(
314314
lease.leaseId,
315-
result.terminatedPids.length > 0 ? "closed" : "lost",
315+
result.skippedReason === "process-list-unavailable"
316+
? "open"
317+
: result.terminatedPids.length > 0
318+
? "closed"
319+
: "lost",
316320
);
317321
}
318322
return { inspectedPids, terminatedPids };

0 commit comments

Comments
 (0)