Skip to content

Commit 8616acb

Browse files
committed
fix(memory): recover restored session freshness
1 parent c263c27 commit 8616acb

4 files changed

Lines changed: 289 additions & 15 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ describe("memory session sync state", () => {
106106
mtimeMs: 250,
107107
size: 20,
108108
},
109+
{
110+
absPath: "/tmp/sessions/rolled-back.jsonl",
111+
path: "sessions/rolled-back.jsonl",
112+
mtimeMs: 150,
113+
size: 20,
114+
},
109115
{
110116
absPath: "/tmp/sessions/resized.jsonl",
111117
path: "sessions/resized.jsonl",
@@ -124,6 +130,7 @@ describe("memory session sync state", () => {
124130
{ path: "sessions/sub-ms-newer.jsonl", hash: "hash-sub-ms", mtime: 100.25, size: 10 },
125131
{ path: "sessions/invalidated.jsonl", hash: "", mtime: 200, size: 20 },
126132
{ path: "sessions/newer.jsonl", hash: "hash-newer", mtime: 200, size: 20 },
133+
{ path: "sessions/rolled-back.jsonl", hash: "hash-rolled-back", mtime: 200, size: 20 },
127134
{ path: "sessions/resized.jsonl", hash: "hash-resized", mtime: 300, size: 30 },
128135
],
129136
});
@@ -132,6 +139,7 @@ describe("memory session sync state", () => {
132139
"/tmp/sessions/sub-ms-newer.jsonl",
133140
"/tmp/sessions/invalidated.jsonl",
134141
"/tmp/sessions/newer.jsonl",
142+
"/tmp/sessions/rolled-back.jsonl",
135143
"/tmp/sessions/resized.jsonl",
136144
"/tmp/sessions/missing.jsonl",
137145
]);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export function resolveMemorySessionStartupDirtyFiles(params: {
2626
dirtyFiles.push(file.absPath);
2727
continue;
2828
}
29-
if (file.size !== indexedSize || file.mtimeMs > indexedMtimeMs) {
29+
// File mtimes and SQLite session updatedAt values can move backward after
30+
// restore/reset. The downstream content-hash gate suppresses unchanged rewrites.
31+
if (file.size !== indexedSize || file.mtimeMs !== indexedMtimeMs) {
3032
dirtyFiles.push(file.absPath);
3133
}
3234
}

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,29 @@ export abstract class MemoryManagerSourceSyncOps extends MemoryManagerSessionSyn
174174
const deleteChunksByPathAndSource = this.db.prepare(
175175
`DELETE FROM memory_index_chunks WHERE path = ? AND source = ?`,
176176
);
177+
const updateUnchangedSessionSourceMetadata = this.db.prepare(
178+
`UPDATE memory_index_sources
179+
SET mtime = ?, size = ?
180+
WHERE path = ? AND source = 'sessions' AND hash = ?`,
181+
);
182+
const refreshUnchangedSessionSourceMetadata = (entry: MemoryIndexEntry): boolean => {
183+
// Hash equality preserves chunks and embeddings; only converge the source
184+
// fingerprint so restored sessions do not repeat catch-up on every startup.
185+
return (
186+
updateUnchangedSessionSourceMetadata.run(entry.mtimeMs, entry.size, entry.path, entry.hash)
187+
.changes === 1
188+
);
189+
};
190+
const canSkipUnchangedSessionEntry = (
191+
entry: MemoryIndexEntry,
192+
absPath: string,
193+
existingHash: string | undefined,
194+
): boolean => {
195+
if (params.needsFullReindex || existingHash !== entry.hash) {
196+
return false;
197+
}
198+
return !this.sessionsDirtyFiles.has(absPath) || refreshUnchangedSessionSourceMetadata(entry);
199+
};
177200
const deleteFtsRowsByPathAndSource =
178201
this.fts.enabled && this.fts.available
179202
? this.db.prepare(`DELETE FROM ${FTS_TABLE} WHERE path = ? AND source = ?`)
@@ -340,7 +363,7 @@ export abstract class MemoryManagerSourceSyncOps extends MemoryManagerSessionSyn
340363
path: entry.path,
341364
existingHashes,
342365
});
343-
if (!params.needsFullReindex && existingHash === entry.hash) {
366+
if (canSkipUnchangedSessionEntry(entry, absPath, existingHash)) {
344367
if (params.progress) {
345368
params.progress.completed += 1;
346369
params.progress.report({
@@ -412,7 +435,7 @@ export abstract class MemoryManagerSourceSyncOps extends MemoryManagerSessionSyn
412435
path: entry.path,
413436
existingHashes,
414437
});
415-
if (!params.needsFullReindex && existingHash === entry.hash) {
438+
if (canSkipUnchangedSessionEntry(entry, absPath, existingHash)) {
416439
if (params.progress) {
417440
params.progress.completed += 1;
418441
params.progress.report({

0 commit comments

Comments
 (0)