Skip to content

Commit b8c23e2

Browse files
committed
fix(auto-reply): verify durable final delivery session
Preserve the session identity and committed pending intent before returning a final reply. Reject deleted or reset SQLite sessions without poisoning replacements, and cover the production reply-runner path. Follow-up to #102128 by @SunnyShu0925.
1 parent ce6fcd0 commit b8c23e2

2 files changed

Lines changed: 126 additions & 12 deletions

File tree

‎src/auto-reply/reply/agent-runner-result-complete.ts‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -399,22 +399,33 @@ export async function completeReplyAgentRun(input: {
399399
runtimePolicySessionKey,
400400
opts,
401401
});
402-
await updateSessionEntry(
402+
const expectedSessionId = activeSessionEntry?.sessionId ?? followupRun.run.sessionId;
403+
// A reset can rebind the key while the model runs; its replacement must
404+
// never inherit the old run's final or advertise an uncommitted intent.
405+
const persistedPendingFinalDelivery = await updateSessionEntry(
403406
{ storePath, sessionKey },
404-
() => ({
405-
pendingFinalDelivery: true,
406-
pendingFinalDeliveryText: resolvedPendingText,
407-
pendingFinalDeliveryIntentId,
408-
pendingFinalDeliveryContext,
409-
pendingFinalDeliveryCreatedAt: Date.now(),
410-
updatedAt: Date.now(),
411-
}),
407+
(entry) =>
408+
entry.sessionId === expectedSessionId
409+
? {
410+
pendingFinalDelivery: true,
411+
pendingFinalDeliveryText: resolvedPendingText,
412+
pendingFinalDeliveryIntentId,
413+
pendingFinalDeliveryContext,
414+
pendingFinalDeliveryCreatedAt: Date.now(),
415+
updatedAt: Date.now(),
416+
}
417+
: null,
412418
{
413419
skipMaintenance: true,
414420
takeCacheOwnership: true,
415-
requireWriteSuccess: true,
416421
},
417422
);
423+
if (
424+
persistedPendingFinalDelivery?.sessionId !== expectedSessionId ||
425+
persistedPendingFinalDelivery.pendingFinalDeliveryIntentId !== pendingFinalDeliveryIntentId
426+
) {
427+
throw new Error("pending final delivery session changed or was deleted");
428+
}
418429
}
419430
}
420431
const result = returnWithQueuedFollowupDrain(

‎src/auto-reply/reply/agent-runner.runreplyagent.e2e.test.ts‎

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite
66
import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js";
77
import type { SessionEntry } from "../../config/sessions.js";
88
import {
9+
applySessionEntryLifecycleMutation,
910
loadSessionEntry,
1011
loadTranscriptEvents,
1112
replaceSessionEntry,
@@ -114,10 +115,10 @@ function requireStoredSessionEntry(storePath: string, sessionKey = "main"): Sess
114115
return entry;
115116
}
116117

117-
async function createSessionStoreFile(entry: SessionEntry): Promise<string> {
118+
async function createSessionStoreFile(entry: SessionEntry, sessionKey = "main"): Promise<string> {
118119
const dir = tempDirs.make("openclaw-agent-runner-");
119120
const storePath = join(dir, "sessions.json");
120-
await replaceSessionEntry({ storePath, sessionKey: "main" }, entry);
121+
await replaceSessionEntry({ storePath, sessionKey }, entry);
121122
return storePath;
122123
}
123124

@@ -1161,6 +1162,108 @@ describe("runReplyAgent pending final delivery capture", () => {
11611162
});
11621163
});
11631164

