11import { createHash } from "node:crypto" ;
22import { readSessionIngestionState , writeSessionIngestionState } from "./dreaming-phases.js" ;
33import {
4- clearMemoryCoreWorkspaceNamespace ,
4+ deleteMemoryCoreWorkspaceEntry ,
55 readMemoryCoreWorkspaceEntries ,
66 SESSION_BACKFILL_REWIND_NAMESPACE ,
77 writeMemoryCoreWorkspaceEntry ,
@@ -13,6 +13,8 @@ import type {
1313
1414const DEFAULT_SESSION_BACKFILL_LIMIT_DAYS = 92 ;
1515const MEMORY_DAY_RE = / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / ;
16+ // Batch keys are SHA-256 hex digests, so this colon-delimited marker cannot collide.
17+ const SESSION_BACKFILL_BASELINE_KEY_PREFIX = "complete-baseline:" ;
1618
1719type SessionBackfillRewindCandidate = {
1820 contentIndex : number ;
@@ -26,6 +28,12 @@ type SessionBackfillRewindBatch = {
2628 candidates : SessionBackfillRewindCandidate [ ] ;
2729} ;
2830
31+ type SessionBackfillBaseline = {
32+ version : 1 ;
33+ complete : true ;
34+ agentId : string ;
35+ } ;
36+
2937function normalizeMemoryDay ( value : string | undefined , flag : string ) : string | undefined {
3038 if ( value === undefined ) {
3139 return undefined ;
@@ -81,6 +89,18 @@ export async function recordSessionBackfillRewindBatch(params: {
8189 } ) ;
8290}
8391
92+ export async function markSessionBackfillRewindBaseline ( params : {
93+ workspaceDir : string ;
94+ agentId : string ;
95+ } ) : Promise < void > {
96+ await writeMemoryCoreWorkspaceEntry < SessionBackfillBaseline > ( {
97+ namespace : SESSION_BACKFILL_REWIND_NAMESPACE ,
98+ workspaceDir : params . workspaceDir ,
99+ key : `${ SESSION_BACKFILL_BASELINE_KEY_PREFIX } ${ params . agentId } ` ,
100+ value : { version : 1 , complete : true , agentId : params . agentId } ,
101+ } ) ;
102+ }
103+
84104function isSessionBackfillRewindCandidate ( value : unknown ) : value is SessionBackfillRewindCandidate {
85105 if ( value === null || typeof value !== "object" || Array . isArray ( value ) ) {
86106 return false ;
@@ -98,25 +118,43 @@ function isSessionBackfillRewindCandidate(value: unknown): value is SessionBackf
98118 ) ;
99119}
100120
101- export async function rewindSessionBackfillIngestionState ( workspaceDir : string ) : Promise < number > {
102- const entries = await readMemoryCoreWorkspaceEntries < SessionBackfillRewindBatch > ( {
121+ function isSessionBackfillRewindBatch (
122+ value : SessionBackfillRewindBatch | SessionBackfillBaseline ,
123+ ) : value is SessionBackfillRewindBatch {
124+ return value . version === 1 && "candidates" in value && Array . isArray ( value . candidates ) ;
125+ }
126+
127+ export async function rewindSessionBackfillIngestionState ( params : {
128+ workspaceDir : string ;
129+ agentId : string ;
130+ } ) : Promise < {
131+ completeCoverage : boolean ;
132+ rewoundCandidates : number ;
133+ } > {
134+ const entries = await readMemoryCoreWorkspaceEntries <
135+ SessionBackfillRewindBatch | SessionBackfillBaseline
136+ > ( {
103137 namespace : SESSION_BACKFILL_REWIND_NAMESPACE ,
104- workspaceDir,
138+ workspaceDir : params . workspaceDir ,
105139 } ) ;
106- const candidates = entries . flatMap ( ( entry ) =>
107- entry . value ?. version === 1 && Array . isArray ( entry . value . candidates )
140+ const completeCoverage = entries . some (
141+ ( entry ) =>
142+ entry . key === `${ SESSION_BACKFILL_BASELINE_KEY_PREFIX } ${ params . agentId } ` &&
143+ "complete" in entry . value &&
144+ entry . value . agentId === params . agentId ,
145+ ) ;
146+ const batchEntries = entries . filter ( ( entry ) => isSessionBackfillRewindBatch ( entry . value ) ) ;
147+ const candidates = batchEntries . flatMap ( ( entry ) =>
148+ isSessionBackfillRewindBatch ( entry . value )
108149 ? entry . value . candidates . filter ( isSessionBackfillRewindCandidate )
109150 : [ ] ,
110151 ) ;
111152 if ( candidates . length === 0 ) {
112- await clearMemoryCoreWorkspaceNamespace ( {
113- namespace : SESSION_BACKFILL_REWIND_NAMESPACE ,
114- workspaceDir,
115- } ) ;
116- return 0 ;
153+ await deleteSessionBackfillRewindBatches ( params . workspaceDir , batchEntries ) ;
154+ return { completeCoverage, rewoundCandidates : 0 } ;
117155 }
118156
119- const state = await readSessionIngestionState ( workspaceDir ) ;
157+ const state = await readSessionIngestionState ( params . workspaceDir ) ;
120158 const removedHashesByScope = new Map < string , Set < string > > ( ) ;
121159 const rewindLineByStateKey = new Map < string , number > ( ) ;
122160 for ( const candidate of candidates ) {
@@ -153,12 +191,24 @@ export async function rewindSessionBackfillIngestionState(workspaceDir: string):
153191 } ;
154192 }
155193 }
156- await writeSessionIngestionState ( workspaceDir , { ...state , files, seenMessages } ) ;
157- await clearMemoryCoreWorkspaceNamespace ( {
158- namespace : SESSION_BACKFILL_REWIND_NAMESPACE ,
159- workspaceDir,
160- } ) ;
161- return candidates . length ;
194+ await writeSessionIngestionState ( params . workspaceDir , { ...state , files, seenMessages } ) ;
195+ await deleteSessionBackfillRewindBatches ( params . workspaceDir , batchEntries ) ;
196+ return { completeCoverage, rewoundCandidates : candidates . length } ;
197+ }
198+
199+ async function deleteSessionBackfillRewindBatches (
200+ workspaceDir : string ,
201+ entries : Array < { key : string } > ,
202+ ) : Promise < void > {
203+ await Promise . all (
204+ entries . map ( ( entry ) =>
205+ deleteMemoryCoreWorkspaceEntry ( {
206+ namespace : SESSION_BACKFILL_REWIND_NAMESPACE ,
207+ workspaceDir,
208+ key : entry . key ,
209+ } ) ,
210+ ) ,
211+ ) ;
162212}
163213
164214function belongsToAgentFileState ( key : string , agentId : string ) : boolean {
0 commit comments