Skip to content
Documentation
On this page

Workflows, webhooks and events

Workflows, webhooks and events from the built-in skills in Executor 2.0.0-beta.79.

This page covers Workflows from the built-in skills shipped with Executor 2.0.0-beta.79. It contains app-authoring/workflows.md, app-authoring/webhooks.md.

Next: Workflows, webhooks and events · Events

Workflows

Register workflow({ input, output?, description? }, async (ctx, input) => result) in defineApp(requirements, { ..., workflows: { name: declaration } }). Import WorkflowContext<typeof requirements> for handlers in separate files, or WorkflowContext<typeof requirements, "app"> for a workflow app-scope work starts, such as an app schedule’s mutation; its steps have no user. The body has runId and step; it has no ctx.sql, accounts or elicit.

Use step.do("name", async (ctx) => value) for external work. Its context has fresh accounts (ctx.app.accounts, and ctx.user.accounts in a run started by a caller), fetch, signal and a stable idempotencyKey. Retry options can precede the callback: { retries: { limit: 3, delay: "2 seconds", backoff: "exponential" }, timeout: "30 seconds" }. Throw NonRetryableError for permanent failure. Return bounded JSON, or null when no result is needed.

Use step.runQuery("name", registeredQuery, input) and step.runMutation("name", registeredMutation, input) for app storage. Register those declarations in the normal query/mutation catalogs too. A mutation run as a step writes only inside one ctx.sql.transaction(...), which also records the step’s receipt, so a retried step never repeats its writes. A write outside that transaction, or a second transaction, fails the step. External API writes still need idempotency.

Use step.sleep("name", "1 minute") or step.sleepUntil("name", timestamp) for waiting. Put time reads, randomness and I/O inside steps; use their results for branches and loops. Promise.all works for independent steps. Runs pin deployed code and account IDs, but each executing step resolves current credentials.

App mutations/webhooks can call ctx.workflows.start({ workflow, input, key? }) and terminate({ run }). Queries also have get({ run }) and list(options?). Controls cannot target another app. Use stable start keys when retrying a caller; starting a workflow is never part of a SQL transaction.

Limits: workflow and step names are 1–200 characters, start input is JSON of at most 1 MiB, a key is 1–128 characters, and list takes limit 1–100 (default 20). A start with the same key and input returns that run; the same key with other input fails with reason conflict. Controls throw WorkflowFailure with reason, retryable and a message. Reason input is refused before any run starts; fix the call. A start that fails with reason engine and retryable: false may still have started its run: start again with the same key and input, or check list, before starting a run without a key. The SDK namespace is executor.apps.workflowRuns, with discovery through executor.apps.workflows.list. Do not invent executor.workflowRuns.

V1 has no webhook/event wait, durable human-input request, or restart helper. Background operations retain approval rules and fail if they require live input.

Scheduled mutations

Declare schedules against the same mutation objects registered on the app. A schedule has one of two scopes:

  • appInterval and appCron: one schedule for the app. Someone who can edit the app enables it, and it runs without a caller, with the app’s accounts only (ctx.app.accounts). Its mutation takes a context of the "app" scope, MutationContext<typeof requirements, "app">, which has no user, or declares no parameters, and declares no approval: nobody is there to ask. A context declared without a type is the caller’s, so the types reject it, and an approval also fails the app’s evaluation. mutation<Input, Output>(…) with explicit type arguments has the caller’s context too, even with no parameters; pass the "app" context as the third type argument.
  • userInterval and userCron: each member enables their own copy, at most one per declaration, which runs as them with the accounts they linked to the app (ctx.user.accounts).
import {
  appInterval,
  defineApp,
  mutation,
  type MutationContext,
  object,
  router,
  string,
  userCron,
} from "apps";

const requirements = {};

// An app schedule's mutation takes a context of the "app" scope: it runs without a caller.
const record = mutation(
  { input: object({ message: string() }) },
  async (ctx: MutationContext<typeof requirements, "app">, { message }) =>
    ctx.sql.exec("INSERT INTO notes (message) VALUES (?) RETURNING message", message).one(),
);

export default defineApp(requirements, {
  tools: router({ record }),
  schedules: {
    heartbeat: appInterval({ minutes: 5 }, record, { message: "Heartbeat" }),
    morning: userCron({ expression: "0 9 * * MON-FRI", timezone: "America/Los_Angeles" }, record, {
      message: "Morning",
    }),
  },
});

