feat: recover interrupted installation preparation safely
This commit is contained in:
@@ -16,6 +16,7 @@ import {
|
||||
import type { DeterministicRuntimeConfigLease } from "./runtime-config-lease.js";
|
||||
import { buildCatalogMetadataSnapshot } from "../catalog/metadata-snapshot.js";
|
||||
import type { CatalogRepository } from "../catalog/types.js";
|
||||
import { acquirePreprocessingOwner, type PreprocessingChildGuard } from "./preprocessing-owner.js";
|
||||
|
||||
export interface WorkspaceOperationResult {
|
||||
schemaVersion: 1;
|
||||
@@ -47,6 +48,7 @@ export interface WorkspaceOperationResult {
|
||||
export interface ChildProcessRequest {
|
||||
argv: string[];
|
||||
configPath: string;
|
||||
guard?: PreprocessingChildGuard;
|
||||
}
|
||||
|
||||
interface ActiveRuntime {
|
||||
@@ -79,6 +81,7 @@ export interface WorkspacePreprocessingServiceDeps {
|
||||
}
|
||||
|
||||
interface RunScope {
|
||||
owner: Awaited<ReturnType<typeof acquirePreprocessingOwner>>;
|
||||
runtime: ActiveRuntime;
|
||||
state: PreprocessingStateStore;
|
||||
job: PreprocessingJobState;
|
||||
@@ -205,13 +208,39 @@ export class WorkspacePreprocessingService {
|
||||
const runtime = await this.deps.acquireActiveRuntime(options.workspaceId);
|
||||
return baseResult(runtime, "preprocess run", "failed", "catalog_not_ready");
|
||||
}
|
||||
return await this.runFromCatalog(options);
|
||||
const owner = await acquirePreprocessingOwner(this.deps.dataRoot, options.workspaceId);
|
||||
try {
|
||||
const previous = await this.deps.catalogRepository.getByWorkspace(options.workspaceId);
|
||||
// Only runs created under this guard protocol can be reclaimed automatically.
|
||||
const lastJob = this.state(options.workspaceId).readLatestJob();
|
||||
if (previous?.preprocessingStatus === "running" && previous.preprocessingInputFingerprint
|
||||
&& (lastJob?.ownerProtocol !== 1 || !previous.preprocessingRunId || lastJob.runId !== previous.preprocessingRunId)) {
|
||||
const runtime = await this.deps.acquireActiveRuntime(options.workspaceId);
|
||||
return baseResult(runtime, "preprocess run", "failed", "preprocessing_conflict",
|
||||
{ warnings: ["An older preprocessing owner cannot be recovered automatically."] });
|
||||
}
|
||||
if (previous?.preprocessingStatus === "running" && previous.preprocessingInputFingerprint && lastJob?.ownerProtocol === 1) {
|
||||
owner.assertOwned();
|
||||
await this.deps.catalogRepository.finishPreprocessing(options.workspaceId,
|
||||
previous.metadataContentRevision, previous.preprocessingInputFingerprint,
|
||||
{ status: "failed", errorCode: "preprocessing_interrupted" }, previous.preprocessingRunId);
|
||||
}
|
||||
owner.assertOwned();
|
||||
return await this.runFromCatalog(options, owner);
|
||||
} finally { await owner.release(); }
|
||||
}
|
||||
|
||||
async clear(options: { workspaceId: string }): Promise<WorkspaceOperationResult> {
|
||||
const owner = await acquirePreprocessingOwner(this.deps.dataRoot, options.workspaceId);
|
||||
try { return await this.clearOwned(options, owner); }
|
||||
finally { await owner.release(); }
|
||||
}
|
||||
|
||||
private async clearOwned(options: { workspaceId: string }, owner: RunScope["owner"]): Promise<WorkspaceOperationResult> {
|
||||
const runtime = await this.deps.acquireActiveRuntime(options.workspaceId);
|
||||
const repository = this.deps.catalogRepository;
|
||||
if (!repository) return baseResult(runtime, "preprocess clear", "failed", "catalog_not_ready");
|
||||
owner.assertOwned();
|
||||
const cleared = await repository.clearPreprocessing(runtime.workspaceId);
|
||||
if (cleared.kind !== "cleared") {
|
||||
return baseResult(
|
||||
@@ -224,7 +253,9 @@ export class WorkspacePreprocessingService {
|
||||
const result = await this.deps.runChild({
|
||||
argv: ["preprocess", "clear", "--json", "-c", "/dev/fd/3"],
|
||||
configPath: runtime.configLease.path,
|
||||
guard: owner.guard,
|
||||
});
|
||||
owner.assertOwned();
|
||||
if (result.exitCode !== 0) {
|
||||
return baseResult(runtime, "preprocess clear", "failed", "preprocessing_clear_failed");
|
||||
}
|
||||
@@ -234,11 +265,13 @@ export class WorkspacePreprocessingService {
|
||||
});
|
||||
}
|
||||
|
||||
private async runFromCatalog(options: { workspaceId: string }): Promise<WorkspaceOperationResult> {
|
||||
const scope = await this.startRun(options.workspaceId);
|
||||
private async runFromCatalog(options: { workspaceId: string }, owner: RunScope["owner"]): Promise<WorkspaceOperationResult> {
|
||||
const scope = await this.startRun(options.workspaceId, owner);
|
||||
const repository = this.deps.catalogRepository!;
|
||||
const fingerprint = scope.runtime.configLease.inputFingerprint;
|
||||
const started = await repository.beginPreprocessing(scope.runtime.workspaceId, fingerprint);
|
||||
owner.assertOwned();
|
||||
const started = await repository.beginPreprocessing(scope.runtime.workspaceId, fingerprint, scope.job.runId);
|
||||
owner.assertOwned();
|
||||
if (started.kind !== "started") {
|
||||
scope.job.status = "failed";
|
||||
scope.state.writeJob(scope.job);
|
||||
@@ -260,19 +293,23 @@ export class WorkspacePreprocessingService {
|
||||
scope.runtime.workspaceId,
|
||||
revision,
|
||||
);
|
||||
owner.assertOwned();
|
||||
const snapshotPath = this.publishCatalogSnapshot(
|
||||
scope.runtime.workspaceId,
|
||||
JSON.stringify(snapshot),
|
||||
);
|
||||
this.completeStages(scope, "catalog_snapshot");
|
||||
const semantic = await this.deps.semanticPreflight(scope.runtime.workspace);
|
||||
owner.assertOwned();
|
||||
if (!semantic.ok) {
|
||||
await repository.finishPreprocessing(
|
||||
scope.runtime.workspaceId,
|
||||
revision,
|
||||
fingerprint,
|
||||
{ status: "failed", errorCode: semantic.code },
|
||||
scope.job.runId,
|
||||
);
|
||||
owner.assertOwned();
|
||||
scope.job.status = "failed";
|
||||
scope.state.writeJob(scope.job);
|
||||
finished = true;
|
||||
@@ -280,7 +317,7 @@ export class WorkspacePreprocessingService {
|
||||
}
|
||||
|
||||
failureCode = "schema_index_failed";
|
||||
const payload = await this.runJsonStage(scope.runtime, [
|
||||
const payload = await this.runJsonStage(scope, [
|
||||
"preprocess",
|
||||
"catalog",
|
||||
"--catalog-metadata",
|
||||
@@ -301,12 +338,15 @@ export class WorkspacePreprocessingService {
|
||||
this.evidenceDependencies(scope),
|
||||
);
|
||||
if (!["succeeded", "unchanged"].includes(outcome.status)) {
|
||||
owner.assertOwned();
|
||||
await repository.finishPreprocessing(
|
||||
scope.runtime.workspaceId,
|
||||
revision,
|
||||
fingerprint,
|
||||
{ status: "failed", errorCode: outcome.code },
|
||||
scope.job.runId,
|
||||
);
|
||||
owner.assertOwned();
|
||||
scope.job.status = "failed";
|
||||
scope.state.writeJob(scope.job);
|
||||
finished = true;
|
||||
@@ -319,29 +359,35 @@ export class WorkspacePreprocessingService {
|
||||
revision,
|
||||
fingerprint,
|
||||
{ status: "succeeded" },
|
||||
scope.job.runId,
|
||||
);
|
||||
owner.assertOwned();
|
||||
if (!completed) throw new Error("catalog preprocessing completion lost its lease");
|
||||
scope.job.status = "succeeded";
|
||||
scope.state.writeJob(scope.job);
|
||||
finished = true;
|
||||
return this.evidenceResult(scope, outcome);
|
||||
} catch (error) {
|
||||
owner.assertOwned();
|
||||
if (!finished) {
|
||||
await repository.finishPreprocessing(
|
||||
scope.runtime.workspaceId,
|
||||
revision,
|
||||
fingerprint,
|
||||
{ status: "failed", errorCode: failureCode },
|
||||
scope.job.runId,
|
||||
);
|
||||
}
|
||||
owner.assertOwned();
|
||||
scope.job.status = "failed";
|
||||
scope.state.writeJob(scope.job);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private async startRun(workspaceId: string): Promise<RunScope> {
|
||||
private async startRun(workspaceId: string, owner: RunScope["owner"]): Promise<RunScope> {
|
||||
const runtime = await this.deps.acquireActiveRuntime(workspaceId);
|
||||
owner.assertOwned();
|
||||
const state = this.state(runtime.workspaceId);
|
||||
const job = await state.beginJob({
|
||||
operation: "preprocess run",
|
||||
@@ -353,7 +399,10 @@ export class WorkspacePreprocessingService {
|
||||
embeddingId: runtime.configLease.effectiveConfig.embedding.id,
|
||||
embeddingDimensions: runtime.configLease.effectiveConfig.embedding.dimensions,
|
||||
});
|
||||
return { runtime, state, job };
|
||||
owner.assertOwned();
|
||||
job.ownerProtocol = 1;
|
||||
state.writeJob(job);
|
||||
return { runtime, state, job, owner };
|
||||
}
|
||||
|
||||
private state(workspaceId: string): PreprocessingStateStore {
|
||||
@@ -361,6 +410,7 @@ export class WorkspacePreprocessingService {
|
||||
}
|
||||
|
||||
private completeStages(scope: RunScope, ...stages: string[]): void {
|
||||
scope.owner.assertOwned();
|
||||
for (const stage of stages) {
|
||||
if (!scope.job.completedStages.includes(stage)) scope.job.completedStages.push(stage);
|
||||
}
|
||||
@@ -384,8 +434,8 @@ export class WorkspacePreprocessingService {
|
||||
|
||||
private evidenceDependencies(scope: RunScope): EvidencePreprocessingDependencies {
|
||||
return {
|
||||
runStage: async (argv) => await this.runJsonStage(scope.runtime, argv),
|
||||
persistJob: () => this.state(scope.runtime.workspaceId).writeJob(scope.job),
|
||||
runStage: async (argv) => await this.runJsonStage(scope, argv),
|
||||
persistJob: () => { scope.owner.assertOwned(); this.state(scope.runtime.workspaceId).writeJob(scope.job); },
|
||||
evidencePreflight: async () => await this.deps.evidencePreflight(scope.runtime.workspace),
|
||||
requireRunId: (value) => this.requireRunId(value),
|
||||
numberRecord: (value) => this.numberRecord(value),
|
||||
@@ -400,8 +450,10 @@ export class WorkspacePreprocessingService {
|
||||
return baseResult(scope.runtime, "preprocess run", status, code, extra);
|
||||
}
|
||||
|
||||
private async runJsonStage(runtime: ActiveRuntime, argv: string[]): Promise<Record<string, unknown>> {
|
||||
const result = await this.deps.runChild({ argv, configPath: runtime.configLease.path });
|
||||
private async runJsonStage(scope: RunScope, argv: string[]): Promise<Record<string, unknown>> {
|
||||
scope.owner.assertOwned();
|
||||
const result = await this.deps.runChild({ argv, configPath: scope.runtime.configLease.path, guard: scope.owner.guard });
|
||||
scope.owner.assertOwned();
|
||||
if (result.exitCode !== 0) throw new Error("workspace child failed");
|
||||
return JSON.parse(result.stdout) as Record<string, unknown>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user