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 151 to 318

Compare

+127 −105 in 3 places
Lines 20–26 at 318 (were 20–26 at 151)
2020 readonly next: number;
2121}
2222export type SequenceOperation =
23− | { kind: "insert"; rank: number; text: string }
23+ | { kind: "insert"; rank: number; text: string; hidden?: boolean }
2424 | { kind: "visibility"; id: number; delta: number }
2525 | { kind: "move"; from: number; length: number; to: number };
2626const safe = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;
Lines 132–163 at 318 (were 132–144 at 151)
132132 return file;
133133 }
134134 update(state: SequenceState, operation: SequenceOperation): SequenceState {
135+ return this.updateMany(state, [operation]);
136+ }
137+ /** A small bounded group shares changed AVL nodes and one locator flush. */
138+ updateMany(state: SequenceState, operations: readonly SequenceOperation[]): SequenceState {
139+ if (operations.length < 1 || operations.length > 16) throw Error("invalid sequence operation batch");
135140 this.#state(state);
136141 return this.storage.transaction(() => {
137142 let map = state.map;
138143 // One operation changes an AVL path plus rotation neighbors, not history.
139144 // Keep only those decoded records and persist each final record once.
140145 const changed = new Map<number, SequenceNode>();
141− const get = (id: number): SequenceNode => changed.get(id) ?? this.get(map, id);
146+ // The immutable input map is fixed for this batch. Cache only its bounded reads;
147+ // changed nodes always take precedence and the cache never survives a transaction.
148+ const original = new Map<number, SequenceNode>();
149+ let originalBytes = 0;
150+ const get = (id: number): SequenceNode => {
151+ const found = changed.get(id) ?? original.get(id);
152+ if (found) return found;
153+ const node = this.get(map, id);
154+ const bytes = 192 + node.text.length * 2;
155+ if (original.size < 256 && originalBytes + bytes <= 65536) {
156+ original.set(id, node);
157+ originalBytes += bytes;
158+ }
159+ return node;
160+ };
142161 const put = (node: SequenceNode): number => {
143162 changed.set(node.id, node);
144163 return node.id;
Lines 211–327 at 318 (were 192–305 at 151)
192211 };
193212 let root = state.root,
194213 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);
214+ for (const operation of operations) {
215+ if (operation.kind === "insert") {
216+ const { rank, text } = operation;
217+ if (!Number.isInteger(rank) || rank < 0 || rank > count(root) || next >= 2 ** 32)
218+ throw Error("invalid insert");
219+ const added = next++;
220+ const insert = (id: Page, at: number, p: Page): number => {
221+ if (id === null)
222+ return put({
223+ id: added,
224+ parent: p,
225+ left: null,
226+ right: null,
227+ text,
228+ deletions: operation.hidden ? 1 : 0,
229+ height: 1,
230+ count: 1,
231+ visible: operation.hidden ? 0 : 1,
232+ fileStarts: text.startsWith("root:") ? 1 : 0,
233+ });
234+ const node = get(id),
235+ leftCount = count(node.left);
236+ if (at <= leftCount) save({ ...node, left: insert(node.left, at, id) });
237+ else save({ ...node, right: insert(node.right, at - leftCount - 1, id) });
238+ return balance(id);
239+ };
240+ root = insert(root, rank, null);
241+ parent(root, null);
242+ } else if (operation.kind === "move") {
243+ const { from, length, to } = operation;
244+ const total = count(root);
245+ if (
246+ ![from, length, to].every(Number.isSafeInteger) ||
247+ from < 0 ||
248+ length < 0 ||
249+ from + length > total ||
250+ to < 0 ||
251+ to > total - length
252+ )
253+ throw Error("invalid move range");
254+ // Join two AVL trees around one existing stable marker.
255+ const join = (left: Page, pivot: number, right: Page): number => {
256+ if (height(left) > height(right) + 1) {
257+ const node = get(left!);
258+ save({ ...node, right: join(node.right, pivot, right) });
259+ const result = balance(left!);
260+ parent(result, null);
261+ return result;
262+ }
263+ if (height(right) > height(left) + 1) {
264+ const node = get(right!);
265+ save({ ...node, left: join(left, pivot, node.left) });
266+ const result = balance(right!);
267+ parent(result, null);
268+ return result;
269+ }
270+ return save({ ...get(pivot), left, right, parent: null });
271+ };
272+ const split = (id: Page, at: number): [Page, Page] => {
273+ if (id === null) return [null, null];
274+ const node = get(id),
275+ size = count(node.left);
276+ if (at <= size) {
277+ const [left, middle] = split(node.left, at);
278+ const right = join(middle, id, node.right);
279+ parent(left, null);
280+ parent(right, null);
281+ return [left, right];
282+ }
283+ const [middle, right] = split(node.right, at - size - 1);
284+ const left = join(node.left, id, middle);
258285 parent(left, null);
259286 parent(right, null);
260287 return [left, right];
288+ };
289+ const merge = (left: Page, right: Page): Page => {
290+ if (left === null) {
291+ parent(right, null);
292+ return right;
293+ }
294+ if (right === null) {
295+ parent(left, null);
296+ return left;
297+ }
298+ const [pivot, rest] = split(right, 1);
299+ return join(left, pivot!, rest);
300+ };
301+ if (length && from !== to) {
302+ const [left, tail] = split(root, from);
303+ const [moved, right] = split(tail, length);
304+ const remaining = merge(left, right);
305+ const [before, after] = split(remaining, to);
306+ root = merge(merge(before, moved), after);
307+ parent(root, null);
261308 }
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;
309+ } else if (operation.kind === "visibility") {
310+ let node = get(operation.id);
311+ if (
312+ !node ||
313+ !Number.isInteger(operation.delta) ||
314+ Math.abs(operation.delta) !== 1 ||
315+ node.deletions + operation.delta < 0
316+ )
317+ throw Error("invalid visibility");
318+ save({ ...node, deletions: node.deletions + operation.delta });
319+ while (node.parent !== null) {
320+ node = get(node.parent);
321+ save(node);
272322 }
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");
323+ } else throw Error("unknown operation");
324+ }
303325 // Keep locator staging bounded even if a valid split/merge changes more
304326 // than one batch. The enclosing transaction commits all batches together.
305327 let batch: Array<readonly [number, SequenceNode]> = [];
MCP
compare on this site's /mcp
CLI
abso diff 151 318 src/engine/experimental/projection-sequence.ts --json
JSON
/christopher/abso-core/compare/151..318?file=src%2Fengine%2Fexperimental%2Fprojection-sequence.ts&format=json