UNPKG

@code-yeongyu/senpi

Version:

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

162 lines • 6.87 kB
import { CompactReadError } from "./compact-read-lease.js"; import { CompactSpoolStorage } from "./compact-spool-storage.js"; /** Byte bound of every segment file, value batches and compaction-round node sets alike (RULING-54). */ export const COMPACT_SEGMENT_BYTES = 65_536; /** * Segment packing of compact current-state storage (senpi#4272, RULING-47): a flush batch is a run of * spool files of at most `COMPACT_SEGMENT_BYTES` each (RULING-54), and each leaf is read back by offset * from the one file holding it. A segment is deleted once its last live leaf is * released and no read of it is in flight. When its live bytes fall below half its size it is * rewritten once, so dead bytes on disk stay at most the live bytes of the segments holding them; * a failed rewrite halts spilling and keeps the old segment readable. */ export class CompactSegmentStorage extends CompactSpoolStorage { /** Writes the texts back to back as one new file; undefined when the write failed and was reported. */ async writeSegment(texts, operation) { const data = Buffer.from(texts.join(""), "utf16le"); const path = await this.writeFile(data, operation, data.byteLength); if (path === undefined) return undefined; return { path, bytes: data.byteLength, operation, live: new Map(), liveBytes: 0, readers: 0, rewritten: false }; } /** Splits sized entries, in order, into runs of at most `COMPACT_SEGMENT_BYTES`; a larger entry runs alone. */ segmentRuns(entries) { const runs = []; let bytes = 0; for (const [entry, size] of entries) { const run = runs.at(-1); if (run && bytes + size <= COMPACT_SEGMENT_BYTES) { run.push(entry); bytes += size; } else { runs.push([entry]); bytes = size; } } return runs; } /** * Writes the texts as one run of bounded segment files, in order, and returns each text's placement * once EVERY file is durable; nothing is registered before then. A failed write discards the files * the run already wrote and returns undefined, so a batch becomes visible as a whole or not at all. */ async writeSegments(texts, operation) { const placements = []; const written = []; let complete = false; try { for (const run of this.segmentRuns(texts.map((text, n) => [n, text.length * 2]))) { const segment = await this.writeSegment(run.map((n) => texts[n]), operation); if (!segment) return undefined; written.push(segment); let offset = 0; for (const n of run) { placements[n] = [segment, offset]; offset += texts[n].length * 2; } } complete = true; return placements; } finally { // A write that failed or threw leaves the files before it unreferenced: reclaim them now. if (!complete) for (const stray of written) this.discard(stray.path, stray.bytes); } } place(segment, id, offset, bytes) { const slot = { segment, offset, bytes }; segment.live.set(id, slot); segment.liveBytes += bytes; return slot; } leave(id, slot) { const segment = slot.segment; segment.live.delete(id); segment.liveBytes -= slot.bytes; this.settle(segment); } async readSlot(slot) { const { segment, offset, bytes } = slot; segment.readers++; try { return await this.readFile(segment.path, bytes, "leaf_read", { offset, length: bytes }); } finally { segment.readers--; this.settle(segment); } } /** Deletes an empty segment no read holds, or queues the one rewrite of a mostly dead segment. */ settle(segment) { if (!segment.live.size) { if (!segment.readers) this.discard(segment.path, segment.bytes); } else if (!segment.rewritten && segment.liveBytes * 2 < segment.bytes) { segment.rewritten = true; this.enqueue("reclaim", () => this.rewrite(segment)); } } /** * Copies the live leaves into a new segment, then repoints their slots; the old file goes when unread. * Only live bytes are read (one range per adjacent run) and they count as I/O copy bytes until the end. */ async rewrite(segment) { if (!this.spillAllowed || !segment.live.size) return; segment.readers++; let next; let copied = 0; try { const moving = [...segment.live].sort(([, a], [, b]) => a.offset - b.offset); const texts = []; for (let n = 0; n < moving.length;) { const start = moving[n][1].offset; let end = start; const run = []; for (; n < moving.length && moving[n][1].offset === end; n++) { run.push(moving[n][1]); end += moving[n][1].bytes; } const data = await this.readFile(segment.path, end - start, "leaf_read", { offset: start, length: end - start, }); this.pendingIoCopyBytes += data.byteLength; copied += data.byteLength; for (const slot of run) texts.push(data.toString("utf16le", slot.offset - start, slot.offset - start + slot.bytes)); } next = await this.writeSegment(texts, segment.operation); let offset = 0; for (const [id, slot] of moving) { // A leaf released while the copy was written stays out of the new segment's live set. if (next && segment.live.delete(id)) { segment.liveBytes -= slot.bytes; next.live.set(id, slot); next.liveBytes += slot.bytes; slot.segment = next; slot.offset = offset; } offset += slot.bytes; } } catch (error) { // Retirement during the copy is an orderly abort: the epoch directory goes with the store. if (!(error instanceof CompactReadError && error.reason === "retired")) throw error; } finally { this.pendingIoCopyBytes -= copied; segment.readers--; this.settle(segment); if (next) this.settle(next); } } } //# sourceMappingURL=compact-segment-storage.js.map