UNPKG

@code-yeongyu/senpi

Version:

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

227 lines • 9.29 kB
import { catalogDigest, catalogDigester, storedBytes } from "./compact-catalog-codec.js"; import { COMPACT_LEAF_CODE_UNITS, } from "./compact-content-store.js"; import { CompactReadError } from "./compact-read-lease.js"; import { FRAGMENT_MAX_BYTES, openFrozenBytes } from "./compact-recovery-bytes.js"; import { CompactStorageReadError } from "./compact-storage-io.js"; /** * The stored bytes one staging owner may hold staged and not yet adopted: B_worker (S12 design section 3), the host's * capacity for worker transfers. `begin` reserves a transfer's whole declared length before any leaf exists, so a * refusal allocates nothing; the bytes leave the bound when the content is adopted, released or aborted. */ export const TRANSFER_STAGING_MAX_BYTES = 64 * 1024 * 1024; /** Code units one fragment carries: D (`FRAGMENT_MAX_BYTES`) stored bytes, two per UTF-16 code unit. */ const FRAGMENT_MAX_UNITS = FRAGMENT_MAX_BYTES / 2; export class TransferStagingError extends Error { constructor(reason) { super(`transfer staging refused (${reason})`); this.name = "TransferStagingError"; this.reason = reason; } } /** A read by identity found its segment gone (`segment_missing`) or holding other bytes (`segment_mismatch`). */ export class StagedReadError extends Error { constructor(reason, options) { super(`staged read refused (${reason})`, options); this.name = "StagedReadError"; this.reason = reason; } } /** * One logical value arriving in fragments (S12 design section 3): its stored length is declared up front, each * fragment becomes owned immutable leaves as it arrives, and nothing is visible until `commit`. A refusal aborts the * transfer and releases what it staged; `abort` is idempotent. */ class StagedTransfer { constructor(store, entry, settle) { this.leaves = []; this.leafDigests = []; this.fed = 0; this.open = true; this.store = store; this.entry = entry; this.settle = settle; this.digester = catalogDigester("leaf", entry.length); } append(text) { this.assertOpen(); const bytes = text.length * 2; if (bytes > FRAGMENT_MAX_BYTES) this.refuse("fragment_over_limit"); if (this.fed + bytes > this.entry.length) this.refuse("length_mismatch"); try { for (let at = 0; at < text.length; at += COMPACT_LEAF_CODE_UNITS) { const piece = text.slice(at, at + COMPACT_LEAF_CODE_UNITS); this.leaves.push(this.store.createLeaf(piece)); this.leafDigests.push(catalogDigest("leaf", storedBytes(piece))); } } catch (cause) { this.abort(); throw cause; } this.digester.update(storedBytes(text)); this.fed += bytes; } commit() { this.assertOpen(); if (this.fed !== this.entry.length) this.refuse("length_mismatch"); this.open = false; const content = { ref: { variant: "staged", entry: { ...this.entry, digest: this.digester.finish() } }, leaves: [...this.leaves], leafDigests: [...this.leafDigests], }; this.settle(content); return content; } abort() { if (!this.open) return; this.open = false; for (const leaf of this.leaves) this.store.adjust(leaf, -1, 0); this.settle(undefined); } assertOpen() { if (!this.open) throw new TransferStagingError("staging_closed"); } refuse(reason) { this.abort(); throw new TransferStagingError(reason); } } /** * The shared bounded staging substrate (S12a; S12b's worker transfers and S13's leases reuse it): content is staged in * bounded fragments into one content store, which spills it like any payload leaf, and named by a `FrozenRef`. Staged * content stays unaccepted until its owner adopts it; `close` aborts every open transfer and releases everything never * adopted before the store retires. */ export class TransferStaging { constructor(store, maxBytes = TRANSFER_STAGING_MAX_BYTES) { this.transfers = new Set(); this.staged = new Map(); this.reserved = 0; this.closed = false; this.store = store; this.maxBytes = maxBytes; } /** Stored bytes reserved by open transfers and committed content not yet adopted. */ get stagedBytes() { return this.reserved; } /** Stored bytes of this store resident in RAM, staged and adopted alike. */ get residentBytes() { return this.store.status().residentPayloadBytes; } begin(kind, id, length) { if (this.closed) throw new TransferStagingError("staging_closed"); if (!Number.isSafeInteger(length) || length < 0 || length % 2 !== 0) throw new TransferStagingError("length_mismatch"); if (this.reserved + length > this.maxBytes) throw new TransferStagingError("staging_over_byte_limit"); this.reserved += length; const transfer = new StagedTransfer(this.store, { kind, id, length }, (content) => { this.transfers.delete(transfer); if (content) this.staged.set(content, length); else this.reserved -= length; }); this.transfers.add(transfer); return transfer; } /** One whole text staged as fragments of at most D stored bytes; a refusal leaves nothing staged. */ stage(kind, id, text) { const transfer = this.begin(kind, id, text.length * 2); try { for (let at = 0; at < text.length; at += FRAGMENT_MAX_UNITS) transfer.append(text.slice(at, at + FRAGMENT_MAX_UNITS)); return transfer.commit(); } catch (cause) { transfer.abort(); throw cause; } } /** The acceptor takes the content: its bytes leave the staging bound and its leaves stay owned by the acceptor. */ adopt(content) { const bytes = this.staged.get(content); if (bytes === undefined) throw new CompactReadError("ownership"); this.staged.delete(content); this.reserved -= bytes; } /** Drops the owner's live reference; content never adopted also leaves the staging bound. */ release(content) { const bytes = this.staged.get(content); if (bytes !== undefined) { this.staged.delete(content); this.reserved -= bytes; } for (const leaf of content.leaves) this.store.adjust(leaf, -1, 0); } /** Releases content only while it is still staged; adopted content is its acceptor's to release. */ abandon(content) { if (this.staged.has(content)) this.release(content); } /** A reader's pin: the leaves outlive every owner's release until the pin is returned. */ pin(content, pins) { for (const leaf of content.leaves) this.store.adjust(leaf, 0, pins); } /** The content as S11a byte pages, its digest verified against the stored leaves before any range is served. */ open(content, valid = () => { }) { return openFrozenBytes(content.ref.entry, content.leaves, (leaf) => this.store.readLeaf(leaf), valid); } /** * The content's text leaf by leaf (at most 64 KiB stored, under D), each leaf verified against its digest before it * is yielded: a missing or changed segment is a typed `StagedReadError` and no other bytes are ever returned. */ async *chunks(content, valid = () => { }) { let length = 0; for (const [index, leaf] of content.leaves.entries()) { let text; try { text = await this.store.readLeaf(leaf); } catch (cause) { if (cause instanceof CompactStorageReadError || cause instanceof CompactReadError) throw new StagedReadError("segment_missing", { cause }); throw cause; } valid(); if (catalogDigest("leaf", storedBytes(text)) !== content.leafDigests[index]) throw new StagedReadError("segment_mismatch"); length += text.length * 2; yield text; } if (length !== content.ref.entry.length) throw new StagedReadError("segment_mismatch"); } /** Moves the content's resident leaves to the spool, so content kept only for readers holds no RAM. */ spill(content) { for (const leaf of content.leaves) this.store.enqueue("spill_write", () => this.store.flushLeaf(leaf)); } status() { return { ...this.store.status(), stagedBytes: this.reserved, openTransfers: this.transfers.size }; } idle() { return this.store.idle(); } close() { this.closed = true; for (const transfer of [...this.transfers]) transfer.abort(); for (const content of [...this.staged.keys()]) this.release(content); return this.store.retire(); } } //# sourceMappingURL=compact-transfer-staging.js.map