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
@@ -3,6 +3,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, expect, test, vi } from "vitest";
import { validateWorkspaceDescriptor } from "../src/workspaces/schema.js";
import { PreprocessingStateStore, type WriterLockLease } from "../src/workspaces/preprocessing-state.js";
import {
WorkspacePreprocessingService,
type ChildProcessRequest,
@@ -10,6 +11,64 @@ import {
const roots: string[] = [];
test("a legacy running lease is never silently adopted by repeated recovery attempts", async () => {
const catalog = repository({ getByWorkspace: vi.fn(async () => ({ ...database, preprocessingInputFingerprint: "sha256:legacy" })) });
const instance = service({ repository: catalog });
for (let attempt = 0; attempt < 2; attempt++) {
expect(await instance.run({ workspaceId: "catalog-workspace" })).toMatchObject({ status: "failed", code: "preprocessing_conflict" });
}
expect(catalog.beginPreprocessing).not.toHaveBeenCalled();
expect(catalog.finishPreprocessing).not.toHaveBeenCalled();
expect(catalog.clearPreprocessing).not.toHaveBeenCalled();
});
test("retry after loss of Catalog completion restarts guarded preprocessing without clearing data", async () => {
let stranded = false;
let runId: string | undefined;
const catalog = repository({ getByWorkspace: vi.fn(async () => ({ ...database,
...(stranded ? { preprocessingInputFingerprint: "sha256:" + "f".repeat(64), preprocessingRunId: runId } : {}) })),
beginPreprocessing: vi.fn(async (_workspace, _fingerprint, id) => {
runId = id; return { kind: "started", database };
}),
});
catalog.finishPreprocessing.mockRejectedValueOnce(Error("Catalog connection lost"))
.mockRejectedValueOnce(Error("Catalog still unavailable"));
const instance = service({ repository: catalog, runChild: async () => {
stranded = true;
return { exitCode: 0, stdout: JSON.stringify({ counts: { tables: 1 } }), stderr: "" };
} });
await expect(instance.run({ workspaceId: "catalog-workspace" })).rejects.toThrow();
const second = await instance.run({ workspaceId: "catalog-workspace" });
expect(second.status).toBe("succeeded");
expect(catalog.finishPreprocessing).toHaveBeenCalledWith("catalog-workspace", 9, "sha256:" + "f".repeat(64),
{ status: "failed", errorCode: "preprocessing_interrupted" }, expect.any(String));
expect(catalog.clearPreprocessing).not.toHaveBeenCalled();
});
test("loss of the real owner during semantic preflight cannot complete or rewrite the job", async () => {
let lease: WriterLockLease | undefined;
const original = PreprocessingStateStore.prototype.acquireWriterLock;
const spy = vi.spyOn(PreprocessingStateStore.prototype, "acquireWriterLock")
.mockImplementation(async function (kind) {
const acquired = await original.call(this, kind);
if (kind !== "workers") lease = acquired;
return acquired;
});
const catalog = repository();
const instance = service({ repository: catalog, semanticPreflight: async () => {
process.kill(lease!.holderPid, "SIGKILL");
for (let i = 0; i < 100 && lease!.isHeld(); i++) await new Promise(resolve => setTimeout(resolve, 10));
expect(lease!.isHeld()).toBe(false);
return { ok: false, code: "semantic_index_incompatible" };
} });
try {
await expect(instance.run({ workspaceId: "catalog-workspace" })).rejects.toMatchObject({ code: "preprocessing_conflict" });
expect(catalog.finishPreprocessing).not.toHaveBeenCalled();
const latest = new PreprocessingStateStore({ dataRoot: roots.at(-1)!, workspaceId: "catalog-workspace" }).readLatestJob();
expect(latest?.status).toBe("active");
} finally { spy.mockRestore(); }
});
test("Evidence consolidation runs only its stage and preserves blocked Catalog readiness", async () => {
const catalog = repository();
const runChild = vi.fn(async () => ({ exitCode: 0, stdout: JSON.stringify({ status: "succeeded", counts: { documents: 2 } }), stderr: "" }));
@@ -219,6 +278,7 @@ test("complete preprocessing snapshots Catalog metadata and commits its PostgreS
9,
"sha256:" + "f".repeat(64),
{ status: "succeeded" },
expect.any(String),
);
});
@@ -257,6 +317,7 @@ test("preprocessing records a failed PostgreSQL state when its hidden worker fai
9,
"sha256:" + "f".repeat(64),
{ status: "failed", errorCode: "schema_index_failed" },
expect.any(String),
);
});
@@ -273,6 +334,7 @@ test("semantic preflight failure is persisted before returning", async () => {
9,
"sha256:" + "f".repeat(64),
{ status: "failed", errorCode: "semantic_index_incompatible" },
expect.any(String),
);
});
@@ -310,6 +372,7 @@ test("clear invalidates Catalog readiness before clearing only derived worker da
expect(runChild).toHaveBeenCalledWith({
argv: ["preprocess", "clear", "--json", "-c", "/dev/fd/3"],
configPath: expect.any(String),
guard: { lockPath: expect.any(String), tokenPath: expect.any(String), token: expect.stringMatching(/^[a-f0-9]{64}$/) },
});
expect(result).toMatchObject({
status: "succeeded",