Skip to content

Commit a9ee661

Browse files
fix(memory): recall prior conversation after session reset (#122051)
* fix(memory): recall archived session generations after reset * test(memory): prove private recall across reset * fix(memory): keep deleted transcripts outside reset recall * fix(memory): reject deleted archives from reset recall * fix(memory): isolate recall across sqlite resets * test(memory): split reset recall coverage * fix(memory): keep reset recall metadata private * fix(memory): keep reset authority scoped --------- Co-authored-by: TheAngryPit <[email protected]>
1 parent 11fe5ab commit a9ee661

15 files changed

Lines changed: 1267 additions & 57 deletions

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ import {
1717
hashText,
1818
INVALID_PROJECT_ANNOTATION_KEY,
1919
MEMORY_EMBEDDING_CACHE_TABLE,
20+
MEMORY_INDEX_CHUNK_PROVENANCE_TABLE,
21+
MEMORY_INDEX_CHUNK_RECALL_METADATA_TABLE,
2022
MEMORY_INDEX_FTS_TABLE,
2123
MEMORY_INDEX_VECTOR_TABLE,
2224
remapChunkLines,
2325
retryTransientMemoryRead,
2426
runWithConcurrency,
2527
stripMemoryAnnotationCarriers,
2628
type MemoryChunk,
27-
type MemorySource,
2829
type MemoryEntryProvenance,
29-
MEMORY_INDEX_CHUNK_PROVENANCE_TABLE,
30-
MEMORY_INDEX_CHUNK_RECALL_METADATA_TABLE,
30+
type MemorySource,
3131
} from "openclaw/plugin-sdk/memory-core-host-engine-storage";
3232
import { MAX_TIMER_TIMEOUT_MS, resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
3333
import { sleepWithAbort } from "openclaw/plugin-sdk/runtime-env";
3434
import { runSqliteImmediateTransactionSync } from "openclaw/plugin-sdk/sqlite-runtime";
35+
import { readSessionResetRecallCutoffMetadata } from "../session-reset-recall-metadata.js";
3536
import type { EmbeddingProvider } from "./embeddings.js";
3637
import {
3738
MEMORY_BATCH_FAILURE_LIMIT,
@@ -59,6 +60,7 @@ import {
5960
resolveMemoryIndexProviderIdentities,
6061
type MemoryIndexProviderIdentity,
6162
} from "./manager-reindex-state.js";
63+
import { chunkSessionContentAtResetBoundary } from "./manager-reset-chunk-boundary.js";
6264
import {
6365
MemoryManagerSyncOps,
6466
type MemoryIndexWorkItem,
@@ -1121,11 +1123,19 @@ export abstract class MemoryManagerEmbeddingOps extends MemoryManagerSyncOps {
11211123
(normalizedEntryPath === "MEMORY.md" || normalizedEntryPath === "USER.md");
11221124
const indexingContent =
11231125
options.source === "memory" ? stripMemoryAnnotationCarriers(content) : content;
1126+
const chunkOptions = { ...this.settings.chunking, perEntry };
11241127
const baseChunks = filterNonEmptyMemoryChunks(
1125-
chunkMarkdown(indexingContent, {
1126-
...this.settings.chunking,
1127-
perEntry,
1128-
}),
1128+
options.source === "sessions"
1129+
? chunkSessionContentAtResetBoundary({
1130+
content: indexingContent,
1131+
cutoffLine: (() => {
1132+
const cutoff = readSessionResetRecallCutoffMetadata(entry);
1133+
return cutoff.state === "valid" ? cutoff.cutoffLine : undefined;
1134+
})(),
1135+
lineMap: entry.lineMap,
1136+
chunking: chunkOptions,
1137+
})
1138+
: chunkMarkdown(indexingContent, chunkOptions),
11291139
);
11301140
for (const chunk of baseChunks) {
11311141
chunk.provenance = this.resolveChunkProvenance(
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, expect, it } from "vitest";
2+
import { chunkSessionContentAtResetBoundary } from "./manager-reset-chunk-boundary.js";
3+
4+
describe("chunkSessionContentAtResetBoundary", () => {
5+
it("never overlaps a pre-reset chunk into the current generation", () => {
6+
const chunks = chunkSessionContentAtResetBoundary({
7+
content: "old one\nold two\nkept live\ncurrent turn",
8+
cutoffLine: 7,
9+
lineMap: [2, 4, 7, 9],
10+
chunking: { tokens: 100, overlap: 50 },
11+
});
12+
13+
expect(chunks.map((chunk) => [chunk.startLine, chunk.endLine, chunk.text])).toEqual([
14+
[1, 2, "old one\nold two"],
15+
[3, 4, "kept live\ncurrent turn"],
16+
]);
17+
});
18+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
chunkMarkdown,
3+
type MemoryChunk,
4+
} from "openclaw/plugin-sdk/memory-core-host-engine-storage";
5+
6+
export function chunkSessionContentAtResetBoundary(params: {
7+
content: string;
8+
cutoffLine?: number;
9+
lineMap?: readonly number[];
10+
chunking: { tokens: number; overlap: number; perEntry?: boolean };
11+
}): MemoryChunk[] {
12+
const cutoffIndex =
13+
params.cutoffLine !== undefined && params.lineMap
14+
? params.lineMap.findIndex((line) => line >= params.cutoffLine!)
15+
: -1;
16+
if (cutoffIndex <= 0) {
17+
return chunkMarkdown(params.content, params.chunking);
18+
}
19+
const lines = params.content.split("\n");
20+
const chunkPartition = (content: string, lineOffset: number) => {
21+
const chunks = chunkMarkdown(content, params.chunking);
22+
for (const chunk of chunks) {
23+
chunk.startLine += lineOffset;
24+
chunk.endLine += lineOffset;
25+
}
26+
return chunks;
27+
};
28+
return [
29+
...chunkPartition(lines.slice(0, cutoffIndex).join("\n"), 0),
30+
...chunkPartition(lines.slice(cutoffIndex).join("\n"), cutoffIndex),
31+
];
32+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from "vitest";
2+
import { readSessionArchiveReasonFromHitPath } from "./session-reset-recall-metadata.js";
3+
4+
describe("readSessionArchiveReasonFromHitPath", () => {
5+
it.each([
6+
["sessions/a.jsonl.reset.2026-08-11T08-00-00Z", "reset"],
7+
["sessions/a.jsonl.reset.2026-08-11T08-00-00.000Z.zst", "reset"],
8+
["sessions/a.jsonl.deleted.2026-08-11T08-00-00Z", "deleted"],
9+
["sessions\\a.jsonl.deleted.2026-08-11T08-00-00.000Z.zst", "deleted"],
10+
["sessions/a.jsonl.reset.2026-08-11T08:00:00Z", undefined],
11+
["sessions/a.jsonl.reset.2026-08-11T08-00-00Z.gz", undefined],
12+
["sessions/a.jsonl.deleted.2026-08-11T08-00-00Z.extra", undefined],
13+
["sessions/a.jsonl.RESET.2026-08-11T08-00-00Z", undefined],
14+
])("classifies %s", (path, expected) => {
15+
expect(readSessionArchiveReasonFromHitPath(path)).toBe(expected);
16+
});
17+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export type SessionResetRecallCutoff =
2+
| { state: "absent" }
3+
| { state: "invalid" }
4+
| { cutoffLine: number; state: "valid" };
5+
6+
const RESET_RECALL_CUTOFF = Symbol.for("openclaw.memory.sessionResetRecallCutoff");
7+
8+
export function readSessionResetRecallCutoffMetadata(value: unknown): SessionResetRecallCutoff {
9+
if (!value || typeof value !== "object") {
10+
return { state: "invalid" };
11+
}
12+
const cutoff = (value as Record<PropertyKey, unknown>)[RESET_RECALL_CUTOFF];
13+
if (!cutoff || typeof cutoff !== "object") {
14+
return { state: "invalid" };
15+
}
16+
const state = (cutoff as { state?: unknown }).state;
17+
if (state === "absent" || state === "invalid") {
18+
return { state };
19+
}
20+
const cutoffLine = (cutoff as { cutoffLine?: unknown }).cutoffLine;
21+
return state === "valid" && typeof cutoffLine === "number" && Number.isInteger(cutoffLine)
22+
? { state, cutoffLine }
23+
: { state: "invalid" };
24+
}
25+
26+
export function readSessionArchiveReasonFromHitPath(
27+
hitPath: string,
28+
): "reset" | "deleted" | undefined {
29+
const match =
30+
/\.jsonl\.(reset|deleted)\.\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}(?:\.\d{3})?Z(?:\.zst)?$/.exec(
31+
hitPath,
32+
);
33+
const reason = match?.[1];
34+
return reason === "reset" || reason === "deleted" ? reason : undefined;
35+
}

0 commit comments

Comments
 (0)