UNPKG

@code-yeongyu/senpi

Version:

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

122 lines • 4.61 kB
import { randomUUID } from "node:crypto"; import { constants } from "node:fs"; import { lstat, mkdir, open, rename, rm } from "node:fs/promises"; import { join } from "node:path"; import { getAgentDir } from "../../config.js"; import { acquireOwnershipSafeLock } from "./ownership-safe-lock.js"; import { privateRootStat } from "./compact-root-owner.js"; import { CompactReadError } from "./compact-read-lease.js"; /** Standalone stores have no host defaults; capture their fallback at module startup, never per session. */ export const recoveryAgentDir = getAgentDir(); /** * RULING-84: shared durable storage-health clock, independent of accepted sequence and freeze generation. * Only a successful real write/fsync or completed repair after a failure advances it. Kernel advisory locking * serializes independent owners; temp fsync, rename and directory fsync precede publication of a new revision. */ export class RecoveryStorageHealth { constructor(base) { this.failures = 0; this.cleared = 0; this.dir = join(base, `.senpi-recovery-health-${process.getuid?.() ?? "user"}`); } ensureDir() { this.setup ??= (async () => { await mkdir(this.dir, { mode: 0o700, recursive: true }); if (!privateRootStat(await lstat(this.dir))) throw new CompactReadError("ownership"); })().catch((cause) => { this.setup = undefined; throw cause; }); return this.setup; } get needsRecovery() { return this.failures > this.cleared; } unavailable() { this.failures += 1; } async revision() { await this.ensureDir(); return (await this.readRevision()) ?? this.advance(); } /** Corrupt metadata has no usable revision; only a locked atomic repair publishes its replacement. */ async readRevision() { try { const handle = await open(join(this.dir, "revision"), constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0)); try { const stat = await handle.stat(); if (!stat.isFile() || stat.size > 128) return undefined; const text = (await handle.readFile("utf8")).trim(); return /^(0|[1-9]\d*)$/.test(text) ? text : undefined; } finally { await handle.close(); } } catch (cause) { if (cause instanceof Error && "code" in cause && cause.code === "ENOENT") return "0"; if (cause instanceof Error && "code" in cause && cause.code === "ELOOP") return undefined; throw cause; } } /** The operation must actually finish successfully; a refusal leaves both failure and revision unchanged. */ async recover(operation) { await this.ensureDir(); const through = this.failures; await operation(); if (through <= this.cleared) return this.revision(); if (this.pending) await this.pending; if (through <= this.cleared) return this.revision(); this.pending = this.advance().then((revision) => { this.cleared = through; return revision; }); try { return await this.pending; } finally { this.pending = undefined; } } async advance() { await this.ensureDir(); const release = await acquireOwnershipSafeLock(join(this.dir, "revision.lock")); const temporary = join(this.dir, `${randomUUID()}.tmp`); try { const next = String(BigInt((await this.readRevision()) ?? "0") + 1n); const file = await open(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | (constants.O_NOFOLLOW ?? 0), 0o600); try { await file.writeFile(next); await file.sync(); } finally { await file.close(); } await rename(temporary, join(this.dir, "revision")); const directory = await open(this.dir, "r"); try { await directory.sync(); } finally { await directory.close(); } return next; } finally { try { await rm(temporary, { force: true }); } finally { await release(); } } } } //# sourceMappingURL=compact-storage-health.js.map