@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
155 lines • 6.21 kB
JavaScript
import { CompactPagedBatch } from "./compact-paged-batch.js";
import { CompactReadError } from "./compact-read-lease.js";
import { CompactValues } from "./compact-values.js";
/**
* Decoded-node budget of one compaction round's LRU (senpi#4559). A round's 128 rows land on up to 128 separate
* tree paths, about 1,000 nodes of about 0.9 KiB each (as accounted below) at 8,000 rows. The selection lookup and the
* batched apply walk the same paths, so the cache must hold a whole round's paths for the apply to reuse them.
* Measured at 8,000 settled rows: 32 KiB re-read 67,509 nodes from the spool, 512 KiB 58,175, 1 MiB 31,543.
*/
export const ROUND_CACHE_BYTES = 1024 * 1024;
/** Cold AVL paths live in owned storage leaves; only an operation's path is hydrated. */
export class CompactPagedTree {
constructor(store, leavesOf) {
this.dirty = [];
this.cacheBytes = 0;
this.allocatedNodes = 0;
this.store = store;
this.values = new CompactValues(store);
this.leavesOf = leavesOf;
}
adjust(root, live, pins) {
if (root)
this.store.adjust(root.ref.owner, live, pins);
}
/** A round-local `ROUND_CACHE_BYTES` LRU retains reused paths, including newly built ones; it ends with the round. */
async cached(operation) {
this.cache = new Map();
this.cacheBytes = 0;
try {
return await operation();
}
finally {
this.cache = undefined;
this.cacheBytes = 0;
}
}
async read(root) {
const cached = this.cache?.get(root.ref.node);
if (cached) {
this.cache?.delete(root.ref.node);
this.cache?.set(root.ref.node, cached);
return cached;
}
const text = await this.store.readLeaf(root.ref.node);
const node = JSON.parse(text);
const name = await this.values.read(node.key);
if (typeof name !== "string")
throw new CompactReadError("ownership");
return this.remember(root.ref.node, node, name, text.length);
}
remember(id, node, name, units) {
const existing = this.cache?.get(id);
if (existing)
return existing;
const bytes = (units + name.length) * 2 + 128;
const decoded = { node, name, bytes };
if (this.cache && bytes <= ROUND_CACHE_BYTES) {
while (this.cacheBytes + bytes > ROUND_CACHE_BYTES) {
const oldest = this.cache.entries().next().value;
if (!oldest)
break;
this.cache.delete(oldest[0]);
this.cacheBytes -= oldest[1].bytes;
}
this.cache.set(id, decoded);
this.cacheBytes += bytes;
}
return decoded;
}
async get(root, key) {
let ref = root;
while (ref) {
const { node, name } = await this.read(ref);
if (key === name)
return node.value;
ref = key < name ? node.left : node.right;
}
return undefined;
}
/** Sorted requests share each descent instead of performing one root lookup per row. */
async getMany(root, keys) {
const ordered = [...keys].sort();
const found = new Map();
const visit = async (ref, from, to) => {
if (!ref || from === to)
return;
const { node, name } = await this.read(ref);
let split = from;
while (split < to && ordered[split] < name)
split++;
await visit(node.left, from, split);
let right = split;
while (right < to && ordered[right] === name)
found.set(ordered[right++], node.value);
await visit(node.right, right, to);
};
await visit(root, 0, ordered.length);
return found;
}
async *entries(root, after) {
if (!root)
return;
const { node, name: key } = await this.read(root);
if (after === undefined || key > after) {
yield* this.entries(node.left, after);
yield [key, node.value];
}
yield* this.entries(node.right, after);
}
/** Applies one round's changes (undefined deletes) in one sorted pass; returns an owned root. */
apply(root, changes) {
return new CompactPagedBatch({
read: (ref) => this.read(ref),
make: (node) => this.make(node),
own: (name) => this.values.own(name),
adjust: (ref, live) => this.adjust(ref, live, 0),
release: (key) => this.store.adjust(key.owner, -1, 0),
}).apply(root, changes);
}
async make(node) {
const text = JSON.stringify(node);
// Decode before allocating: a failed key read must not strand a new leaf.
const name = this.cache ? await this.values.read(node.key) : undefined;
const leaf = this.store.createLeaf(text, true);
const owner = this.store.text.own([
leaf,
node.key.owner,
...this.leavesOf(node.value),
node.left?.ref.owner ?? 0,
node.right?.ref.owner ?? 0,
]);
this.store.adjust(leaf, -1, 0);
this.dirty.push(leaf);
this.allocatedNodes++;
if (this.cache && typeof name === "string")
this.remember(leaf, node, name, text.length);
return {
ref: { node: leaf, owner },
height: 1 + Math.max(node.left?.height ?? 0, node.right?.height ?? 0),
size: 1 + (node.left?.size ?? 0) + (node.right?.size ?? 0),
};
}
/**
* Commits a round as `node-` files of at most 64 KiB each, published together once the last one is
* durable (RULING-54): nodes replaced within the round were released and are skipped, so only
* survivors are written. Until then a round's built nodes stay resident, at most 384 keys x 3 nodes per
* level x (height + 1).
*/
async flush() {
const dirty = this.dirty;
this.dirty = [];
await this.store.flushLeaves(dirty, "node_write");
}
}
//# sourceMappingURL=compact-paged-tree.js.map