Skip to content

Signals

Pause a workflow run until a tool or a member answers one of its signals.

Updated

A signal lets a run wait for something outside it, such as a person’s approval. The workflow declares its signals, the body waits for one at a time, and a tool or a member answers.

Signal waits

A workflow declares the signals it may wait for under signals, each with the value type of its payload. A signal’s name is letters, digits, and _, starts with a letter or _, and has at most 64 characters, which publishing checks. step.waitForSignal(signal, { timeout }) waits for an answer to one of them:

import { boolean, object, workflow, WorkflowSignalTimeoutError } from "@buildr/sdk";

const approveChangeOrder = workflow(
  {
    description: "Escalates a change order nobody decides on",
    input: object({}),
    signals: { decision: object({ approved: boolean() }) },
  },
  async ({ step }) => {
    try {
      const { payload, sentBy } = await step.waitForSignal<{ approved: boolean }>("decision", {
        timeout: "3 days",
      });
      return { approved: payload.approved, decidedBy: sentBy };
    } catch (error) {
      if (!(error instanceof WorkflowSignalTimeoutError)) throw error;
      await step.do("escalate", async (s) => {
        // Escalate through s.forge
      });
      return null;
    }
  },
);
  • Answer: The wait resolves to a WorkflowSignal: payload, which Forge checked against the signal’s declared type, and sentBy, the id of the caller (member or extension) who answered.
  • Timeout: timeout is milliseconds or a unit string from 1 second to 365 days. "1 year" is refused, because Forge counts a year as 365.25 days. Without a timeout, the wait lasts 365 days. When the time runs out, step.waitForSignal rejects with a WorkflowSignalTimeoutError whose signal property names the signal. The body can catch it and carry on; uncaught, it fails the run.
  • One wait at a time: While a run waits, its status is waiting, its waiting_for_signal names the signal, and its signal_sequence numbers the wait, counting the run’s waits from 1. Starting a second wait before the first ends rejects with a TypeError, as does waiting for a signal the workflow doesn’t declare or waiting inside step.do’s work.

Workflow signal answers

Any tool of the extension answers a run’s signal wait with ctx.workflows.signal:

const { runId, signal, sequence } = await ctx.workflows.signal({
  run: runId,
  signal: "decision",
  payload: { approved: true },
});
  • Which runs a tool answers: A tool answers the runs that its extension started on the branch it reads. An installation’s tools answer that installation’s runs on main; a run of another extension or branch fails with ExtensionWorkflowRunNotFound.
  • Checked at the run’s version: The payload is checked against the signal as the run’s pinned version declares it, which may differ from the version the answering tool runs. A signal that version doesn’t declare fails with ExtensionWorkflowSignalNotFound, a payload that doesn’t fit fails with ExtensionWorkflowSignalPayloadDoesNotFit or ExtensionWorkflowSignalPayloadDoesNotFitAt, and an answer over 1 MiB of JSON fails with ExtensionWorkflowSignalPayloadTooLarge.
  • One answer per wait: Without sequence, the answer is for the wait the run is on; with it, only for that wait, the signal_sequence a read of the run shows. An answer to a wait that was already answered, a stale sequence, or a run that isn’t waiting for the signal fails with ExtensionWorkflowSignalNotAwaited (409).
  • Who answered: The run’s body reads sentBy as the id of whoever the tool acts as: its caller or, for a schedule’s, trigger’s, webhook’s, or mailbox’s run, the extension itself.

Signal answers from a member

A member with signal on the workflow answers a waiting run through the API or the CLI, without a tool, so a person can approve or deny what a workflow waits for:

import { defineExtension, literal, object, string, tool, workflow } from "@buildr/sdk";

const approveChangeOrder = workflow(
  {
    description: "Waits for a member to approve or deny a change order",
    input: object({ changeOrderId: string() }),
    signals: { decision: object({ decision: literal("approve", "deny") }) },
  },
  async ({ step }, { changeOrderId }) => {
    const { payload, sentBy } = await step.waitForSignal<{ decision: "approve" | "deny" }>(
      "decision",
      { timeout: "3 days" },
    );
    return { changeOrderId, decision: payload.decision, decidedBy: sentBy };
  },
);

const routeChangeOrder = tool(
  {
    description: "Routes a change order for approval",
    input: object({ changeOrderId: string() }),
  },
  (ctx, input) => ctx.workflows.start("approveChangeOrder", input, { key: input.changeOrderId }),
);

export default defineExtension(
  {},
  { tools: { routeChangeOrder }, workflows: { approveChangeOrder } },
);

Once the run reads waiting, a member approves it:

forge extensions create-extension-installation-workflow-run-signal --repo "$OPS_REPO" \
  --name project-controls.change-orders --run "$RUN_ID" --signal decision \
  --data '{"payload":{"decision":"approve"}}'
  • Request: POST /repos/:repo_id/extension-installations/:name/workflow-runs/:run_id/signals/:signal with the body { payload, sequence }, where sequence is optional. For a run that a tool started on a branch of the defining repo, use POST /repos/:repo_id/branches/:branch/extensions/:name/workflow-runs/:run_id/signals/:signal or forge extensions create-branch-extension-workflow-run-signal --branch. The response is the run, running again.
  • Same checks as a tool’s answer: Forge checks the run, the signal, the payload, and the wait exactly as it checks ctx.workflows.signal, and refuses with the same errors and statuses. A run of another installation, branch, or repo fails with ExtensionWorkflowRunNotFound.
  • Who may answer: Answering takes signal on the workflow (Actions), and the body reads the member as sentBy. A workflow doesn’t name who may answer a specific signal, so any member whom policies allow to signal the workflow can answer it.

Type to search…

↑↓ navigate↵ selectEsc close