feat: graph context injection — the session's prior knowledge, steered in at session start (vision-context-injection.md)

One shot per chat, fired at the first turn's onFinish: the session's
topic (its only typed message) is embedded as an EmbeddingGemma retrieval
query; the semantically closest graph propositions are selected under the
session's token budget (pure budget, no similarity threshold — the
decided doctrine) and delivered as a marker-only steer through the wake
pipeline, queuing behind pending questions like every sub-agent result.

- contracts: ChatSettings.graphContextTokens (default 1024, max 4096,
  step 128, 0 = off); GraphContextData + graphContextText; the
  graph-context JobKind + buildGraphContextSteer (id steer_ctx_<chatId>)
- core/graph-context.ts: brute-force cosine top-K, greedy chars/4 fill
  under budget, most-recent UnderstandingLevel fold, count cap 64,
  parse-don't-cast at the marker seam
- server: embedQuery (role-asymmetric query format, same warm singleton
  — vector-space parity by construction); the runner (startGraphContext-
  Injection one-shot gates; runGraphContextJob validates ledger parity);
  wake branches (chatIdOf/jobIdOf, finalizeJob done, recoverStaleJobs
  re-deliver or contextRespawn); abortJob kills context jobs SILENTLY
  (nothing was promised in the lesson); turn-service trigger seam
- adapter: data-graph-context flattens to the canonical block (malformed
  parts have no model face)
- UI: the budget slider (home dock + /chat/new draft, the session-data
  strip); the quiet SystemLine "~537 · 10 propositions"; dock kind
  "Knowledge context"; model-picker PUT now spreads settings (a model
  pick can no longer clear the sibling override)

