fix: surface preprocessing state error codes to the operator result

This commit is contained in:
2026-08-11 20:01:16 +02:00
parent de479b8b38
commit ec5ed413f7
+18 -3
View File
@@ -20,11 +20,15 @@ export interface WorkspaceMaintenanceIo {
type Command = "inspect" | "preprocess-dwh" | "schema-suggest-fks" | "schema-check" | "index-schema" | "preprocess-evidence" | "preprocess-run"; type Command = "inspect" | "preprocess-dwh" | "schema-suggest-fks" | "schema-check" | "index-schema" | "preprocess-evidence" | "preprocess-run";
function failureResult(operation: string, workspaceId = ""): WorkspaceOperationResult { function failureResult(
operation: string,
workspaceId = "",
code: WorkspaceOperationResult["code"] = "workspace_not_activatable",
): WorkspaceOperationResult {
return { return {
schemaVersion: 1, schemaVersion: 1,
status: "failed", status: "failed",
code: "workspace_not_activatable", code,
workspaceId, workspaceId,
workspaceRevision: "", workspaceRevision: "",
descriptorBlob: "", descriptorBlob: "",
@@ -33,6 +37,11 @@ function failureResult(operation: string, workspaceId = ""): WorkspaceOperationR
}; };
} }
const STATE_ERROR_CODES: Record<string, WorkspaceOperationResult["code"]> = {
preprocessing_resume_mismatch: "preprocessing_resume_mismatch",
preprocessing_conflict: "preprocessing_conflict",
};
function boundedJson(result: WorkspaceOperationResult): string { function boundedJson(result: WorkspaceOperationResult): string {
const encoded = JSON.stringify(result); const encoded = JSON.stringify(result);
if (Buffer.byteLength(encoded, "utf8") > 1024 * 1024) { if (Buffer.byteLength(encoded, "utf8") > 1024 * 1024) {
@@ -131,9 +140,15 @@ export async function runWorkspaceMaintenanceCli(
io.writeStdout(boundedJson(result)); io.writeStdout(boundedJson(result));
return exitCodeFor(result); return exitCodeFor(result);
} catch (error) { } catch (error) {
const failureCode = error instanceof Error
&& "code" in error
&& typeof (error as { code?: unknown }).code === "string"
&& (error as { code: string }).code in STATE_ERROR_CODES
? STATE_ERROR_CODES[(error as { code: string }).code]
: "workspace_not_activatable";
const result = failureResult(command, (() => { const result = failureResult(command, (() => {
try { return JSON.parse(io.stdin).workspaceId ?? ""; } catch { return ""; } try { return JSON.parse(io.stdin).workspaceId ?? ""; } catch { return ""; }
})()); })(), failureCode);
if (process.env.P2_CHILD_DEBUG === "1") { if (process.env.P2_CHILD_DEBUG === "1") {
const detail = String((error as Error).message ?? "").replace(/[\r\n]+/g, " ").slice(0, 2048); const detail = String((error as Error).message ?? "").replace(/[\r\n]+/g, " ").slice(0, 2048);
result.warnings = detail.length > 0 ? [detail] : undefined; result.warnings = detail.length > 0 ? [detail] : undefined;