UNPKG

@code-yeongyu/senpi

Version:

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

247 lines • 9.97 kB
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) { if (value !== null && value !== void 0) { if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected."); var dispose, inner; if (async) { if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined."); dispose = value[Symbol.asyncDispose]; } if (dispose === void 0) { if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined."); dispose = value[Symbol.dispose]; if (async) inner = dispose; } if (typeof dispose !== "function") throw new TypeError("Object not disposable."); if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } }; env.stack.push({ value: value, dispose: dispose, async: async }); } else if (async) { env.stack.push({ async: true }); } return value; }; var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) { return function (env) { function fail(e) { env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e; env.hasError = true; } var r, s = 0; function next() { while (r = env.stack.pop()) { try { if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next); if (r.dispose) { var result = r.dispose.call(r.value); if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); }); } else s |= 1; } catch (e) { fail(e); } } if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve(); if (env.hasError) throw env.error; } return next(); }; })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }); import { CompactCommitDependencies } from "./compact-commit-dependencies.js"; import { CompactIndexTree } from "./compact-index-tree.js"; import { CompactPagedTree } from "./compact-paged-tree.js"; import { CompactReadError, CompactReadLease } from "./compact-read-lease.js"; import { CompactValues } from "./compact-values.js"; import { CompactIndexCompactor } from "./compact-index-compactor.js"; import { readIndexEntries, readIndexPage } from "./compact-index-traversal.js"; /** Pending overlays stay resident; settled keys and navigation paths live in spillable nodes. */ export class CompactContentIndex { constructor(store, leavesOf, threshold) { this.pages = new Set(); /** Overlay keys with no pending reference: the compactor selects only from these, in key order. */ this.ready = new Set(); this.cuts = new WeakMap(); this.tombstones = 0; this.maskUnits = 0; this.residentPages = 0; this.peakResidentPages = 0; this.store = store; this.leavesOf = leavesOf; this.values = new CompactValues(store); this.bases = new CompactPagedTree(store, ({ page }) => [page.owner]); this.overlays = new CompactIndexTree((value, live, pins) => store.adjust(value.owner, live, pins)); this.pending = new CompactCommitDependencies(store, (key) => { this.ready.add(key); this.maybeCompact(); }); this.compactor = new CompactIndexCompactor(this, threshold); } set(key, row) { const value = this.values.own(row); const refs = [value.owner]; try { const keyRef = this.values.own(key); refs.push(keyRef.owner); const payload = this.store.text.own(this.leavesOf(row)); refs.push(payload); this.put(key, { key: keyRef, value, payload }, new Map(), false); } finally { for (const ref of refs) this.store.adjust(ref, -1, 0); } } patch(key, fields) { const current = this.overlays.get(this.overrides, key); const patch = new Map(current?.patch); const owned = []; try { for (const [name, value] of Object.entries(fields)) { const ref = this.values.own(value); owned.push(ref); patch.set(name, ref); } this.put(key, current?.row, patch, false); } finally { for (const ref of owned) this.store.adjust(ref.owner, -1, 0); } } delete(key) { this.put(key, undefined, new Map(), true); } freeze() { this.store.assertReadable(); const cut = { base: this.base, overrides: this.overrides }; this.bases.adjust(cut.base, 0, 1); this.overlays.adjust(cut.overrides, 0, 1); this.cuts.set(cut, new CompactReadLease(() => { this.bases.adjust(cut.base, 0, -1); this.overlays.adjust(cut.overrides, 0, -1); }, () => this.store.assertReadable())); return cut; } release(cut) { this.lease(cut).release(); } /** Point descriptor read under a cut lease; never hydrates a message or its block collection. */ async lookup(cut, key) { return this.lease(cut).run(async () => { const env_1 = { stack: [], error: void 0, hasError: false }; try { const change = this.overlays.get(cut.overrides, key); if (change?.deleted) return undefined; if (change?.row) return change.row; const old = await this.bases.get(cut.base, key); if (!old) return undefined; const held = __addDisposableResource(env_1, await this.readPage(old.page), false); return held.rows[old.offset]; } catch (e_1) { env_1.error = e_1; env_1.hasError = true; } finally { __disposeResources(env_1); } }); } /** Compatibility assembly only; recovery descriptors use page(), never this whole-row view. */ async entries(cut) { return this.lease(cut).run(() => readIndexEntries(this, cut)); } async page(cut, cursor) { return this.lease(cut).run(() => readIndexPage(this, cut, cursor)); } async readPage(page) { if (page.bytes > 65_536) throw new CompactReadError("size_limit"); if (page.count > 128) throw new CompactReadError("count_limit"); this.peakResidentPages = Math.max(this.peakResidentPages, ++this.residentPages); try { const data = await this.store.readFile(page.path, page.bytes, "page_read"); const rows = JSON.parse(data.toString("utf8")); if (rows.length > 128) throw new CompactReadError("count_limit"); if (rows.length !== page.count) throw new CompactReadError("ownership"); return { rows, [Symbol.dispose]: () => { this.residentPages--; }, }; } catch (error) { this.residentPages--; throw error; } } status() { return { overrideSlots: this.overrides?.size ?? 0, deletionMasks: this.tombstones, deletionMaskBytes: this.maskUnits * 2, basePages: this.pages.size, baseRows: this.base?.size ?? 0, residentIndexNodes: this.overlays.residentNodes, allocatedIndexNodes: this.bases.allocatedNodes + this.overlays.allocatedNodes, residentPages: this.residentPages + this.values.residentPages, peakResidentPages: this.peakResidentPages + this.values.peakResidentPages, }; } maybeCompact() { this.compactor.schedule(); } drop(key) { const old = this.overlays.get(this.overrides, key); if (!old) return; if (old.deleted) { this.tombstones--; this.maskUnits -= key.length; } const next = this.overlays.set(this.overrides, key, undefined); this.overlays.adjust(this.overrides, -1, 0); this.overrides = next; this.pending.delete(key); this.ready.delete(key); } put(key, row, patch, deleted) { const patchValue = patch.size ? this.values.record(patch) : undefined; const refs = patchValue ? [patchValue.owner] : []; if (row) refs.push(row.key.owner, row.value.owner, row.payload); const owner = this.store.text.own(refs); this.drop(key); const next = this.overlays.set(this.overrides, key, { row, patch, patchValue, owner, deleted }); this.overlays.adjust(this.overrides, -1, 0); this.overrides = next; this.store.adjust(owner, -1, 0); if (patchValue) this.store.adjust(patchValue.owner, -1, 0); if (deleted) { this.tombstones++; this.maskUnits += key.length; } this.pending.set(key, refs); if (!this.pending.has(key)) this.ready.add(key); this.maybeCompact(); } lease(cut) { const lease = this.cuts.get(cut); if (!lease) throw new CompactReadError("ownership"); return lease; } } //# sourceMappingURL=compact-content-index.js.map