@@ -24,6 +24,7 @@ import {
2424 writeMemoryCoreWorkspaceEntry ,
2525} from "./dreaming-state.js" ;
2626import { previewRemHarness } from "./rem-harness.js" ;
27+ import { writeSessionIngestionState } from "./session-ingestion.js" ;
2728import {
2829 applyShortTermPromotions ,
2930 rankShortTermPromotionCandidates ,
@@ -2309,6 +2310,98 @@ describe("memory-core dreaming phases", () => {
23092310 expect ( corpus ) . toContain ( "bulk-line-159" ) ;
23102311 } ) ;
23112312
2313+ it ( "preserves checkpoints for known sessions beyond a capped sweep" , async ( ) => {
2314+ const workspaceDir = await createDreamingWorkspace ( ) ;
2315+ setDreamingTestEnv ( path . join ( workspaceDir , ".state" ) ) ;
2316+ const busySessionIds = Array . from (
2317+ { length : 20 } ,
2318+ ( _ , index ) => `aa-busy-${ index . toString ( ) . padStart ( 2 , "0" ) } ` ,
2319+ ) ;
2320+ for ( const sessionId of [ ...busySessionIds , "zz-known" ] ) {
2321+ await seedDreamingSessionTranscript ( {
2322+ sessionId,
2323+ messages : [
2324+ {
2325+ role : "user" ,
2326+ timestamp : "2026-04-05T18:00:00.000Z" ,
2327+ content : `Initial durable note for ${ sessionId } ` ,
2328+ } ,
2329+ ] ,
2330+ } ) ;
2331+ }
2332+ const { beforeAgentReply } = createHarness ( LIGHT_DREAMING_TEST_CONFIG , workspaceDir ) ;
2333+
2334+ try {
2335+ await withDreamingTestClock ( async ( ) => {
2336+ await triggerLightDreaming ( beforeAgentReply , workspaceDir , 5 ) ;
2337+ } ) ;
2338+ const before = await dreamingTestState . readSessionIngestionState ( workspaceDir ) ;
2339+ const knownStateKey = "main:sessions/main/zz-known" ;
2340+ const knownCheckpoint = expectDefined (
2341+ before . files [ knownStateKey ] ,
2342+ "known session checkpoint" ,
2343+ ) ;
2344+
2345+ for ( const sessionId of busySessionIds ) {
2346+ await seedDreamingSessionTranscript ( {
2347+ sessionId,
2348+ messages : Array . from ( { length : 12 } , ( _ , index ) => ( {
2349+ role : "user" as const ,
2350+ timestamp : "2026-04-05T18:05:00.000Z" ,
2351+ content : `New durable note ${ index } for ${ sessionId } ` ,
2352+ } ) ) ,
2353+ } ) ;
2354+ }
2355+ await withDreamingTestClock ( async ( ) => {
2356+ await triggerLightDreaming ( beforeAgentReply , workspaceDir , 6 ) ;
2357+ } ) ;
2358+
2359+ const after = await dreamingTestState . readSessionIngestionState ( workspaceDir ) ;
2360+ expect ( after . files [ knownStateKey ] ) . toStrictEqual ( knownCheckpoint ) ;
2361+ } finally {
2362+ restoreDreamingTestEnv ( ) ;
2363+ }
2364+ } ) ;
2365+
2366+ it ( "prunes absent live checkpoints without deleting foreign backfill state" , async ( ) => {
2367+ const workspaceDir = await createDreamingWorkspace ( ) ;
2368+ setDreamingTestEnv ( path . join ( workspaceDir , ".state" ) ) ;
2369+ const archiveName = "retained.jsonl.deleted.2026-04-06T01-00-00.000Z" ;
2370+ const sessionsDir = resolveSessionTranscriptsDirForAgent ( "main" ) ;
2371+ await fs . mkdir ( sessionsDir , { recursive : true } ) ;
2372+ await fs . writeFile ( path . join ( sessionsDir , archiveName ) , "" ) ;
2373+ const checkpoint = {
2374+ mtimeMs : 1 ,
2375+ size : 1 ,
2376+ contentHash : "hash" ,
2377+ lineCount : 1 ,
2378+ lastContentLine : 1 ,
2379+ } ;
2380+ await writeSessionIngestionState ( workspaceDir , {
2381+ version : 3 ,
2382+ files : {
2383+ "main:sessions/main/missing" : checkpoint ,
2384+ [ `main:sessions/main/${ archiveName } ` ] : checkpoint ,
2385+ "session-backfill:/archive.jsonl" : checkpoint ,
2386+ } ,
2387+ seenMessages : { } ,
2388+ } ) ;
2389+ const { beforeAgentReply } = createHarness ( LIGHT_DREAMING_TEST_CONFIG , workspaceDir ) ;
2390+
2391+ try {
2392+ await withDreamingTestClock ( async ( ) => {
2393+ await triggerLightDreaming ( beforeAgentReply , workspaceDir , 5 ) ;
2394+ } ) ;
2395+ const state = await dreamingTestState . readSessionIngestionState ( workspaceDir ) ;
2396+ expect ( state . files ) . toEqual ( {
2397+ [ `main:sessions/main/${ archiveName } ` ] : checkpoint ,
2398+ "session-backfill:/archive.jsonl" : checkpoint ,
2399+ } ) ;
2400+ } finally {
2401+ restoreDreamingTestEnv ( ) ;
2402+ }
2403+ } ) ;
2404+
23122405 it ( "ingests appended SQLite session transcript rows after prior checkpoint" , async ( ) => {
23132406 const workspaceDir = await createDreamingWorkspace ( ) ;
23142407 setDreamingTestEnv ( path . join ( workspaceDir , ".state" ) ) ;
0 commit comments