Implement vector recovery and refresh docs

This commit is contained in:
Youzini-afk
2026-03-24 12:38:46 +08:00
parent 7b68eebb9e
commit 50ee8cc8ed
13 changed files with 2668 additions and 805 deletions

66
tests/runtime-history.mjs Normal file
View File

@@ -0,0 +1,66 @@
import assert from "node:assert/strict";
import {
appendBatchJournal,
cloneGraphSnapshot,
createBatchJournalEntry,
detectHistoryMutation,
findJournalRecoveryPoint,
snapshotProcessedMessageHashes,
} from "../runtime-state.js";
import { createEmptyGraph } from "../graph.js";
const chat = [
{ is_user: true, mes: "你好" },
{ is_user: false, mes: "我记住了。" },
{ is_user: true, mes: "继续" },
{ is_user: false, mes: "新的回复" },
];
const hashes = snapshotProcessedMessageHashes(chat, 3);
const cleanDetection = detectHistoryMutation(chat, {
lastProcessedAssistantFloor: 3,
processedMessageHashes: hashes,
});
assert.equal(cleanDetection.dirty, false);
const editedChat = structuredClone(chat);
editedChat[1].mes = "我改过内容了。";
const editedDetection = detectHistoryMutation(editedChat, {
lastProcessedAssistantFloor: 3,
processedMessageHashes: hashes,
});
assert.equal(editedDetection.dirty, true);
assert.equal(editedDetection.earliestAffectedFloor, 1);
const truncatedChat = chat.slice(0, 2);
const truncatedDetection = detectHistoryMutation(truncatedChat, {
lastProcessedAssistantFloor: 3,
processedMessageHashes: hashes,
});
assert.equal(truncatedDetection.dirty, true);
assert.equal(truncatedDetection.earliestAffectedFloor, 2);
const graph = createEmptyGraph();
graph.historyState.chatId = "chat-history-test";
const beforeSnapshot = cloneGraphSnapshot(graph);
graph.lastProcessedSeq = 3;
graph.historyState.lastProcessedAssistantFloor = 3;
const afterSnapshot = cloneGraphSnapshot(graph);
appendBatchJournal(
graph,
createBatchJournalEntry(beforeSnapshot, afterSnapshot, {
processedRange: [1, 3],
postProcessArtifacts: ["compression"],
vectorHashesInserted: [1234],
}),
);
const recoveryPoint = findJournalRecoveryPoint(graph, 2);
assert.ok(recoveryPoint);
assert.equal(recoveryPoint.journal.processedRange[1], 3);
assert.equal(
recoveryPoint.snapshotBefore.historyState.lastProcessedAssistantFloor,
-1,
);
console.log("runtime-history tests passed");