Skip to content

Commit e95b669

Browse files
committed
fix(durable): map ok subagent outcomes
1 parent db39743 commit e95b669

3 files changed

Lines changed: 117 additions & 1 deletion

File tree

‎src/agents/openclaw-tools.subagents.sessions-spawn.lifecycle.test.ts‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
// Verifies sessions_spawn lifecycle hooks, cleanup, and completion announcements.
2+
import fs from "node:fs";
3+
import os from "node:os";
4+
import path from "node:path";
25
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
36
import type { AgentRouteBinding } from "../config/types.agents.js";
7+
import { openDurableWorkflowSqliteStore } from "../durable/sqlite-store.js";
8+
import {
9+
DURABLE_AGENT_TURN_WORKFLOW_ID,
10+
DURABLE_SUBAGENT_RUN_WORKFLOW_ID,
11+
} from "../durable/workflow-ids.js";
412
import { emitAgentEvent } from "../infra/agent-events.js";
513
import {
614
testing as bundleMcpRuntimeTesting,
@@ -317,6 +325,111 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
317325
expect(childAgentCall?.timeoutMs).toBe(125_000);
318326
});
319327

328+
it("maps successful agent.wait ok outcomes to durable succeeded child links", async () => {
329+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sessions-spawn-durable-"));
330+
const dbPath = path.join(dir, "state", "openclaw.sqlite");
331+
const previousDurableWorkflows = process.env.OPENCLAW_DURABLE_WORKFLOWS;
332+
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
333+
const parentSessionKey = "main";
334+
const requesterRunId = "run_parent_ok";
335+
let parentWorkflowRunId = "";
336+
337+
process.env.OPENCLAW_DURABLE_WORKFLOWS = "1";
338+
process.env.OPENCLAW_STATE_DIR = dir;
339+
try {
340+
const setupStore = openDurableWorkflowSqliteStore({ path: dbPath });
341+
try {
342+
parentWorkflowRunId = setupStore.createRun({
343+
workflowId: DURABLE_AGENT_TURN_WORKFLOW_ID,
344+
status: "running",
345+
recoveryState: "running",
346+
idempotencyKey: requesterRunId,
347+
sourceType: "agent_turn",
348+
sourceRef: parentSessionKey,
349+
metadata: { sessionKey: parentSessionKey },
350+
now: 100,
351+
}).workflowRunId;
352+
} finally {
353+
setupStore.close();
354+
}
355+
356+
const ctx = setupSessionsSpawnGatewayMock({
357+
includeChatHistory: true,
358+
agentWaitResult: { status: "ok", startedAt: 1000, endedAt: 2000 },
359+
});
360+
const tool = await getSessionsSpawnTool({
361+
agentSessionKey: "main",
362+
agentChannel: "whatsapp",
363+
requesterRunId,
364+
});
365+
366+
await executeSpawnAndExpectAccepted({
367+
tool,
368+
callId: "call-durable-ok",
369+
cleanup: "keep",
370+
label: "durable-ok",
371+
});
372+
373+
const child = ctx.getChild();
374+
if (!child.runId) {
375+
throw new Error("missing child runId");
376+
}
377+
if (!child.sessionKey) {
378+
throw new Error("missing child sessionKey");
379+
}
380+
await waitForSessionsSpawnEvent(
381+
"durable ok outcome",
382+
() =>
383+
ctx.waitCalls.some((call) => call.runId === child.runId) &&
384+
getLatestSubagentRunByChildSessionKey(child.sessionKey!)?.outcome?.status === "ok",
385+
);
386+
await waitForRunCleanup(child.sessionKey);
387+
388+
const assertStore = openDurableWorkflowSqliteStore({ path: dbPath });
389+
try {
390+
const durableChild = assertStore
391+
.listRuns({ limit: 50 })
392+
.find(
393+
(run) =>
394+
run.workflowId === DURABLE_SUBAGENT_RUN_WORKFLOW_ID &&
395+
run.idempotencyKey === child.runId,
396+
);
397+
expect(durableChild).toMatchObject({
398+
status: "succeeded",
399+
recoveryState: "terminal",
400+
parentWorkflowRunId,
401+
metadata: expect.objectContaining({
402+
childSessionKey: child.sessionKey,
403+
status: "ok",
404+
}),
405+
});
406+
const parentLink = assertStore.listParentLinks(durableChild!.workflowRunId)[0];
407+
expect(parentLink).toMatchObject({
408+
parentWorkflowRunId,
409+
status: "succeeded",
410+
metadata: expect.objectContaining({
411+
childSessionKey: child.sessionKey,
412+
status: "ok",
413+
}),
414+
});
415+
} finally {
416+
assertStore.close();
417+
}
418+
} finally {
419+
if (previousDurableWorkflows === undefined) {
420+
delete process.env.OPENCLAW_DURABLE_WORKFLOWS;
421+
} else {
422+
process.env.OPENCLAW_DURABLE_WORKFLOWS = previousDurableWorkflows;
423+
}
424+
if (previousStateDir === undefined) {
425+
delete process.env.OPENCLAW_STATE_DIR;
426+
} else {
427+
process.env.OPENCLAW_STATE_DIR = previousStateDir;
428+
}
429+
fs.rmSync(dir, { recursive: true, force: true });
430+
}
431+
});
432+
320433
it("sessions_spawn retires bundle MCP runtime when run-mode cleanup completes", async () => {
321434
let resumeAnnounceFlow: ((value: boolean) => void) | undefined;
322435
let announceFlowStarted: (() => void) | undefined;

‎src/durable/subagent.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ describe("durable subagent bridge", () => {
184184
recordDurableSubagentTerminal({
185185
runId: "run_child",
186186
childSessionKey,
187-
status: "success",
187+
status: "ok",
188188
summary: "done",
189189
env,
190190
});
@@ -202,6 +202,7 @@ describe("durable subagent bridge", () => {
202202
taskFlowId: "flow_child",
203203
taskHash: expect.any(String),
204204
childSessionKey,
205+
status: "ok",
205206
agentId: "bo",
206207
requesterAgentId: "bo",
207208
summary: "done",
@@ -217,6 +218,7 @@ describe("durable subagent bridge", () => {
217218
taskFlowId: "flow_child",
218219
taskHash: expect.any(String),
219220
childSessionKey,
221+
status: "ok",
220222
summary: "done",
221223
},
222224
});

‎src/durable/subagent.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function findLatestOpenParentRun(params: {
7575

7676
function mapOutcomeToLinkStatus(status: string | undefined): DurableWorkflowLinkStatus {
7777
switch (status) {
78+
case "ok":
7879
case "success":
7980
case "succeeded":
8081
return "succeeded";

0 commit comments

Comments
 (0)