Intervals accept one positive integer unit: seconds, minutes or hours, and must resolve to at least 60 seconds. A shorter interval fails when the app is evaluated. Use Run now to try a schedule without waiting for its next tick. Calendar schedules accept five-field cron expressions and default to UTC. The mutation must appear once in the app’s tools router, outside dynamic routers. Its input is checked during app evaluation. External handlers use MutationContext<typeof requirements> for a user schedule, exactly as ordinary mutations do, and MutationContext<typeof requirements, "app"> for an app schedule. No factory that supplies accounts and no storage-specific mutation constructor is needed. A workflow such a mutation starts declares WorkflowContext<typeof requirements, "app">.

Schedules start paused. Use the app’s Schedules tab or the Executor management app’s schedule definitions/configure operations to enable them. The management API also lists saved schedules and runs, pauses, removes and runs one now. Only people who can edit an app enable, pause, run or remove its app schedules. Each run uses the current version and, at that moment, the member’s linked accounts for a user schedule or the accounts linked for everyone for an app schedule. A schedule whose plain requirement has no account skips its occurrences and shows “Account required” until one is linked. Only one run is active per schedule; overdue ticks coalesce into one run after downtime.

For a user schedule, the default automatic approval mode accepts approval prompts using the saved schedule authorization. Explicit denials still block execution. Select browser to review requests on the Approvals page. These requests expire after 15 minutes and occupy the active slot while waiting. Browser review requires the normal signed-in user; an agent cannot answer through the management app. Background input requests (elicit) are unsupported. No automatic retries, workflow checkpoints or replay of uncertain side effects are provided.

Webhook subscriptions

Apps may return a webhooks catalog. Define the account requirement whose accounts are its source, config and state schemas, and async register, handle, and unregister callbacks. Keep these callbacks together. The host supplies a stable subscriptionId, callbackUrl, signing secret, and the source account; the other accounts are available in context.

account names the requirement with its scope. account: { user: "github" } registers the webhook for each account a member links, which runs as that member: its context has ctx.user.accounts and ctx.app.accounts. account: { app: "datadog" } registers it for the account linked to the app for everyone, without a caller: its context has only ctx.app.accounts. Linking the account registers it and unlinking removes it. Use WebhookContext<typeof requirements> or WebhookContext<typeof requirements, "app"> for external lifecycle handlers. A webhook written inline in defineApp cannot infer its scope from account, so state it to type the handlers:

const empty = object({});
const alerts = {
  account: { app: "datadog" },
  config: empty,
  state: empty,
  register: async (ctx) => ({}),
  unregister: async () => {},
  handle: async (ctx, { request }) => new Response(null, { status: 204 }),
} satisfies Webhook<WebhookContext<typeof requirements, "app">, typeof empty, typeof empty>;

An account has one webhook source per app. The same account may be linked for everyone and by members; it then serves only as the app’s account, for the app webhooks of the requirements it is linked to. When its last app link is removed, a member who links it becomes the source.

Webhook contexts expose writable ctx.sql when the app declares sql, and exclude elicit in both the type and runtime object. No handler runs inside a transaction, so register can wait on a provider that verifies the callback URL by calling handle before it answers. Registration and cleanup must be idempotent. A callback must verify the provider signature over raw bytes before parsing the body or performing side effects.

handle, register and unregister each have 45 seconds. Providers often resend a delivery much sooner, some after 5 seconds, so answer fast: verify, store the event or start a workflow with ctx.workflows.start (workflows.md), then reply.

Discover definitions/config schemas through the Executor app’s webhook operations. Create with a stable key, handler name, config, and an explicit sourceAccount for a collection requirement. Check the returned status and failure. Retry failed or interrupted registration/cleanup with reconcile. Remove a subscription before deleting its app or connected accounts. Subscriptions pin their original version and account IDs; recreate to upgrade. Providers retry deliveries; Executor does not deduplicate or replay them. state can be null during provider validation or cleanup after a lost registration response. Local providers need a publicly reachable callback origin.

For providers without a registration API, replace register and unregister with setup: { instructions: "...", signingSecret: "executor" }. Keep handle and both schemas. state describes the private setup fields; use object({}) if only a signing secret is needed. executor means the operator copies a generated secret into the provider; provider means they paste the provider’s secret into Executor’s secure page. A version that keeps signingSecret and the fields state accepts keeps the subscription, callback URL and secret, also when an earlier version is activated again; reordering fields is no change. Changing either needs a new setup at the provider.

An app webhook’s subscription comes from linking an account to the app, so webhooks.create refuses it with reason: "input" and a message saying so; find it with webhooks.list.

When creation returns setup-required, request webhookLinks.link({ path: { app, subscription } }) (local) or the hosted setupLink operation and show the URL to the user. Do not ask for signing secrets in chat or include them in tool arguments. Read status with webhooks.get. Manual removal returns disabled; after removing it in the provider, use webhooks.confirmRemoval. This confirms the operator’s action; Executor cannot verify external deletion without a provider API.

Was this page helpful?