UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

185 lines • 7.29 kB
import { CompactCommitDependencies } from "./compact-commit-dependencies.js"; import { COMPACT_LEAF_CODE_UNITS } from "./compact-content-store.js"; import { CompactReadError } from "./compact-read-lease.js"; /** Persistent append tree: positive refs are leaves, negative refs are nodes, zero is empty. */ export class CompactTextRoots { constructor(store) { this.nodes = new Map(); this.next = -1; this.store = store; this.pending = new CompactCommitDependencies(store, (id) => store.committed(id)); } get residentNodes() { return this.nodes.size; } /** One owned dependency root; callers retain their existing references. */ own(refs) { if (refs.length === 0) return 0; if (refs.length === 1) { const [ref] = refs; this.store.adjust(ref, 1, 0); return ref; } const middle = Math.floor(refs.length / 2); const left = this.own(refs.slice(0, middle)); const right = this.own(refs.slice(middle)); if (left === 0) return right; if (right === 0) return left; const root = this.join(left, right); this.store.adjust(left, -1, 0); this.store.adjust(right, -1, 0); return root; } units(id) { return id === 0 ? 0 : id > 0 ? this.store.leafUnits(id) : this.node(id).units; } isCommitted(id) { return this.nodes.has(id) && !this.pending.has(id); } /** Transfers one live root and one newly owned bounded leaf into the new root. */ append(root, text, structured = false) { this.units(root); const leaf = this.store.createLeaf(text, structured); if (root === 0) return leaf; const result = this.appendLeaf(root, leaf); this.store.adjust(root, -1, 0); this.store.adjust(leaf, -1, 0); return result; } /** Iterative, so a value owner chain of any depth (S03b-4) is released on the same bounded stack. */ adjust(id, live, pins) { const work = [[id, live, pins]]; for (let next = work.pop(); next; next = work.pop()) { const [id, live, pins] = next; if (id >= 0) { this.store.adjust(id, live, pins); continue; } const node = this.node(id); const beforeLive = Number(node.live > 0); const beforePin = Number(node.live === 0 && node.pins > 0); if (node.live + live < 0 || node.pins + pins < 0) throw new CompactReadError("ownership"); node.live += live; node.pins += pins; const liveChange = Number(node.live > 0) - beforeLive; const pinChange = Number(node.live === 0 && node.pins > 0) - beforePin; // Extra cuts on a live subtree do not visit children. Only category transitions propagate. if (liveChange || pinChange) work.push([node.right, liveChange, pinChange], [node.left, liveChange, pinChange]); if (node.live === 0 && node.pins === 0) { this.pending.delete(id); this.nodes.delete(id); } } } /** Bounded UTF-16 result and bounded leaf/tail count, checked before any I/O. */ async read(root, tail, offset, units, maxLeaves = 2, valid = () => this.store.assertReadable()) { this.store.assertReadable(); valid(); const length = this.units(root); if (!Number.isSafeInteger(offset) || !Number.isSafeInteger(units) || offset < 0 || units < 0 || offset + units > length + tail.length) throw new CompactReadError("range"); if (units > COMPACT_LEAF_CODE_UNITS) throw new CompactReadError("size_limit"); if (!Number.isSafeInteger(maxLeaves) || maxLeaves < 1 || maxLeaves > 2) throw new CompactReadError("count_limit"); const selected = []; const visit = (id, start, end) => { if (start >= end || id === 0) return; if (id > 0) { if (selected.length === maxLeaves) throw new CompactReadError("count_limit"); selected.push({ id, offset: start, units: end - start }); } else { const node = this.node(id); const leftUnits = this.units(node.left); visit(node.left, start, Math.min(end, leftUnits)); visit(node.right, Math.max(0, start - leftUnits), end - leftUnits); } }; visit(root, offset, Math.min(length, offset + units)); const tailUnits = units === 0 ? 0 : Math.max(0, offset + units - Math.max(offset, length)); if (selected.length + Number(tailUnits > 0) > maxLeaves) throw new CompactReadError("count_limit"); this.store.adjust(root, 0, 1); const done = this.store.beginRead(); try { let result = ""; for (const part of selected) { const text = await this.store.readLeaf(part.id); valid(); result += text.slice(part.offset, part.offset + part.units); } valid(); return result + tail.slice(Math.max(0, offset - length), Math.max(0, offset - length) + tailUnits); } finally { this.store.adjust(root, 0, -1); done(); } } node(id) { const node = this.nodes.get(id); if (!node) throw new CompactReadError("ownership"); return node; } /** Compatibility assembly visits actual leaves, including short append-only delta leaves. */ async *chunks(root, valid) { valid(); if (root === 0) return; if (root > 0) { const text = await this.read(root, "", 0, this.units(root), 1, valid); valid(); yield text; } else { const node = this.node(root); yield* this.chunks(node.left, valid); yield* this.chunks(node.right, valid); } } appendLeaf(root, leaf) { const node = this.nodes.get(root); if (!node || node.full) return this.join(root, leaf); const right = this.appendLeaf(node.right, leaf); const result = this.join(node.left, right); this.store.adjust(right, -1, 0); return result; } height(id) { return id > 0 ? 0 : this.node(id).height; } join(left, right) { const id = this.next--; this.nodes.set(id, { left, right, units: this.units(left) + this.units(right), height: 1 + Math.max(this.height(left), this.height(right)), full: this.height(left) === this.height(right) && (this.nodes.get(left)?.full ?? true) && (this.nodes.get(right)?.full ?? true), live: 1, pins: 0, }); this.store.adjust(left, 1, 0); this.store.adjust(right, 1, 0); this.pending.set(id, [left, right]); return id; } } //# sourceMappingURL=compact-text-roots.js.map