Skip to content

Commit 778bd6a

Browse files
committed
test(subagents): stabilize completion recovery after restore replay (#133058)
The restore replay is canonical bookkeeping: it publishes the blocked alert/follow-up, not the retained provider result, and completion retry/dismiss resolve by exact task-run identity without gating on task.deliveryStatus (admission validates a freshly projected session_queued generation, not the stored row). The recovery-identity and admission-store tests captured pre-replay rows and raced the startup replay; settle the replay deterministically before capturing baselines and assert the exact canonical post-replay task state. Also type the replay catch callback parameter as unknown (use-unknown-in-catch-callback-variable).
1 parent 5d607eb commit 778bd6a

3 files changed

Lines changed: 41 additions & 7 deletions

File tree

‎src/agents/subagents/completion/subagent-completion-admission.store.test.ts‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ describe("atomic subagent completion admission store", () => {
133133
);
134134
}
135135

136+
async function waitForRestoredSessionQueue(taskId: string): Promise<number> {
137+
ensureTaskRegistryReady();
138+
await vi.waitFor(() => {
139+
expect(
140+
database.db
141+
.prepare("SELECT delivery_status FROM task_runs WHERE task_id = ?")
142+
.get(taskId),
143+
).toEqual({ delivery_status: "session_queued" });
144+
});
145+
const restored = database.db
146+
.prepare("SELECT last_event_at FROM task_runs WHERE task_id = ?")
147+
.get(taskId) as { last_event_at: number | null } | undefined;
148+
if (typeof restored?.last_event_at !== "number") {
149+
throw new Error("restored task replay did not persist its event timestamp");
150+
}
151+
return restored.last_event_at;
152+
}
153+
136154
it.each(["queue", "subagent", "task"] as const)(
137155
"rolls every owner row back when the %s cut fails",
138156
(cut) => {
@@ -389,7 +407,7 @@ describe("atomic subagent completion admission store", () => {
389407
for (const [runId, entry] of loadSubagentRegistryFromSqlite()) {
390408
subagentRuns.set(runId, entry);
391409
}
392-
ensureTaskRegistryReady();
410+
const replayedLastEventAt = await waitForRestoredSessionQueue(input.task.taskId);
393411
expect(subagentRuns.get(input.subagent.runId)?.delivery).toMatchObject({
394412
status: "suspended",
395413
disposition: "permanent_failure",
@@ -403,10 +421,10 @@ describe("atomic subagent completion admission store", () => {
403421
expect(subagentRuns.get(input.subagent.runId)?.delivery?.payload).not.toHaveProperty(
404422
"frozenResultText",
405423
);
406-
expect(getTaskById(input.task.taskId)).toMatchObject({
407-
deliveryStatus: "failed",
408-
terminalOutcome: "blocked",
409-
cleanupAfter: input.task.cleanupAfter,
424+
expect(getTaskById(input.task.taskId)).toEqual({
425+
...input.task,
426+
deliveryStatus: "session_queued",
427+
lastEventAt: replayedLastEventAt,
410428
progressSummary: "canonical result",
411429
});
412430

‎src/agents/subagents/completion/subagent-completion-delivery.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
openOpenClawStateDatabase,
77
type OpenClawStateDatabase,
88
} from "../../../state/openclaw-state-db.js";
9-
import { getTaskById } from "../../../tasks/runtime-internal.js";
9+
import { ensureTaskRegistryReady, getTaskById } from "../../../tasks/runtime-internal.js";
1010
import type { TaskRecord } from "../../../tasks/task-registry.types.js";
1111
import { resetTaskRegistryForTests } from "../../../tasks/task-runtime.test-helpers.js";
1212
import { createSubagentRunRecord } from "../../subagent-test-fixtures.test-helpers.js";
@@ -102,6 +102,17 @@ describe("subagent completion recovery identity", () => {
102102
};
103103
}
104104

105+
async function waitForRestoredSessionQueue(
106+
...pairs: Array<ReturnType<typeof persistCompletion>>
107+
) {
108+
ensureTaskRegistryReady();
109+
await vi.waitFor(() => {
110+
for (const pair of pairs) {
111+
expect(storedPair(pair).task).toMatchObject({ delivery_status: "session_queued" });
112+
}
113+
});
114+
}
115+
105116
function dismiss(taskId: string) {
106117
return dismissSubagentCompletionDelivery(taskId, {
107118
discardTerminalDelivery: SubagentLifecycleController.discardTerminalDelivery,
@@ -115,6 +126,7 @@ describe("subagent completion recovery identity", () => {
115126
// Retained completions precede follow-up runs on the same reusable child session.
116127
const old = persistCompletion("old");
117128
const current = persistCompletion("current", "suspended", remapped);
129+
await waitForRestoredSessionQueue(old, current);
118130
const oldRows = storedPair(old);
119131
const oldLive = structuredClone(old.subagent);
120132

@@ -148,6 +160,9 @@ describe("subagent completion recovery identity", () => {
148160
async (oldStatus) => {
149161
const old = persistCompletion("old", oldStatus);
150162
const current = persistCompletion("current", "suspended", true);
163+
await waitForRestoredSessionQueue(
164+
...(oldStatus === "suspended" ? [old, current] : [current]),
165+
);
151166
const oldRows = storedPair(old);
152167
const oldLive = structuredClone(old.subagent);
153168

@@ -186,6 +201,7 @@ describe("subagent completion recovery identity", () => {
186201
const current = persistCompletion("current");
187202
subagentRuns.delete(current.subagent.runId);
188203
database.db.prepare("DELETE FROM subagent_runs WHERE run_id = ?").run(current.subagent.runId);
204+
await waitForRestoredSessionQueue(old, current);
189205
const oldRows = storedPair(old);
190206
const currentRows = storedPair(current);
191207
const oldLive = structuredClone(old.subagent);

‎src/tasks/task-registry-lifecycle.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const ACTIVITY_LIVENESS_WRITE_MS = 60_000;
2828

2929
function replayRestoredTaskTerminalDeliveries(taskIds: readonly string[]) {
3030
for (const taskId of taskIds) {
31-
void maybeDeliverTaskTerminalUpdate(taskId).catch((error) => {
31+
void maybeDeliverTaskTerminalUpdate(taskId).catch((error: unknown) => {
3232
taskRegistryLog.warn("Failed to replay restored task terminal delivery", {
3333
taskId,
3434
error,

0 commit comments

Comments
 (0)