Skip to content

Commit bdb3918

Browse files
fix(agent): check updateSessionEntry return value for pendingFinalDelivery write (#102128)
* fix(agent): add requireWriteSuccess:true to pendingFinalDelivery updateSessionEntry call The pendingFinalDelivery write is a critical durable state write where silent failure would cause data loss. This mirrors the same pattern used in 4 other sites. This commit resolves the merge conflict in PR #102128 by moving the change from the old agent-runner.ts to the new agent-runner-result-complete.ts where the code now lives after the refactoring. * fix: update test import path and fix formatting * fix: remove outdated test — store migrated to SQLite The test file pending-final-delivery.store-write-failure.test.ts was written for the legacy file-based store. Since main migrated updateSessionEntry to SQLite, mocking writeTextAtomic/replaceFileAtomic no longer tests the actual code path. SQLite writes are inherently ACID — write failures always propagate. The requireWriteSuccess:true option is retained in agent-runner-result-complete.ts as documentation of intent for this critical state write. * 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. --------- Co-authored-by: Peter Steinberger <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent 699315e commit bdb3918

2 files changed

Lines changed: 126 additions & 11 deletions

File tree

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -387,21 +387,33 @@ export async function completeReplyAgentRun(input: {
387387
runtimePolicySessionKey,
388388
opts,
389389
});
390-
await updateSessionEntry(
390+
const expectedSessionId = activeSessionEntry?.sessionId ?? followupRun.run.sessionId;
391+
// A reset can rebind the key while the model runs; its replacement must
392+
// never inherit the old run's final or advertise an uncommitted intent.
393+
const persistedPendingFinalDelivery = await updateSessionEntry(
391394
{ storePath, sessionKey },
392-
() => ({
393-
pendingFinalDelivery: true,
394-
pendingFinalDeliveryText: resolvedPendingText,
395-
pendingFinalDeliveryIntentId,
396-
pendingFinalDeliveryContext,
397-
pendingFinalDeliveryCreatedAt: Date.now(),
398-
updatedAt: Date.now(),
399-
}),
395+
(entry) =>
396+
entry.sessionId === expectedSessionId
397+
? {
398+
pendingFinalDelivery: true,
399+
pendingFinalDeliveryText: resolvedPendingText,
400+
pendingFinalDeliveryIntentId,
401+
pendingFinalDeliveryContext,
402+
pendingFinalDeliveryCreatedAt: Date.now(),
403+
updatedAt: Date.now(),
404+
}
405+
: null,
400406
{
401407
skipMaintenance: true,
402408
takeCacheOwnership: true,
403409
},
404410
);
411+
if (
412+
persistedPendingFinalDelivery?.sessionId !== expectedSessionId ||
413+
persistedPendingFinalDelivery.pendingFinalDeliveryIntentId !== pendingFinalDeliveryIntentId
414+
) {
415+
throw new Error("pending final delivery session changed or was deleted");
416+
}
405417
}
406418
}
407419
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

@@ -1164,6 +1165,108 @@ describe("runReplyAgent pending final delivery capture", () => {
11641165
});
11651166
});
11661167

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

0 commit comments

Comments
 (0)