UNPKG

@code-yeongyu/senpi

Version:

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

166 lines • 6.38 kB
import { createHash, randomBytes } from "node:crypto"; import { createReadStream } from "node:fs"; import { mkdir, readFile, rm, stat, writeFile } from "node:fs/promises"; import { dirname } from "node:path"; import { refFilePath, teardownRefGeneration } from "./payload-ref-paths.js"; import { TOOL_CHILD_LIMITS } from "./protocol.js"; import { isRefId } from "./protocol-guard.js"; export class PayloadRefError extends Error { constructor(code, message, reason) { super(`${code}: ${message}`); this.name = "PayloadRefError"; this.code = code; this.reason = reason; } } /** * Writer side of an acknowledged payload reference. The writer owns a ref only until the receiver answers: * `ref_ack` transfers ownership (the writer forgets the file and never deletes it), `ref_nack` makes the writer * delete it. Settling the sending operation changes nothing; only generation teardown removes an unanswered ref. */ export class PayloadRefWriter { #root; #now; #pending = new Map(); #ownedBytes = 0; constructor(options) { this.generation = options.generation; this.budget = options.budget ?? TOOL_CHILD_LIMITS.refsBudget; this.#root = options.root; this.#now = options.now ?? Date.now; } get ownedBytes() { return this.#ownedBytes; } async create(payload) { const size = payload.byteLength; if (this.#ownedBytes + size > this.budget) { throw new PayloadRefError("ref_budget_exceeded", `${size} bytes on top of ${this.#ownedBytes} owned exceeds ${this.budget}`); } this.#ownedBytes += size; const ref = { refId: randomBytes(12).toString("hex"), generation: this.generation, bytes: size, sha256: createHash("sha256").update(payload).digest("hex"), }; const path = refFilePath(this.#root, ref); try { await mkdir(dirname(path), { recursive: true, mode: 0o700 }); await writeFile(path, payload, { flag: "wx", mode: 0o600 }); } catch (error) { this.#ownedBytes -= size; await rm(path, { force: true }); throw error; } this.#pending.set(ref.refId, { bytes: size, createdAtMs: this.#now() }); return ref; } /** A `ref_ack` or `ref_nack` from the receiver; an answer for a ref this writer does not hold is ignored. */ async handle(control) { const pending = this.#pending.get(control.refId); if (pending === undefined) return; this.#pending.delete(control.refId); this.#ownedBytes -= pending.bytes; if (control.type === "ref_nack") { await rm(refFilePath(this.#root, { refId: control.refId, generation: this.generation }), { force: true }); } } pendingIds() { return [...this.#pending.keys()]; } /** Refs still unanswered after `refAckMs`; reported for liveness handling, never deleted here. */ overdue() { const deadline = this.#now() - TOOL_CHILD_LIMITS.refAckMs; return [...this.#pending].filter(([, ref]) => ref.createdAtMs <= deadline).map(([refId]) => refId); } async teardown() { this.#pending.clear(); this.#ownedBytes = 0; await teardownRefGeneration(this.#root, this.generation); } } const isMissingFile = (error) => error instanceof Error && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR"); async function hashFile(path, signal) { const hash = createHash("sha256"); for await (const chunk of createReadStream(path)) { if (signal?.aborted) return "cancelled"; hash.update(chunk); } return hash.digest("hex"); } /** * Receiver side. `accept` verifies size and sha256 and answers `ref_ack` (taking ownership) or `ref_nack`. * The owner deletes the file when it consumes it; a dead owner's files go with its generation's teardown. */ export class PayloadRefReader { #root; #generation; #budget; #send; #owned = new Map(); #ownedBytes = 0; constructor(options) { this.#root = options.root; this.#generation = options.generation; this.#budget = options.budget ?? TOOL_CHILD_LIMITS.refsBudget; this.#send = options.send; } async accept(ref, signal) { const reason = await this.#verify(ref, signal); if (reason !== undefined) { this.refuse(ref, reason); return { ok: false, reason }; } this.#owned.set(ref.refId, ref); this.#ownedBytes += ref.bytes; this.#send({ type: "ref_ack", refId: ref.refId }); return { ok: true }; } /** Answers `ref_nack` for a ref this side will not take, e.g. one whose job was cancelled before it was opened. */ refuse(ref, reason) { if (this.#owned.has(ref.refId)) return; this.#send({ type: "ref_nack", refId: ref.refId, reason }); } async consume(refId) { const ref = this.#owned.get(refId); if (ref === undefined) throw new PayloadRefError("ref_not_owned", `ref ${refId} is not owned by this side`); const path = refFilePath(this.#root, ref); const bytes = await readFile(path); await rm(path, { force: true }); this.#owned.delete(refId); this.#ownedBytes -= ref.bytes; return bytes; } ownedIds() { return [...this.#owned.keys()]; } async #verify(ref, signal) { if (signal?.aborted) return "cancelled"; if (ref.generation !== this.#generation || !isRefId(ref.refId) || this.#owned.has(ref.refId)) return "foreign"; if (this.#ownedBytes + ref.bytes > this.#budget) return "budget"; const path = refFilePath(this.#root, ref); try { if ((await stat(path)).size !== ref.bytes) return "corrupt"; const digest = await hashFile(path, signal); if (digest === "cancelled") return "cancelled"; return digest === ref.sha256 ? undefined : "corrupt"; } catch (error) { if (isMissingFile(error)) return "missing"; throw error; } } } //# sourceMappingURL=payload-ref.js.map