UNPKG

@code-yeongyu/senpi

Version:

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

53 lines • 2.03 kB
import { readFile, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { z } from "zod"; import { CompactAdmissionError } from "./compact-admission-error.js"; /** * Per socket epoch (RULING-113): 16 live producers; terminal rows in an LRU of 128 rows / 16 MiB reserved disk * bytes; an evicted ID keeps only its digest in a 4,096-entry ring that answers admission_retired. */ export const ADMISSION_LIMITS = { entries: 128, bytes: 16 * 1024 * 1024, hot: 16, row: 131_584, retired: 4096, }; const rowSchema = z.object({ fingerprint: z.string().regex(/^[0-9a-f]{64}$/), state: z.enum(["pending", "complete", "cancelled", "failed"]), failure: z.literal("storage_unavailable").optional(), answer: z.string().max(65_536).optional(), }); export async function readAdmissionRow(epoch, key) { const failure = epoch.failures?.get(key); if (failure) return failure; try { const text = await readFile(join(await epoch.directory, key), "utf8"); if (Buffer.byteLength(text) > ADMISSION_LIMITS.row) throw new CompactAdmissionError("storage_unavailable"); return rowSchema.parse(JSON.parse(text)); } catch (cause) { if (cause instanceof Error && "code" in cause && cause.code === "ENOENT") return undefined; if (cause instanceof CompactAdmissionError) throw cause; throw new CompactAdmissionError("storage_unavailable"); } } export async function writeAdmissionRow(epoch, key, row) { const text = JSON.stringify(row); if (Buffer.byteLength(text) > ADMISSION_LIMITS.row) throw new CompactAdmissionError("storage_unavailable"); try { await writeFile(join(await epoch.directory, key), text, { mode: 0o600, flush: true }); } catch (cause) { if (!(cause instanceof Error)) throw cause; throw new CompactAdmissionError("storage_unavailable"); } } //# sourceMappingURL=compact-admission-storage.js.map