Extraction pipeline: extractor → auditor → merger — the sole graph writer (stage 5)
The knowledge graph's write channel is now the pivot's target
architecture (vision §1 construction):
- src/agent/subagents/extraction.ts — the pipeline factory
(createExtractionPipeline({ chatStore, graphStore, extractor?,
auditor? }); models env LH_EXTRACT_MODEL ?? LH_MODEL):
* transcriptView(): the compact view both LLM stages see — final plan
tree with node ids, conversation verbatim, QUIZ lines with outcomes,
mark_done lines; job tool parts are one-liners, plan ops fold into
the tree line.
* Extractor: transcript → SessionFindings (propositions incl.
misconceptions and prose-only supports edges, attribution for
UNTAGGED probes, all with verbatim evidence) via one zod-validated
submit_findings call.
* Auditor: transcript + findings → the verified subset + drops (the
faithfulness net) via submit_review.
- src/core/graph.ts — emitChat/foldStaleChats/replayFinalTree retire;
mergeFindings() replaces them as the pipeline's deterministic apply
stage: exact/alias identity resolution (one mapping line per (chat,
graph node)), edge dedupe, and event rebuild FROM THE TRANSCRIPT's tool
parts — kinds/timestamps/refs/notes deterministic, the LLM's only event
say being WHICH proposition an untagged probe tested (retroactive —
the vision's open question #3, resolved structurally: tagged events
resolve deterministically; attribution rescues untagged and
tagged-but-unmapped ones; unattributable land null + question text).
pendingQuizAnswers() is the trigger helper.
- Trigger (turn-service.ts triggerExtraction, replacing the old
foldTurnIntoGraph body): once a chat accumulates
EXTRACTION.everyNQuizAnswers (LH_EXTRACT_N, default 3) new answered
quizzes, fire the pipeline async — single-flight per chat, failure
checkpoints nothing and re-runs on the next trigger. Deliberately a v1
placeholder isolated in ONE seam, trigger-agnostic pipeline.
- GET /api/graph is now a pure read (foldStaleChats gone).
- NodeMapping.treeNodeId → sourceId (the mapping key is now the writer's
local proposition id — tree node when tree-derived); store strict-read
schema updated. Local ledger data regenerates through the pipeline.
- Tests: graph.test.ts rewritten around mergeFindings (identity,
attribution paths incl. ghost-rescue, idempotency, cross-session
provenance, trigger counting), graph-context.test.ts merge section
converted to findings-driven, NEW extraction.test.ts — transcriptView
units + the full pipeline with mock models (audit drops, failure
checkpoints nothing, single-flight). 195/195, tsc/eslint/build clean.
Probe attribution (vision open question): probes attach retroactively by
the extractor against the whole transcript; a pre-tree probe maps to a
proposition the tree only articulates later. Fuzzy/embedding merge
adjudication stays deferred (conservative exact/alias only).
This commit is contained in:
@@ -0,0 +1,373 @@
|
||||
/**
|
||||
* The extraction pipeline tests (vision §1 construction): extractor →
|
||||
* auditor → merger, the sole graph writer.
|
||||
*
|
||||
* transcriptView — the compact view both LLM stages see (pure)
|
||||
* pipeline — MockLanguageModelV4 drives both agents: findings out,
|
||||
* audit verdict in, deterministic merge applied; failure
|
||||
* paths checkpoint nothing; single-flight per chat
|
||||
*
|
||||
* The merger itself (identity resolution, 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.
|
||||
*
|
||||
* Ledger: in-memory (the GraphStore port) — this suite never touches
|
||||
* data/graph. Chats: real ChatRecords in the real chat store (created and
|
||||
* deleted by this suite).
|
||||
*/
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
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 { 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";
|
||||
|
||||
// ─── fixtures ────────────────────────────────────────────────────────────────
|
||||
|
||||
const createdChats: string[] = [];
|
||||
|
||||
function usage() {
|
||||
return {
|
||||
inputTokens: { total: 10, noCache: 10, cacheRead: 0, cacheWrite: 0 },
|
||||
outputTokens: { total: 5, text: 5, reasoning: 0 },
|
||||
};
|
||||
}
|
||||
|
||||
/** An in-memory GraphStore (core/ports.ts) — this suite never touches data/graph. */
|
||||
function memoryLedgers() {
|
||||
const nodes: GraphNode[] = [];
|
||||
const edges: GraphEdge[] = [];
|
||||
const events: LedgerEvent[] = [];
|
||||
const mappings: NodeMapping[] = [];
|
||||
const checkpoints: SessionCheckpoint[] = [];
|
||||
return {
|
||||
appendNode: (n: GraphNode) => void nodes.push(n),
|
||||
readNodes: () => nodes.map((n) => ({ ...n })),
|
||||
appendEdge: (e: GraphEdge) => void edges.push(e),
|
||||
readEdges: () => edges.map((e) => ({ ...e })),
|
||||
appendEvent: (e: LedgerEvent) => void events.push(e),
|
||||
readEvents: () => events.map((e) => ({ ...e })),
|
||||
appendMapping: (m: NodeMapping) => void mappings.push(m),
|
||||
readMappings: () => mappings.map((m) => ({ ...m })),
|
||||
appendCheckpoint: (c: SessionCheckpoint) => void checkpoints.push(c),
|
||||
readCheckpoints: () => checkpoints.map((c) => ({ ...c })),
|
||||
} satisfies GraphStore;
|
||||
}
|
||||
|
||||
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: Record<string, unknown>): unknown {
|
||||
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 {
|
||||
return {
|
||||
type: `tool-${name}`,
|
||||
toolCallId,
|
||||
state: "output-available",
|
||||
input,
|
||||
output: { ok: true, tree },
|
||||
};
|
||||
}
|
||||
|
||||
/** A session-shaped fixture chat: probe → plan → check → mark_done. */
|
||||
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", [
|
||||
quizPart("probe-1", { question: "What is a derivative, roughly?" }, {
|
||||
questionId: "probe-1", kind: "quiz", questionText: "What is a derivative, roughly?",
|
||||
correct: false, dontKnow: false, answeredAt: "2026-08-22T01:00:00.000Z",
|
||||
}),
|
||||
]),
|
||||
assistant("a2", [
|
||||
{ type: "text", text: "The derivative measures how fast a function changes." },
|
||||
planPart("p1", "plan_init", { nodes: [], edges: [] }, tree),
|
||||
]),
|
||||
assistant("a3", [
|
||||
quizPart("check-1", { question: "Check", nodeId: "apply" }, {
|
||||
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)! };
|
||||
}
|
||||
|
||||
after(() => {
|
||||
for (const id of createdChats) {
|
||||
try {
|
||||
fs.unlinkSync(`${PATHS.chats}/${id}.json`);
|
||||
} catch {
|
||||
/* already gone */
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
/** What the extractor would produce for the fixture session. */
|
||||
const FINDINGS: SessionFindings = {
|
||||
propositions: [
|
||||
{
|
||||
id: "p1",
|
||||
statement: "Chain rule intuition",
|
||||
aliases: [],
|
||||
kind: "truth",
|
||||
treeNodeId: "cr",
|
||||
evidence: ["TEACHER: The derivative measures how fast a function changes."],
|
||||
},
|
||||
{
|
||||
id: "p2",
|
||||
statement: "Applying the chain rule",
|
||||
aliases: [],
|
||||
kind: "truth",
|
||||
treeNodeId: "apply",
|
||||
evidence: ["FINAL PLAN TREE (node ids): cr = Chain rule intuition; apply = Applying the chain rule"],
|
||||
},
|
||||
],
|
||||
edges: [{ from: "p1", to: "p2", type: "requires", evidence: ["TREE EDGES: cr requires apply"] }],
|
||||
attribution: { quiz: { "probe-1": "p1" } },
|
||||
};
|
||||
|
||||
/** A model whose ONE tool call is `name` with the given input; then text. */
|
||||
function submitModel(name: string, input: unknown): MockLanguageModelV4 {
|
||||
let turn = 0;
|
||||
return new MockLanguageModelV4({
|
||||
doGenerate: async () => {
|
||||
turn++;
|
||||
if (turn === 1) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "tool-call" as const,
|
||||
toolCallId: `${name}-call`,
|
||||
toolName: name,
|
||||
input: JSON.stringify(input),
|
||||
},
|
||||
],
|
||||
finishReason: { unified: "tool-calls" as const, raw: "tool-calls" },
|
||||
usage: usage(),
|
||||
warnings: [],
|
||||
};
|
||||
}
|
||||
return {
|
||||
content: [{ type: "text" as const, text: "submitted" }],
|
||||
finishReason: { unified: "stop" as const, raw: "stop" },
|
||||
usage: usage(),
|
||||
warnings: [],
|
||||
};
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** A model that never calls a tool (the failure path). */
|
||||
function silentModel(): MockLanguageModelV4 {
|
||||
return new MockLanguageModelV4({
|
||||
doGenerate: async () => ({
|
||||
content: [{ type: "text" as const, text: "I refuse to use tools" }],
|
||||
finishReason: { unified: "stop" as const, raw: "stop" },
|
||||
usage: usage(),
|
||||
warnings: [],
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
// ─── transcriptView (pure) ───────────────────────────────────────────────────
|
||||
|
||||
describe("transcriptView (pure)", () => {
|
||||
it("shows the final tree, verbatim conversation, structured events — 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\./);
|
||||
|
||||
// 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/);
|
||||
assert.doesNotMatch(view, /output-available/);
|
||||
});
|
||||
|
||||
it("summarizes job tool results as one line and drops empty text", () => {
|
||||
const chat = chatStore.createChat("View noise fixture");
|
||||
createdChats.push(chat.id);
|
||||
chatStore.saveMessages(chat.id, [
|
||||
user("u1", "go"),
|
||||
assistant("a1", [
|
||||
{
|
||||
type: "tool-research",
|
||||
toolCallId: "r1",
|
||||
state: "output-available",
|
||||
input: { question: "q" },
|
||||
output: { brief: "long brief", sources: [] },
|
||||
},
|
||||
{ type: "text", text: " " },
|
||||
]),
|
||||
]);
|
||||
const view = transcriptView(chatStore.getChat(chat.id)!);
|
||||
assert.match(view, /\(research tool result delivered\)/);
|
||||
assert.doesNotMatch(view, /long brief/);
|
||||
assert.doesNotMatch(view, /TEACHER: {2}/);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── the pipeline ───────────────────────────────────────────────────────────
|
||||
|
||||
describe("the extraction pipeline (extractor → auditor → merger)", () => {
|
||||
it("runs end to end: findings → audit verdict → 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]!],
|
||||
edges: [],
|
||||
attribution: { quiz: { "probe-1": "p1" } },
|
||||
};
|
||||
|
||||
const pipeline = createExtractionPipeline({
|
||||
chatStore,
|
||||
graphStore: ledgers,
|
||||
extractor: submitModel("submit_findings", FINDINGS),
|
||||
auditor: submitModel("submit_review", { ...verified, dropped: [{ ref: "p2", reason: "evidence not found" }] }),
|
||||
});
|
||||
const stats = await pipeline.runExtraction(chatId);
|
||||
|
||||
assert.deepEqual(stats, { nodes: 1, 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 }]);
|
||||
|
||||
// Events rebuilt deterministically from the transcript — the probe
|
||||
// retroactively attributed, the check tagged, mark_done mapped.
|
||||
const events = ledgers.readEvents();
|
||||
assert.deepEqual(
|
||||
events.map((e) => [e.kind, e.ref]),
|
||||
[
|
||||
["probe-miss", "probe-1"],
|
||||
["check-correct", "check-1"],
|
||||
["taught", "taught:cr"],
|
||||
],
|
||||
);
|
||||
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");
|
||||
|
||||
// Checkpointed through the record's updatedAt — never the same turns twice.
|
||||
assert.equal(ledgers.readCheckpoints().length, 1);
|
||||
});
|
||||
|
||||
it("checkpoints nothing when a stage fails", async () => {
|
||||
const { chatId } = fixtureChat();
|
||||
const ledgers = memoryLedgers();
|
||||
|
||||
const pipeline = createExtractionPipeline({
|
||||
chatStore,
|
||||
graphStore: ledgers,
|
||||
extractor: submitModel("submit_findings", FINDINGS),
|
||||
auditor: silentModel(), // never submits the review
|
||||
});
|
||||
await assert.rejects(pipeline.runExtraction(chatId), /the auditor never submitted a review/);
|
||||
assert.equal(ledgers.readNodes().length, 0);
|
||||
assert.equal(ledgers.readCheckpoints().length, 0, "no checkpoint — the next trigger re-runs");
|
||||
|
||||
const failingExtractor = createExtractionPipeline({
|
||||
chatStore,
|
||||
graphStore: ledgers,
|
||||
extractor: silentModel(),
|
||||
auditor: submitModel("submit_review", FINDINGS),
|
||||
});
|
||||
await assert.rejects(failingExtractor.runExtraction(chatId), /the extractor never submitted findings/);
|
||||
assert.equal(ledgers.readCheckpoints().length, 0);
|
||||
});
|
||||
|
||||
it("is single-flight per chat — a concurrent second run is a no-op", async () => {
|
||||
const { chatId } = fixtureChat();
|
||||
const ledgers = memoryLedgers();
|
||||
|
||||
// The extractor's first call hangs until we release it.
|
||||
let release!: () => void;
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
let calls = 0;
|
||||
const gatedExtractor = new MockLanguageModelV4({
|
||||
doGenerate: async () => {
|
||||
calls++;
|
||||
if (calls === 1) await gate;
|
||||
return {
|
||||
content: [{ type: "text" as const, text: "…" }],
|
||||
finishReason: { unified: "stop" as const, raw: "stop" },
|
||||
usage: usage(),
|
||||
warnings: [],
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
const pipeline = createExtractionPipeline({
|
||||
chatStore,
|
||||
graphStore: ledgers,
|
||||
extractor: gatedExtractor,
|
||||
auditor: silentModel(),
|
||||
});
|
||||
|
||||
const first = pipeline.runExtraction(chatId);
|
||||
const second = await pipeline.runExtraction(chatId); // in-flight → zeros
|
||||
assert.deepEqual(second, { nodes: 0, edges: 0, events: 0 });
|
||||
|
||||
release();
|
||||
await assert.rejects(first, /the extractor never submitted findings/);
|
||||
});
|
||||
|
||||
it("throws for unknown chats", async () => {
|
||||
const ledgers = memoryLedgers();
|
||||
const pipeline = createExtractionPipeline({
|
||||
chatStore,
|
||||
graphStore: ledgers,
|
||||
extractor: silentModel(),
|
||||
auditor: silentModel(),
|
||||
});
|
||||
await assert.rejects(pipeline.runExtraction("chat_does_not_exist"), /Chat not found/);
|
||||
});
|
||||
});
|
||||
+71
-47
@@ -5,10 +5,11 @@
|
||||
* 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 — emitChat dedupes case/whitespace-identical titles
|
||||
* onto existing nodes (no fuzzy matching)
|
||||
* exact-match merge — the merger (mergeFindings) dedupes
|
||||
* case/whitespace-identical statements onto existing
|
||||
* nodes (no fuzzy matching)
|
||||
*
|
||||
* Ledger isolation: NONE needed for the merge tests — emitChat's ledger is
|
||||
* 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
|
||||
* here (tests/graph.test.ts owns the backup-and-clear dance; both suites run
|
||||
* concurrently as separate processes). Chat fixtures are real ChatRecords,
|
||||
@@ -21,9 +22,9 @@ 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 } from "../src/core/contracts.ts";
|
||||
import type { GraphEdge, GraphNode, LedgerEvent, SessionFindings } from "../src/core/contracts.ts";
|
||||
import type { NodeStateClass } from "../src/core/contracts.ts";
|
||||
import { emitChat, getGraph } from "../src/core/graph.ts";
|
||||
import { getGraph, mergeFindings } from "../src/core/graph.ts";
|
||||
import type { GraphStore } from "../src/core/ports.ts";
|
||||
import {
|
||||
buildGraphContext,
|
||||
@@ -295,7 +296,7 @@ describe("graph-context injection (chat-flow style)", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// ─── exact-match merge (emitChat dedup) ─────────────────────────────────────
|
||||
// ─── exact-match merge (merger identity resolution) ──────────────────────────
|
||||
|
||||
/** An in-memory stand-in for the five ledgers (the GraphStore port, core/ports.ts). */
|
||||
function memoryLedgers() {
|
||||
@@ -320,7 +321,31 @@ function memoryLedgers() {
|
||||
} satisfies GraphStore & { counts: { nodes: number; mappings: number } };
|
||||
}
|
||||
|
||||
/** A real chat record with a stored tree (emitChat reads the real chat store). */
|
||||
/** 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}`,
|
||||
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>) {
|
||||
const chat = chatStore.createChat(title);
|
||||
createdChats.push(chat.id);
|
||||
@@ -329,24 +354,26 @@ function fixtureChat(title: string, tree: ReturnType<typeof createTree>) {
|
||||
return chatStore.getChat(chat.id)!;
|
||||
}
|
||||
|
||||
describe("exact-match merge (emitChat)", () => {
|
||||
describe("exact-match merge (merger identity resolution)", () => {
|
||||
it("maps a case/whitespace-identical title onto the existing node instead of duplicating", () => {
|
||||
const ledgers = memoryLedgers();
|
||||
const chat1 = fixtureChat("Merge A", createTree([{ id: "cr", title: "Chain rule intuition" }], []));
|
||||
let stats = emitChat(chat1.id, { chatStore, graphStore: ledgers });
|
||||
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 chat2 = fixtureChat("Merge B", createTree([{ id: "cr2", title: " chain Rule INTUITION " }], []));
|
||||
stats = emitChat(chat2.id, { chatStore, graphStore: ledgers });
|
||||
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, treeNodeId).
|
||||
// Provenance stays honest: one mapping line per (chat, graph node).
|
||||
assert.equal(ledgers.counts.mappings, 2);
|
||||
assert.deepEqual(ledgers.readMappings()[1], { chatId: chat2.id, treeNodeId: "cr2", graphNodeId: firstId });
|
||||
assert.deepEqual(ledgers.readMappings()[1], { chatId: chat2.id, sourceId: "cr2", graphNodeId: firstId });
|
||||
});
|
||||
|
||||
it("matches aliases too (exact, case-insensitive)", () => {
|
||||
@@ -358,52 +385,49 @@ describe("exact-match merge (emitChat)", () => {
|
||||
kind: "truth",
|
||||
createdAt: "2026-08-22T00:00:00.000Z",
|
||||
});
|
||||
const chat = fixtureChat("Merge alias", createTree([{ id: "d", title: "The Derivative" }], []));
|
||||
const stats = emitChat(chat.id, { chatStore, graphStore: ledgers });
|
||||
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, treeNodeId: "d", graphNodeId: "n_alias" }]);
|
||||
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 chat1 = fixtureChat("Fuzzy A", createTree([{ id: "cr", title: "Chain rule intuition" }], []));
|
||||
emitChat(chat1.id, { chatStore, graphStore: ledgers });
|
||||
const chat2 = fixtureChat(
|
||||
"Fuzzy B",
|
||||
createTree([{ id: "cr2", title: "Chain rules intuition" }], []),
|
||||
);
|
||||
const stats = emitChat(chat2.id, { chatStore, graphStore: ledgers });
|
||||
assert.equal(stats.nodes, 1, "near-miss titles stay separate nodes");
|
||||
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 chat1 = fixtureChat(
|
||||
"Edge A",
|
||||
createTree(
|
||||
[
|
||||
{ id: "base", title: "Derivative as a limit" },
|
||||
{ id: "chain", title: "Chain rule intuition" },
|
||||
],
|
||||
[{ from: "base", to: "chain" }],
|
||||
),
|
||||
const tree1 = createTree(
|
||||
[
|
||||
{ id: "base", title: "Derivative as a limit" },
|
||||
{ id: "chain", title: "Chain rule intuition" },
|
||||
],
|
||||
[{ from: "base", to: "chain" }],
|
||||
);
|
||||
emitChat(chat1.id, { chatStore, graphStore: ledgers });
|
||||
const chat1 = fixtureChat("Edge A", tree1);
|
||||
mergeFindings(chat1.id, treeFindings(tree1), { chatStore, graphStore: ledgers });
|
||||
|
||||
// Second session re-teaches the same two propositions (exact titles)
|
||||
// and asserts the same edge — it must dedupe onto the existing nodes.
|
||||
const chat2 = fixtureChat(
|
||||
"Edge B",
|
||||
createTree(
|
||||
[
|
||||
{ id: "base", title: "derivative as a limit" },
|
||||
{ id: "chain", title: "chain rule intuition" },
|
||||
],
|
||||
[{ from: "base", to: "chain" }],
|
||||
),
|
||||
// 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" },
|
||||
],
|
||||
[{ from: "base", to: "chain" }],
|
||||
);
|
||||
const stats = emitChat(chat2.id, { chatStore, graphStore: ledgers });
|
||||
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.
|
||||
|
||||
+125
-87
@@ -1,21 +1,27 @@
|
||||
/**
|
||||
* Graph store + emission tests (W4; vision §1, contracts.ts "v1 emission").
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
* deleted by this suite) — emission reads exactly what production reads.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
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 } from "../src/core/contracts.ts";
|
||||
import type { PlanTree, PlanToolResult, SessionFindings } from "../src/core/contracts.ts";
|
||||
import type { QuestionAnswerData } from "../src/core/question-core.ts";
|
||||
import { createTree, markDone, removeNode } from "../src/core/plan-tree.ts";
|
||||
import { emitChat, foldStaleChats, getGraph, graphSnapshot, type GraphDeps } from "../src/core/graph.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";
|
||||
import { LEDGER_FILES, PATHS } from "../config.ts";
|
||||
@@ -32,7 +38,7 @@ const createdChats: string[] = [];
|
||||
fs.mkdirSync(PATHS.graph, { recursive: true });
|
||||
for (const file of LEDGER_PATHS) {
|
||||
if (fs.existsSync(file)) {
|
||||
const bak = `${file}.w4test.bak`;
|
||||
const bak = `${file}.w5test.bak`;
|
||||
fs.renameSync(file, bak);
|
||||
backups.push({ file, bak });
|
||||
}
|
||||
@@ -120,6 +126,32 @@ function fixtureTree(): PlanTree {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}`,
|
||||
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[] {
|
||||
return [
|
||||
@@ -154,7 +186,8 @@ function fixtureMessages(tree: PlanTree): UIMessage[] {
|
||||
),
|
||||
]),
|
||||
assistant("a6", [
|
||||
// nodeId present but NOT in the tree → unmapped → null + question text
|
||||
// nodeId present but NOT in the tree → tagged-but-unmapped: the
|
||||
// attribution can rescue it, or it lands null + question text.
|
||||
quizPart(
|
||||
"call-5",
|
||||
{ question: "Q5", nodeId: "ghost", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
||||
@@ -163,7 +196,7 @@ function fixtureMessages(tree: PlanTree): UIMessage[] {
|
||||
]),
|
||||
assistant("a7", [planPart("p2", "plan_mark_done", { id: "alpha" }, tree)]),
|
||||
assistant("a8", [
|
||||
// dismissed quiz + an answered ask — neither emits an event
|
||||
// dismissed quiz + an answered ask — neither becomes an event
|
||||
quizPart(
|
||||
"call-6",
|
||||
{ question: "Q6", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
||||
@@ -180,13 +213,27 @@ function fixtureMessages(tree: PlanTree): UIMessage[] {
|
||||
];
|
||||
}
|
||||
|
||||
/** The chat every emission test folds (shared, so ordering is intentional). */
|
||||
/**
|
||||
* 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 chat every merger test folds (shared, so ordering is intentional). */
|
||||
const chat1 = fixtureChat("Graph fixture", fixtureTree(), fixtureMessages(fixtureTree()));
|
||||
|
||||
/** Set by the provenance test; the snapshot test (which runs after it) reads it. */
|
||||
/** Set by the cross-session test; the snapshot test (which runs after it) reads it. */
|
||||
let chat2Id = "";
|
||||
|
||||
// ─── emission ────────────────────────────────────────────────────────────────
|
||||
// ─── the merger ──────────────────────────────────────────────────────────────
|
||||
|
||||
/** Everything the graph holds for a set of chats (other processes' test
|
||||
* emissions run concurrently against the same files — scope, never assume). */
|
||||
@@ -205,9 +252,9 @@ function scopedGraph(chatIds: string[]) {
|
||||
};
|
||||
}
|
||||
|
||||
describe("graph emission (emitChat)", () => {
|
||||
it("folds tree nodes/edges and quiz/mark_done events with the probe/check boundary", () => {
|
||||
const stats = emitChat(chat1.id, graphDeps);
|
||||
describe("the merger (mergeFindings)", () => {
|
||||
it("merges propositions/edges by identity and rebuilds events with retroactive probe attribution", () => {
|
||||
const stats = mergeFindings(chat1.id, fixtureFindings(), graphDeps);
|
||||
assert.deepEqual(stats, { nodes: 3, edges: 1, events: 6 });
|
||||
|
||||
const graph = scopedGraph([chat1.id]);
|
||||
@@ -219,7 +266,7 @@ describe("graph emission (emitChat)", () => {
|
||||
assert.match(node.id, /^n_/);
|
||||
}
|
||||
|
||||
// Tree edge alpha→beta becomes a `requires` edge on the mapped nodes.
|
||||
// The tree edge alpha→beta becomes a `requires` edge on the mapped nodes.
|
||||
assert.equal(graph.edges.length, 1);
|
||||
const edge = graph.edges[0]!;
|
||||
assert.equal(edge.from, idByStatement.get("Alpha intuition"));
|
||||
@@ -227,77 +274,69 @@ describe("graph emission (emitChat)", () => {
|
||||
assert.equal(edge.type, "requires");
|
||||
assert.deepEqual(edge.provenance, [chat1.id]);
|
||||
|
||||
// Events — kinds, refs, node attribution, notes, question fallback.
|
||||
// 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, null);
|
||||
assert.equal(probeMiss.question, "Q1 text");
|
||||
assert.equal(probeMiss.chatId, chat1.id);
|
||||
assert.equal(probeMiss.nodeId, idByStatement.get("Alpha intuition"), "retroactive probe attribution");
|
||||
assert.equal(probeMiss.question, undefined, "attributed → no question text needed");
|
||||
|
||||
const probeDontKnow = byRef.get("call-2")!;
|
||||
assert.equal(probeDontKnow.kind, "dont-know"); // same kind, both phases
|
||||
assert.equal(probeDontKnow.nodeId, null);
|
||||
assert.equal(probeDontKnow.nodeId, null, "unattributable probe → null");
|
||||
assert.equal(probeDontKnow.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"));
|
||||
assert.equal(check.nodeId, idByStatement.get("Alpha intuition"), "tagged quiz → deterministic");
|
||||
assert.equal(check.note, "recalled the diagram");
|
||||
assert.equal(check.question, undefined); // resolved nodeId → no question text
|
||||
|
||||
const checkMiss = byRef.get("call-4")!;
|
||||
assert.equal(checkMiss.kind, "check-incorrect");
|
||||
assert.equal(checkMiss.nodeId, idByStatement.get("Beta rule"));
|
||||
|
||||
const unmapped = byRef.get("call-5")!;
|
||||
assert.equal(unmapped.kind, "dont-know");
|
||||
assert.equal(unmapped.nodeId, null); // nodeId present but unmapped
|
||||
assert.equal(unmapped.question, "Q5 text");
|
||||
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 taught = byRef.get("taught:alpha")!;
|
||||
assert.equal(taught.kind, "taught");
|
||||
assert.equal(taught.nodeId, idByStatement.get("Alpha intuition"));
|
||||
assert.equal(taught.nodeId, idByStatement.get("Alpha intuition"), "mark_done → tree-node mapping");
|
||||
|
||||
// The dismissed quiz and the ask question emitted nothing.
|
||||
// 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-emission appends nothing", () => {
|
||||
const stats = emitChat(chat1.id, graphDeps);
|
||||
it("is idempotent — re-merging the same findings appends nothing", () => {
|
||||
const stats = mergeFindings(chat1.id, fixtureFindings(), graphDeps);
|
||||
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);
|
||||
// exactly one checkpoint line per fold, though — checkpoints are a log
|
||||
// exactly one checkpoint line per merge, though — checkpoints are a log
|
||||
assert.equal(graphStore.readCheckpoints().filter((c) => c.chatId === chat1.id).length, 2);
|
||||
});
|
||||
|
||||
it("accumulates edge provenance when another chat maps onto the same nodes", () => {
|
||||
// Simulates what a future consolidation merge produces: chat2's tree
|
||||
// slugs map onto chat1's graph nodes, so its edge assertion dedupes
|
||||
// by (from,to,type) and just adds chat2 to the provenance.
|
||||
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: "alpha", title: "Alpha (second session)" },
|
||||
{ id: "beta", title: "Beta (second session)" },
|
||||
{ id: "a", title: "Alpha intuition" },
|
||||
{ id: "b", title: "Beta rule" },
|
||||
],
|
||||
[{ from: "alpha", to: "beta" }],
|
||||
[{ from: "a", to: "b" }],
|
||||
);
|
||||
const chat2 = fixtureChat("Graph fixture 2", tree2, [
|
||||
user("u1", "again"),
|
||||
assistant("a1", [planPart("p1", "plan_init", { nodes: [], edges: [] }, tree2)]),
|
||||
]);
|
||||
|
||||
const before = getGraph(graphStore);
|
||||
const gAlpha = before.nodes.find((n) => n.statement === "Alpha intuition")!.id;
|
||||
const gBeta = before.nodes.find((n) => n.statement === "Beta rule")!.id;
|
||||
graphStore.appendMapping({ chatId: chat2.id, treeNodeId: "alpha", graphNodeId: gAlpha });
|
||||
graphStore.appendMapping({ chatId: chat2.id, treeNodeId: "beta", graphNodeId: gBeta });
|
||||
|
||||
const stats = emitChat(chat2.id, graphDeps);
|
||||
const stats = mergeFindings(chat2.id, treeFindings(tree2), graphDeps);
|
||||
assert.deepEqual(stats, { nodes: 0, edges: 1, events: 0 });
|
||||
chat2Id = chat2.id;
|
||||
|
||||
@@ -312,7 +351,9 @@ describe("graph emission (emitChat)", () => {
|
||||
const idByStatement = new Map(snap.nodes.map((n) => [n.statement, n.id]));
|
||||
assert.equal(snap.nodeStates[idByStatement.get("Alpha intuition")!], "taught");
|
||||
assert.equal(snap.nodeStates[idByStatement.get("Beta rule")!], "missed");
|
||||
assert.equal(snap.nodeStates[idByStatement.get("Gamma application")!], "candidate");
|
||||
// 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");
|
||||
assert.equal(snap.sessions[chat2Id], "Graph fixture 2");
|
||||
@@ -320,44 +361,25 @@ describe("graph emission (emitChat)", () => {
|
||||
(snap.nodeSessions[idByStatement.get("Alpha intuition")!] ?? []).sort(),
|
||||
[chat1.id, chat2Id].sort(),
|
||||
);
|
||||
// Gamma was only ever created (mapping) by chat1.
|
||||
// Gamma was only ever touched by chat1.
|
||||
assert.deepEqual(snap.nodeSessions[idByStatement.get("Gamma application")!], [chat1.id]);
|
||||
});
|
||||
|
||||
it("replays plan tool outputs when record.tree is missing (removed nodes stay out)", () => {
|
||||
const initTree = createTree(
|
||||
[
|
||||
{ id: "x", title: "X live" },
|
||||
{ id: "y", title: "Y pruned" },
|
||||
],
|
||||
[{ from: "x", to: "y" }],
|
||||
);
|
||||
const prunedTree = removeNode(initTree, "y");
|
||||
const chat = fixtureChat("Replay fixture", null, [
|
||||
user("u1", "go"),
|
||||
assistant("a1", [planPart("p1", "plan_init", { nodes: [], edges: [] }, initTree)]),
|
||||
assistant("a2", [planPart("p2", "plan_remove_node", { id: "y" }, prunedTree)]),
|
||||
]);
|
||||
|
||||
const stats = emitChat(chat.id, graphDeps);
|
||||
assert.deepEqual(stats, { nodes: 1, edges: 0, events: 0 }); // y pruned, edge cascaded away
|
||||
|
||||
const graph = scopedGraph([chat.id]);
|
||||
assert.ok(graph.nodes.some((n) => n.statement === "X live"));
|
||||
assert.ok(!graph.nodes.some((n) => n.statement === "Y pruned"));
|
||||
});
|
||||
|
||||
it("returns zeros for unknown chats", () => {
|
||||
assert.deepEqual(emitChat("chat_does_not_exist", graphDeps), { nodes: 0, edges: 0, events: 0 });
|
||||
assert.deepEqual(mergeFindings("chat_does_not_exist", treeFindings(fixtureTree()), graphDeps), {
|
||||
nodes: 0,
|
||||
edges: 0,
|
||||
events: 0,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// chat2 from the provenance test, reachable by the snapshot test below it.
|
||||
// ─── the trigger helper ──────────────────────────────────────────────────────
|
||||
|
||||
describe("catch-up (foldStaleChats)", () => {
|
||||
it("folds chats newer than their checkpoint, exactly once, deriving known state", () => {
|
||||
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("Catch-up fixture", tree, [
|
||||
const chat = fixtureChat("Trigger fixture", tree, [
|
||||
user("u1", "go"),
|
||||
assistant("a1", [planPart("p1", "plan_init", { nodes: [], edges: [] }, tree)]),
|
||||
assistant("a2", [
|
||||
@@ -366,23 +388,39 @@ describe("catch-up (foldStaleChats)", () => {
|
||||
{ question: "QS", nodeId: "solo", 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(
|
||||
"call-s2",
|
||||
{ question: "QS2", options: [{ label: "a" }, { label: "b" }], correctAnswer: "a", explanation: "e" },
|
||||
answer({ questionId: "call-s2", questionText: "QS2 text", dismissed: true }),
|
||||
),
|
||||
{
|
||||
type: "tool-ask_user_question",
|
||||
toolCallId: "call-s3",
|
||||
state: "output-available",
|
||||
input: { question: "free?" },
|
||||
output: answer({ questionId: "call-s3", kind: "ask", questionText: "free?", text: "y" }),
|
||||
},
|
||||
]),
|
||||
]);
|
||||
|
||||
const folded = foldStaleChats(graphDeps);
|
||||
assert.ok(folded >= 1, "the catch-up fixture was folded");
|
||||
assert.equal(graphStore.readCheckpoints().filter((c) => c.chatId === chat.id).length, 1);
|
||||
assert.equal(pendingQuizAnswers(chat.id, graphDeps), 1, "one answered quiz pending");
|
||||
assert.equal(pendingQuizAnswers("chat_does_not_exist", graphDeps), 0);
|
||||
|
||||
const snap = graphSnapshot(graphDeps);
|
||||
const solo = snap.nodes.find((n) => n.statement === "Solo node");
|
||||
assert.ok(solo, "solo node was emitted by the catch-up fold");
|
||||
assert.equal(snap.nodeStates[solo.id], "known"); // correct check, no taught event
|
||||
assert.equal(snap.sessions[chat.id], "Catch-up fixture");
|
||||
mergeFindings(chat.id, treeFindings(tree), graphDeps);
|
||||
assert.equal(pendingQuizAnswers(chat.id, graphDeps), 0, "extracted → nothing pending");
|
||||
|
||||
// Already checkpointed → not folded again (other test files' chats may
|
||||
// legitimately fold here, so assert on THIS chat only).
|
||||
foldStaleChats(graphDeps);
|
||||
assert.equal(graphStore.readCheckpoints().filter((c) => c.chatId === chat.id).length, 1);
|
||||
// A NEW answered quiz raises the count again (the re-trigger path).
|
||||
chatStore.saveMessages(chat.id, [
|
||||
...chatStore.getChat(chat.id)!.messages,
|
||||
assistant("a3", [
|
||||
quizPart(
|
||||
"call-s4",
|
||||
{ question: "QS4", nodeId: "solo", 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" }),
|
||||
),
|
||||
]),
|
||||
]);
|
||||
assert.equal(pendingQuizAnswers(chat.id, graphDeps), 1);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user