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 142 to 143

Compare

+343 −0 in 1 place
new file, 343 lines
1+import type { GraphStateStorage } from "../state-source.ts";
2+import { ProjectionLocator } from "./projection-locator.ts";
3+
4+type Page = number | null;
5+interface SequenceNode {
6+ id: number;
7+ parent: Page;
8+ left: Page;
9+ right: Page;
10+ text: string;
11+ deletions: number;
12+ height: number;
13+ count: number;
14+ visible: number;
15+ fileStarts: number;
16+}
17+export interface SequenceState {
18+ readonly map: Page;
19+ readonly root: Page;
20+ readonly next: number;
21+}
22+export type SequenceOperation =
23+ | { kind: "insert"; rank: number; text: string }
24+ | { kind: "visibility"; id: number; delta: number }
25+ | { kind: "move"; from: number; length: number; to: number };
26+const safe = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;
27+function decode(value: unknown): SequenceNode {
28+ if (!value || typeof value !== "object" || Array.isArray(value)) throw Error("invalid sequence node");
29+ const row = value as Record<string, unknown>;
30+ if (Object.keys(row).length !== 10 || !safe(row.id) || row.id >= 2 ** 32 || typeof row.text !== "string")
31+ throw Error("invalid sequence node fields");
32+ for (const name of ["parent", "left", "right"])
33+ if (row[name] !== null && (!safe(row[name]) || Number(row[name]) >= 2 ** 32 || row[name] === row.id))
34+ throw Error("invalid sequence edge");
35+ for (const name of ["deletions", "height", "count", "visible", "fileStarts"])
36+ if (!safe(row[name])) throw Error("invalid sequence aggregate");
37+ if (
38+ Number(row.height) < 1 ||
39+ Number(row.height) > 64 ||
40+ Number(row.count) < 1 ||
41+ Number(row.visible) > Number(row.count) ||
42+ Number(row.fileStarts) > Number(row.count)
43+ )
44+ throw Error("invalid sequence aggregate bounds");
45+ return row as unknown as SequenceNode;
46+}
47+export class ProjectionSequence {
48+ readonly storage: GraphStateStorage;
49+ readonly #locator: ProjectionLocator<SequenceNode>;
50+ constructor(storage: GraphStateStorage, namespace: string) {
51+ this.storage = storage;
52+ this.#locator = new ProjectionLocator(storage, namespace, decode);
53+ }
54+ empty(): SequenceState {
55+ return { map: null, root: null, next: 0 };
56+ }
57+ get(map: Page, id: number): SequenceNode {
58+ const node = this.#locator.get(map, id);
59+ if (!node || node.id !== id) throw Error("missing sequence node");
60+ const child = (key: Page): SequenceNode | undefined => {
61+ if (key === null) return undefined;
62+ const value = this.#locator.get(map, key);
63+ if (!value || value.id !== key || value.parent !== id) throw Error("invalid sequence child identity");
64+ return value;
65+ };
66+ const left = child(node.left),
67+ right = child(node.right);
68+ if (
69+ node.height !== 1 + Math.max(left?.height ?? 0, right?.height ?? 0) ||
70+ Math.abs((left?.height ?? 0) - (right?.height ?? 0)) > 1 ||
71+ node.count !== 1 + (left?.count ?? 0) + (right?.count ?? 0) ||
72+ node.visible !== (node.deletions === 0 ? 1 : 0) + (left?.visible ?? 0) + (right?.visible ?? 0) ||
73+ node.fileStarts !== (node.text.startsWith("root:") ? 1 : 0) + (left?.fileStarts ?? 0) + (right?.fileStarts ?? 0)
74+ )
75+ throw Error("inconsistent sequence aggregate");
76+ return node;
77+ }
78+ #state(state: SequenceState): void {
79+ if (
80+ !state ||
81+ !safe(state.next) ||
82+ state.next > 2 ** 32 ||
83+ (state.map !== null && !safe(state.map)) ||
84+ (state.root !== null && (!safe(state.root) || state.root >= state.next)) ||
85+ (state.root === null) !== (state.next === 0) ||
86+ (state.map === null) !== (state.root === null)
87+ )
88+ throw Error("invalid sequence state");
89+ if (state.root !== null && this.get(state.map, state.root).parent !== null)
90+ throw Error("invalid sequence root parent");
91+ }
92+ set(map: Page, id: number, node: SequenceNode): number {
93+ if (node.id !== id) throw Error("sequence node identity mismatch");
94+ return this.#locator.set(map, id, node);
95+ }
96+ rank(state: SequenceState, id: number): number {
97+ this.#state(state);
98+ let node = this.get(state.map, id);
99+ if (!node) throw Error("unknown marker");
100+ const count = (id: Page): number => (id === null ? 0 : this.get(state.map, id).count);
101+ let rank = count(node.left),
102+ steps = 0;
103+ while (node.parent !== null) {
104+ if (++steps > 64) throw Error("invalid parent chain");
105+ const parent = this.get(state.map, node.parent);
106+ if (parent.right === node.id) rank += count(parent.left) + 1;
107+ else if (parent.left !== node.id) throw Error("broken parent edge");
108+ node = parent;
109+ }
110+ if (node.id !== state.root) throw Error("wrong root");
111+ return rank;
112+ }
113+ /** Nearest file start before a marker, using subtree counts instead of line ancestry. */
114+ fileAt(state: SequenceState, id: number): string {
115+ const target = this.rank(state, id);
116+ const seek = (id: Page, offset: number, depth: number): string | undefined => {
117+ if (id === null || offset > target) return undefined;
118+ if (depth > 64) throw Error("invalid file lookup depth");
119+ const node = this.get(state.map, id);
120+ if (!node.fileStarts) return undefined;
121+ const leftCount = node.left === null ? 0 : this.get(state.map, node.left).count;
122+ const at = offset + leftCount;
123+ if (at <= target) {
124+ const right = seek(node.right, at + 1, depth + 1);
125+ if (right !== undefined) return right;
126+ if (node.text.startsWith("root:")) return node.text.slice(5);
127+ }
128+ return seek(node.left, offset, depth + 1);
129+ };
130+ const file = seek(state.root, 0, 0);
131+ if (file === undefined) throw Error("missing projected file start");
132+ return file;
133+ }
134+ update(state: SequenceState, operation: SequenceOperation): SequenceState {
135+ this.#state(state);
136+ return this.storage.transaction(() => {
137+ let map = state.map;
138+ // One operation changes an AVL path plus rotation neighbors, not history.
139+ // Keep only those decoded records and persist each final record once.
140+ const changed = new Map<number, SequenceNode>();
141+ const get = (id: number): SequenceNode => changed.get(id) ?? this.get(map, id);
142+ const put = (node: SequenceNode): number => {
143+ changed.set(node.id, node);
144+ return node.id;
145+ };
146+ const height = (id: Page): number => (id === null ? 0 : get(id).height);
147+ const count = (id: Page): number => (id === null ? 0 : get(id).count);
148+ const visible = (id: Page): number => (id === null ? 0 : get(id).visible);
149+ const fileStarts = (id: Page): number => (id === null ? 0 : get(id).fileStarts);
150+ const parent = (id: Page, p: Page): void => {
151+ if (id !== null) {
152+ const node = get(id);
153+ if (node.parent !== p) put({ ...node, parent: p });
154+ }
155+ };
156+ const save = (node: SequenceNode): number => {
157+ parent(node.left, node.id);
158+ parent(node.right, node.id);
159+ return put({
160+ ...node,
161+ height: 1 + Math.max(height(node.left), height(node.right)),
162+ count: 1 + count(node.left) + count(node.right),
163+ visible: (node.deletions === 0 ? 1 : 0) + visible(node.left) + visible(node.right),
164+ fileStarts: (node.text.startsWith("root:") ? 1 : 0) + fileStarts(node.left) + fileStarts(node.right),
165+ });
166+ };
167+ const rotateLeft = (id: number): number => {
168+ const a = get(id),
169+ b = get(a.right!);
170+ save({ ...a, right: b.left, parent: b.id });
171+ return save({ ...b, left: a.id, parent: a.parent });
172+ };
173+ const rotateRight = (id: number): number => {
174+ const a = get(id),
175+ b = get(a.left!);
176+ save({ ...a, left: b.right, parent: b.id });
177+ return save({ ...b, right: a.id, parent: a.parent });
178+ };
179+ const balance = (id: number): number => {
180+ const node = get(id);
181+ if (height(node.left) - height(node.right) > 1) {
182+ const left = get(node.left!);
183+ if (height(left.left) < height(left.right)) save({ ...node, left: rotateLeft(left.id) });
184+ return rotateRight(id);
185+ }
186+ if (height(node.right) - height(node.left) > 1) {
187+ const right = get(node.right!);
188+ if (height(right.right) < height(right.left)) save({ ...node, right: rotateRight(right.id) });
189+ return rotateLeft(id);
190+ }
191+ return id;
192+ };
193+ let root = state.root,
194+ next = state.next;
195+ if (operation.kind === "insert") {
196+ const { rank, text } = operation;
197+ if (!Number.isInteger(rank) || rank < 0 || rank > count(root) || next >= 2 ** 32) throw Error("invalid insert");
198+ const added = next++;
199+ const insert = (id: Page, at: number, p: Page): number => {
200+ if (id === null)
201+ return put({
202+ id: added,
203+ parent: p,
204+ left: null,
205+ right: null,
206+ text,
207+ deletions: 0,
208+ height: 1,
209+ count: 1,
210+ visible: 1,
211+ fileStarts: text.startsWith("root:") ? 1 : 0,
212+ });
213+ const node = get(id),
214+ leftCount = count(node.left);
215+ if (at <= leftCount) save({ ...node, left: insert(node.left, at, id) });
216+ else save({ ...node, right: insert(node.right, at - leftCount - 1, id) });
217+ return balance(id);
218+ };
219+ root = insert(root, rank, null);
220+ parent(root, null);
221+ } else if (operation.kind === "move") {
222+ const { from, length, to } = operation;
223+ const total = count(root);
224+ if (
225+ ![from, length, to].every(Number.isSafeInteger) ||
226+ from < 0 ||
227+ length < 0 ||
228+ from + length > total ||
229+ to < 0 ||
230+ to > total - length
231+ )
232+ throw Error("invalid move range");
233+ // Join two AVL trees around one existing stable marker.
234+ const join = (left: Page, pivot: number, right: Page): number => {
235+ if (height(left) > height(right) + 1) {
236+ const node = get(left!);
237+ save({ ...node, right: join(node.right, pivot, right) });
238+ const result = balance(left!);
239+ parent(result, null);
240+ return result;
241+ }
242+ if (height(right) > height(left) + 1) {
243+ const node = get(right!);
244+ save({ ...node, left: join(left, pivot, node.left) });
245+ const result = balance(right!);
246+ parent(result, null);
247+ return result;
248+ }
249+ return save({ ...get(pivot), left, right, parent: null });
250+ };
251+ const split = (id: Page, at: number): [Page, Page] => {
252+ if (id === null) return [null, null];
253+ const node = get(id),
254+ size = count(node.left);
255+ if (at <= size) {
256+ const [left, middle] = split(node.left, at);
257+ const right = join(middle, id, node.right);
258+ parent(left, null);
259+ parent(right, null);
260+ return [left, right];
261+ }
262+ const [middle, right] = split(node.right, at - size - 1);
263+ const left = join(node.left, id, middle);
264+ parent(left, null);
265+ parent(right, null);
266+ return [left, right];
267+ };
268+ const merge = (left: Page, right: Page): Page => {
269+ if (left === null) {
270+ parent(right, null);
271+ return right;
272+ }
273+ if (right === null) {
274+ parent(left, null);
275+ return left;
276+ }
277+ const [pivot, rest] = split(right, 1);
278+ return join(left, pivot!, rest);
279+ };
280+ if (length && from !== to) {
281+ const [left, tail] = split(root, from);
282+ const [moved, right] = split(tail, length);
283+ const remaining = merge(left, right);
284+ const [before, after] = split(remaining, to);
285+ root = merge(merge(before, moved), after);
286+ parent(root, null);
287+ }
288+ } else if (operation.kind === "visibility") {
289+ let node = get(operation.id);
290+ if (
291+ !node ||
292+ !Number.isInteger(operation.delta) ||
293+ Math.abs(operation.delta) !== 1 ||
294+ node.deletions + operation.delta < 0
295+ )
296+ throw Error("invalid visibility");
297+ save({ ...node, deletions: node.deletions + operation.delta });
298+ while (node.parent !== null) {
299+ node = get(node.parent);
300+ save(node);
301+ }
302+ } else throw Error("unknown operation");
303+ for (const [id, node] of changed) map = this.set(map, id, node);
304+ return { map, root, next };
305+ });
306+ }
307+ *range(
308+ state: SequenceState,
309+ from: number,
310+ to: number,
311+ visibleOnly = true,
312+ ): IterableIterator<{ id: number; text: string }> {
313+ this.#state(state);
314+ if (!safe(from) || !safe(to) || from > to) throw Error("invalid sequence range");
315+ const visit = function* (
316+ seq: ProjectionSequence,
317+ id: Page,
318+ offset: number,
319+ ): IterableIterator<{ id: number; text: string }> {
320+ if (id === null) return;
321+ const node = seq.get(state.map, id);
322+ if ((visibleOnly && !node.visible) || offset >= to || offset + node.count <= from) return;
323+ const leftCount = node.left === null ? 0 : seq.get(state.map, node.left).count;
324+ yield* visit(seq, node.left, offset);
325+ const at = offset + leftCount;
326+ if (at >= from && at < to && (!visibleOnly || node.deletions === 0)) yield { id: node.id, text: node.text };
327+ yield* visit(seq, node.right, at + 1);
328+ };
329+ yield* visit(this, state.root, 0);
330+ }
331+ *values(state: SequenceState, visibleOnly = true): IterableIterator<{ id: number; text: string }> {
332+ this.#state(state);
333+ const visit = function* (seq: ProjectionSequence, id: Page): IterableIterator<{ id: number; text: string }> {
334+ if (id === null) return;
335+ const node = seq.get(state.map, id);
336+ if (visibleOnly && node.visible === 0) return;
337+ yield* visit(seq, node.left);
338+ if (!visibleOnly || node.deletions === 0) yield { id: node.id, text: node.text };
339+ yield* visit(seq, node.right);
340+ };
341+ yield* visit(this, state.root);
342+ }
343+}
MCP
compare on this site's /mcp
CLI
abso diff 142 143 src/engine/experimental/projection-sequence.ts --json
JSON
/christopher/abso-core/compare/142..143?file=src%2Fengine%2Fexperimental%2Fprojection-sequence.ts&format=json