@@ -14,6 +14,10 @@ import type { OpenClawConfig } from "../../config/config.js";
1414import * as sessionTypesModule from "../../config/sessions.js" ;
1515import type { SessionEntry } from "../../config/sessions.js" ;
1616import { loadSessionStore , saveSessionStore } from "../../config/sessions.js" ;
17+ import {
18+ readSessionStoreForTest ,
19+ writeSessionStoreForTestAsync ,
20+ } from "../../config/sessions/test-helpers.js" ;
1721import {
1822 onInternalDiagnosticEvent ,
1923 resetDiagnosticEventsForTest ,
@@ -248,6 +252,16 @@ function firstMockCallArg(mock: MockCallSource, label: string): unknown {
248252 return call [ 0 ] ;
249253}
250254
255+ async function seedSessionStore ( params : {
256+ storePath : string ;
257+ sessionKey : string ;
258+ entry : Record < string , unknown > ;
259+ } ) {
260+ await writeSessionStoreForTestAsync ( params . storePath , {
261+ [ params . sessionKey ] : params . entry as SessionEntry ,
262+ } ) ;
263+ }
264+
251265beforeEach ( ( ) => {
252266 vi . useRealTimers ( ) ;
253267 registerCliBackendsForTest ( ) ;
@@ -298,19 +312,6 @@ afterEach(() => {
298312} ) ;
299313
300314describe ( "runReplyAgent auto-compaction token update" , ( ) => {
301- async function seedSessionStore ( params : {
302- storePath : string ;
303- sessionKey : string ;
304- entry : Record < string , unknown > ;
305- } ) {
306- await fs . mkdir ( path . dirname ( params . storePath ) , { recursive : true } ) ;
307- await fs . writeFile (
308- params . storePath ,
309- JSON . stringify ( { [ params . sessionKey ] : params . entry } , null , 2 ) ,
310- "utf-8" ,
311- ) ;
312- }
313-
314315 function createBaseRun ( params : {
315316 storePath : string ;
316317 sessionEntry : Record < string , unknown > ;
@@ -421,7 +422,7 @@ describe("runReplyAgent auto-compaction token update", () => {
421422 unsubscribe ?.( ) ;
422423 }
423424
424- const stored = JSON . parse ( await fs . readFile ( storePath , "utf-8" ) ) ;
425+ const stored = readSessionStoreForTest ( storePath ) ;
425426 const usageEvent = diagnostics . find ( ( event ) => event . type === "model.usage" ) ;
426427 return { sessionKey, stored, usageEvent } ;
427428 }
@@ -827,17 +828,7 @@ describe("runReplyAgent Active Memory inline debug", () => {
827828 verboseLevel : "on" ,
828829 } ;
829830
830- await fs . writeFile (
831- storePath ,
832- JSON . stringify (
833- {
834- [ sessionKey ] : sessionEntry ,
835- } ,
836- null ,
837- 2 ,
838- ) ,
839- "utf-8" ,
840- ) ;
831+ await seedSessionStore ( { storePath, sessionKey, entry : sessionEntry } ) ;
841832
842833 runEmbeddedAgentMock . mockImplementationOnce ( async ( ) => {
843834 const latest = loadSessionStore ( storePath , { skipCache : true } ) ;
@@ -939,17 +930,7 @@ describe("runReplyAgent Active Memory inline debug", () => {
939930 traceLevel : "on" ,
940931 } ;
941932
942- await fs . writeFile (
943- storePath ,
944- JSON . stringify (
945- {
946- [ sessionKey ] : sessionEntry ,
947- } ,
948- null ,
949- 2 ,
950- ) ,
951- "utf-8" ,
952- ) ;
933+ await seedSessionStore ( { storePath, sessionKey, entry : sessionEntry } ) ;
953934
954935 runEmbeddedAgentMock . mockImplementationOnce ( async ( ) => {
955936 const latest = loadSessionStore ( storePath , { skipCache : true } ) ;
@@ -1050,17 +1031,7 @@ describe("runReplyAgent Active Memory inline debug", () => {
10501031 traceLevel : "on" ,
10511032 } ;
10521033
1053- await fs . writeFile (
1054- storePath ,
1055- JSON . stringify (
1056- {
1057- [ sessionKey ] : sessionEntry ,
1058- } ,
1059- null ,
1060- 2 ,
1061- ) ,
1062- "utf-8" ,
1063- ) ;
1034+ await seedSessionStore ( { storePath, sessionKey, entry : sessionEntry } ) ;
10641035
10651036 runEmbeddedAgentMock . mockImplementationOnce ( async ( ) => {
10661037 const latest = loadSessionStore ( storePath , { skipCache : true } ) ;
@@ -1163,17 +1134,7 @@ describe("runReplyAgent Active Memory inline debug", () => {
11631134 compactionCount : 3 ,
11641135 } ;
11651136
1166- await fs . writeFile (
1167- storePath ,
1168- JSON . stringify (
1169- {
1170- [ sessionKey ] : sessionEntry ,
1171- } ,
1172- null ,
1173- 2 ,
1174- ) ,
1175- "utf-8" ,
1176- ) ;
1137+ await seedSessionStore ( { storePath, sessionKey, entry : sessionEntry } ) ;
11771138 await fs . writeFile (
11781139 sessionFile ,
11791140 [
@@ -1409,7 +1370,7 @@ describe("runReplyAgent Active Memory inline debug", () => {
14091370 traceLevel : "raw" ,
14101371 } ;
14111372
1412- await fs . writeFile ( storePath , JSON . stringify ( { [ sessionKey ] : sessionEntry } , null , 2 ) , "utf-8" ) ;
1373+ await seedSessionStore ( { storePath, sessionKey, entry : sessionEntry } ) ;
14131374 await fs . writeFile ( sessionFile , "" , "utf-8" ) ;
14141375
14151376 runEmbeddedAgentMock . mockResolvedValueOnce ( {
@@ -1625,7 +1586,7 @@ describe("runReplyAgent Active Memory inline debug", () => {
16251586 traceLevel : "raw" ,
16261587 } ;
16271588
1628- await fs . writeFile ( storePath , JSON . stringify ( { [ sessionKey ] : sessionEntry } , null , 2 ) , "utf-8" ) ;
1589+ await seedSessionStore ( { storePath, sessionKey, entry : sessionEntry } ) ;
16291590 await fs . writeFile ( sessionFile , "" , "utf-8" ) ;
16301591
16311592 runEmbeddedAgentMock . mockResolvedValueOnce ( {
@@ -3010,7 +2971,7 @@ describe("runReplyAgent private message_tool_only final warning (#85714)", () =>
30102971 const storePath = path . join ( tmp , "sessions.json" ) ;
30112972 const sessionKey = "stranded" ;
30122973 const sessionEntry = { sessionId : "session" , updatedAt : Date . now ( ) , totalTokens : 1_000 } ;
3013- await fs . writeFile ( storePath , JSON . stringify ( { [ sessionKey ] : sessionEntry } , null , 2 ) , "utf-8" ) ;
2974+ await seedSessionStore ( { storePath, sessionKey, entry : sessionEntry } ) ;
30142975
30152976 const finalAssistantText =
30162977 params . finalAssistantText ??
0 commit comments