@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
101 lines • 4.27 kB
JavaScript
import { randomUUID } from "node:crypto";
import { lstat, mkdir, rename, rm, rmdir, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { describeCause } from "./compact-storage-io.js";
import { currentRootOwner, readRootMarker, ROOT_MARKER } from "./compact-root-owner.js";
/**
* Ownership of the compact spool's epoch directory (senpi#4272). The store creates the directory
* itself (0700, never adopting an existing one) and writes a private marker into it; removal
* re-checks the directory and that marker without following symlinks, so retirement never deletes
* a directory this store did not create and still own.
*/
const MARKER = ROOT_MARKER;
class CompactSpoolRefusedError extends Error {
constructor(reason, path, cause, cleanupFailure) {
const detail = cause === undefined ? "" : `: ${describeCause(cause)}`;
const kept = cleanupFailure === undefined ? "" : `; removing the directory failed: ${describeCause(cleanupFailure)}`;
super(`compact spool directory refused (${reason}): ${path}${detail}${kept}`, { cause });
this.name = "CompactSpoolRefusedError";
this.reason = reason;
this.path = path;
}
}
/** Creates a fresh private epoch directory and returns the token its ownership marker holds. */
export async function createSpoolDir(dir) {
await mkdir(dirname(dir), { recursive: true, mode: 0o700 });
try {
await mkdir(dir, { mode: 0o700 });
}
catch (cause) {
if (cause.code === "EEXIST")
throw new CompactSpoolRefusedError("pre_existing", dir);
throw cause;
}
const token = randomUUID();
const temporary = join(dir, `${MARKER}.${token}.tmp`);
try {
await writeFile(temporary, JSON.stringify({ kind: "scratch", token, owner: await currentRootOwner() }), {
flag: "wx",
mode: 0o600,
});
await rename(temporary, join(dir, MARKER));
}
catch (cause) {
throw await abandonSpoolDir(dir, cause, temporary);
}
return token;
}
/**
* Removes a directory `createSpoolDir` just made but could not mark, so a later store creates it
* fresh instead of refusing it as pre-existing. Only a marker this call's exclusive create made (any
* failure but EEXIST) is unlinked, then the directory itself, which `rmdir` removes only while empty.
*/
async function abandonSpoolDir(dir, cause, temporary) {
try {
await rm(temporary, { force: true });
if (cause.code !== "EEXIST")
await rm(join(dir, MARKER), { force: true });
await rmdir(dir);
return new CompactSpoolRefusedError("marker_write_failed", dir, cause);
}
catch (cleanupFailure) {
return new CompactSpoolRefusedError("marker_write_failed", dir, cause, cleanupFailure);
}
}
/**
* Removes `dir` only while it is still the private, marked directory `createSpoolDir` made. A
* directory that is already gone leaves nothing to remove, so retirement settles as a no-op.
*/
export async function removeSpoolDir(dir, token) {
const owned = await lstat(dir).catch((cause) => {
if (cause.code === "ENOENT")
return undefined;
throw cause;
});
if (owned === undefined)
return;
const refusal = await refusalFor(dir, owned, token);
if (refusal)
throw new CompactSpoolRefusedError(refusal, dir);
await rm(dir, { recursive: true, force: true });
}
async function refusalFor(dir, owned, token) {
if (owned.isSymbolicLink())
return "symlink";
if (!owned.isDirectory())
return "not_directory";
const markerPath = join(dir, MARKER);
const marker = await lstat(markerPath).catch(() => undefined);
if (!marker?.isFile() || (await readRootMarker(dir))?.token !== token)
return "marker_missing";
// Windows has no POSIX owner or mode bits; the marker token and the no-follow checks still apply.
const uid = process.getuid?.();
if (uid === undefined)
return undefined;
if (owned.uid !== uid || marker.uid !== uid)
return "foreign_owner";
if (((owned.mode | marker.mode) & 0o077) !== 0)
return "wide_mode";
return undefined;
}
//# sourceMappingURL=compact-spool-dir.js.map