Skip to content

Commit 1f3b4e8

Browse files
bladinsteipete
andauthored
fix(agents): keep not_required delivery status when completion_announced_at exists (#108710)
* fix(agents): keep not_required delivery status when completion_announced_at exists for expectsCompletionMessage=false runs createDeliveryFromTypedColumns unconditionally overrode status to 'delivered' when completion_announced_at was present, even for runs where expects_completion_message is false. This produced a contradictory delivery state where a run that does not require completion delivery was marked as delivered. Only set status to 'delivered' when expects_completion_message is true. For not_required runs that still have completion_announced_at recorded (for informational purposes), preserve announcedAt without overriding the status. * test(agents): cover tainted delivery rows --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 0815f6a commit 1f3b4e8

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import fs from "node:fs/promises";
33
import os from "node:os";
44
import path from "node:path";
55
import { afterEach, beforeEach, describe, expect, it } from "vitest";
6+
import { executeSqliteQuerySync, getNodeSqliteKysely } from "../infra/kysely-sync.js";
7+
import type { DB as OpenClawStateKyselyDatabase } from "../state/openclaw-state-db.generated.js";
68
import {
79
closeOpenClawStateDatabaseForTest,
810
openOpenClawStateDatabase,
@@ -14,6 +16,8 @@ import {
1416
} from "./subagent-registry.store.sqlite.js";
1517
import type { SubagentRunRecord } from "./subagent-registry.types.js";
1618

19+
type SubagentRegistryDatabase = Pick<OpenClawStateKyselyDatabase, "subagent_runs">;
20+
1721
function createRun(overrides: Partial<SubagentRunRecord> = {}): SubagentRunRecord {
1822
return {
1923
runId: "run-one",
@@ -121,6 +125,57 @@ describe("subagent registry sqlite store", () => {
121125
});
122126
});
123127

128+
it("preserves announcedAt for not_required delivery when completion was announced", async () => {
129+
await withTempStateEnv(async () => {
130+
const run = createRun({
131+
expectsCompletionMessage: false,
132+
completion: { required: false },
133+
delivery: { status: "not_required", announcedAt: 300 },
134+
});
135+
136+
saveSubagentRegistryToSqlite(new Map([[run.runId, run]]));
137+
138+
const restored = loadSubagentRegistryFromSqlite();
139+
const restoredRun = restored.get(run.runId)!;
140+
expect(restoredRun.delivery?.status).toBe("not_required");
141+
expect(restoredRun.delivery?.announcedAt).toBe(300);
142+
expect(restoredRun.delivery?.deliveredAt).toBeUndefined();
143+
});
144+
});
145+
146+
it("repairs a tainted delivered status when completion is not required", async () => {
147+
await withTempStateEnv(async () => {
148+
const run = createRun({
149+
expectsCompletionMessage: false,
150+
completion: { required: false },
151+
delivery: { status: "not_required", announcedAt: 300 },
152+
});
153+
saveSubagentRegistryToSqlite(new Map([[run.runId, run]]));
154+
155+
const { db } = openOpenClawStateDatabase();
156+
const stateDb = getNodeSqliteKysely<SubagentRegistryDatabase>(db);
157+
executeSqliteQuerySync(
158+
db,
159+
stateDb
160+
.updateTable("subagent_runs")
161+
.set({
162+
payload_json: JSON.stringify({
163+
...run,
164+
delivery: { status: "delivered", announcedAt: 300, deliveredAt: 300 },
165+
}),
166+
})
167+
.where("run_id", "=", run.runId),
168+
);
169+
170+
const restoredRun = loadSubagentRegistryFromSqlite().get(run.runId)!;
171+
expect(restoredRun.delivery).toMatchObject({
172+
status: "not_required",
173+
announcedAt: 300,
174+
deliveredAt: 300,
175+
});
176+
});
177+
});
178+
124179
it("does not read or delete the retired JSON registry at runtime", async () => {
125180
await withTempStateEnv(async () => {
126181
const legacyRun = createRun({

‎src/agents/subagent-registry.store.sqlite.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,16 @@ function createDeliveryFromTypedColumns(
8989
...(row.pending_final_delivery_last_error !== null
9090
? { lastError: row.pending_final_delivery_last_error }
9191
: {}),
92-
...(row.completion_announced_at !== null
92+
...(row.completion_announced_at !== null && row.expects_completion_message === 1
9393
? {
9494
status: "delivered",
9595
announcedAt: row.completion_announced_at,
9696
deliveredAt: delivery?.deliveredAt ?? row.completion_announced_at,
9797
}
98-
: {}),
98+
: row.completion_announced_at !== null
99+
? { announcedAt: row.completion_announced_at }
100+
: {}),
101+
...(row.expects_completion_message === 0 ? { status: "not_required" } : {}),
99102
};
100103
}
101104

0 commit comments

Comments
 (0)