1165+
it("persists canonical SQLite pending final delivery after its intent commits", async () => {
1166+
const sessionKey = "agent:main:main";
1167+
const sessionEntry: SessionEntry = {
1168+
sessionId: "session",
1169+
updatedAt: Date.now(),
1170+
};
1171+
const sessionStore = { [sessionKey]: sessionEntry };
1172+
const storePath = await createSessionStoreFile(sessionEntry, sessionKey);
1173+
state.runEmbeddedAgentMock.mockResolvedValueOnce({
1174+
payloads: [{ text: "visible canonical final" }],
1175+
meta: {},
1176+
});
1177+
1178+
const { run } = createMinimalRun({
1179+
sessionEntry,
1180+
sessionStore,
1181+
sessionKey,
1182+
storePath,
1183+
});
1184+
1185+
const result = await run();
1186+
const stored = loadSessionEntry({ sessionKey, storePath });
1187+
expect(stored).toMatchObject({
1188+
pendingFinalDelivery: true,
1189+
pendingFinalDeliveryIntentId: expect.any(String),
1190+
pendingFinalDeliveryText: "visible canonical final",
1191+
sessionId: "session",
1192+
});
1193+
const visiblePayload = Array.isArray(result) ? result[0] : result;
1194+
expect(getReplyPayloadMetadata(visiblePayload ?? {})).toMatchObject({
1195+
pendingFinalDeliveryIntentId: stored?.pendingFinalDeliveryIntentId,
1196+
pendingFinalDeliveryRetryText: "visible canonical final",
1197+
});
1198+
expect(state.runEmbeddedAgentMock).toHaveBeenCalledOnce();
1199+
});
1200+
1201+
it("rejects canonical SQLite pending final delivery when its session is deleted", async () => {
1202+
const sessionKey = "agent:main:main";
1203+
const sessionEntry: SessionEntry = {
1204+
sessionId: "session",
1205+
updatedAt: Date.now(),
1206+
};
1207+
const sessionStore = { [sessionKey]: sessionEntry };
1208+
const storePath = await createSessionStoreFile(sessionEntry, sessionKey);
1209+
state.runEmbeddedAgentMock.mockImplementationOnce(async () => {
1210+
await applySessionEntryLifecycleMutation({
1211+
removals: [{ sessionKey }],
1212+
skipMaintenance: true,
1213+
storePath,
1214+
});
1215+
return {
1216+
payloads: [{ text: "final from deleted session" }],
1217+
meta: {},
1218+
};
1219+
});
1220+
1221+
const { run } = createMinimalRun({
1222+
sessionEntry,
1223+
sessionStore,
1224+
sessionKey,
1225+
storePath,
1226+
});
1227+
1228+
await expect(run()).rejects.toThrow("pending final delivery session changed or was deleted");
1229+
expect(loadSessionEntry({ sessionKey, storePath })).toBeUndefined();
1230+
expect(state.runEmbeddedAgentMock).toHaveBeenCalledOnce();
1231+
});
1232+
1233+
it("does not persist canonical SQLite pending final delivery on a reset session", async () => {
1234+
const sessionKey = "agent:main:main";
1235+
const sessionEntry: SessionEntry = {
1236+
sessionId: "session",
1237+
updatedAt: Date.now(),
1238+
};
1239+
const sessionStore = { [sessionKey]: sessionEntry };
1240+
const storePath = await createSessionStoreFile(sessionEntry, sessionKey);
1241+
state.runEmbeddedAgentMock.mockImplementationOnce(async () => {
1242+
await replaceSessionEntry(
1243+
{ sessionKey, storePath },
1244+
{ sessionId: "session-after-reset", updatedAt: Date.now() },
1245+
);
1246+
return {
1247+
payloads: [{ text: "final from previous session" }],
1248+
meta: {},
1249+
};
1250+
});
1251+
1252+
const { run } = createMinimalRun({
1253+
sessionEntry,
1254+
sessionStore,
1255+
sessionKey,
1256+
storePath,
1257+
});
1258+
1259+
await expect(run()).rejects.toThrow("pending final delivery session changed or was deleted");
1260+
expect(loadSessionEntry({ sessionKey, storePath })).toMatchObject({
1261+
sessionId: "session-after-reset",
1262+
});
1263+
expect(loadSessionEntry({ sessionKey, storePath })?.pendingFinalDelivery).toBeUndefined();
1264+
expect(state.runEmbeddedAgentMock).toHaveBeenCalledOnce();
1265+
});
1266+
11641267
it("persists auto-reply delivery context for restart recovery", async () => {
11651268
const sessionEntry: SessionEntry = {
11661269
sessionId: "session",

0 commit comments

Comments
 (0)