feat: recover interrupted installation preparation safely

This commit is contained in:
Codex
2026-10-06 19:02:01 +02:00
parent 5c05778614
commit af6cf88b5a
32 changed files with 497 additions and 37 deletions
+16 -3
View File
@@ -34,6 +34,7 @@ export interface SessionInventoryRow {
export interface WriterLockLease {
holderPid: number;
isHeld(): boolean;
release(): Promise<void>;
}
@@ -66,6 +67,7 @@ export interface PreprocessingJobState {
status: "active" | "succeeded" | "blocked" | "failed";
candidateDigest?: string;
reviewDigest?: string;
ownerProtocol?: 1;
}
export interface FkReviewRecord {
@@ -195,6 +197,14 @@ export class PreprocessingStateStore {
}
writerLockPath(): string { return join(this.root, "writer.lock"); }
workerLockPath(): string { return join(this.root, "workers.lock"); }
ownerTokenPath(): string { return join(this.root, "owner-token"); }
rotateOwnerToken(): string {
this.ensureLayout();
const token = randomBytes(32).toString("hex");
writeAtomicFile(this.ownerTokenPath(), token, 0o600);
return token;
}
runtimeConfigDirectory(): string { return join(this.root, "runtime-config"); }
runtimeConfigManifestDirectory(): string { return join(this.root, "runtime-config-manifests"); }
jobsDirectory(): string { return join(this.root, "jobs"); }
@@ -216,9 +226,9 @@ export class PreprocessingStateStore {
]) ensureDirectory(directory);
}
async acquireWriterLock(): Promise<WriterLockLease> {
async acquireWriterLock(kind: "writer" | "workers" = "writer"): Promise<WriterLockLease> {
this.ensureLayout();
const lockPath = this.writerLockPath();
const lockPath = kind === "writer" ? this.writerLockPath() : this.workerLockPath();
try {
const entry = lstatSync(lockPath);
if (!entry.isFile() || entry.isSymbolicLink()) throw new Error("invalid writer lock path");
@@ -257,11 +267,14 @@ export class PreprocessingStateStore {
let released = false;
return {
holderPid: holder.pid ?? 0,
isHeld: () => !released && holder.exitCode === null && holder.signalCode === null,
release: async () => {
if (released) return;
released = true;
if (holder.exitCode !== null || holder.signalCode !== null) return;
const exited = new Promise<void>((resolve) => holder.once("exit", () => resolve()));
if (!holder.stdin.destroyed) holder.stdin.end();
await new Promise<void>((resolve) => holder.once("exit", () => resolve()));
await exited;
},
};
}