Wave 7 chunk B: remove the embeddings machinery — the ladder collapses

The owner's wave-7 ruling: embeddings die entirely (git history is the
stash); clustering is birth-session derived (chunk A, 950ddfd), labels
are session names, and the prober's graph tools replace injection.

- settings: GraphSettings collapses to { enabled } — embedding,
  embeddingModel, clustering, labeling all die. Migration story: stale
  record keys are UNREAD and dropped by the sanitizer's reconstruction;
  the next graph-section PUT (replace-whole) persists the collapsed
  shape. The settings-panel GraphSection renders one master row.
- deleted: src/server/embeddings.ts (adapters, resolveEmbeddingPortFor,
  embedNewNodes, embeddingPort, embeddingModelCatalog), the
  GET /api/models/embeddings route, scripts/embed-graph.ts, tests for
  all three, and the /api/models/embeddings picker surfaces in the UI.
- prices: EMBEDDING_PRICES overlay + callCost fallback die. cost-summary:
  the embedding stage fold dies — back to three stages (extraction/
  audit/naming) per the owner ruling; historical stages.embedding spend
  in old run records is simply not displayed.
- ports: EmbeddingPort/EmbeddingResult/EmbeddingTask die. The embeddings
  LEDGER surface (readEmbeddings/writeEmbeddings + EmbeddingsLedger +
  EMBEDDINGS_FILE + graph-deletion's vector pruning) STAYS — inert data,
  kept only so graph-context.ts compiles until Teacher's pass removes
  it; the seam dies in the follow-up interlude.
- chat-handler (approved cross-lane unwiring, wave-6 precedent): the
  extraction pipeline's embedder becomes a documented no-op (nothing
  embedded, nothing spent, ledger untouched) until Teacher's pass
  removes the phase; the graph-context injection dispatch gate, the
  graphContext deps wiring, the setContextRespawn seam, and
  turn-service's triggerGraphContext are gone — injection is dead
  code pending Teacher's file deletions.
- scripts: backfill-graph/import-notes pass the same no-op embedder;
  replay-layout derives clusters from birthSessionPartition (the same
  partition the real settle uses) instead of the embeddings ledger.
- settings-body: parseGraphSettingsBody reads the master switch only —
  old-shape keys in a body are ignored, not rejected; bad shapes 400.
- next.config: the @huggingface tracing-exclusion guards die with the
  last reference.