Live-smoked end to end on the real embedder + real GLM: seeded ledger
(10 propositions), session on "modal logic: possible worlds and the
necessity box" — job in the dock, queued behind the prober's question,
delivered on the answer, SystemLine in the transcript, wake turn reads
the block and continues the probe. Gates: tsc, lint, 459/459, build.
This commit is contained in:
amosblomqvist
2026-09-15 07:54:50 +03:00
parent ecb293a77d
commit 1d45d77303
25 changed files with 1443 additions and 28 deletions
+64 -1
View File
@@ -10,7 +10,7 @@ import type { UIMessage } from "ai";
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
import type { Phase3 } from "../phases.ts";
import type { PlanNodeStatus, PlanTree } from "./plan.ts";
import type { EmissionStats } from "./graph.ts";
import type { EmissionStats, UnderstandingLevel } from "./graph.ts";
// ─── Chat records (the session store's on-disk format) ───────────────────────
@@ -58,8 +58,23 @@ export interface ChatSettings {
/** pi thinking level — must be in the chosen model's supported list
* (getSupportedThinkingLevels; enforced by the settings route). */
thinkingLevel?: ThinkingLevel;
/** Graph context injection (vision-context-injection.md): the session's
* token budget for the prior-knowledge block — the top semantically
* closest propositions from the knowledge graph, injected once, at
* session start, as a steer. Absent = the default (on); 0 = off. The
* settings route enforces the range; the trigger (turn-service →
* subagents/graph-context.ts) is one-shot, so a mid-session change
* affects only sessions that haven't injected yet. */
graphContextTokens?: number;
}
/** The injection budget's decided knobs (2026-09-14): default 1024 (a
* meaningful, modest slice of prior knowledge), hard max 4096, slider
* step 128, 0 = off. */
export const GRAPH_CONTEXT_TOKENS_DEFAULT = 1024;
export const GRAPH_CONTEXT_TOKENS_MAX = 4096;
export const GRAPH_CONTEXT_TOKENS_STEP = 128;
// ─── Background-extraction usage (the money channel, extraction side) ────────
/** One pipeline stage's model usage — the stage's model + every step it
@@ -176,6 +191,54 @@ export const HANDOFF_TEXT =
"The probe is concluded — the learner assessment is recorded. " +
"Author the session's plan map now (call plan_init) and present it, then begin teaching.";
// ─── Graph context injection (vision-context-injection.md, 2026-09-14) ────────
/** One selected prior-knowledge proposition: similarity-ranked, annotated
* with the learner's CURRENT understanding level — the fold is MOST
* RECENT extraction instance (levels are per-instance, tied to moments in
* time; the latest evidence is the best estimate of current understanding).
* null = the node has no observation event at all. */
export interface GraphContextProposition {
id: string;
statement: string;
level: UnderstandingLevel | null;
}
/** The data-graph-context part payload: what the UI renders (the counts)
* AND the audit trail (the full block content — the transcript shows
* exactly what was injected, and when). The model-facing text is derived
* from this by graphContextText below — the adapter flattens the marker
* part to it (the custom-message scheme: no authored human speech). */
export interface GraphContextData {
chatId: string;
/** The session's budget at injection time (for the card's readout). */
budget: number;
/** Estimated tokens of the injected block — chars/4 heuristic. */
tokens: number;
/** The selected propositions, similarity-descending. */
propositions: GraphContextProposition[];
}
/** The model-facing block text — single-sourced (the adapter, the tests,
* and reality can't drift). The level word travels with each line: the
* closing gloss tells the agent what to do with each. */
export function graphContextText(data: GraphContextData): string {
const n = data.propositions.length;
if (n === 0) return "";
const lines = data.propositions.map(
(p) => `- ${p.level ? `[${p.level}] ` : ""}${p.statement}`,
);
return [
`Prior-knowledge context — what this learner already knows (from their knowledge graph, ` +
`${n} proposition${n === 1 ? "" : "s"}, ~${data.tokens} tokens):`,
"",
...lines,
"",
"Treat this as reliable prior knowledge: build on what is solid, firm up what is shaky, " +
"and teach around anything resisted. Do not re-teach solid ground.",
].join("\n");
}
export interface PlanBlockData {
/** Only "full" survives (the 2026-09-11 pivot): the plan renders IN
* FLOW once, at authoring. The neighborhood fragments are retired —
+35 -3
View File
@@ -7,11 +7,11 @@
* of "ai" (safe everywhere: server, client, tests). See docs/vision.md.
*/
import type { UIMessage } from "ai";
import type { CallUsage } from "./chat.ts";
import type { CallUsage, GraphContextData } from "./chat.ts";
// ─── Async sub-agent jobs (vision §2.5; plan.md Wave 0 protocol) ────────────
export type JobKind = "research" | "demo" | "visual";
export type JobKind = "research" | "demo" | "visual" | "graph-context";
export type JobStatus = "running" | "done" | "error";
export interface JobRecord {
@@ -263,7 +263,11 @@ export interface AbortSteerMessage extends UIMessage {
}
/** Anything the wake pipeline may queue and deliver. */
export type SteerMessage = SubagentSteerMessage | AbortSteerMessage | ProbeHandoffSteerMessage;
export type SteerMessage =
| SubagentSteerMessage
| AbortSteerMessage
| ProbeHandoffSteerMessage
| GraphContextSteerMessage;
/** Whether a result is the failure shape (html/svg empty, failure brief). */
export function isSubagentFailure(kind: JobKind, result: ResearchResult | DemoResult | VisualResult): boolean {
@@ -380,6 +384,34 @@ export function buildProbeHandoffSteer(chatId: string): ProbeHandoffSteerMessage
};
}
// ─── the graph-context steer (vision-context-injection.md) ───────────────────
//
// The injection's delivery: same wake pipeline, same gates — it queues
// behind a pending quiz and rides the turn after the learner's answer, or
// fires in the pump's idle window. Marker-only (the custom-message scheme):
// no text part; the adapter flattens data-graph-context to graphContextText
// (core/contracts/chat.ts owns the wording). One per chat, ever — the id is
// deterministic per chat, so re-delivery is idempotent.
export interface GraphContextSteerMessage extends UIMessage {
id: `steer_ctx_${string}`;
role: "user";
metadata: { graphContext: { chatId: string; jobId: string } };
parts: [{ type: "data-graph-context"; data: GraphContextData }];
}
export function buildGraphContextSteer(
meta: { chatId: string; jobId: string },
data: GraphContextData,
): GraphContextSteerMessage {
return {
id: `steer_ctx_${meta.chatId}`,
role: "user",
metadata: { graphContext: meta },
parts: [{ type: "data-graph-context", data }],
};
}
/** True when the transcript already carries this steer — idempotent
* delivery for ANY steer kind: every steer id is deterministic per its
* event (steer_<jobId> for jobs, steer_probe_<chatId> for the handoff). */