11// Memory Core plugin module owns memory and session source indexing.
2- import path from "node:path" ;
32import { createSubsystemLogger } from "openclaw/plugin-sdk/memory-core-host-engine-foundation" ;
43import {
54 buildSessionEntry ,
6- parseSqliteSessionFileMarker ,
7- parseUsageCountedSessionIdFromFileName ,
8- sessionPathForFile ,
95 sessionPathForSessionIdentity ,
106 type SessionTranscriptCorpusEntry ,
117} from "openclaw/plugin-sdk/memory-core-host-engine-qmd" ;
@@ -186,10 +182,17 @@ export abstract class MemoryManagerSourceSyncOps extends MemoryManagerSessionSyn
186182 const corpusEntries = params . corpusEntries ?? ( await this . listSessionCorpusEntries ( ) ) ;
187183 const targetArchiveFiles = params . needsFullReindex
188184 ? null
189- : this . normalizeTargetArchiveFiles ( params . targetArchiveFiles , corpusEntries ) ;
185+ : this . normalizeTargetArchiveFiles ( params . targetArchiveFiles , corpusEntries , true ) ;
190186 const corpusEntryByPath = new Map < string , SessionTranscriptCorpusEntry > (
191187 corpusEntries . map ( ( entry ) => [ entry . sessionFile , entry ] ) ,
192188 ) ;
189+ const corpusEntryForPath = ( file : string ) : SessionTranscriptCorpusEntry => {
190+ const entry = corpusEntryByPath . get ( file ) ;
191+ if ( ! entry ) {
192+ throw new Error ( `Missing session corpus entry for ${ file } ` ) ;
193+ }
194+ return entry ;
195+ } ;
193196 const files = targetArchiveFiles
194197 ? Array . from ( targetArchiveFiles )
195198 : corpusEntries . map ( ( entry ) => entry . sessionFile ) ;
@@ -204,16 +207,7 @@ export abstract class MemoryManagerSourceSyncOps extends MemoryManagerSessionSyn
204207 db : this . db ,
205208 source : "sessions" ,
206209 } ) . rows ,
207- sessionPathForFile : ( file ) => {
208- const corpusEntry = corpusEntryByPath . get ( file ) ;
209- if ( corpusEntry ) {
210- return this . sessionPathForCorpusEntry ( corpusEntry ) ;
211- }
212- const sqliteMarker = parseSqliteSessionFileMarker ( file ) ;
213- return sqliteMarker
214- ? sessionPathForSessionIdentity ( sqliteMarker . agentId , sqliteMarker . sessionId )
215- : sessionPathForFile ( file ) ;
216- } ,
210+ sessionPathForFile : ( file ) => this . sessionPathForCorpusEntry ( corpusEntryForPath ( file ) ) ,
217211 } ) ;
218212 const { activePaths, existingRows, existingHashes, indexAll } = sessionPlan ;
219213 log . debug ( "memory sync: indexing session files" , {
@@ -278,19 +272,11 @@ export abstract class MemoryManagerSourceSyncOps extends MemoryManagerSessionSyn
278272 } ) . rows . map ( ( row ) => row . path ) ,
279273 ) ;
280274 for ( const file of targetArchiveFiles ) {
281- const corpusEntry = corpusEntryByPath . get ( file ) ;
282- const sqliteMarker = parseSqliteSessionFileMarker ( file ) ;
283- const sessionId =
284- corpusEntry ?. sessionId ??
285- sqliteMarker ?. sessionId ??
286- parseUsageCountedSessionIdFromFileName ( path . basename ( file ) ) ;
287- if ( ! sessionId ) {
288- continue ;
289- }
290- const staleAgentId = corpusEntry ?. agentId ?? sqliteMarker ?. agentId ?? this . agentId ;
275+ const corpusEntry = corpusEntryForPath ( file ) ;
276+ const staleAgentId = corpusEntry . agentId ;
291277 const staleLivePaths = [
292- sessionPathForSessionIdentity ( staleAgentId , sessionId ) ,
293- this . legacyExtensionlessSessionPathForIdentity ( staleAgentId , sessionId ) ,
278+ sessionPathForSessionIdentity ( staleAgentId , corpusEntry . sessionId ) ,
279+ this . legacyExtensionlessSessionPathForIdentity ( staleAgentId , corpusEntry . sessionId ) ,
294280 ] ;
295281 for ( const staleLivePath of staleLivePaths ) {
296282 if ( activeCorpusPaths . has ( staleLivePath ) || ! existingSessionPaths . has ( staleLivePath ) ) {
@@ -334,10 +320,9 @@ export abstract class MemoryManagerSourceSyncOps extends MemoryManagerSessionSyn
334320 }
335321 return null ;
336322 }
337- const corpusEntry = corpusEntryByPath . get ( absPath ) ;
338323 const entry = await buildSessionEntry (
339324 absPath ,
340- corpusEntry ? this . buildSessionEntryOptions ( corpusEntry ) : undefined ,
325+ this . buildSessionEntryOptions ( corpusEntryForPath ( absPath ) ) ,
341326 ) ;
342327 if ( ! entry ) {
343328 if ( params . progress ) {
@@ -363,7 +348,6 @@ export abstract class MemoryManagerSourceSyncOps extends MemoryManagerSessionSyn
363348 total : params . progress . total ,
364349 } ) ;
365350 }
366- this . resetSessionDelta ( absPath , entry . size ) ;
367351 return null ;
368352 }
369353 return entry ;
@@ -379,7 +363,6 @@ export abstract class MemoryManagerSourceSyncOps extends MemoryManagerSessionSyn
379363 ( entry ) : MemoryIndexWorkItem => ( {
380364 entry,
381365 source : "sessions" ,
382- afterIndex : ( ) => this . resetSessionDelta ( entry . absPath , entry . size ) ,
383366 } ) ,
384367 ) ,
385368 ) ;
@@ -409,10 +392,9 @@ export abstract class MemoryManagerSourceSyncOps extends MemoryManagerSessionSyn
409392 }
410393 return ;
411394 }
412- const corpusEntry = corpusEntryByPath . get ( absPath ) ;
413395 const entry = await buildSessionEntry (
414396 absPath ,
415- corpusEntry ? this . buildSessionEntryOptions ( corpusEntry ) : undefined ,
397+ this . buildSessionEntryOptions ( corpusEntryForPath ( absPath ) ) ,
416398 ) ;
417399 if ( ! entry ) {
418400 if ( params . progress ) {
@@ -438,11 +420,9 @@ export abstract class MemoryManagerSourceSyncOps extends MemoryManagerSessionSyn
438420 total : params . progress . total ,
439421 } ) ;
440422 }
441- this . resetSessionDelta ( absPath , entry . size ) ;
442423 return ;
443424 }
444425 await this . indexFile ( entry , { source : "sessions" , content : entry . content } ) ;
445- this . resetSessionDelta ( absPath , entry . size ) ;
446426 if ( params . progress ) {
447427 params . progress . completed += 1 ;
448428 params . progress . report ( {
0 commit comments