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
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-adoption.ts?format=json
188 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 149, 2 versions changed it
1import { createHash } from "node:crypto";
2import type { RelocationInfo } from "../graph.ts";
3import { orderedChildKey } from "../ordered-index.ts";
4import type { GraphStateStorage } from "../state-source.ts";
5import type { ExperimentalMarkerProjection, MarkerState } from "./projection-marker.ts";
6import { graphMoveSource, type ProjectionMoveSource, resolveMovesPage } from "./projection-move-resolution.ts";
7
8type Phase = "files" | "resolve" | "plans" | "park" | "place" | "cleanup" | "done";
9interface Cursor {
10 identity: string;
11 phase: Phase;
12 mover: number;
13 move: number;
14 count: number;
15 item: number;
16 collection: number;
18interface Plan {
19 n: number;
20 parentKey: number;
21 key: string;
23const collections = ["selected", "seen", "work", "ignored", "reached", "progress", "planned", "plans"];
24const digest = (value: unknown) => createHash("sha256").update(JSON.stringify(value)).digest("hex");
26function shape(value: unknown, fields: string): asserts value is Record<string, unknown> {
27 const keys = fields.split(" ");
28 if (
29 !value ||
30 typeof value !== "object" ||
31 Array.isArray(value) ||
32 Object.keys(value).length !== keys.length ||
33 keys.some((key) => !Object.hasOwn(value, key))
34 )
35 throw Error("invalid move adoption record");
38/** The caller must persist the returned private root in the same transaction. */
39export function adoptMovesStep(
40 p: ExperimentalMarkerProjection,
41 previous: MarkerState,
42 task: string,
43 identity: string,
44 source: ProjectionMoveSource = graphMoveSource(p.graph),
45): { done: boolean; state: MarkerState } {
46 p.assertContext(previous);
47 if (!task || task.length > 128 || !/^[a-f0-9]{64}$/.test(identity)) throw Error("invalid move adoption identity");
48 const name = `move-adoption:${task}`;
49 const scratch: GraphStateStorage = {
50 read: (c, k) => p.storage.read(`${name}:${c}`, k),
51 write: (c, k, v) => p.storage.write(`${name}:${c}`, k, v),
52 remove: (c, k) => p.storage.remove(`${name}:${c}`, k),
53 entries: (c) => p.storage.entries(`${name}:${c}`),
54 transaction: (body) => p.storage.transaction(body),
55 };
56 return p.storage.transaction(() => {
57 const saved = p.storage.read(name, "cursor");
58 let restored: Cursor | undefined;
59 if (saved !== undefined) {
60 shape(saved, "format checksum cursor");
61 if (saved.format !== 1 || saved.checksum !== digest(saved.cursor)) throw Error("damaged move adoption cursor");
62 shape(saved.cursor, "identity phase mover move count item collection");
63 restored = saved.cursor as unknown as Cursor;
64 }
65 const cursor: Cursor = restored ?? {
66 identity,
67 phase: "files",
68 mover: 0,
69 move: 0,
70 count: 0,
71 item: 0,
72 collection: 0,
73 };
74 if (
75 cursor.identity !== identity ||
76 !["files", "resolve", "plans", "park", "place", "cleanup", "done"].includes(cursor.phase) ||
77 ![cursor.mover, cursor.move, cursor.count, cursor.item, cursor.collection].every(
78 (n) => Number.isSafeInteger(n) && n >= 0,
79 ) ||
80 cursor.mover > p.graph.movers().length ||
81 cursor.item > cursor.count ||
82 cursor.count > p.graph.lineCount ||
83 cursor.collection > collections.length
84 )
85 throw Error("invalid move adoption cursor");
86 const mover = p.graph.movers()[cursor.mover];
87 if (cursor.move > (mover === undefined ? 0 : source.count(mover))) throw Error("invalid move adoption offset");
88 let state = { ...previous };
89 const has = (id: string) => p.members.get(state.membership, Number(id.slice(1)))?.present ?? false;
90 switch (cursor.phase) {
91 case "files":
92 case "plans": {
93 if (cursor.mover === p.graph.movers().length) {
94 cursor.phase = cursor.phase === "files" ? "resolve" : "park";
95 cursor.mover = 0;
96 cursor.move = 0;
97 break;
98 }
99 const moverId = p.graph.movers()[cursor.mover]!;
100 const count = source.count(moverId);
101 if (cursor.move > count) throw Error("invalid move adoption offset");
102 if (cursor.move === count) {
103 cursor.mover++;
104 cursor.move = 0;
105 break;
106 }
107 const move = source.at(moverId, cursor.move++);
108 if (!move) throw Error("missing indexed move");
109 if (cursor.phase === "files") {
110 if (move.parent.startsWith("^")) p.ensureFile(state, move.parent.slice(1));
111 break;
112 }
113 if (scratch.read("planned", move.line)) break;
114 scratch.write("planned", move.line, true);
115 const n = p.graph.lineN(move.line);
116 if (!has(p.graph.idOfSeq(p.graph.writerSeqOf(n)))) break;
117 const marker = p.markers.get(state.locator, n + 1);
118 if (marker?.kind !== "line") throw Error("missing selected relocation marker");
119 const selected = scratch.read("selected", move.line) as RelocationInfo | undefined;
120 const parent = selected?.parent ?? p.graph.lineParent(move.line);
121 if (parent === undefined) throw Error("missing relocation ancestry");
122 const parentKey = parent.startsWith("^") ? p.fileKey(parent.slice(1)) : p.graph.lineN(parent) + 1;
123 if (parentKey === undefined) throw Error("missing relocation file");
124 const seq = selected ? Number(selected.atom.slice(1)) : p.graph.writerSeqOf(n);
125 const key = orderedChildKey({
126 n,
127 replaces: selected ? 0 : Number(p.graph.replacesN(n)),
128 lamport: p.graph.lamportOfSeq(seq),
129 seq,
130 k: selected ? selected.k : p.graph.kOf(n),
131 });
132 if (parentKey !== marker.parentKey || key !== marker.key)
133 scratch.write("plans", String(cursor.count++), { n, parentKey, key });
134 break;
135 }
136 case "resolve":
137 if (resolveMovesPage(p.graph, has, scratch, identity, 1, source).done) cursor.phase = "plans";
138 break;
139 case "park":
140 case "place": {
141 if (cursor.item === cursor.count) {
142 cursor.phase = cursor.phase === "park" ? "place" : "cleanup";
143 cursor.item = 0;
144 break;
145 }
146 const plan = scratch.read("plans", String(cursor.item++)) as Plan | undefined;
147 if (
148 !plan ||
149 !Number.isSafeInteger(plan.n) ||
150 plan.n < 0 ||
151 !Number.isSafeInteger(plan.parentKey) ||
152 plan.parentKey < 0 ||
153 !/^[0-9a-f]{57}$/.test(plan.key)
154 )
155 throw Error("invalid saved relocation plan");
156 const marker = p.markers.get(state.locator, plan.n + 1);
157 if (marker?.kind !== "line") throw Error("missing relocation marker");
158 state = p.relocate(
159 state,
160 plan.n,
161 cursor.phase === "park" ? 0 : plan.parentKey,
162 cursor.phase === "park" ? marker.key : plan.key,
163 );
164 break;
165 }
166 case "cleanup": {
167 if (cursor.collection === collections.length) {
168 cursor.phase = "done";
169 break;
170 }
171 const collection = collections[cursor.collection]!;
172 const iterator = scratch.entries(collection)[Symbol.iterator]();
173 let row: IteratorResult<readonly [string, unknown]>;
174 try {
175 row = iterator.next();
176 } finally {
177 iterator.return?.();
178 }
179 if (row.done) cursor.collection++;
180 else scratch.remove(collection, row.value[0]);
181 break;
182 }
183 }
184 if (cursor.phase === "done") p.storage.remove(name, "cursor");
185 else p.storage.write(name, "cursor", { format: 1, checksum: digest(cursor), cursor });
186 return { done: cursor.phase === "done", state };
187 });