wave 7: the prober reads its map — graph-context injection dies whole
The owner's zero-configuration ruling (2026-09-23): nothing configures the graph beyond chat models. The prior-knowledge injection — its budget dial, its runner, its marker part, its dock/dispatch entries — is removed across all bands; the prober now reads the learner's persistent knowledge map on demand through two gated tools instead: - list_sessions: every birth session, names + kinds + counts + dates (graphSessionIndex, Platform's core seam) - list_propositions(session): what was born there + the session's own latest understanding level + that evaluation's date (graphSessionPropositions) — names/levels/dating ONLY, no statement text (owner ruling: the map's organization is the prober's business, its prose is not) Registered via createProberTools when chat-handler passes graphStore (the graphToolsEnabled gate decided server-side, per-turn fresh); the prober doctrine gains the map-review nudge at session start. Also: runner's JobKind special-casing collapses (KIND_ROLE/KIND_RUNNERS are plain records again), semantic-clusters.ts dies with its test, the embedder dep's doc tells the wave-7 stub story, and ports.ts's readClusterLabels/readNamingRuns surface stays untouched for Platform's ledger-port interlude. 848/848 tests, typecheck/lint/build green.
This commit is contained in:
@@ -10,7 +10,6 @@
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { GRAPH_CONTEXT_TOKENS_MAX } from "../src/core";
|
||||
import { parseSettingsBody } from "../src/server/settings-body.ts";
|
||||
|
||||
describe("parseSettingsBody (shared by both settings routes)", () => {
|
||||
@@ -24,13 +23,11 @@ describe("parseSettingsBody (shared by both settings routes)", () => {
|
||||
const r = parseSettingsBody({
|
||||
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
|
||||
thinkingLevel: "high",
|
||||
graphContextTokens: 256,
|
||||
});
|
||||
assert.ok(r.ok);
|
||||
assert.deepEqual(r.settings, {
|
||||
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
|
||||
thinkingLevel: "high",
|
||||
graphContextTokens: 256,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -54,12 +51,4 @@ describe("parseSettingsBody (shared by both settings routes)", () => {
|
||||
assert.equal(parseSettingsBody({ thinkingLevel: 7 }).ok, false);
|
||||
});
|
||||
|
||||
it("rejects out-of-range or non-integer graphContextTokens — never clamps", () => {
|
||||
for (const bad of [-1, GRAPH_CONTEXT_TOKENS_MAX + 1, 64.5, "256"]) {
|
||||
const r = parseSettingsBody({ graphContextTokens: bad });
|
||||
assert.equal(r.ok, false, String(bad));
|
||||
}
|
||||
const ok = parseSettingsBody({ graphContextTokens: GRAPH_CONTEXT_TOKENS_MAX });
|
||||
assert.equal(ok.ok, true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -43,7 +43,6 @@ import type { GraphStore } from "../src/core/ports.ts";
|
||||
import { abortExtraction, createExtractionPipeline, extractionStatuses, transcriptView } from "../src/agent/subagents/extraction.ts";
|
||||
import { ModelsNotConfiguredError } from "../src/core/settings.ts";
|
||||
import { batchTag, coverageOf, coverageSlots } from "../src/core/coverage.ts";
|
||||
import { semanticClusters } from "../src/core/semantic-clusters.ts";
|
||||
import { chatStore } from "../src/store/chats.ts";
|
||||
import { PATHS } from "../config.ts";
|
||||
|
||||
@@ -839,9 +838,11 @@ const NOTES_VERDICT = {
|
||||
dropped: [],
|
||||
} as const;
|
||||
|
||||
/** Test seam for the embedder: fakes the post-merge embedding batch —
|
||||
* one identical unit vector per node → semanticClusters merges them all
|
||||
* into ONE topic — then persists the partition like the real embedder. */
|
||||
/** Test seam for the embedder: semantic clustering died with wave 7
|
||||
* (birth-session clusters + session-name labels), so the production
|
||||
* embedder is a no-op stub pending Platform's ledger-port removal —
|
||||
* this fake mirrors it: one identical vector per node persisted to the
|
||||
* (still-live) embeddings ledger port, no clusters computed. */
|
||||
const fakeEmbedder = async (store: GraphStore) => {
|
||||
const seen = new Set<string>();
|
||||
const ids: string[] = [];
|
||||
@@ -850,12 +851,11 @@ const fakeEmbedder = async (store: GraphStore) => {
|
||||
seen.add(n.id);
|
||||
ids.push(n.id);
|
||||
}
|
||||
const vectors = Object.fromEntries(ids.map((id) => [id, [1, 0, 0]]));
|
||||
store.writeEmbeddings({
|
||||
model: "test/embedder",
|
||||
dim: 3,
|
||||
vectors,
|
||||
clusters: semanticClusters(vectors),
|
||||
vectors: Object.fromEntries(ids.map((id) => [id, [1, 0, 0]])),
|
||||
clusters: [],
|
||||
updatedAt: "2026-09-13T00:00:00.000Z",
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,293 +0,0 @@
|
||||
/**
|
||||
* Graph context injection — the pure core (docs/vision-context-injection.md):
|
||||
* selection doctrine, level fold, block text, the marker-part parser, and
|
||||
* the runner's pure gates (topic/budget/one-shot). Deterministic, zero
|
||||
* network, zero stores — hand-made vectors.
|
||||
*/
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import type { UIMessage } from "ai";
|
||||
import {
|
||||
buildGraphContextSteer,
|
||||
steerDelivered,
|
||||
GRAPH_CONTEXT_TOKENS_DEFAULT,
|
||||
GRAPH_CONTEXT_TOKENS_MAX,
|
||||
GRAPH_CONTEXT_TOKENS_STOPS,
|
||||
graphContextText,
|
||||
type GraphContextData,
|
||||
} from "../src/core/contracts.ts";
|
||||
import {
|
||||
currentUnderstanding,
|
||||
GRAPH_CONTEXT_MAX_PROPOSITIONS,
|
||||
graphContextOfMessage,
|
||||
hasContext,
|
||||
isUnderstandingLevel,
|
||||
selectGraphContext,
|
||||
} from "../src/core/graph-context.ts";
|
||||
import {
|
||||
contextToolCallId,
|
||||
effectiveContextBudget,
|
||||
topicOf,
|
||||
} from "../src/agent/subagents/graph-context.ts";
|
||||
import type { GraphNode, LedgerEvent } from "../src/core/contracts/graph.ts";
|
||||
|
||||
function unit(i: number): number[] {
|
||||
const v = new Array(8).fill(0);
|
||||
v[i] = 1;
|
||||
return v;
|
||||
}
|
||||
|
||||
function node(id: string, statement: string): GraphNode {
|
||||
return {
|
||||
id,
|
||||
name: id,
|
||||
statement,
|
||||
aliases: [],
|
||||
provenance: [],
|
||||
createdAt: "2026-09-14T00:00:00.000Z",
|
||||
};
|
||||
}
|
||||
|
||||
function obs(nodeId: string, level: string, at: string, chatId = "chat_x"): LedgerEvent {
|
||||
return { nodeId, kind: "observation", chatId, at, note: level };
|
||||
}
|
||||
|
||||
/** A text-bearing user message (the learner's typed speech). */
|
||||
function userText(text: string): UIMessage {
|
||||
return { id: `m_${text.length}`, role: "user", parts: [{ type: "text", text }] };
|
||||
}
|
||||
|
||||
/** A marker-only user message (steer scaffolding — no text). */
|
||||
function marker(type: string, data: unknown): UIMessage {
|
||||
return { id: `m_${type}`, role: "user", parts: [{ type, data } as never] };
|
||||
}
|
||||
|
||||
describe("currentUnderstanding — the most-recent fold", () => {
|
||||
it("latest observation wins, across chats, ledger order breaks ties", () => {
|
||||
const events = [
|
||||
obs("n1", "shaky", "2026-09-01T00:00:00Z", "chat_a"),
|
||||
obs("n1", "solid", "2026-09-10T00:00:00Z", "chat_b"),
|
||||
obs("n2", "resisted", "2026-09-05T00:00:00Z"),
|
||||
obs("n2", "accepted", "2026-09-05T00:00:00Z"), // same at → later line wins
|
||||
];
|
||||
const levels = currentUnderstanding(events);
|
||||
assert.equal(levels.get("n1"), "solid");
|
||||
assert.equal(levels.get("n2"), "accepted");
|
||||
assert.equal(levels.size, 2);
|
||||
});
|
||||
|
||||
it("off-scale notes never count (the closed list is the only vocabulary)", () => {
|
||||
assert.equal(isUnderstandingLevel("solid"), true);
|
||||
assert.equal(isUnderstandingLevel("pretty good"), false);
|
||||
const levels = currentUnderstanding([obs("n1", "confident", "2026-09-01T00:00:00Z")]);
|
||||
assert.equal(levels.size, 0);
|
||||
});
|
||||
|
||||
it("quiz outcome events and null nodes are not observations", () => {
|
||||
const events: LedgerEvent[] = [
|
||||
{ nodeId: "n1", kind: "correct", chatId: "c", at: "2026-09-01T00:00:00Z" },
|
||||
{ nodeId: null, kind: "observation", chatId: "c", at: "2026-09-01T00:00:00Z", note: "solid" },
|
||||
];
|
||||
assert.equal(currentUnderstanding(events).size, 0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("selectGraphContext — similarity orders, the budget is the only amount control", () => {
|
||||
const base = {
|
||||
chatId: "chat_1",
|
||||
query: unit(0),
|
||||
events: [] as LedgerEvent[],
|
||||
};
|
||||
|
||||
it("fills similarity-descending and stops before exceeding the budget", () => {
|
||||
const vectors = { a: unit(0), b: unit(0), c: unit(7) };
|
||||
const nodes = [
|
||||
node("a", "Water boils at 100°C at sea level."),
|
||||
node("b", "Evaporation cools the surrounding air."),
|
||||
node("c", "The Schrödinger equation is linear."),
|
||||
];
|
||||
// budget 55 (framing) + ~9 tokens for statement a ≈ fits exactly one line.
|
||||
const data = selectGraphContext({ ...base, vectors, nodes, budget: 66 });
|
||||
assert.deepEqual(
|
||||
data.propositions.map((p) => p.id),
|
||||
["a"],
|
||||
);
|
||||
assert.equal(data.tokens <= 66, true);
|
||||
assert.equal(data.budget, 66);
|
||||
});
|
||||
|
||||
it("an oversized line is skipped, a later smaller one may still fit (no threshold, pure budget)", () => {
|
||||
const vectors = { big: unit(0), small: unit(0), off: unit(7) };
|
||||
const big = "X".repeat(400); // ~100 tokens — bigger than the whole budget
|
||||
const nodes = [
|
||||
node("big", big),
|
||||
node("small", "Ice melts."),
|
||||
node("off", "Irrelevant."),
|
||||
];
|
||||
// budget 60: 'big' (~100 tokens) is skipped outright; 'small' (4 tokens
|
||||
// incl. the level slot + framing 55 = 59) fits; 'off' (63) doesn't.
|
||||
const data = selectGraphContext({ ...base, vectors, nodes, budget: 60 });
|
||||
assert.deepEqual(
|
||||
data.propositions.map((p) => p.id),
|
||||
["small"],
|
||||
);
|
||||
});
|
||||
|
||||
it("annotates each line with the folded current level (null when unobserved)", () => {
|
||||
const vectors = { a: unit(0), b: unit(0) };
|
||||
const nodes = [node("a", "A."), node("b", "B.")];
|
||||
const events = [obs("b", "shaky", "2026-09-10T00:00:00Z")];
|
||||
const data = selectGraphContext({ ...base, vectors, nodes, events, budget: 4096 });
|
||||
const byId = Object.fromEntries(data.propositions.map((p) => [p.id, p]));
|
||||
assert.equal(byId.a!.level, null);
|
||||
assert.equal(byId.b!.level, "shaky");
|
||||
});
|
||||
|
||||
it("the count cap bounds even a huge budget", () => {
|
||||
const vectors: Record<string, number[]> = {};
|
||||
const nodes: GraphNode[] = [];
|
||||
for (let i = 0; i < GRAPH_CONTEXT_MAX_PROPOSITIONS + 10; i++) {
|
||||
const id = `n${i}`;
|
||||
vectors[id] = unit(0);
|
||||
nodes.push(node(id, `Statement ${i}.`));
|
||||
}
|
||||
const data = selectGraphContext({ ...base, vectors, nodes, budget: GRAPH_CONTEXT_TOKENS_MAX });
|
||||
assert.equal(data.propositions.length, GRAPH_CONTEXT_MAX_PROPOSITIONS);
|
||||
});
|
||||
|
||||
it("dimension-mismatched vectors and duplicate node lines are handled", () => {
|
||||
const vectors = { ok: unit(0), stale: [1, 2, 3] };
|
||||
const nodes = [
|
||||
node("ok", "Fine."),
|
||||
node("dup", "First line."),
|
||||
node("dup", "Second line never redefines."),
|
||||
];
|
||||
// "dup" has no vector anyway; the pin is that the scan neither throws
|
||||
// nor scores the stale-dim vector.
|
||||
const data = selectGraphContext({ ...base, vectors, nodes, budget: 4096 });
|
||||
assert.deepEqual(
|
||||
data.propositions.map((p) => p.id),
|
||||
["ok"],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("graphContextText + hasContext — the canonical block", () => {
|
||||
it("carries the counts in the header, the level tag per line, the gloss at the end", () => {
|
||||
const data: GraphContextData = {
|
||||
chatId: "c1",
|
||||
budget: 1024,
|
||||
tokens: 42,
|
||||
propositions: [
|
||||
{ id: "n1", statement: "Water boils at 100°C.", level: "solid" },
|
||||
{ id: "n2", statement: "Humidity slows evaporation.", level: null },
|
||||
],
|
||||
};
|
||||
const text = graphContextText(data);
|
||||
assert.match(text, /2 propositions, ~42 tokens/);
|
||||
assert.match(text, /- \[solid\] Water boils at 100°C\./);
|
||||
assert.match(text, /- Humidity slows evaporation\./);
|
||||
assert.match(text, /Do not re-teach solid ground\./);
|
||||
});
|
||||
|
||||
it("an empty selection has no text and no context", () => {
|
||||
const empty: GraphContextData = { chatId: "c1", budget: 1024, tokens: 55, propositions: [] };
|
||||
assert.equal(graphContextText(empty), "");
|
||||
assert.equal(hasContext(empty), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("graphContextOfMessage — parse-don't-cast at the marker seam", () => {
|
||||
it("round-trips a well-formed part", () => {
|
||||
const msg = marker("data-graph-context", {
|
||||
chatId: "c1",
|
||||
budget: 512,
|
||||
tokens: 60,
|
||||
propositions: [{ id: "n1", statement: "S.", level: "shaky" }],
|
||||
});
|
||||
const data = graphContextOfMessage(msg);
|
||||
assert.equal(data?.chatId, "c1");
|
||||
assert.equal(data?.propositions[0]?.level, "shaky");
|
||||
});
|
||||
|
||||
it("rejects malformed shapes wholesale (bad level, bad budget, non-array)", () => {
|
||||
assert.equal(graphContextOfMessage(marker("data-graph-context", { chatId: "c" })), null);
|
||||
assert.equal(
|
||||
graphContextOfMessage(
|
||||
marker("data-graph-context", {
|
||||
chatId: "c",
|
||||
budget: 512,
|
||||
tokens: 1,
|
||||
propositions: [{ id: "n", statement: "s", level: "kinda" }],
|
||||
}),
|
||||
),
|
||||
null,
|
||||
);
|
||||
assert.equal(
|
||||
graphContextOfMessage(
|
||||
marker("data-graph-context", { chatId: "c", budget: 999999, tokens: 1, propositions: [] }),
|
||||
),
|
||||
null,
|
||||
);
|
||||
});
|
||||
|
||||
it("absent part → null", () => {
|
||||
assert.equal(graphContextOfMessage(userText("hello")), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildGraphContextSteer — the marker-only delivery", () => {
|
||||
it("id is deterministic per chat; metadata carries chat + job; one part", () => {
|
||||
const data: GraphContextData = {
|
||||
chatId: "chat_9",
|
||||
budget: 1024,
|
||||
tokens: 10,
|
||||
propositions: [{ id: "n1", statement: "S.", level: null }],
|
||||
};
|
||||
const steer = buildGraphContextSteer({ chatId: "chat_9", jobId: "job_1" }, data);
|
||||
assert.equal(steer.id, "steer_ctx_chat_9");
|
||||
assert.equal(steer.role, "user");
|
||||
assert.deepEqual(steer.metadata, { graphContext: { chatId: "chat_9", jobId: "job_1" } });
|
||||
assert.equal(steer.parts.length, 1);
|
||||
assert.equal(steer.parts[0]!.type, "data-graph-context");
|
||||
// steerDelivered keys on the id — the one-shot guard.
|
||||
assert.equal(steerDelivered([steer], "steer_ctx_chat_9"), true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the runner's pure gates", () => {
|
||||
it("topicOf: exactly one typed message is the topic; steers and answers never count", () => {
|
||||
assert.equal(topicOf([userText("the water cycle")]), "the water cycle");
|
||||
assert.equal(topicOf([]), null);
|
||||
assert.equal(topicOf([userText("a"), userText("b")]), null);
|
||||
// A quiz answer: tool-output part, no text — probing never breaks the gate.
|
||||
const answer: UIMessage = {
|
||||
id: "m_ans",
|
||||
role: "user",
|
||||
parts: [{ type: "tool-quiz", toolCallId: "t1", state: "output-available", input: {}, output: {} } as never],
|
||||
};
|
||||
assert.equal(topicOf([userText("topic"), answer, marker("data-probe-handoff", { chatId: "c" })]), "topic");
|
||||
// Multi-part text joins into the topic.
|
||||
assert.equal(topicOf([userText("a"), userText("")]), "a");
|
||||
});
|
||||
|
||||
it("effectiveContextBudget: absent = the default (on), 0 = off, set = set", () => {
|
||||
assert.equal(effectiveContextBudget(undefined), GRAPH_CONTEXT_TOKENS_DEFAULT);
|
||||
assert.equal(effectiveContextBudget({}), GRAPH_CONTEXT_TOKENS_DEFAULT);
|
||||
assert.equal(effectiveContextBudget({ graphContextTokens: 0 }), 0);
|
||||
assert.equal(effectiveContextBudget({ graphContextTokens: 2048 }), 2048);
|
||||
});
|
||||
|
||||
it("the decided knobs: default 1024, max 16384, discrete powers-of-four stops from 64", () => {
|
||||
assert.equal(GRAPH_CONTEXT_TOKENS_DEFAULT, 1024);
|
||||
assert.equal(GRAPH_CONTEXT_TOKENS_MAX, 16384);
|
||||
// The scale revision (owner, 2026-09-15): off then powers of four —
|
||||
// every stop a real decision, no fine-grained dial theater.
|
||||
assert.deepEqual([...GRAPH_CONTEXT_TOKENS_STOPS], [0, 64, 256, 1024, 4096, 16384]);
|
||||
});
|
||||
|
||||
it("contextToolCallId is deterministic per chat (the one-shot dedup key)", () => {
|
||||
assert.equal(contextToolCallId("chat_abc"), "ctx_chat_abc");
|
||||
});
|
||||
});
|
||||
@@ -1,333 +0,0 @@
|
||||
/**
|
||||
* Graph context injection — the runner + the wake integration
|
||||
* (docs/vision-context-injection.md). The trigger gates, the failure
|
||||
* modes, the one happy delivery (real wake pipeline, fake turn runner,
|
||||
* scripted embedder), and the SILENT abort (the decided contrast with
|
||||
* teacher-dispatched sub-agents, which abort with a steer).
|
||||
*
|
||||
* Cleanup follows tests/jobs.test.ts: files removed in finally/after.
|
||||
*/
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import { after, describe, it } from "node:test";
|
||||
import { deliverJobResult, setTurnRunner } from "../src/server/jobs.ts";
|
||||
import { abortJob } from "../src/server/jobs.ts";
|
||||
import { persistTurn } from "../src/server/turn-service.ts";
|
||||
import { startGraphContextInjection, runGraphContextJob, type GraphContextDeps, type GraphContextEmbedder } from "../src/agent/subagents/graph-context.ts";
|
||||
import { chatStore } from "../src/store/chats.ts";
|
||||
import { jobStore } from "../src/store/jobs.ts";
|
||||
import { PATHS } from "../config.ts";
|
||||
import type { EmbeddingsLedger, GraphNode, LedgerEvent } from "../src/core/contracts/graph.ts";
|
||||
import type { JobRecord, SteerMessage } from "../src/core/contracts.ts";
|
||||
|
||||
const createdChats: string[] = [];
|
||||
const createdJobs: string[] = [];
|
||||
|
||||
function trackChat(id: string) {
|
||||
createdChats.push(id);
|
||||
}
|
||||
function trackJob(id: string) {
|
||||
createdJobs.push(id);
|
||||
}
|
||||
|
||||
after(() => {
|
||||
for (const id of createdChats) {
|
||||
try {
|
||||
fs.unlinkSync(path.join(PATHS.chats, `${id}.json`));
|
||||
} catch {
|
||||
/* already gone */
|
||||
}
|
||||
}
|
||||
for (const id of createdJobs) {
|
||||
try {
|
||||
fs.unlinkSync(path.join(PATHS.data, "jobs", `${id}.json`));
|
||||
} catch {
|
||||
/* already gone */
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function waitFor(cond: () => boolean, timeoutMs = 8000, what = "condition"): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const started = Date.now();
|
||||
const tick = () => {
|
||||
if (cond()) return resolve();
|
||||
if (Date.now() - started > timeoutMs) return reject(new Error(`timeout waiting for ${what}`));
|
||||
setTimeout(tick, 25);
|
||||
};
|
||||
tick();
|
||||
});
|
||||
}
|
||||
|
||||
function unit(i: number): number[] {
|
||||
const v = new Array(8).fill(0);
|
||||
v[i] = 1;
|
||||
return v;
|
||||
}
|
||||
|
||||
function node(id: string, statement: string): GraphNode {
|
||||
return { id, name: id, statement, aliases: [], provenance: [], createdAt: "2026-09-14T00:00:00.000Z" };
|
||||
}
|
||||
|
||||
/** A minimal in-memory graph store — the runner only reads. */
|
||||
function fakeGraphStore(opts: {
|
||||
vectors?: Record<string, number[]>;
|
||||
nodes?: GraphNode[];
|
||||
events?: LedgerEvent[];
|
||||
model?: string;
|
||||
dim?: number;
|
||||
}): Pick<GraphContextDeps["graphStore"], "readNodes" | "readEvents" | "readEmbeddings"> {
|
||||
const ledger: EmbeddingsLedger = {
|
||||
model: opts.model ?? "test-embed",
|
||||
dim: opts.dim ?? 8,
|
||||
vectors: opts.vectors ?? {},
|
||||
clusters: [],
|
||||
updatedAt: "2026-09-14T00:00:00.000Z",
|
||||
};
|
||||
return {
|
||||
readNodes: () => opts.nodes ?? [],
|
||||
readEvents: () => opts.events ?? [],
|
||||
readEmbeddings: () => ledger,
|
||||
};
|
||||
}
|
||||
|
||||
/** A chat at session start: exactly one typed message (the topic). */
|
||||
function newTopicChat(label: string, settings?: Record<string, unknown>): { id: string } {
|
||||
const chat = chatStore.createChat(`Test: ${label}`);
|
||||
trackChat(chat.id);
|
||||
chatStore.saveMessages(chat.id, [
|
||||
{ id: "u1", role: "user", parts: [{ type: "text", text: "the water cycle" }] },
|
||||
] as never);
|
||||
if (settings) chatStore.saveSettings(chat.id, settings as never);
|
||||
return { id: chat.id };
|
||||
}
|
||||
|
||||
/** The scripted embedder: deterministic query vectors, recorded topics.
|
||||
* Wave 6 shape: the query carries its (absent) prompt-token count; the
|
||||
* resolver seam wraps the static fake (fresh resolution per run). */
|
||||
function fakeEmbedder(vec: number[] = unit(0)) {
|
||||
const topics: string[] = [];
|
||||
return {
|
||||
topics,
|
||||
embedder: {
|
||||
model: "test-embed",
|
||||
embedQuery: async (text: string) => {
|
||||
topics.push(text);
|
||||
return { vector: vec, promptTokens: undefined };
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function deps(
|
||||
graph: Pick<GraphContextDeps["graphStore"], "readNodes" | "readEvents" | "readEmbeddings">,
|
||||
embedder: GraphContextEmbedder,
|
||||
deliver: (steer: SteerMessage) => Promise<void>,
|
||||
): GraphContextDeps {
|
||||
return { chatStore, jobStore, graphStore: graph, embedder: () => embedder, deliver };
|
||||
}
|
||||
|
||||
describe("startGraphContextInjection — the one-shot gates", () => {
|
||||
it("starts at session start: job kind graph-context, deterministic toolCallId, runner runs to delivery", async () => {
|
||||
const { id: chatId } = newTopicChat("injection happy gate");
|
||||
const graph = fakeGraphStore({
|
||||
vectors: { n1: unit(0) },
|
||||
nodes: [node("n1", "Water evaporates from oceans.")],
|
||||
});
|
||||
const { embedder, topics } = fakeEmbedder();
|
||||
const delivered: SteerMessage[] = [];
|
||||
const d = deps(graph, embedder, async (steer) => {
|
||||
delivered.push(steer);
|
||||
});
|
||||
|
||||
const job = startGraphContextInjection(d, chatId);
|
||||
assert.ok(job, "a chat at session start with budget on starts the injection");
|
||||
trackJob(job.id);
|
||||
assert.equal(job.kind, "graph-context");
|
||||
assert.equal(job.toolCallId, `ctx_${chatId}`);
|
||||
|
||||
await waitFor(() => delivered.length > 0, 8000, "delivery");
|
||||
assert.deepEqual(topics, ["the water cycle"], "the topic is the session's only typed message");
|
||||
const steer = delivered[0]!;
|
||||
assert.equal(steer.id, `steer_ctx_${chatId}`);
|
||||
assert.equal(steer.parts.length, 1);
|
||||
assert.equal(steer.parts[0]!.type, "data-graph-context");
|
||||
|
||||
// One shot: a second trigger does nothing (job exists).
|
||||
assert.equal(startGraphContextInjection(d, chatId), null);
|
||||
});
|
||||
|
||||
it("budget 0 (off) never starts; a session past its start never injects", async () => {
|
||||
const { id: off } = newTopicChat("injection off", { graphContextTokens: 0 });
|
||||
const { id: rolling } = (() => {
|
||||
const chat = chatStore.createChat("Test: two typed messages");
|
||||
trackChat(chat.id);
|
||||
chatStore.saveMessages(chat.id, [
|
||||
{ id: "u1", role: "user", parts: [{ type: "text", text: "topic" }] },
|
||||
{ id: "u2", role: "user", parts: [{ type: "text", text: "a second message" }] },
|
||||
] as never);
|
||||
return { id: chat.id };
|
||||
})();
|
||||
const graph = fakeGraphStore({ vectors: { n1: unit(0) }, nodes: [node("n1", "S.")] });
|
||||
const d = deps(graph, fakeEmbedder().embedder, async () => {});
|
||||
|
||||
assert.equal(startGraphContextInjection(d, off), null, "0 budget = off");
|
||||
assert.equal(startGraphContextInjection(d, rolling), null, "no retroactive injection");
|
||||
assert.equal(jobStore.listJobs(off).length, 0);
|
||||
assert.equal(jobStore.listJobs(rolling).length, 0);
|
||||
assert.equal(startGraphContextInjection(d, "chat_missing"), null, "unknown chat");
|
||||
});
|
||||
|
||||
it("an ERRORED context job still blocks re-injection (one shot means one shot)", async () => {
|
||||
const { id: chatId } = newTopicChat("injection error one-shot");
|
||||
const graph = fakeGraphStore({ vectors: {} }); // empty ledger → the run errors
|
||||
const d = deps(graph, fakeEmbedder().embedder, async () => {});
|
||||
const job = startGraphContextInjection(d, chatId);
|
||||
assert.ok(job);
|
||||
trackJob(job.id);
|
||||
await waitFor(() => jobStore.getJob(job.id)!.status === "error", 8000, "job errors");
|
||||
assert.match(jobStore.getJob(job.id)!.error ?? "", /no embeddings yet/);
|
||||
// Re-trigger with a now-populated ledger: still nothing.
|
||||
const populated = deps(
|
||||
fakeGraphStore({ vectors: { n1: unit(0) }, nodes: [node("n1", "S.")] }),
|
||||
fakeEmbedder().embedder,
|
||||
async () => {},
|
||||
);
|
||||
assert.equal(startGraphContextInjection(populated, chatId), null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("runGraphContextJob — failure modes are honest errors, never phantom deliveries", () => {
|
||||
it("a stale ledger (model/dim mismatch) errors with the parity reason", async () => {
|
||||
const { id: chatId } = newTopicChat("injection stale ledger");
|
||||
const chat = chatStore.createChat(`Test: ${chatId}`); // placeholder to satisfy lint-free compile
|
||||
trackChat(chat.id);
|
||||
const job = jobStore.createJob({ kind: "graph-context", chatId, toolCallId: `ctx_${chatId}` });
|
||||
trackJob(job.id);
|
||||
// Ledger stamped with a DIFFERENT model than the embedder (wave 6:
|
||||
// staleness is the model identity — dim is checked against the
|
||||
// served vector below).
|
||||
const d = deps(fakeGraphStore({ vectors: { n1: unit(0) }, model: "other-model" }), fakeEmbedder().embedder, async () => {});
|
||||
await runGraphContextJob(d, job.id);
|
||||
const got = jobStore.getJob(job.id)!;
|
||||
assert.equal(got.status, "error");
|
||||
assert.match(got.error ?? "", /stale/);
|
||||
});
|
||||
|
||||
it("BYOK: a null embedder (unset model or vanished key) fails honestly, never a phantom query", async () => {
|
||||
const { id: chatId } = newTopicChat("injection unconfigured");
|
||||
const job = jobStore.createJob({ kind: "graph-context", chatId, toolCallId: `ctx_${chatId}` });
|
||||
trackJob(job.id);
|
||||
// Wave 6: the resolver collapses BOTH unset-model and missing-key
|
||||
// into null — the error must name the two honestly, never a false
|
||||
// "graph.embeddingModel is null" for a key race.
|
||||
const d = { ...deps(fakeGraphStore({ vectors: { n1: unit(0) } }), fakeEmbedder().embedder, async () => {}), embedder: () => null };
|
||||
await runGraphContextJob(d, job.id);
|
||||
const got = jobStore.getJob(job.id)!;
|
||||
assert.equal(got.status, "error");
|
||||
assert.match(got.error ?? "", /no embedding model available/);
|
||||
assert.match(got.error ?? "", /unset, or its provider key is missing/);
|
||||
});
|
||||
|
||||
it("nothing fitting the budget errors rather than delivering an empty block", async () => {
|
||||
// Budget below the framing overhead: nothing can fit, ever.
|
||||
const { id: chatId } = newTopicChat("injection empty selection", { graphContextTokens: 10 });
|
||||
const job = jobStore.createJob({ kind: "graph-context", chatId, toolCallId: `ctx_${chatId}` });
|
||||
trackJob(job.id);
|
||||
// Query orthogonal to every vector; budget can hold nothing.
|
||||
const graph = fakeGraphStore({ vectors: { n1: unit(7) }, nodes: [node("n1", "A very long statement indeed.")] });
|
||||
const d = deps(graph, fakeEmbedder(unit(0)).embedder, async () => {});
|
||||
await runGraphContextJob(d, job.id);
|
||||
const got = jobStore.getJob(job.id)!;
|
||||
assert.equal(got.status, "error");
|
||||
assert.match(got.error ?? "", /nothing relevant fit the budget/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("the injection + the wake pipeline (end to end, fake turn runner)", () => {
|
||||
it("delivers through the wake: the record gains the marker steer, the job flips done, the dock kind rides along", async () => {
|
||||
const { id: chatId } = newTopicChat("injection wake delivery");
|
||||
const graph = fakeGraphStore({
|
||||
vectors: { n1: unit(0), n2: unit(0) },
|
||||
nodes: [node("n1", "Water evaporates from oceans."), node("n2", "Condensation forms clouds.")],
|
||||
events: [
|
||||
{ nodeId: "n1", kind: "observation", chatId: "chat_old", at: "2026-09-10T00:00:00Z", note: "solid" },
|
||||
],
|
||||
});
|
||||
const fired: { chatId: string; last: SteerMessage }[] = [];
|
||||
setTurnRunner(async (cid, messages) => {
|
||||
// Mimic the real wake turn: write-ahead persistence of the incoming
|
||||
// messages (the handler does this), then 200.
|
||||
await persistTurn(chatStore, cid, messages as never, false);
|
||||
fired.push({ chatId: cid, last: messages[messages.length - 1] as SteerMessage });
|
||||
return 200;
|
||||
});
|
||||
|
||||
const d = deps(graph, fakeEmbedder().embedder, deliverJobResult);
|
||||
const job = startGraphContextInjection(d, chatId);
|
||||
assert.ok(job);
|
||||
trackJob(job.id);
|
||||
|
||||
await waitFor(() => (jobStore.getJob(job.id)?.status ?? "running") === "done", 8000, "wake flipped the job");
|
||||
const got = jobStore.getJob(job.id)!;
|
||||
assert.equal(got.status, "done");
|
||||
assert.equal(got.activity, "delivered");
|
||||
|
||||
// The transcript carries the marker steer (audit trail + dedup key).
|
||||
const record = chatStore.getChat(chatId)!;
|
||||
const steerMsg = record.messages.find((m) => m.id === `steer_ctx_${chatId}`);
|
||||
assert.ok(steerMsg, "the steer persisted to the record");
|
||||
const part = steerMsg.parts[0] as { type: string; data: { propositions?: unknown[] } };
|
||||
assert.equal(part.type, "data-graph-context");
|
||||
assert.ok(part.data.propositions!.length >= 1);
|
||||
|
||||
// The server-side turn fired once, with the steer as its last input.
|
||||
assert.equal(fired.length, 1);
|
||||
assert.equal(fired[0]!.chatId, chatId);
|
||||
assert.equal(fired[0]!.last.id, `steer_ctx_${chatId}`);
|
||||
});
|
||||
|
||||
it("aborts SILENTLY: the record flips, the teacher is never told (nothing was promised)", async () => {
|
||||
const { id: chatId } = newTopicChat("injection abort");
|
||||
// A hanging embedder — killed mid-call, like the research abort test.
|
||||
let release: (() => void) | null = null;
|
||||
const hanging = new Promise<void>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
const embedder = {
|
||||
model: "test-embed",
|
||||
embedQuery: () => hanging.then(() => ({ vector: unit(0), promptTokens: undefined })),
|
||||
};
|
||||
const fired: number[] = [];
|
||||
setTurnRunner(async () => {
|
||||
fired.push(1);
|
||||
return 200;
|
||||
});
|
||||
const graph = fakeGraphStore({ vectors: { n1: unit(0) }, nodes: [node("n1", "S.")] });
|
||||
const d = deps(graph, embedder, deliverJobResult);
|
||||
const job = startGraphContextInjection(d, chatId);
|
||||
assert.ok(job);
|
||||
trackJob(job.id);
|
||||
|
||||
await waitFor(() => jobStore.getJob(job.id)!.activity === "embedding topic", 8000, "run started");
|
||||
const outcome = abortJob(job.id);
|
||||
assert.ok(outcome.ok);
|
||||
const got = jobStore.getJob(job.id)!;
|
||||
assert.equal(got.status, "error");
|
||||
assert.equal(got.error, "Aborted by learner");
|
||||
|
||||
release!(); // unwind the hanging call; the abort check exits silently
|
||||
await waitFor(() => fired.length >= 0, 50, "settle");
|
||||
await new Promise((r) => setTimeout(r, 150));
|
||||
assert.equal(fired.length, 0, "NO abort steer — the decided contrast with sub-agent jobs");
|
||||
assert.equal(jobStore.getJob(job.id)!.status, "error", "terminal");
|
||||
// And the chat record never gained a context steer.
|
||||
const record = chatStore.getChat(chatId)!;
|
||||
assert.equal(
|
||||
record.messages.some((m) => m.id === `steer_ctx_${chatId}`),
|
||||
false,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,203 @@
|
||||
// THE PROBER'S GRAPH TOOLS (wave 7) — pinned behaviors:
|
||||
// 1. REGISTRATION: the factory registers list_sessions/list_propositions
|
||||
// on the prober ONLY when the composition root passes a graphStore
|
||||
// (the graphToolsEnabled gate lives server-side; absent store = closed
|
||||
// gate — the factory must not reach for settings itself).
|
||||
// 2. OUTPUT: names + levels + dating only — the owner ruling bars
|
||||
// statement text and session contents from the prober's view of the map.
|
||||
// 3. HONESTY: empty graph, unknown session id — the tools answer in
|
||||
// their own voice, never throw, never guess.
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { createProberTools } from "../src/agent/tools.ts";
|
||||
import type { ChatStore, GraphStore, JobStore } from "../src/core/ports.ts";
|
||||
import type { ChatSummary } from "../src/core/contracts/chat.ts";
|
||||
import type { GraphEdge, GraphNode, LedgerEvent } from "../src/core/contracts/graph.ts";
|
||||
|
||||
// ─── fixtures ────────────────────────────────────────────────────────────────
|
||||
|
||||
const pt = (chatId: string, messageId = "m1") => ({ chatId, messageId });
|
||||
const node = (
|
||||
id: string,
|
||||
name: string,
|
||||
provenance: Array<{ chatId: string; messageId: string }>,
|
||||
): GraphNode => ({
|
||||
id,
|
||||
name,
|
||||
statement: `statement of ${name}`, // must NEVER leak into tool output
|
||||
aliases: [],
|
||||
createdAt: "2026-09-23T00:00:00.000Z",
|
||||
provenance,
|
||||
});
|
||||
|
||||
const observation = (
|
||||
nodeId: string,
|
||||
chatId: string,
|
||||
note: string,
|
||||
at: string,
|
||||
): LedgerEvent =>
|
||||
({ kind: "observation", nodeId, chatId, note, at }) as unknown as LedgerEvent;
|
||||
|
||||
function fakeStores(
|
||||
nodes: GraphNode[],
|
||||
over: {
|
||||
events?: LedgerEvent[];
|
||||
chats?: Array<Pick<ChatSummary, "id" | "title" | "createdAt" | "updatedAt">>;
|
||||
} = {},
|
||||
): { chatStore: ChatStore; graphStore: GraphStore } {
|
||||
const edges: GraphEdge[] = [];
|
||||
const graphStore: GraphStore = {
|
||||
readNodes: () => nodes,
|
||||
readEdges: () => edges,
|
||||
readEvents: () => over.events ?? [],
|
||||
readMappings: () => [],
|
||||
readCheckpoints: () => [],
|
||||
readCoverage: () => [],
|
||||
readSources: () => [],
|
||||
readPositions: () => ({}),
|
||||
writePositions: () => {},
|
||||
readEmbeddings: () => ({ model: "dead", dim: 0, vectors: {}, clusters: [], updatedAt: "" }),
|
||||
writeEmbeddings: () => {},
|
||||
readClusterLabels: () => [],
|
||||
appendClusterLabel: () => {},
|
||||
appendNamingRun: () => {},
|
||||
readNamingRuns: () => [],
|
||||
} as unknown as GraphStore;
|
||||
const chatStore: ChatStore = {
|
||||
listChats: () =>
|
||||
(over.chats ?? []).map((c) => ({ ...c, messageCount: 0 }) as unknown as ChatSummary),
|
||||
getChat: (id: string) =>
|
||||
((over.chats ?? []).find((c) => c.id === id) as unknown as ChatSummary) ?? undefined,
|
||||
} as unknown as ChatStore;
|
||||
return { chatStore, graphStore };
|
||||
}
|
||||
|
||||
const stubJobStore = {
|
||||
createJob: (() => ({})) as never,
|
||||
getJob: (() => undefined) as never,
|
||||
listJobs: (() => []) as never,
|
||||
updateJob: (() => {}) as never,
|
||||
} as unknown as JobStore;
|
||||
|
||||
const baseCtx = {
|
||||
chatId: "c1",
|
||||
deliver: () => {},
|
||||
jobStore: stubJobStore,
|
||||
spawn: () => {},
|
||||
};
|
||||
|
||||
// ─── registration (the factory gate) ────────────────────────────────────────
|
||||
|
||||
describe("prober graph tools — registration", () => {
|
||||
it("no graphStore in the context → the tools are absent (gate decided server-side)", () => {
|
||||
const { chatStore } = fakeStores([]);
|
||||
const tools = createProberTools({ ...baseCtx, chatStore });
|
||||
assert.equal("list_sessions" in tools, false);
|
||||
assert.equal("list_propositions" in tools, false);
|
||||
});
|
||||
|
||||
it("graphStore present → list_sessions + list_propositions join the registry beside the usual set", () => {
|
||||
const { chatStore, graphStore } = fakeStores([node("n1", "Water cycle", [pt("c1")])]);
|
||||
const tools = createProberTools({ ...baseCtx, chatStore, graphStore });
|
||||
// The standing set (fix wave 2026-09-20: research + pdf are
|
||||
// phase-omnipresent) plus the two graph-map readers.
|
||||
assert.deepEqual(Object.keys(tools).sort(), [
|
||||
"ask_user_question",
|
||||
"find",
|
||||
"grep",
|
||||
"list_propositions",
|
||||
"list_sessions",
|
||||
"ls",
|
||||
"pdf",
|
||||
"probe_done",
|
||||
"quiz",
|
||||
"read",
|
||||
"research",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── output shape + the no-prose rule ──────────────────────────────────────
|
||||
|
||||
describe("list_sessions", () => {
|
||||
it("names, kinds, counts, dates, ids — in the prober's voice, no statements", async () => {
|
||||
const { chatStore, graphStore } = fakeStores(
|
||||
[
|
||||
node("n1", "Water cycle", [pt("c1")]),
|
||||
node("n2", "Evaporation", [pt("c1")]),
|
||||
node("n3", "Nietzsche", [pt("c-file")]),
|
||||
],
|
||||
{
|
||||
chats: [
|
||||
{
|
||||
id: "c1",
|
||||
title: "First lesson",
|
||||
createdAt: "2026-09-20T10:00:00.000Z",
|
||||
updatedAt: "2026-09-21T10:00:00.000Z",
|
||||
},
|
||||
],
|
||||
events: [],
|
||||
},
|
||||
);
|
||||
const tools = createProberTools({ ...baseCtx, chatStore, graphStore });
|
||||
const out = (await tools.list_sessions!.execute!({}, { toolCallId: "tc" })) as string;
|
||||
assert.match(out, /2 session\(s\) in the knowledge graph, most recent first:/);
|
||||
// The chat: name, kind, born count, dates, id — the full resume.
|
||||
assert.match(out, /First lesson \(chat, 2 propositions, born 2026-09-20T10:00:00.000Z, last active 2026-09-21T10:00:00.000Z\) — id: c1/);
|
||||
// The unrecorded birth session falls back honestly: the id, no fake date.
|
||||
assert.match(out, /c-file \(file, 1 proposition, born unknown date\) — id: c-file/);
|
||||
// The owner ruling: the map's prose never reaches the prober.
|
||||
assert.ok(!out.includes("statement of"));
|
||||
});
|
||||
|
||||
it("an empty graph gets an honest answer, not an empty string or a throw", async () => {
|
||||
const { chatStore, graphStore } = fakeStores([]);
|
||||
const tools = createProberTools({ ...baseCtx, chatStore, graphStore });
|
||||
const out = (await tools.list_sessions!.execute!({}, { toolCallId: "tc" })) as string;
|
||||
assert.equal(out, "The knowledge graph is empty — no session has contributed propositions yet.");
|
||||
});
|
||||
});
|
||||
|
||||
describe("list_propositions", () => {
|
||||
it("names + latest understanding + that evaluation's date — the latest event wins", async () => {
|
||||
const { chatStore, graphStore } = fakeStores(
|
||||
[node("n1", "Water cycle", [pt("c1")]), node("n2", "Evaporation", [pt("c1")])],
|
||||
{
|
||||
events: [
|
||||
// The birth session's OWN evaluations only — a later
|
||||
// session's observation must not leak in here.
|
||||
observation("n1", "c1", "shaky", "2026-09-20T12:00:00.000Z"),
|
||||
observation("n1", "c1", "solid", "2026-09-22T12:00:00.000Z"),
|
||||
observation("n1", "c2", "resisted", "2026-09-23T12:00:00.000Z"),
|
||||
observation("n2", "c1", "not yet engaged", "2026-09-21T12:00:00.000Z"),
|
||||
],
|
||||
},
|
||||
);
|
||||
const tools = createProberTools({ ...baseCtx, chatStore, graphStore });
|
||||
const out = (await tools.list_propositions!.execute!({ session: "c1" }, { toolCallId: "tc" })) as string;
|
||||
assert.match(out, /2 proposition\(s\) born in this session:/);
|
||||
assert.match(out, /- Water cycle — solid \(as of 2026-09-22T12:00:00.000Z\)/);
|
||||
assert.match(out, /- Evaporation — not yet engaged \(as of 2026-09-21T12:00:00.000Z\)/);
|
||||
// Never the other session's word, never the statement text.
|
||||
assert.ok(!out.includes("resisted"));
|
||||
assert.ok(!out.includes("statement of"));
|
||||
});
|
||||
|
||||
it("a proposition with no observation reads 'never evaluated', undated", async () => {
|
||||
const { chatStore, graphStore } = fakeStores([node("n1", "Water cycle", [pt("c1")])], {
|
||||
events: [],
|
||||
});
|
||||
const tools = createProberTools({ ...baseCtx, chatStore, graphStore });
|
||||
const out = (await tools.list_propositions!.execute!({ session: "c1" }, { toolCallId: "tc" })) as string;
|
||||
assert.match(out, /- Water cycle — never evaluated/);
|
||||
assert.ok(!out.includes("(as of"));
|
||||
});
|
||||
|
||||
it("an unknown session id — honest redirect, not a throw", async () => {
|
||||
const { chatStore, graphStore } = fakeStores([node("n1", "Water cycle", [pt("c1")])]);
|
||||
const tools = createProberTools({ ...baseCtx, chatStore, graphStore });
|
||||
const out = (await tools.list_propositions!.execute!({ session: "no-such" }, { toolCallId: "tc" })) as string;
|
||||
assert.equal(out, 'No propositions born in session "no-such". Call list_sessions for the current ids.');
|
||||
});
|
||||
});
|
||||
@@ -13,7 +13,10 @@ function userText(id: string, text: string): TeacherUIMessage {
|
||||
return { id, role: "user", parts: [{ type: "text", text }] };
|
||||
}
|
||||
function steer(id: string): TeacherUIMessage {
|
||||
return { id, role: "user", parts: [{ type: "data-graph-context", data: { chatId: "c", budget: 1024, tokens: 1, propositions: [] } }] };
|
||||
// Wave 7 killed the graph-context injection; its marker part is gone.
|
||||
// The steer-shaped user message here exercises mergeAdopted's
|
||||
// non-text user parts path with the handoff marker instead.
|
||||
return { id, role: "user", parts: [{ type: "data-probe-handoff", data: { chatId: "c" } }] };
|
||||
}
|
||||
function assistant(id: string): TeacherUIMessage {
|
||||
return { id, role: "assistant", parts: [{ type: "text", text: "reply" }] };
|
||||
|
||||
@@ -117,10 +117,10 @@ describe("the model catalog projection", () => {
|
||||
const partial = catalogDefaultsFor({ model: { provider: "openrouter", id: "qwen/qwen3.8-flash" } }, strong);
|
||||
assert.ok(partial);
|
||||
assert.equal(partial!.thinkingLevel, "low");
|
||||
// graphContextTokens-only record: model untouched
|
||||
assert.deepEqual(catalogDefaultsFor({ graphContextTokens: 512 }, strong), {
|
||||
// thinkingLevel-only record: model untouched
|
||||
assert.deepEqual(catalogDefaultsFor({ thinkingLevel: "minimal" }, strong), {
|
||||
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
|
||||
thinkingLevel: "low",
|
||||
thinkingLevel: "minimal",
|
||||
});
|
||||
// chat.model with NO strong slot: the conversation runs on it alone
|
||||
assert.deepEqual(
|
||||
|
||||
@@ -86,35 +86,4 @@ describe("record-adapter: system message flattening", () => {
|
||||
assert.equal(out.length, 0, "no model face at all — skipped");
|
||||
});
|
||||
|
||||
it("the graph-context steer flattens to the canonical prior-knowledge block", () => {
|
||||
const out = recordToPiMessages([
|
||||
userParts([
|
||||
{
|
||||
type: "data-graph-context",
|
||||
id: "ctx1",
|
||||
data: {
|
||||
chatId: "chat_1",
|
||||
budget: 1024,
|
||||
tokens: 64,
|
||||
propositions: [
|
||||
{ id: "n1", statement: "Water boils at 100°C.", level: "solid" },
|
||||
{ id: "n2", statement: "Evaporation cools things.", level: null },
|
||||
],
|
||||
},
|
||||
},
|
||||
]),
|
||||
]);
|
||||
assert.equal(out.length, 1, "one user message — the marker part is the model face");
|
||||
const got = texts(out);
|
||||
assert.equal(got.length, 1);
|
||||
assert.match(got[0]!, /Prior-knowledge context/);
|
||||
assert.match(got[0]!, /\[solid\] Water boils at 100°C\./);
|
||||
});
|
||||
|
||||
it("a malformed graph-context part has no model face — the message is skipped wholesale", () => {
|
||||
const out = recordToPiMessages([
|
||||
userParts([{ type: "data-graph-context", id: "ctx2", data: { chatId: "chat_1" } }]),
|
||||
]);
|
||||
assert.equal(out.length, 0, "parse-don't-cast: no validated data, no face");
|
||||
});
|
||||
});
|
||||
@@ -166,17 +166,6 @@ describe("activity section render", () => {
|
||||
assert.match(html, /activity-total-cost[^>]*>—</);
|
||||
});
|
||||
|
||||
it("renders a graph-context job as 'Graph context' — the dock's fourth kind", () => {
|
||||
const html = renderToStaticMarkup(
|
||||
<ActivitySection
|
||||
feed={{ jobs: [job({ id: "jctx", kind: "graph-context", activity: "selecting propositions", title: "Water cycle" })], extractions: [] }}
|
||||
/>,
|
||||
);
|
||||
assert.match(html, /data-testid="activity-blob-job"/);
|
||||
assert.match(html, /Graph context/);
|
||||
assert.match(html, /selecting propositions/);
|
||||
assert.match(html, /data-testid="activity-abort"/, "END kills the injection like any job");
|
||||
});
|
||||
|
||||
it("shows total running cost when jobs are priced", () => {
|
||||
const usage = {
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { GraphContextList, GraphContextMarker } from "../components/graph-context-marker.tsx";
|
||||
import type { GraphContextData, TeacherUIMessage } from "../src/core/index.ts";
|
||||
|
||||
// The knowledge-context delivery marker (owner observability ask, 2026-09-15):
|
||||
// a quiet line that opens the delivered propositions, in delivery order,
|
||||
// each with its folded understanding level. Static markup can't click — the
|
||||
// closed line and the popover body are pinned separately.
|
||||
|
||||
const DATA: GraphContextData = {
|
||||
chatId: "c1",
|
||||
budget: 1024,
|
||||
tokens: 537,
|
||||
propositions: [
|
||||
{ id: "n1", statement: "Water boils at 100°C at standard pressure.", level: "solid" },
|
||||
{ id: "n2", statement: "Modal realism takes possible worlds as concrete.", level: "shaky" },
|
||||
{ id: "n3", statement: "An untagged proposition has no folded level.", level: null },
|
||||
],
|
||||
};
|
||||
|
||||
function markerMessage(): TeacherUIMessage {
|
||||
return { id: "steer_ctx_c1", role: "user", parts: [{ type: "data-graph-context", data: DATA }] };
|
||||
}
|
||||
|
||||
describe("GraphContextMarker", () => {
|
||||
it("renders the quiet summary line with token + proposition counts", () => {
|
||||
const html = renderToStaticMarkup(<GraphContextMarker message={markerMessage()} />);
|
||||
assert.ok(html.includes("Graph context — ~537"));
|
||||
assert.ok(html.includes("3 propositions"));
|
||||
assert.ok(html.includes('data-testid="graph-context"'));
|
||||
assert.ok(html.includes("<button"), "the line is clickable");
|
||||
});
|
||||
|
||||
it("a malformed part still renders the generic line (parse-don't-cast)", () => {
|
||||
const html = renderToStaticMarkup(
|
||||
<GraphContextMarker message={{ parts: [{ type: "data-graph-context", data: { nope: true } }] }} />,
|
||||
);
|
||||
assert.ok(html.includes("Graph context</"));
|
||||
assert.ok(!html.includes("Graph context —"), "no counts on a malformed part");
|
||||
});
|
||||
});
|
||||
|
||||
describe("GraphContextList — delivery order is the render order", () => {
|
||||
it("lists propositions 1..n with their level tags; null level gets no tag", () => {
|
||||
const html = renderToStaticMarkup(<GraphContextList data={DATA} />);
|
||||
const first = html.indexOf("Water boils");
|
||||
const second = html.indexOf("Modal realism");
|
||||
const third = html.indexOf("untagged");
|
||||
assert.ok(first !== -1 && second !== -1 && third !== -1, "all three statements render");
|
||||
assert.ok(first < second && second < third, "delivery order preserved");
|
||||
assert.ok(html.includes("solid") && html.includes("shaky"), "level tags render");
|
||||
// The null-level row: a bare <span> of text, no level chip before it.
|
||||
const nullRow = html.slice(html.indexOf("untagged") - 120, html.indexOf("untagged"));
|
||||
assert.ok(!nullRow.includes(">resisted<") && !nullRow.includes(">accepted<"));
|
||||
assert.ok(html.includes("537 tok of 1024 budget"), "budget + delivered tokens visible");
|
||||
});
|
||||
});
|
||||
@@ -111,51 +111,6 @@ describe("DraftShell — the sessionless surface", () => {
|
||||
assert.ok(cta !== -1 && blob !== -1 && cta < blob, "CTA above the blob");
|
||||
});
|
||||
|
||||
it("renders the knowledge-context slider in the stats bar's left slot, at the decided default (absent settings = on, 1024)", () => {
|
||||
const html = renderToStaticMarkup(
|
||||
<DraftShell
|
||||
busy={false}
|
||||
text=""
|
||||
onChange={() => {}}
|
||||
onSend={() => {}}
|
||||
settings={undefined}
|
||||
onSettingsSaved={() => {}}
|
||||
/>,
|
||||
);
|
||||
assert.ok(html.includes('data-testid="graph-context-control"'));
|
||||
// INSIDE the stats row (the session-data slot), not a strip above the
|
||||
// composer: the control renders after the stats bar opens.
|
||||
assert.ok(
|
||||
html.indexOf('data-testid="stats-bar"') < html.indexOf('data-testid="graph-context-control"'),
|
||||
"the slider is the stats bar's left slot",
|
||||
);
|
||||
// The DISCRETE slider (owner, 2026-09-15): 6 positions (off + powers of
|
||||
// four from 64), not a continuous 0-4096 dial.
|
||||
assert.ok(html.includes('min="0"') && html.includes('max="5"') && html.includes('step="1"'));
|
||||
assert.ok(html.includes('value="3"'), "default 1024 = stop index 3");
|
||||
// The readout: "tok" abbreviated, but the FULL number (owner, 2026-09-15:
|
||||
// no k-rounding — 1024 reads as 1024).
|
||||
assert.ok(html.includes("1024 tok"), "the readout shows the default budget, not 0 — default is ON");
|
||||
assert.ok(html.includes("Graph context"), "the control is labeled");
|
||||
// Absent settings render the default — never 'off'.
|
||||
assert.ok(!html.includes(">off<"));
|
||||
});
|
||||
|
||||
it("an explicit 0 renders as off — the learner turned the graph off for this session", () => {
|
||||
const html = renderToStaticMarkup(
|
||||
<DraftShell
|
||||
busy={false}
|
||||
text=""
|
||||
onChange={() => {}}
|
||||
onSend={() => {}}
|
||||
settings={{ graphContextTokens: 0 }}
|
||||
onSettingsSaved={() => {}}
|
||||
/>,
|
||||
);
|
||||
assert.ok(html.includes('data-testid="graph-context-value"'));
|
||||
assert.ok(html.includes(">off<"));
|
||||
});
|
||||
|
||||
it("no chat id anywhere — nothing in the markup references a session", () => {
|
||||
const html = renderToStaticMarkup(
|
||||
<DraftShell
|
||||
|
||||
@@ -1,118 +0,0 @@
|
||||
/**
|
||||
* Semantic topic clustering (the Louvain REPLACEMENT, DECIDED
|
||||
* 2026-09-13): average-linkage agglomerative clustering over the
|
||||
* embedding ledger — related TOPICS, not reasoning chains. Pure and
|
||||
* deterministic, so these tests are exact.
|
||||
*/
|
||||
import { strict as assert } from "node:assert";
|
||||
import { describe, test } from "node:test";
|
||||
import { semanticClusters, SEMANTIC_SIM_THRESHOLD, cosineSimilarity } from "../src/core/semantic-clusters.ts";
|
||||
|
||||
/** Two clearly-related one-hot-ish vectors (high cosine similarity). */
|
||||
function near(vec: number[], target: number[], amount = 0.2): number[] {
|
||||
let dot = 0;
|
||||
for (let i = 0; i < vec.length; i++) dot += vec[i]! * target[i]!;
|
||||
const mixed = vec.map((x, i) => x + amount * target[i]!);
|
||||
let mag = 0;
|
||||
for (const x of mixed) mag += x * x;
|
||||
mag = Math.sqrt(mag);
|
||||
return mixed.map((x) => x / mag);
|
||||
}
|
||||
|
||||
function axis(dim: number, k: number): number[] {
|
||||
return Array.from({ length: dim }, (_, i) => (i === k ? 1 : 0));
|
||||
}
|
||||
|
||||
describe("cosineSimilarity", () => {
|
||||
test("identical unit vectors → 1; orthogonal → 0; defensive normalization", () => {
|
||||
const a = axis(4, 0);
|
||||
const b = axis(4, 1);
|
||||
assert.ok(cosineSimilarity(a, a) > 0.999);
|
||||
assert.ok(Math.abs(cosineSimilarity(a, b)) < 1e-9);
|
||||
// Unnormalized twins still match.
|
||||
assert.ok(cosineSimilarity([3, 0], [5, 0]) > 0.999);
|
||||
});
|
||||
});
|
||||
|
||||
describe("semanticClusters", () => {
|
||||
test("empty ledger: zero clusters (nothing embedded yet)", () => {
|
||||
assert.deepEqual(semanticClusters({}), []);
|
||||
});
|
||||
|
||||
test("related vectors merge; distant ones stay separate (threshold)", () => {
|
||||
const dim = 8;
|
||||
const topicA = axis(dim, 0);
|
||||
const topicB = axis(dim, 1);
|
||||
const vectors = {
|
||||
a1: near(topicA, topicB, 0.1),
|
||||
a2: near(topicA, topicB, 0.15),
|
||||
a3: near(topicA, topicB, 0.05),
|
||||
b1: near(topicB, topicA, 0.1),
|
||||
b2: near(topicB, topicA, 0.12),
|
||||
};
|
||||
const clusters = semanticClusters(vectors);
|
||||
// Both topics form; average-linkage similarity within each is well
|
||||
// above the default threshold, across is well below.
|
||||
const big = clusters.filter((c) => c.members.length >= 2);
|
||||
assert.equal(big.length, 2);
|
||||
assert.deepEqual(
|
||||
big.map((c) => c.members.sort()).sort((x, y) => x[0]!.localeCompare(y[0]!)),
|
||||
[["a1", "a2", "a3"], ["b1", "b2"]],
|
||||
);
|
||||
});
|
||||
|
||||
test("threshold is the granularity knob: higher splits, lower merges", () => {
|
||||
const dim = 6;
|
||||
const vectors = {
|
||||
x: near(axis(dim, 0), axis(dim, 1), 0.3),
|
||||
y: near(axis(dim, 1), axis(dim, 0), 0.3),
|
||||
};
|
||||
// Similarity ~0.72: merges below that, splits above.
|
||||
const sim = cosineSimilarity(vectors.x!, vectors.y!);
|
||||
assert.equal(semanticClusters(vectors, { similarityThreshold: sim - 0.01 }).length, 1);
|
||||
assert.equal(semanticClusters(vectors, { similarityThreshold: sim + 0.01 }).length, 2);
|
||||
});
|
||||
|
||||
test("stragglers stay singletons by default; attachStragglers forces membership", () => {
|
||||
const dim = 6;
|
||||
const topic = axis(dim, 0);
|
||||
const vectors = {
|
||||
t1: near(topic, axis(dim, 1), 0.1),
|
||||
t2: near(topic, axis(dim, 2), 0.1),
|
||||
t3: near(topic, axis(dim, 3), 0.1),
|
||||
loner: axis(dim, 5), // orthogonal: fits nothing
|
||||
};
|
||||
const loose = semanticClusters(vectors);
|
||||
const looseLoner = loose.find((c) => c.members.includes("loner"));
|
||||
assert.equal(looseLoner?.members.length, 1, "default: loner is a straggler");
|
||||
assert.equal(loose.find((c) => c.members.length >= 3)?.members.length, 3);
|
||||
|
||||
const allIn = semanticClusters(vectors, { attachStragglers: true });
|
||||
assert.equal(allIn.length, 1, "attach: the loner joins the nearest topic");
|
||||
assert.ok(allIn[0]!.members.includes("loner"));
|
||||
});
|
||||
|
||||
test("members sorted; clusters ordered by size then id (deterministic)", () => {
|
||||
const dim = 4;
|
||||
const vectors = {
|
||||
solo: axis(dim, 3),
|
||||
p1: near(axis(dim, 0), axis(dim, 1), 0.05),
|
||||
p2: near(axis(dim, 0), axis(dim, 2), 0.05),
|
||||
q1: near(axis(dim, 1), axis(dim, 0), 0.05),
|
||||
q2: near(axis(dim, 1), axis(dim, 2), 0.05),
|
||||
};
|
||||
const a = semanticClusters(vectors);
|
||||
const b = semanticClusters(vectors);
|
||||
assert.deepEqual(a, b, "same input → identical partition");
|
||||
for (let i = 1; i < a.length; i++) {
|
||||
assert.ok(a[i - 1]!.members.length >= a[i]!.members.length, "size-descending");
|
||||
}
|
||||
for (const c of a) {
|
||||
assert.deepEqual(c.members, [...c.members].sort(), "members sorted");
|
||||
}
|
||||
});
|
||||
|
||||
test("default threshold is the swept real-graph value", () => {
|
||||
assert.equal(SEMANTIC_SIM_THRESHOLD, 0.6);
|
||||
});
|
||||
});
|
||||
@@ -20,7 +20,6 @@ import type { ChatSettings } from "../src/core";
|
||||
const CHAT_FIXTURE: ChatSettings = {
|
||||
model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" },
|
||||
thinkingLevel: "low",
|
||||
graphContextTokens: 1024,
|
||||
};
|
||||
|
||||
// Snapshot any pre-existing files; restore in after() so the worktree's
|
||||
|
||||
@@ -53,7 +53,7 @@ describe("sanitizeUserSettings", () => {
|
||||
const record = {
|
||||
version: 1,
|
||||
general: { autoUpdate: false },
|
||||
chat: { model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" }, thinkingLevel: "low", graphContextTokens: 256 },
|
||||
chat: { model: { provider: "openrouter", id: "z-ai/glm-5.3-flash" }, thinkingLevel: "low" },
|
||||
agent: {
|
||||
models: {
|
||||
researcher: { model: { provider: "openrouter", id: "qwen/qwen3.8-flash" }, thinkingLevel: "high" },
|
||||
|
||||
Reference in New Issue
Block a user