@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
113 lines • 4.29 kB
JavaScript
import { execFile } from "node:child_process";
import { constants } from "node:fs";
import { lstat, open, readFile } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { z } from "zod";
import { readProcessIdentity, processIsLive, processStartTimeMs, } from "../../utils/process-identity.js";
export const ROOT_MARKER = ".compact-spool-owner";
const Owner = z.object({
bootNonce: z.string().min(1),
pid: z.number().int().positive(),
startTime: z
.string()
.min(1)
.refine((value) => processStartTimeMs(value) !== undefined),
});
const Marker = z.object({ kind: z.enum(["scratch", "live"]), token: z.string().min(1), owner: Owner.nullable() });
let boot;
let self;
/** A boot-session identity, never an age estimate. Unsupported or unobservable means UNKNOWN. */
function bootNonce() {
boot ??= (async () => {
switch (process.platform) {
case "darwin":
return ((await promisify(execFile)("/usr/sbin/sysctl", ["-n", "kern.bootsessionuuid"], { timeout: 2_000 })).stdout.trim() || null);
case "linux":
return (await readFile("/proc/sys/kernel/random/boot_id", "utf8")).trim() || null;
default:
return null;
}
})().catch((cause) => {
if (cause instanceof Error)
return null;
throw cause;
});
return boot;
}
export function currentRootOwner() {
self ??= (async () => {
const bootNonceValue = await bootNonce();
if (!bootNonceValue)
return null;
const start = await rootProcessIdentity(process.pid);
return start.kind === "present"
? { bootNonce: bootNonceValue, pid: process.pid, startTime: start.identity }
: null;
})();
return self;
}
/** RULING-101b: an unstarted probe is UNKNOWN, never ownership or death evidence. */
async function rootProcessIdentity(pid) {
try {
return await readProcessIdentity(pid, process.platform, 2_000, processIsLive, "UTC");
}
catch (cause) {
if (cause instanceof Error &&
"syscall" in cause &&
typeof cause.syscall === "string" &&
/^spawn(?: |$)/.test(cause.syscall))
return { kind: "error", error: cause };
throw cause;
}
}
/** Only these three proofs permit reclaim. A probe failure never becomes evidence of death. */
export async function rootOwnerState(owner) {
const currentBoot = await bootNonce();
if (!owner || !currentBoot)
return "unknown";
if (owner.bootNonce !== currentBoot)
return "dead";
const start = await rootProcessIdentity(owner.pid);
switch (start.kind) {
case "absent":
return processIsLive(owner.pid) ? "unknown" : "dead";
case "present":
return start.identity === owner.startTime ? "alive" : "dead";
case "error":
return "unknown";
}
}
export function privateRootStat(stat) {
return (stat.isDirectory() &&
!stat.isSymbolicLink() &&
(process.getuid === undefined || (stat.uid === process.getuid() && (stat.mode & 0o077) === 0)));
}
/** The file is opened no-follow, bounded, then parsed once; an old or unreadable marker is UNKNOWN. */
export async function readRootMarker(dir) {
try {
const path = join(dir, ROOT_MARKER);
const entry = await lstat(path);
if (!entry.isFile() || entry.isSymbolicLink())
return undefined;
const handle = await open(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
try {
const stat = await handle.stat();
if (!stat.isFile() ||
stat.size > 8_192 ||
(process.getuid !== undefined && (stat.uid !== process.getuid() || (stat.mode & 0o077) !== 0)))
return undefined;
const parsed = Marker.safeParse(JSON.parse(await handle.readFile("utf8")));
return parsed.success ? parsed.data : undefined;
}
finally {
await handle.close();
}
}
catch (cause) {
if (cause instanceof Error)
return undefined;
throw cause;
}
}
//# sourceMappingURL=compact-root-owner.js.map