/** Shared user-facing diagnostics for execution failures and recorded-workflow recovery. */ import type { WorkflowSource } from "../engine/types.ts" import { workflowSourceLabel } from "./workflow-source.ts" export interface WorkflowCrashIdentity { readonly workflowName: string readonly runId: string readonly path?: string } export interface WorkflowCrashDetails extends WorkflowCrashIdentity { readonly cause: string } export function workflowCrashHeading(details: WorkflowCrashIdentity): string { return `workflow "${details.workflowName}" crashed${details.path ? ` at "${details.path}"` : ""} (run ${details.runId})` } export function workflowCrashRecovery(runId: string): readonly [string, string] { return [`Resume: /workflow resume ${runId}`, `Details: /workflow status ${runId}`] } export function workflowCrashMessage(details: WorkflowCrashDetails): string { return [ workflowCrashHeading(details), ` ${details.cause}`, ...workflowCrashRecovery(details.runId).map((line) => ` ${line}`), ].join("\n") } export function workflowFailureLine(details: Pick): string { return `Failure${details.path ? ` at "${details.path}"` : ""}: ${details.cause}` } export function recordedWorkflowLoadFailure(options: { readonly workflowName: string readonly runId: string readonly workflowSource: WorkflowSource readonly action: "resume" | "show status" readonly cause?: string }): string { const sourceKind = options.workflowSource.kind === "file" ? "File" : "Built-in" return [ `workflow "${options.workflowName}" cannot ${options.action} (run ${options.runId})`, ` ${sourceKind}: ${workflowSourceLabel(options.workflowSource)}`, ` ${options.cause ?? defaultLoadFailure(options.workflowSource)}`, " The recorded run has been preserved.", ].join("\n") } function defaultLoadFailure(source: WorkflowSource): string { return source.kind === "file" ? "The workflow file no longer exists." : "The package-owned workflow is unavailable." } export function missingWorkflowProvenance(runId: string, action: "resumed" | "shown"): string { return [ `workflow run ${runId} cannot be ${action}`, " Its history does not record the workflow source it came from.", " The recorded run has been preserved.", ].join("\n") }