Cross-lane flags: extraction.ts/graph-context.ts keep stale embed
comments + the embed phase until Teacher's full pass (their parallel
interlude owns those files — zero overlap kept by design);
semantic-clusters.ts + its test stay one more round (extraction.test.ts,
Teacher's file, still imports it — they delete it with the namer cases);
ChatSettings.graphContextTokens + the graph-context-control UI stay
until Teacher+UI.

Gates: tsc clean, eslint clean, 885/885 tests, build compiles.
Live-verified on the port-3004 dev tree: PUT /api/settings/graph with
dead keys collapses on write, bad shapes 400, GET reads {enabled: true},
/api/models/embeddings 404s.
This commit is contained in:
amosblomqvist
2026-09-23 15:02:55 +03:00
parent 0fcffdffaf
commit c86430e8bf
26 changed files with 120 additions and 1823 deletions
+8 -21
View File
@@ -55,12 +55,13 @@ export interface GraphCostSummary {
totalDollars: number;
/** Runs where EVERY stage was unpriced (totalDollars excludes them). */
unpricedRuns: number;
/** The billable pipeline stages (wave 6: the embedder became one —
* provider embeddings cost money; merger is the only free stage). */
/** The billable pipeline stages. (Wave 7, owner ruling 2026-09-23:
* back to three — the embedding stage is gone with the provider
* embedder; historical records' stages.embedding spend is simply not
* displayed.) */
stages: {
extraction: StageCost;
audit: StageCost;
embedding: StageCost;
naming: StageCost;
};
/** Newest first, capped at `recentCap` (default 10). */
@@ -115,7 +116,6 @@ function mergeTokens(into: CallUsage, steps: readonly CallUsage[]): void {
export function graphCostSummary(input: CostSummaryInput): GraphCostSummary {
const extraction = emptyStage();
const audit = emptyStage();
const embedding = emptyStage();
const naming = emptyStage();
const runs: RecentRun[] = [];
@@ -134,25 +134,12 @@ export function graphCostSummary(input: CostSummaryInput): GraphCostSummary {
if (audDollars === null) audit.unpricedRuns++;
else audit.dollars += audDollars;
// Wave 6: the embedding tail — present only when an embedding model
// ran (absent stage = the run embedded nothing, never "free").
let embDollars: number | null = null;
let embSteps: CallUsage[] = [];
if (stages.embedding) {
embedding.runs++;
embSteps = stages.embedding.steps;
mergeTokens(embedding.tokens, embSteps);
embDollars = stageDollars(stages.embedding.model, embSteps);
if (embDollars === null) embedding.unpricedRuns++;
else embedding.dollars += embDollars;
}
// Run-level: null only when EVERY stage is unpriced.
const allStages: Array<number | null> = [extDollars, audDollars, embDollars];
const allStages: Array<number | null> = [extDollars, audDollars];
const runDollars = allStages.every((d) => d === null)
? null
: allStages.reduce<number>((a, d) => a + (d ?? 0), 0);
const allSteps = [...stages.extractor.steps, ...stages.auditor.steps, ...embSteps];
const allSteps = [...stages.extractor.steps, ...stages.auditor.steps];
runs.push({
startedAt: run.startedAt,
at: run.at,
@@ -195,9 +182,9 @@ export function graphCostSummary(input: CostSummaryInput): GraphCostSummary {
// A run is "unpriced" when it billed nothing countable at all.
const unpricedRuns = runs.filter((r) => r.dollars === null).length;
return {
totalDollars: extraction.dollars + audit.dollars + embedding.dollars + naming.dollars,
totalDollars: extraction.dollars + audit.dollars + naming.dollars,
unpricedRuns,
stages: { extraction, audit, embedding, naming },
stages: { extraction, audit, naming },
runs: capped,
};
}
+8 -49
View File
@@ -128,11 +128,15 @@ export interface GraphStore {
/** Replace the whole ledger (the settle is a full-graph computation). */
writePositions(positions: Record<string, Position>): void;
/** The embedding ledger (config.EMBEDDINGS_FILE). WRITTEN ONLY by
* the embedder (server/embeddings.ts) after a merge — read-only for
* clustering consumers (snapshot, settle, namer). */
/** The embeddings ledger (config.EMBEDDINGS_FILE) — INERT DATA since
* wave 7 (owner ruling 2026-09-23: the provider embedder is dead;
* clustering is birth-session, labels are session names). Kept ONLY
* because the graph-context injection (agent band, Teacher's pass
* removes it) still reads it for vector-space staleness — the seam
* dies with it in the follow-up interlude. graph-deletion keeps
* pruning dead vectors meanwhile (harmless housekeeping). */
readEmbeddings(): EmbeddingsLedger;
/** Replace the whole ledger (embedding is a full-batch computation). */
/** Replace the whole ledger (graph-deletion's vector pruning only). */
writeEmbeddings(ledger: EmbeddingsLedger): void;
/** THE DESTRUCTIVE SEAM (session-deletion wave 6, owner 2026-09-22 —
@@ -244,48 +248,3 @@ export interface AuthStore {
deleteAuthVar(name: string): void;
}
// ─── EmbeddingPort — BYOK provider embeddings (wave 6, 2026-09-23) ───────────
/**
* The provider-embedding seam (the local transformers embedder died with
* this wave): vectors come from the user's keyed provider — OpenRouter,
* OpenAI, or Google — named by graph.embeddingModel ("provider:id", null =
* honestly off: embedding/clustering/injection skip with an info log).
*
* Owner decisions (2026-09-23) baked in:
* - The adapters are resolved FRESH per call from the setting + the auth
* store (key rotations land immediately — the search port's treatment).
* - Vectors are stored as the provider returns them (default dims, no
* truncation, no normalization); the ledger is self-describing on
* model + dim, and any model change voids it (full re-embed).
* - Usage (prompt tokens) flows back to the money channel: adapters
* report it, the caller records it as a CallUsage step.
*
* Pure types only (browser-safe): the adapters are server-side
* (src/server/embeddings.ts) and arrive as injected dependencies.
*/
/** Which side of retrieval the text is — providers bill identically but
* quality-optimize differently (OpenRouter's input_type, Gemini's
* taskType); every input in one call shares the task. */
export type EmbeddingTask = "document" | "query";
/** One embedding batch's result: vectors in input order, the model's
* identity + dim (the ledger stamps), and the provider-reported prompt
* token count (undefined = provider didn't report; cost stays honest
* "—" via the existing unpriced path). */
export interface EmbeddingResult {
vectors: number[][];
/** The full "provider:id" slug — ledger/cost keys use it verbatim. */
model: string;
dim: number;
promptTokens?: number;
}
/** A live provider embedding adapter. `embed` throws on failure (adapters
* map HTTP errors to clear Error messages); callers treat failure as
* non-fatal per the extraction contract. */
export interface EmbeddingPort {
model: string;
embed(texts: readonly string[], task: EmbeddingTask): Promise<EmbeddingResult>;
}
+1 -26
View File
@@ -2752,30 +2752,6 @@ export const CONTEXT_LIMITS: Record<string, number> = {
// ─── Pure helpers (folds + cost inference) ───────────────────────────────────
// ─── Embedding prices (wave 6, 2026-09-23) ─────────────────────────────────
/** Hand-maintained BYOK embedding prices, USD per 1M INPUT tokens — the
* sync script never touches this block (pi-ai's catalogs carry no
* embedding models, so the generated PRICES table has none of these).
* VERIFIED against provider pricing pages 2026-09-23: OpenAI's model
* docs ($0.02 3-small / $0.13 3-large / $0.10 ada-002), Google's pricing
* page ($0.15 gemini-embedding-001 / $0.20 Gemini Embedding 2 text),
* OpenRouter passes provider pricing through. output: 0 — embeddings
* emit no output tokens. Unlisted models stay unpriced ("—", never
* zero); tokens still get captured. */
export const EMBEDDING_PRICES: Record<string, ModelPrice> = {
"openai:text-embedding-3-small": { input: 0.02, output: 0 },
"openai:text-embedding-3-large": { input: 0.13, output: 0 },
"openai:text-embedding-ada-002": { input: 0.1, output: 0 },
"google:gemini-embedding-001": { input: 0.15, output: 0 },
"google:gemini-embedding-2": { input: 0.2, output: 0 },
"openrouter:openai/text-embedding-3-small": { input: 0.02, output: 0 },
"openrouter:openai/text-embedding-3-large": { input: 0.13, output: 0 },
"openrouter:openai/text-embedding-ada-002": { input: 0.1, output: 0 },
"openrouter:google/gemini-embedding-001": { input: 0.15, output: 0 },
};
/** Sum a list of per-step usages into one CallUsage. A field stays
* undefined when EVERY step left it unreported — absent is not zero. */
export function sumSteps(steps: readonly CallUsage[]): CallUsage {
@@ -2797,8 +2773,7 @@ export function callCost(
usage: { inputTokens?: number; outputTokens?: number; cacheReadTokens?: number; cacheWriteTokens?: number },
model: string,
): number | null {
// Wave 6: the hand-maintained embedding overlay backs the lookup too.
const price = PRICES[model] ?? EMBEDDING_PRICES[model];
const price = PRICES[model];
if (!price) return null;
if (usage.inputTokens === undefined || usage.outputTokens === undefined) return null;
const cacheRead = usage.cacheReadTokens ?? 0;
+7 -18
View File
@@ -201,13 +201,10 @@ export function resolveSettings(record: UserSettings | null): EffectiveSettings
},
},
graph: {
// Wave 7 collapse (owner ruling 2026-09-23): the master switch ONLY —
// the embedding/clustering/labeling rungs and the BYOK embeddingModel
// are gone (stale record keys are simply never read here).
enabled: graph.enabled ?? FACTORY_SETTINGS.graph.enabled,
embedding: graph.embedding ?? FACTORY_SETTINGS.graph.embedding,
clustering: graph.clustering ?? FACTORY_SETTINGS.graph.clustering,
labeling: graph.labeling ?? FACTORY_SETTINGS.graph.labeling,
// BYOK (wave 6): null is the factory floor and a VALID resolved
// value — ?? only fills the absent case, null passes through.
embeddingModel: graph.embeddingModel ?? FACTORY_SETTINGS.graph.embeddingModel,
},
};
}
@@ -216,23 +213,13 @@ export function resolveSettings(record: UserSettings | null): EffectiveSettings
/**
* The WRITE channel gate (extraction + its auditor): dies with the master
* switch. Used by the chat-handler's post-turn trigger; deeper pipeline
* consumers (embedder, clustering, labeling — later waves) gate on the
* deeper rungs the same way.
* switch. Used by the chat-handler's post-turn trigger. (Wave 7: the master
* switch is the ONLY graph setting left — the section collapsed.)
*/
export function extractionEnabled(graph: Required<GraphSettings>): boolean {
return graph.enabled;
}
/**
* The READ channel gate (graph-context injection): needs the master switch
* AND the embedding rung — injection is a vector query, so a graph without
* embeddings has nothing to inject.
*/
export function graphContextEnabled(graph: Required<GraphSettings>): boolean {
return graph.enabled && graph.embedding;
}
/**
* The PROBER GRAPH TOOLS gate (wave 7, owner ruling 2026-09-23): the
* toggle is an agent-capability switch (it gates tool registration), but
@@ -246,3 +233,5 @@ export function graphToolsEnabled(effective: {
}): boolean {
return effective.graph.enabled && (effective.agent.graphTools.enabled ?? true);
}
+14 -31
View File
@@ -249,23 +249,14 @@ export interface AppearanceSettings {
* meaningful when its parent is on (the UI grays dependents out; stored
* values persist but resolve inert). All default true. */
export interface GraphSettings {
/** MASTER: off = no extraction, no auditor, no embedding, no
* graph-context injection, no clustering. Data stays stored. */
/** MASTER (and, since wave 7, ONLY): off = no extraction, no auditor.
* The wave-7 collapse (owner ruling 2026-09-23) deleted the three
* subordinate rungs — embedding/clustering/labeling — and the BYOK
* embeddingModel: clustering is the free birth-session partition
* derived on read (core/session-clusters.ts), labels ARE session
* names, and the prober graph tools read the ledgers directly — there
* is nothing left to configure. Data stays stored either way. */
enabled?: boolean;
/** Vector embedding + graph-context injection (consumes vectors even
* without clustering — hence its own rung). */
embedding?: boolean;
/** Cluster computation + settle (needs embedding). */
clustering?: boolean;
/** The LLM cluster-namer (needs clustering). */
labeling?: boolean;
/** BYOK embeddings (wave 6, 2026-09-23): the provider embedding model
* as a "provider:id" slug (openrouter:…, openai:…, google:…). null =
* no model — embedding/clustering/injection skip honestly (info log,
* graph stays textual); no factory floor exists. Keys live in the auth
* store, resolved fresh per call. A model CHANGE re-embeds everything
* (the ledger's staleness semantics). */
embeddingModel?: string | null;
}
/** THE record. Every section optional (delta); unknown top-level keys are
@@ -318,7 +309,7 @@ export const FACTORY_SETTINGS: FactorySettings = {
sessionNameSerif: true,
mdRendering: { headingWeight: 900, boldColor: null, italicColor: null },
},
graph: { enabled: true, embedding: true, clustering: true, labeling: true, embeddingModel: null },
graph: { enabled: true },
};
// ─── Sanitizer ─────────────────────────────────────────────────────────────
@@ -467,23 +458,15 @@ function sanitizeAppearance(raw: Record<string, unknown>): AppearanceSettings {
}
function sanitizeGraph(raw: Record<string, unknown>): GraphSettings {
// Wave 7 collapse (owner ruling 2026-09-23): the section is the master
// switch ONLY. The dead rungs' keys (embedding/clustering/labeling/
// embeddingModel) in existing records are simply UNREAD and DROPPED by
// this reconstruction — the migration story: the next graph-section
// write (PUT is replace-whole) persists the collapsed shape, and no
// active code ever looks at the stale fields.
const out: GraphSettings = {};
const enabled = boolOrDrop(raw.enabled);
if (enabled !== undefined) out.enabled = enabled;
const embedding = boolOrDrop(raw.embedding);
if (embedding !== undefined) out.embedding = embedding;
const clustering = boolOrDrop(raw.clustering);
if (clustering !== undefined) out.clustering = clustering;
const labeling = boolOrDrop(raw.labeling);
if (labeling !== undefined) out.labeling = labeling;
// BYOK embeddings (wave 6): null or a provider-qualified slug; anything
// else (bad type, bad shape) DROPS to absent — an invalid write never
// becomes a live model (the PUT parser 400s it first; this is the
// read-side belt-and-suspenders for hand-edited files).
if (raw.embeddingModel === null) out.embeddingModel = null;
else if (typeof raw.embeddingModel === "string" && /^\S+:\S+$/.test(raw.embeddingModel)) {
out.embeddingModel = raw.embeddingModel;
}
return out;
}
+8 -54
View File
@@ -4,23 +4,20 @@ import { createProberAgent, createTeacherAgent, type AgentBuildOptions, type Tea
import { piEngineForSpec, type PiEngine } from "../agent/models.ts";
import { createJobRunner } from "../agent/subagents/runner.ts";
import { createExtractionPipeline } from "../agent/subagents/extraction.ts";
import { runGraphContextJob, type GraphContextDeps } from "../agent/subagents/graph-context.ts";
import { embedNewNodes, embeddingPort } from "./embeddings.ts";
import type { ModelRole } from "../../config.ts";
import { claimSteers, deliverJobResult, setContextRespawn, setJobRespawn, setTurnRunner } from "./jobs.ts";
import { claimSteers, deliverJobResult, setJobRespawn, setTurnRunner } from "./jobs.ts";
import { recordToPiMessages } from "./pi/record-adapter.ts";
import { createPiTurnResponse } from "./pi/ui-stream.ts";
import { effectiveSettings, stores } from "./stores.ts";
import { searchAvailable, searchProvider } from "./search.ts";
import {
extractionEnabled,
graphContextEnabled,
resolveConversationSpec,
resolveRoleSpec,
} from "../core/settings-resolution.ts";
import { modelsNotConfigured } from "../core/settings.ts";
import { beginTurn, endTurn } from "./turn-liveness.ts";
import { persistTurn, triggerExtraction, triggerGraphContext, withMessageIds } from "./turn-service.ts";
import { persistTurn, triggerExtraction, withMessageIds } from "./turn-service.ts";
import type { CallUsage } from "../core/contracts.ts";
/**
@@ -63,9 +60,11 @@ const jobRunner = createJobRunner({
* (/api/graph/import) hit the same single-flight registry. */
export const extraction = createExtractionPipeline({
...stores,
// The provider-embedder batch (BYOK wave 6): post-merge embedding —
// resolves the model fresh per call; null model = info-skip inside.
embedder: embedNewNodes,
// Wave 7 (owner ruling 2026-09-23): embeddings are DEAD — the pipeline's
// embed seam becomes a no-op until Teacher's pass removes the phase
// outright. Nothing embedded, nothing spent, the ledger untouched
// (its data is inert anyway: nothing reads it but the dying injection).
embedder: async () => ({ embedded: 0, total: 0, skipped: "embeddings removed (wave 7)" }),
// BYOK (owner, 2026-09-22): the extraction stages resolve the chain —
// role override → slot → NOTHING — read FRESH per stage call; an empty
// chain flows through as undefined and the pipeline refuses the run
@@ -74,30 +73,6 @@ export const extraction = createExtractionPipeline({
resolveEngine(role),
});
/** Graph context injection (vision-context-injection.md): the runner's
* deps — same storage ports, the provider-embedder RESOLVER as the query
* seam (BYOK wave 6: fresh setting + key reads per run, so vector-space
* parity with the ledger is checked against the CURRENT model; a key or
* settings PUT between dispatch and spawn lands), and the wake pipeline
* as the delivery seam. */
export const graphContext: GraphContextDeps = {
chatStore: stores.chatStore,
jobStore: stores.jobStore,
graphStore: stores.graphStore,
embedder: () => {
const resolved = embeddingPort();
if (!resolved.ok) return null;
const port = resolved.port;
return {
model: port.model,
embedQuery: async (text: string) => {
const result = await port.embed([text], "query");
return { vector: result.vectors[0]!, promptTokens: result.promptTokens };
},
};
},
deliver: deliverJobResult,
};
// Wake-pipeline wiring (contracts.ts "THE ASYNC PROTOCOL" §4): the pipeline
// fires its server-side teacher turns through THIS composition root, and
@@ -125,7 +100,6 @@ function resolveEngine(role: ModelRole): PiEngine | undefined {
}
setJobRespawn((jobId) => void jobRunner.runJob(jobId));
setContextRespawn((jobId) => void runGraphContextJob(graphContext, jobId));
/** The message-shape gate the SDK's UIMessageValidator owned: a malformed
* body must fail BEFORE the stream exists — the path where no onFinish will
@@ -219,27 +193,7 @@ export async function handleChatRequest(
console.error(`[chat ${chatId}] write-ahead persist failed:`, err);
}
// THE GRAPH READ CHANNEL (vision-context-injection.md): one shot per
// chat, at session start — fired HERE, at turn start, not in onFinish:
// the injection's only input is the first typed message (the topic),
// which the write-ahead persist above just made durable, so it runs
// CONCURRENT with the prober's turn (embedding + cosine scan, no model
// call) and the dock shows the job the moment the session starts. Its
// steer then waits in the wake queue through the turn (waitForIdle) —
// exactly the same hold as a sub-agent result finishing mid-turn. All
// gates (exactly-one-text-message topic, budget, one-shot) live in the
// runner module; for every session past its start this is a no-op.
// Extraction stays in onFinish by contrast: it depends on quiz
// answers accumulated DURING the turn.
// GRAPH GATES (settings-storage wave, 2026-09-19): the read channel
// needs BOTH the master switch and the embedding rung (injection is a
// vector query); with graph off there is no context job at all.
// BYOK (wave 6): an unconfigured embedding model means no vectors to
// query — the dispatch skips honestly (the runner's own resolution is
// the belt-and-suspenders for respawn races).
if (graphContextEnabled(effective.graph) && effective.graph.embeddingModel !== null) {
triggerGraphContext(graphContext, chatId);
}
// MONEY CHANNEL (wave 4): one collector per request, fed by the agent's
// message_end events. Every model call's usage lands in the chat record's
-493
View File
@@ -1,493 +0,0 @@
/**
* BYOK PROVIDER EMBEDDINGS (wave 6, 2026-09-23): the local transformers
* embedder is dead — vectors now come from the user's keyed provider
* (OpenRouter / OpenAI / Google), named by graph.embeddingModel
* ("provider:id", null = honestly off; skip with an info log, the graph
* stays textual — owner ruling 2026-09-23).
*
* Owner decisions baked in:
* - NO factory model (BYOK, like the model slots): null is the floor.
* - Adapters resolve FRESH per call from the setting + the auth store
* (the search port's treatment — key rotations land immediately); a
* key missing for a SET model is an honest error, never a fallback.
* - Vectors stored as the provider returns them (default dims, no
* truncation, no normalization). The ledger is self-describing on
* model + dim; a model change voids it (full re-embed, minutes at
* current scale — the local embedder's same staleness rule).
* - Usage (prompt tokens) flows to the money channel: every API call
* reports one CallUsage step (outputTokens: 0 — embeddings emit no
* output; priced via the hand-maintained EMBEDDING_PRICES overlay).
*
* The adapters are injected-fetch dependencies so tests never touch the
* network; wire protocols VERIFIED against provider docs 2026-09-23:
* - OpenRouter: POST /api/v1/embeddings (Bearer), input_type
* search_document|search_query; catalog GET /api/v1/embeddings/models
* (VERIFIED live 2026-09-23 — the inverse path /api/v1/models/
* embeddings 404s).
* - OpenAI: POST /v1/embeddings (Bearer); catalog GET /v1/models.
* - Google: POST /v1beta/models/{m}:batchEmbedContents (x-goog-api-key),
* taskType RETRIEVAL_DOCUMENT|RETRIEVAL_QUERY; catalog GET /v1beta/models
* filtered on supportedGenerationMethods ∋ embedContent.
*/
import type { CallUsage, EmbeddingsLedger, GraphNode } from "../core/contracts.ts";
import type { ExtractionStageUsage } from "../core/contracts/chat.ts";
import type { EmbeddingPort, EmbeddingResult, EmbeddingTask } from "../core/ports.ts";
import { semanticClusters, SEMANTIC_SIM_THRESHOLD } from "../core/semantic-clusters.ts";
import { keyValue } from "./keys.ts";
import { effectiveSettings } from "./stores.ts";
/** The wire-callable shape the adapters close over (tests inject a stub). */
type FetchLike = (input: string, init: RequestInit) => Promise<Response>;
/** One provider embedding API call — kept small enough to never trip
* per-request input limits (propositions are tiny; 32 was the local
* embedder's batch and it stays). */
const EMBED_BATCH = 32;
/** Per-request timeout: a hung provider call must not hang an extraction
* run (the wave-1 hung-job lesson). */
const REQUEST_TIMEOUT_MS = 60_000;
/** The providers this wave ships + their auth vars (the curated key
* catalog, same ids the auth store and /api/keys use). */
const PROVIDER_KEYS = {
openrouter: "OPENROUTER_API_KEY",
openai: "OPENAI_API_KEY",
google: "GEMINI_API_KEY",
} as const;
export type EmbeddingProviderId = keyof typeof PROVIDER_KEYS;
/** "provider:id" → {provider, id}; null when malformed (no guessing). */
export function parseEmbeddingModelSlug(slug: string): { provider: EmbeddingProviderId; id: string } | null {
const colon = slug.indexOf(":");
if (colon <= 0 || colon === slug.length - 1) return null;
const provider = slug.slice(0, colon) as EmbeddingProviderId;
if (!(provider in PROVIDER_KEYS)) return null;
return { provider, id: slug.slice(colon + 1) };
}
// ─── Adapters (one per provider; pure injected-fetch factories) ──────────────
/** Shared: HTTP-error → clear Error (the search adapters' mapping). */
async function demandOk(resp: Response, label: string): Promise<void> {
if (resp.ok) return;
const body = await resp.text();
throw new Error(`${label} API ${resp.status}: ${body.slice(0, 200)}`);
}
/** Shared: normalize a provider's {embedding, index} rows to input order. */
function rowsInOrder(
data: Array<{ embedding?: unknown; index?: unknown }>,
count: number,
label: string,
): number[][] {
const byIndex = new Map<number, number[]>();
for (const row of data) {
if (
Array.isArray(row.embedding) &&
row.embedding.every((x) => typeof x === "number") &&
typeof row.index === "number"
) {
byIndex.set(row.index, row.embedding as number[]);
}
}
const out: number[][] = [];
for (let i = 0; i < count; i++) {
const vec = byIndex.get(i);
if (!vec) throw new Error(`${label} API returned ${byIndex.size}/${count} vectors`);
out.push(vec);
}
return out;
}
/** One API call's money-channel step: embeddings emit no output, cache
* nothing, reason never — the fields exist (CallUsage requires them) as
* absent-is-not-zero undefineds. */
function embeddingStep(promptTokens: number): CallUsage {
return {
inputTokens: promptTokens,
outputTokens: 0,
cacheReadTokens: undefined,
cacheWriteTokens: undefined,
reasoningTokens: undefined,
};
}
/** OpenRouter: POST https://openrouter.ai/api/v1/embeddings — Bearer auth,
* input_type hints the retrieval side (verified against the docs 2026-09-23). */
export function openrouterEmbeddingProvider(model: string, apiKey: string, fetchFn: FetchLike = fetch): EmbeddingPort {
const parsed = parseEmbeddingModelSlug(model);
if (!parsed) throw new Error(`invalid embedding model slug: ${model}`);
return {
model,
async embed(texts: readonly string[], task: EmbeddingTask): Promise<EmbeddingResult> {
const resp = await fetchFn("https://openrouter.ai/api/v1/embeddings", {
method: "POST",
headers: { "content-type": "application/json", authorization: `Bearer ${apiKey}` },
body: JSON.stringify({
model: parsed.id,
input: texts,
input_type: task === "query" ? "search_query" : "search_document",
}),
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
await demandOk(resp, "OpenRouter embeddings");
const data = (await resp.json()) as {
data?: Array<{ embedding?: unknown; index?: unknown }>;
usage?: { prompt_tokens?: number };
};
return {
vectors: rowsInOrder(data.data ?? [], texts.length, "OpenRouter embeddings"),
model,
dim: 0, // filled by the caller from the first vector (below)
promptTokens: data.usage?.prompt_tokens,
};
},
};
}
/** OpenAI: POST https://api.openai.com/v1/embeddings — Bearer auth; the
* response's usage carries prompt_tokens (verified 2026-09-23). */
export function openaiEmbeddingProvider(model: string, apiKey: string, fetchFn: FetchLike = fetch): EmbeddingPort {
const parsed = parseEmbeddingModelSlug(model);
if (!parsed) throw new Error(`invalid embedding model slug: ${model}`);
return {
model,
async embed(texts: readonly string[], task: EmbeddingTask): Promise<EmbeddingResult> {
// OpenAI's embeddings API takes no task hint (one format per
// model); the argument exists for seam parity and is unused here.
void task;
const resp = await fetchFn("https://api.openai.com/v1/embeddings", {
method: "POST",
headers: { "content-type": "application/json", authorization: `Bearer ${apiKey}` },
body: JSON.stringify({ model: parsed.id, input: texts }),
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
await demandOk(resp, "OpenAI embeddings");
const data = (await resp.json()) as {
data?: Array<{ embedding?: unknown; index?: unknown }>;
usage?: { prompt_tokens?: number };
};
return {
vectors: rowsInOrder(data.data ?? [], texts.length, "OpenAI embeddings"),
model,
dim: 0,
promptTokens: data.usage?.prompt_tokens,
};
},
};
}
/** Google: POST …/models/{m}:batchEmbedContents — x-goog-api-key, one
* request per text with taskType RETRIEVAL_QUERY|RETRIEVAL_DOCUMENT
* (verified against the Gemini API docs 2026-09-23). */
export function googleEmbeddingProvider(model: string, apiKey: string, fetchFn: FetchLike = fetch): EmbeddingPort {
const parsed = parseEmbeddingModelSlug(model);
if (!parsed) throw new Error(`invalid embedding model slug: ${model}`);
return {
model,
async embed(texts: readonly string[], task: EmbeddingTask): Promise<EmbeddingResult> {
const resp = await fetchFn(
`https://generativelanguage.googleapis.com/v1beta/models/${encodeURIComponent(parsed.id)}:batchEmbedContents`,
{
method: "POST",
headers: { "content-type": "application/json", "x-goog-api-key": apiKey },
body: JSON.stringify({
requests: texts.map((text) => ({
model: `models/${parsed.id}`,
content: { parts: [{ text }] },
taskType: task === "query" ? "RETRIEVAL_QUERY" : "RETRIEVAL_DOCUMENT",
})),
}),
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
},
);
await demandOk(resp, "Google embeddings");
const data = (await resp.json()) as {
embeddings?: Array<{ values?: unknown }>;
/** embedContent responses carry usageMetadata; the batch
* shape may too — parsed opportunistically, never guessed. */
usageMetadata?: { promptTokenCount?: number };
};
const vectors = (data.embeddings ?? []).map((row, i) => {
if (!Array.isArray(row.values) || !row.values.every((x) => typeof x === "number")) {
throw new Error(`Google embeddings API returned a bad vector at index ${i}`);
}
return row.values as number[];
});
if (vectors.length !== texts.length) {
throw new Error(`Google embeddings API returned ${vectors.length}/${texts.length} vectors`);
}
return { vectors, model, dim: 0, promptTokens: data.usageMetadata?.promptTokenCount };
},
};
}
// ─── Resolution (fresh per call — the search port's treatment) ───────────────
/** The resolved state: a live port, or the honest reason it isn't. */
export type EmbeddingResolution =
| { ok: true; port: EmbeddingPort }
| { ok: false; reason: string };
/** Pure resolution from a setting slug + a key lookup (testable): null
* slug = off; a set slug with no key = honest unavailability. */
export function resolveEmbeddingPortFor(
slug: string | null | undefined,
keyOf: (envVar: string) => string | undefined,
fetchFn?: FetchLike,
): EmbeddingResolution {
if (slug === null || slug === undefined || slug === "") {
return { ok: false, reason: "no embedding model configured (graph.embeddingModel is null)" };
}
const parsed = parseEmbeddingModelSlug(slug);
if (!parsed) {
return { ok: false, reason: `invalid embedding model slug: ${slug}` };
}
const apiKey = keyOf(PROVIDER_KEYS[parsed.provider]);
if (!apiKey) {
return {
ok: false,
reason: `embedding model is set to "${slug}" but its ${PROVIDER_KEYS[parsed.provider]} key is missing`,
};
}
const factory =
parsed.provider === "openrouter"
? openrouterEmbeddingProvider
: parsed.provider === "openai"
? openaiEmbeddingProvider
: googleEmbeddingProvider;
return { ok: true, port: factory(slug, apiKey, fetchFn) };
}
/** The LIVE resolution — reads the settings record + auth store fresh on
* every call, so a settings PUT or a key save lands on the next call. */
export function embeddingPort(): EmbeddingResolution {
return resolveEmbeddingPortFor(effectiveSettings().graph.embeddingModel, keyValue);
}
// ─── The post-merge batch (the extraction pipeline's embedding tail) ────────
/** What the embedding tail reports back: counts for logs, the skip reason
* when nothing ran (owner ruling: null model = info skip, not error), the
* failure message when it died partway (owner ruling 2026-09-23:
* usage-on-error — a multi-batch embed that fails mid-run still reports
* the spend of the batches that landed, so the money channel never
* loses a billed call), and the money-channel stage when it billed (one
* CallUsage step per API call, outputTokens: 0 — priced via
* EMBEDDING_PRICES). */
export interface EmbedBatchResult {
embedded: number;
total: number;
skipped?: string;
/** Set when an embed batch failed after others may have landed. The
* landed vectors are already persisted (pay-once, owner ruling A
* 2026-09-23) — the next run embeds only what remains. */
failed?: string;
usage?: ExtractionStageUsage;
}
/**
* Embed every node the ledger is missing — the post-merge hook (new
* propositions only, typically a handful) AND the cold-start backfill
* (empty ledger = every node) in one function. A model change voids the
* ledger first (full re-embed — vectors from different models live in
* incompatible spaces). Also prunes vectors for nodes that no longer
* exist, then re-clusters ONCE per batch (consumers read the persisted
* partition). Non-fatal by contract: the caller treats failure as
* "clustering runs without the new nodes until the next merge retries".
* Usage-on-error + pay-once (owner ruling 2026-09-23): an embed batch
* that throws never propagates — the batches that already landed are
* persisted (each text is paid for exactly once; a retry continues with
* the remaining nodes), their spend still reaches the money channel via
* usage, and `failed` carries the error for logs and callers.
*/
export async function embedNewNodes(
graphStore: {
readNodes(): GraphNode[];
readEmbeddings(): EmbeddingsLedger;
writeEmbeddings(ledger: EmbeddingsLedger): void;
},
/** Test seam: the live resolution by default; tests inject a scripted
* port (they never read the machine's settings/auth files). */
resolvePort: () => EmbeddingResolution = embeddingPort,
): Promise<EmbedBatchResult> {
const resolved = resolvePort();
const ledger = graphStore.readEmbeddings();
if (!resolved.ok) {
console.info(`[embeddings] skipped — ${resolved.reason}`);
return { embedded: 0, total: Object.keys(ledger.vectors).length, skipped: resolved.reason };
}
const port = resolved.port;
const stale = ledger.model !== port.model;
const vectors: Record<string, number[]> = stale ? {} : { ...ledger.vectors };
const steps: NonNullable<EmbedBatchResult["usage"]>["steps"] = [];
let dim = ledger.dim;
// Node LINES may repeat an id — dedupe, first defines (getGraph rule).
const seen = new Set<string>();
const todo: GraphNode[] = [];
for (const node of graphStore.readNodes()) {
if (seen.has(node.id)) continue;
seen.add(node.id);
if (!vectors[node.id]) todo.push(node);
}
let landed = 0;
let failed: string | undefined;
for (let i = 0; i < todo.length; i += EMBED_BATCH) {
const batch = todo.slice(i, i + EMBED_BATCH);
let result: EmbeddingResult;
try {
result = await port.embed(
batch.map((n) => n.statement),
"document",
);
} catch (err) {
// Usage-on-error + pay-once (owner ruling 2026-09-23): stop
// here, keep the batches that landed, report the failure — the
// throw never escapes this function.
failed = err instanceof Error ? err.message : String(err);
break;
}
for (let j = 0; j < batch.length; j++) {
vectors[batch[j]!.id] = result.vectors[j]!;
landed++;
}
if (result.vectors[0]) dim = result.vectors[0].length;
if (result.promptTokens !== undefined) {
// outputTokens: 0 — embeddings emit no output; callCost needs it
// defined to price the input at all.
steps.push(embeddingStep(result.promptTokens));
}
}
if (failed !== undefined) {
console.error(
`[embeddings] embedding failed after ${landed}/${todo.length} nodes (non-fatal; landed vectors persist, the next run embeds only what remains): ${failed}`,
);
}
// Prune ghosts: vectors for node ids that no longer exist.
let pruned = 0;
for (const id of Object.keys(vectors)) {
if (!seen.has(id)) {
delete vectors[id];
pruned++;
}
}
// RECLUSTER AT THE END OF THE BATCH (user-decided 2026-09-13): the
// agglomerative pass runs HERE — once per embedding batch — and
// consumers (snapshot, settle, namer) read the persisted partition.
const clusters = semanticClusters(vectors, { similarityThreshold: SEMANTIC_SIM_THRESHOLD });
const wrote =
failed !== undefined
? landed > 0
: todo.length > 0 || pruned > 0 || stale || ledger.clusters.length !== clusters.length;
if (wrote) {
graphStore.writeEmbeddings({
model: port.model,
dim,
vectors,
clusters,
updatedAt: new Date().toISOString(),
});
}
const result: EmbedBatchResult = {
embedded: landed,
total: Object.keys(vectors).length,
usage: steps.length > 0 ? { model: port.model, steps } : undefined,
};
if (failed !== undefined) result.failed = failed;
return result;
}
// ─── The federated embedding catalog (wave 6, owner ruling 2026-09-23) ──────
/** One catalog row: provider-qualified id (the setting's exact slug
* format) + display name. */
export interface EmbeddingCatalogModel {
/** The full "provider:id" slug — what graph.embeddingModel stores. */
id: string;
/** Human label for the picker (the provider's name, when given). */
name: string;
}
/** The embedding models of every KEYED provider, fetched live (the
* chat-models picker is a static pi-ai catalog, but pi-ai carries no
* embedding models — so this endpoint federates the providers' own
* lists). A provider without a key contributes nothing; a provider whose
* catalog call fails is skipped with an error log (never poisons the
* rest, never fails the endpoint). Empty array = nothing keyed. */
export async function embeddingModelCatalog(
fetchFn: FetchLike = fetch,
/** Test seam: the live key lookup by default; tests inject scripted
* keys (they never read the machine's auth file). */
keyOf: (id: string) => string | undefined = keyValue,
): Promise<EmbeddingCatalogModel[]> {
const out: EmbeddingCatalogModel[] = [];
const fetchJson = async (url: string, headers: Record<string, string>): Promise<unknown> => {
const resp = await fetchFn(url, { headers, signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) });
await demandOk(resp, "Embeddings catalog");
return resp.json();
};
// OpenRouter: GET /api/v1/embeddings/models — the dedicated embedding
// list (VERIFIED live 2026-09-23: the inverse path /api/v1/models/
// embeddings 404s; this one returns {data: [{id, name, …}]}). Paginated
// — limit=500 covers the whole list at current size (33).
if (keyOf(PROVIDER_KEYS.openrouter)) {
try {
const data = (await fetchJson("https://openrouter.ai/api/v1/embeddings/models?limit=500", {
authorization: `Bearer ${keyOf(PROVIDER_KEYS.openrouter)}`,
})) as { data?: Array<{ id?: unknown; name?: unknown }> };
for (const m of data.data ?? []) {
if (typeof m.id !== "string" || !m.id) continue;
out.push({ id: `openrouter:${m.id}`, name: typeof m.name === "string" && m.name ? m.name : m.id });
}
} catch (err) {
console.error("[embeddings] OpenRouter catalog fetch failed:", err);
}
}
// OpenAI: GET /v1/models — no embedding-only list, so filter the
// generic one on the embedding naming convention.
if (keyOf(PROVIDER_KEYS.openai)) {
try {
const data = (await fetchJson("https://api.openai.com/v1/models", {
authorization: `Bearer ${keyOf(PROVIDER_KEYS.openai)}`,
})) as { data?: Array<{ id?: unknown }> };
for (const m of data.data ?? []) {
if (typeof m.id !== "string" || !/^text-embedding-/.test(m.id)) continue;
out.push({ id: `openai:${m.id}`, name: m.id });
}
} catch (err) {
console.error("[embeddings] OpenAI catalog fetch failed:", err);
}
}
// Google: GET /v1beta/models — filter on the methods the model
// actually serves (embedContent present).
if (keyOf(PROVIDER_KEYS.google)) {
try {
const data = (await fetchJson("https://generativelanguage.googleapis.com/v1beta/models?pageSize=1000", {
"x-goog-api-key": keyOf(PROVIDER_KEYS.google)!,
})) as {
models?: Array<{ name?: unknown; displayName?: unknown; supportedGenerationMethods?: unknown }>;
};
for (const m of data.models ?? []) {
if (typeof m.name !== "string" || !m.name.startsWith("models/")) continue;
if (!Array.isArray(m.supportedGenerationMethods) || !m.supportedGenerationMethods.includes("embedContent")) continue;
const id = m.name.slice("models/".length);
out.push({
id: `google:${id}`,
name: typeof m.displayName === "string" && m.displayName ? m.displayName : id,
});
}
} catch (err) {
console.error("[embeddings] Google catalog fetch failed:", err);
}
}
return out;
}
-1
View File
@@ -6,7 +6,6 @@
export * from "./chat-handler.ts";
export * from "./chats.ts";
export * from "./embeddings.ts";
export * from "./graph.ts";
export * from "./commit-positions.ts";
export * from "./jobs.ts";
+12 -31
View File
@@ -289,41 +289,22 @@ export function parseAppearanceSettingsBody(body: unknown): SettingsBodyResult<A
return { ok: true, settings };
}
/** graph: the four-rung ladder { enabled?, embedding?, clustering?, labeling? }
* + embeddingModel? — the BYOK provider embedding model (wave 6,
* 2026-09-23): null or a "provider:id" slug of a shipped provider.
* Catalog validity is network-dependent — the parser checks SHAPE only
* (the federated /api/models/embeddings gives the picker real options). */
/** graph: the collapsed section { enabled? } (wave 7, owner ruling
* 2026-09-23): the master switch ONLY — the embedding/clustering/labeling
* rungs and the BYOK embeddingModel died with the provider embedder
* (clustering is the free birth-session partition, labels are session
* names). Old-shape keys in a body are ignored, not rejected. */
export function parseGraphSettingsBody(body: unknown): SettingsBodyResult<GraphSettings> {
const raw = asRecord(body);
if (!raw) return { ok: false, error: "Invalid JSON body" };
// Wave 7 collapse (owner ruling 2026-09-23): the section is the master
// switch ONLY. PUT is replace-whole — a body like {enabled: true} IS the
// whole section now; the dead rungs' keys in old records are unread
// (the sanitizer drops them; see sanitizeGraph).
const settings: GraphSettings = {};
for (const key of ["enabled", "embedding", "clustering", "labeling"] as const) {
if (raw[key] !== undefined) {
if (typeof raw[key] !== "boolean") {
return { ok: false, error: `${key} must be a boolean` };
}
settings[key] = raw[key];
}
}
if (raw.embeddingModel !== undefined) {
if (raw.embeddingModel !== null) {
// NOTE: the id may itself contain colons (OpenRouter suffixes like
// "liquid/lfm-2.5-embedding-350m:free") — the provider is the part
// before the FIRST colon, so the id half just forbids whitespace.
if (
typeof raw.embeddingModel !== "string" ||
!/^(openrouter|openai|google):\S+$/.test(raw.embeddingModel)
) {
return {
ok: false,
error: 'embeddingModel must be null or a "openrouter|openai|google:model-id" slug',
};
}
settings.embeddingModel = raw.embeddingModel;
} else {
settings.embeddingModel = null;
}
if (raw.enabled !== undefined) {
if (typeof raw.enabled !== "boolean") return { ok: false, error: "enabled must be a boolean" };
settings.enabled = raw.enabled;
}
return { ok: true, settings };
}
-19
View File
@@ -21,7 +21,6 @@ import { coverageOf } from "../core/coverage.ts";
import type { ChatStore, GraphStore } from "../core/ports.ts";
import { EXTRACTION } from "../../config.ts";
import type { ExtractionPipeline } from "../agent/subagents/extraction.ts";
import { startGraphContextInjection, type GraphContextDeps } from "../agent/subagents/graph-context.ts";
import { ModelsNotConfiguredError } from "../core/settings.ts";
/**
@@ -113,21 +112,3 @@ export function triggerExtraction(
}
}
/**
* The graph READ channel (vision-context-injection.md): one shot per chat,
* at session start — the first typed message is the session's topic; the
* semantically closest propositions are selected under the session's token
* budget and steered in through the wake pipeline. Fire-and-forget like
* triggerExtraction; the one-shot gates (topic, budget, prior injection)
* live in the runner module, not here — this seam only decides WHEN to ask.
* Skipped under tests, same doctrine: the runner is tested directly with a
* scripted embedder.
*/
export function triggerGraphContext(deps: GraphContextDeps, chatId: string): void {
if (process.env.NODE_ENV === "test") return;
try {
startGraphContextInjection(deps, chatId);
} catch (err) {
console.error(`[chat ${chatId}] graph-context trigger failed:`, err);
}
}