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