1- import { describe , expect , it } from "vitest" ;
1+ import { describe , expect , it , vi } from "vitest" ;
2+ import { applyFileBackedSessionStoreMaintenance } from "./store-maintenance-operations.js" ;
23import { capEntryCount , getActiveSessionMaintenanceWarning } from "./store-maintenance.js" ;
34import type { SessionEntry } from "./types.js" ;
45
@@ -12,6 +13,14 @@ function makeStore(entries: Array<[string, SessionEntry]>): Record<string, Sessi
1213 return Object . fromEntries ( entries ) ;
1314}
1415
16+ function createMaintenanceArtifacts ( ) {
17+ return {
18+ archiveRemovedSessionTranscripts : async ( ) => new Set < string > ( ) ,
19+ removeRemovedSessionTrajectoryArtifacts : async ( ) => { } ,
20+ cleanupArchivedSessionTranscripts : async ( ) => { } ,
21+ } ;
22+ }
23+
1524describe ( "session maintenance total entry cap" , ( ) => {
1625 it ( "counts archived sessions toward the cap without evicting them" , ( ) => {
1726 const now = Date . now ( ) ;
@@ -77,4 +86,50 @@ describe("session maintenance total entry cap", () => {
7786 } ) ,
7887 ) . toMatchObject ( { wouldCap : true , wouldPrune : false } ) ;
7988 } ) ;
89+
90+ it ( "uses enforcement preservation when predicting active-session eviction" , async ( ) => {
91+ const now = Date . now ( ) ;
92+ const storePath = "/tmp/openclaw-sessions/warn-enforce-parity.json" ;
93+ const makePressureStore = ( ) =>
94+ makeStore ( [
95+ [ "archived" , { ...makeEntry ( now - 2 ) , archivedAt : now } ] ,
96+ [ "active" , makeEntry ( now - 1 ) ] ,
97+ [ "recent" , makeEntry ( now ) ] ,
98+ ] ) ;
99+ const maintenanceConfig = {
100+ mode : "warn" as const ,
101+ pruneAfterMs : 30 * DAY_MS ,
102+ maxEntries : 2 ,
103+ modelRunPruneAfterMs : DAY_MS ,
104+ resetArchiveRetentionMs : null ,
105+ maxDiskBytes : null ,
106+ highWaterBytes : null ,
107+ } ;
108+ const onWarn = vi . fn ( ) ;
109+
110+ await applyFileBackedSessionStoreMaintenance ( {
111+ storePath,
112+ store : makePressureStore ( ) ,
113+ activeSessionKey : "active" ,
114+ maintenanceConfig,
115+ onWarn,
116+ log : { warn : ( ) => { } , info : ( ) => { } } ,
117+ artifacts : createMaintenanceArtifacts ( ) ,
118+ } ) ;
119+
120+ const enforcedStore = makePressureStore ( ) ;
121+ await applyFileBackedSessionStoreMaintenance ( {
122+ storePath,
123+ store : enforcedStore ,
124+ activeSessionKey : "active" ,
125+ maintenanceConfig : { ...maintenanceConfig , mode : "enforce" } ,
126+ log : { warn : ( ) => { } , info : ( ) => { } } ,
127+ artifacts : createMaintenanceArtifacts ( ) ,
128+ } ) ;
129+
130+ expect ( onWarn ) . not . toHaveBeenCalled ( ) ;
131+ expect ( enforcedStore ) . toHaveProperty ( "archived" ) ;
132+ expect ( enforcedStore ) . toHaveProperty ( "active" ) ;
133+ expect ( enforcedStore . recent ) . toBeUndefined ( ) ;
134+ } ) ;
80135} ) ;
0 commit comments