Sync main (graph redesign b07c907): extraction observability ported onto the redesigned pipeline
Conflict resolution: extraction.ts takes main's redesign wholesale and
re-applies 072671f's observability deltas (stage durations, pipeline
failure line) — log shape only, no behavior change. graph-view.tsx's
legend/filters (094bafa) rekeyed from node.name (removed) to
statement/description, matching the new GraphNode shape.
This commit is contained in:
+89
-71
@@ -1,13 +1,16 @@
|
||||
/**
|
||||
* The extraction pipeline tests (vision §1 construction): extractor →
|
||||
* auditor → merger, the sole graph writer.
|
||||
* The extraction pipeline tests (vision §1 construction, 2026-09-02 redesign):
|
||||
* extractor → auditor → merger, the sole graph writer — tree-blind,
|
||||
* free-form, with the auditor owning faithfulness and identity.
|
||||
*
|
||||
* transcriptView — the compact view both LLM stages see (pure)
|
||||
* transcriptView — the compact view both LLM stages see (pure):
|
||||
* message-id-tagged verbatim text + quiz lines, NO tree
|
||||
* pipeline — MockLanguageModelV4 drives both agents: findings out,
|
||||
* audit verdict in, deterministic merge applied; failure
|
||||
* paths checkpoint nothing; single-flight per chat
|
||||
* audit verdict (with linkTo identity) in, deterministic
|
||||
* merge applied; failure paths checkpoint nothing;
|
||||
* single-flight per chat
|
||||
*
|
||||
* The merger itself (identity resolution, event rebuild, idempotency) is
|
||||
* The merger itself (identity verdicts, event rebuild, idempotency) is
|
||||
* tested in tests/graph.test.ts directly against mergeFindings — here it is
|
||||
* the integration: what one full pipeline run does to the ledgers.
|
||||
*
|
||||
@@ -21,9 +24,16 @@ import * as fs from "node:fs";
|
||||
import { after, describe, it } from "node:test";
|
||||
import { MockLanguageModelV4 } from "ai/test";
|
||||
import type { UIMessage } from "ai";
|
||||
import type { GraphEdge, GraphNode, LedgerEvent, NodeMapping, SessionCheckpoint, SessionFindings } from "../src/core/contracts.ts";
|
||||
import type {
|
||||
AuditResult,
|
||||
GraphEdge,
|
||||
GraphNode,
|
||||
LedgerEvent,
|
||||
NodeMapping,
|
||||
SessionCheckpoint,
|
||||
SessionFindings,
|
||||
} from "../src/core/contracts.ts";
|
||||
import type { GraphStore } from "../src/core/ports.ts";
|
||||
import { createTree, markDone } from "../src/core/plan-tree.ts";
|
||||
import { createExtractionPipeline, transcriptView } from "../src/agent/subagents/extraction.ts";
|
||||
import { chatStore } from "../src/store/chats.ts";
|
||||
import { PATHS } from "../config.ts";
|
||||
@@ -72,7 +82,7 @@ function quizPart(toolCallId: string, input: Record<string, unknown>, output: Re
|
||||
return { type: "tool-quiz", toolCallId, state: "output-available", input, output };
|
||||
}
|
||||
|
||||
function planPart(toolCallId: string, name: string, input: Record<string, unknown>, tree: ReturnType<typeof createTree>): unknown {
|
||||
function planPart(toolCallId: string, name: string, input: Record<string, unknown>, tree: unknown): unknown {
|
||||
return {
|
||||
type: `tool-${name}`,
|
||||
toolCallId,
|
||||
@@ -82,21 +92,11 @@ function planPart(toolCallId: string, name: string, input: Record<string, unknow
|
||||
};
|
||||
}
|
||||
|
||||
/** A session-shaped fixture chat: probe → plan → check → mark_done. */
|
||||
/** A session-shaped fixture chat: probe → teaching text → check. Tree-blind
|
||||
* world: the plan tree is NOT part of extraction — it is noise. */
|
||||
function fixtureChat() {
|
||||
const tree = markDone(
|
||||
createTree(
|
||||
[
|
||||
{ id: "cr", title: "Chain rule intuition" },
|
||||
{ id: "apply", title: "Applying the chain rule" },
|
||||
],
|
||||
[{ from: "cr", to: "apply" }],
|
||||
),
|
||||
"cr",
|
||||
);
|
||||
const chat = chatStore.createChat("Extraction fixture");
|
||||
createdChats.push(chat.id);
|
||||
chatStore.saveTree(chat.id, tree);
|
||||
chatStore.saveMessages(chat.id, [
|
||||
user("u1", "teach me the chain rule"),
|
||||
assistant("a1", [
|
||||
@@ -107,15 +107,14 @@ function fixtureChat() {
|
||||
]),
|
||||
assistant("a2", [
|
||||
{ type: "text", text: "The derivative measures how fast a function changes." },
|
||||
planPart("p1", "plan_init", { nodes: [], edges: [] }, tree),
|
||||
planPart("p1", "plan_init", { nodes: [], edges: [] }, { nodes: [], edges: [], currentNodeId: null, doneNodeIds: [], version: 1 }),
|
||||
]),
|
||||
assistant("a3", [
|
||||
quizPart("check-1", { question: "Check", nodeId: "apply" }, {
|
||||
quizPart("check-1", { question: "Check" }, {
|
||||
questionId: "check-1", kind: "quiz", questionText: "Check", correct: true, dontKnow: false,
|
||||
answeredAt: "2026-08-22T02:00:00.000Z",
|
||||
}),
|
||||
]),
|
||||
assistant("a4", [planPart("p2", "plan_mark_done", { id: "cr" }, tree)]),
|
||||
]);
|
||||
return { chatId: chat.id, record: chatStore.getChat(chat.id)! };
|
||||
}
|
||||
@@ -130,30 +129,38 @@ after(() => {
|
||||
}
|
||||
});
|
||||
|
||||
/** What the extractor would produce for the fixture session. */
|
||||
/** What the extractor would produce for the fixture session — tree-blind,
|
||||
* free-form, with message-granular provenance + an understanding note. */
|
||||
const FINDINGS: SessionFindings = {
|
||||
propositions: [
|
||||
{
|
||||
id: "p1",
|
||||
name: "Chain rule",
|
||||
statement: "Chain rule intuition",
|
||||
statement: "The derivative measures how fast a function changes",
|
||||
description: "The derivative of a function at a point is its instantaneous rate of change there.",
|
||||
aliases: [],
|
||||
kind: "truth",
|
||||
treeNodeId: "cr",
|
||||
evidence: ["TEACHER: The derivative measures how fast a function changes."],
|
||||
messages: ["a2"],
|
||||
evidence: ["TEACHER [a2]: The derivative measures how fast a function changes."],
|
||||
understanding: "solid",
|
||||
},
|
||||
{
|
||||
id: "p2",
|
||||
name: "Applying chain rule",
|
||||
statement: "Applying the chain rule",
|
||||
statement: "Chain rule intuition",
|
||||
description: "The chain rule differentiates composed functions by multiplying local rates.",
|
||||
aliases: [],
|
||||
kind: "truth",
|
||||
treeNodeId: "apply",
|
||||
evidence: ["FINAL PLAN TREE (node ids): cr = Chain rule intuition; apply = Applying the chain rule"],
|
||||
messages: ["a2"],
|
||||
evidence: ["LEARNER [u1]: teach me the chain rule"],
|
||||
},
|
||||
],
|
||||
edges: [{ from: "p1", to: "p2", type: "requires", evidence: ["TREE EDGES: cr requires apply"] }],
|
||||
attribution: { quiz: { "probe-1": "p1" } },
|
||||
edges: [
|
||||
{
|
||||
from: "p1",
|
||||
to: "p2",
|
||||
type: "follows",
|
||||
messages: ["a2"],
|
||||
evidence: ["LEARNER [u1]: teach me the chain rule"],
|
||||
},
|
||||
],
|
||||
attribution: { quiz: { "probe-1": "p1", "check-1": "p2" } },
|
||||
};
|
||||
|
||||
/** A model whose ONE tool call is `name` with the given input; then text. */
|
||||
@@ -202,28 +209,26 @@ function silentModel(): MockLanguageModelV4 {
|
||||
// ─── transcriptView (pure) ───────────────────────────────────────────────────
|
||||
|
||||
describe("transcriptView (pure)", () => {
|
||||
it("shows the final tree, verbatim conversation, structured events — plan ops folded", () => {
|
||||
it("shows message-id-tagged verbatim conversation + quiz lines — tree-blind, plan ops folded", () => {
|
||||
const { record } = fixtureChat();
|
||||
const view = transcriptView(record);
|
||||
|
||||
// The final tree with node ids (the extractor's backbone + the
|
||||
// pruned-out stays pruned — only the FINAL tree is shown).
|
||||
assert.match(view, /SESSION: Extraction fixture/);
|
||||
assert.match(view, /FINAL PLAN TREE \(node ids\): cr = Chain rule intuition; apply = Applying the chain rule/);
|
||||
assert.match(view, /TREE EDGES: cr requires apply/);
|
||||
|
||||
// Conversation verbatim — evidence quotes can come from it.
|
||||
assert.match(view, /LEARNER: teach me the chain rule/);
|
||||
assert.match(view, /TEACHER: The derivative measures how fast a function changes\./);
|
||||
// Conversation verbatim, each line tagged with its message id —
|
||||
// the provenance anchors the extractor reports back.
|
||||
assert.match(view, /LEARNER \[u1\] teach me the chain rule/);
|
||||
assert.match(view, /TEACHER \[a2\] The derivative measures how fast a function changes\./);
|
||||
|
||||
// Structured events, attribution-ready.
|
||||
assert.match(view, /QUIZ \[probe-1\] What is a derivative, roughly\? — the learner answered incorrectly/);
|
||||
assert.match(view, /QUIZ \[check-1\] Check — the learner answered correctly/);
|
||||
assert.match(view, /TEACHER marked tree node done \[p2\]: cr/);
|
||||
|
||||
// plan_init / plan_mark_done tool parts themselves are folded into
|
||||
// the tree line — no raw JSON noise.
|
||||
assert.doesNotMatch(view, /plan_init/);
|
||||
// TREE-BLIND: no plan tree, no tree edges, no mark_done lines — plan
|
||||
// ops fold to the generic one-liner like every other non-quiz tool.
|
||||
assert.doesNotMatch(view, /FINAL PLAN TREE/);
|
||||
assert.doesNotMatch(view, /TREE EDGES/);
|
||||
assert.match(view, /\(plan_init tool result delivered\)/);
|
||||
assert.doesNotMatch(view, /output-available/);
|
||||
});
|
||||
|
||||
@@ -253,48 +258,61 @@ describe("transcriptView (pure)", () => {
|
||||
// ─── the pipeline ───────────────────────────────────────────────────────────
|
||||
|
||||
describe("the extraction pipeline (extractor → auditor → merger)", () => {
|
||||
it("runs end to end: findings → audit verdict → deterministic merge + checkpoint", async () => {
|
||||
it("runs end to end: findings → audit verdict (identity) → deterministic merge + checkpoint", async () => {
|
||||
const { chatId } = fixtureChat();
|
||||
const ledgers = memoryLedgers();
|
||||
|
||||
// The auditor passes p1 through, drops p2 (unbacked), re-points the
|
||||
// probe attribution to p1, and records the drop.
|
||||
const verified: SessionFindings = {
|
||||
propositions: [FINDINGS.propositions[0]!],
|
||||
// The auditor passes p1 through and LINKS it to a pre-existing node
|
||||
// (identity verdict), drops p2 (unbacked) + the edge with it.
|
||||
const existingId = "n_existing";
|
||||
ledgers.appendNode({
|
||||
id: existingId,
|
||||
statement: "Derivatives measure rates of change",
|
||||
description: "Pre-existing node from an earlier session.",
|
||||
aliases: [],
|
||||
createdAt: "2026-08-01T00:00:00.000Z",
|
||||
provenance: [],
|
||||
});
|
||||
const verdict: AuditResult = {
|
||||
propositions: [
|
||||
{ ...FINDINGS.propositions[0]!, linkTo: existingId },
|
||||
],
|
||||
edges: [],
|
||||
attribution: { quiz: { "probe-1": "p1" } },
|
||||
dropped: [{ ref: "p2", reason: "evidence not found" }, { ref: "p1→p2", reason: "endpoint dropped" }],
|
||||
};
|
||||
|
||||
const pipeline = createExtractionPipeline({
|
||||
chatStore,
|
||||
graphStore: ledgers,
|
||||
extractor: submitModel("submit_findings", FINDINGS),
|
||||
auditor: submitModel("submit_review", { ...verified, dropped: [{ ref: "p2", reason: "evidence not found" }] }),
|
||||
auditor: submitModel("submit_review", verdict),
|
||||
});
|
||||
const stats = await pipeline.runExtraction(chatId);
|
||||
|
||||
assert.deepEqual(stats, { nodes: 1, edges: 0, events: 3 });
|
||||
// The linked proposition created NO new node; the understanding note
|
||||
// became an observation; both quizzes became events.
|
||||
assert.deepEqual(stats, { nodes: 0, edges: 0, events: 3 });
|
||||
|
||||
// Only the audited proposition became a node; mapping provenance kept.
|
||||
assert.equal(ledgers.readNodes().length, 1);
|
||||
assert.equal(ledgers.readNodes()[0]!.statement, "Chain rule intuition");
|
||||
assert.deepEqual(ledgers.readMappings(), [{ chatId, sourceId: "cr", graphNodeId: ledgers.readNodes()[0]!.id }]);
|
||||
// Link-append: the phrasing joined the existing node's aliases and
|
||||
// the provenance unioned; no second node line defines a new node.
|
||||
assert.equal(ledgers.readNodes().length, 2); // the pre-seeded line + the link line
|
||||
assert.deepEqual(ledgers.readMappings(), [{ chatId, sourceId: "p1", graphNodeId: existingId }]);
|
||||
|
||||
// Events rebuilt deterministically from the transcript — the probe
|
||||
// retroactively attributed, the check tagged, mark_done mapped.
|
||||
// Events rebuilt deterministically: quizzes in transcript order, then
|
||||
// the observation. check-1's attribution pointed at the DROPPED p2 →
|
||||
// unresolvable → null (the question text is kept for later attribution).
|
||||
const events = ledgers.readEvents();
|
||||
assert.deepEqual(
|
||||
events.map((e) => [e.kind, e.ref]),
|
||||
events.map((e) => [e.kind, e.ref, e.nodeId]),
|
||||
[
|
||||
["probe-miss", "probe-1"],
|
||||
["check-correct", "check-1"],
|
||||
["taught", "taught:cr"],
|
||||
["miss", "probe-1", existingId],
|
||||
["correct", "check-1", null],
|
||||
["observation", "obs:p1", existingId],
|
||||
],
|
||||
);
|
||||
const nodeId = ledgers.readNodes()[0]!.id;
|
||||
assert.equal(events[0]!.nodeId, nodeId, "probe attributed via audit");
|
||||
assert.equal(events[1]!.nodeId, null, "tagged to a dropped proposition → null");
|
||||
assert.equal(events[2]!.nodeId, nodeId, "taught via tree-node mapping");
|
||||
assert.equal(events[0]!.nodeId, existingId, "probe attributed via the verdict's mapping");
|
||||
assert.equal(events[2]!.note, "solid", "the understanding note joins the ledger");
|
||||
|
||||
// Checkpointed through the record's updatedAt — never the same turns twice.
|
||||
assert.equal(ledgers.readCheckpoints().length, 1);
|
||||
@@ -318,7 +336,7 @@ describe("the extraction pipeline (extractor → auditor → merger)", () => {
|
||||
chatStore,
|
||||
graphStore: ledgers,
|
||||
extractor: silentModel(),
|
||||
auditor: submitModel("submit_review", FINDINGS),
|
||||
auditor: submitModel("submit_review", { ...FINDINGS, dropped: [] }),
|
||||
});
|
||||
await assert.rejects(failingExtractor.runExtraction(chatId), /the extractor never submitted findings/);
|
||||
assert.equal(ledgers.readCheckpoints().length, 0);
|
||||
|
||||
+117
-138
@@ -1,13 +1,14 @@
|
||||
/**
|
||||
* Graph-as-context tests (W8; vision §1 "the graph as session context").
|
||||
* Graph-as-context tests (vision §1 "the graph as session context", 2026-09-02
|
||||
* redesign vocabulary: follows edges, no node kinds).
|
||||
*
|
||||
* renderGraphContext — pure: empty / small / cap-truncation rendering
|
||||
* buildGraphContext — per-request entry point (derived-graph → block)
|
||||
* injection plumbing — chat-flow style: the teacher's instructions carry
|
||||
* the block (MockLanguageModelV4 captures the prompt)
|
||||
* exact-match merge — the merger (mergeFindings) dedupes
|
||||
* case/whitespace-identical statements onto existing
|
||||
* nodes (no fuzzy matching)
|
||||
* cross-chat edges — the merger (mergeFindings) appends one ASSERTION
|
||||
* line per session; reads merge with unioned
|
||||
* message-granular provenance
|
||||
*
|
||||
* Ledger isolation: NONE needed for the merge tests — the merger's ledger is
|
||||
* injectable (in-memory), so the live data/graph files are only ever READ
|
||||
@@ -22,7 +23,14 @@ import * as path from "node:path";
|
||||
import { after, describe, it } from "node:test";
|
||||
import { MockLanguageModelV4, simulateReadableStream } from "ai/test";
|
||||
import type { LanguageModelV4StreamPart } from "@ai-sdk/provider";
|
||||
import type { GraphEdge, GraphNode, LedgerEvent, SessionFindings } from "../src/core/contracts.ts";
|
||||
import type {
|
||||
AuditResult,
|
||||
GraphEdge,
|
||||
GraphNode,
|
||||
LedgerEvent,
|
||||
NodeMapping,
|
||||
SessionCheckpoint,
|
||||
} from "../src/core/contracts.ts";
|
||||
import type { NodeStateClass } from "../src/core/contracts.ts";
|
||||
import { getGraph, mergeFindings } from "../src/core/graph.ts";
|
||||
import type { GraphStore } from "../src/core/ports.ts";
|
||||
@@ -35,19 +43,25 @@ import {
|
||||
import { createTeacherAgent } from "../src/agent/teacher-agent.ts";
|
||||
import { handleChatRequest } from "../src/server/chat-handler.ts";
|
||||
import { chatStore } from "../src/store/chats.ts";
|
||||
import type { NodeMapping, SessionCheckpoint } from "../src/core/contracts.ts";
|
||||
import { graphStore } from "../src/store/graph.ts";
|
||||
import { createTree } from "../src/core/plan-tree.ts";
|
||||
import { PATHS } from "../config.ts";
|
||||
|
||||
// ─── fixtures ────────────────────────────────────────────────────────────────
|
||||
|
||||
function node(id: string, statement: string, over: Partial<GraphNode> = {}): GraphNode {
|
||||
return { id, name: statement.split(";")[0]!, statement, aliases: [], kind: "truth", createdAt: "2026-08-22T00:00:00.000Z", ...over };
|
||||
return {
|
||||
id,
|
||||
statement,
|
||||
description: `${statement} — elaborated.`,
|
||||
aliases: [],
|
||||
createdAt: "2026-08-22T00:00:00.000Z",
|
||||
provenance: [],
|
||||
...over,
|
||||
};
|
||||
}
|
||||
|
||||
function edge(from: string, to: string): GraphEdge {
|
||||
return { from, to, type: "requires", provenance: ["chat_x"] };
|
||||
return { from, to, type: "follows", provenance: [{ chatId: "chat_x", messageId: "m1" }] };
|
||||
}
|
||||
|
||||
function states(...entries: [string, NodeStateClass][]): Record<string, NodeStateClass> {
|
||||
@@ -61,7 +75,7 @@ describe("renderGraphContext (pure)", () => {
|
||||
assert.equal(renderGraphContext([], [], {}), null);
|
||||
});
|
||||
|
||||
it("renders a header, state-grouped node lines (hotspots first), and chained requires edges", () => {
|
||||
it("renders a header, state-grouped node lines (hotspots first), and chained follows edges", () => {
|
||||
const nodes = [
|
||||
node("n_a", "Chain rule intuition"),
|
||||
node("n_b", "Quotient rule"),
|
||||
@@ -73,26 +87,26 @@ describe("renderGraphContext (pure)", () => {
|
||||
["n_a", "known"],
|
||||
["n_b", "missed"],
|
||||
["n_c", "candidate"],
|
||||
["n_d", "taught"],
|
||||
["n_d", "dont-know"],
|
||||
);
|
||||
const out = renderGraphContext(nodes, edges, st)!;
|
||||
|
||||
assert.ok(out.includes(GRAPH_CONTEXT_HEADER), "header line present");
|
||||
assert.match(out, /4 nodes, 3 requires edges/);
|
||||
assert.match(out, /4 nodes, 3 follows edges/);
|
||||
|
||||
// Node lines: "• [state] statement (id)".
|
||||
assert.ok(out.includes("• [missed] Quotient rule (n_b)"));
|
||||
assert.ok(out.includes("• [known] Chain rule intuition (n_a)"));
|
||||
assert.ok(out.includes("• [taught] Composite functions (n_d)"));
|
||||
assert.ok(out.includes("• [dont-know] Composite functions (n_d)"));
|
||||
assert.ok(out.includes("• [candidate] Product rule (n_c)"));
|
||||
|
||||
// Groups in the decided order: hotspots, known, taught, candidates.
|
||||
const order = ["Hotspots", "Known", "Taught", "Candidates"].map((g) => out.indexOf(g));
|
||||
// Groups in the decided order: hotspots, known, candidates.
|
||||
const order = ["Hotspots", "Known", "Candidates"].map((g) => out.indexOf(g));
|
||||
assert.deepEqual([...order].sort((a, b) => a - b), order, "groups appear in priority order");
|
||||
|
||||
// Edges merged into chains: d → a → b → c (one line, three arrows).
|
||||
assert.ok(out.includes("Composite functions → Chain rule intuition → Quotient rule → Product rule"));
|
||||
assert.ok(out.includes("Requires (prerequisite → dependent)"));
|
||||
assert.ok(out.includes("Follows (premise → derivation)"));
|
||||
assert.ok(out.includes("===== end of knowledge graph ====="), "clear end delimiter");
|
||||
});
|
||||
|
||||
@@ -104,15 +118,8 @@ describe("renderGraphContext (pure)", () => {
|
||||
assert.ok(!out.includes("B → C"), "no edge was invented between siblings");
|
||||
});
|
||||
|
||||
it("marks misconception-kind nodes as hotspots even when their state is candidate", () => {
|
||||
const nodes = [node("n_m", "Division by zero is fine", { kind: "misconception" })];
|
||||
const out = renderGraphContext(nodes, [], states(["n_m", "candidate"]))!;
|
||||
assert.match(out, /• \[candidate\] Division by zero is fine \(n_m\) · misconception/);
|
||||
assert.ok(out.includes("Hotspots"), "misconception nodes join the hotspot group");
|
||||
});
|
||||
|
||||
it("caps at MAX_CONTEXT_NODES: hotspots + their 1-hop requires neighborhood only, with a truncation note", () => {
|
||||
// 3 hotspots, each with a prerequisite and a dependent (1-hop), plus a
|
||||
it("caps at MAX_CONTEXT_NODES: hotspots + their 1-hop follows neighborhood only, with a truncation note", () => {
|
||||
// 3 hotspots, each with a premise and a derivation (1-hop), plus a
|
||||
// long tail of disconnected candidates that must drop out.
|
||||
const nodes: GraphNode[] = [];
|
||||
const edges: GraphEdge[] = [];
|
||||
@@ -121,7 +128,7 @@ describe("renderGraphContext (pure)", () => {
|
||||
const h = `n_h${i}`;
|
||||
const pre = `n_p${i}`;
|
||||
const dep = `n_d${i}`;
|
||||
nodes.push(node(pre, `Prerequisite ${i}`), node(h, `Hotspot ${i}`), node(dep, `Dependent ${i}`));
|
||||
nodes.push(node(pre, `Premise ${i}`), node(h, `Hotspot ${i}`), node(dep, `Derivation ${i}`));
|
||||
edges.push(edge(pre, h), edge(h, dep));
|
||||
st[pre] = "known";
|
||||
st[h] = "missed";
|
||||
@@ -139,11 +146,11 @@ describe("renderGraphContext (pure)", () => {
|
||||
|
||||
const out = renderGraphContext(nodes, edges, st)!;
|
||||
assert.ok(out.includes("• [missed] Hotspot 1 (n_h1)"), "hotspots stay");
|
||||
assert.ok(out.includes("• [known] Prerequisite 1 (n_p1)"), "1-hop prerequisites stay");
|
||||
assert.ok(out.includes("• [candidate] Dependent 1 (n_d1)"), "1-hop dependents stay");
|
||||
assert.ok(out.includes("• [known] Premise 1 (n_p1)"), "1-hop premises stay");
|
||||
assert.ok(out.includes("• [candidate] Derivation 1 (n_d1)"), "1-hop derivations stay");
|
||||
assert.ok(!out.includes("Far away"), "disconnected nodes drop out");
|
||||
assert.match(out, new RegExp(`showing 9 of ${nodes.length} nodes`), "truncation is noted");
|
||||
assert.ok(out.includes(", 6 requires edges"), "edges among the selected nodes stay");
|
||||
assert.ok(out.includes(", 6 follows edges"), "edges among the selected nodes stay");
|
||||
});
|
||||
|
||||
it("keeps the full graph at exactly the cap (no truncation note)", () => {
|
||||
@@ -169,11 +176,11 @@ describe("buildGraphContext", () => {
|
||||
it("derives states from events and renders the block", () => {
|
||||
const nodes = [node("n_a", "Solo node"), node("n_b", "Checked node")];
|
||||
const events: LedgerEvent[] = [
|
||||
{ nodeId: "n_b", kind: "check-correct", chatId: "c1", at: "2026-08-22T00:00:00.000Z", ref: "r1" },
|
||||
{ nodeId: "n_b", kind: "correct", chatId: "c1", at: "2026-08-22T00:00:00.000Z", ref: "r1" },
|
||||
];
|
||||
const out = buildGraphContext({ nodes, edges: [], events })!;
|
||||
assert.ok(out.includes(GRAPH_CONTEXT_HEADER));
|
||||
assert.ok(out.includes("• [known] Checked node (n_b)"), "check-correct → known");
|
||||
assert.ok(out.includes("• [known] Checked node (n_b)"), "correct → known");
|
||||
assert.ok(out.includes("• [candidate] Solo node (n_a)"), "no events → candidate");
|
||||
});
|
||||
|
||||
@@ -244,7 +251,7 @@ describe("graph-context injection (chat-flow style)", () => {
|
||||
nodes: [node("n_ctx", "Chain rule intuition"), node("n_ctx2", "Derivative as a limit")],
|
||||
edges: [edge("n_ctx2", "n_ctx")],
|
||||
events: [
|
||||
{ nodeId: "n_ctx2", kind: "check-correct", chatId: "c1", at: "2026-08-22T00:00:00.000Z", ref: "r1" },
|
||||
{ nodeId: "n_ctx2", kind: "correct", chatId: "c1", at: "2026-08-22T00:00:00.000Z", ref: "r1" },
|
||||
] as LedgerEvent[],
|
||||
};
|
||||
const context = buildGraphContext(graph);
|
||||
@@ -296,7 +303,7 @@ describe("graph-context injection (chat-flow style)", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// ─── exact-match merge (merger identity resolution) ──────────────────────────
|
||||
// ─── cross-chat edge assertions (merger + read-time merging) ────────────────
|
||||
|
||||
/** An in-memory stand-in for the five ledgers (the GraphStore port, core/ports.ts). */
|
||||
function memoryLedgers() {
|
||||
@@ -317,128 +324,100 @@ function memoryLedgers() {
|
||||
appendCheckpoint: (c: SessionCheckpoint) => void checkpoints.push(c),
|
||||
readCheckpoints: () => checkpoints.map((c) => ({ ...c })),
|
||||
// Test accessors (not part of the GraphStore surface).
|
||||
counts: { get nodes() { return nodes.length; }, get mappings() { return mappings.length; } },
|
||||
} satisfies GraphStore & { counts: { nodes: number; mappings: number } };
|
||||
counts: { get nodes() { return nodes.length; } },
|
||||
} satisfies GraphStore & { counts: { nodes: number } };
|
||||
}
|
||||
|
||||
/** Findings as the extractor would file them for a plan tree: one
|
||||
* proposition per node (statement = title, treeNodeId set), one `requires`
|
||||
* edge per tree edge — the merge tests drive the merger with these. */
|
||||
function treeFindings(tree: ReturnType<typeof createTree>): SessionFindings {
|
||||
const idByNodeId = new Map(tree.nodes.map((n, i) => [n.id, `p${i + 1}`]));
|
||||
return {
|
||||
propositions: tree.nodes.map((n, i) => ({
|
||||
id: `p${i + 1}`,
|
||||
name: n.title,
|
||||
statement: n.title,
|
||||
aliases: [],
|
||||
kind: "truth" as const,
|
||||
treeNodeId: n.id,
|
||||
evidence: ["fixture evidence"],
|
||||
})),
|
||||
edges: tree.edges.map((e) => ({
|
||||
from: idByNodeId.get(e.from)!,
|
||||
to: idByNodeId.get(e.to)!,
|
||||
type: "requires" as const,
|
||||
evidence: ["fixture evidence"],
|
||||
})),
|
||||
attribution: { quiz: {} },
|
||||
};
|
||||
}
|
||||
|
||||
/** A real chat record with a stored tree (the merger reads the real chat store). */
|
||||
function fixtureChat(title: string, tree: ReturnType<typeof createTree>) {
|
||||
/** A minimal chat record with one user message (the merger reads the real chat store). */
|
||||
function fixtureChat(title: string) {
|
||||
const chat = chatStore.createChat(title);
|
||||
createdChats.push(chat.id);
|
||||
chatStore.saveTree(chat.id, tree);
|
||||
chatStore.saveMessages(chat.id, [{ id: "u1", role: "user", parts: [{ type: "text", text: "go" }] }]);
|
||||
return chatStore.getChat(chat.id)!;
|
||||
}
|
||||
|
||||
describe("exact-match merge (merger identity resolution)", () => {
|
||||
it("maps a case/whitespace-identical title onto the existing node instead of duplicating", () => {
|
||||
describe("cross-chat edge assertions (merger + read-time merging)", () => {
|
||||
it("a second session asserting the same edge appends a second line; reads merge with unioned provenance", () => {
|
||||
const ledgers = memoryLedgers();
|
||||
const tree1 = createTree([{ id: "cr", title: "Chain rule intuition" }], []);
|
||||
const chat1 = fixtureChat("Merge A", tree1);
|
||||
let stats = mergeFindings(chat1.id, treeFindings(tree1), { chatStore, graphStore: ledgers });
|
||||
assert.deepEqual(stats, { nodes: 1, edges: 0, events: 0 });
|
||||
const firstId = ledgers.readNodes()[0]!.id;
|
||||
|
||||
// Second session, same proposition: different casing + padding.
|
||||
const tree2 = createTree([{ id: "cr2", title: " chain Rule INTUITION " }], []);
|
||||
const chat2 = fixtureChat("Merge B", tree2);
|
||||
stats = mergeFindings(chat2.id, treeFindings(tree2), { chatStore, graphStore: ledgers });
|
||||
assert.deepEqual(stats, { nodes: 0, edges: 0, events: 0 }, "no duplicate node appended");
|
||||
|
||||
assert.equal(ledgers.counts.nodes, 1, "still exactly one graph node");
|
||||
assert.equal(ledgers.readNodes()[0]!.id, firstId);
|
||||
// Provenance stays honest: one mapping line per (chat, graph node).
|
||||
assert.equal(ledgers.counts.mappings, 2);
|
||||
assert.deepEqual(ledgers.readMappings()[1], { chatId: chat2.id, sourceId: "cr2", graphNodeId: firstId });
|
||||
});
|
||||
|
||||
it("matches aliases too (exact, case-insensitive)", () => {
|
||||
const ledgers = memoryLedgers();
|
||||
ledgers.appendNode({
|
||||
id: "n_alias",
|
||||
name: "Differentiation",
|
||||
statement: "Differentiation basics",
|
||||
aliases: ["the derivative"],
|
||||
kind: "truth",
|
||||
createdAt: "2026-08-22T00:00:00.000Z",
|
||||
});
|
||||
const tree = createTree([{ id: "d", title: "The Derivative" }], []);
|
||||
const chat = fixtureChat("Merge alias", tree);
|
||||
const stats = mergeFindings(chat.id, treeFindings(tree), { chatStore, graphStore: ledgers });
|
||||
assert.deepEqual(stats, { nodes: 0, edges: 0, events: 0 }, "alias match → no new node");
|
||||
assert.deepEqual(ledgers.readMappings(), [{ chatId: chat.id, sourceId: "d", graphNodeId: "n_alias" }]);
|
||||
});
|
||||
|
||||
it("does NOT fuzzy-match near-identical titles (that stays deferred)", () => {
|
||||
const ledgers = memoryLedgers();
|
||||
const tree1 = createTree([{ id: "cr", title: "Chain rule intuition" }], []);
|
||||
const chat1 = fixtureChat("Fuzzy A", tree1);
|
||||
mergeFindings(chat1.id, treeFindings(tree1), { chatStore, graphStore: ledgers });
|
||||
const tree2 = createTree([{ id: "cr2", title: "Chain rules intuition" }], []);
|
||||
const chat2 = fixtureChat("Fuzzy B", tree2);
|
||||
const stats = mergeFindings(chat2.id, treeFindings(tree2), { chatStore, graphStore: ledgers });
|
||||
assert.equal(stats.nodes, 1, "near-miss statements stay separate nodes");
|
||||
assert.equal(ledgers.counts.nodes, 2);
|
||||
});
|
||||
|
||||
it("merged nodes unify the second chat's edges onto the existing graph node", () => {
|
||||
const ledgers = memoryLedgers();
|
||||
const tree1 = createTree(
|
||||
[
|
||||
{ id: "base", title: "Derivative as a limit" },
|
||||
{ id: "chain", title: "Chain rule intuition" },
|
||||
// Session 1: two fresh propositions + a follows edge between them.
|
||||
const chat1 = fixtureChat("Edge A");
|
||||
const verdict1: AuditResult = {
|
||||
propositions: [
|
||||
{
|
||||
id: "p1",
|
||||
statement: "Derivative as a limit",
|
||||
description: "The derivative is the limit of difference quotients.",
|
||||
aliases: [],
|
||||
messages: ["u1"],
|
||||
evidence: ["e"],
|
||||
},
|
||||
{
|
||||
id: "p2",
|
||||
statement: "Chain rule intuition",
|
||||
description: "Composed functions differentiate by multiplying local rates.",
|
||||
aliases: [],
|
||||
messages: ["u1"],
|
||||
evidence: ["e"],
|
||||
},
|
||||
],
|
||||
[{ from: "base", to: "chain" }],
|
||||
);
|
||||
const chat1 = fixtureChat("Edge A", tree1);
|
||||
mergeFindings(chat1.id, treeFindings(tree1), { chatStore, graphStore: ledgers });
|
||||
edges: [{ from: "p1", to: "p2", type: "follows", messages: ["u1"], evidence: ["e"] }],
|
||||
attribution: { quiz: {} },
|
||||
dropped: [],
|
||||
};
|
||||
let stats = mergeFindings(chat1.id, verdict1, { chatStore, graphStore: ledgers });
|
||||
assert.deepEqual(stats, { nodes: 2, edges: 1, events: 0 });
|
||||
const base = ledgers.readNodes()[0]!.id;
|
||||
const chain = ledgers.readNodes()[1]!.id;
|
||||
|
||||
// Second session re-teaches the same two propositions (exact statements
|
||||
// modulo case) and asserts the same edge — it must dedupe onto the
|
||||
// existing nodes.
|
||||
const tree2 = createTree(
|
||||
[
|
||||
{ id: "base", title: "derivative as a limit" },
|
||||
{ id: "chain", title: "chain rule intuition" },
|
||||
// Session 2: the auditor links both propositions to the SAME nodes
|
||||
// (identity verdicts) and asserts the same relation.
|
||||
const chat2 = fixtureChat("Edge B");
|
||||
const verdict2: AuditResult = {
|
||||
propositions: [
|
||||
{
|
||||
id: "q1",
|
||||
statement: "The derivative as a limit of quotients", // different phrasing
|
||||
description: "…",
|
||||
aliases: [],
|
||||
messages: ["u1"],
|
||||
evidence: ["e"],
|
||||
linkTo: base,
|
||||
},
|
||||
{
|
||||
id: "q2",
|
||||
statement: "Chain rule, intuitively",
|
||||
description: "…",
|
||||
aliases: [],
|
||||
messages: ["u1"],
|
||||
evidence: ["e"],
|
||||
linkTo: chain,
|
||||
},
|
||||
],
|
||||
[{ from: "base", to: "chain" }],
|
||||
);
|
||||
const chat2 = fixtureChat("Edge B", tree2);
|
||||
const stats = mergeFindings(chat2.id, treeFindings(tree2), { chatStore, graphStore: ledgers });
|
||||
// A new ASSERTION line is appended (provenance is honest) — reads merge
|
||||
// by (from,to,type), so the derived graph still has one edge with both
|
||||
// sessions in its provenance.
|
||||
edges: [{ from: "q1", to: "q2", type: "follows", messages: ["u1"], evidence: ["e"] }],
|
||||
attribution: { quiz: {} },
|
||||
dropped: [],
|
||||
};
|
||||
stats = mergeFindings(chat2.id, verdict2, { chatStore, graphStore: ledgers });
|
||||
|
||||
// No new nodes; ONE new assertion line (per-session honest provenance).
|
||||
assert.deepEqual(stats, { nodes: 0, edges: 1, events: 0 }, "no new nodes, one re-assertion line");
|
||||
assert.equal(ledgers.counts.nodes, 2);
|
||||
assert.equal(ledgers.counts.nodes, 4, "2 defining lines + 2 link-appends for the linked propositions");
|
||||
const lines = ledgers.readEdges();
|
||||
assert.equal(lines.length, 2, "two assertion lines for the same edge");
|
||||
assert.equal(lines[0]!.from, lines[1]!.from);
|
||||
assert.equal(lines[0]!.to, lines[1]!.to);
|
||||
assert.deepEqual(lines.map((l) => l.provenance), [[chat1.id], [chat2.id]]);
|
||||
assert.deepEqual(lines.map((l) => l.provenance), [
|
||||
[{ chatId: chat1.id, messageId: "u1" }],
|
||||
[{ chatId: chat2.id, messageId: "u1" }],
|
||||
]);
|
||||
|
||||
// Read-time merging: one derived edge, both sessions in its provenance.
|
||||
const derived = getGraph(ledgers).edges;
|
||||
assert.equal(derived.length, 1);
|
||||
assert.deepEqual(derived[0]!.provenance, [
|
||||
{ chatId: chat1.id, messageId: "u1" },
|
||||
{ chatId: chat2.id, messageId: "u1" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,7 +9,10 @@ import { isNodeVisible } from "../components/graph-view.tsx";
|
||||
import type { NodeStateClass } from "../src/core/index.ts";
|
||||
|
||||
const none: ReadonlySet<NodeStateClass> = new Set();
|
||||
const node = { name: "Money multiplier", statement: "The money multiplier is the maximum ratio of broad money to base money." };
|
||||
const node = {
|
||||
statement: "The money multiplier is the maximum ratio of broad money to base money.",
|
||||
description: "How much broad money banks create from each unit of base money.",
|
||||
};
|
||||
|
||||
describe("isNodeVisible (legend/filters predicate)", () => {
|
||||
it("is visible with no filters", () => {
|
||||
@@ -26,11 +29,11 @@ describe("isNodeVisible (legend/filters predicate)", () => {
|
||||
assert.equal(isNodeVisible(node, "taught", hidden, ""), true);
|
||||
});
|
||||
|
||||
it("search matches the name case-insensitively", () => {
|
||||
it("search matches the statement case-insensitively", () => {
|
||||
assert.equal(isNodeVisible(node, "candidate", none, "MONEY multiplier"), true);
|
||||
});
|
||||
|
||||
it("search matches the statement when the name does not", () => {
|
||||
it("search matches the description when the statement does not", () => {
|
||||
assert.equal(isNodeVisible(node, "candidate", none, "maximum ratio"), true);
|
||||
assert.equal(isNodeVisible(node, "candidate", none, " velocity"), false);
|
||||
});
|
||||
|
||||
+220
-164
@@ -1,9 +1,9 @@
|
||||
/**
|
||||
* Graph merger + trigger + derivation tests (vision §1; contracts.ts
|
||||
* "extraction pipeline"). The merger (core/graph.ts mergeFindings) is the
|
||||
* pipeline's deterministic apply stage: findings in, ledger appends out —
|
||||
* identity resolution, retroactive probe attribution, event rebuild, and
|
||||
* the idempotency guarantees.
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
@@ -18,9 +18,8 @@ import assert from "node:assert/strict";
|
||||
import * as fs from "node:fs";
|
||||
import { after, describe, it } from "node:test";
|
||||
import type { UIMessage } from "ai";
|
||||
import type { PlanTree, PlanToolResult, SessionFindings } from "../src/core/contracts.ts";
|
||||
import type { AuditResult } from "../src/core/contracts.ts";
|
||||
import type { QuestionAnswerData } from "../src/core/question-core.ts";
|
||||
import { createTree, markDone } from "../src/core/plan-tree.ts";
|
||||
import { getGraph, graphSnapshot, mergeFindings, pendingQuizAnswers, type GraphDeps } from "../src/core/graph.ts";
|
||||
import { graphStore } from "../src/store/graph.ts";
|
||||
import { chatStore } from "../src/store/chats.ts";
|
||||
@@ -80,16 +79,6 @@ function quizPart(
|
||||
return { type: "tool-quiz", toolCallId, state: "output-available", input, output };
|
||||
}
|
||||
|
||||
function planPart(toolCallId: string, name: string, input: Record<string, unknown>, tree: PlanTree): unknown {
|
||||
return {
|
||||
type: `tool-${name}`,
|
||||
toolCallId,
|
||||
state: "output-available",
|
||||
input,
|
||||
output: { ok: true, tree } satisfies PlanToolResult,
|
||||
};
|
||||
}
|
||||
|
||||
function answer(over: Partial<QuestionAnswerData>): QuestionAnswerData {
|
||||
return {
|
||||
questionId: "q",
|
||||
@@ -102,59 +91,17 @@ function answer(over: Partial<QuestionAnswerData>): QuestionAnswerData {
|
||||
};
|
||||
}
|
||||
|
||||
/** Create a chat in the real store with a tree and messages; returns the record. */
|
||||
function fixtureChat(title: string, tree: PlanTree | null, messages: UIMessage[]) {
|
||||
/** 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[]) {
|
||||
const chat = chatStore.createChat(title);
|
||||
createdChats.push(chat.id);
|
||||
if (tree) chatStore.saveTree(chat.id, tree);
|
||||
chatStore.saveMessages(chat.id, messages);
|
||||
return chatStore.getChat(chat.id)!;
|
||||
}
|
||||
|
||||
/** The fixture tree: alpha → beta, gamma free-floating, alpha done. */
|
||||
function fixtureTree(): PlanTree {
|
||||
return markDone(
|
||||
createTree(
|
||||
[
|
||||
{ id: "alpha", title: "Alpha intuition" },
|
||||
{ id: "beta", title: "Beta rule" },
|
||||
{ id: "gamma", title: "Gamma application" },
|
||||
],
|
||||
[{ from: "alpha", to: "beta" }],
|
||||
),
|
||||
"alpha",
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Findings shaped exactly like what the extractor would produce for a
|
||||
* session's plan tree: one proposition per tree node (statement = title,
|
||||
* treeNodeId set), one `requires` edge per tree edge.
|
||||
*/
|
||||
function treeFindings(tree: PlanTree): SessionFindings {
|
||||
const idByNodeId = new Map(tree.nodes.map((n, i) => [n.id, `p${i + 1}`]));
|
||||
return {
|
||||
propositions: tree.nodes.map((n, i) => ({
|
||||
id: `p${i + 1}`,
|
||||
name: n.title,
|
||||
statement: n.title,
|
||||
aliases: [],
|
||||
kind: "truth" as const,
|
||||
treeNodeId: n.id,
|
||||
evidence: ["fixture evidence"],
|
||||
})),
|
||||
edges: tree.edges.map((e) => ({
|
||||
from: idByNodeId.get(e.from)!,
|
||||
to: idByNodeId.get(e.to)!,
|
||||
type: "requires" as const,
|
||||
evidence: ["fixture evidence"],
|
||||
})),
|
||||
attribution: { quiz: {} },
|
||||
};
|
||||
}
|
||||
|
||||
/** The full fixture transcript: probes → plan → checks → mark_done → noise. */
|
||||
function fixtureMessages(tree: PlanTree): UIMessage[] {
|
||||
/** The full fixture transcript: five graded quizzes + a dismissed one + an ask. */
|
||||
function fixtureMessages(): UIMessage[] {
|
||||
return [
|
||||
user("u1", "teach me the fixture topic"),
|
||||
assistant("a1", [
|
||||
@@ -171,32 +118,30 @@ function fixtureMessages(tree: PlanTree): UIMessage[] {
|
||||
answer({ questionId: "call-2", questionText: "Q2 text", dontKnow: true, correct: false, answeredAt: "2026-08-22T01:01:00.000Z" }),
|
||||
),
|
||||
]),
|
||||
assistant("a3", [planPart("p1", "plan_init", { nodes: [], edges: [] }, tree)]),
|
||||
assistant("a4", [
|
||||
assistant("a3", [
|
||||
// input.nodeId "alpha" is deliberately present and IGNORED —
|
||||
// attribution is the only quiz→node path in the tree-blind world.
|
||||
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" }),
|
||||
),
|
||||
]),
|
||||
assistant("a5", [
|
||||
assistant("a4", [
|
||||
quizPart(
|
||||
"call-4",
|
||||
{ question: "Q4", nodeId: "beta", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
||||
{ question: "Q4", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
||||
answer({ questionId: "call-4", questionText: "Q4 text", correct: false, answeredAt: "2026-08-22T02:01:00.000Z" }),
|
||||
),
|
||||
]),
|
||||
assistant("a6", [
|
||||
// nodeId present but NOT in the tree → tagged-but-unmapped: the
|
||||
// attribution can rescue it, or it lands null + question text.
|
||||
assistant("a5", [
|
||||
quizPart(
|
||||
"call-5",
|
||||
{ question: "Q5", nodeId: "ghost", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
||||
{ question: "Q5", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
||||
answer({ questionId: "call-5", questionText: "Q5 text", dontKnow: true, correct: false, answeredAt: "2026-08-22T02:02:00.000Z" }),
|
||||
),
|
||||
]),
|
||||
assistant("a7", [planPart("p2", "plan_mark_done", { id: "alpha" }, tree)]),
|
||||
assistant("a8", [
|
||||
assistant("a6", [
|
||||
// dismissed quiz + an answered ask — neither becomes an event
|
||||
quizPart(
|
||||
"call-6",
|
||||
@@ -214,25 +159,58 @@ function fixtureMessages(tree: PlanTree): UIMessage[] {
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The extractor's findings for the fixture chat: the tree as propositions +
|
||||
* the probe attribution. The audit stage is a no-op pass-through in these
|
||||
* merger tests — its behavior is tested with the pipeline (extraction.test).
|
||||
*/
|
||||
function fixtureFindings(): SessionFindings {
|
||||
const findings = treeFindings(fixtureTree());
|
||||
// Untagged probe quizzes: call-1 attributed retroactively to Alpha (the
|
||||
// vision's open question — resolved by the extractor); call-2 genuinely
|
||||
// unattributable; call-5's ghost nodeId is rescued onto Gamma.
|
||||
findings.attribution.quiz = { "call-1": "p1", "call-2": null, "call-5": "p3" };
|
||||
return findings;
|
||||
/** 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",
|
||||
statement: "Alpha intuition",
|
||||
description: "Alpha intuition, elaborated: what alpha means and how it behaves in practice.",
|
||||
aliases: ["alpha feel"],
|
||||
messages: ["a1", "a3"],
|
||||
evidence: ["fixture evidence"],
|
||||
understanding: "solid after the check",
|
||||
},
|
||||
{
|
||||
id: "p2",
|
||||
statement: "Beta rule",
|
||||
description: "The beta rule and when it applies.",
|
||||
aliases: [],
|
||||
messages: ["a4"],
|
||||
evidence: ["fixture evidence"],
|
||||
},
|
||||
{
|
||||
id: "p3",
|
||||
statement: "Gamma application",
|
||||
description: "Applying the whole framework to gamma cases.",
|
||||
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: [],
|
||||
};
|
||||
}
|
||||
|
||||
/** The chat every merger test folds (shared, so ordering is intentional). */
|
||||
const chat1 = fixtureChat("Graph fixture", fixtureTree(), fixtureMessages(fixtureTree()));
|
||||
const chat1 = fixtureChat("Graph fixture", fixtureMessages());
|
||||
|
||||
/** Set by the cross-session test; the snapshot test (which runs after it) reads it. */
|
||||
/** Set by the cross-session tests; the snapshot test (which runs after) reads them. */
|
||||
let chat2Id = "";
|
||||
let chat3Id = "";
|
||||
|
||||
// ─── the merger ──────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -248,71 +226,83 @@ function scopedGraph(chatIds: string[]) {
|
||||
);
|
||||
return {
|
||||
nodes: graph.nodes.filter((n) => nodeIds.has(n.id)),
|
||||
edges: graph.edges.filter((e) => e.provenance.some((c) => chatIds.includes(c))),
|
||||
edges: graph.edges.filter((e) => e.provenance.some((pt) => chatIds.includes(pt.chatId))),
|
||||
events: graph.events.filter((e) => chatIds.includes(e.chatId)),
|
||||
};
|
||||
}
|
||||
|
||||
describe("the merger (mergeFindings)", () => {
|
||||
it("merges propositions/edges by identity and rebuilds events with retroactive probe attribution", () => {
|
||||
const stats = mergeFindings(chat1.id, fixtureFindings(), graphDeps);
|
||||
it("creates nodes from the verdicts, rebuilds quiz events deterministically, records observations", () => {
|
||||
const stats = mergeFindings(chat1.id, fixtureAudit(), graphDeps);
|
||||
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.equal(node.kind, "truth");
|
||||
assert.deepEqual(node.aliases, []);
|
||||
assert.match(node.id, /^n_/);
|
||||
assert.ok(node.description.length > 0, "description is required");
|
||||
assert.equal("kind" in node, false, "no kind field");
|
||||
assert.ok(Array.isArray(node.provenance), "message-granular provenance");
|
||||
}
|
||||
// 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" },
|
||||
]);
|
||||
|
||||
// The tree edge alpha→beta becomes a `requires` edge on the mapped nodes.
|
||||
// The follows edge, provenance attached.
|
||||
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"));
|
||||
assert.equal(edge.type, "requires");
|
||||
assert.deepEqual(edge.provenance, [chat1.id]);
|
||||
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"],
|
||||
);
|
||||
|
||||
// Events — kinds, refs, notes, and the attribution paths.
|
||||
assert.equal(graph.events.length, 6);
|
||||
const byRef = new Map(graph.events.map((e) => [e.ref, e]));
|
||||
|
||||
const probeMiss = byRef.get("call-1")!;
|
||||
assert.equal(probeMiss.kind, "probe-miss"); // quiz BEFORE plan_init
|
||||
assert.equal(probeMiss.nodeId, idByStatement.get("Alpha intuition"), "retroactive probe attribution");
|
||||
assert.equal(probeMiss.question, undefined, "attributed → no question text needed");
|
||||
const miss = byRef.get("call-1")!;
|
||||
assert.equal(miss.kind, "miss");
|
||||
assert.equal(miss.nodeId, idByStatement.get("Alpha intuition"), "attributed quiz");
|
||||
|
||||
const probeDontKnow = byRef.get("call-2")!;
|
||||
assert.equal(probeDontKnow.kind, "dont-know"); // same kind, both phases
|
||||
assert.equal(probeDontKnow.nodeId, null, "unattributable probe → null");
|
||||
assert.equal(probeDontKnow.question, "Q2 text");
|
||||
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");
|
||||
|
||||
const check = byRef.get("call-3")!;
|
||||
assert.equal(check.kind, "check-correct"); // quiz AFTER plan_init
|
||||
assert.equal(check.nodeId, idByStatement.get("Alpha intuition"), "tagged quiz → deterministic");
|
||||
assert.equal(check.note, "recalled the diagram");
|
||||
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");
|
||||
|
||||
const checkMiss = byRef.get("call-4")!;
|
||||
assert.equal(checkMiss.kind, "check-incorrect");
|
||||
assert.equal(checkMiss.nodeId, idByStatement.get("Beta rule"));
|
||||
const miss2 = byRef.get("call-4")!;
|
||||
assert.equal(miss2.kind, "miss");
|
||||
assert.equal(miss2.nodeId, idByStatement.get("Beta rule"));
|
||||
|
||||
const ghost = byRef.get("call-5")!;
|
||||
assert.equal(ghost.kind, "dont-know");
|
||||
assert.equal(ghost.nodeId, idByStatement.get("Gamma application"), "tagged-but-unmapped rescued by attribution");
|
||||
assert.equal(ghost.question, undefined);
|
||||
const dontKnow2 = byRef.get("call-5")!;
|
||||
assert.equal(dontKnow2.kind, "dont-know");
|
||||
assert.equal(dontKnow2.nodeId, idByStatement.get("Gamma application"));
|
||||
|
||||
const taught = byRef.get("taught:alpha")!;
|
||||
assert.equal(taught.kind, "taught");
|
||||
assert.equal(taught.nodeId, idByStatement.get("Alpha intuition"), "mark_done → tree-node mapping");
|
||||
const observation = byRef.get("obs:p1")!;
|
||||
assert.equal(observation.kind, "observation");
|
||||
assert.equal(observation.nodeId, idByStatement.get("Alpha intuition"));
|
||||
assert.equal(observation.note, "solid after the check");
|
||||
|
||||
// The dismissed quiz and the ask question became nothing.
|
||||
assert.equal([...byRef.keys()].filter((r) => r === "call-6" || r === "call-7").length, 0);
|
||||
});
|
||||
|
||||
it("is idempotent — re-merging the same findings appends nothing", () => {
|
||||
const stats = mergeFindings(chat1.id, fixtureFindings(), graphDeps);
|
||||
it("is idempotent — re-merging the same verdicts appends nothing", () => {
|
||||
const stats = mergeFindings(chat1.id, fixtureAudit(), graphDeps);
|
||||
assert.deepEqual(stats, { nodes: 0, edges: 0, events: 0 });
|
||||
const graph = scopedGraph([chat1.id]);
|
||||
assert.equal(graph.nodes.length, 3);
|
||||
@@ -322,38 +312,63 @@ describe("the merger (mergeFindings)", () => {
|
||||
assert.equal(graphStore.readCheckpoints().filter((c) => c.chatId === chat1.id).length, 2);
|
||||
});
|
||||
|
||||
it("merges a second session onto the same nodes and accumulates edge provenance", () => {
|
||||
// chat2 re-teaches the same two propositions (identical statements) —
|
||||
// the conservative identity resolution recognizes them.
|
||||
const tree2 = createTree(
|
||||
[
|
||||
{ id: "a", title: "Alpha intuition" },
|
||||
{ id: "b", title: "Beta rule" },
|
||||
],
|
||||
[{ from: "a", to: "b" }],
|
||||
);
|
||||
const chat2 = fixtureChat("Graph fixture 2", tree2, [
|
||||
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", [
|
||||
user("u1", "again"),
|
||||
assistant("a1", [planPart("p1", "plan_init", { nodes: [], edges: [] }, tree2)]),
|
||||
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" }),
|
||||
),
|
||||
]),
|
||||
]);
|
||||
|
||||
const stats = mergeFindings(chat2.id, treeFindings(tree2), graphDeps);
|
||||
assert.deepEqual(stats, { nodes: 0, edges: 1, events: 0 });
|
||||
chat2Id = chat2.id;
|
||||
|
||||
// The auditor's identity verdict: this proposition IS the alpha node.
|
||||
const stats = mergeFindings(
|
||||
chat2.id,
|
||||
{
|
||||
propositions: [
|
||||
{
|
||||
id: "q1",
|
||||
statement: "Alpha feel", // a DIFFERENT phrasing — identity is the verdict, not strings
|
||||
description: "The intuitive grasp of alpha.",
|
||||
aliases: ["alpha intuition"],
|
||||
messages: ["a1"],
|
||||
evidence: ["fixture evidence"],
|
||||
linkTo: alphaId,
|
||||
},
|
||||
],
|
||||
edges: [],
|
||||
attribution: { quiz: { "call-9": "q1" } },
|
||||
dropped: [],
|
||||
},
|
||||
graphDeps,
|
||||
);
|
||||
assert.deepEqual(stats, { nodes: 0, edges: 0, events: 1 });
|
||||
|
||||
const after = scopedGraph([chat1.id, chat2.id]);
|
||||
assert.equal(after.nodes.length, 3); // no duplicate nodes
|
||||
assert.equal(after.edges.length, 1); // one merged edge…
|
||||
assert.deepEqual(after.edges[0]!.provenance, [chat1.id, chat2.id]); // …two sessions
|
||||
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 });
|
||||
});
|
||||
|
||||
it("derives node states, sessions, and node provenance in graphSnapshot", () => {
|
||||
it("derives node states and sessions in graphSnapshot", () => {
|
||||
const snap = graphSnapshot(graphDeps);
|
||||
const idByStatement = new Map(snap.nodes.map((n) => [n.statement, n.id]));
|
||||
assert.equal(snap.nodeStates[idByStatement.get("Alpha intuition")!], "taught");
|
||||
// Alpha's latest quiz is call-9 (chat2, correct) → known.
|
||||
assert.equal(snap.nodeStates[idByStatement.get("Alpha intuition")!], "known");
|
||||
assert.equal(snap.nodeStates[idByStatement.get("Beta rule")!], "missed");
|
||||
// Gamma: no taught/checks — but the rescued dont-know probe (call-5)
|
||||
// makes its latest event a dont-know.
|
||||
assert.equal(snap.nodeStates[idByStatement.get("Gamma application")!], "dont-know");
|
||||
|
||||
assert.equal(snap.sessions[chat1.id], "Graph fixture");
|
||||
@@ -366,8 +381,35 @@ describe("the merger (mergeFindings)", () => {
|
||||
assert.deepEqual(snap.nodeSessions[idByStatement.get("Gamma application")!], [chat1.id]);
|
||||
});
|
||||
|
||||
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",
|
||||
statement: "Alpha intuition", // IDENTICAL to the existing node's statement
|
||||
description: "Same words, no verdict.",
|
||||
aliases: [],
|
||||
messages: ["u1"],
|
||||
evidence: ["fixture evidence"],
|
||||
// no linkTo — identity is the auditor's call alone
|
||||
},
|
||||
],
|
||||
edges: [],
|
||||
attribution: { quiz: {} },
|
||||
dropped: [],
|
||||
},
|
||||
graphDeps,
|
||||
);
|
||||
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);
|
||||
});
|
||||
|
||||
it("returns zeros for unknown chats", () => {
|
||||
assert.deepEqual(mergeFindings("chat_does_not_exist", treeFindings(fixtureTree()), graphDeps), {
|
||||
assert.deepEqual(mergeFindings("chat_does_not_exist", fixtureAudit(), graphDeps), {
|
||||
nodes: 0,
|
||||
edges: 0,
|
||||
events: 0,
|
||||
@@ -379,14 +421,12 @@ describe("the merger (mergeFindings)", () => {
|
||||
|
||||
describe("the trigger helper (pendingQuizAnswers)", () => {
|
||||
it("counts answered quizzes not yet in the ledger — dismissed/ask never count", () => {
|
||||
const tree = createTree([{ id: "solo", title: "Solo node" }], []);
|
||||
const chat = fixtureChat("Trigger fixture", tree, [
|
||||
const chat = fixtureChat("Trigger fixture", [
|
||||
user("u1", "go"),
|
||||
assistant("a1", [planPart("p1", "plan_init", { nodes: [], edges: [] }, tree)]),
|
||||
assistant("a2", [
|
||||
assistant("a1", [
|
||||
quizPart(
|
||||
"call-s",
|
||||
{ question: "QS", nodeId: "solo", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
||||
{ question: "QS", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
||||
answer({ questionId: "call-s", questionText: "QS text", correct: true, answeredAt: "2026-08-22T03:00:00.000Z" }),
|
||||
),
|
||||
quizPart(
|
||||
@@ -407,7 +447,9 @@ describe("the trigger helper (pendingQuizAnswers)", () => {
|
||||
assert.equal(pendingQuizAnswers(chat.id, graphDeps), 1, "one answered quiz pending");
|
||||
assert.equal(pendingQuizAnswers("chat_does_not_exist", graphDeps), 0);
|
||||
|
||||
mergeFindings(chat.id, treeFindings(tree), graphDeps);
|
||||
mergeFindings(chat.id, fixtureAudit(), graphDeps);
|
||||
// call-s is unattributed here (fixtureAudit has no call-s attribution)
|
||||
// — but it still becomes an event, so nothing stays pending.
|
||||
assert.equal(pendingQuizAnswers(chat.id, graphDeps), 0, "extracted → nothing pending");
|
||||
|
||||
// A NEW answered quiz raises the count again (the re-trigger path).
|
||||
@@ -416,7 +458,7 @@ describe("the trigger helper (pendingQuizAnswers)", () => {
|
||||
assistant("a3", [
|
||||
quizPart(
|
||||
"call-s4",
|
||||
{ question: "QS4", nodeId: "solo", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
||||
{ question: "QS4", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
||||
answer({ questionId: "call-s4", questionText: "QS4 text", correct: false, answeredAt: "2026-08-22T03:01:00.000Z" }),
|
||||
),
|
||||
]),
|
||||
@@ -429,29 +471,43 @@ describe("the trigger helper (pendingQuizAnswers)", () => {
|
||||
|
||||
describe("graph ledger store", () => {
|
||||
it("round-trips lines and skips corrupt / foreign-shaped lines (strict reads)", () => {
|
||||
const node = { id: "n_storetest", name: "Store test", statement: "Store test", aliases: [], kind: "truth" as const, createdAt: "2026-08-22T00:00:00.000Z" };
|
||||
const node = {
|
||||
id: "n_storetest",
|
||||
statement: "Store test",
|
||||
description: "Store test, elaborated.",
|
||||
aliases: [] as string[],
|
||||
createdAt: "2026-08-22T00:00:00.000Z",
|
||||
provenance: [{ chatId: "chat_c", messageId: "m1" }],
|
||||
};
|
||||
graphStore.appendNode(node);
|
||||
// A torn append and a legacy V0-shaped node (different schema) — both skipped.
|
||||
// A torn append and a legacy pre-redesign node (old schema) — both skipped.
|
||||
fs.appendFileSync(
|
||||
LEDGER_FILES.nodes,
|
||||
`{not json}\n${JSON.stringify({ id: "legacy", statement: "Legacy shape", domain: "x", state: "known" })}\n`,
|
||||
`{not json}\n${JSON.stringify({ id: "legacy", statement: "Legacy shape", kind: "truth", name: "Legacy", createdAt: "2026-08-22T00:00:00.000Z" })}\n`,
|
||||
"utf-8",
|
||||
);
|
||||
const nodes = graphStore.readNodes().filter((n) => n.id === "n_storetest" || n.id === "legacy");
|
||||
assert.equal(nodes.length, 1);
|
||||
assert.equal(nodes[0]!.statement, "Store test");
|
||||
assert.equal(nodes[0]!.name, "Store test");
|
||||
assert.deepEqual(nodes[0]!.provenance, [{ chatId: "chat_c", messageId: "m1" }]);
|
||||
});
|
||||
|
||||
it("derives a name for pre-name ledger lines (legacy reads)", () => {
|
||||
// A full, valid node line written before `name` existed.
|
||||
fs.appendFileSync(
|
||||
LEDGER_FILES.nodes,
|
||||
`${JSON.stringify({ id: "n_prename", statement: "Derivatives measure instantaneous change; they are limits.", aliases: [], kind: "truth", createdAt: "2026-08-22T00:00:00.000Z" })}\n`,
|
||||
"utf-8",
|
||||
);
|
||||
const node = graphStore.readNodes().find((n) => n.id === "n_prename");
|
||||
assert.ok(node);
|
||||
assert.equal(node.name, "Derivatives measure instantaneous change");
|
||||
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",
|
||||
statement: `Phrasing ${msgId}`,
|
||||
description: "Elaboration.",
|
||||
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"]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user