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 145 to 146

Compare

+234 −0 in 1 place
new file, 234 lines
1+import { createHash } from "node:crypto";
2+import type { Graph, RelocationInfo } from "../graph.ts";
3+import { isStartNode } from "../keys.ts";
4+import type { GraphStateStorage } from "../state-source.ts";
5+import type { NodeId } from "../types.ts";
6+
7+interface Progress {
8+ identity: string;
9+ phase: "scan" | "walk" | "entry" | "weakest" | "repick" | "mark" | "done";
10+ mover: number;
11+ move: number;
12+ work: number;
13+ item: number;
14+ line: NodeId | null;
15+ slow: NodeId | null;
16+ fast: NodeId | null;
17+ meeting: NodeId | null;
18+ entry: NodeId | null;
19+ current: NodeId | null;
20+ weakest: RelocationInfo | null;
21+ best: RelocationInfo | null;
22+}
23+const digest = (value: unknown) => createHash("sha256").update(JSON.stringify(value)).digest("hex");
24+const integer = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;
25+function shape(value: unknown, fields: string): asserts value is Record<string, unknown> {
26+ const keys = fields.split(" ");
27+ if (
28+ !value ||
29+ typeof value !== "object" ||
30+ Array.isArray(value) ||
31+ Object.keys(value).length !== keys.length ||
32+ keys.some((key) => !Object.hasOwn(value, key))
33+ )
34+ throw Error("invalid move resolution record");
35+}
36+function readProgress(value: unknown, graph: Graph): Progress | undefined {
37+ if (value === undefined) return undefined;
38+ shape(value, "format checksum state");
39+ if (value.format !== 1 || value.checksum !== digest(value.state)) throw Error("damaged move resolution cursor");
40+ shape(value.state, "identity phase mover move work item line slow fast meeting entry current weakest best");
41+ const state = value.state;
42+ if (
43+ typeof state.identity !== "string" ||
44+ !state.identity ||
45+ state.identity.length > 4096 ||
46+ !["scan", "walk", "entry", "weakest", "repick", "mark", "done"].includes(String(state.phase)) ||
47+ ![state.mover, state.move, state.work, state.item].every(integer) ||
48+ Number(state.mover) > graph.movers().length ||
49+ Number(state.item) > Number(state.work) ||
50+ Number(state.work) > graph.lineCount
51+ )
52+ throw Error("invalid move resolution position");
53+ const mover = graph.movers()[Number(state.mover)];
54+ if (Number(state.move) > (mover === undefined ? 0 : (graph.atom(mover).moves?.length ?? 0)))
55+ throw Error("invalid move offset");
56+ for (const key of ["line", "slow", "fast", "meeting", "entry", "current"])
57+ if (state[key] !== null && (typeof state[key] !== "string" || !state[key] || String(state[key]).length > 4096))
58+ throw Error("invalid move ancestry cursor");
59+ for (const key of ["weakest", "best"]) {
60+ const move = state[key];
61+ if (move === null) continue;
62+ shape(move, "atom line parent k");
63+ if (typeof move.atom !== "string" || !integer(move.k)) throw Error("invalid saved move");
64+ const canonical = graph.atom(move.atom).moves?.[move.k];
65+ if (!canonical || canonical.line !== move.line || canonical.parent !== move.parent)
66+ throw Error("saved move identity mismatch");
67+ }
68+ if (
69+ (state.phase === "repick" && state.weakest === null) ||
70+ (["entry", "weakest", "repick", "mark"].includes(String(state.phase)) && state.line === null) ||
71+ (state.phase === "entry" && (state.entry === null || state.meeting === null)) ||
72+ (state.phase === "weakest" && (state.entry === null || state.current === null))
73+ )
74+ throw Error("incomplete move resolution cursor");
75+ return state as unknown as Progress;
76+}
78+/** Private paged equivalent of resolveScratchMoves. Caller isolates storage and
79+ * binds identity to immutable graph, membership and source before every page. */
80+export function resolveMovesPage(
81+ graph: Graph,
82+ has: (id: string) => boolean,
83+ storage: GraphStateStorage,
84+ identity: string,
85+ maxSteps: number,
86+): { done: boolean; steps: number } {
87+ if (!identity || identity.length > 4096 || !Number.isSafeInteger(maxSteps) || maxSteps < 1 || maxSteps > 256)
88+ throw Error("invalid move resolution page");
89+ return storage.transaction(() => {
90+ let state = readProgress(storage.read("progress", "cursor"), graph);
91+ if (state && state.identity !== identity) throw Error("move resolution identity changed");
92+ state ??= {
93+ identity,
94+ phase: "scan",
95+ mover: 0,
96+ move: 0,
97+ work: 0,
98+ item: 0,
99+ line: null,
100+ slow: null,
101+ fast: null,
102+ meeting: null,
103+ entry: null,
104+ current: null,
105+ weakest: null,
106+ best: null,
107+ };
108+ const selected = (line: string) => storage.read("selected", line) as RelocationInfo | undefined;
109+ const parent = (line: string): NodeId => {
110+ const value = selected(line)?.parent ?? graph.lineParent(line);
111+ if (value === undefined) throw Error("missing relocation ancestry");
112+ return value;
113+ };
114+ const terminal = (node: NodeId | null) => node === null || isStartNode(node) || !!storage.read("reached", node);
115+ const step = (node: NodeId | null): NodeId | null => (terminal(node) ? null : parent(node!));
116+ const stronger = (a: RelocationInfo, b: RelocationInfo) => graph.comparePriority(a.atom, b.atom) > 0;
117+ const moveKey = (move: RelocationInfo) => JSON.stringify([move.atom, move.k]);
118+ const restartWalk = () => {
119+ state!.phase = "walk";
120+ state!.slow = state!.line;
121+ state!.fast = state!.line;
122+ };
123+ let steps = 0;
124+ while (state.phase !== "done" && steps < maxSteps) {
125+ steps++;
126+ switch (state.phase) {
127+ case "scan":
128+ case "repick": {
129+ if (state.mover === graph.movers().length) {
130+ if (state.phase === "scan") {
131+ state.phase = "walk";
132+ state.line = null;
133+ } else {
134+ const line = state.weakest!.line;
135+ if (state.best) storage.write("selected", line, state.best);
136+ else storage.remove("selected", line);
137+ restartWalk();
138+ }
139+ break;
140+ }
141+ const id = graph.movers()[state.mover]!;
142+ if (!has(id)) {
143+ state.mover++;
144+ state.move = 0;
145+ break;
146+ }
147+ const moves = graph.atom(id).moves ?? [];
148+ if (state.move === moves.length) {
149+ state.mover++;
150+ state.move = 0;
151+ break;
152+ }
153+ const k = state.move++;
154+ const candidate: RelocationInfo = { atom: id, ...moves[k]!, k };
155+ if (state.phase === "scan") {
156+ if (!storage.read("seen", candidate.line)) {
157+ storage.write("seen", candidate.line, true);
158+ storage.write("work", String(state.work++), candidate.line);
159+ }
160+ const best = selected(candidate.line);
161+ if (!best || stronger(candidate, best)) storage.write("selected", candidate.line, candidate);
162+ } else if (
163+ candidate.line === state.weakest!.line &&
164+ !storage.read("ignored", moveKey(candidate)) &&
165+ (!state.best || stronger(candidate, state.best))
166+ )
167+ state.best = candidate;
168+ break;
169+ }
170+ case "walk": {
171+ if (state.line === null) {
172+ if (state.item === state.work) {
173+ state.phase = "done";
174+ break;
175+ }
176+ state.line = storage.read("work", String(state.item)) as string;
177+ restartWalk();
178+ break;
179+ }
180+ state.slow = step(state.slow);
181+ state.fast = step(step(state.fast));
182+ if (terminal(state.slow) || terminal(state.fast)) {
183+ state.phase = "mark";
184+ state.current = state.line;
185+ } else if (state.slow === state.fast) {
186+ state.phase = "entry";
187+ state.meeting = state.slow;
188+ state.entry = state.line;
189+ }
190+ break;
191+ }
192+ case "entry": {
193+ if (state.entry === state.meeting) {
194+ state.phase = "weakest";
195+ state.current = state.entry;
196+ state.weakest = null;
197+ break;
198+ }
199+ state.entry = step(state.entry);
200+ state.meeting = step(state.meeting);
201+ break;
202+ }
203+ case "weakest": {
204+ const move = selected(state.current!);
205+ if (move && (!state.weakest || stronger(state.weakest, move))) state.weakest = move;
206+ state.current = parent(state.current!);
207+ if (state.current === state.entry) {
208+ if (!state.weakest) throw Error("canonical ancestry unexpectedly cycles");
209+ storage.write("ignored", moveKey(state.weakest), true);
210+ state.phase = "repick";
211+ state.mover = 0;
212+ state.move = 0;
213+ state.best = null;
214+ }
215+ break;
216+ }
217+ case "mark": {
218+ if (terminal(state.current)) {
219+ state.item++;
220+ state.line = null;
221+ state.phase = "walk";
222+ break;
223+ }
224+ const next = parent(state.current!);
225+ storage.write("reached", state.current!, true);
226+ state.current = next;
227+ break;
228+ }
229+ }
230+ }
231+ storage.write("progress", "cursor", { format: 1, checksum: digest(state), state });
232+ return { done: state.phase === "done", steps };
233+ });
234+}
MCP
compare on this site's /mcp
CLI
abso diff 145 146 src/engine/experimental/projection-move-resolution.ts --json
JSON
/christopher/abso-core/compare/145..146?file=src%2Fengine%2Fexperimental%2Fprojection-move-resolution.ts&format=json