2026-09-30 03:22:52 +03:00
|
|
|
import type { GraphChain, GraphNode, Position } from "./contracts.ts";
|
2026-09-13 09:52:12 +03:00
|
|
|
import { incrementalLayout } from "./layout.ts";
|
2026-09-30 04:30:25 +03:00
|
|
|
import { attachImpToCommunities, birthSessionPartition } from "./session-clusters.ts";
|
2026-09-30 03:22:52 +03:00
|
|
|
import { deriveView, getGraph } from "./graph.ts";
|
2026-09-13 09:52:12 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The merge-time settle: recompute the resting layout from the CURRENT
|
|
|
|
|
* graph + the positions ledger, and replace the ledger. Called by the
|
|
|
|
|
* extraction pipeline after every merge (the sole writer) and by the
|
|
|
|
|
* positions-commit endpoint when the graph grew under a live-physics
|
|
|
|
|
* commit (client positions become the anchor set). Synchronous — it must
|
|
|
|
|
* finish before the next /api/graph fetch reflects the change.
|
|
|
|
|
*
|
2026-09-30 03:22:52 +03:00
|
|
|
* CHAINS-PRIMARY (owner, 2026-09-30): the settle lays out the DERIVED
|
|
|
|
|
* view — propositions AND the derived implication nodes (one per chain,
|
|
|
|
|
* arg_ ids) over the derived untyped arrows — so positions exist for
|
|
|
|
|
* every node the canvas renders. Implication nodes participate in the
|
|
|
|
|
* physics as regular nodes (rendering specialization comes later).
|
|
|
|
|
*
|
2026-09-13 09:52:12 +03:00
|
|
|
* SPLIT FROM layout.ts deliberately (2026-09-13): layout.ts is in the
|
2026-09-23 14:51:37 +03:00
|
|
|
* browser barrel, and the settle needs the cluster partition for the
|
|
|
|
|
* cluster centroid pull — this module must not ship in the client bundle.
|
|
|
|
|
* Server code imports it DIRECTLY; it is never re-exported through
|
|
|
|
|
* src/core/index.ts.
|
2026-09-13 09:52:12 +03:00
|
|
|
*/
|
|
|
|
|
export function settleGraphLayout(graphStore: {
|
|
|
|
|
readNodes(): GraphNode[];
|
2026-09-30 03:22:52 +03:00
|
|
|
readChains(): GraphChain[];
|
2026-09-13 09:52:12 +03:00
|
|
|
readPositions(): Record<string, Position>;
|
|
|
|
|
writePositions(positions: Record<string, Position>): void;
|
|
|
|
|
}): void {
|
2026-09-30 03:22:52 +03:00
|
|
|
const graph = getGraph(graphStore);
|
|
|
|
|
if (graph.nodes.length === 0) return;
|
|
|
|
|
const view = deriveView(graph);
|
2026-09-13 09:52:12 +03:00
|
|
|
const previous = new Map(Object.entries(graphStore.readPositions()));
|
2026-09-23 14:51:37 +03:00
|
|
|
// The cluster partition for the centroid pull: the SAME birth-session
|
|
|
|
|
// partition the snapshot renders (derived from provenance on read —
|
|
|
|
|
// core/session-clusters.ts, wave 7) — one cluster concept everywhere.
|
2026-09-30 03:22:52 +03:00
|
|
|
// PROPOSITIONS ONLY (owner, 2026-09-30): implication nodes never
|
|
|
|
|
// partition; the stored world clusters, its image follows.
|
|
|
|
|
const communities = birthSessionPartition(graph.nodes);
|
2026-09-30 04:30:25 +03:00
|
|
|
// The SAME imp-node assignment the snapshot renders (majority among
|
|
|
|
|
// premises' births — core/session-clusters.ts): the resting ledger and
|
|
|
|
|
// the live sim must agree on where bundles belong, or the first live
|
|
|
|
|
// tick would fight the settle. Unfiltered here (settle pulls every
|
|
|
|
|
// community, hulls are a canvas-only concern).
|
|
|
|
|
attachImpToCommunities(communities, graph.nodes, graph.chains);
|
|
|
|
|
const next = incrementalLayout(view.nodes, view.edges, previous, {
|
|
|
|
|
clusters: communities,
|
|
|
|
|
logicalEdges: view.logicalEdges,
|
|
|
|
|
});
|
2026-09-13 09:52:12 +03:00
|
|
|
graphStore.writePositions(Object.fromEntries(next));
|
2026-09-30 03:22:52 +03:00
|
|
|
}
|