Skip to content

Commit f75a7ed

Browse files
committed
feat(durable): record subagent child runs
1 parent f1e88d4 commit f75a7ed

3 files changed

Lines changed: 439 additions & 2 deletions

File tree

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
import { getRuntimeConfig } from "../config/config.js";
77
import type { OpenClawConfig } from "../config/types.openclaw.js";
8+
import {
9+
recordDurableSubagentRegistered,
10+
recordDurableSubagentTerminal,
11+
} from "../durable/subagent.js";
812
import { callGateway } from "../gateway/call.js";
913
import { createSubsystemLogger } from "../logging/subsystem.js";
1014
import { getGlobalHookRunner } from "../plugins/hook-runner-global.js";
@@ -313,6 +317,11 @@ export function createSubagentRunManager(params: {
313317
timeoutCompletion.startedAt = startedAt;
314318
}
315319
completionForRetry = timeoutCompletion;
320+
recordDurableSubagentTerminal({
321+
runId,
322+
childSessionKey: entry.childSessionKey,
323+
status: "timeout",
324+
});
316325
await params.completeSubagentRun(completionForRetry);
317326
};
318327
if (waitStatus === "timeout") {
@@ -441,6 +450,12 @@ export function createSubagentRunManager(params: {
441450
triggerCleanup: true,
442451
startedAt: observedStartedAt,
443452
};
453+
recordDurableSubagentTerminal({
454+
runId,
455+
childSessionKey: entry.childSessionKey,
456+
status: outcome.status,
457+
error: outcome.status === "error" ? outcome.error : undefined,
458+
});
444459
await params.completeSubagentRun(completionForRetry);
445460
} catch (error) {
446461
const current = params.runs.get(runId);
@@ -681,8 +696,9 @@ export function createSubagentRunManager(params: {
681696
params.runs.delete(runId);
682697
throw error;
683698
}
699+
let backgroundTask: ReturnType<typeof createRunningTaskRun> | null | undefined;
684700
try {
685-
const task = createRunningTaskRun({
701+
backgroundTask = createRunningTaskRun({
686702
runtime: "subagent",
687703
sourceId: runId,
688704
ownerKey: requesterSessionKey,
@@ -699,7 +715,7 @@ export function createSubagentRunManager(params: {
699715
startedAt: now,
700716
lastEventAt: now,
701717
});
702-
if (!task) {
718+
if (!backgroundTask) {
703719
log.warn("Failed to persist background task for subagent run", {
704720
runId: registerParams.runId,
705721
});
@@ -710,6 +726,18 @@ export function createSubagentRunManager(params: {
710726
error,
711727
});
712728
}
729+
recordDurableSubagentRegistered({
730+
runId,
731+
childSessionKey,
732+
requesterSessionKey,
733+
taskId: backgroundTask?.taskId,
734+
taskFlowId: backgroundTask?.parentFlowId,
735+
task: registerParams.task,
736+
taskName: registerParams.taskName,
737+
label: registerParams.label,
738+
agentId: registerParams.agentId,
739+
requesterAgentId: registerParams.requesterAgentId,
740+
});
713741
params.ensureListener();
714742
params.persist();
715743
// Always start sweeper — session-mode runs (no archiveAtMs) also need TTL cleanup.
@@ -787,6 +815,12 @@ export function createSubagentRunManager(params: {
787815
entry.cleanupHandled = true;
788816
entry.cleanupCompletedAt = now;
789817
entry.suppressAnnounceReason = "killed";
818+
recordDurableSubagentTerminal({
819+
runId,
820+
childSessionKey: entry.childSessionKey,
821+
status: "killed",
822+
error: reason,
823+
});
790824
if (!entriesByChildSessionKey.has(entry.childSessionKey)) {
791825
entriesByChildSessionKey.set(entry.childSessionKey, entry);
792826
}

‎src/durable/subagent.test.ts‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import fs from "node:fs";
2+
import os from "node:os";
3+
import path from "node:path";
4+
import { describe, expect, it } from "vitest";
5+
import { openDurableWorkflowSqliteStore } from "./sqlite-store.js";
6+
import { recordDurableSubagentRegistered, recordDurableSubagentTerminal } from "./subagent.js";
7+
import {
8+
DURABLE_AGENT_TURN_WORKFLOW_ID,
9+
DURABLE_SUBAGENT_RUN_WORKFLOW_ID,
10+
} from "./workflow-ids.js";
11+
12+
describe("durable subagent bridge", () => {
13+
it("preserves background task and taskflow bindings on child runs and parent links", () => {
14+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-durable-subagent-"));
15+
const dbPath = path.join(dir, "workflows.sqlite");
16+
const env = {
17+
...process.env,
18+
OPENCLAW_DURABLE_WORKFLOWS: "1",
19+
OPENCLAW_DURABLE_WORKFLOWS_DB_PATH: dbPath,
20+
};
21+
const parentSessionKey = "agent:bo:discord:channel:bo-main";
22+
const childSessionKey = "agent:bo:subagent:workboard-default-card";
23+
24+
const setupStore = openDurableWorkflowSqliteStore({ path: dbPath });
25+
try {
26+
setupStore.createRun({
27+
workflowId: DURABLE_AGENT_TURN_WORKFLOW_ID,
28+
status: "running",
29+
recoveryState: "running",
30+
sourceType: "agent_turn",
31+
sourceRef: parentSessionKey,
32+
metadata: { sessionKey: parentSessionKey },
33+
now: 100,
34+
});
35+
} finally {
36+
setupStore.close();
37+
}
38+
39+
recordDurableSubagentRegistered({
40+
runId: "run_child",
41+
childSessionKey,
42+
requesterSessionKey: parentSessionKey,
43+
taskId: "task_child",
44+
taskFlowId: "flow_child",
45+
task: "Summarize durable bridge",
46+
label: "durable bridge",
47+
agentId: "bo",
48+
requesterAgentId: "bo",
49+
env,
50+
});
51+
52+
recordDurableSubagentTerminal({
53+
runId: "run_child",
54+
childSessionKey,
55+
status: "success",
56+
summary: "done",
57+
env,
58+
});
59+
60+
const assertStore = openDurableWorkflowSqliteStore({ path: dbPath });
61+
try {
62+
const child = assertStore
63+
.listRuns({ limit: 20 })
64+
.find((run) => run.workflowId === DURABLE_SUBAGENT_RUN_WORKFLOW_ID);
65+
expect(child).toMatchObject({
66+
status: "succeeded",
67+
metadata: {
68+
runId: "run_child",
69+
taskId: "task_child",
70+
taskFlowId: "flow_child",
71+
childSessionKey,
72+
agentId: "bo",
73+
requesterAgentId: "bo",
74+
summary: "done",
75+
},
76+
});
77+
expect(child).toBeDefined();
78+
const parentLink = assertStore.listParentLinks(child!.workflowRunId)[0];
79+
expect(parentLink).toMatchObject({
80+
status: "succeeded",
81+
metadata: {
82+
runId: "run_child",
83+
taskId: "task_child",
84+
taskFlowId: "flow_child",
85+
childSessionKey,
86+
summary: "done",
87+
},
88+
});
89+
} finally {
90+
assertStore.close();
91+
fs.rmSync(dir, { recursive: true, force: true });
92+
}
93+
});
94+
});

0 commit comments

Comments
 (0)