@@ -6,6 +6,7 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite
66import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js" ;
77import type { SessionEntry } from "../../config/sessions.js" ;
88import {
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