Skip to content

Commit 15f2400

Browse files
steipetewonfong
andcommitted
fix(subagents): reject undurable paused-run adoption
Fail plugin follow-up admission closed when the paused-run ownership swap cannot be persisted, while retaining the existing restart-recovery return-false contract. Trim duplicate tests and keep boundary coverage for requester routing, wake-batch remapping, repeated yield, and persistence rollback. Co-authored-by: zhou.huanfeng <[email protected]>
1 parent c6f6a3e commit 15f2400

8 files changed

Lines changed: 63 additions & 76 deletions

‎docs/tools/subagents.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ A sub-agent can also yield on its own behalf to wait for external work, such
301301
as a remote job or a long-running task it does not drive itself. That pauses
302302
the child run instead of completing it, so the requester receives no
303303
completion event yet and keeps waiting. A plugin can then continue that same run
304-
by calling `runtime.subagent.run` with the paused `sessionKey`, instead of
304+
by calling `api.runtime.subagent.run` with the paused `sessionKey`, instead of
305305
starting a sibling. The requester is announced once such a follow-up finishes
306306
normally; a follow-up that yields again leaves the run paused and the requester
307307
waiting.

‎src/agents/subagent-control.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,7 @@ describe("killControlledSubagentRun", () => {
20162016
nextRunId: recoveryRunId,
20172017
expected: source,
20182018
restartRecovery: receipt,
2019-
requirePersistence: true,
2019+
persistenceFailure: "return-false",
20202020
}),
20212021
).toBe(true);
20222022
addSubagentRunForTests({

‎src/agents/subagent-registry-restart-recovery.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ describe("subagent registry restart recovery", () => {
256256
nextRunId: expect.stringMatching(/^subagent-recovery:[a-f0-9]{64}$/),
257257
expected: entry,
258258
task: "finish the restart-safe task",
259-
requirePersistence: true,
259+
persistenceFailure: "return-false",
260260
}),
261261
);
262262
expect(replaceRun.mock.calls[0]?.[0].restartRecovery).toMatchObject({
@@ -782,7 +782,7 @@ describe("subagent registry restart recovery", () => {
782782
previousRunId: entry.runId,
783783
nextRunId: "subagent-recovery:accepted",
784784
restartRecovery: expect.objectContaining({ phase: "accepted" }),
785-
requirePersistence: true,
785+
persistenceFailure: "return-false",
786786
}),
787787
);
788788
expect(dispatchAgent).not.toHaveBeenCalled();

‎src/agents/subagent-registry-restart-recovery.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async function reconcileAcceptedRecovery(params: {
115115
}),
116116
task: params.entry.task,
117117
restartRecovery: params.receipt,
118-
requirePersistence: true,
118+
persistenceFailure: "return-false",
119119
});
120120
} catch {}
121121
if (!remapped) {

‎src/agents/subagent-registry-run-manager.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ export function createSubagentRunManager(params: {
684684
task?: string;
685685
restartRecovery?: SubagentRestartRecoveryReceipt;
686686
lifecycleGeneration?: string;
687-
requirePersistence?: boolean;
687+
persistenceFailure?: "return-false" | "throw";
688688
}) => {
689689
const previousRunId = replaceParams.previousRunId.trim();
690690
const nextRunId = replaceParams.nextRunId.trim();
@@ -838,7 +838,7 @@ export function createSubagentRunManager(params: {
838838
params.persistOrThrow(...changedRunIds);
839839
} catch (error) {
840840
if (
841-
replaceParams.requirePersistence === true ||
841+
replaceParams.persistenceFailure !== undefined ||
842842
replaceParams.lifecycleGeneration !== undefined
843843
) {
844844
restoreKillReconciliationSnapshots(killReconciliationSnapshots);
@@ -849,6 +849,9 @@ export function createSubagentRunManager(params: {
849849
previousRunId,
850850
nextRunId,
851851
});
852+
if (replaceParams.persistenceFailure === "throw") {
853+
throw error;
854+
}
852855
return false;
853856
}
854857
// The gateway has already started nextRunId. Keep its in-memory owner

‎src/agents/subagent-registry.test.ts‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3388,30 +3388,6 @@ describe("subagent registry seam flow", () => {
33883388
// deferring instead of waking on a run that has not produced a result.
33893389
expect(mod.countPendingDescendantRuns("agent:main:main")).toBe(1);
33903390
});
3391-
3392-
it("declines adoption when the session has no paused run", async () => {
3393-
mockPendingAgentWait();
3394-
mod.registerSubagentRun({
3395-
runId: PAUSED_RUN_ID,
3396-
childSessionKey: CHILD_SESSION_KEY,
3397-
task: "still running",
3398-
});
3399-
await waitForFast(() => {
3400-
expect(
3401-
expectDefined(findRequesterRun(PAUSED_RUN_ID), "paused subagent run").execution.status,
3402-
).toBe("running");
3403-
});
3404-
3405-
expect(
3406-
mod.adoptPausedSubagentRunForFollowUp({
3407-
childSessionKey: CHILD_SESSION_KEY,
3408-
runId: FOLLOW_UP_RUN_ID,
3409-
task: "concurrent work",
3410-
}),
3411-
).toBe(false);
3412-
expect(findRequesterRun(PAUSED_RUN_ID)).toBeDefined();
3413-
expect(findRequesterRun(FOLLOW_UP_RUN_ID)).toBeUndefined();
3414-
});
34153391
});
34163392

34173393
it("ignores a late yield lifecycle event after the paused run is killed", async () => {

‎src/agents/subagent-registry.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,10 @@ export function adoptPausedSubagentRunForFollowUp(params: {
518518
// The original requester is idle behind its own yield, so its wake credential
519519
// is the only path back to it once this follow-up settles.
520520
preserveRequesterSettleWake: true,
521+
// Gateway admission has not started provider work yet. If this owner swap
522+
// is not durable, reject the dispatch instead of registering a sibling or
523+
// leaving a live successor that restart recovery cannot identify.
524+
persistenceFailure: "throw",
521525
// Persist the follow-up text so restart recovery cannot reissue the task that
522526
// the child already yielded on.
523527
task: params.task,

‎src/gateway/server-methods/agent.sessions-and-models.test-utils.ts‎

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -370,50 +370,6 @@ describe("gateway agent handler", () => {
370370
});
371371
});
372372

373-
it("adopts a sessions_yield-paused run when a plugin follow-up targets its session", async () => {
374-
await withTempDir({ prefix: "openclaw-gateway-plugin-subagent-adopt-" }, async (root) => {
375-
useTestStateDir(root);
376-
resetSubagentRegistryForTests({ persist: false });
377-
const childSessionKey = "agent:work:subagent:plugin-yield-followup";
378-
const originalRequester = "agent:main:telegram:direct:777";
379-
addSubagentRunForTests({
380-
runId: "plugin-subagent-paused",
381-
childSessionKey,
382-
requesterSessionKey: originalRequester,
383-
requesterDisplayKey: originalRequester,
384-
task: "wait for the remote job",
385-
endedAt: 2_000,
386-
pauseReason: "sessions_yield",
387-
expectsCompletionMessage: true,
388-
});
389-
390-
await registerPluginSubagentRunFromGateway({
391-
cfg: {
392-
session: { mainKey: "main", scope: "per-sender" },
393-
agents: { list: [{ id: "main", default: true }, { id: "work" }] },
394-
},
395-
runId: "plugin-subagent-followup",
396-
childSessionKey,
397-
task: "the remote job finished",
398-
pluginId: "memory-core",
399-
});
400-
401-
const run = requireValue(
402-
getSubagentRunByChildSessionKey(childSessionKey),
403-
"expected adopted plugin subagent run",
404-
);
405-
// A follow-up without requester context would otherwise register a sibling
406-
// row owned by the child's own agent session, stranding the requester that
407-
// is still parked behind its yield.
408-
expectRecordFields(run, {
409-
runId: "plugin-subagent-followup",
410-
requesterSessionKey: originalRequester,
411-
task: "the remote job finished",
412-
pauseReason: undefined,
413-
});
414-
});
415-
});
416-
417373
it("registers normally when a follow-up to a paused session names its own requester", async () => {
418374
await withTempDir(
419375
{ prefix: "openclaw-gateway-plugin-subagent-own-requester-" },
@@ -525,7 +481,7 @@ describe("gateway agent handler", () => {
525481
);
526482
});
527483

528-
it("rejects plugin SDK subagent runs and releases admission when registry persistence fails", async () => {
484+
it("rejects plugin SDK subagent registration and adoption when persistence fails", async () => {
529485
await withTempDir(
530486
{ prefix: "openclaw-gateway-plugin-subagent-registry-fail-" },
531487
async (root) => {
@@ -602,6 +558,54 @@ describe("gateway agent handler", () => {
602558
expect.stringContaining("rejecting untracked dispatch"),
603559
);
604560

561+
resetSubagentRegistryForTests({ persist: false });
562+
const pausedRunId = "plugin-subagent-paused-before-persistence-failure";
563+
addSubagentRunForTests({
564+
runId: pausedRunId,
565+
childSessionKey,
566+
requesterSessionKey: "agent:main:telegram:direct:777",
567+
requesterDisplayKey: "agent:main:telegram:direct:777",
568+
task: "wait for the remote job",
569+
endedAt: 2_000,
570+
pauseReason: "sessions_yield",
571+
expectsCompletionMessage: true,
572+
});
573+
persistSubagentRunsToDiskOrThrow.mockImplementationOnce(() => {
574+
throw new Error("disk full during paused-run adoption");
575+
});
576+
const adoptionRunId = "plugin-subagent-adoption-registry-fail";
577+
const adoptionRespond = vi.fn();
578+
await invokeAgent(
579+
{
580+
message: "the remote job finished",
581+
sessionKey: childSessionKey,
582+
idempotencyKey: adoptionRunId,
583+
},
584+
{
585+
context,
586+
reqId: adoptionRunId,
587+
respond: adoptionRespond,
588+
client: {
589+
connect: baseClient.connect,
590+
internal: {
591+
...baseClient.internal,
592+
agentRunTracking: "plugin_subagent",
593+
pluginRuntimeOwnerId: "memory-core",
594+
},
595+
},
596+
},
597+
);
598+
599+
expect(mocks.agentCommand).toHaveBeenCalledTimes(commandCallCount);
600+
expect(getSubagentRunByChildSessionKey(childSessionKey)).toMatchObject({
601+
runId: pausedRunId,
602+
pauseReason: "sessions_yield",
603+
});
604+
expect(getSubagentRunByChildSessionKey(childSessionKey)?.runId).not.toBe(adoptionRunId);
605+
const adoptionError = expectRespondError(adoptionRespond, { code: ErrorCodes.UNAVAILABLE });
606+
expectStringFieldContains(adoptionError, "message", "run was not started");
607+
608+
resetSubagentRegistryForTests({ persist: false });
605609
const retryRunId = "plugin-subagent-registry-retry";
606610
await invokeAgent(
607611
{

0 commit comments

Comments
 (0)