Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed

Versions 86 to 318

Compare

+144 −0 in 1 place
new file, 144 lines
1+import type { Graph, RelocationInfo } from "./graph.ts";
2+import { isStartNode } from "./keys.ts";
3+import { OrderedIndex, orderedChildKey } from "./ordered-index.ts";
4+import type { GraphStateStorage } from "./state-source.ts";
5+import type { AtomId, LineId, NodeId } from "./types.ts";
6+
7+/** Scope-local relocation state. The caller must consume this handle inside
8+ * withProjectionScratch; selected moves and cycle bookkeeping live on disk. */
9+export function resolveScratchMoves(
10+ graph: Graph,
11+ has: (id: AtomId) => boolean,
12+ scratch: GraphStateStorage,
13+): { selected(line: LineId): RelocationInfo | undefined; parent(line: LineId): NodeId } {
14+ const selected = (line: LineId) => scratch.read("selected", line) as RelocationInfo | undefined;
15+ const identity = (move: RelocationInfo) => JSON.stringify([move.atom, move.k]);
16+ const stronger = (a: RelocationInfo, b: RelocationInfo) => graph.comparePriority(a.atom, b.atom) > 0;
17+ const pick = (line: LineId): RelocationInfo | undefined => {
18+ let best: RelocationInfo | undefined;
19+ for (const move of graph.eachRelocationOf(line)) {
20+ if (has(move.atom) && !scratch.read("ignored", identity(move)) && (!best || stronger(move, best))) best = move;
21+ }
22+ return best;
23+ };
24+ let work = 0;
25+ for (const id of graph.movers()) {
26+ if (!has(id)) continue;
27+ for (const move of graph.atom(id).moves!) {
28+ if (scratch.read("seen", move.line)) continue;
29+ scratch.write("seen", move.line, true);
30+ scratch.write("work", String(work++), move.line);
31+ const best = pick(move.line);
32+ if (best) scratch.write("selected", move.line, best);
33+ }
34+ }
35+ const parent = (line: LineId): NodeId => {
36+ const node = selected(line)?.parent ?? graph.lineParent(line);
37+ if (node === undefined) throw new Error(`missing relocation ancestry for ${line}`);
38+ return node;
39+ };
40+ const terminal = (node: NodeId | null): boolean =>
41+ node === null || isStartNode(node) || !!scratch.read("reached", node);
42+ const step = (node: NodeId | null): NodeId | null => (terminal(node) ? null : parent(node!));
43+ for (let item = 0; item < work; item++) {
44+ const line = scratch.read("work", String(item)) as LineId;
45+ for (;;) {
46+ // Floyd's walk finds the exact cycle entry without a retained path.
47+ let slow: NodeId | null = line;
48+ let fast: NodeId | null = line;
49+ let meeting: NodeId | null = null;
50+ for (;;) {
51+ slow = step(slow);
52+ fast = step(step(fast));
53+ if (terminal(slow) || terminal(fast)) break;
54+ if (slow === fast) {
55+ meeting = slow;
56+ break;
57+ }
58+ }
59+ if (meeting === null) {
60+ let current: NodeId = line;
61+ while (!terminal(current)) {
62+ const next = parent(current);
63+ scratch.write("reached", current, true);
64+ current = next;
65+ }
66+ break;
67+ }
68+ let entry: NodeId | null = line;
69+ while (entry !== meeting) {
70+ entry = step(entry);
71+ meeting = step(meeting);
72+ }
73+ let current = entry!;
74+ let weakest: RelocationInfo | undefined;
75+ do {
76+ const move = selected(current);
77+ if (move && (!weakest || stronger(weakest, move))) weakest = move;
78+ current = parent(current);
79+ } while (current !== entry);
80+ if (!weakest) throw new Error("canonical ancestry unexpectedly cycles");
81+ scratch.write("ignored", identity(weakest), true);
82+ const next = pick(weakest.line);
83+ if (next) scratch.write("selected", weakest.line, next);
84+ else scratch.remove("selected", weakest.line);
85+ }
86+ }
87+ return { selected, parent };
88+}
90+/** Consume entirely within the projection scope. Traversal retains one line;
91+ * both priority ordering and parent links are held in rollback-only storage. */
92+export function* scratchLayoutNodes(
93+ graph: Graph,
94+ has: (id: AtomId) => boolean,
95+ scratch: GraphStateStorage,
96+ start: NodeId,
97+): IterableIterator<number> {
98+ const moves = resolveScratchMoves(graph, has, scratch);
99+ for (let n = 0; n < graph.lineCount; n++) {
100+ if (!has(graph.idOfSeq(graph.writerSeqOf(n)))) continue;
101+ const line = graph.nodeOf(n);
102+ const move = moves.selected(line);
103+ const seq = move ? graph.atom(move.atom).seq : graph.writerSeqOf(n);
104+ const key = orderedChildKey({
105+ n,
106+ replaces: move ? 0 : Number(graph.replacesN(n)),
107+ lamport: graph.lamportOfSeq(seq),
108+ seq,
109+ k: move ? move.k : graph.kOf(n),
110+ });
111+ const parent = moves.parent(line);
112+ scratch.write("layout-parent", line, { parent, key });
113+ new OrderedIndex(scratch, "layout-children", parent).insert(key, n);
114+ }
115+ const first = (parent: NodeId, after?: string): number | undefined => {
116+ for (const n of new OrderedIndex(scratch, "layout-children", parent).values(after)) return n;
117+ return undefined;
118+ };
119+ let n = first(start);
120+ while (n !== undefined) {
121+ yield n;
122+ const line = graph.nodeOf(n);
123+ const child = first(line);
124+ if (child !== undefined) {
125+ n = child;
126+ continue;
127+ }
128+ let previous = line;
129+ for (;;) {
130+ const row = scratch.read("layout-parent", previous) as { parent: NodeId; key: string } | undefined;
131+ if (!row) throw new Error("missing scratch layout ancestry");
132+ const sibling = first(row.parent, row.key);
133+ if (sibling !== undefined) {
134+ n = sibling;
135+ break;
136+ }
137+ if (row.parent === start) {
138+ n = undefined;
139+ break;
140+ }
141+ previous = row.parent;
142+ }
143+ }
144+}
MCP
compare on this site's /mcp
CLI
abso diff 86 318 src/engine/relocation-scratch.ts --json
JSON
/christopher/abso-core/compare/86..318?file=src%2Fengine%2Frelocation-scratch.ts&format=json