@@ -144,10 +144,9 @@ describe("memory index same-file legacy migration", () => {
144144 { key : "legacy-only-key" , value : "imported" } ,
145145 { key : "memory_index_meta_v1" , value : "canonical" } ,
146146 ] ,
147- sources : [
148- { path : "doc.md" , hash : "new-hash" , size : 42 } ,
149- { path : "old-note.md" , hash : "old-note-hash" , size : 10 } ,
150- ] ,
147+ // The extra legacy chunk identity makes doc.md's canonical chunk set
148+ // ambiguous, so removing its source row forces a normal sync rebuild.
149+ sources : [ { path : "old-note.md" , hash : "old-note-hash" , size : 10 } ] ,
151150 chunks : [
152151 { id : "chunk-new-1" , text : "current canonical body" } ,
153152 { id : "chunk-old-2" , text : "note body" } ,
@@ -193,7 +192,7 @@ describe("memory index same-file legacy migration", () => {
193192 }
194193 } ) ;
195194
196- it ( "imports legacy chunks only for matching canonical sources that have no chunks yet " , ( ) => {
195+ it ( "keeps canonical chunk sets coherent and invalidates ambiguous partial sources " , ( ) => {
197196 const db = new DatabaseSync ( ":memory:" ) ;
198197 try {
199198 ensureMemoryIndexSchema ( { db, cacheEnabled : false , ftsEnabled : false } ) ;
@@ -205,6 +204,14 @@ describe("memory index same-file legacy migration", () => {
205204 'chunk-doc-canonical', 'doc.md', 'memory', 1, 10, 'doc-chunk-hash', 'model',
206205 'canonical body', '[]', 200
207206 );
207+ -- partial.md has only the first canonical chunk. Seeing a second legacy
208+ -- identity makes completeness ambiguous, so the source must reindex.
209+ INSERT INTO memory_index_sources (path, source, hash, mtime, size)
210+ VALUES ('partial.md', 'memory', 'partial-hash', 175.0, 30);
211+ INSERT INTO memory_index_chunks VALUES (
212+ 'chunk-partial-1', 'partial.md', 'memory', 1, 5, 'partial-1-hash', 'model',
213+ 'canonical first half', '[]', 175
214+ );
208215 -- pending.md has a canonical source row but no chunks yet (indexing
209216 -- interrupted before its chunks were written): its legacy chunk is the
210217 -- only searchable content and must import instead of being stranded.
@@ -237,12 +244,21 @@ describe("memory index same-file legacy migration", () => {
237244 updated_at INTEGER NOT NULL
238245 );
239246 INSERT INTO files VALUES ('doc.md', 'memory', 'doc-hash', 200, 42);
247+ INSERT INTO files VALUES ('partial.md', 'memory', 'partial-hash', 175, 30);
240248 INSERT INTO files VALUES ('pending.md', 'memory', 'pending-hash', 150, 20);
241249 INSERT INTO files VALUES ('diverged.md', 'memory', 'stale-hash', 50, 12);
242250 INSERT INTO chunks VALUES (
243251 'chunk-doc-legacy', 'doc.md', 'memory', 1, 8, 'stale-hash', 'model',
244252 'stale legacy body', '[]', 100
245253 );
254+ INSERT INTO chunks VALUES (
255+ 'chunk-partial-1', 'partial.md', 'memory', 1, 5, 'legacy-partial-1-hash', 'model',
256+ 'legacy first half', '[]', 150
257+ );
258+ INSERT INTO chunks VALUES (
259+ 'chunk-partial-2', 'partial.md', 'memory', 6, 10, 'partial-2-hash', 'model',
260+ 'legacy second half', '[]', 150
261+ );
246262 INSERT INTO chunks VALUES (
247263 'chunk-pending-legacy', 'pending.md', 'memory', 1, 6, 'pending-chunk-hash', 'model',
248264 'only searchable content for pending', '[]', 150
@@ -264,10 +280,17 @@ describe("memory index same-file legacy migration", () => {
264280 // chunk so the file is not left silently unsearchable.
265281 expect ( db . prepare ( "SELECT id, text FROM memory_index_chunks ORDER BY id" ) . all ( ) ) . toEqual ( [
266282 { id : "chunk-doc-canonical" , text : "canonical body" } ,
283+ { id : "chunk-partial-1" , text : "canonical first half" } ,
267284 { id : "chunk-pending-legacy" , text : "only searchable content for pending" } ,
268285 ] ) ;
269- // Removing the diverged chunkless source prevents hash-based sync from
270- // skipping it; the next sync must rebuild current content from disk.
286+ // Removing ambiguous or diverged sources prevents hash-based sync from
287+ // skipping them; the next sync must rebuild current content from disk.
288+ expect (
289+ db . prepare ( "SELECT hash FROM memory_index_sources WHERE path = 'doc.md'" ) . get ( ) ,
290+ ) . toBeUndefined ( ) ;
291+ expect (
292+ db . prepare ( "SELECT hash FROM memory_index_sources WHERE path = 'partial.md'" ) . get ( ) ,
293+ ) . toBeUndefined ( ) ;
271294 expect (
272295 db . prepare ( "SELECT hash FROM memory_index_sources WHERE path = 'diverged.md'" ) . get ( ) ,
273296 ) . toBeUndefined ( ) ;
0 commit comments