@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
86 lines • 3.72 kB
JavaScript
import { CompactIndexTree } from "./compact-index-tree.js";
import { CompactReadError } from "./compact-read-lease.js";
import { CompactValues } from "./compact-values.js";
/**
* Active AVL headers project only changed paths into bounded, spillable nodes. Frozen and settled
* owners retain the disk-addressable root, not the hot navigation tree or an array of its values.
*/
export class CompactComponentRoots {
constructor(store, leavesOf) {
this.refs = new WeakMap();
this.store = store;
this.values = new CompactValues(store);
this.leavesOf = leavesOf;
this.tree = new CompactIndexTree((value, live, pins) => store.adjust(value.owner, live, pins));
}
get residentNodes() {
return this.tree.residentNodes;
}
set(root, key, value) {
const ref = value && this.values.own(value);
const owner = ref && value ? this.store.text.own([ref.owner, ...this.leavesOf(value)]) : 0;
const header = this.tree.set(root.header, key, value && ref ? { value, ref, owner } : undefined);
if (ref)
this.store.adjust(ref.owner, -1, 0);
this.store.adjust(owner, -1, 0);
const created = [];
const next = header ? this.project(header, created) : undefined;
if (next)
this.store.adjust(next.owner, 1, 0);
for (const ref of created)
this.store.adjust(ref.owner, -1, 0);
this.tree.adjust(root.header, -1, 0);
if (root.ref)
this.store.adjust(root.ref.owner, -1, 0);
return { header, ref: next };
}
/** Header reclamation is off the acceptance path; the transferred payload root already owns every value. */
release(root) {
this.store.enqueue("reclaim", async () => {
this.tree.adjust(root.header, -1, 0);
if (root.ref)
this.store.adjust(root.ref.owner, -1, 0);
});
}
/**
* The incremental reader (S03b-4): rows in key order, each value as the ref its walk starts from, with one
* tree page resident per open level. Nothing here assembles a value; `entries` does, for the whole-view callers.
*/
async *rows(ref, valid) {
if (!ref)
return;
valid();
const page = JSON.parse(await this.store.readLeaf(ref.node));
valid();
yield* this.rows(page.left, valid);
const key = await this.values.read(page.key, valid);
if (typeof key !== "string")
throw new CompactReadError("ownership");
yield [key, page.value];
yield* this.rows(page.right, valid);
}
async *entries(ref, valid) {
for await (const [key, value] of this.rows(ref, valid)) {
const row = (await this.values.read(value, valid));
valid();
yield [key, row];
}
}
project(node, created) {
const known = this.refs.get(node);
if (known)
return known;
const left = node.left && this.project(node.left, created);
const right = node.right && this.project(node.right, created);
// Component keys are fixed-width ordinals; unchanged path keys need no new value owner.
const key = { node: 0, owner: 0, value: node.key };
const page = this.store.createLeaf(JSON.stringify({ key, value: node.value.ref, left, right }), true);
const owner = this.store.text.own([page, key.owner, node.value.owner, left?.owner ?? 0, right?.owner ?? 0]);
this.store.adjust(page, -1, 0);
const ref = { node: page, owner };
this.refs.set(node, ref);
created.push(ref);
return ref;
}
}
//# sourceMappingURL=compact-component-roots.js.map