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
+19 -2
View File
@@ -1,6 +1,6 @@
import { getChat, validateChatSettings, stores } from "@/src/server";
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
import type { ChatSettings } from "@/src/core";
import { GRAPH_CONTEXT_TOKENS_MAX, type ChatSettings } from "@/src/core";
/**
* The per-chat model control (model-control wave): replace-whole settings —
@@ -16,7 +16,7 @@ export async function PUT(req: Request, { params }: { params: Promise<{ id: stri
if (!getChat(id)) return Response.json({ error: "Not found" }, { status: 404 });
const body = (await req.json().catch(() => null)) as
| { model?: { provider?: unknown; id?: unknown }; thinkingLevel?: unknown }
| { model?: { provider?: unknown; id?: unknown }; thinkingLevel?: unknown; graphContextTokens?: unknown }
| null;
if (!body || typeof body !== "object") {
return Response.json({ error: "Invalid JSON body" }, { status: 400 });
@@ -39,6 +39,23 @@ export async function PUT(req: Request, { params }: { params: Promise<{ id: stri
}
settings.thinkingLevel = body.thinkingLevel as ThinkingLevel;
}
// Graph context injection budget (vision-context-injection.md): integer
// tokens, 0 (off) to GRAPH_CONTEXT_TOKENS_MAX, no silent clamping — the
// slider is the only writer, and it only ever sends multiples of the step.
if (body.graphContextTokens !== undefined) {
if (
typeof body.graphContextTokens !== "number" ||
!Number.isInteger(body.graphContextTokens) ||
body.graphContextTokens < 0 ||
body.graphContextTokens > GRAPH_CONTEXT_TOKENS_MAX
) {
return Response.json(
{ error: `graphContextTokens must be an integer between 0 and ${GRAPH_CONTEXT_TOKENS_MAX}` },
{ status: 400 },
);
}
settings.graphContextTokens = body.graphContextTokens;
}
const verdict = validateChatSettings(settings);
if (!verdict.ok) {