2026-08-22 10:30:00 +03:00
|
|
|
/**
|
2026-09-08 22:19:37 +03:00
|
|
|
* Graph merger + trigger + derivation tests (vision §1, 2026-09-02 redesign).
|
|
|
|
|
* The merger (core/graph.ts mergeFindings) is the pipeline's deterministic
|
|
|
|
|
* apply stage: AUDITED VERDICTS in, ledger appends out — link-or-create
|
|
|
|
|
* identity (the auditor's linkTo, never string matching), deterministic quiz
|
|
|
|
|
* events, understanding observations, and the idempotency guarantees.
|
2026-08-22 10:30:00 +03:00
|
|
|
*
|
|
|
|
|
* Ledger isolation: PATHS is fixed at import time, so the five data/graph
|
|
|
|
|
* ledgers are backed up and cleared before the suite and restored after
|
|
|
|
|
* (try/finally-shaped: the after() hook always runs, even on failure).
|
|
|
|
|
* The chat fixtures are real ChatRecords in the chat store (created and
|
2026-08-31 22:36:41 +03:00
|
|
|
* deleted by this suite) — the merger reads exactly what production reads.
|
|
|
|
|
* The full LLM pipeline (extractor → auditor) is tested in
|
|
|
|
|
* tests/extraction.test.ts with mock models.
|
2026-08-22 10:30:00 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import assert from "node:assert/strict";
|
|
|
|
|
import * as fs from "node:fs";
|
|
|
|
|
import { after, describe, it } from "node:test";
|
|
|
|
|
import type { UIMessage } from "ai";
|
2026-09-08 22:19:37 +03:00
|
|
|
import type { AuditResult } from "../src/core/contracts.ts";
|
2026-08-22 10:30:00 +03:00
|
|
|
import type { QuestionAnswerData } from "../src/core/question-core.ts";
|
2026-09-15 17:41:56 +03:00
|
|
|
import { clusterContentId, getGraph, graphSnapshot, mergeFindings, type GraphDeps } from "../src/core/graph.ts";
|
2026-08-31 20:00:12 +03:00
|
|
|
import { graphStore } from "../src/store/graph.ts";
|
|
|
|
|
import { chatStore } from "../src/store/chats.ts";
|
2026-09-13 14:29:16 +03:00
|
|
|
import { LEDGER_FILES, PATHS, SOURCES_FILE, EMBEDDINGS_FILE } from "../config.ts";
|
2026-08-22 10:30:00 +03:00
|
|
|
|
2026-08-31 20:00:12 +03:00
|
|
|
/** The wired fs adapters, exactly what src/server/stores.ts passes in production. */
|
|
|
|
|
const graphDeps = { chatStore, graphStore } as const satisfies GraphDeps;
|
|
|
|
|
|
2026-09-13 02:08:55 +03:00
|
|
|
/** The session merge source for a chat — what the pipeline passes: the live
|
|
|
|
|
* transcript + its updatedAt checkpoint stamp. (Notes imports pass
|
|
|
|
|
* messages: null — see the notes-merger test below.) */
|
|
|
|
|
function sessionSource(chatId: string): { messages: UIMessage[]; foldedAt: string } {
|
|
|
|
|
const record = chatStore.getChat(chatId)!;
|
|
|
|
|
return { messages: record.messages as UIMessage[], foldedAt: record.updatedAt };
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-22 10:30:00 +03:00
|
|
|
// ─── ledger isolation (backup-and-clear of data/graph/*.jsonl) ──────────────
|
|
|
|
|
|
2026-09-13 14:29:16 +03:00
|
|
|
// EMBEDDINGS_FILE too: the semantic-cluster source (read by graphSnapshot)
|
|
|
|
|
// — isolated like the rest so this suite never touches the real vectors.
|
|
|
|
|
const LEDGER_PATHS = [...Object.values(LEDGER_FILES), SOURCES_FILE, EMBEDDINGS_FILE];
|
2026-08-22 10:30:00 +03:00
|
|
|
const backups: Array<{ file: string; bak: string }> = [];
|
|
|
|
|
const createdChats: string[] = [];
|
|
|
|
|
|
|
|
|
|
fs.mkdirSync(PATHS.graph, { recursive: true });
|
|
|
|
|
for (const file of LEDGER_PATHS) {
|
|
|
|
|
if (fs.existsSync(file)) {
|
2026-08-31 22:36:41 +03:00
|
|
|
const bak = `${file}.w5test.bak`;
|
2026-08-22 10:30:00 +03:00
|
|
|
fs.renameSync(file, bak);
|
|
|
|
|
backups.push({ file, bak });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
|
for (const file of LEDGER_PATHS) {
|
|
|
|
|
try {
|
|
|
|
|
fs.unlinkSync(file);
|
|
|
|
|
} catch {
|
|
|
|
|
/* nothing was written here */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (const { file, bak } of backups) fs.renameSync(bak, file);
|
|
|
|
|
for (const id of createdChats) {
|
|
|
|
|
try {
|
|
|
|
|
fs.unlinkSync(`${PATHS.chats}/${id}.json`);
|
|
|
|
|
} catch {
|
|
|
|
|
/* already gone */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ─── fixture builders ────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
function assistant(id: string, parts: unknown[]): UIMessage {
|
|
|
|
|
return { id, role: "assistant", parts } as unknown as UIMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function user(id: string, text: string): UIMessage {
|
|
|
|
|
return { id, role: "user", parts: [{ type: "text", text }] } as unknown as UIMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function quizPart(
|
|
|
|
|
toolCallId: string,
|
|
|
|
|
input: Record<string, unknown>,
|
|
|
|
|
output: QuestionAnswerData,
|
|
|
|
|
): unknown {
|
|
|
|
|
return { type: "tool-quiz", toolCallId, state: "output-available", input, output };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function answer(over: Partial<QuestionAnswerData>): QuestionAnswerData {
|
|
|
|
|
return {
|
|
|
|
|
questionId: "q",
|
|
|
|
|
kind: "quiz",
|
|
|
|
|
questionText: "question text",
|
|
|
|
|
dontKnow: false,
|
|
|
|
|
answers: [],
|
|
|
|
|
answeredAt: "2026-08-22T00:00:00.000Z",
|
|
|
|
|
...over,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
/** Create a chat in the real store with messages; returns the record.
|
|
|
|
|
* Tree-blind world: the plan tree is irrelevant to the merger — no tree. */
|
|
|
|
|
function fixtureChat(title: string, messages: UIMessage[]) {
|
2026-08-31 20:00:12 +03:00
|
|
|
const chat = chatStore.createChat(title);
|
2026-08-22 10:30:00 +03:00
|
|
|
createdChats.push(chat.id);
|
2026-08-31 20:00:12 +03:00
|
|
|
chatStore.saveMessages(chat.id, messages);
|
|
|
|
|
return chatStore.getChat(chat.id)!;
|
2026-08-22 10:30:00 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
/** The full fixture transcript: five graded quizzes + a dismissed one + an ask. */
|
|
|
|
|
function fixtureMessages(): UIMessage[] {
|
2026-08-22 10:30:00 +03:00
|
|
|
return [
|
|
|
|
|
user("u1", "teach me the fixture topic"),
|
|
|
|
|
assistant("a1", [
|
|
|
|
|
quizPart(
|
|
|
|
|
"call-1",
|
|
|
|
|
{ question: "Q1", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
|
|
|
|
answer({ questionId: "call-1", questionText: "Q1 text", correct: false, answeredAt: "2026-08-22T01:00:00.000Z" }),
|
|
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
assistant("a2", [
|
|
|
|
|
quizPart(
|
|
|
|
|
"call-2",
|
|
|
|
|
{ question: "Q2", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
|
|
|
|
answer({ questionId: "call-2", questionText: "Q2 text", dontKnow: true, correct: false, answeredAt: "2026-08-22T01:01:00.000Z" }),
|
|
|
|
|
),
|
|
|
|
|
]),
|
2026-09-08 22:19:37 +03:00
|
|
|
assistant("a3", [
|
|
|
|
|
// input.nodeId "alpha" is deliberately present and IGNORED —
|
|
|
|
|
// attribution is the only quiz→node path in the tree-blind world.
|
2026-08-22 10:30:00 +03:00
|
|
|
quizPart(
|
|
|
|
|
"call-3",
|
|
|
|
|
{ question: "Q3", nodeId: "alpha", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
|
|
|
|
answer({ questionId: "call-3", questionText: "Q3 text", correct: true, note: "recalled the diagram", answeredAt: "2026-08-22T02:00:00.000Z" }),
|
|
|
|
|
),
|
|
|
|
|
]),
|
2026-09-08 22:19:37 +03:00
|
|
|
assistant("a4", [
|
2026-08-22 10:30:00 +03:00
|
|
|
quizPart(
|
|
|
|
|
"call-4",
|
2026-09-08 22:19:37 +03:00
|
|
|
{ question: "Q4", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
2026-08-22 10:30:00 +03:00
|
|
|
answer({ questionId: "call-4", questionText: "Q4 text", correct: false, answeredAt: "2026-08-22T02:01:00.000Z" }),
|
|
|
|
|
),
|
|
|
|
|
]),
|
2026-09-08 22:19:37 +03:00
|
|
|
assistant("a5", [
|
2026-08-22 10:30:00 +03:00
|
|
|
quizPart(
|
|
|
|
|
"call-5",
|
2026-09-08 22:19:37 +03:00
|
|
|
{ question: "Q5", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
2026-08-22 10:30:00 +03:00
|
|
|
answer({ questionId: "call-5", questionText: "Q5 text", dontKnow: true, correct: false, answeredAt: "2026-08-22T02:02:00.000Z" }),
|
|
|
|
|
),
|
|
|
|
|
]),
|
2026-09-08 22:19:37 +03:00
|
|
|
assistant("a6", [
|
2026-08-31 22:36:41 +03:00
|
|
|
// dismissed quiz + an answered ask — neither becomes an event
|
2026-08-22 10:30:00 +03:00
|
|
|
quizPart(
|
|
|
|
|
"call-6",
|
|
|
|
|
{ question: "Q6", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
|
|
|
|
answer({ questionId: "call-6", questionText: "Q6 text", dismissed: true }),
|
|
|
|
|
),
|
|
|
|
|
{
|
|
|
|
|
type: "tool-ask_user_question",
|
|
|
|
|
toolCallId: "call-7",
|
|
|
|
|
state: "output-available",
|
|
|
|
|
input: { question: "free text?" },
|
|
|
|
|
output: answer({ questionId: "call-7", kind: "ask", questionText: "free text?", text: "because" }),
|
|
|
|
|
},
|
|
|
|
|
]),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
/** The auditor's verdict for the fixture chat: three verified propositions
|
|
|
|
|
* (no linkTo — the graph starts empty), one follows edge, all quizzes
|
|
|
|
|
* attributed, one understanding note. */
|
|
|
|
|
function fixtureAudit(): AuditResult {
|
|
|
|
|
return {
|
|
|
|
|
propositions: [
|
|
|
|
|
{
|
|
|
|
|
id: "p1",
|
2026-09-09 00:40:02 +03:00
|
|
|
name: "Alpha",
|
2026-09-08 22:19:37 +03:00
|
|
|
statement: "Alpha intuition",
|
|
|
|
|
aliases: ["alpha feel"],
|
|
|
|
|
messages: ["a1", "a3"],
|
|
|
|
|
evidence: ["fixture evidence"],
|
2026-09-09 02:06:32 +03:00
|
|
|
understanding: "solid",
|
2026-09-08 22:19:37 +03:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "p2",
|
2026-09-09 00:40:02 +03:00
|
|
|
name: "Beta rule",
|
2026-09-08 22:19:37 +03:00
|
|
|
statement: "Beta rule",
|
|
|
|
|
aliases: [],
|
|
|
|
|
messages: ["a4"],
|
|
|
|
|
evidence: ["fixture evidence"],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "p3",
|
2026-09-09 00:40:02 +03:00
|
|
|
name: "Gamma",
|
2026-09-08 22:19:37 +03:00
|
|
|
statement: "Gamma application",
|
|
|
|
|
aliases: [],
|
|
|
|
|
messages: ["a5"],
|
|
|
|
|
evidence: ["fixture evidence"],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
edges: [
|
|
|
|
|
{
|
|
|
|
|
from: "p1",
|
|
|
|
|
to: "p2",
|
|
|
|
|
type: "follows",
|
|
|
|
|
messages: ["a4"],
|
|
|
|
|
evidence: ["fixture evidence"],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
attribution: { quiz: { "call-1": "p1", "call-2": null, "call-3": "p1", "call-4": "p2", "call-5": "p3" } },
|
|
|
|
|
dropped: [],
|
|
|
|
|
};
|
2026-08-31 22:36:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The chat every merger test folds (shared, so ordering is intentional). */
|
2026-09-08 22:19:37 +03:00
|
|
|
const chat1 = fixtureChat("Graph fixture", fixtureMessages());
|
2026-08-22 10:30:00 +03:00
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
/** Set by the cross-session tests; the snapshot test (which runs after) reads them. */
|
2026-08-22 10:30:00 +03:00
|
|
|
let chat2Id = "";
|
2026-09-08 22:19:37 +03:00
|
|
|
let chat3Id = "";
|
2026-08-22 10:30:00 +03:00
|
|
|
|
2026-08-31 22:36:41 +03:00
|
|
|
// ─── the merger ──────────────────────────────────────────────────────────────
|
2026-08-22 10:30:00 +03:00
|
|
|
|
|
|
|
|
/** Everything the graph holds for a set of chats (other processes' test
|
|
|
|
|
* emissions run concurrently against the same files — scope, never assume). */
|
|
|
|
|
function scopedGraph(chatIds: string[]) {
|
2026-08-31 20:00:12 +03:00
|
|
|
const graph = getGraph(graphStore);
|
2026-08-22 10:30:00 +03:00
|
|
|
const nodeIds = new Set(
|
2026-08-31 20:00:12 +03:00
|
|
|
graphStore
|
2026-08-22 10:30:00 +03:00
|
|
|
.readMappings()
|
|
|
|
|
.filter((m) => chatIds.includes(m.chatId))
|
|
|
|
|
.map((m) => m.graphNodeId),
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
nodes: graph.nodes.filter((n) => nodeIds.has(n.id)),
|
2026-09-08 22:19:37 +03:00
|
|
|
edges: graph.edges.filter((e) => e.provenance.some((pt) => chatIds.includes(pt.chatId))),
|
2026-08-22 10:30:00 +03:00
|
|
|
events: graph.events.filter((e) => chatIds.includes(e.chatId)),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-31 22:36:41 +03:00
|
|
|
describe("the merger (mergeFindings)", () => {
|
2026-09-08 22:19:37 +03:00
|
|
|
it("creates nodes from the verdicts, rebuilds quiz events deterministically, records observations", () => {
|
2026-09-13 02:08:55 +03:00
|
|
|
const stats = mergeFindings(chat1.id, fixtureAudit(), graphDeps, sessionSource(chat1.id));
|
2026-08-22 10:30:00 +03:00
|
|
|
assert.deepEqual(stats, { nodes: 3, edges: 1, events: 6 });
|
|
|
|
|
|
|
|
|
|
const graph = scopedGraph([chat1.id]);
|
|
|
|
|
assert.equal(graph.nodes.length, 3);
|
|
|
|
|
const idByStatement = new Map(graph.nodes.map((n) => [n.statement, n.id]));
|
|
|
|
|
for (const node of graph.nodes) {
|
|
|
|
|
assert.match(node.id, /^n_/);
|
2026-09-09 00:40:02 +03:00
|
|
|
assert.ok(node.name.length > 0, "name is required");
|
2026-09-08 22:19:37 +03:00
|
|
|
assert.equal("kind" in node, false, "no kind field");
|
|
|
|
|
assert.ok(Array.isArray(node.provenance), "message-granular provenance");
|
2026-08-22 10:30:00 +03:00
|
|
|
}
|
2026-09-08 22:19:37 +03:00
|
|
|
// Provenance is message-granular: p1 came up in a1 and a3.
|
|
|
|
|
const alpha = graph.nodes.find((n) => n.statement === "Alpha intuition")!;
|
|
|
|
|
assert.deepEqual(alpha.provenance, [
|
|
|
|
|
{ chatId: chat1.id, messageId: "a1" },
|
|
|
|
|
{ chatId: chat1.id, messageId: "a3" },
|
|
|
|
|
]);
|
2026-08-22 10:30:00 +03:00
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
// The follows edge, provenance attached.
|
2026-08-22 10:30:00 +03:00
|
|
|
assert.equal(graph.edges.length, 1);
|
|
|
|
|
const edge = graph.edges[0]!;
|
|
|
|
|
assert.equal(edge.from, idByStatement.get("Alpha intuition"));
|
|
|
|
|
assert.equal(edge.to, idByStatement.get("Beta rule"));
|
2026-09-08 22:19:37 +03:00
|
|
|
assert.equal(edge.type, "follows");
|
|
|
|
|
assert.deepEqual(edge.provenance, [{ chatId: chat1.id, messageId: "a4" }]);
|
|
|
|
|
|
|
|
|
|
// Mapping lines: one per (chat, sourceId).
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
graphStore.readMappings().map((m) => m.sourceId),
|
|
|
|
|
["p1", "p2", "p3"],
|
|
|
|
|
);
|
2026-08-22 10:30:00 +03:00
|
|
|
|
2026-08-31 22:36:41 +03:00
|
|
|
// Events — kinds, refs, notes, and the attribution paths.
|
2026-08-22 10:30:00 +03:00
|
|
|
assert.equal(graph.events.length, 6);
|
|
|
|
|
const byRef = new Map(graph.events.map((e) => [e.ref, e]));
|
|
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
const miss = byRef.get("call-1")!;
|
|
|
|
|
assert.equal(miss.kind, "miss");
|
|
|
|
|
assert.equal(miss.nodeId, idByStatement.get("Alpha intuition"), "attributed quiz");
|
2026-08-22 10:30:00 +03:00
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
const dontKnow = byRef.get("call-2")!;
|
|
|
|
|
assert.equal(dontKnow.kind, "dont-know");
|
|
|
|
|
assert.equal(dontKnow.nodeId, null, "unattributable → null");
|
|
|
|
|
assert.equal(dontKnow.question, "Q2 text");
|
2026-08-22 10:30:00 +03:00
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
const correct = byRef.get("call-3")!;
|
|
|
|
|
assert.equal(correct.kind, "correct");
|
|
|
|
|
assert.equal(correct.nodeId, idByStatement.get("Alpha intuition"), "attribution wins over the ignored input.nodeId");
|
|
|
|
|
assert.equal(correct.note, "recalled the diagram");
|
2026-08-22 10:30:00 +03:00
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
const miss2 = byRef.get("call-4")!;
|
|
|
|
|
assert.equal(miss2.kind, "miss");
|
|
|
|
|
assert.equal(miss2.nodeId, idByStatement.get("Beta rule"));
|
2026-08-22 10:30:00 +03:00
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
const dontKnow2 = byRef.get("call-5")!;
|
|
|
|
|
assert.equal(dontKnow2.kind, "dont-know");
|
|
|
|
|
assert.equal(dontKnow2.nodeId, idByStatement.get("Gamma application"));
|
2026-08-22 10:30:00 +03:00
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
const observation = byRef.get("obs:p1")!;
|
|
|
|
|
assert.equal(observation.kind, "observation");
|
|
|
|
|
assert.equal(observation.nodeId, idByStatement.get("Alpha intuition"));
|
2026-09-09 02:06:32 +03:00
|
|
|
assert.equal(observation.note, "solid");
|
2026-08-22 10:30:00 +03:00
|
|
|
|
2026-08-31 22:36:41 +03:00
|
|
|
// The dismissed quiz and the ask question became nothing.
|
2026-08-22 10:30:00 +03:00
|
|
|
assert.equal([...byRef.keys()].filter((r) => r === "call-6" || r === "call-7").length, 0);
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
it("is idempotent — re-merging the same verdicts appends nothing", () => {
|
2026-09-13 02:08:55 +03:00
|
|
|
const stats = mergeFindings(chat1.id, fixtureAudit(), graphDeps, sessionSource(chat1.id));
|
2026-08-22 10:30:00 +03:00
|
|
|
assert.deepEqual(stats, { nodes: 0, edges: 0, events: 0 });
|
|
|
|
|
const graph = scopedGraph([chat1.id]);
|
|
|
|
|
assert.equal(graph.nodes.length, 3);
|
|
|
|
|
assert.equal(graph.edges.length, 1);
|
|
|
|
|
assert.equal(graph.events.length, 6);
|
2026-08-31 22:36:41 +03:00
|
|
|
// exactly one checkpoint line per merge, though — checkpoints are a log
|
2026-08-31 20:00:12 +03:00
|
|
|
assert.equal(graphStore.readCheckpoints().filter((c) => c.chatId === chat1.id).length, 2);
|
2026-08-22 10:30:00 +03:00
|
|
|
});
|
|
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
it("applies a linkTo verdict: phrasings join aliases, provenance unions, no duplicate node", () => {
|
|
|
|
|
const alphaId = scopedGraph([chat1.id]).nodes.find((n) => n.statement === "Alpha intuition")!.id;
|
|
|
|
|
const chat2 = fixtureChat("Graph fixture 2", [
|
2026-08-22 10:30:00 +03:00
|
|
|
user("u1", "again"),
|
2026-09-08 22:19:37 +03:00
|
|
|
assistant("a1", [
|
|
|
|
|
quizPart(
|
|
|
|
|
"call-9",
|
|
|
|
|
{ question: "Q9", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
|
|
|
|
answer({ questionId: "call-9", questionText: "Q9 text", correct: true, answeredAt: "2026-08-22T03:00:00.000Z" }),
|
|
|
|
|
),
|
|
|
|
|
]),
|
2026-08-22 10:30:00 +03:00
|
|
|
]);
|
|
|
|
|
chat2Id = chat2.id;
|
|
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
// The auditor's identity verdict: this proposition IS the alpha node.
|
|
|
|
|
const stats = mergeFindings(
|
|
|
|
|
chat2.id,
|
|
|
|
|
{
|
|
|
|
|
propositions: [
|
|
|
|
|
{
|
|
|
|
|
id: "q1",
|
2026-09-09 00:40:02 +03:00
|
|
|
name: "Alpha",
|
2026-09-08 22:19:37 +03:00
|
|
|
statement: "Alpha feel", // a DIFFERENT phrasing — identity is the verdict, not strings
|
|
|
|
|
aliases: ["alpha intuition"],
|
|
|
|
|
messages: ["a1"],
|
|
|
|
|
evidence: ["fixture evidence"],
|
|
|
|
|
linkTo: alphaId,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
edges: [],
|
|
|
|
|
attribution: { quiz: { "call-9": "q1" } },
|
|
|
|
|
dropped: [],
|
|
|
|
|
},
|
|
|
|
|
graphDeps,
|
2026-09-13 02:08:55 +03:00
|
|
|
sessionSource(chat2.id),
|
2026-09-08 22:19:37 +03:00
|
|
|
);
|
|
|
|
|
assert.deepEqual(stats, { nodes: 0, edges: 0, events: 1 });
|
|
|
|
|
|
2026-08-22 10:30:00 +03:00
|
|
|
const after = scopedGraph([chat1.id, chat2.id]);
|
2026-09-08 22:19:37 +03:00
|
|
|
assert.equal(after.nodes.length, 3, "no duplicate node");
|
|
|
|
|
const alpha = getGraph(graphStore).nodes.find((n) => n.id === alphaId)!;
|
|
|
|
|
assert.ok(alpha.aliases.includes("Alpha feel"), "the session's phrasing joins the aliases");
|
|
|
|
|
assert.ok(alpha.aliases.includes("alpha intuition"));
|
|
|
|
|
assert.ok(
|
|
|
|
|
alpha.provenance.some((pt) => pt.chatId === chat2.id && pt.messageId === "a1"),
|
|
|
|
|
"provenance unions",
|
|
|
|
|
);
|
|
|
|
|
// The mapping records the verdict: (chat2, q1) → the linked node.
|
|
|
|
|
const m = graphStore.readMappings().find((x) => x.chatId === chat2.id)!;
|
|
|
|
|
assert.deepEqual(m, { chatId: chat2.id, sourceId: "q1", graphNodeId: alphaId });
|
2026-08-22 10:30:00 +03:00
|
|
|
});
|
|
|
|
|
|
2026-09-13 02:08:55 +03:00
|
|
|
it("resolves imported-notes ids to their file paths in the snapshot (static provenance)", () => {
|
|
|
|
|
// The notes front door in miniature: a source record + a merge under
|
|
|
|
|
// its src id — the snapshot must label it with the file path (a
|
|
|
|
|
// static entry), never as a chat session.
|
|
|
|
|
graphStore.upsertSource({ id: "src_snapfixture", filePath: "/vault/learn vim.md", importedAt: "2026-09-13T00:00:00.000Z" });
|
|
|
|
|
mergeFindings(
|
|
|
|
|
"src_snapfixture",
|
|
|
|
|
{
|
|
|
|
|
propositions: [
|
|
|
|
|
{
|
|
|
|
|
id: "p1",
|
|
|
|
|
name: "Vim registers",
|
|
|
|
|
statement: "Vim registers map single keys to editing commands.",
|
|
|
|
|
aliases: [],
|
|
|
|
|
messages: [],
|
|
|
|
|
evidence: ["fixture evidence"],
|
|
|
|
|
understanding: "solid",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
edges: [],
|
|
|
|
|
attribution: { quiz: {} },
|
|
|
|
|
dropped: [],
|
|
|
|
|
},
|
|
|
|
|
graphDeps,
|
|
|
|
|
{ messages: null, foldedAt: "2026-09-13T00:00:00.000Z" },
|
|
|
|
|
);
|
|
|
|
|
const snap = graphSnapshot(graphDeps);
|
|
|
|
|
const node = snap.nodes.find((n) => n.statement === "Vim registers map single keys to editing commands.")!;
|
|
|
|
|
assert.equal(snap.fileSources["src_snapfixture"], "/vault/learn vim.md");
|
|
|
|
|
assert.deepEqual(snap.nodeSessions[node.id], ["src_snapfixture"], "the file entry joins the node's sessions list");
|
|
|
|
|
assert.equal(snap.sessions["src_snapfixture"], undefined, "a notes source is never a chat session");
|
|
|
|
|
// The understanding observation joins under the source id like any session's.
|
|
|
|
|
assert.equal(snap.nodeObservations[node.id]!["src_snapfixture"], "solid");
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-13 14:29:16 +03:00
|
|
|
it("derives semantic clusters in graphSnapshot, joined with persisted labelings", () => {
|
|
|
|
|
// Three propositions; the cluster comes from the EMBEDDING ledger
|
|
|
|
|
// (semantic topics, DECIDED 2026-09-13) — the embedder persists the
|
|
|
|
|
// partition, the snapshot just reads it. Here we play the embedder.
|
2026-09-13 03:59:48 +03:00
|
|
|
const tri = ["Alpha.", "Beta.", "Gamma cluster."] as const;
|
|
|
|
|
const merge = mergeFindings(
|
|
|
|
|
"src_clusterfixture",
|
|
|
|
|
{
|
|
|
|
|
propositions: tri.map((statement, i) => ({
|
|
|
|
|
id: `p${i + 1}`,
|
|
|
|
|
name: `Cluster node ${i + 1}`,
|
|
|
|
|
statement,
|
|
|
|
|
aliases: [],
|
|
|
|
|
messages: [],
|
|
|
|
|
evidence: ["fixture evidence"],
|
|
|
|
|
})),
|
|
|
|
|
edges: [
|
|
|
|
|
{ from: "p1", to: "p2", type: "follows", messages: [], evidence: ["e"] },
|
|
|
|
|
{ from: "p2", to: "p3", type: "follows", messages: [], evidence: ["e"] },
|
|
|
|
|
{ from: "p1", to: "p3", type: "follows", messages: [], evidence: ["e"] },
|
|
|
|
|
],
|
|
|
|
|
attribution: { quiz: {} },
|
|
|
|
|
dropped: [],
|
|
|
|
|
},
|
|
|
|
|
graphDeps,
|
|
|
|
|
{ messages: null, foldedAt: "2026-09-13T00:00:00.000Z" },
|
|
|
|
|
);
|
|
|
|
|
assert.equal(merge.nodes, 3);
|
2026-09-13 14:29:16 +03:00
|
|
|
// The embedder's contract: recluster at the end of the batch and
|
|
|
|
|
// PERSIST the partition in the embeddings ledger.
|
|
|
|
|
const ids = tri.map((s) => getGraph(graphStore).nodes.find((n) => n.statement === s)!.id);
|
|
|
|
|
graphStore.writeEmbeddings({
|
|
|
|
|
model: "test/embedder",
|
|
|
|
|
dim: 2,
|
|
|
|
|
vectors: {},
|
|
|
|
|
clusters: [{ members: ids }],
|
|
|
|
|
updatedAt: "2026-09-13T00:00:00.000Z",
|
|
|
|
|
});
|
2026-09-13 03:59:48 +03:00
|
|
|
const snap = graphSnapshot(graphDeps);
|
|
|
|
|
// Presentation threshold: every cluster is >= 3 members.
|
|
|
|
|
assert.ok(snap.clusters.every((c) => c.members.length >= 3));
|
|
|
|
|
// Our trio is exactly one cluster, unlabeled until the namer runs.
|
|
|
|
|
const mine = snap.clusters.find((c) => c.members.length === 3 && tri.every((s) => c.members.includes(snap.nodes.find((n) => n.statement === s)!.id)));
|
|
|
|
|
assert.ok(mine, "the 3-clique is a cluster");
|
|
|
|
|
assert.equal(mine!.label, null);
|
|
|
|
|
assert.match(mine!.id, /^clu_/);
|
|
|
|
|
// Mint the labeling the namer would append — the next read joins it.
|
|
|
|
|
graphStore.appendClusterLabel({
|
|
|
|
|
id: clusterContentId(mine!.members),
|
|
|
|
|
label: "Test region",
|
|
|
|
|
core: mine!.members,
|
|
|
|
|
createdAt: "2026-09-13T00:00:00.000Z",
|
|
|
|
|
});
|
|
|
|
|
const snap2 = graphSnapshot(graphDeps);
|
|
|
|
|
const claimed = snap2.clusters.find((c) => c.members.length === 3 && tri.every((s) => c.members.includes(snap2.nodes.find((n) => n.statement === s)!.id)))!;
|
|
|
|
|
assert.equal(claimed.label, "Test region");
|
|
|
|
|
assert.equal(claimed.id, clusterContentId(mine!.members), "the claimed cluster carries the record's id");
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-09 01:25:14 +03:00
|
|
|
it("derives sessions and per-session understanding observations in graphSnapshot", () => {
|
2026-08-31 20:00:12 +03:00
|
|
|
const snap = graphSnapshot(graphDeps);
|
2026-08-22 10:30:00 +03:00
|
|
|
const idByStatement = new Map(snap.nodes.map((n) => [n.statement, n.id]));
|
2026-09-09 01:24:08 +03:00
|
|
|
// No node-state classes anymore (2026-09-08): the snapshot carries
|
|
|
|
|
// only the objective graph + sessions; quiz outcomes stay ledger data.
|
|
|
|
|
assert.equal("nodeStates" in snap, false);
|
2026-08-22 10:30:00 +03:00
|
|
|
|
|
|
|
|
assert.equal(snap.sessions[chat1.id], "Graph fixture");
|
|
|
|
|
assert.equal(snap.sessions[chat2Id], "Graph fixture 2");
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
(snap.nodeSessions[idByStatement.get("Alpha intuition")!] ?? []).sort(),
|
|
|
|
|
[chat1.id, chat2Id].sort(),
|
|
|
|
|
);
|
2026-08-31 22:36:41 +03:00
|
|
|
// Gamma was only ever touched by chat1.
|
2026-08-22 10:30:00 +03:00
|
|
|
assert.deepEqual(snap.nodeSessions[idByStatement.get("Gamma application")!], [chat1.id]);
|
2026-09-09 01:25:14 +03:00
|
|
|
|
|
|
|
|
// Understanding observations: (node → chat → the extractor's note).
|
2026-09-09 02:06:32 +03:00
|
|
|
// Alpha carries chat1's "solid" level; chat2's q1 has no level.
|
2026-09-09 01:25:14 +03:00
|
|
|
const alphaObs = snap.nodeObservations[idByStatement.get("Alpha intuition")!] ?? {};
|
2026-09-09 02:06:32 +03:00
|
|
|
assert.equal(alphaObs[chat1.id], "solid");
|
2026-09-09 01:25:14 +03:00
|
|
|
assert.equal(alphaObs[chat2Id], undefined);
|
2026-08-22 10:30:00 +03:00
|
|
|
});
|
|
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
it("never string-matches without a verdict — an identical statement without linkTo is a NEW node", () => {
|
|
|
|
|
const chat3 = fixtureChat("Graph fixture 3", [user("u1", "third")]);
|
|
|
|
|
chat3Id = chat3.id;
|
|
|
|
|
const stats = mergeFindings(
|
|
|
|
|
chat3.id,
|
|
|
|
|
{
|
|
|
|
|
propositions: [
|
|
|
|
|
{
|
|
|
|
|
id: "r1",
|
2026-09-09 00:40:02 +03:00
|
|
|
name: "Alpha",
|
2026-09-08 22:19:37 +03:00
|
|
|
statement: "Alpha intuition", // IDENTICAL to the existing node's statement
|
|
|
|
|
aliases: [],
|
|
|
|
|
messages: ["u1"],
|
|
|
|
|
evidence: ["fixture evidence"],
|
|
|
|
|
// no linkTo — identity is the auditor's call alone
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
edges: [],
|
|
|
|
|
attribution: { quiz: {} },
|
|
|
|
|
dropped: [],
|
|
|
|
|
},
|
|
|
|
|
graphDeps,
|
2026-09-13 02:08:55 +03:00
|
|
|
sessionSource(chat3.id),
|
2026-09-08 22:19:37 +03:00
|
|
|
);
|
|
|
|
|
assert.equal(stats.nodes, 1, "created — the merger does not match strings");
|
|
|
|
|
assert.equal(getGraph(graphStore).nodes.filter((n) => n.statement === "Alpha intuition").length, 2);
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-13 02:08:55 +03:00
|
|
|
it("merges the notes front door: source id as chatId, no quiz events, checkpoint at import time", () => {
|
|
|
|
|
// Raw personal notes carry no transcript — the merger gets no
|
|
|
|
|
// message parts, so NO quiz events are rebuilt; the understanding
|
|
|
|
|
// observation still lands, keyed to the source id.
|
|
|
|
|
const stats = mergeFindings(
|
|
|
|
|
"src_notesfixture",
|
|
|
|
|
{
|
|
|
|
|
propositions: [
|
|
|
|
|
{
|
|
|
|
|
id: "p1",
|
|
|
|
|
name: "Vim normal mode",
|
|
|
|
|
statement: "Vim starts in Normal mode, where keys are commands.",
|
|
|
|
|
aliases: [],
|
|
|
|
|
messages: [], // notes carry no message anchors — the file is the provenance
|
|
|
|
|
evidence: ["fixture evidence"],
|
|
|
|
|
understanding: "solid",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
edges: [],
|
|
|
|
|
attribution: { quiz: {} }, // normalized by the notes pipeline — nothing to attribute
|
|
|
|
|
dropped: [],
|
|
|
|
|
},
|
|
|
|
|
graphDeps,
|
|
|
|
|
{ messages: null, foldedAt: "2026-09-13T00:00:00.000Z" },
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(stats, { nodes: 1, edges: 0, events: 1 });
|
|
|
|
|
|
|
|
|
|
const node = getGraph(graphStore).nodes.find((n) => n.statement.startsWith("Vim starts"))!;
|
|
|
|
|
assert.deepEqual(node.provenance, [], "no message anchors — the mapping line carries the source");
|
|
|
|
|
assert.deepEqual(graphStore.readMappings().find((m) => m.chatId === "src_notesfixture"), {
|
|
|
|
|
chatId: "src_notesfixture",
|
|
|
|
|
sourceId: "p1",
|
|
|
|
|
graphNodeId: node.id,
|
2026-08-31 22:36:41 +03:00
|
|
|
});
|
2026-09-13 02:08:55 +03:00
|
|
|
// The observation event is file-scoped; no quiz events exist.
|
|
|
|
|
const events = graphStore.readEvents().filter((e) => e.chatId === "src_notesfixture");
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
events.map((e) => [e.kind, e.ref, e.note]),
|
|
|
|
|
[["observation", "obs:p1", "solid"]],
|
|
|
|
|
);
|
|
|
|
|
// The checkpoint stamps the import time.
|
|
|
|
|
assert.equal(graphStore.readCheckpoints().find((c) => c.chatId === "src_notesfixture")?.foldedAt, "2026-09-13T00:00:00.000Z");
|
2026-08-22 10:30:00 +03:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ─── store primitives ────────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
describe("graph ledger store", () => {
|
|
|
|
|
it("round-trips lines and skips corrupt / foreign-shaped lines (strict reads)", () => {
|
2026-09-08 22:19:37 +03:00
|
|
|
const node = {
|
|
|
|
|
id: "n_storetest",
|
2026-09-09 00:40:02 +03:00
|
|
|
name: "Store test",
|
|
|
|
|
statement: "Store test, elaborated.",
|
2026-09-08 22:19:37 +03:00
|
|
|
aliases: [] as string[],
|
|
|
|
|
createdAt: "2026-08-22T00:00:00.000Z",
|
|
|
|
|
provenance: [{ chatId: "chat_c", messageId: "m1" }],
|
|
|
|
|
};
|
2026-08-31 20:00:12 +03:00
|
|
|
graphStore.appendNode(node);
|
2026-09-09 00:40:02 +03:00
|
|
|
// A torn append and a pre-name-pivot node (old schema: description, no
|
|
|
|
|
// name) — both skipped by the strict guard.
|
2026-08-22 10:30:00 +03:00
|
|
|
fs.appendFileSync(
|
|
|
|
|
LEDGER_FILES.nodes,
|
2026-09-09 00:40:02 +03:00
|
|
|
`{not json}\n${JSON.stringify({ id: "legacy", statement: "Legacy shape", description: "Old schema.", aliases: [], createdAt: "2026-08-22T00:00:00.000Z", provenance: [] })}\n`,
|
2026-08-22 10:30:00 +03:00
|
|
|
"utf-8",
|
|
|
|
|
);
|
2026-08-31 20:00:12 +03:00
|
|
|
const nodes = graphStore.readNodes().filter((n) => n.id === "n_storetest" || n.id === "legacy");
|
2026-08-22 10:30:00 +03:00
|
|
|
assert.equal(nodes.length, 1);
|
2026-09-09 00:40:02 +03:00
|
|
|
assert.equal(nodes[0]!.name, "Store test");
|
|
|
|
|
assert.equal(nodes[0]!.statement, "Store test, elaborated.");
|
2026-09-08 22:19:37 +03:00
|
|
|
assert.deepEqual(nodes[0]!.provenance, [{ chatId: "chat_c", messageId: "m1" }]);
|
2026-09-01 03:34:47 +03:00
|
|
|
});
|
|
|
|
|
|
2026-09-08 22:19:37 +03:00
|
|
|
it("returns RAW node lines — repeated ids (link-appends) are each their own line; getGraph merges", () => {
|
|
|
|
|
const line = (msgId: string) =>
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
id: "n_linktest",
|
2026-09-09 00:40:02 +03:00
|
|
|
name: "Link test",
|
2026-09-08 22:19:37 +03:00
|
|
|
statement: `Phrasing ${msgId}`,
|
|
|
|
|
aliases: [],
|
|
|
|
|
createdAt: "2026-08-22T00:00:00.000Z",
|
|
|
|
|
provenance: [{ chatId: "chat_c", messageId: msgId }],
|
|
|
|
|
});
|
|
|
|
|
fs.appendFileSync(LEDGER_FILES.nodes, `${line("m1")}\n${line("m2")}\n`, "utf-8");
|
|
|
|
|
const lines = graphStore.readNodes().filter((n) => n.id === "n_linktest");
|
|
|
|
|
assert.equal(lines.length, 2, "the store returns raw lines, one per append");
|
|
|
|
|
const merged = getGraph(graphStore).nodes.find((n) => n.id === "n_linktest")!;
|
|
|
|
|
assert.equal(merged.statement, "Phrasing m1", "the FIRST line defines the node");
|
|
|
|
|
assert.ok(merged.aliases.includes("Phrasing m2"), "later phrasings join the aliases");
|
|
|
|
|
assert.deepEqual(merged.provenance.map((p) => p.messageId), ["m1", "m2"]);
|
2026-08-22 10:30:00 +03:00
|
|
|
});
|
|
|
|
|
});
|