import { CurrentActor } from "../contracts/executor.ts"; import { Cause, Clock, Effect, Exit, Redacted, Result, Schema } from "effect"; import { Digest } from "effect/crypto"; import type { HostCrypto } from "./host-crypto.ts"; import { Hex } from "effect/encoding"; import { UserFacingError } from "@executor-js/utils/user-facing-error"; import { HostedWorkflow, ProviderError, WorkflowFailure, WorkflowRun, WorkflowRunId, WorkflowRunFailure, WorkflowRunPage, WorkflowValue, maxFailureMessageLength, type WorkflowHostControls, } from "apps/contracts"; import { workflowFailureDetail } from "../contracts/workflow-errors.ts"; import { WorkflowHost, type WorkflowRuntime } from "../contracts/workflow-runtime.ts"; import { StartWorkflow, ListWorkflowRuns, WorkflowTarget, WorkflowApp, } from "../contracts/workflows.ts"; import { AccountScope, SelectedAccounts } from "../contracts/apps.ts"; import { AppId, VersionId, OwnerId, StorageError, type Json } from "../contracts/shared.ts"; import type { Credentials } from "../contracts/storage.ts"; import type { Runtime } from "../contracts/runtime.ts"; import type { ExecutorDatabase } from "./storage.ts"; import type { Executor } from "../contracts/executor.ts"; import type { makeOAuth } from "./oauth.ts"; import { database, query, transaction } from "./database.ts"; import { storedAccount } from "./accounts.ts"; import { storedApp, storedVersion } from "./apps.ts"; import { resolve, snapshot, type InvocationSnapshot } from "./tools.ts"; import { invocationScope, scopedSelection } from "./selection.ts"; import type { Declarations } from "./declarations.ts"; import { ownsDatabase } from "../contracts/apps.ts"; import { AppEvaluationFailed, evaluationFailure } from "../contracts/tools.ts"; import { appProviderFailure } from "./provider-error.ts"; const StoredRun = Schema.Struct({ id: WorkflowRunId, app: AppId, actor: Schema.NullOr(Schema.NonEmptyString), scope: AccountScope, owner: OwnerId, key: Schema.String, version: VersionId, name: Schema.String, accounts: SelectedAccounts, status: Schema.Literals(["queued", "running", "complete", "errored", "terminated"]), failure: Schema.NullOr(WorkflowFailure.fields.reason), encrypted: Schema.RedactedFromValue(Schema.Uint8Array), createdAt: Schema.Date, }).pipe(Schema.encodeKeys({ version: "deployment" })); const Payload = Schema.Struct({ input: WorkflowValue, request: WorkflowValue, output: Schema.optionalKey(WorkflowValue), failure: Schema.optionalKey(WorkflowRunFailure), }); const terminal = (row: typeof StoredRun.Type) => row.status === "complete" || row.status === "errored" || row.status === "terminated"; const unavailable = () => new WorkflowFailure({ reason: "unavailable", retryable: false }); const failure = (reason: WorkflowFailure["reason"], retryable = false) => new WorkflowFailure({ reason, retryable }); const repeatStart = (key: string | undefined) => key === undefined ? "This start had no key, so starting again creates another run." : "Starting again with the same key and input returns this run."; const unconfirmedStart = (run: string, key: string | undefined) => new WorkflowFailure({ reason: "engine", retryable: false, message: `Run ${run} exists for this start, but Executor could not read it back, so its state is unknown. Get the run by its ID before starting another. ${repeatStart(key)}`, }); const unknownStart = (key: string | undefined) => new WorkflowFailure({ reason: "engine", retryable: false, message: key === undefined ? "Executor could not confirm whether this start saved its run, so the run may exist. This start had no key: check ctx.workflows.list before starting again, because another start creates another run." : "Executor could not confirm whether this start saved its run, so the run may exist. Starting again with the same key and input returns the run if it was saved.", }); const stable = (value: Json): string => { if (Array.isArray(value)) return `[${value.map(stable).join(",")}]`; if (value !== null && typeof value === "object") return `{${Object.entries(value) .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)) .map(([key, item]) => `${JSON.stringify(key)}:${stable(item)}`) .join(",")}}`; return JSON.stringify(value); }; const safe = ( work: Effect.Effect, reason: WorkflowFailure["reason"], retryable = false, ): Effect.Effect => work.pipe( Effect.catchCause((cause) => { if (Cause.hasInterrupts(cause)) return Effect.interrupt; const error = Cause.squash(cause); return Effect.fail(Schema.is(WorkflowFailure)(error) ? error : failure(reason, retryable)); }), ); const dispatch = (backend: WorkflowRuntime, run: WorkflowRunId) => backend.start(run).pipe( Effect.as(true), Effect.catchIf( (error) => error.reason === "engine" && error.retryable, () => Effect.annotateCurrentSpan("executor.workflow.dispatch", "deferred").pipe( Effect.andThen( Effect.logWarning("Workflow dispatch deferred to reconciliation", { run }), ), Effect.as(false), ), ), ); export const makeWorkflowRuns = ( storage: ExecutorDatabase, runtime: Runtime, resolveAccount: ReturnType["resolveSelected"], credentials: Credentials, crypto: HostCrypto, declarations: Declarations, backend?: WorkflowRuntime, ) => { const db = database(storage); const read = (run: WorkflowRunId, app?: AppId) => Effect.gen(function* () { const row = yield* query(() => db.findFirst("workflowRuns", { where: (b) => app === undefined ? b("id", "=", run) : b.and(b("id", "=", run), b("app", "=", app)), }), ); if (row === null) return yield* failure("not_found"); return yield* Schema.decodeUnknownEffect(StoredRun)(row).pipe( Effect.mapError(() => new StorageError()), ); }); const decrypt = (row: typeof StoredRun.Type) => credentials.decrypt(row.id, row.encrypted).pipe( Effect.flatMap((value) => Schema.decodeUnknownEffect(Payload)(Redacted.value(value))), Effect.mapError(() => failure("engine")), ); const view = (row: typeof StoredRun.Type) => Effect.gen(function* () { const payload = yield* decrypt(row); return yield* Schema.decodeUnknownEffect(WorkflowRun)({ id: row.id, app: row.app, version: row.version, workflow: row.name, createdAt: row.createdAt.toISOString(), status: row.status, ...(row.status === "complete" ? { output: payload.output } : {}), ...(row.status === "errored" ? { error: row.failure, ...(payload.failure === undefined ? {} : { failure: payload.failure }), } : {}), }).pipe(Effect.mapError(() => failure("engine"))); }); const finish: WorkflowHost["finish"] = (run, result) => safe( transaction(db, () => Effect.gen(function* () { const row = yield* read(run); if (terminal(row)) return; const payload = yield* decrypt(row); const encrypted = yield* credentials.encrypt( row.id, Redacted.make({ input: payload.input, request: payload.request, ...(result.ok ? { output: result.output } : {}), ...(!result.ok && result.detail !== undefined ? { failure: result.detail } : {}), }), ); yield* query(() => db.updateMany("workflowRuns", { where: (b) => b.and( b("id", "=", run), b.or(b("status", "=", "queued"), b("status", "=", "running")), ), set: { status: result.ok ? "complete" : "errored", failure: result.ok ? null : result.error, encrypted, }, }), ); yield* query(() => db.deleteMany("workflowAccounts", { where: (b) => b("run", "=", run) }), ); }), ), "engine", true, ); const captured = (row: typeof StoredRun.Type) => snapshot(db, { app: row.app, version: row.version, captured: { accounts: row.accounts, scope: row.scope }, }).pipe(Effect.provideService(CurrentActor, row.actor ?? undefined)); const context: WorkflowHost["context"] = (run) => safe( Effect.gen(function* () { const row = yield* read(run); if (terminal(row)) return yield* failure(row.status === "terminated" ? "terminated" : "conflict"); const state = yield* captured(row); return { ...(yield* resolve(state, resolveAccount).pipe( Effect.provideService(CurrentActor, state.actor), )), hostApi: declarations.hostApi(state.version), database: ownsDatabase(state.version.requirements), workflowControls: controls(state), }; }), "credentials", ); const seed: WorkflowHost["seed"] = (run) => safe( Effect.gen(function* () { const row = yield* read(run); if (row.status === "terminated" || row.status === "errored") return yield* failure(row.status === "terminated" ? "terminated" : "conflict"); const state = yield* captured(row); const payload = yield* decrypt(row); if (row.status === "queued") yield* query(() => db.updateMany("workflowRuns", { where: (b) => b.and(b("id", "=", run), b("status", "=", "queued")), set: { status: "running" }, }), ); const hostApi = declarations.hostApi(state.version); return { runId: run, app: row.app, build: state.version.build, ...(hostApi === undefined ? {} : { hostApi }), version: row.version, name: row.name, input: payload.input, }; }), "engine", true, ); const invoke: WorkflowHost["invoke"] = (run, input) => safe( Effect.gen(function* () { const deadline = (yield* Clock.currentTimeMillis) + input.timeout; const current = yield* seed(run); const bound = yield* context(run); const result = yield* runtime .call({ app: current.app, build: current.build, ...bound, deadline, tool: input.name, kind: input.kind, input: input.input, ...(input.kind === "mutation" ? { replay: { key: input.stepId, fingerprint: Hex.encode( yield* crypto.digest.digest( Digest.sha256, new TextEncoder().encode( stable({ version: current.version, name: input.name, input: input.input, }), ), ), ), }, } : {}), }) .pipe( Effect.catchTags({ HostToolApprovalRequired: () => Effect.fail(failure("approval")), HostToolBlocked: () => Effect.fail(failure("approval")), HostInputInvalid: () => Effect.fail(failure("input")), HostToolNotFound: () => Effect.fail(failure("operation")), HostKindMismatch: () => Effect.fail(failure("operation")), HostOperationFailed: ({ errorName, message }) => Effect.fail( new WorkflowFailure({ reason: "execution", retryable: true, ...(errorName === undefined ? {} : { errorName }), ...(message.length === 0 ? {} : { message }), }), ), DatabaseLimitExceeded: (error) => Effect.fail( new WorkflowFailure({ reason: "execution", retryable: false, errorName: error._tag, message: error.message, }), ), }), ); return yield* Schema.decodeUnknownEffect(WorkflowValue)(result).pipe( Effect.mapError(() => failure("output")), ); }), "execution", true, ); const execute: WorkflowHost["execute"] = (run, driver) => Effect.gen(function* () { const existing = yield* safe(read(run), "engine", true); if (existing.status === "complete") { const result = yield* decrypt(existing); return yield* Schema.decodeUnknownEffect(WorkflowValue)(result.output).pipe( Effect.mapError(() => failure("engine")), ); } const result = yield* safe( Effect.gen(function* () { const current = yield* seed(run); yield* Effect.annotateCurrentSpan({ "executor.app.id": current.app, "executor.build.id": current.build, "executor.workflow.name": current.name, }); const bound = yield* context(run); return yield* runtime.workflow({ app: current.app, build: current.build, ...bound, command: { operation: "workflow-run", name: current.name, input: current.input }, workflow: { runId: run, driver, resolve: () => context(run), invoke: (input) => invoke(run, input), }, }); }), "execution", ).pipe(Effect.result); if (Result.isFailure(result)) { const detail = workflowFailureDetail(result.failure); if (result.failure.reason === "engine" && result.failure.retryable) { if (detail !== undefined && backend !== undefined) { const state = yield* backend.status(run).pipe(Effect.result); if ( Result.isSuccess(state) && (state.success.status === "running" || state.success.status === "waiting") ) yield* finish(run, { ok: false, error: "engine", detail }).pipe(Effect.ignore); } return yield* result.failure; } yield* finish(run, { ok: false, error: result.failure.reason, ...(detail === undefined ? {} : { detail }), }); return yield* result.failure; } yield* finish(run, { ok: true, output: result.success }); return result.success; }).pipe(Effect.withSpan("workflow.run", { attributes: { "executor.run.id": run } })); const reconcile = (row: typeof StoredRun.Type) => Effect.gen(function* () { if (terminal(row)) return yield* view(row); if (backend === undefined) return yield* unavailable(); let state = yield* backend.status(row.id); if (row.status === "queued" && state.status === "missing") { const current = yield* read(row.id); if (terminal(current)) return yield* view(current); if (current.status === "queued" && !(yield* dispatch(backend, row.id))) return yield* view(current); state = yield* backend.status(row.id); } if (state.status === "complete") { yield* finish(row.id, { ok: true, output: state.output }); return yield* view(yield* read(row.id)); } if (state.status === "errored" || (state.status === "missing" && row.status !== "queued")) { yield* finish(row.id, { ok: false, error: "engine" }); return yield* view(yield* read(row.id)); } if (state.status === "terminated") { yield* transaction(db, () => Effect.gen(function* () { yield* query(() => db.updateMany("workflowRuns", { where: (b) => b.and( b("id", "=", row.id), b.or(b("status", "=", "queued"), b("status", "=", "running")), ), set: { status: "terminated" }, }), ); yield* query(() => db.deleteMany("workflowAccounts", { where: (b) => b("run", "=", row.id) }), ); }), ); return yield* view(yield* read(row.id)); } const current = yield* view(row); return yield* Schema.decodeUnknownEffect(WorkflowRun)({ ...current, status: state.status === "missing" ? "queued" : state.status, }).pipe(Effect.mapError(() => failure("engine"))); }); const get = (input: typeof WorkflowTarget.Type) => Effect.gen(function* () { yield* storedApp(db, { app: input.app }); return yield* reconcile(yield* read(input.run, input.app)); }); const start = (input: typeof StartWorkflow.Type, inherited?: InvocationSnapshot) => Effect.gen(function* () { if (backend === undefined) return yield* unavailable(); yield* storedApp(db, { app: input.app }); const id = WorkflowRunId.make(`wfr_${yield* crypto.random.uuidV4()}`); const key = input.key ?? id; const actor = inherited === undefined ? yield* CurrentActor : inherited.actor; const scope = inherited === undefined ? invocationScope(input.accounts) : inherited.scope; const find = () => query(() => db.findFirst("workflowRuns", { where: (b) => b.and( b("app", "=", input.app), b("key", "=", key), actor === undefined ? b("actor", "is", null) : b("actor", "=", actor), b("scope", "=", scope), ), }), ); let row = yield* find(); if (row === null) { const state = inherited ?? (yield* snapshot(db, input)); const bound = yield* resolve(state, resolveAccount).pipe( Effect.provideService(CurrentActor, actor), ); const parsed = yield* safe( runtime .workflow({ app: input.app, build: state.version.build, hostApi: declarations.hostApi(state.version), ...bound, command: { operation: "workflow-validate", name: input.workflow, input: input.input }, }) .pipe( Effect.catchTags({ RuntimeBuildUnavailable: () => Effect.fail(failure("engine", true)), RuntimeProtocolFailed: () => Effect.fail(failure("engine", true)), }), ), "input", ); const encrypted = yield* credentials.encrypt( id, Redacted.make({ input: parsed, request: input.input }), ); const createdAt = new Date(yield* Clock.currentTimeMillis); let committing = false; const settled = yield* transaction(db, () => Effect.gen(function* () { yield* query(() => db.updateMany("apps", { where: (b) => b("id", "=", state.app.id), set: { createdAt: state.app.createdAt }, }), ); const currentApp = yield* storedApp(db, { app: state.app.id }); if (inherited === undefined && currentApp.activeVersion !== state.app.activeVersion) return yield* failure("conflict"); const accountIds = [...new Set(Object.values(state.accounts).flat())].sort(); for (const account of accountIds) { const saved = yield* storedAccount(db, account); yield* query(() => db.updateMany("accounts", { where: (b) => b("id", "=", account), set: { createdAt: saved.createdAt }, }), ); yield* storedAccount(db, account); } yield* query(() => db.create("workflowRuns", { id, app: input.app, owner: state.app.owner, actor: actor ?? null, scope: state.scope, key, deployment: state.version.id, name: input.workflow, accounts: state.accounts, status: "queued", failure: null, encrypted, createdAt, }), ); const accounts = new Set(Object.values(state.accounts).flat()); for (const account of accounts) yield* query(() => db.create("workflowAccounts", { id: `${id}:${account}`, run: id, account }), ); committing = true; }), ).pipe(Effect.exit); if ( Exit.isFailure(settled) && (Cause.hasInterrupts(settled.cause) || (!committing && !Cause.hasFails(settled.cause))) ) return yield* Effect.failCause(settled.cause); if (Exit.isSuccess(settled)) yield* Effect.annotateCurrentSpan("executor.run.id", id); const found = yield* find().pipe( Effect.result, Effect.withSpan("workflow.start.read-back"), ); if (Exit.isSuccess(settled)) { if (Result.isFailure(found) || found.success === null) return yield* unconfirmedStart(id, input.key); row = found.success; } else if (committing) { if (Result.isFailure(found) || found.success === null) return yield* unknownStart(input.key); row = found.success; } else { const error = Cause.findError(settled.cause); if (Result.isFailure(error)) return yield* Effect.failCause(settled.cause); if (Result.isFailure(found)) return yield* Schema.is(WorkflowFailure)(error.success) ? error.success : unknownStart(input.key); if (found.success === null) return yield* error.success; row = found.success; } } const saved = row; return yield* Effect.gen(function* () { const retained = yield* Schema.decodeUnknownEffect(StoredRun)(saved).pipe( Effect.mapError(() => new StorageError()), ); const payload = yield* decrypt(retained); if (retained.name !== input.workflow || stable(payload.request) !== stable(input.input)) return yield* failure("conflict"); yield* Effect.annotateCurrentSpan("executor.run.id", retained.id); if (!terminal(retained)) yield* dispatch(backend, retained.id); return yield* view(yield* read(retained.id)); }).pipe( Effect.catchIf( (error) => !(Schema.is(WorkflowFailure)(error) && error.reason === "conflict"), () => Effect.fail(unconfirmedStart(String(saved.id), input.key)), ), ); }).pipe( Effect.tapError((error) => Effect.annotateCurrentSpan( "executor.workflow.failure.reason", Schema.is(WorkflowFailure)(error) ? error.reason : error._tag, ), ), Effect.withSpan("workflow.start", { attributes: { "executor.app.id": input.app } }), ); const terminate = (input: typeof WorkflowTarget.Type) => Effect.gen(function* () { yield* storedApp(db, { app: input.app }); const row = yield* read(input.run, input.app); if (row.status === "complete" || row.status === "errored") return yield* view(row); if (backend === undefined) return yield* unavailable(); yield* backend.terminate(row.id); yield* transaction(db, () => Effect.gen(function* () { yield* query(() => db.updateMany("workflowRuns", { where: (b) => b.and( b("id", "=", row.id), b.or(b("status", "=", "queued"), b("status", "=", "running")), ), set: { status: "terminated" }, }), ); yield* query(() => db.deleteMany("workflowAccounts", { where: (b) => b("run", "=", row.id) }), ); }), ); return yield* view(yield* read(row.id)); }); const list = (input: typeof ListWorkflowRuns.Type, actor?: string | null) => Effect.gen(function* () { yield* storedApp(db, { app: input.app }); const limit = input.limit ?? 20; const rows = yield* query(() => db.findMany("workflowRuns", { where: (b) => b.and( b("app", "=", input.app), ...(actor === undefined ? [] : [actor === null ? b("actor", "is", null) : b("actor", "=", actor)]), ...(input.workflow === undefined ? [] : [b("name", "=", input.workflow)]), ...(input.key === undefined ? [] : [b("key", "=", input.key)]), ...(input.cursor === undefined ? [] : [b("id", ">", input.cursor)]), ), orderBy: ["id", "asc"], limit: limit + 1, }), ); const page = yield* Schema.decodeUnknownEffect(Schema.Array(StoredRun))( rows.slice(0, limit), ).pipe(Effect.mapError(() => new StorageError())); const items = yield* Effect.forEach(page, reconcile, { concurrency: 4, }); const last = page.at(-1); return WorkflowRunPage.make({ items, ...(rows.length > limit && last !== undefined ? { next: last.id } : {}), }); }).pipe(Effect.withSpan("sdk.workflows.list")); function controls(inherited: InvocationSnapshot): WorkflowHostControls { const app = inherited.app.id; const actor = inherited.actor ?? null; const parse = ( schema: Schema.Decoder, input: unknown, run: (input: A) => Effect.Effect, ) => Schema.decodeUnknownEffect(schema)(input).pipe( Effect.flatMap(run), Effect.catchCause((cause) => { if (Cause.hasInterrupts(cause)) return Effect.interrupt; const error = Cause.squash(cause); if (Schema.is(WorkflowFailure)(error)) return Effect.fail(error); return Effect.fail( UserFacingError.is(error) ? new WorkflowFailure({ reason: "execution", retryable: error.retryable, errorName: error._tag.slice(0, 128), message: error.message.slice(0, maxFailureMessageLength), }) : failure("execution"), ); }), ); const within = (input: typeof WorkflowTarget.Type) => Effect.gen(function* () { const row = yield* read(input.run, input.app); if (row.actor !== actor) return yield* failure("not_found"); }); return { start: (input) => parse( StartWorkflow, { ...input, app, accounts: scopedSelection( inherited.version.requirements.accounts, inherited.accounts, inherited.scope, ), }, (input) => start(input, inherited), ), get: (input) => parse(WorkflowTarget, { ...input, app }, (input) => within(input).pipe(Effect.andThen(get(input))), ), list: (input) => parse(Schema.toType(ListWorkflowRuns), { ...input, app }, (input) => list(input, actor)), terminate: (input) => parse(WorkflowTarget, { ...input, app }, (input) => within(input).pipe(Effect.andThen(terminate(input))), ), }; } return { controls, definitions: (input: typeof WorkflowApp.Type) => Effect.gen(function* () { const state = yield* snapshot(db, input); const identity = { app: state.app.id, version: state.version.id }; const value = yield* declarations.read("workflows", state, (context) => runtime .workflow({ app: input.app, build: state.version.build, hostApi: declarations.hostApi(state.version), ...context, command: { operation: "workflows" }, }) .pipe( Effect.mapError((error) => Schema.is(ProviderError)(error) ? appProviderFailure(state, error) : evaluationFailure(identity, error, "Workflow evaluation failed"), ), ), ); return yield* Schema.decodeUnknownEffect(Schema.Array(HostedWorkflow))(value).pipe( Effect.mapError( () => new AppEvaluationFailed({ ...identity, reason: "Invalid workflow catalog" }), ), ); }).pipe(Effect.withSpan("sdk.workflows.definitions")), runs: { start, get, terminate, list, pinned: (input: Parameters[0]) => Effect.gen(function* () { const app = yield* storedApp(db, { app: input.app, owner: input.owner }); const row = yield* read(input.run, input.app); if (input.owner !== undefined && row.owner !== input.owner) return yield* failure("not_found"); const version = yield* storedVersion(db, app, row.version).pipe( Effect.catchTags({ VersionNotFound: () => failure("not_found"), AppNotDeployed: () => failure("not_found"), }), ); return { accounts: scopedSelection(version.requirements.accounts, row.accounts, row.scope), actor: row.actor, }; }), }, host: { get: (run) => safe(read(run).pipe(Effect.flatMap(view)), "engine", true), seed, context, invoke, execute, finish, reconcile: safe( Effect.gen(function* () { if (backend === undefined) return; let cursor: WorkflowRunId | undefined; while (true) { const after = cursor; const pending = yield* query(() => db.findMany("workflowRuns", { where: (b) => b.and( b.or(b("status", "=", "queued"), b("status", "=", "running")), ...(after === undefined ? [] : [b("id", ">", after)]), ), orderBy: ["id", "asc"], limit: 100, }), ); yield* Effect.forEach( pending, (row) => safe( Effect.gen(function* () { if (row.status === "queued") yield* backend.start(row.id); yield* get({ app: row.app, run: row.id }); }), "engine", true, ).pipe( Effect.catch((error) => Effect.logWarning("Workflow reconciliation failed", { run: row.id, reason: error.reason, }), ), ), { concurrency: 4, discard: true }, ); cursor = pending.at(-1)?.id; if (pending.length < 100 || cursor === undefined) break; } }), "engine", true, ), } satisfies WorkflowHost, }; };