Birth-session clustering + the prober graph-tools data seam (wave 7 chunk A)

Embeddings are dying (chunk B removes the machinery); the clusters they
used to carry become the session that created them — derived on read.

- New src/core/session-clusters.ts: birthSessionPartition groups merged
  nodes by their FIRST SURVIVING provenance point's chatId (owner-locked
  2026-09-23). Deletion is self-healing by construction: stripped-birth
  survivors re-attribute to their earliest remaining contributor, and
  empty-provenance nodes stay unclustered. Notes imports are birth-sessions
  too (owner ruling) — their label is the file path.
- graphSnapshot: clusters = the birth partition filtered >= MIN_CLUSTER_SIZE
  (the hull presentation rule), id = session id, label = session name (chat
  title / file path; a deleted record renders unlabeled). The persisted
  cluster-labelings join dies; no LLM naming pass needed anymore.
- settleGraphLayout derives the same partition from readNodes — drops its
  readEmbeddings dep. One cluster concept everywhere.
- Prober data seam (core/graph.ts, pure ports-driven like the snapshot):
  graphSessionIndex (every birth session: id, name, chat|file kind,
  createdAt/updatedAt, birth-proposition count, recency-ordered) and
  graphSessionPropositions (a session's birth propositions with the
  extractor's latest understanding level and THAT event's own timestamp —
  owner ruling: quiz outcomes are ledger evidence, they never move the
  evaluation date). Teacher's tool handlers consume these in-process.
- New agent.graphTools subsection { enabled?: boolean } (owner ruling:
  the toggle gates prober tool REGISTRATION — agent capability, agent
  section, its own subsection, NOT the tool-toggles map; absent = ON).
  Lenient sanitizer, strict settings-body parser, passthrough resolution,
  and graphToolsEnabled(effective) = graph.enabled && toggle for Teacher's
  registry wiring.

Gates: tsc clean, eslint clean, 923/923 tests, next build compiles.
Live-verified on the worktree dev data: 10 nodes → 2 clusters labeled
with their chat titles; session index + propositions carry levels and
their own timestamps.
This commit is contained in:
amosblomqvist
2026-09-23 14:51:37 +03:00
parent fb66606555
commit 0fcffdffaf
12 changed files with 551 additions and 61 deletions
+73
View File
@@ -0,0 +1,73 @@
/**
* BIRTH-SESSION CLUSTERING (wave 7, owner-locked 2026-09-23): embeddings
* are dead — a proposition's cluster is the SESSION THAT CREATED IT,
* derived deterministically from provenance. Cluster labels are session
* names (the chat title or the imported file's path): no model call, no
* key, no configuration. What the agglomerative pass grouped by MEANING
* proximity, this groups by WHERE KNOWLEDGE WAS BORN — the owner's call
* that the session that minted a set of propositions is the honest
* "region of the world model" they belong to.
*
* Birth session (owner ruling 2026-09-23): a node's FIRST surviving
* provenance point's chatId — the create line's first point at read time
* (getGraph merges lines by id, first line defines, provenance unions in
* line order — so provenance[0] is the birth point of the surviving
* lines). This makes deletion self-healing with ZERO special handling:
* delete-nodes strips a session's create lines and its provenance points;
* a SURVIVING shared node re-attributes to its earliest remaining
* contributor (provenance[0] walks forward), and a node left with NO
* provenance at all is simply unclustered. The partition is derived on
* every read — there is no persisted partition to invalidate.
*
* Notes imports are birth-sessions too (owner ruling 2026-09-23): their
* nodes carry a "src_…" source id instead of a chat id; the caller labels
* them with the file's path (kind: file in the session views).
*
* Browser-safe pure logic (no node builtins, no fs): same contract style
* as the rest of the core graph derivation. Not re-exported through the
* core barrel — the shell reads clusters from the snapshot; only the
* server-side derivations (snapshot, settle) and tests import this.
*/
import type { GraphNode } from "./contracts.ts";
/** One birth-session cluster: the session id + the SORTED member node
* ids born in it. The shape is deliberately the { members } contract
* the layout consumer takes; `id` rides along for consumers that need
* the session identity (the snapshot's cluster id + label join). */
export interface BirthSessionCluster {
/** The birth session's id — a chatId or a notes source id ("src_…"). */
id: string;
/** Node ids born in this session, sorted. */
members: string[];
}
/** A node's birth session: its first surviving provenance point's chatId,
* or null when the node carries no provenance at all (unclustered). */
export function birthSessionOf(node: GraphNode): string | null {
const first = node.provenance[0];
return first === undefined ? null : first.chatId;
}
/**
* The birth-session partition over the given node LINES: dedupes by id
* (first line defines — the getGraph rule), groups by birth session, and
* returns the groups size-desc with members sorted (deterministic output:
* ties break by session id ascending). Nodes with no surviving provenance
* are left unclustered — they belong to no session the graph can name.
*/
export function birthSessionPartition(nodes: readonly GraphNode[]): BirthSessionCluster[] {
const members = new Map<string, string[]>();
const seen = new Set<string>();
for (const node of nodes) {
if (seen.has(node.id)) continue;
seen.add(node.id);
const birth = birthSessionOf(node);
if (birth === null) continue;
const bucket = members.get(birth);
if (bucket) bucket.push(node.id);
else members.set(birth, [node.id]);
}
return [...members.entries()]
.map(([id, list]) => ({ id, members: [...list].sort() }))
.sort((a, b) => b.members.length - a.members.length || (a.id < b.id ? -1 : 1));
}