@@ -9,10 +9,11 @@ import {
99 type ResolvedMemorySearchConfig ,
1010} from "openclaw/plugin-sdk/memory-core-host-engine-foundation" ;
1111import { statSessionEntrySync } from "openclaw/plugin-sdk/memory-core-host-engine-qmd" ;
12- import type {
13- MemorySource ,
14- MemorySyncParams ,
15- MemorySyncProgressUpdate ,
12+ import {
13+ MEMORY_CHUNKING_VERSION ,
14+ type MemorySource ,
15+ type MemorySyncParams ,
16+ type MemorySyncProgressUpdate ,
1617} from "openclaw/plugin-sdk/memory-core-host-engine-storage" ;
1718import {
1819 clearConfigCache ,
@@ -25,6 +26,11 @@ import {
2526 formatSqliteSessionFileMarker ,
2627} from "openclaw/plugin-sdk/sqlite-runtime-testing" ;
2728import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
29+ import {
30+ MEMORY_INDEX_PROVENANCE_VERSION ,
31+ resolveConfiguredScopeHash ,
32+ type MemoryIndexMeta ,
33+ } from "./manager-reindex-state.js" ;
2834import { MemoryManagerSyncOps } from "./manager-sync-ops.js" ;
2935
3036type MemoryIndexEntry = {
@@ -142,6 +148,8 @@ class SessionStartupCatchupHarness extends MemoryManagerSyncOps {
142148 readonly syncCalls : SyncParams [ ] = [ ] ;
143149 readonly indexedPaths : string [ ] = [ ] ;
144150 readonly indexedContents : string [ ] = [ ] ;
151+ corpusListCalls = 0 ;
152+ private afterNextCorpusList : ( ( ) => Promise < void > ) | null = null ;
145153 private pendingSyncWork : Promise < void > = Promise . resolve ( ) ;
146154
147155 constructor (
@@ -202,18 +210,8 @@ class SessionStartupCatchupHarness extends MemoryManagerSyncOps {
202210 await this . pendingSyncWork ;
203211 }
204212
205- async combineTargetArchiveFilesForTest ( params : {
206- sessions ?: MemorySyncParams [ "sessions" ] ;
207- archiveFiles ?: string [ ] ;
208- } ) : Promise < Set < string > | null > {
209- return await (
210- this as unknown as {
211- combineTargetArchiveFiles : ( params : {
212- sessions ?: MemorySyncParams [ "sessions" ] ;
213- archiveFiles ?: string [ ] ;
214- } ) => Promise < Set < string > | null > ;
215- }
216- ) . combineTargetArchiveFiles ( params ) ;
213+ afterNextCorpusListForTest ( callback : ( ) => Promise < void > ) : void {
214+ this . afterNextCorpusList = callback ;
217215 }
218216
219217 isSessionsDirty ( ) : boolean {
@@ -253,6 +251,24 @@ class SessionStartupCatchupHarness extends MemoryManagerSyncOps {
253251 return [ ] ;
254252 }
255253
254+ protected override readMeta ( ) : MemoryIndexMeta {
255+ return {
256+ model : "fts-only" ,
257+ provider : "none" ,
258+ sources : [ "sessions" ] ,
259+ scopeHash : resolveConfiguredScopeHash ( {
260+ workspaceDir : this . workspaceDir ,
261+ extraPaths : this . settings . extraPaths ,
262+ multimodal : this . settings . multimodal ,
263+ } ) ,
264+ chunkTokens : this . settings . chunking . tokens ,
265+ chunkOverlap : this . settings . chunking . overlap ,
266+ chunkingVersion : MEMORY_CHUNKING_VERSION ,
267+ ftsTokenizer : this . settings . store . fts . tokenizer ,
268+ provenanceVersion : MEMORY_INDEX_PROVENANCE_VERSION ,
269+ } ;
270+ }
271+
256272 protected async sync ( params ?: MemorySyncParams ) : Promise < void > {
257273 this . syncCalls . push ( params ?? { } ) ;
258274 this . pendingSyncWork = this . indexSessionUpdates
@@ -273,6 +289,15 @@ class SessionStartupCatchupHarness extends MemoryManagerSyncOps {
273289 return 1 ;
274290 }
275291
292+ protected override async listSessionCorpusEntries ( ) {
293+ const entries = await super . listSessionCorpusEntries ( ) ;
294+ this . corpusListCalls += 1 ;
295+ const callback = this . afterNextCorpusList ;
296+ this . afterNextCorpusList = null ;
297+ await callback ?.( ) ;
298+ return entries ;
299+ }
300+
276301 protected pruneEmbeddingCacheIfNeeded ( ) : void { }
277302
278303 protected resetProviderInitializationForRetry ( ) : void { }
@@ -599,6 +624,14 @@ describe("session startup catch-up", () => {
599624 expect ( harness . indexedPaths ) . toEqual ( [ ] ) ;
600625 } ) ;
601626
627+ it ( "skips corpus preflight when an ordinary sync has no session work" , async ( ) => {
628+ const harness = new SessionStartupCatchupHarness ( [ ] ) ;
629+
630+ await harness . runSyncForTest ( { reason : "session-delta" } ) ;
631+
632+ expect ( harness . corpusListCalls ) . toBe ( 0 ) ;
633+ } ) ;
634+
602635 it ( "resolves identity-targeted delta sync through a custom session store" , async ( ) => {
603636 const storePath = path . join ( stateDir , "custom-sessions" , "sessions.json" ) ;
604637 const session = await writeSqliteSession ( {
@@ -626,6 +659,42 @@ describe("session startup catch-up", () => {
626659 expect ( harness . syncCalls ) . toEqual ( [ { reason : "session-delta" } ] ) ;
627660 } ) ;
628661
662+ it ( "keeps targeted indexing on the SQLite store resolved by its corpus snapshot" , async ( ) => {
663+ const session = await writeSqliteSession ( {
664+ storePath : path . join ( stateDir , "custom-sessions" , "sessions.json" ) ,
665+ sessionId : "snapshot-thread" ,
666+ sessionKey : "agent:main:chat:snapshot" ,
667+ content : "original snapshot target" ,
668+ } ) ;
669+ const replacement = await writeSqliteSession ( {
670+ storePath : path . join ( stateDir , "replacement-sessions" , "sessions.json" ) ,
671+ sessionId : session . sessionId ,
672+ sessionKey : session . sessionKey ,
673+ content : "replacement store target" ,
674+ } ) ;
675+ await configureTestSessionStore ( session . storePath ) ;
676+ const harness = new SessionStartupCatchupHarness ( [ ] ) ;
677+ harness . afterNextCorpusListForTest ( async ( ) => {
678+ await configureTestSessionStore ( replacement . storePath ) ;
679+ } ) ;
680+
681+ await harness . runSyncForTest ( {
682+ reason : "queued-sessions" ,
683+ sessions : [
684+ {
685+ agentId : "main" ,
686+ sessionId : session . sessionId ,
687+ sessionKey : session . sessionKey ,
688+ } ,
689+ ] ,
690+ } ) ;
691+
692+ expect ( harness . corpusListCalls ) . toBe ( 1 ) ;
693+ expect ( harness . indexedPaths ) . toEqual ( [ session . corpusPath ] ) ;
694+ expect ( harness . indexedContents [ 0 ] ) . toContain ( "original snapshot target" ) ;
695+ expect ( harness . indexedContents [ 0 ] ) . not . toContain ( "replacement store target" ) ;
696+ } ) ;
697+
629698 it ( "preserves generated-session classification during targeted custom-store indexing" , async ( ) => {
630699 const storePath = path . join ( stateDir , "custom-sessions" , "sessions.json" ) ;
631700 const session = await writeSqliteSession ( {
0 commit comments