UNPKG

@mastra/core

Version:
1,340 lines • 93.6 kB
const require_validate = require("./validate-BDarBL_5.cjs");
const require_schedules = require("./schedules-CyoPADNx.cjs");
const require_run_registry = require("./run-registry-BrKoxzT0.cjs");
let zod = require("zod");
// src/workflows/builder/authoring-playbook.ts
const WORKFLOW_BUILDER_AUTHORING_CONSTRAINTS = `# Persisted workflow authoring contract

A persisted workflow is a JSON-safe static graph. The supported entry types are agent, tool, classifier, mapping, nested workflow, parallel, foreach, sleep, sleepUntil, declarative conditional, and declarative loop. Closure mappings, function predicates, callbacks, and arbitrary executable functions are unsupported.

Every adjacent step must compose exactly: the previous output shape must satisfy the next input schema. Agent inputs are always { prompt: string }. Insert a mapping step whenever shapes differ; never rely on implicit coercion. A mapping's output keys are the top-level keys of its JSON-encoded mapConfig. Persisted mappings only select, rename, template, or provide constant values; they cannot evaluate arithmetic or arbitrary expressions. Template placeholders must use inputData, initData, state, requestContext, or stepResults namespaces (for example \${stepResults.add-numbers.result}), never input, steps, or JavaScript expressions. Use a discovered tool or agent when computation is required.

Mapping entries must be top-level linear steps. Parallel and conditional children, foreach bodies, and loop bodies may be agent, tool, classifier, or nested workflow entries; do not place mappings or nested containers inside them. Parallel and conditional children all receive the same preceding output. Foreach requires an array input and passes each array item directly to its body. Loop bodies must accept both the preceding output and their own output on later iterations. Use a nested workflow when a branch or foreach item needs its own input-shaping mapping. Conditional predicates align by index with their branch steps. Loop and conditional predicates must use the declarative predicate DSL.

Nested workflow entries use \`workflowId\` to identify the discovered dependency and \`id\` as the local call-site identity. They may differ; downstream mappings reference the declared call-site \`id\`. Use dependency IDs returned by discovery. Never invent agent, tool, classifier, or workflow IDs. Keep workflow IDs, step IDs, schemas, mapping configs, options, predicates, and metadata JSON-safe.`;
const WORKFLOW_BUILDER_AUTHORING_PLAYBOOK = `${WORKFLOW_BUILDER_AUTHORING_CONSTRAINTS}

# How a workflow runs

A workflow takes one **input value** matching \`inputSchema\` and runs an ordered list of **steps**. Most steps exchange objects, but schemas may also describe arrays or scalars where the step contract permits them. Each step receives the previous step's output as its input and produces its own output. The workflow's final output is the last step's output, which must match \`outputSchema\`.

There are eleven step types. The COLUMNS in the table below are the contract you must respect.

| Step type     | Input it receives | Output it produces |
|---------------|-------------------|--------------------|
| \`tool\`        | Previous step's output, validated against the tool's \`inputSchema\`. | The exact shape of the tool's \`outputSchema\`. |
| \`classifier\`  | The complete previous output. Insert a \`mapping\` step first when the classifier needs a reshaped input. | \`{ answers, usage }\`. Route with \`inputData.answers.<question>.choice\`, \`inputData.answers.<question>.score\`, or \`inputData.answers.<question>.probability\`. The complete answer object includes distributions when provided. A refused question is \`{ type: "refusal" }\` with no choice, score, or probability; route it with \`inputData.answers.<question>.type\`. |
| \`agent\`       | STRICTLY \`{ prompt: string }\`. The engine does NOT coerce; it validates and throws "expected object, received …" if the previous step's output isn't exactly this shape. If your previous step doesn't already produce \`{ prompt: string }\`, you MUST insert a \`mapping\` step in between. | Default: \`{ text: string }\`. If the entry sets \`outputSchema\` (see "Structured agent output" below), the output IS that schema's shape. |
| \`workflow\`    | Previous step's output, validated against the referenced workflow's \`inputSchema\`. The nested workflow is identified by \`workflowId\` (id of another workflow registered on the Mastra instance — either code-defined via \`createWorkflow\` or stored through the authoring surface). | The referenced workflow's \`outputSchema\`. |
| \`mapping\`     | Nothing directly — mappings *project* from any prior step's results, the workflow input, etc. (See "Mappings" below.) | An object whose top-level keys are the keys of \`mapConfig\`. |
| \`parallel\`    | Previous step's output, forwarded to EVERY child step. Children must be single-step-like (\`agent\` / \`tool\` / \`classifier\` / \`workflow\`) — no mappings or nested containers. | An object keyed by each child step's \`id\`, whose value is that child's output. |
| \`foreach\`     | An **array**. The previous step MUST output an array. The inner step runs once per element (with concurrency you choose). | An array of the inner step's outputs, one per input element, order-preserving. |
| \`sleep\`       | Passes the previous step's output through unchanged after waiting \`duration\` ms. | Same as its input. Use to space out steps deterministically. |
| \`sleepUntil\`  | Passes the previous step's output through unchanged after waiting until an ISO date. | Same as its input. Use for "run at a specific wall-clock time". |
| \`conditional\` | Previous step's output, forwarded to EVERY branch step. Each branch fires only if its declarative \`predicate\` evaluates truthy. | An object keyed by each branch step's \`id\`, whose value is that branch's output (or \`undefined\` for branches whose predicate was false). |
| \`loop\`        | Previous step's output on iteration 1; the inner step's own previous output on subsequent iterations. \`dowhile\` re-runs while the predicate is TRUE; \`dountil\` re-runs until the predicate is TRUE. | The inner step's LAST-iteration output. |

# Discovery — your four catalog tools

Every authoring surface gives you the same four discovery tools. All four take **no arguments** and return the **entire** catalog for their kind, so you call each one once, up front, and you are done discovering:

- \`list-available-agents\` → every agent you may put in \`{ type: "agent", agentId }\`. Each row carries the id to copy verbatim, a description to choose by, and the agent's output contract. Agent input is ALWAYS \`{ prompt: string }\`; the output contract describes the DEFAULT output (\`{ text: string }\`), which a step-level \`outputSchema\` overrides for that step only.
- \`list-available-tools\` → every tool you may put in \`{ type: "tool", toolId }\`. Each row carries the id to copy verbatim, a description, and \`inputSchema\` / \`outputSchema\` as JSON Schema. READ THE SCHEMAS — they are your ground truth for every field name you interpolate. If a row has no \`outputSchema\`, that tool's output shape is unknown to you: you may only consume it through a mapping that builds the next input from scratch.
- \`list-available-classifiers\` → every configured classifier you may put in \`{ type: "classifier", classifierId }\`. Each row carries the id, configured question metadata, and a routing path for each question. Copy question keys and choice literals exactly. A classifier returns \`answers\` and \`usage\`; use a following \`conditional\` entry to route on \`inputData.answers.<question>.choice\`, \`.score\`, or \`.probability\`. A refused question has only \`type: "refusal"\`; check \`inputData.answers.<question>.type\` to route refusals. There is no classifier-specific switch entry.
- \`list-available-workflows\` → every already-registered workflow you may put in \`{ type: "workflow", workflowId }\`, with its id, description, and both schemas. NEVER reference a workflow id that is not in this list. The one exception is a helper workflow you author in this same request, and only exactly as your surface policy allows.

Do not skip a listing because you "already know" what exists, and do not compose from a name the user said out loud. A registry key that is not in these results does not exist.

These four plus the completion tool named in your surface's execution protocol are ALL the tools you have. There is no other way to learn a schema and no lookup that returns one resource at a time — if something is not in these four catalogs, stop and say so rather than probing for it.

# Composition procedure

Follow this sequence for every authoring request:

1. **Discover authoritative resources.** Call \`list-available-agents\`, \`list-available-tools\`, \`list-available-classifiers\`, and \`list-available-workflows\` before composing anything. Treat the returned ids, schemas, and classifier question metadata as ground truth. Never infer availability or fields from a name in the user's request.
2. **Pick the smallest useful graph.** Decide the ordered steps needed to satisfy the request. Do not add speculative helpers or alternative graphs.
3. **Classify every reference.** Agent, tool, classifier, and workflow IDs come from different registries. An agent entry needs an \`agentId\`, a tool entry needs a \`toolId\`, a classifier entry needs a \`classifierId\`, and a nested-workflow entry needs a \`workflowId\`. Copy the discovered registry key verbatim into the matching discriminant; a local step \`id\` is not a resource ID.
4. **Wire every boundary.** For each step, compare the input shape it requires with the exact output shape it will receive from the workflow input or previous step. Insert a top-level mapping when object shapes differ. For containers, recursively verify every child against the common input or array item it receives.
5. **Construct one complete definition.** Include a kebab-case workflow ID, concise non-empty description, schemas, and full graph. Do not emit incremental fragments or speculative alternatives.
6. **Run the shared pre-action check.** Before using the surface-specific completion tool, verify that every resource reference came from authoritative discovery, every adjacent schema boundary composes, every mapping path exists on its declared source, container children receive the correct common input or array item, and the final step output satisfies the workflow output schema.

The authoring surface's execution protocol owns what happens next. Stop composition here and follow that concrete policy exactly; do not substitute another surface's completion, authority, or persistence semantics.

# Shared summary rules

- Only summarize after the surface-specific execution protocol reports its authoritative success condition. Never turn a candidate, rejected definition, tool call, or natural-language intention into a success claim.
- Base the summary on the authoritative definition returned or accepted by the surface, not on the graph you intended to create. Do not invent, omit, or rename steps, resources, schemas, lifecycle state, or persistence state.
- Keep the final summary concise. State the workflow ID, what it does, its expected input and output, and the important resources or control-flow constructs it uses.
- End with the concrete next action defined by the surface policy. The shared response protocol does not decide whether that action is running a persisted workflow or reviewing and explicitly saving a Ready draft.

# The composition rule — schemas MUST match

This is the single most important rule in this document. Every step declares an \`inputSchema\` (what it consumes) and an \`outputSchema\` (what it produces). Two adjacent steps compose ONLY IF the previous step's output shape structurally satisfies the next step's input shape. When they don't match, the engine throws a validation error at runtime and the workflow fails.

**When shapes don't line up, the fix is ALWAYS to insert a \`mapping\` step between them.** There is no other mechanism. Do not hope the engine will "figure it out" — it will not.

For every adjacent pair of steps you plan, run this check:

- If the NEXT step is an **agent** → its required input is HARD-CODED to \`{ prompt: string }\`. Nothing else. If the previous step doesn't produce that exact shape, insert a mapping whose \`mapConfig\` has a single key \`prompt\`.
- If the NEXT step is a **tool** → its required input is the tool's \`inputSchema\` from authoritative resource discovery. If the previous step's output doesn't match every required field, insert a mapping producing exactly that shape.
- If the NEXT step is a **workflow** → its required input is the referenced workflow's \`inputSchema\` from authoritative resource discovery. If the previous step's output doesn't match, insert a mapping. The nested workflow runs to completion and its final output becomes the next step's input.
- If the NEXT step is a **mapping** → no check. Mappings can pull from any prior step by id.
- If the NEXT step is a **foreach** → the previous step's output MUST be a raw array \`Array<T>\`, where \`T\` structurally matches the foreach's INNER step's input. Recurse the check: inner is agent → \`T\` must be \`{ prompt: string }\`; inner is tool → \`T\` must be that tool's \`inputSchema\`; inner is workflow → \`T\` must be the referenced workflow's \`inputSchema\`.
- If the NEXT step is a **parallel** → its children each receive the previous step's output. Each child runs the check independently for its own input shape.
- If the NEXT step is **sleep** or **sleepUntil** → pass-through; the check applies to the step AFTER it.
- If the NEXT step is a **conditional** → each branch step receives the previous step's output; recurse the check independently per branch step. The predicates themselves only read paths — they do not consume input.
- If the NEXT step is a **loop** → the inner step receives the previous step's output on iteration 1 and its own previous output thereafter, so the inner step's \`inputSchema\` MUST also be satisfied by its own \`outputSchema\` (input/output shapes must match, or the second iteration will fail validation).

## Schema shapes you MUST have memorised

- **Tool step.** Input and output are exactly what authoritative resource discovery reports. No wrapping. No coercion. If the tool's \`outputSchema\` is a string, the next step receives a string. Period.
- **Agent step.** Input is ALWAYS \`{ prompt: string }\` — this is fixed by the engine, not something you can change on the entry. Output is \`{ text: string }\` unless the entry declares \`outputSchema\`, in which case the output IS that declared shape.
- **Mapping step.** Output is an object whose top-level keys are the keys of \`mapConfig\`. Input is unconstrained (mappings source from anywhere by id).

## The single most common miswire

Tool that returns a string → agent step. The tool emits \`"…text…"\`; the agent expects \`{ prompt: string }\`. The engine throws \`Step input validation failed: Invalid input: expected object, received string\`. The fix is a mapping between them:

\`\`\`json
[
  { "type": "tool", "id": "list", "toolId": "<discovered-listing-tool-id>" },
  {
    "type": "mapping",
    "id": "to-prompt",
    "mapConfig": "{\\"prompt\\":{\\"template\\":\\"Extract every .ts path from the listing below.\\\\n\\\\n\${stepResults.list}\\"}}"
  },
  { "type": "agent", "id": "extract", "agentId": "<discovered-agent-id>" }
]
\`\`\`

Read the tool's actual \`outputSchema\` first. If it's a primitive (\`z.string()\`, \`z.number()\`, \`z.boolean()\`), reference the whole result: \`\${stepResults.<id>}\`. If it's an object or array, you can either pluck a specific field (\`\${stepResults.<id>.<field>}\`) OR reference the whole thing bare (\`\${stepResults.<id>}\`) and let the template JSON-encode it for the agent. Never guess field names.

# Mappings — how to reshape data between steps

A mapping step's \`mapConfig\` is a **JSON-encoded string** of an object (yes, encoded — \`mapConfig\` is a string, not an object). Each top-level key becomes a field of the mapping's output. Each value is one of these source forms:

- \`{ "template": "<text with \${placeholders}>" }\` — interpolates a string. Placeholders can read from these namespaces:
  - \`\${inputData.<field>}\` — a field of the CURRENT step's live input, which equals the PREVIOUS step's output. For step 1 only, this happens to equal the workflow input (because step 1's input IS the workflow input). From step 2 onward, \`inputData\` is the previous step's output — if you want the workflow's original input past step 1, use \`\${initData.<field>}\` instead.
  - \`\${initData.<field>}\` — a field of the WORKFLOW's original input, available from ANY step. Use this whenever a mid-workflow step needs an argument from the top-level workflow input (e.g. a step-3 mapping referencing \`\${initData.path}\`).
  - \`\${stepResults.<stepId>.<field>}\` — a field of an earlier step's output when the output is an object. Dotted paths drill into nested fields.
  - \`\${stepResults.<stepId>}\` — the whole step result. Primitives render as \`String(v)\`; objects and arrays are JSON-encoded (via \`JSON.stringify\`) so downstream agents get the full structure inline. Use this bare form when you want an agent to see the entire upstream shape (e.g. feeding \`foreach(agent)\`'s \`{ text }[]\` output into a synthesis step).
  - \`\${state.<field>}\`, \`\${requestContext.<field>}\` — advanced, rarely needed.
  Templates render primitives as strings and JSON-encode objects/arrays. \`null\`/\`undefined\` render as \`""\`. Pluck a field only when you specifically want just that field; bare references are fine (and preferred) when the agent should see the whole structure.
- \`{ "value": <constant> }\` — embed a literal JSON value.
- \`{ "initData": true, "path": "<field.path>" }\` — pluck a field from the workflow's original input. This is the canonical direct source form; do not emit \`{ "initData": "<field>" }\`, \`{ "initData": true }\` without \`path\`, or combine it with \`step\`.
- \`{ "step": "<stepId>", "path": "<field.path>" }\` — pluck a single field from a prior step's output. Dotted paths drill into nested objects. This source must not also include \`initData\`.
- \`{ "step": ["<stepIdA>", "<stepIdB>", ...], "path": "<field.path>" }\` — the ARRAY form of the same source. It resolves the listed steps in order and uses the FIRST one that actually produced a result, then applies \`path\` to it. This is how you read a value out of a set of steps when only some of them ran — most importantly, collapsing mutually exclusive \`conditional\` branches. See the conditional section.
- \`{ "requestContextPath": "<field.path>" }\` — read a field from the run's request context (per-run ambient values such as a caller id or tenant, supplied at run time rather than in the workflow input). Note the shape: the path is the VALUE of \`requestContextPath\`, with no separate \`path\` key. Use it only for values the caller genuinely passes as request context; if the value belongs in the workflow's own input, use \`initData\` instead. Declare \`requestContextSchema\` on the workflow so the field can be validated.

Canonical direct-source examples:

\`\`\`json
{
  "mapConfig": "{\\"name\\":{\\"initData\\":true,\\"path\\":\\"name\\"},\\"customerId\\":{\\"step\\":\\"lookup-customer\\",\\"path\\":\\"customerId\\"},\\"status\\":{\\"value\\":\\"open\\"},\\"summary\\":{\\"template\\":\\"Ticket for \${initData.name}: \${stepResults.lookup-customer.customerId}\\"}}"
}
\`\`\`

Every direct path mapping references **exactly one** source: either \`initData: true\` plus \`path\`, or \`step\` plus \`path\`. Constants use only \`value\`; interpolated strings use only \`template\`.

# Structured agent output — how to make an agent step return more than \`{ text }\`

By default, every agent step's output is \`{ text: string }\`. That's fine when the agent's job is to write prose. It is NOT fine when a downstream step needs a machine-readable value — most importantly, when the next step is a \`foreach\` (which requires an array).

To make an agent step produce a structured shape, set \`outputSchema\` on the entry. It's a JSON Schema (Draft 2020-12) that the engine enforces at runtime and that also becomes the step's declared output shape for downstream wiring.

\`\`\`json
{
  "type": "agent",
  "id": "extract-paths",
  "agentId": "<discovered-agent-id>",
  "outputSchema": {
    "type": "array",
    "items": { "type": "string" },
    "description": "Absolute or repo-relative file paths, one per string."
  }
}
\`\`\`

Rules:
- \`outputSchema\` must be plain JSON Schema — same Draft 2020-12 subset the workflow's top-level \`inputSchema\` / \`outputSchema\` use. Nested objects, arrays, enums, and \`required\` all round-trip.
- When set, the step's output IS the schema's shape. So the agent above produces \`string[]\` — a raw array — which means a \`foreach\` can iterate it directly.
- The agent's input is still strictly \`{ prompt: string }\`. If the previous step does not already return that exact shape, insert a mapping that builds it. \`outputSchema\` shapes only what the agent RETURNS, not what it receives.
- Only agent entries support \`outputSchema\`. Tool entries derive their output shape from the tool's registered \`outputSchema\` — you don't set it on the step.
- Both agent and tool entries also accept an optional \`options: { retries?, metadata? }\` bag. Skip it unless the user asks for retries.

Use structured output when: the downstream step needs an array (for \`foreach\`), a specific object (for a mapping's \`step:\` source), or any value beyond free-form prose.

# Fan-out, iteration, and waiting — the container step types

These four types are top-level entries in \`graph\`. They can NOT nest inside each other in v1: a \`parallel\`'s children are \`agent\` / \`tool\` / \`classifier\` / \`workflow\` only, and \`foreach\`'s inner step is a single step, not another container.

**\`parallel\` — run several branches on the same input.** Emit exactly this shape:

\`\`\`json
{
  "type": "parallel",
  "steps": [
    { "type": "agent", "id": "summarise", "agentId": "<discovered-agent-id>" },
    { "type": "tool",  "id": "count-lines", "toolId": "wc-lines-tool" }
  ]
}
\`\`\`

The parallel step's output is \`{ "summarise": { "text": "..." }, "count-lines": <its outputSchema> }\`. It contains the complete output of **every** child under that child's id; never replace child outputs with only the input values used to call them. Downstream steps that need one branch's result pluck it via \`stepResults.<parallelId>.<childId>.<field>\` in a mapping.

**Giving parallel branches different inputs.** Every child of a \`parallel\` receives the SAME value — the preceding step's output — and children have no per-child input mapping. That does not mean branches must all consume the same thing. There are two patterns; pick by whether the branches need different FIELDS or different VALUES OF THE SAME FIELD.

*Pattern A — one shared object, each branch reads its own field.* Put a \`mapping\` BEFORE the \`parallel\` that builds one object containing every field the branches need, then let each branch consume the field that satisfies its own input schema. This is the default and needs no helper workflows.

\`\`\`json
[
  { "type": "mapping", "id": "prepare-inputs", "mapConfig": "{\\"prompt\\":{\\"initData\\":true,\\"path\\":\\"question\\"},\\"path\\":{\\"initData\\":true,\\"path\\":\\"filePath\\"}}" },
  { "type": "parallel", "steps": [
    { "type": "agent", "id": "answer", "agentId": "<discovered-agent-id>" },
    { "type": "tool", "id": "read-file", "toolId": "<discovered-tool-id>" }
  ] }
]
\`\`\`

Here the shared object is \`{ prompt, path }\`: the agent branch is satisfied by \`prompt\`, the tool branch by \`path\`. Extra fields a branch doesn't need are harmless — a branch only requires that the shared object SATISFIES its input schema.

*Pattern B — two branches call the SAME resource on different values.* Pattern A cannot express this. If both branches are the same tool requiring \`{ email }\`, one shared object has only one \`email\`, so both branches would look up the same value. The fix is a small **nested helper workflow per branch**: each helper accepts the shared object, maps ITS OWN field into the resource's input, and calls it. Helpers are ordinary workflows, so their first step may be a mapping — the restriction against mappings applies to container children, not to the inside of a nested workflow.

\`\`\`json
{ "type": "parallel", "steps": [
  { "type": "workflow", "id": "lookup-first", "workflowId": "lookup-first-customer" },
  { "type": "workflow", "id": "lookup-second", "workflowId": "lookup-second-customer" }
] }
\`\`\`

…where \`lookup-first-customer\` is \`[ mapping \`{"email":{"initData":true,"path":"email1"}}\`, tool \`lookup-customer\` ]\` and \`lookup-second-customer\` is the same with \`email2\`. Merge the results afterwards with a mapping reading \`{ "step": "lookup-first", "path": "customerId" }\`, using the call-site \`id\`.

Pattern B requires those helper workflows to exist as registered workflows. Whether you may create them yourself, and how, is defined by your surface policy below — do NOT assume it is forbidden, and do NOT invent per-child \`inputMapping\`, \`input\`, or \`with\` fields on container children. They do not exist and will be rejected.

**\`foreach\` — run the same step over every item in an array.** THIS IS THE ONLY WAY to run a step per-item. If the user says "for each", "for every", "on each", "one per", "iterate over", "run X on all the Ys", "map over" — the answer is \`foreach\`. Do not try to fake it with an agent that "loops internally"; do not try to unroll the array into N sibling steps. Emit:

\`\`\`json
{
  "type": "foreach",
  "step": { "type": "agent", "id": "review-file", "agentId": "<discovered-agent-id>" },
  "opts": { "concurrency": 3 }
}
\`\`\`

The rules:
- The step IMMEDIATELY BEFORE a \`foreach\` MUST produce an ARRAY as its top-level output. Not an object with an array field — the array itself. Foreach iterates \`previous.output\`, not \`previous.output.<somekey>\`.
- Because a \`mapping\` step always outputs an OBJECT (its top-level keys are \`mapConfig\`'s keys), a mapping CANNOT be the step before a \`foreach\` — a mapping's output is never a raw array.
- The inner \`step\` is a SINGLE step-like entry: agent, tool, classifier, or nested workflow. No nested \`foreach\` / \`parallel\` / \`conditional\` / \`loop\` / \`mapping\`.
- The inner step's \`id\` MUST be distinct from every other step id in the workflow (including the surrounding steps). A duplicate id will collide with \`stepResults\` lookups.
- The inner step receives ONE ELEMENT of the array at a time as its input, without coercion. An agent body therefore requires every array item to be exactly \`{ prompt: string }\`; a tool or nested-workflow body requires every item to satisfy its discovered input schema.
- Output is an array of the inner step's outputs, order-preserved. Agent inner steps ⇒ \`{ text: string }[]\`. Tool inner steps ⇒ \`toolOutputSchema[]\`.
- \`opts.concurrency\` (optional, default 1) controls how many elements run at once.

**When the upstream step does NOT produce a raw array — INSERT A MAPPING AND A BRIDGE AGENT.** This is the critical case, and it is what you will hit most often. Many tools return a formatted \`string\`; others return objects. You must NOT give up on \`foreach\` in this case, and you must NOT fake iteration inside a single agent's prompt. First insert a \`mapping\` that builds the bridge agent's exact \`{ prompt: string }\` input, then insert an \`agent\` step whose sole job is to convert the upstream data into the array shape \`foreach\` needs. Never feed a raw string, array, or differently shaped object directly into the bridge agent: declarative agent inputs are always \`{ prompt: string }\`. The bridge agent MUST declare an \`outputSchema\` whose top-level shape is the array (expressed as \`{ type: "array", items: {...} }\` in the canonical authoring schema). Because you can override an agent step's output shape via \`outputSchema\`, this bridge is always available, no matter what the upstream tool returns.

Concretely, the shape is ALWAYS one of:

- \`tool (returns array) → foreach\` — direct, no bridge.
- \`agent-with-outputSchema-array → foreach\` — direct, the agent step itself is the array producer.
- \`upstream-step (returns string OR object) → mapping (builds { prompt }) → bridge-agent (outputSchema: array) → foreach\` — the common case, USE THIS.
- A bridge agent may directly follow the upstream step only when that step already outputs exactly \`{ prompt: string }\`; otherwise the mapping is mandatory.

If the array elements must be strings and the inner \`foreach\` step is an \`agent\`, prefer \`outputSchema: z.array(z.object({ prompt: z.string() }))\` so each iteration receives a well-formed \`{ prompt }\` input.

Only fall back to a single discovered reasoning agent that iterates internally if there is literally no way to produce an array — for example, if the upstream data is unbounded streaming or the user explicitly forbids an extra LLM turn. "The tool returns a string" is NOT a valid excuse — insert the bridge agent.

Worked example — \`foreach\` after a string-returning tool:

\`\`\`json
[
  { "type": "tool", "id": "list-files", "toolId": "<discovered-listing-tool-id>" },
  {
    "type": "mapping",
    "id": "build-extraction-prompt",
    "mapConfig": "{\\"prompt\\":{\\"template\\":\\"Create one { prompt } object per file in this listing. Each prompt must tell the summarizer to read and summarize that file.\\\\n\\\\nListing:\\\\n\\\\n\${stepResults.list-files}\\"}}"
  },
  {
    "type": "agent",
    "id": "extract-paths",
    "agentId": "<discovered-agent-id>",
    "outputSchema": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": { "prompt": { "type": "string" } },
        "required": ["prompt"]
      },
      "description": "One { prompt } per file in the listing, where prompt asks the summarizer to read and summarize that file."
    }
  },
  {
    "type": "foreach",
    "step": { "type": "agent", "id": "summarise-one", "agentId": "<discovered-agent-id>" },
    "opts": { "concurrency": 3 }
  }
]
\`\`\`

The \`build-extraction-prompt\` mapping wraps the listing in the exact \`{ prompt: string }\` input the \`extract-paths\` bridge agent requires. The bridge agent then emits one \`{ prompt }\` object per file. Its \`outputSchema\` forces the array shape at the top level, which \`foreach\` iterates over.

**\`sleep\` — wait a fixed number of milliseconds.** Static only; a function form exists in code but does NOT round-trip.

\`\`\`json
{ "type": "sleep", "id": "cool-off", "duration": 5000 }
\`\`\`

**\`sleepUntil\` — wait until an ISO wall-clock date.** Also static only.

\`\`\`json
{ "type": "sleepUntil", "id": "wait-for-noon", "date": "2026-07-14T12:00:00Z" }
\`\`\`

# Conditional branches and loops — declarative predicates

The engine supports \`conditional\` (branch-on-predicate) and \`loop\` (dowhile / dountil) as static, round-trippable step types PROVIDED the condition is expressed as a small declarative JSON predicate — NOT as JS code. Closure-based conditions cannot round-trip through storage; if the user asks for one, you must express it in the predicate DSL below or fall back to an agent step that decides internally.

**Predicate DSL — the exhaustive list of \`op\` shapes.** Every predicate is one of:

- Comparison: \`{ "op": "eq" | "ne" | "lt" | "lte" | "gt" | "gte", "left": <PathOrLiteral>, "right": <PathOrLiteral> }\`
- Membership: \`{ "op": "in" | "notIn", "value": <PathOrLiteral>, "set": [<scalar>, ...] }\`
- Existence: \`{ "op": "exists" | "notExists", "path": "<dot.path>" }\`
- Truthiness: \`{ "op": "truthy" | "falsy", "value": <PathOrLiteral> }\`
- Boolean: \`{ "op": "and" | "or", "args": [<predicate>, ...] }\` — one or more sub-predicates.
- Negation: \`{ "op": "not", "arg": <predicate> }\`

\`<PathOrLiteral>\` is EITHER \`{ "path": "<dot.path>" }\` (looks up a value in the runtime scope) OR \`{ "literal": <string|number|boolean|null> }\` (an inline scalar). \`<scalar>\` in \`set\` is a raw string / number / boolean / null.

**Path scope — what \`"path"\` reads.** Predicates are evaluated with the same runtime scope as mappings:

- \`initData.<field>\` — the workflow's original input.
- \`inputData.<field>\` — the CURRENT step's input, i.e. the previous step's output. Use this to read what the conditional/loop sees on this iteration.
- \`stepResults.<stepId>.<field>\` — any earlier step's output. Dotted paths drill into nested fields.
- \`state.<field>\`, \`requestContext.<field>\` — advanced.

**\`conditional\` — run branches whose predicate is true.** Emit exactly this shape:

\`\`\`json
{
  "type": "conditional",
  "steps": [
    { "type": "agent", "id": "fix-lint", "agentId": "<discovered-agent-id>" },
    { "type": "agent", "id": "celebrate", "agentId": "<discovered-agent-id>" }
  ],
  "predicates": [
    { "op": "gt", "left": { "path": "inputData.errorCount" }, "right": { "literal": 0 } },
    { "op": "eq", "left": { "path": "inputData.errorCount" }, "right": { "literal": 0 } }
  ]
}
\`\`\`

Rules:
- \`predicates\` MUST be the same length as \`steps\`, aligned by index — predicate \`i\` gates step \`i\`.
- Every branch that evaluates truthy runs (multiple branches CAN run in parallel — this is not a switch/case). If you need exactly-one, make the predicates mutually exclusive.
- Every branch step is a single step (\`agent\` / \`tool\` / \`classifier\` / \`workflow\`) — no mappings or nested containers.
- All branches receive the same input: the previous step's output.
- The output is an object keyed by each branch step's \`id\`; a branch whose predicate was false has an \`undefined\` entry.

**Collapsing branches back into one field.** This is the step everyone gets wrong. Because an unfired branch produces no result, you CANNOT reference it individually: \`\${stepResults.<unfiredBranch>.<field>}\` THROWS at runtime and fails the entire run — even though the branch that did fire succeeded. To return "whatever the selected branch produced", add a following mapping that uses the step ARRAY source form, which picks the first branch that actually ran:

\`\`\`json
{
  "type": "mapping",
  "id": "select-response",
  "mapConfig": "{\\"response\\":{\\"step\\":[\\"urgent-support\\",\\"normal-support\\"],\\"path\\":\\"text\\"}}"
}
\`\`\`

List every branch id in the array. This is the ONLY correct way to merge mutually exclusive branches — do not concatenate them in a template, and do not map from the container (the \`conditional\` entry itself has no id and is not a readable step result).

**\`loop\` — repeat a step while / until a predicate holds.** Emit:

\`\`\`json
{
  "type": "loop",
  "step": { "type": "tool", "id": "poll-job", "toolId": "check_status_tool" },
  "loopType": "dountil",
  "predicate": { "op": "eq", "left": { "path": "inputData.status" }, "right": { "literal": "done" } }
}
\`\`\`

Rules:
- \`loopType: "dowhile"\` keeps looping while the predicate is TRUE.
- \`loopType: "dountil"\` keeps looping until the predicate is TRUE (predicate is the EXIT condition).
- The inner step runs at least once. Its \`outputSchema\` MUST also satisfy its own \`inputSchema\` (iteration N+1 feeds N's output back in), otherwise the second iteration fails validation.
- The predicate is evaluated on the inner step's output; use \`inputData.<field>\` to read that output inside the predicate.

# Nested workflows — compose one workflow inside another

You can reference an existing workflow as a single step. Discover valid ids through authoritative resource discovery and emit:

\`\`\`json
{ "type": "workflow", "id": "run-digest", "workflowId": "daily-standup-digest-only" }
\`\`\`

Rules:
- \`workflowId\` MUST resolve to a real workflow. Normally that means an id returned by authoritative resource discovery — never a name you invented or merely saw mentioned by the user. The one exception is a helper workflow you are authoring yourself as part of this same request (see Pattern B above): your surface policy defines whether that is allowed and how the helper is supplied, so follow it exactly rather than assuming either direction.
- The nested workflow's \`inputSchema\` is what the step CONSUMES; its \`outputSchema\` is what the step PRODUCES. Apply the composition check exactly as you would for a tool step.
- \`workflow\` entries are legal as branch steps inside \`conditional\`, as the inner step of \`foreach\` / \`dowhile\` / \`dountil\`, and as a child of \`parallel\`. Use this to keep the main graph flat: put a multi-step subgraph in its own stored workflow, then reference it.
- Do NOT self-reference (referencing the workflow you are currently authoring). Do NOT create cycles across workflows — the pre-flight validator will reject them.
- The nested workflow runs with its own scopes: its steps see their own \`initData\` (the input the parent passes into the nested workflow), its own \`stepResults\`, etc. The parent workflow only observes the nested workflow's final output.

# Anti-patterns — don't do these

- ❌ \`\${stepResults.fetch-weather.temperture}\` (typo) or any other field name you didn't see in the discovered \`outputSchema\`. Both \`\${stepResults.<id>}\` (JSON-encoded whole result) and \`\${stepResults.<id>.<realField>}\` (specific field) are valid — the wrong move is inventing field names.
- ❌ Inventing field names like \`.summary\` or \`.headline\` when they aren't in the previous step's \`outputSchema\`. If it's not in the schema you got from discovery, it doesn't exist.
- ❌ Using \`\${inputData.<workflowInputField>}\` in a mapping AFTER step 1 — \`inputData\` past step 1 is the previous step's OUTPUT, not the workflow input. To reach the workflow's original input, use \`\${initData.<field>}\`. (For the specific previous step by name, use \`\${stepResults.<previous-step-id>.<field>}\`.)
- ❌ Building fake indexed access into a template like \`\${stepResults.foreach-id.0.text} \${stepResults.foreach-id.1.text} ...\` to work around "templates can't render arrays". Templates now JSON-encode arrays and objects automatically; just write \`\${stepResults.foreach-id}\`.
- ❌ Skipping a mapping when shapes don't line up. Two consecutive steps whose output/input shapes don't match WILL fail.
- ❌ Feeding a tool that returns a string DIRECTLY into an agent step. Agent input is strictly \`{ prompt: string }\` — the engine does NOT wrap or coerce. Insert a mapping producing \`{ prompt: "<template referencing the tool output>" }\`.
- ❌ Feeding a \`foreach\` over an \`agent\` inner step from an upstream that emits \`Array<string>\` or \`Array<{someObject}>\`. The inner agent step still requires \`{ prompt: string }\` per iteration — and \`mapping\` CANNOT sit inside a \`foreach\`. Fix: change the upstream so it emits \`Array<{ prompt: string }>\` directly via its \`outputSchema\` (an agent with structured output can do this trivially by prompting "emit an array of \`{ prompt }\` objects, one per file"), OR make the foreach's inner a \`tool\` whose \`inputSchema\` matches what your array elements already look like.
- ❌ Adding a no-op step-1 mapping that just renames \`inputData\` keys. Step 1 receives the workflow input object directly. (Past step 1, if you need workflow input again, use \`\${initData.…}\` — not a rename mapping.)
- ❌ \`mapConfig\` as an object (\`"mapConfig": { ... }\`). It MUST be a JSON-encoded string (\`"mapConfig": "{...}"\`).
- ❌ Refusing to use \`foreach\` because no upstream tool returns an array, and falling back to a single agent step that "loops internally". The engine has NO array→iteration workaround that beats \`foreach\`. For a string/object-returning upstream, the correct move is ALWAYS \`upstream → mapping ({ prompt }) → bridge agent (array outputSchema) → foreach\`. "The tool doesn't return an array" is never a reason to skip \`foreach\` — it is the reason to add both the mapping and bridge agent.
- ❌ Concatenating \`conditional\` branches in a template to "get whichever one ran" — \`\${stepResults.urgent-support.text}\${stepResults.normal-support.text}\`. Only one branch runs; referencing the unfired one THROWS and fails the whole workflow even though the selected branch succeeded. Use the step-array mapping source instead: \`{"response":{"step":["urgent-support","normal-support"],"path":"text"}}\`.
- ❌ Guessing a \`workflowId\` — inventing an id, or using a name the user mentioned without confirming it through authoritative resource discovery. Nested references must resolve when the definition is validated. (Helper workflows you author yourself for Pattern B are the one exception, and only in the exact way your surface policy specifies.)
- ❌ Self-referencing (\`workflowId\` equal to the workflow you are currently authoring) or building A→B→A cycles across workflows. The pre-flight validator will reject them.
- ❌ Writing a bridge mapping that pipes ONLY the previous step's output when the downstream agent needs ADDITIONAL context from the workflow input to be useful. Classic case: a listing tool returns bare basenames (e.g. \`app-tools.ts\\nserver.ts\`) — no path prefix — so a downstream agent asked to "read and summarize each file" has no idea what folder they live in. Fix: combine both scopes in the mapping template. \`\${initData.<workflowInputField>}\` is available in EVERY mapping; use it to thread the workflow's original input (folder path, repo name, target branch, ticket id, etc.) into the prompt alongside \`\${stepResults.<upstream>}\`. See the "combining upstream output with workflow input" worked example below.

# Worked examples

The examples below use whatever domain made them concrete — files, GitHub issues, customer records. **The domain is never the point; the SHAPES are.** Do not read them as a claim that any particular tool or agent is registered for you. Every \`toolId\` / \`agentId\` written as \`<discovered-…>\` is a slot you fill from your own authoritative discovery result, and a workflow built from real resources in a completely different domain follows the identical shape rules.

# Worked example: list files → review each

User says: "build a workflow that lists the .ts files in a directory and runs the security-expert agent on each one's contents. id it sec-review."

Discovery returns (excerpts):
- a listing tool (referred to below as \`<discovered-listing-tool-id>\`): inputSchema \`{ path: string, ... }\`, outputSchema tree-formatted text (string output).
- a file-reading tool: inputSchema \`{ path: string, ... }\`, outputSchema string (file contents).
- (If a "security-expert" agent isn't registered) agent steps reference a discovered reasoning agent, outputShape \`{ text: string }\`. Use that instead.

If discovery shows these tools return raw strings (not objects), templates can interpolate the string directly. If discovery shows a richer object shape, pluck specific fields via \`stepResults.<id>.<field>\`. **Always read the schema first; the worked-example shapes above are illustrative — confirm against your discovery result.**

# Worked example: foreach — run an agent on each item of a list

User says: "for every open GitHub issue in the repo, have a discovered reasoning agent write a one-line triage note. id: triage-issues."

Discovery must surface an upstream that returns an ARRAY as its top-level output, AND each element of that array must already be shaped like the inner step's required input. The inner step here is an \`agent\`, so each element must be \`{ prompt: string }\`. If \`github_list_open_issues\` returns \`{ title: string, body: string }[]\`, that's the WRONG shape — the agent step will reject each iteration with "expected object, received …" because \`{ title, body }\` is not \`{ prompt }\`. And \`mapping\` cannot sit inside a \`foreach\` to fix it per-iteration.

The fix: map the raw list into the bridge agent's \`{ prompt: string }\` input, have that agent produce \`Array<{ prompt: string }>\` with a structured \`outputSchema\`, then iterate that array:

\`\`\`json
[
  { "type": "tool", "id": "list-issues", "toolId": "github_list_open_issues" },
  {
    "type": "mapping",
    "id": "build-triage-prompt",
    "mapConfig": "{\\"prompt\\":{\\"template\\":\\"Convert every issue below into one { prompt } object that asks for a one-line triage note and includes the issue title and body.\\\\n\\\\nIssues:\\\\n\\\\n\${stepResults.list-issues}\\"}}"
  },
  {
    "type": "agent",
    "id": "prep-prompts",
    "agentId": "<discovered-agent-id>",
    "outputSchema": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": { "prompt": { "type": "string" } },
        "required": ["prompt"]
      },
      "description": "One { prompt } per input issue; the prompt should ask for a one-line triage note and embed the issue's title and body."
    }
  },
  {
    "type": "foreach",
    "step": { "type": "agent", "id": "triage-one", "agentId": "<discovered-agent-id>" },
    "opts": { "concurrency": 3 }
  }
]
\`\`\`

Now \`triage-one\` receives \`{ prompt: string }\` per iteration — schemas line up — and returns \`{ text }\`. The foreach's output is \`{ text }[]\`, one per issue, in list order. The workflow's \`outputSchema\` is \`{ type: "array", items: { type: "object", properties: { text: { type: "string" } }, required: ["text"] } }\`.

**Why both extra steps exist:** \`build-triage-prompt\` gives the bridge agent its required \`{ prompt: string }\` input. The bridge agent is then the only declarative way to project \`Array<X>\` into a raw \`Array<{ prompt: string }>\` root today. A mapping can't produce an array-shaped root, and it can't live inside a foreach.

If instead \`github_list_open_issues\` returns \`{ issues: [...] }\` (array nested inside an object), you STILL need both steps: the mapping places that object in the bridge agent's prompt, and \`prep-prompts\` handles both un-wrapping and shape conversion.

# Worked example: extract-then-iterate using structured agent output

User says: "summarise every .ts file in packages/core/src/workflows. id: summarise-workflows."

Discovery surfaces:
- a listing tool (referred to below as \`<discovered-listing-tool-id>\`) — inputSchema \`{ path: string, ... }\`, outputSchema string (tree-formatted).
- a discovered reasoning agent — \`{ text: string }\` by default.

The tree string isn't iterable. We need to (a) turn it into an array whose elements match the foreach inner step's input, then (b) foreach over it. The inner step here is an \`agent\`, so each array element must be \`{ prompt: string }\`. Bridge with a structured agent step that emits that shape directly:

\`\`\`json
[
  { "type": "tool", "id": "list", "toolId": "<discovered-listing-tool-id>" },
  {
    "type": "mapping",
    "id": "to-extract-prompt",
    "mapConfig": "{\\"prompt\\":{\\"template\\":\\"The listing below contains BARE filenames (no path prefix) inside the folder \${initData.path}. For every .ts entry, emit an object { prompt: <a request to summarise the file at ABSOLUTE PATH \${initData.path}/<filename>> }. Return the array only, no prose.\\\\n\\\\nListing:\\\\n\${stepResults.list}\\"}}"
  },
  {
    "type": "agent",
    "id": "prep-summarise-prompts",
    "agentId": "<discovered-agent-id>",
    "outputSchema": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": { "prompt": { "type": "string" } },
        "required": ["prompt"]
      },
      "description": "One { prompt } per .ts file, ready to feed a foreach-over-agent."
    }
  },
  {
    "type": "foreach",
    "step": { "type": "agent", "id": "summarise-one", "agentId": "<discovered-agent-id>" },
    "opts": { "concurrency": 3 }
  }
]
\`\`\`

Walk the shapes:
- \`list\` outputs a string (bare filenames relative to the listed folder, no path prefix).
- \`to-extract-prompt\` (mapping) combines the bare listing with \`\${initData.path}\` so the bridge agent gets both. Result matches \`prep-summarise-prompts\`'s required \`{ prompt: string }\`.
- \`prep-summarise-prompts\` (agent with \`outputSchema\`) emits \`Array<{ prompt: string }>\` where each prompt embeds the ABSOLUTE path — so \`summarise-one\` can actually read the file.
- \`foreach\` iterates that array; each element \`{ prompt: string }\` matches \`summarise-one\`'s required input exactly.
- \`summarise-one\` returns \`{ text }\`; foreach's output is \`{ text }[]\`.

**The general pattern for fanning out to an agent from an unstructured upstream:** tool-string → mapping-to-prompt-that-combines-tool-output-with-\`initData\` → agent-with-array-of-prompt-objects → foreach-over-agent. If the foreach's inner is a \`tool\` instead of an agent, the bridge agent should emit \`Array<{...that tool's inputSchema}>\` instead of \`Array<{ prompt }>\`.

**The critical thing to notice:** the bridge agent CANNOT invent context that isn't in its prompt. If the upstream tool strips context (like a listing tool stripping the folder path from each entry), the mapping MUST re-thread that context via \`\${initData.…}\`. Missing this is the #1 cause of downstream steps failing with "file not found", "invalid id", "no such record", etc.

# Worked example: feeding a foreach's output into a synthesis agent

The output of a \`foreach(agent)\` step is \`Array<{ text: string }>\`, one entry per iteration. To fan the results back INTO a final synthesis agent, DO NOT write out indexed slots like \`\${stepResults.summarise-one.0.text}\`, \`\${stepResults.summarise-one.1.text}\`, etc. — that's an anti-pattern. Templates JSON-encode arrays and objects, so hand the whole thing to the synthesis agent in a single placeholder:

\`\`\`json
[
  { "type": "tool", "id": "list", "toolId": "<discovered-listing-tool-id>" },
  { "type": "mapping", "id": "to-extract-prompt", "mapConfig": "..." },
  { "type": "agent", "id": "prep-summarise-prompts", "agentId": "<discovered-agent-id>", "outputSchema": { "type": "array", "items": { "type": "object", "properties": { "prompt": { "type": "string" } }, "required": ["prompt"] } } },
  { "type": "foreach", "step": { "type": "agent", "id": "summarise-one", "agentId": "<discovered-agent-id>" }, "opts": { "concurrency": 3 } },
  {
    "type": "mapping",
    "id": "to-synth-prompt",
    "mapConfig": "{\\"prompt\\":{\\"template\\":\\"You are given a list of individual file summaries as JSON. Produce a single coherent overview of what the folder contains.\\\\n\\\\nSummaries (JSON):\\\\n\${stepResults.summarise-one}\\"}}"
  },
  { "type": "agent", "id": "final-summary", "agentId": "<discovered-agent-id>", "outputSchema": { "type": "object", "properties": { "summary": { "type": "string" } }, "required": ["summary"] } }
]
\`\`\`

\`\${stepResults.summarise-one}\` becomes a JSON-encoded string like \`[{"text":"..."},{"text":"..."}]\`, which the synthesis agent can read directly. This scales to any number of foreach iterations — no fixed slot count.

# Worked example: combining upstream output with workflow input in a mapping

Very common pattern: an upstream tool returns a value that's only meaningful IN CONTEXT of the workflow's original input, and a downstream agent needs both. Example: a listing tool returns \`app-tools.ts\\nserver.ts\` (bare basenames), but the workflow input has the folder path (\`{ path: "/repo/src/agents" }\`). A downstream agent asked to summarise each file needs the absolute path — combine both scopes in the mapping:

\`\`\`json
{
  "type": "mapping",
  "id": "to-summary-prompt",
  "mapConfig": "{\\"prompt\\":{\\"template\\":\\"Files in \${initData.path}:\\\\n\${stepResults.list-files}\\\\n\\\\nFor each file above, read it at absolute path \${initData.path}/<filename> and write a summary.\\"}}"
}
\`\`\`

The mapping template can reference AS MANY scopes AS YOU NEED. \`initData\` is always the workflow's original input; \`stepResults.<id>\` is any prior step's output. Use both together whenever the upstream step alone doesn't carry enough context for the downstream to act.

# Worked example: reusing the workflow's original input past step 1

If the workflow input is \`{ path: string }\` and step 3 needs that same \`path\` again, you CANNOT use \`\${inputData.path}\` — at step 3, \`inputData\` is step 2's output. Use \`\${initData.path}\`:

\`\`\`json
[
  { "type": "tool", "id": "list", "toolId": "<discovered-listing-tool-id>" },
  { "type": "agent", "id": "pick-first", "agentId": "<discovered-agent-id>" },
  {
    "type": "mapping",
    "id": "final-prompt",
    "mapConfig": "{\\"prompt\\":{\\"template\\":\\"Root path was \${initData.path}. First candidate: \${stepResults.pick-first.text}\\"}}"
  }
]
\`\`\`

Rule of thumb: for the workflow's original input, \`initData\` is always safe. \`inputData\` is only equal to the workflow input at step 1.

# Definition quality

- Always include a concise \`description\` on the workflow definition that summarizes what the workflow does. Do not submit a workflow with a null or empty description.

# Out of scope — do NOT emit these

- Any \`sleep\` / \`sleepUntil\` with a function-form duration/date.
- \`conditional\` / \`loop\` with a JS-closure condition. Use the declarative predicate DSL above instead. If the condition genuinely cannot be expressed as a predicate (e.g. requires an LLM decision), fall back to an \`agent\` step that decides internally and returns \`{ text }\` naming the branch.
- Any \`mapping\` with an \`fn\` source. Only declarative sources (\`template\`, \`value\`, \`step\`, \`initData\`, \`requestContextPath\`) round-trip.
- Human-in-the-loop **suspend / resume**. There is no graph entry that suspends a run to collect input and resumes it later, and no step field that requests one. If the user asks the workflow to pause for approval, wait for a confirmation, or ask a human mid-run, say plainly that persisted workflows cannot suspend, and offer the closest supported design: split the work into two workflows the caller runs on either side of its own approval step, or use an \`agent\` step that decides without human input. Do NOT emit \`suspend\`, \`resume\`, \`suspendSchema\`, or \`resumeSchema\` fields, and do NOT silently drop the requirement — the user must learn the pause is not there.
- Writes to workflow \`state\`. State is **read-only** to the graph you author: you may READ it (\`\${state.<field>}\` in a template, against a \`stateSchema\` you declare), but no mapping, predicate, or container entry can SET it. Only a tool's own implementation can write state, through the \`setState\` handle the engine passes into that tool at execution time. So "remember this between steps" is satisfiable only when a discovered tool already writes the value itself; otherwise carry the value forward explicitly with \`initData\` / \`stepResults\` mappings rather than claiming state persistence you cannot author.`;
// src/workflows/builder/preflight.ts
/**
* Compatibility wrapper over the one dynamic-workflow validation domain
* (`workflows/dynamic/validate/`). Kept so authoring frontends (Studio,
* mastracode) can keep importing preflight names from
* `@mastra/core/workflows/builder`; all rules live in the shared core.
*/
/**
* Collect-mode validation of an authoring definition. Context gates the
* context-dependent checks: reference checks only run for dependency kinds
* present in the context, and schema-flow compatibility only proves
* mismatches where schemas are known.
*/
function preflightWorkflowDefinition(definition, context = {}) {
	const issues = require_validate.validateDynamicWorkflow(definition, context);
	return issues.length === 0 ? { ok: true } : {
		ok: false,
		issues
	};
}
// src/workflows/builder/inspection.ts
function inspectWorkflowBuilderSchemas(definition, registry = {}) {
	return require_validate.inferGraphSchemas(definition, registry);
}
function compareWorkflowBuilderSchemas(source, destination) {
	return require_validate.schemaCompatibility(source, destination);
}
// src/workflows/builder/authoring-schema.ts
/**
* Shared model-facing authoring contract for complete persisted workflow
* definitions. Both authoring surfaces (Mastra Code `save-workflow` and
* Studio `submit-workflow-draft`) consume these schemas so the guidance a
* model sees at the final submission boundary is identical everywhere.
*
* The model-facing input flavor (`...InputSchema`) reuses the canonical entry
* schemas directly and differs only where the wider input is genuinely
* friendlier to authoring models AND survives provider strict-schema
* compatibility transforms (OpenAI makes every property required and closes
* arbitrary-key records to empty objects, so optional alias pairs and
* required `z.record(...)` fields become unfillable contradictions):
* - `mapConfig` accepts object form as well as the canonical JSON string.
* - Optional fields accept explicit `null` (strict providers force models to
*   emit `null` for fields they cannot omit).
* - Opaque JSON Schema fields are `z.unknown()`, mirroring the persisted
*   `WorkflowDefinition` contract, so they stay fillable under OpenAI.
* `normalizeWorkflowBuilderDefinition` canonicalizes the wider input
* (stringifies object `mapConfig`, drops `null` optionals).
*
* Surface-specific lifecycle wording (persist-immediately vs. Ready + explicit
* user Save) stays out of this module; attach it on the tool description at
* each surface.
*/
const WORKFLOW_BUILDER_MAPPING_CONFIG_DESCRIPTION = "An object whose top-level keys become the mapping output fields. Each value must use exactly one canonical source form: { \"template\": \"<text with ${placeholders}>\" }, { \"value\": <constant> }, { \"step\": \"<stepId>\", \"path\": \"<field.path>\" }, { \"initData\": true, \"path\": \"<workflow-input-field.path>\" }, or { \"requestContextPath\": \"<field.path>\" }. IMPORTANT: initData is the boolean true, never a field name string; put the workflow input field name in path. Template placeholders use JavaScript-style ${initData.<field>}, ${inputData.<field>}, ${stepResults.<stepId>.<field>}, ${state.<field>}, or ${requestContext.<field>} — never Handlebars {{...}} and never separate sources/data bindings. May also be provided as a JSON-encoded string of the same object.";
const jsonValueSchema = zod.z.lazy(() => zod.z.union([
	zod.z.null(),
	zod.z.boolean(),
	zod.z.string(),
	zod.z.number().finite(),
	zod.z.array(jsonValueSchema),
	zod.z.record(zod.z.string(), jsonValueSchema)
]));
const jsonSchema = zod.z.record(zod.z.string(), jsonValueSchema);
const STEP_OPTIONS_DESCRIPTION = "JSON-safe subset of step options that round-trips through storage. `onFinish` callbacks and function-valued scorers are NOT supported.";
const stepOptionsSchema = zod.z.object({
	retries: zod.z.number().int().nonnegative().optional().describe("Retry count on failure. Static number only."),
	metadata: jsonSchema.optional().describe("Arbitrary JSON-safe metadata attached to the step.")
}).optional().describe(STEP_OPTIONS_DESCRIPTION);
const stepOptionsInputSchema = zod.z.object({
	retries: zod.z.number().int().nonnegative().nullish().describe("Retry count on failure. Static number only."),
	metadata: zod.z.unknown().nullish().describe("Arbitrary JSON-safe metadata object attached to the step.")
}).nullish().describe(STEP_OPTIONS_DESCRIPTION);
const ENTRY_ID_DESCRIPTION = "Optional stable entry id — kebab-case, unique within the workflow. Lets editors and tools address this control-flow entry across edits and serialization.";
const ENTRY_DESCRIPTION_DESCRIPTION = "Optional human-readable description of why this control-flow operation exists.";
const ENTRY_METADATA_DESCRIPTION = "Arbitrary JSON-safe metadata attached to this entry (e.g. a display title for visual editors). Does not affect execution.";
const entryDisplayFields = {
	description: zod.z.string().optional().describe(ENTRY_DESCRIPTION_DESCRIPTION),
	metadata: jsonSchema.optional().describe(ENTRY_METADATA_DESCRIPTION)
};
const entryDisplayInputFields = {
	description: zod.z.string().nullish().describe(ENTRY_DESCRIPTION_DESCRIPTION),
	metadata: zod.z.unknown().nullish().describe(ENTRY_METADATA_DESCRIPTION)
};
const containerIdentityFields = {
	id: zod.z.string().min(1).optional().describe(ENTRY_ID_DESCRIPTION),
	...entryDisplayFields
};
const containerIdentityInputFields = {
	id: zod.z.string().min(1).nullish().describe(ENTRY_ID_DESCRIPTION),
	...entryDisplayInputFields
};
const agentOutputSchemaDescription = "OPTIONAL JSON Schema (Draft 2020-12) describing the structured output the agent must produce for this step. When set, the agent runs with structured output and the step's output IS that shape (not `{ text: string }`). Use this when a downstream step needs a machine-readable field — for example, an agent that reads a listing and emits `{ files: string[] }`, which a subsequent `foreach` iterates over.";
const AGENT_ENTRY_DESCRIPTION = "Agent step. Default agents consume { prompt: string } and return { text: string }; insert a mapping step producing { prompt } before the agent when shapes differ, and map its result from the `text` field afterwards — never invent output fields such as `response`. When `outputSchema` is set the output IS that schema shape instead. Use an agent ID returned by resource discovery; never invent IDs.";
const workflowBuilderAgentEntrySchema = zod.z.strictObject({
	type: zod.z.literal("agent"),
	id: zod.z.string().min(1).describe("Step id — kebab-case, unique within the workflow."),
	agentId: zod.z.string().min(1).describe("Id of an agent registered on this Mastra instance (from resource discovery)."),
	description: zod.z.string().optional(),
	outputSchema: zod.z.unknown().optional().describe(agentOutputSchemaDescription),
	options: stepOptionsSchema
}).describe(AGENT_ENTRY_DESCRIPTION);
const workflowBuilderAgentEntryInputSchema = workflowBuilderAgentEntrySchema.extend({
	description: zod.z.string().nullish(),
	outputSchema: zod.z.unknown().nullish().describe(agentOutputSchemaDescription),
	options: stepOptionsInputSchema
}).describe(AGENT_ENTRY_DESCRIPTION);
const TOOL_ENTRY_DESCRIPTION = "Tool step. The previous step's output is validated against the tool's inputSchema and the step produces the tool's outputSchema shape exactly.";
const workflowBuilderToolEntrySchema = zod.z.strictObject({
	type: zod.z.literal("tool"),
	id: zod.z.string().min(1).describe("Step id — kebab-case, unique within the workflow."),
	toolId: zod.z.string().min(1).describe("Id of a tool registered on this Mastra instance (from resource discovery)."),
	description: zod.z.string().optional(),
	options: stepOptionsSchema
}).describe(TOOL_ENTRY_DESCRIPTION);
const workflowBuilderToolEntryInputSchema = workflowBuilderToolEntrySchema.extend({
	description: zod.z.string().nullish(),
	options: stepOptionsInputSchema
}).describe(TOOL_ENTRY_DESCRIPTION);
const CLASSIFIER_ENTRY_DESCRIPTION = "Classifier step. Evaluates its complete input with a registered configured classifier and returns { answers, usage }. Route with a following conditional entry using inputData.answers.<question>.choice, inputData.answers.<question>.score, or inputData.answers.<question>.probability. A refused question is { type: \"refusal\" } with no choice, score, or probability; route refusals with inputData.answers.<question>.type. Use a mapping entry before the classifier to reshape its input.";
const classifierOptionsSchema = zod.z.object({
	maxRetries: zod.z.number().int().nonnegative().optional().describe("Retry count for the classifier model call."),
	providerOptions: jsonSchema.optional().describe("JSON-safe provider-specific classifier model options."),
	retries: zod.z.number().int().nonnegative().optional().describe("Workflow step retry count, separate from maxRetries."),
	metadata: jsonSchema.optional().describe("Arbitrary JSON-safe metadata attached to the step.")
}).optional();
const classifierOptionsInputSchema = zod.z.object({
	maxRetries: zod.z.number().int().nonnegative().nullish().describe("Retry count for the classifier model call."),
	providerOptions: jsonSchema.nullish().describe("JSON-safe provider-specific classifier model options."),
	retries: zod.z.number().int().nonnegative().nullish().describe("Workflow step retry count, separate from maxRetries."),
	metadata: jsonSchema.nullish().describe("Arbitrary JSON-safe metadata attached to the step.")
}).nullish();
const workflowBuilderClassifierEntrySchema = zod.z.strictObject({
	type: zod.z.literal("classifier"),
	id: zod.z.string().min(1).describe("Step id — kebab-case, unique within the workflow."),
	classifierId: zod.z.string().min(1).describe("Id of a configured classifier registered on this Mastra instance (from resource discovery)."),
	description: zod.z.string().optional(),
	options: classifierOptionsSchema
}).describe(CLASSIFIER_ENTRY_DESCRIPTION);
const workflowBuilderClassifierEntryInputSchema = workflowBuilderClassifierEntrySchema.extend({
	description: zod.z.string().nullish(),
	options: classifierOptionsInputSchema
}).describe(CLASSIFIER_ENTRY_DESCRIPTION);
const workflowBuilderMappingDescriptorSchema = zod.z.union([
	zod.z.object({ value: zod.z.unknown() }).strict().describe("Constant source: { \"value\": <JSON value> }."),
	zod.z.object({ template: zod.z.string().min(1).describe("JavaScript-style interpolation using ${initData.name}, ${inputData.field}, ${stepResults.stepId.field}, ${state.field}, or ${requestContext.field}. Do not use Handlebars {{...}} or separate sources/data bindings.") }).strict().describe("Template source: { \"template\": \"Hello, ${initData.name}!\" }. The descriptor contains only template."),
	zod.z.object({ requestContextPath: zod.z.string().min(1) }).strict(),
	zod.z.object({
		initData: zod.z.literal(true),
		path: zod.z.string().min(1)
	}).strict().describe("Workflow-input source: { \"initData\": true, \"path\": \"field.path\" }. initData must be true."),
	zod.z.object({
		step: zod.z.union([zod.z.string().min(1), zod.z.array(zod.z.string().min(1)).min(1)]),
		path: zod.z.string()
	}).strict().describe("Prior-step source: { \"step\": \"step-id\", \"path\": \"field.path\" }.")
]).describe("Use exactly one source form. Never combine initData and step.");
const workflowBuilderMappingConfigSchema = zod.z.record(zod.z.string(), workflowBuilderMappingDescriptorSchema);
const workflowBuilderMappingEntrySchema = zod.z.strictObject({
	type: zod.z.literal("mapping"),
	id: zod.z.string().min(1).describe("Step id — kebab-case, unique within the workflow."),
	...entryDisplayFields,
	mapConfig: zod.z.string().min(1).describe(`A JSON-ENCODED STRING of ${WORKFLOW_BUILDER_MAPPING_CONFIG_DESCRIPTION}`)
}).describe("Mapping step. Its output is an object whose top-level keys are exactly the keys of mapConfig.");
const workflowBuilderMappingEntryInputSchema = zod.z.strictObject({
	type: zod.z.literal("mapping"),
	id: zod.z.string().min(1).describe("Step id — kebab-case, unique within the workflow."),
	...entryDisplayInputFields,
	mapConfig: zod.z.union([workflowBuilderMappingConfigSchema, zod.z.string().min(1)]).describe(WORKFLOW_BUILDER_MAPPING_CONFIG_DESCRIPTION)
}).describe("Mapping step. Its output is an object whose top-level keys are exactly the keys of mapConfig.");
const workflowBuilderNestedWorkflowEntrySchema = zod.z.strictObject({
	type: zod.z.literal("workflow"),
	id: zod.z.string().min(1).describe("Call-site step id — kebab-case, unique within the workflow. This is the id that addresses this step result (stepResults.<id>); it does not need to equal workflowId."),
	workflowId: zod.z.string().min(1).describe("Authoritative ID of another workflow registered on this Mastra instance, exactly as returned by resource discovery. Never invent workflow IDs, self-reference, or create cycles."),
	description: zod.z.string().optional(),
	options: stepOptionsSchema
}).describe("Nested workflow step. The referenced workflow runs as a single step: its input is the current step input (a first top-level nested workflow receives the parent input directly when schemas match) and its output becomes this step output. Map its output through stepResults.<id> — this step own id — when a different final shape is required.");
const workflowBuilderNestedWorkflowEntryInputSchema = workflowBuilderNestedWorkflowEntrySchema.extend({
	description: zod.z.string().nullish(),
	options: stepOptionsInputSchema
}).describe(String(workflowBuilderNestedWorkflowEntrySchema.description));
const executableInnerStepSchema = zod.z.union([
	workflowBuilderAgentEntrySchema,
	workflowBuilderToolEntrySchema,
	workflowBuilderClassifierEntrySchema,
	workflowBuilderNestedWorkflowEntrySchema
]);
const executableInnerStepInputSchema = zod.z.union([
	workflowBuilderAgentEntryInputSchema,
	workflowBuilderToolEntryInputSchema,
	workflowBuilderClassifierEntryInputSchema,
	workflowBuilderNestedWorkflowEntryInputSchema
]);
const literalScalarSchema = zod.z.union([
	zod.z.string(),
	zod.z.number(),
	zod.z.boolean(),
	zod.z.null()
]);
const predicatePathSchema = zod.z.string().regex(/^(initData|inputData|stepResults|state)(\.[A-Za-z0-9_-]+)*$/, "Use a canonical predicate path root.").describe("Declarative path: initData.<field> for workflow input, inputData.<field> for the previous step output, stepResults.<stepId>.<field> for another step output, or state.<field>.");
const pathOrLiteralSchema = zod.z.union([zod.z.strictObject({ path: predicatePathSchema }), zod.z.strictObject({ literal: literalScalarSchema })]);
const workflowBuilderPredicateSchema = zod.z.lazy(() => zod.z.union([
	zod.z.strictObject({
		op: zod.z.enum([
			"eq",
			"ne",
			"lt",
			"lte",
			"gt",
			"gte"
		]),
		left: pathOrLiteralSchema,
		right: pathOrLiteralSchema
	}),
	zod.z.strictObject({
		op: zod.z.enum(["in", "notIn"]),
		value: pathOrLiteralSchema,
		set: zod.z.array(literalScalarSchema).min(1)
	}),
	zod.z.strictObject({
		op: zod.z.enum(["exists", "notExists"]),
		path: predicatePathSchema
	}),
	zod.z.strictObject({
		op: zod.z.enum(["truthy", "falsy"]),
		value: pathOrLiteralSchema
	}),
	zod.z.strictObject({
		op: zod.z.enum(["and", "or"]),
		args: zod.z.array(workflowBuilderPredicateSchema).min(1)
	}),
	zod.z.strictObject({
		op: zod.z.literal("not"),
		arg: workflowBuilderPredicateSchema
	})
]));
const PARALLEL_DESCRIPTION = "Parallel container. Each child receives the same preceding input and children must be agent/tool/classifier/nested workflow — no nested containers or mappings. The result is an object keyed by each child step id containing that child complete output; downstream steps pluck fields via stepResults.<childId>.<field>.";
const FOREACH_DESCRIPTION = "Foreach container. The preceding output MUST be a raw array (not an object with an array field). Each item is passed directly to the child step — no child inputMapping — and the output is an array of child outputs, order preserved. Give the inner step its own unique id.";
const CONDITIONAL_DESCRIPTION = "Conditional container. Predicates align by index with steps and use declarative initData, inputData, stepResults, or state paths; every branch whose predicate is truthy runs on the same preceding input. Branch results are keyed by the BRANCH step ids — map from those, never from the container id, which is not a readable step result. To collapse mutually exclusive branches into one field, give the mapping source a step ARRAY: { \"step\": [\"urgent-support\", \"normal-support\"], \"path\": \"text\" } selects whichever branch actually ran. Add a final mapping after the conditional whenever the branch result does not already match outputSchema.";
const LOOP_DESCRIPTION = "`dowhile` keeps looping while the predicate is TRUE; `dountil` keeps looping until the predicate is TRUE (exit condition). The inner step runs at least once and receives its own previous output on later iterations.";
const workflowBuilderParallelEntrySchema = zod.z.strictObject({
	type: zod.z.literal("parallel"),
	...containerIdentityFields,
	steps: zod.z.array(executableInnerStepSchema).min(1)
}).describe(PARALLEL_DESCRIPTION);
const workflowBuilderForeachEntrySchema = zod.z.strictObject({
	type: zod.z.literal("foreach"),
	...containerIdentityFields,
	step: executableInnerStepSchema,
	opts: zod.z.object({ concurrency: zod.z.number().int().positive() }).optional().describe("Optional concurrency control; defaults to 1 (sequential).")
}).describe(FOREACH_DESCRIPTION);
const workflowBuilderSleepEntrySchema = zod.z.strictObject({
	type: zod.z.literal("sleep"),
	id: zod.z.string().min(1),
	...entryDisplayFields,
	duration: zod.z.number().nonnegative().describe("Milliseconds to wait. Static number only.")
});
const workflowBuilderSleepUntilEntrySchema = zod.z.strictObject({
	type: zod.z.literal("sleepUntil"),
	id: zod.z.string().min(1),
	...entryDisplayFields,
	date: zod.z.string().min(1).describe("ISO 8601 wall-clock date to wait until. Static string only.")
});
const workflowBuilderConditionalEntrySchema = zod.z.strictObject({
	type: zod.z.literal("conditional"),
	...containerIdentityFields,
	steps: zod.z.array(executableInnerStepSchema).min(1),
	predicates: zod.z.array(workflowBuilderPredicateSchema).min(1).describe("One declarative predicate per branch, aligned by array index with steps. No JS closures.")
}).describe(CONDITIONAL_DESCRIPTION);
const workflowBuilderLoopEntrySchema = zod.z.strictObject({
	type: zod.z.literal("loop"),
	...containerIdentityFields,
	step: executableInnerStepSchema,
	loopType: zod.z.enum(["dowhile", "dountil"]),
	predicate: workflowBuilderPredicateSchema.describe("Declarative predicate — no JS closures.")
}).describe(LOOP_DESCRIPTION);
const workflowBuilderParallelEntryInputSchema = zod.z.strictObject({
	type: zod.z.literal("parallel"),
	...containerIdentityInputFields,
	steps: zod.z.array(executableInnerStepInputSchema).min(1)
}).describe(PARALLEL_DESCRIPTION);
const workflowBuilderForeachEntryInputSchema = zod.z.strictObject({
	type: zod.z.literal("foreach"),
	...containerIdentityInputFields,
	step: executableInnerStepInputSchema,
	opts: zod.z.object({ concurrency: zod.z.number().int().positive().nullish() }).nullish().describe("Optional concurrency control; defaults to 1 (sequential).")
}).describe(FOREACH_DESCRIPTION);
const workflowBuilderSleepEntryInputSchema = zod.z.strictObject({
	type: zod.z.literal("sleep"),
	id: zod.z.string().min(1),
	...entryDisplayInputFields,
	duration: zod.z.number().nonnegative().describe("Milliseconds to wait. Static number only.")
});
const workflowBuilderSleepUntilEntryInputSchema = zod.z.strictObject({
	type: zod.z.literal("sleepUntil"),
	id: zod.z.string().min(1),
	...entryDisplayInputFields,
	date: zod.z.string().min(1).describe("ISO 8601 wall-clock date to wait until. Static string only.")
});
const workflowBuilderConditionalEntryInputSchema = zod.z.strictObject({
	type: zod.z.literal("conditional"),
	...containerIdentityInputFields,
	steps: zod.z.array(executableInnerStepInputSchema).min(1),
	predicates: zod.z.array(workflowBuilderPredicateSchema).min(1).describe("One declarative predicate per branch, aligned by array index with steps. No JS closures.")
}).describe(CONDITIONAL_DESCRIPTION);
const workflowBuilderLoopEntryInputSchema = zod.z.strictObject({
	type: zod.z.literal("loop"),
	...containerIdentityInputFields,
	step: executableInnerStepInputSchema,
	loopType: zod.z.enum(["dowhile", "dountil"]),
	predicate: workflowBuilderPredicateSchema.describe("Declarative predicate — no JS closures.")
}).describe(LOOP_DESCRIPTION);
const workflowBuilderGraphEntrySchema = zod.z.discriminatedUnion("type", [
	workflowBuilderAgentEntrySchema,
	workflowBuilderToolEntrySchema,
	workflowBuilderClassifierEntrySchema,
	workflowBuilderMappingEntrySchema,
	workflowBuilderNestedWorkflowEntrySchema,
	workflowBuilderParallelEntrySchema,
	workflowBuilderForeachEntrySchema,
	workflowBuilderSleepEntrySchema,
	workflowBuilderSleepUntilEntrySchema,
	workflowBuilderConditionalEntrySchema,
	workflowBuilderLoopEntrySchema
]);
const workflowBuilderGraphEntryInputSchema = zod.z.discriminatedUnion("type", [
	workflowBuilderAgentEntryInputSchema,
	workflowBuilderToolEntryInputSchema,
	workflowBuilderClassifierEntryInputSchema,
	workflowBuilderMappingEntryInputSchema,
	workflowBuilderNestedWorkflowEntryInputSchema,
	workflowBuilderParallelEntryInputSchema,
	workflowBuilderForeachEntryInputSchema,
	workflowBuilderSleepEntryInputSchema,
	workflowBuilderSleepUntilEntryInputSchema,
	workflowBuilderConditionalEntryInputSchema,
	workflowBuilderLoopEntryInputSchema
]);
const GRAPH_DESCRIPTION = "The complete ordered top-level graph covering all eleven persisted graph families: agent, tool, classifier, mapping, nested workflow, parallel, foreach, sleep, sleepUntil, conditional, and loop. Every adjacent pair must compose: the previous output shape must satisfy the next input schema — insert a mapping step whenever shapes differ. The workflow result is exactly the final top-level entry output, so add an explicit final mapping whenever that output does not match outputSchema.";
const SCHEDULE_DESCRIPTION = "Optional declarative cron schedule(s) for the workflow. A single config or an array (array entries must each provide a unique stable id). Persisted with the definition and re-registered on every boot.";
const workflowBuilderJsonValueSchema = zod.z.lazy(() => zod.z.union([
	zod.z.null(),
	zod.z.string(),
	zod.z.number().finite(),
	zod.z.boolean(),
	zod.z.array(workflowBuilderJsonValueSchema),
	zod.z.record(zod.z.string(), workflowBuilderJsonValueSchema)
]));
const workflowBuilderScheduleConfigSchema = zod.z.strictObject({
	id: zod.z.string().min(1).optional().describe("Stable schedule id, scoped to the workflow. Required in array form."),
	cron: zod.z.string().min(1).describe("Cron expression (5-, 6-, or 7-part)."),
	timezone: zod.z.string().min(1).optional().describe("Optional IANA timezone."),
	inputData: workflowBuilderJsonValueSchema.optional().describe("Static input data for each scheduled run."),
	initialState: workflowBuilderJsonValueSchema.optional().describe("Static initial state for each scheduled run."),
	requestContext: zod.z.record(zod.z.string(), workflowBuilderJsonValueSchema).optional().describe("Request context for each scheduled run."),
	metadata: zod.z.record(zod.z.string(), workflowBuilderJsonValueSchema).optional().describe("Metadata persisted on the schedule row.")
}).superRefine(({ cron, timezone }, ctx) => {
	try {
		require_schedules.validateCron(cron, timezone);
	} catch (error) {
		const message = error instanceof Error ? error.message : String(error);
		ctx.addIssue({
			code: zod.z.ZodIssueCode.custom,
			message,
			path: message.startsWith("Invalid timezone") ? ["timezone"] : ["cron"]
		});
	}
});
const workflowBuilderScheduleSchema = zod.z.union([workflowBuilderScheduleConfigSchema, zod.z.array(workflowBuilderScheduleConfigSchema)]).describe(SCHEDULE_DESCRIPTION);
const workflowBuilderDefinitionSchema = zod.z.strictObject({
	id: zod.z.string().min(1).describe("Workflow id — kebab-case. Preserve the exact requested workflow ID."),
	description: zod.z.string().optional(),
	metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional(),
	inputSchema: zod.z.unknown().describe("Complete JSON Schema (Draft 2020-12) for the workflow input."),
	outputSchema: zod.z.unknown().describe("Complete JSON Schema (Draft 2020-12) for the workflow output."),
	stateSchema: zod.z.unknown().optional().describe("Optional JSON Schema for persisted workflow state."),
	requestContextSchema: zod.z.unknown().optional().describe("Optional JSON Schema for request context values."),
	graph: zod.z.array(workflowBuilderGraphEntrySchema).min(1).describe(GRAPH_DESCRIPTION),
	schedule: workflowBuilderScheduleSchema.optional()
});
const workflowBuilderDefinitionInputSchema = zod.z.strictObject({
	id: zod.z.string().min(1).describe("Workflow id — kebab-case. Preserve the exact requested workflow ID unless the user renames it."),
	description: zod.z.string().nullish(),
	metadata: zod.z.record(zod.z.string(), zod.z.unknown()).nullish(),
	inputSchema: zod.z.unknown().describe("Complete JSON Schema (Draft 2020-12) for the workflow input."),
	outputSchema: zod.z.unknown().describe("Complete JSON Schema (Draft 2020-12) for the workflow output."),
	stateSchema: zod.z.unknown().nullish().describe("Optional JSON Schema for persisted workflow state."),
	requestContextSchema: zod.z.unknown().nullish().describe("Optional JSON Schema for request context values."),
	graph: zod.z.array(workflowBuilderGraphEntryInputSchema).min(1).describe(GRAPH_DESCRIPTION),
	schedule: workflowBuilderScheduleSchema.nullish()
}).describe("One complete canonical WorkflowDefinition. Submit exactly one complete candidate per attempt — never parallel alternatives. After diagnostics, correct and resubmit the whole definition.");
/** Public HTTP/SDK contract for a persisted workflow definition row. */
const storedWorkflowDefinitionSchema = workflowBuilderDefinitionSchema.extend({
	status: zod.z.enum(["active", "archived"]),
	source: zod.z.literal("storage"),
	authorId: zod.z.string().optional(),
	createdAt: zod.z.union([zod.z.date(), zod.z.string()]),
	updatedAt: zod.z.union([zod.z.date(), zod.z.string()])
});
const listStoredWorkflowsResponseSchema = zod.z.object({
	workflows: zod.z.array(storedWorkflowDefinitionSchema),
	total: zod.z.number()
});
const upsertStoredWorkflowResponseSchema = zod.z.object({
	ok: zod.z.literal(true),
	id: zod.z.string()
});
const deleteStoredWorkflowResponseSchema = zod.z.object({
	success: zod.z.literal(true),
	message: zod.z.string()
});
// src/workflows/builder/agent.ts
/**
* Creates a Workflow Builder agent with the shared composition contract and a
* concrete surface policy for resource discovery and mutation semantics.
*/
function createWorkflowBuilderAgent({ surfaceInstructions, ...config }) {
	return new require_run_registry.Agent({
		...config,
		instructions: `${WORKFLOW_BUILDER_AUTHORING_PLAYBOOK}\n\n${surfaceInstructions}`
	});
}
// src/workflows/builder/index.ts
const WORKFLOW_BUILDER_SUPPORTED_STEP_TYPES = [
	"agent",
	"tool",
	"classifier",
	"mapping",
	"workflow",
	"parallel",
	"foreach",
	"sleep",
	"sleepUntil",
	"conditional",
	"loop"
];
function normalizeJsonValue(value, path, seen) {
	if (value === null || typeof value === "string" || typeof value === "boolean") return value;
	if (typeof value === "number") {
		if (!Number.isFinite(value)) throw new TypeError(`${path} must contain only finite numbers.`);
		return value;
	}
	if (typeof value !== "object") throw new TypeError(`${path} must be JSON-safe.`);
	if (seen.has(value)) throw new TypeError(`${path} must not contain cycles.`);
	seen.add(value);
	try {
		if (Array.isArray(value)) return value.map((item, index) => normalizeJsonValue(item, `${path}.${index}`, seen));
		if (Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) throw new TypeError(`${path} must contain only plain objects.`);
		const normalized = {};
		for (const [key, item] of Object.entries(value)) if (item !== void 0) normalized[key] = normalizeJsonValue(item, `${path}.${key}`, seen);
		return normalized;
	} finally {
		seen.delete(value);
	}
}
const OPTIONAL_ENTRY_KEYS = [
	"description",
	"metadata",
	"outputSchema",
	"options",
	"opts"
];
const OPTIONAL_ID_ENTRY_TYPES = /* @__PURE__ */ new Set([
	"parallel",
	"conditional",
	"foreach",
	"loop"
]);
const OPTIONAL_STEP_OPTION_KEYS = [
	"retries",
	"metadata",
	"maxRetries",
	"providerOptions"
];
const OPTIONAL_FOREACH_OPT_KEYS = ["concurrency"];
function dropNullKeys(target, keys) {
	for (const key of keys) if (target[key] === null) delete target[key];
}
function normalizeEntry(entry) {
	const normalized = normalizeJsonValue(entry, "graph entry", /* @__PURE__ */ new Set());
	dropNullKeys(normalized, OPTIONAL_ENTRY_KEYS);
	if (typeof normalized.type === "string" && OPTIONAL_ID_ENTRY_TYPES.has(normalized.type)) dropNullKeys(normalized, ["id"]);
	if (normalized.options && typeof normalized.options === "object" && !Array.isArray(normalized.options)) {
		dropNullKeys(normalized.options, OPTIONAL_STEP_OPTION_KEYS);
		if (Object.keys(normalized.options).length === 0) delete normalized.options;
	}
	if (normalized.opts && typeof normalized.opts === "object" && !Array.isArray(normalized.opts)) {
		dropNullKeys(normalized.opts, OPTIONAL_FOREACH_OPT_KEYS);
		if (Object.keys(normalized.opts).length === 0) delete normalized.opts;
	}
	if (normalized.type === "mapping" && typeof normalized.mapConfig !== "string" && normalized.mapConfig !== null && normalized.mapConfig !== void 0) normalized.mapConfig = JSON.stringify(normalized.mapConfig);
	if ((normalized.type === "parallel" || normalized.type === "conditional") && Array.isArray(normalized.steps)) normalized.steps = normalized.steps.map((step) => normalizeEntry(step));
	if ((normalized.type === "foreach" || normalized.type === "loop") && normalized.step) normalized.step = normalizeEntry(normalized.step);
	return normalized;
}
function normalizeWorkflowBuilderDefinition(input) {
	const normalized = normalizeJsonValue(input, "workflow definition", /* @__PURE__ */ new Set());
	if (normalized.description === null) delete normalized.description;
	if (normalized.metadata === null) delete normalized.metadata;
	if (normalized.stateSchema === null) delete normalized.stateSchema;
	if (normalized.requestContextSchema === null) delete normalized.requestContextSchema;
	if (normalized.schedule === null) delete normalized.schedule;
	if (!Array.isArray(normalized.graph)) throw new TypeError("Workflow definition graph must be an array.");
	normalized.graph = normalized.graph.map((entry) => normalizeEntry(entry));
	return normalized;
}
Object.defineProperty(exports, "WORKFLOW_BUILDER_AUTHORING_CONSTRAINTS", {
	enumerable: true,
	get: function() {
		return WORKFLOW_BUILDER_AUTHORING_CONSTRAINTS;
	}
});
Object.defineProperty(exports, "WORKFLOW_BUILDER_AUTHORING_PLAYBOOK", {
	enumerable: true,
	get: function() {
		return WORKFLOW_BUILDER_AUTHORING_PLAYBOOK;
	}
});
Object.defineProperty(exports, "WORKFLOW_BUILDER_MAPPING_CONFIG_DESCRIPTION", {
	enumerable: true,
	get: function() {
		return WORKFLOW_BUILDER_MAPPING_CONFIG_DESCRIPTION;
	}
});
Object.defineProperty(exports, "WORKFLOW_BUILDER_SUPPORTED_STEP_TYPES", {
	enumerable: true,
	get: function() {
		return WORKFLOW_BUILDER_SUPPORTED_STEP_TYPES;
	}
});
Object.defineProperty(exports, "compareWorkflowBuilderSchemas", {
	enumerable: true,
	get: function() {
		return compareWorkflowBuilderSchemas;
	}
});
Object.defineProperty(exports, "createWorkflowBuilderAgent", {
	enumerable: true,
	get: function() {
		return createWorkflowBuilderAgent;
	}
});
Object.defineProperty(exports, "deleteStoredWorkflowResponseSchema", {
	enumerable: true,
	get: function() {
		return deleteStoredWorkflowResponseSchema;
	}
});
Object.defineProperty(exports, "inspectWorkflowBuilderSchemas", {
	enumerable: true,
	get: function() {
		return inspectWorkflowBuilderSchemas;
	}
});
Object.defineProperty(exports, "listStoredWorkflowsResponseSchema", {
	enumerable: true,
	get: function() {
		return listStoredWorkflowsResponseSchema;
	}
});
Object.defineProperty(exports, "normalizeWorkflowBuilderDefinition", {
	enumerable: true,
	get: function() {
		return normalizeWorkflowBuilderDefinition;
	}
});
Object.defineProperty(exports, "preflightWorkflowDefinition", {
	enumerable: true,
	get: function() {
		return preflightWorkflowDefinition;
	}
});
Object.defineProperty(exports, "storedWorkflowDefinitionSchema", {
	enumerable: true,
	get: function() {
		return storedWorkflowDefinitionSchema;
	}
});
Object.defineProperty(exports, "upsertStoredWorkflowResponseSchema", {
	enumerable: true,
	get: function() {
		return upsertStoredWorkflowResponseSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderAgentEntryInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderAgentEntryInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderAgentEntrySchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderAgentEntrySchema;
	}
});
Object.defineProperty(exports, "workflowBuilderClassifierEntryInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderClassifierEntryInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderClassifierEntrySchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderClassifierEntrySchema;
	}
});
Object.defineProperty(exports, "workflowBuilderConditionalEntryInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderConditionalEntryInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderConditionalEntrySchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderConditionalEntrySchema;
	}
});
Object.defineProperty(exports, "workflowBuilderDefinitionInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderDefinitionInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderDefinitionSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderDefinitionSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderForeachEntryInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderForeachEntryInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderForeachEntrySchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderForeachEntrySchema;
	}
});
Object.defineProperty(exports, "workflowBuilderGraphEntryInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderGraphEntryInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderGraphEntrySchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderGraphEntrySchema;
	}
});
Object.defineProperty(exports, "workflowBuilderLoopEntryInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderLoopEntryInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderLoopEntrySchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderLoopEntrySchema;
	}
});
Object.defineProperty(exports, "workflowBuilderMappingConfigSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderMappingConfigSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderMappingDescriptorSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderMappingDescriptorSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderMappingEntryInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderMappingEntryInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderMappingEntrySchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderMappingEntrySchema;
	}
});
Object.defineProperty(exports, "workflowBuilderNestedWorkflowEntryInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderNestedWorkflowEntryInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderNestedWorkflowEntrySchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderNestedWorkflowEntrySchema;
	}
});
Object.defineProperty(exports, "workflowBuilderParallelEntryInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderParallelEntryInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderParallelEntrySchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderParallelEntrySchema;
	}
});
Object.defineProperty(exports, "workflowBuilderPredicateSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderPredicateSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderScheduleConfigSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderScheduleConfigSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderScheduleSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderScheduleSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderSleepEntryInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderSleepEntryInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderSleepEntrySchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderSleepEntrySchema;
	}
});
Object.defineProperty(exports, "workflowBuilderSleepUntilEntryInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderSleepUntilEntryInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderSleepUntilEntrySchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderSleepUntilEntrySchema;
	}
});
Object.defineProperty(exports, "workflowBuilderToolEntryInputSchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderToolEntryInputSchema;
	}
});
Object.defineProperty(exports, "workflowBuilderToolEntrySchema", {
	enumerable: true,
	get: function() {
		return workflowBuilderToolEntrySchema;
	}
});

//# sourceMappingURL=builder-BQtzatAh.cjs.map