Skip to content

Commit 9cfd1cd

Browse files
jalehmanvincentkoc
authored andcommitted
fix(memory): key index sources by path and source
1 parent 21163a7 commit 9cfd1cd

8 files changed

Lines changed: 62 additions & 14 deletions

File tree

‎extensions/memory-core/src/memory/manager-db.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ describe("memory manager database publication", () => {
140140

141141
concurrentDb = new DatabaseSync(targetPath);
142142
concurrentDb
143-
.prepare("UPDATE memory_index_sources SET hash = ? WHERE path = ?")
144-
.run("newer", "memory.md");
143+
.prepare("UPDATE memory_index_sources SET hash = ? WHERE path = ? AND source = ?")
144+
.run("newer", "memory.md", "memory");
145145
concurrentDb.close();
146146
concurrentDb = undefined;
147147

@@ -154,7 +154,9 @@ describe("memory manager database publication", () => {
154154
}),
155155
).rejects.toThrow(/changed while full reindex was building/);
156156
expect(
157-
targetDb.prepare("SELECT hash FROM memory_index_sources WHERE path = ?").get("memory.md"),
157+
targetDb
158+
.prepare("SELECT hash FROM memory_index_sources WHERE path = ? AND source = ?")
159+
.get("memory.md", "memory"),
158160
).toEqual({ hash: "newer" });
159161
} finally {
160162
try {

‎extensions/memory-core/src/memory/manager-embedding-ops.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,7 @@ export abstract class MemoryManagerEmbeddingOps extends MemoryManagerSyncOps {
719719
this.db
720720
.prepare(
721721
`INSERT INTO memory_index_sources (path, source, hash, mtime, size) VALUES (?, ?, ?, ?, ?)
722-
ON CONFLICT(path) DO UPDATE SET
723-
source=excluded.source,
722+
ON CONFLICT(path, source) DO UPDATE SET
724723
hash=excluded.hash,
725724
mtime=excluded.mtime,
726725
size=excluded.size`,

‎extensions/memory-core/src/memory/manager.self-heal-missing-identity.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ describe("memory manager self-heal missing identity with FTS-only chunks", () =>
118118
updated_at INTEGER NOT NULL
119119
);
120120
CREATE TABLE IF NOT EXISTS memory_index_sources (
121-
path TEXT PRIMARY KEY,
121+
path TEXT NOT NULL,
122122
source TEXT NOT NULL DEFAULT 'memory',
123123
hash TEXT NOT NULL,
124124
mtime INTEGER NOT NULL,
125-
size INTEGER NOT NULL
125+
size INTEGER NOT NULL,
126+
PRIMARY KEY (path, source)
126127
);
127128
INSERT INTO memory_index_chunks (id, path, source, start_line, end_line, hash, model, text, embedding, updated_at)
128129
VALUES ('chunk-1', 'MEMORY.md', 'memory', 1, 3, 'hash-1', '${model}', 'Alpha topic keep note', '[]', ${Date.now()});

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,33 @@ describe("memory index schema", () => {
8484
}
8585
});
8686

87+
it("stores source records with the same path in separate sources", () => {
88+
const db = new DatabaseSync(":memory:");
89+
try {
90+
ensureMemoryIndexSchema({
91+
db,
92+
cacheEnabled: false,
93+
ftsEnabled: false,
94+
});
95+
96+
db.prepare(
97+
"INSERT INTO memory_index_sources (path, source, hash, mtime, size) VALUES (?, ?, ?, ?, ?)",
98+
).run("shared.md", "memory", "memory-hash", 10, 20);
99+
db.prepare(
100+
"INSERT INTO memory_index_sources (path, source, hash, mtime, size) VALUES (?, ?, ?, ?, ?)",
101+
).run("shared.md", "sessions", "session-hash", 30, 40);
102+
103+
expect(
104+
db.prepare("SELECT path, source, hash FROM memory_index_sources ORDER BY source").all(),
105+
).toEqual([
106+
{ path: "shared.md", source: "memory", hash: "memory-hash" },
107+
{ path: "shared.md", source: "sessions", hash: "session-hash" },
108+
]);
109+
} finally {
110+
db.close();
111+
}
112+
});
113+
87114
it("leaves unrelated generic tables untouched", () => {
88115
const db = new DatabaseSync(":memory:");
89116
try {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,12 @@ export function ensureMemoryIndexSchema(params: {
194194
value TEXT NOT NULL
195195
);
196196
CREATE TABLE IF NOT EXISTS ${MEMORY_INDEX_SOURCES_TABLE} (
197-
path TEXT PRIMARY KEY,
197+
path TEXT NOT NULL,
198198
source TEXT NOT NULL DEFAULT 'memory',
199199
hash TEXT NOT NULL,
200200
mtime INTEGER NOT NULL,
201-
size INTEGER NOT NULL
201+
size INTEGER NOT NULL,
202+
PRIMARY KEY (path, source)
202203
);
203204
CREATE TABLE IF NOT EXISTS ${MEMORY_INDEX_CHUNKS_TABLE} (
204205
id TEXT PRIMARY KEY,
@@ -250,6 +251,10 @@ export function ensureMemoryIndexSchema(params: {
250251
UPDATE ${MEMORY_INDEX_STATE_TABLE} SET revision = revision + 1 WHERE id = 1;
251252
END;
252253
254+
CREATE INDEX IF NOT EXISTS idx_memory_index_sources_source
255+
ON ${MEMORY_INDEX_SOURCES_TABLE}(source);
256+
CREATE INDEX IF NOT EXISTS idx_memory_index_chunks_path_source
257+
ON ${MEMORY_INDEX_CHUNKS_TABLE}(path, source);
253258
CREATE INDEX IF NOT EXISTS idx_memory_index_chunks_path
254259
ON ${MEMORY_INDEX_CHUNKS_TABLE}(path);
255260
CREATE INDEX IF NOT EXISTS idx_memory_index_chunks_source

‎src/state/openclaw-agent-db.generated.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export interface MemoryIndexMeta {
6262
export interface MemoryIndexSources {
6363
hash: string;
6464
mtime: number;
65-
path: string | null;
65+
path: string;
6666
size: number;
6767
source: Generated<string>;
6868
}

‎src/state/openclaw-agent-schema.generated.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ CREATE TABLE IF NOT EXISTS memory_index_meta (
4848
);
4949
5050
CREATE TABLE IF NOT EXISTS memory_index_sources (
51-
path TEXT PRIMARY KEY,
51+
path TEXT NOT NULL,
5252
source TEXT NOT NULL DEFAULT 'memory',
5353
hash TEXT NOT NULL,
5454
mtime INTEGER NOT NULL,
55-
size INTEGER NOT NULL
55+
size INTEGER NOT NULL,
56+
PRIMARY KEY (path, source)
5657
);
5758
5859
CREATE TABLE IF NOT EXISTS memory_index_chunks (
@@ -125,6 +126,12 @@ END;
125126
CREATE INDEX IF NOT EXISTS idx_memory_embedding_cache_updated_at
126127
ON memory_embedding_cache(updated_at);
127128
129+
CREATE INDEX IF NOT EXISTS idx_memory_index_sources_source
130+
ON memory_index_sources(source);
131+
132+
CREATE INDEX IF NOT EXISTS idx_memory_index_chunks_path_source
133+
ON memory_index_chunks(path, source);
134+
128135
CREATE INDEX IF NOT EXISTS idx_memory_index_chunks_path
129136
ON memory_index_chunks(path);
130137

‎src/state/openclaw-agent-schema.sql‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ CREATE TABLE IF NOT EXISTS memory_index_meta (
4343
);
4444

4545
CREATE TABLE IF NOT EXISTS memory_index_sources (
46-
path TEXT PRIMARY KEY,
46+
path TEXT NOT NULL,
4747
source TEXT NOT NULL DEFAULT 'memory',
4848
hash TEXT NOT NULL,
4949
mtime INTEGER NOT NULL,
50-
size INTEGER NOT NULL
50+
size INTEGER NOT NULL,
51+
PRIMARY KEY (path, source)
5152
);
5253

5354
CREATE TABLE IF NOT EXISTS memory_index_chunks (
@@ -120,6 +121,12 @@ END;
120121
CREATE INDEX IF NOT EXISTS idx_memory_embedding_cache_updated_at
121122
ON memory_embedding_cache(updated_at);
122123

124+
CREATE INDEX IF NOT EXISTS idx_memory_index_sources_source
125+
ON memory_index_sources(source);
126+
127+
CREATE INDEX IF NOT EXISTS idx_memory_index_chunks_path_source
128+
ON memory_index_chunks(path, source);
129+
123130
CREATE INDEX IF NOT EXISTS idx_memory_index_chunks_path
124131
ON memory_index_chunks(path);
125132

0 commit comments

Comments
 (0)