@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
120 lines • 4.17 kB
JavaScript
/**
* The server-owned record of every process a tool child is about to start (senpi#3566, plan todo 4).
*
* An entry is appended and fsynced before the server sends `launch_ack`, so a containment handle is on disk before the
* command can run. That is what lets the server clean up descendants even when the child that started them is gone.
*/
import { appendFile, mkdir, open, readFile, rm } from "node:fs/promises";
import { dirname, join } from "node:path";
/** Where a generation's ledger lives under a session's tool-child directory; the server writes it, the child reads it. */
export function generationLedgerPath(dir, generation) {
return join(dir, `g${generation}`, "descendants.ledger");
}
export class DescendantsLedger {
#path;
#durableDir = false;
#entries = new Map();
constructor(path) {
this.#path = path;
}
get path() {
return this.#path;
}
entries() {
return [...this.#entries.values()];
}
async append(entry) {
if (!this.#durableDir) {
// The first entry creates the generation directory and the file: both directory entries are synced, or a crash
// could leave an fsynced ledger that no directory names.
const directory = dirname(this.#path);
await mkdir(directory, { recursive: true, mode: 0o700 });
const created = await open(this.#path, "a", 0o600);
await created.close();
await syncDirectory(directory);
await syncDirectory(dirname(directory));
this.#durableDir = true;
}
const handle = await open(this.#path, "a", 0o600);
try {
await handle.write(`${JSON.stringify(entry)}\n`);
await handle.datasync();
}
finally {
await handle.close();
}
this.#entries.set(entry.launchId, entry);
}
/** Entries a previous server instance left behind (crash recovery). A torn final line is ignored. */
static async readPersisted(path) {
let text;
try {
text = await readFile(path, "utf8");
}
catch {
return [];
}
const entries = [];
for (const line of text.split("\n")) {
if (line.trim() === "")
continue;
try {
entries.push(JSON.parse(line));
}
catch {
// A torn last line from a crash mid-append; earlier lines were fsynced whole.
}
}
return entries;
}
async remove() {
this.#entries.clear();
await rm(this.#path, { force: true });
}
}
/** fsync a directory so the entries created in it survive a crash; a platform that cannot open a directory skips it. */
async function syncDirectory(directory) {
if (process.platform === "win32")
return;
const handle = await open(directory, "r");
try {
await handle.sync();
}
finally {
await handle.close();
}
}
/** Append-only record of every barrier frame, for diagnostics and the QA driver. Not fsynced. */
export class LaunchLog {
#path;
#chain = Promise.resolve();
#failedWrites = 0;
constructor(path) {
this.#path = path;
}
get path() {
return this.#path;
}
/** Records the log could not write; the log is diagnostic, so a failed write is counted, never thrown. */
get failedWrites() {
return this.#failedWrites;
}
settled() {
return this.#chain;
}
/** Appends in order. Never rejects: one failed write neither poisons later records nor what awaits settled(). */
record(record) {
const line = `${JSON.stringify({ atMs: Date.now(), ...record })}\n`;
this.#chain = this.#chain.then(async () => {
try {
await mkdir(dirname(this.#path), { recursive: true, mode: 0o700 });
await appendFile(this.#path, line, { mode: 0o600 });
}
catch {
this.#failedWrites += 1;
}
});
return this.#chain;
}
}
//# sourceMappingURL=descendants.js.map