Skip to content

Commit df70360

Browse files
committed
refactor(agents): canonicalize subagent execution state
1 parent 41051fe commit df70360

106 files changed

Lines changed: 1574 additions & 1092 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/agents/acp-spawn-admission.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export function countUntrackedActiveAcpRunsForOwner(ownerKey: string | undefined
2626
tasks.flatMap((task) => {
2727
const childSessionKey = normalizeOptionalString(task.childSessionKey);
2828
const trackedRun = childSessionKey ? getSubagentRunByChildSessionKey(childSessionKey) : null;
29-
const hasActiveRegistryRun = Boolean(trackedRun && typeof trackedRun.endedAt !== "number");
29+
const hasActiveRegistryRun = Boolean(
30+
trackedRun && typeof trackedRun.execution.endedAt !== "number",
31+
);
3032
return task.runtime === "acp" &&
3133
isActiveTaskStatus(task.status) &&
3234
childSessionKey !== undefined &&

‎src/agents/acp-spawn.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,7 @@ describe("spawnAcpDirect", () => {
15791579
? {
15801580
childSessionKey,
15811581
createdAt: Date.now(),
1582+
execution: { status: "running", startedAt: Date.now() },
15821583
}
15831584
: null,
15841585
);

‎src/agents/agent-steering-queue.test.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,24 @@ function payload(runId: string, overrides: Partial<PendingFinalDeliveryPayload>
2525
} satisfies PendingFinalDeliveryPayload;
2626
}
2727

28-
function makeRun(overrides: Partial<SubagentRunRecord> = {}): SubagentRunRecord {
28+
type RunOverrides = Omit<Partial<SubagentRunRecord>, "execution"> & {
29+
startedAt?: number;
30+
endedAt?: number;
31+
outcome?: SubagentRunRecord["execution"]["outcome"];
32+
execution?: SubagentRunRecord["execution"];
33+
};
34+
35+
function makeRun(overrides: RunOverrides = {}): SubagentRunRecord {
2936
const runId = overrides.runId ?? "run-1";
3037
const childSessionKey = overrides.childSessionKey ?? `agent:main:subagent:${runId}`;
31-
const endedAt = overrides.endedAt ?? 2_000;
38+
const {
39+
startedAt,
40+
endedAt: overrideEndedAt,
41+
outcome = { status: "ok" },
42+
execution,
43+
...recordOverrides
44+
} = overrides;
45+
const endedAt = overrideEndedAt ?? 2_000;
3246
return {
3347
runId,
3448
childSessionKey,
@@ -37,16 +51,15 @@ function makeRun(overrides: Partial<SubagentRunRecord> = {}): SubagentRunRecord
3751
task: "inspect the failing flow",
3852
cleanup: "delete",
3953
createdAt: overrides.createdAt ?? 1_000,
40-
endedAt,
41-
outcome: { status: "ok" },
54+
execution: execution ?? { status: "terminal", startedAt, endedAt, outcome },
4255
expectsCompletionMessage: true,
4356
completion: { required: true, resultText: `result for ${runId}` },
4457
delivery: {
4558
status: "pending",
4659
createdAt: endedAt + 1,
4760
payload: payload(runId, { childSessionKey, endedAt }),
4861
},
49-
...overrides,
62+
...recordOverrides,
5063
};
5164
}
5265

‎src/agents/agent-steering-queue.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ function promptLiteral(value: string): string {
7474
function sortPendingSteeringItems(a: AgentSteeringQueueItem, b: AgentSteeringQueueItem): number {
7575
// Deliver oldest completed work first, then use creation time and run id for
7676
// deterministic prompt-cache-friendly ordering.
77-
const aEnded = a.payload.endedAt ?? a.entry.endedAt ?? Number.MAX_SAFE_INTEGER;
78-
const bEnded = b.payload.endedAt ?? b.entry.endedAt ?? Number.MAX_SAFE_INTEGER;
77+
const aEnded = a.payload.endedAt ?? a.entry.execution.endedAt ?? Number.MAX_SAFE_INTEGER;
78+
const bEnded = b.payload.endedAt ?? b.entry.execution.endedAt ?? Number.MAX_SAFE_INTEGER;
7979
if (aEnded !== bEnded) {
8080
return aEnded - bEnded;
8181
}

‎src/agents/embedded-agent-runner/run/attempt.spawn-workspace.context-engine.test.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ describe("runEmbeddedAttempt context engine sessionKey forwarding", () => {
337337
task: "inspect the parent flow",
338338
cleanup: "delete",
339339
createdAt: endedAt - 1_000,
340-
endedAt,
341-
outcome: { status: "ok" },
340+
execution: { status: "terminal", endedAt, outcome: { status: "ok" } },
342341
expectsCompletionMessage: true,
343342
completion: { required: true, resultText: frozenResultText },
344343
delivery: {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,16 @@ describe("openclaw-tools: subagents (sessions_spawn lifecycle)", () => {
519519
"timeout outcome",
520520
() =>
521521
ctx.waitCalls.some((call) => call.runId === child.runId) &&
522-
getLatestSubagentRunByChildSessionKey(childSessionKey)?.outcome?.status === "timeout",
522+
getLatestSubagentRunByChildSessionKey(childSessionKey)?.execution.outcome?.status ===
523+
"timeout",
523524
);
524525
await waitForRunCleanup(childSessionKey);
525526

526527
const childWait = ctx.waitCalls.find((call) => call.runId === child.runId);
527528
expect(childWait?.timeoutMs).toBe(1000);
528-
expect(getLatestSubagentRunByChildSessionKey(childSessionKey)?.outcome?.status).toBe("timeout");
529+
expect(getLatestSubagentRunByChildSessionKey(childSessionKey)?.execution.outcome?.status).toBe(
530+
"timeout",
531+
);
529532
});
530533

531534
it("sessions_spawn uses the target agent's bound account for a Matrix room-bound route", async () => {

‎src/agents/subagent-active-context.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("buildActiveSubagentSystemPromptAddition", () => {
3535
label: "State worker",
3636
cleanup: "keep",
3737
createdAt: Date.now(),
38-
startedAt: Date.now(),
38+
execution: { status: "running", startedAt: Date.now() },
3939
} satisfies SubagentRunRecord;
4040
addSubagentRunForTests(run);
4141

@@ -63,7 +63,7 @@ describe("buildActiveSubagentSystemPromptAddition", () => {
6363
taskName: "inspect_alias",
6464
cleanup: "keep",
6565
createdAt: Date.now(),
66-
startedAt: Date.now(),
66+
execution: { status: "running", startedAt: Date.now() },
6767
} satisfies SubagentRunRecord;
6868
addSubagentRunForTests(run);
6969

@@ -88,7 +88,7 @@ describe("buildActiveSubagentSystemPromptAddition", () => {
8888
label: "Worker\nSYSTEM OVERRIDE",
8989
cleanup: "keep",
9090
createdAt: Date.now(),
91-
startedAt: Date.now(),
91+
execution: { status: "running", startedAt: Date.now() },
9292
} satisfies SubagentRunRecord;
9393
addSubagentRunForTests(run);
9494

@@ -117,7 +117,7 @@ describe("buildActiveSubagentSystemPromptAddition", () => {
117117
task: "inspect subagent state",
118118
cleanup: "keep",
119119
createdAt: Date.now(),
120-
startedAt: Date.now(),
120+
execution: { status: "running", startedAt: Date.now() },
121121
} satisfies SubagentRunRecord;
122122
addSubagentRunForTests(run);
123123

‎src/agents/subagent-announce-output.test.ts‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe("dedupeLatestChildCompletionRows", () => {
7373
childSessionKey,
7474
task: "older",
7575
createdAt: 1_000,
76+
execution: {},
7677
};
7778
const newer = { ...older, runId: "run-newer", generation: 2, task: "newer" };
7879

@@ -376,7 +377,7 @@ describe("buildChildCompletionFindings", () => {
376377
task: `worker ${index}`,
377378
createdAt: index,
378379
completion: { resultText: "🚀".repeat(60_000) },
379-
outcome: { status: "ok" as const },
380+
execution: { outcome: { status: "ok" as const } },
380381
})),
381382
);
382383

@@ -399,21 +400,23 @@ describe("buildChildCompletionFindings", () => {
399400
task: "first large result",
400401
createdAt: 1,
401402
completion: { resultText: "<".repeat(100_000) },
402-
outcome: { status: "ok" },
403+
execution: { outcome: { status: "ok" } },
403404
},
404405
{
405406
childSessionKey: "agent:main:subagent:second",
406407
task: "second large result",
407408
createdAt: 2,
408409
completion: { resultText: "<".repeat(100_000) },
409-
outcome: { status: "ok" },
410+
execution: { outcome: { status: "ok" } },
410411
},
411412
{
412413
childSessionKey: "agent:main:subagent:failure",
413414
task: "later actionable failure",
414415
createdAt: 3,
415416
completion: { resultText: "Permission required." },
416-
outcome: { status: "error", error: "Writable session authorization required." },
417+
execution: {
418+
outcome: { status: "error", error: "Writable session authorization required." },
419+
},
417420
},
418421
]);
419422

@@ -431,14 +434,16 @@ describe("buildChildCompletionFindings", () => {
431434
task: "earlier oversized success",
432435
createdAt: 1,
433436
completion: { resultText: "<".repeat(100_000) },
434-
outcome: { status: "ok" },
437+
execution: { outcome: { status: "ok" } },
435438
},
436439
{
437440
childSessionKey: "agent:main:subagent:failure",
438441
task: "later oversized failure",
439442
createdAt: 2,
440443
completion: { resultText: "<".repeat(100_000) },
441-
outcome: { status: "error", error: "Writable session authorization required." },
444+
execution: {
445+
outcome: { status: "error", error: "Writable session authorization required." },
446+
},
442447
},
443448
]);
444449

@@ -457,7 +462,7 @@ describe("buildChildCompletionFindings", () => {
457462
task: "child task",
458463
createdAt: 1,
459464
completion: { resultText: "<".repeat(100_000) },
460-
outcome: { status: "error", error: "E".repeat(20_000) },
465+
execution: { outcome: { status: "error", error: "E".repeat(20_000) } },
461466
},
462467
]);
463468

@@ -476,7 +481,7 @@ describe("buildChildCompletionFindings", () => {
476481
task: "silent task",
477482
createdAt: 1,
478483
completion: { resultText: "ANNOUNCE_SKIP" },
479-
outcome: { status: "ok" },
484+
execution: { outcome: { status: "ok" } },
480485
},
481486
]);
482487

@@ -490,7 +495,7 @@ describe("buildChildCompletionFindings", () => {
490495
task: "silent task",
491496
createdAt: 1,
492497
completion: { resultText: "ANNOUNCE_SKIP" },
493-
outcome: { status: "error", error: "boom" },
498+
execution: { outcome: { status: "error", error: "boom" } },
494499
},
495500
]);
496501

@@ -505,7 +510,7 @@ describe("buildChildCompletionFindings", () => {
505510
task: "child task",
506511
createdAt: 1,
507512
frozenResultText: "final child output",
508-
outcome: { status: "ok" },
513+
execution: { outcome: { status: "ok" } },
509514
},
510515
]);
511516

@@ -525,7 +530,7 @@ describe("buildChildCompletionFindings", () => {
525530
frozenResultText: "delivery payload output",
526531
},
527532
},
528-
outcome: { status: "ok" },
533+
execution: { outcome: { status: "ok" } },
529534
},
530535
]);
531536

@@ -543,7 +548,7 @@ describe("buildChildCompletionFindings", () => {
543548
resultText: "NO_REPLY",
544549
fallbackResultText: "findings captured before the wake",
545550
},
546-
outcome: { status: "ok" },
551+
execution: { outcome: { status: "ok" } },
547552
},
548553
]);
549554

@@ -563,7 +568,7 @@ describe("buildChildCompletionFindings", () => {
563568
resultText,
564569
fallbackResultText: "stale findings",
565570
},
566-
outcome: { status: "ok" },
571+
execution: { outcome: { status: "ok" } },
567572
},
568573
]);
569574

