Skip to content

Commit 33cc8ad

Browse files
committed
refactor(memory): use sqlite transcript identities
1 parent 8317e23 commit 33cc8ad

17 files changed

Lines changed: 387 additions & 1010 deletions

‎extensions/memory-core/src/memory/manager-session-sync-ops.ts‎

Lines changed: 31 additions & 261 deletions
Large diffs are not rendered by default.

‎extensions/memory-core/src/memory/manager-source-sync-ops.ts‎

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
// Memory Core plugin module owns memory and session source indexing.
2-
import path from "node:path";
32
import { createSubsystemLogger } from "openclaw/plugin-sdk/memory-core-host-engine-foundation";
43
import {
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({

‎extensions/memory-core/src/memory/manager-sync-base.ts‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,12 @@ export type MemorySourceSyncPlan = {
8181
finalize: () => Promise<void> | void;
8282
};
8383

84-
type MemorySessionDeltaState = {
85-
lastSize: number;
86-
pendingBytes: number;
87-
pendingMessages: number;
88-
};
89-
9084
type MemoryReindexRetryState = {
9185
dirty: boolean;
9286
memoryFullRetryDirty: boolean;
9387
sessionsDirty: boolean;
9488
sessionsFullRetryDirty: boolean;
9589
sessionsDirtyFiles: Set<string>;
96-
sessionDeltas: Map<string, MemorySessionDeltaState>;
9790
};
9891

9992
export const MEMORY_INDEX_META_KEY = "memory_index_meta_v1";
@@ -164,7 +157,6 @@ export abstract class MemoryManagerSyncBase {
164157
protected sessionsDirtyFiles = new Set<string>();
165158
protected sessionPendingFiles = new Set<string>();
166159
protected sessionPendingTargets = new Map<string, MemorySessionSyncTarget>();
167-
protected sessionDeltas = new Map<string, MemorySessionDeltaState>();
168160
protected vectorDegradedWriteWarningShown = false;
169161
protected lastMetaSerialized: string | null = null;
170162

@@ -215,9 +207,6 @@ export abstract class MemoryManagerSyncBase {
215207
sessionsDirty: this.sessionsDirty,
216208
sessionsFullRetryDirty: this.sessionsFullRetryDirty,
217209
sessionsDirtyFiles: new Set(this.sessionsDirtyFiles),
218-
sessionDeltas: new Map(
219-
Array.from(this.sessionDeltas, ([file, state]) => [file, { ...state }]),
220-
),
221210
};
222211
}
223212

@@ -226,13 +215,6 @@ export abstract class MemoryManagerSyncBase {
226215
this.memoryFullRetryDirty = snapshot.memoryFullRetryDirty || this.memoryFullRetryDirty;
227216
this.sessionsFullRetryDirty = snapshot.sessionsFullRetryDirty || this.sessionsFullRetryDirty;
228217
this.sessionsDirtyFiles = new Set([...snapshot.sessionsDirtyFiles, ...this.sessionsDirtyFiles]);
229-
const currentDeltas = this.sessionDeltas;
230-
this.sessionDeltas = new Map(
231-
Array.from(currentDeltas, ([file, state]) => [file, { ...state }]),
232-
);
233-
for (const [file, state] of snapshot.sessionDeltas) {
234-
this.sessionDeltas.set(file, { ...state });
235-
}
236218
this.sessionsDirty =
237219
snapshot.sessionsDirty ||
238220
this.sessionsDirty ||

‎extensions/memory-core/src/memory/manager-sync-ops.archive-delta-bypass.test.ts‎

Lines changed: 0 additions & 182 deletions
This file was deleted.

0 commit comments

Comments
 (0)