Skip to content

Commit 60cb875

Browse files
committed
fix(memory): rebuild ambiguous partial legacy sources
1 parent 495e8a7 commit 60cb875

2 files changed

Lines changed: 49 additions & 12 deletions

File tree

‎packages/memory-host-sdk/src/host/memory-schema.legacy-migration.test.ts‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

‎packages/memory-host-sdk/src/host/memory-schema.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,28 @@ function copyLegacyMemoryIndexRows(
368368
preservedEmbeddingCacheTable?: string,
369369
): void {
370370
// A (path, source) the canonical index already has chunks for keeps its whole
371-
// canonical chunk set. A chunkless canonical source imports legacy chunks only
372-
// when both source rows describe the same content. Diverged chunkless sources
373-
// are invalidated after the copy so sync cannot skip their required rebuild.
371+
// canonical chunk set. If legacy has additional chunk identities, invalidate
372+
// the source metadata so sync rebuilds instead of accepting an incomplete set.
373+
// Chunkless canonical sources import legacy chunks only when their source rows
374+
// match; diverged chunkless sources are invalidated for the same reason.
374375
// Snapshot exclusions before inserts so the predicate sees canonical state.
375376
db.exec(`
376377
CREATE TEMP TABLE legacy_import_chunk_excluded_sources AS
377-
SELECT DISTINCT path, source, 0 AS force_reindex
378-
FROM main.${MEMORY_INDEX_CHUNKS_TABLE}
378+
SELECT DISTINCT owned.path, owned.source,
379+
CASE WHEN EXISTS (
380+
SELECT 1 FROM ${schema}.chunks AS legacy_chunk
381+
WHERE legacy_chunk.path = owned.path AND legacy_chunk.source IS owned.source
382+
AND NOT EXISTS (
383+
SELECT 1 FROM main.${MEMORY_INDEX_CHUNKS_TABLE} AS canonical_chunk
384+
WHERE canonical_chunk.id = legacy_chunk.id
385+
AND canonical_chunk.path IS legacy_chunk.path AND canonical_chunk.source IS legacy_chunk.source
386+
)
387+
) THEN 1 ELSE 0 END AS force_reindex
388+
FROM main.${MEMORY_INDEX_CHUNKS_TABLE} AS owned
389+
WHERE EXISTS (
390+
SELECT 1 FROM ${schema}.files AS legacy_file
391+
WHERE legacy_file.path = owned.path AND legacy_file.source IS owned.source
392+
)
379393
UNION ALL
380394
SELECT canonical.path, canonical.source, 1 AS force_reindex
381395
FROM main.${MEMORY_INDEX_SOURCES_TABLE} AS canonical

0 commit comments

Comments
 (0)