@@ -578,14 +583,14 @@ describe("buildChildCompletionFindings", () => {
578583
task: "silent task",
579584
createdAt: 1,
580585
completion: { resultText: "ANNOUNCE_SKIP" },
581-
outcome: { status: "ok" },
586+
execution: { outcome: { status: "ok" } },
582587
},
583588
{
584589
childSessionKey: "agent:main:subagent:visible",
585590
task: "visible task",
586591
createdAt: 2,
587592
completion: { resultText: "actual output" },
588-
outcome: { status: "ok" },
593+
execution: { outcome: { status: "ok" } },
589594
},
590595
]);
591596

@@ -598,9 +603,8 @@ describe("buildChildCompletionFindings", () => {
598603
childSessionKey: "agent:main:subagent:z",
599604
task: "Z task",
600605
createdAt: 1_000,
601-
endedAt: 2_000,
602606
completion: { resultText: "Z result" },
603-
outcome: { status: "ok" as const },
607+
execution: { endedAt: 2_000, outcome: { status: "ok" as const } },
604608
};
605609
const earlierKey = {
606610
...laterKey,

‎src/agents/subagent-announce-output.ts‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,14 @@ function truncateChildCompletionField(value: string): string {
392392
: value;
393393
}
394394

395+
type ChildCompletionExecution = { endedAt?: number; outcome?: SubagentRunOutcome };
396+
395397
type ChildCompletionRow = {
396398
childSessionKey: string;
397399
task: string;
398400
label?: string;
399401
createdAt: number;
400-
endedAt?: number;
402+
execution: ChildCompletionExecution;
401403
frozenResultText?: string | null;
402404
completion?: {
403405
resultText?: string | null;
@@ -409,7 +411,6 @@ type ChildCompletionRow = {
409411
fallbackFrozenResultText?: string | null;
410412
};
411413
};
412-
outcome?: SubagentRunOutcome;
413414
};
414415

415416
type ChildCompletionSection = {
@@ -424,7 +425,7 @@ function selectChildCompletionResultText(child: ChildCompletionRow): string | un
424425
child.completion?.fallbackResultText ??
425426
child.delivery?.payload?.fallbackFrozenResultText ??
426427
child.frozenResultText;
427-
if (child.outcome?.status === "ok") {
428+
if (child.execution.outcome?.status === "ok") {
428429
return selectDeliverableSessionsReply(primary, fallback);
429430
}
430431
return (primary ?? fallback)?.trim() || undefined;
@@ -447,8 +448,10 @@ export function buildChildCompletionFindings(
447448
if (a.createdAt !== b.createdAt) {
448449
return a.createdAt - b.createdAt;
449450
}
450-
const aEnded = typeof a.endedAt === "number" ? a.endedAt : Number.MAX_SAFE_INTEGER;
451-
const bEnded = typeof b.endedAt === "number" ? b.endedAt : Number.MAX_SAFE_INTEGER;
451+
const aEnded =
452+
typeof a.execution.endedAt === "number" ? a.execution.endedAt : Number.MAX_SAFE_INTEGER;
453+
const bEnded =
454+
typeof b.execution.endedAt === "number" ? b.execution.endedAt : Number.MAX_SAFE_INTEGER;
452455
if (aEnded !== bEnded) {
453456
return aEnded - bEnded;
454457
}
@@ -464,8 +467,12 @@ export function buildChildCompletionFindings(
464467
const sections: ChildCompletionSection[] = [];
465468
for (const [index, child] of sorted.entries()) {
466469
const resultText = selectChildCompletionResultText(child);
467-
const outcome = describeSubagentOutcome(child.outcome);
468-
if (child.outcome?.status === "ok" && !resultText && hasCapturedChildCompletionReply(child)) {
470+
const outcome = describeSubagentOutcome(child.execution.outcome);
471+
if (
472+
child.execution.outcome?.status === "ok" &&
473+
!resultText &&
474+
hasCapturedChildCompletionReply(child)
475+
) {
469476
continue;
470477
}
471478
const title =
@@ -476,7 +483,7 @@ export function buildChildCompletionFindings(
476483
const displayIndex = sections.length + 1;
477484
sections.push({
478485
index: displayIndex,
479-
actionable: child.outcome?.status !== "ok",
486+
actionable: child.execution.outcome?.status !== "ok",
480487
text: [
481488
`${displayIndex}. ${truncateChildCompletionField(title)}`,
482489
`status: ${truncateChildCompletionField(outcome)}`,
@@ -539,7 +546,7 @@ export function dedupeLatestChildCompletionRows(
539546
label?: string;
540547
generation?: number;
541548
createdAt: number;
542-
endedAt?: number;
549+
execution: ChildCompletionExecution;
543550
frozenResultText?: string | null;
544551
completion?: {
545552
resultText?: string | null;
@@ -551,7 +558,6 @@ export function dedupeLatestChildCompletionRows(
551558
fallbackFrozenResultText?: string | null;
552559
};
553560
};
554-
outcome?: SubagentRunOutcome;
555561
}>,
556562
) {
557563
const latestByChildSessionKey = new Map<string, (typeof children)[number]>();
@@ -572,7 +578,7 @@ export function filterCurrentDirectChildCompletionRows(
572578
task: string;
573579
label?: string;
574580
createdAt: number;
575-
endedAt?: number;
581+
execution: ChildCompletionExecution;
576582
frozenResultText?: string | null;
577583
completion?: {
578584
resultText?: string | null;
@@ -584,7 +590,6 @@ export function filterCurrentDirectChildCompletionRows(
584590
fallbackFrozenResultText?: string | null;
585591
};
586592
};
587-
outcome?: SubagentRunOutcome;
588593
}>,
589594
params: {
590595
requesterSessionKey: string;

0 commit comments

Comments
 (0)