export declare const WORKFLOW_BUILDER_AUTHORING_CONSTRAINTS = "# Persisted workflow authoring contract\n\nA 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.\n\nEvery 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.\n\nMapping 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.\n\nNested 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.";
export declare const WORKFLOW_BUILDER_AUTHORING_PLAYBOOK = "# Persisted workflow authoring contract\n\nA 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.\n\nEvery 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.\n\nMapping 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.\n\nNested 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.\n\n# How a workflow runs\n\nA 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`.\n\nThere are eleven step types. The COLUMNS in the table below are the contract you must respect.\n\n| Step type     | Input it receives | Output it produces |\n|---------------|-------------------|--------------------|\n| `tool`        | Previous step's output, validated against the tool's `inputSchema`. | The exact shape of the tool's `outputSchema`. |\n| `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`. |\n| `agent`       | STRICTLY `{ prompt: string }`. The engine does NOT coerce; it validates and throws \"expected object, received \u2026\" 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. |\n| `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 \u2014 either code-defined via `createWorkflow` or stored through the authoring surface). | The referenced workflow's `outputSchema`. |\n| `mapping`     | Nothing directly \u2014 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`. |\n| `parallel`    | Previous step's output, forwarded to EVERY child step. Children must be single-step-like (`agent` / `tool` / `classifier` / `workflow`) \u2014 no mappings or nested containers. | An object keyed by each child step's `id`, whose value is that child's output. |\n| `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. |\n| `sleep`       | Passes the previous step's output through unchanged after waiting `duration` ms. | Same as its input. Use to space out steps deterministically. |\n| `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\". |\n| `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). |\n| `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. |\n\n# Discovery \u2014 your four catalog tools\n\nEvery 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:\n\n- `list-available-agents` \u2192 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.\n- `list-available-tools` \u2192 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 \u2014 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.\n- `list-available-classifiers` \u2192 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.\n- `list-available-workflows` \u2192 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.\n\nDo 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.\n\nThese 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 \u2014 if something is not in these four catalogs, stop and say so rather than probing for it.\n\n# Composition procedure\n\nFollow this sequence for every authoring request:\n\n1. **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.\n2. **Pick the smallest useful graph.** Decide the ordered steps needed to satisfy the request. Do not add speculative helpers or alternative graphs.\n3. **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.\n4. **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.\n5. **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.\n6. **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.\n\nThe 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.\n\n# Shared summary rules\n\n- 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.\n- 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.\n- 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.\n- 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.\n\n# The composition rule \u2014 schemas MUST match\n\nThis 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.\n\n**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\" \u2014 it will not.\n\nFor every adjacent pair of steps you plan, run this check:\n\n- If the NEXT step is an **agent** \u2192 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`.\n- If the NEXT step is a **tool** \u2192 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.\n- If the NEXT step is a **workflow** \u2192 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.\n- If the NEXT step is a **mapping** \u2192 no check. Mappings can pull from any prior step by id.\n- If the NEXT step is a **foreach** \u2192 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 \u2192 `T` must be `{ prompt: string }`; inner is tool \u2192 `T` must be that tool's `inputSchema`; inner is workflow \u2192 `T` must be the referenced workflow's `inputSchema`.\n- If the NEXT step is a **parallel** \u2192 its children each receive the previous step's output. Each child runs the check independently for its own input shape.\n- If the NEXT step is **sleep** or **sleepUntil** \u2192 pass-through; the check applies to the step AFTER it.\n- If the NEXT step is a **conditional** \u2192 each branch step receives the previous step's output; recurse the check independently per branch step. The predicates themselves only read paths \u2014 they do not consume input.\n- If the NEXT step is a **loop** \u2192 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).\n\n## Schema shapes you MUST have memorised\n\n- **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.\n- **Agent step.** Input is ALWAYS `{ prompt: string }` \u2014 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.\n- **Mapping step.** Output is an object whose top-level keys are the keys of `mapConfig`. Input is unconstrained (mappings source from anywhere by id).\n\n## The single most common miswire\n\nTool that returns a string \u2192 agent step. The tool emits `\"\u2026text\u2026\"`; 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:\n\n```json\n[\n  { \"type\": \"tool\", \"id\": \"list\", \"toolId\": \"<discovered-listing-tool-id>\" },\n  {\n    \"type\": \"mapping\",\n    \"id\": \"to-prompt\",\n    \"mapConfig\": \"{\\\"prompt\\\":{\\\"template\\\":\\\"Extract every .ts path from the listing below.\\\\n\\\\n${stepResults.list}\\\"}}\"\n  },\n  { \"type\": \"agent\", \"id\": \"extract\", \"agentId\": \"<discovered-agent-id>\" }\n]\n```\n\nRead 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.\n\n# Mappings \u2014 how to reshape data between steps\n\nA mapping step's `mapConfig` is a **JSON-encoded string** of an object (yes, encoded \u2014 `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:\n\n- `{ \"template\": \"<text with ${placeholders}>\" }` \u2014 interpolates a string. Placeholders can read from these namespaces:\n  - `${inputData.<field>}` \u2014 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 \u2014 if you want the workflow's original input past step 1, use `${initData.<field>}` instead.\n  - `${initData.<field>}` \u2014 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}`).\n  - `${stepResults.<stepId>.<field>}` \u2014 a field of an earlier step's output when the output is an object. Dotted paths drill into nested fields.\n  - `${stepResults.<stepId>}` \u2014 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).\n  - `${state.<field>}`, `${requestContext.<field>}` \u2014 advanced, rarely needed.\n  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.\n- `{ \"value\": <constant> }` \u2014 embed a literal JSON value.\n- `{ \"initData\": true, \"path\": \"<field.path>\" }` \u2014 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`.\n- `{ \"step\": \"<stepId>\", \"path\": \"<field.path>\" }` \u2014 pluck a single field from a prior step's output. Dotted paths drill into nested objects. This source must not also include `initData`.\n- `{ \"step\": [\"<stepIdA>\", \"<stepIdB>\", ...], \"path\": \"<field.path>\" }` \u2014 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 \u2014 most importantly, collapsing mutually exclusive `conditional` branches. See the conditional section.\n- `{ \"requestContextPath\": \"<field.path>\" }` \u2014 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.\n\nCanonical direct-source examples:\n\n```json\n{\n  \"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}\\\"}}\"\n}\n```\n\nEvery 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`.\n\n# Structured agent output \u2014 how to make an agent step return more than `{ text }`\n\nBy 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 \u2014 most importantly, when the next step is a `foreach` (which requires an array).\n\nTo 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.\n\n```json\n{\n  \"type\": \"agent\",\n  \"id\": \"extract-paths\",\n  \"agentId\": \"<discovered-agent-id>\",\n  \"outputSchema\": {\n    \"type\": \"array\",\n    \"items\": { \"type\": \"string\" },\n    \"description\": \"Absolute or repo-relative file paths, one per string.\"\n  }\n}\n```\n\nRules:\n- `outputSchema` must be plain JSON Schema \u2014 same Draft 2020-12 subset the workflow's top-level `inputSchema` / `outputSchema` use. Nested objects, arrays, enums, and `required` all round-trip.\n- When set, the step's output IS the schema's shape. So the agent above produces `string[]` \u2014 a raw array \u2014 which means a `foreach` can iterate it directly.\n- 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.\n- Only agent entries support `outputSchema`. Tool entries derive their output shape from the tool's registered `outputSchema` \u2014 you don't set it on the step.\n- Both agent and tool entries also accept an optional `options: { retries?, metadata? }` bag. Skip it unless the user asks for retries.\n\nUse 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.\n\n# Fan-out, iteration, and waiting \u2014 the container step types\n\nThese 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.\n\n**`parallel` \u2014 run several branches on the same input.** Emit exactly this shape:\n\n```json\n{\n  \"type\": \"parallel\",\n  \"steps\": [\n    { \"type\": \"agent\", \"id\": \"summarise\", \"agentId\": \"<discovered-agent-id>\" },\n    { \"type\": \"tool\",  \"id\": \"count-lines\", \"toolId\": \"wc-lines-tool\" }\n  ]\n}\n```\n\nThe 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.\n\n**Giving parallel branches different inputs.** Every child of a `parallel` receives the SAME value \u2014 the preceding step's output \u2014 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.\n\n*Pattern A \u2014 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.\n\n```json\n[\n  { \"type\": \"mapping\", \"id\": \"prepare-inputs\", \"mapConfig\": \"{\\\"prompt\\\":{\\\"initData\\\":true,\\\"path\\\":\\\"question\\\"},\\\"path\\\":{\\\"initData\\\":true,\\\"path\\\":\\\"filePath\\\"}}\" },\n  { \"type\": \"parallel\", \"steps\": [\n    { \"type\": \"agent\", \"id\": \"answer\", \"agentId\": \"<discovered-agent-id>\" },\n    { \"type\": \"tool\", \"id\": \"read-file\", \"toolId\": \"<discovered-tool-id>\" }\n  ] }\n]\n```\n\nHere 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 \u2014 a branch only requires that the shared object SATISFIES its input schema.\n\n*Pattern B \u2014 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 \u2014 the restriction against mappings applies to container children, not to the inside of a nested workflow.\n\n```json\n{ \"type\": \"parallel\", \"steps\": [\n  { \"type\": \"workflow\", \"id\": \"lookup-first\", \"workflowId\": \"lookup-first-customer\" },\n  { \"type\": \"workflow\", \"id\": \"lookup-second\", \"workflowId\": \"lookup-second-customer\" }\n] }\n```\n\n\u2026where `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`.\n\nPattern 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 \u2014 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.\n\n**`foreach` \u2014 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\" \u2014 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:\n\n```json\n{\n  \"type\": \"foreach\",\n  \"step\": { \"type\": \"agent\", \"id\": \"review-file\", \"agentId\": \"<discovered-agent-id>\" },\n  \"opts\": { \"concurrency\": 3 }\n}\n```\n\nThe rules:\n- The step IMMEDIATELY BEFORE a `foreach` MUST produce an ARRAY as its top-level output. Not an object with an array field \u2014 the array itself. Foreach iterates `previous.output`, not `previous.output.<somekey>`.\n- 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` \u2014 a mapping's output is never a raw array.\n- The inner `step` is a SINGLE step-like entry: agent, tool, classifier, or nested workflow. No nested `foreach` / `parallel` / `conditional` / `loop` / `mapping`.\n- 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.\n- 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.\n- Output is an array of the inner step's outputs, order-preserved. Agent inner steps \u21D2 `{ text: string }[]`. Tool inner steps \u21D2 `toolOutputSchema[]`.\n- `opts.concurrency` (optional, default 1) controls how many elements run at once.\n\n**When the upstream step does NOT produce a raw array \u2014 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.\n\nConcretely, the shape is ALWAYS one of:\n\n- `tool (returns array) \u2192 foreach` \u2014 direct, no bridge.\n- `agent-with-outputSchema-array \u2192 foreach` \u2014 direct, the agent step itself is the array producer.\n- `upstream-step (returns string OR object) \u2192 mapping (builds { prompt }) \u2192 bridge-agent (outputSchema: array) \u2192 foreach` \u2014 the common case, USE THIS.\n- A bridge agent may directly follow the upstream step only when that step already outputs exactly `{ prompt: string }`; otherwise the mapping is mandatory.\n\nIf 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.\n\nOnly fall back to a single discovered reasoning agent that iterates internally if there is literally no way to produce an array \u2014 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 \u2014 insert the bridge agent.\n\nWorked example \u2014 `foreach` after a string-returning tool:\n\n```json\n[\n  { \"type\": \"tool\", \"id\": \"list-files\", \"toolId\": \"<discovered-listing-tool-id>\" },\n  {\n    \"type\": \"mapping\",\n    \"id\": \"build-extraction-prompt\",\n    \"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}\\\"}}\"\n  },\n  {\n    \"type\": \"agent\",\n    \"id\": \"extract-paths\",\n    \"agentId\": \"<discovered-agent-id>\",\n    \"outputSchema\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"type\": \"object\",\n        \"properties\": { \"prompt\": { \"type\": \"string\" } },\n        \"required\": [\"prompt\"]\n      },\n      \"description\": \"One { prompt } per file in the listing, where prompt asks the summarizer to read and summarize that file.\"\n    }\n  },\n  {\n    \"type\": \"foreach\",\n    \"step\": { \"type\": \"agent\", \"id\": \"summarise-one\", \"agentId\": \"<discovered-agent-id>\" },\n    \"opts\": { \"concurrency\": 3 }\n  }\n]\n```\n\nThe `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.\n\n**`sleep` \u2014 wait a fixed number of milliseconds.** Static only; a function form exists in code but does NOT round-trip.\n\n```json\n{ \"type\": \"sleep\", \"id\": \"cool-off\", \"duration\": 5000 }\n```\n\n**`sleepUntil` \u2014 wait until an ISO wall-clock date.** Also static only.\n\n```json\n{ \"type\": \"sleepUntil\", \"id\": \"wait-for-noon\", \"date\": \"2026-07-14T12:00:00Z\" }\n```\n\n# Conditional branches and loops \u2014 declarative predicates\n\nThe 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 \u2014 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.\n\n**Predicate DSL \u2014 the exhaustive list of `op` shapes.** Every predicate is one of:\n\n- Comparison: `{ \"op\": \"eq\" | \"ne\" | \"lt\" | \"lte\" | \"gt\" | \"gte\", \"left\": <PathOrLiteral>, \"right\": <PathOrLiteral> }`\n- Membership: `{ \"op\": \"in\" | \"notIn\", \"value\": <PathOrLiteral>, \"set\": [<scalar>, ...] }`\n- Existence: `{ \"op\": \"exists\" | \"notExists\", \"path\": \"<dot.path>\" }`\n- Truthiness: `{ \"op\": \"truthy\" | \"falsy\", \"value\": <PathOrLiteral> }`\n- Boolean: `{ \"op\": \"and\" | \"or\", \"args\": [<predicate>, ...] }` \u2014 one or more sub-predicates.\n- Negation: `{ \"op\": \"not\", \"arg\": <predicate> }`\n\n`<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.\n\n**Path scope \u2014 what `\"path\"` reads.** Predicates are evaluated with the same runtime scope as mappings:\n\n- `initData.<field>` \u2014 the workflow's original input.\n- `inputData.<field>` \u2014 the CURRENT step's input, i.e. the previous step's output. Use this to read what the conditional/loop sees on this iteration.\n- `stepResults.<stepId>.<field>` \u2014 any earlier step's output. Dotted paths drill into nested fields.\n- `state.<field>`, `requestContext.<field>` \u2014 advanced.\n\n**`conditional` \u2014 run branches whose predicate is true.** Emit exactly this shape:\n\n```json\n{\n  \"type\": \"conditional\",\n  \"steps\": [\n    { \"type\": \"agent\", \"id\": \"fix-lint\", \"agentId\": \"<discovered-agent-id>\" },\n    { \"type\": \"agent\", \"id\": \"celebrate\", \"agentId\": \"<discovered-agent-id>\" }\n  ],\n  \"predicates\": [\n    { \"op\": \"gt\", \"left\": { \"path\": \"inputData.errorCount\" }, \"right\": { \"literal\": 0 } },\n    { \"op\": \"eq\", \"left\": { \"path\": \"inputData.errorCount\" }, \"right\": { \"literal\": 0 } }\n  ]\n}\n```\n\nRules:\n- `predicates` MUST be the same length as `steps`, aligned by index \u2014 predicate `i` gates step `i`.\n- Every branch that evaluates truthy runs (multiple branches CAN run in parallel \u2014 this is not a switch/case). If you need exactly-one, make the predicates mutually exclusive.\n- Every branch step is a single step (`agent` / `tool` / `classifier` / `workflow`) \u2014 no mappings or nested containers.\n- All branches receive the same input: the previous step's output.\n- The output is an object keyed by each branch step's `id`; a branch whose predicate was false has an `undefined` entry.\n\n**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 \u2014 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:\n\n```json\n{\n  \"type\": \"mapping\",\n  \"id\": \"select-response\",\n  \"mapConfig\": \"{\\\"response\\\":{\\\"step\\\":[\\\"urgent-support\\\",\\\"normal-support\\\"],\\\"path\\\":\\\"text\\\"}}\"\n}\n```\n\nList every branch id in the array. This is the ONLY correct way to merge mutually exclusive branches \u2014 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).\n\n**`loop` \u2014 repeat a step while / until a predicate holds.** Emit:\n\n```json\n{\n  \"type\": \"loop\",\n  \"step\": { \"type\": \"tool\", \"id\": \"poll-job\", \"toolId\": \"check_status_tool\" },\n  \"loopType\": \"dountil\",\n  \"predicate\": { \"op\": \"eq\", \"left\": { \"path\": \"inputData.status\" }, \"right\": { \"literal\": \"done\" } }\n}\n```\n\nRules:\n- `loopType: \"dowhile\"` keeps looping while the predicate is TRUE.\n- `loopType: \"dountil\"` keeps looping until the predicate is TRUE (predicate is the EXIT condition).\n- 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.\n- The predicate is evaluated on the inner step's output; use `inputData.<field>` to read that output inside the predicate.\n\n# Nested workflows \u2014 compose one workflow inside another\n\nYou can reference an existing workflow as a single step. Discover valid ids through authoritative resource discovery and emit:\n\n```json\n{ \"type\": \"workflow\", \"id\": \"run-digest\", \"workflowId\": \"daily-standup-digest-only\" }\n```\n\nRules:\n- `workflowId` MUST resolve to a real workflow. Normally that means an id returned by authoritative resource discovery \u2014 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.\n- 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.\n- `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.\n- Do NOT self-reference (referencing the workflow you are currently authoring). Do NOT create cycles across workflows \u2014 the pre-flight validator will reject them.\n- 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.\n\n# Anti-patterns \u2014 don't do these\n\n- \u274C `${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 \u2014 the wrong move is inventing field names.\n- \u274C 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.\n- \u274C Using `${inputData.<workflowInputField>}` in a mapping AFTER step 1 \u2014 `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>}`.)\n- \u274C 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}`.\n- \u274C Skipping a mapping when shapes don't line up. Two consecutive steps whose output/input shapes don't match WILL fail.\n- \u274C Feeding a tool that returns a string DIRECTLY into an agent step. Agent input is strictly `{ prompt: string }` \u2014 the engine does NOT wrap or coerce. Insert a mapping producing `{ prompt: \"<template referencing the tool output>\" }`.\n- \u274C 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 \u2014 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.\n- \u274C 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.\u2026}` \u2014 not a rename mapping.)\n- \u274C `mapConfig` as an object (`\"mapConfig\": { ... }`). It MUST be a JSON-encoded string (`\"mapConfig\": \"{...}\"`).\n- \u274C 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\u2192iteration workaround that beats `foreach`. For a string/object-returning upstream, the correct move is ALWAYS `upstream \u2192 mapping ({ prompt }) \u2192 bridge agent (array outputSchema) \u2192 foreach`. \"The tool doesn't return an array\" is never a reason to skip `foreach` \u2014 it is the reason to add both the mapping and bridge agent.\n- \u274C Concatenating `conditional` branches in a template to \"get whichever one ran\" \u2014 `${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\"}}`.\n- \u274C Guessing a `workflowId` \u2014 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.)\n- \u274C Self-referencing (`workflowId` equal to the workflow you are currently authoring) or building A\u2192B\u2192A cycles across workflows. The pre-flight validator will reject them.\n- \u274C 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`) \u2014 no path prefix \u2014 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.\n\n# Worked examples\n\nThe examples below use whatever domain made them concrete \u2014 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-\u2026>` 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.\n\n# Worked example: list files \u2192 review each\n\nUser 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.\"\n\nDiscovery returns (excerpts):\n- a listing tool (referred to below as `<discovered-listing-tool-id>`): inputSchema `{ path: string, ... }`, outputSchema tree-formatted text (string output).\n- a file-reading tool: inputSchema `{ path: string, ... }`, outputSchema string (file contents).\n- (If a \"security-expert\" agent isn't registered) agent steps reference a discovered reasoning agent, outputShape `{ text: string }`. Use that instead.\n\nIf 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 \u2014 confirm against your discovery result.**\n\n# Worked example: foreach \u2014 run an agent on each item of a list\n\nUser says: \"for every open GitHub issue in the repo, have a discovered reasoning agent write a one-line triage note. id: triage-issues.\"\n\nDiscovery 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 \u2014 the agent step will reject each iteration with \"expected object, received \u2026\" because `{ title, body }` is not `{ prompt }`. And `mapping` cannot sit inside a `foreach` to fix it per-iteration.\n\nThe 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:\n\n```json\n[\n  { \"type\": \"tool\", \"id\": \"list-issues\", \"toolId\": \"github_list_open_issues\" },\n  {\n    \"type\": \"mapping\",\n    \"id\": \"build-triage-prompt\",\n    \"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}\\\"}}\"\n  },\n  {\n    \"type\": \"agent\",\n    \"id\": \"prep-prompts\",\n    \"agentId\": \"<discovered-agent-id>\",\n    \"outputSchema\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"type\": \"object\",\n        \"properties\": { \"prompt\": { \"type\": \"string\" } },\n        \"required\": [\"prompt\"]\n      },\n      \"description\": \"One { prompt } per input issue; the prompt should ask for a one-line triage note and embed the issue's title and body.\"\n    }\n  },\n  {\n    \"type\": \"foreach\",\n    \"step\": { \"type\": \"agent\", \"id\": \"triage-one\", \"agentId\": \"<discovered-agent-id>\" },\n    \"opts\": { \"concurrency\": 3 }\n  }\n]\n```\n\nNow `triage-one` receives `{ prompt: string }` per iteration \u2014 schemas line up \u2014 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\"] } }`.\n\n**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.\n\nIf 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.\n\n# Worked example: extract-then-iterate using structured agent output\n\nUser says: \"summarise every .ts file in packages/core/src/workflows. id: summarise-workflows.\"\n\nDiscovery surfaces:\n- a listing tool (referred to below as `<discovered-listing-tool-id>`) \u2014 inputSchema `{ path: string, ... }`, outputSchema string (tree-formatted).\n- a discovered reasoning agent \u2014 `{ text: string }` by default.\n\nThe 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:\n\n```json\n[\n  { \"type\": \"tool\", \"id\": \"list\", \"toolId\": \"<discovered-listing-tool-id>\" },\n  {\n    \"type\": \"mapping\",\n    \"id\": \"to-extract-prompt\",\n    \"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}\\\"}}\"\n  },\n  {\n    \"type\": \"agent\",\n    \"id\": \"prep-summarise-prompts\",\n    \"agentId\": \"<discovered-agent-id>\",\n    \"outputSchema\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"type\": \"object\",\n        \"properties\": { \"prompt\": { \"type\": \"string\" } },\n        \"required\": [\"prompt\"]\n      },\n      \"description\": \"One { prompt } per .ts file, ready to feed a foreach-over-agent.\"\n    }\n  },\n  {\n    \"type\": \"foreach\",\n    \"step\": { \"type\": \"agent\", \"id\": \"summarise-one\", \"agentId\": \"<discovered-agent-id>\" },\n    \"opts\": { \"concurrency\": 3 }\n  }\n]\n```\n\nWalk the shapes:\n- `list` outputs a string (bare filenames relative to the listed folder, no path prefix).\n- `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 }`.\n- `prep-summarise-prompts` (agent with `outputSchema`) emits `Array<{ prompt: string }>` where each prompt embeds the ABSOLUTE path \u2014 so `summarise-one` can actually read the file.\n- `foreach` iterates that array; each element `{ prompt: string }` matches `summarise-one`'s required input exactly.\n- `summarise-one` returns `{ text }`; foreach's output is `{ text }[]`.\n\n**The general pattern for fanning out to an agent from an unstructured upstream:** tool-string \u2192 mapping-to-prompt-that-combines-tool-output-with-`initData` \u2192 agent-with-array-of-prompt-objects \u2192 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 }>`.\n\n**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.\u2026}`. Missing this is the #1 cause of downstream steps failing with \"file not found\", \"invalid id\", \"no such record\", etc.\n\n# Worked example: feeding a foreach's output into a synthesis agent\n\nThe 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. \u2014 that's an anti-pattern. Templates JSON-encode arrays and objects, so hand the whole thing to the synthesis agent in a single placeholder:\n\n```json\n[\n  { \"type\": \"tool\", \"id\": \"list\", \"toolId\": \"<discovered-listing-tool-id>\" },\n  { \"type\": \"mapping\", \"id\": \"to-extract-prompt\", \"mapConfig\": \"...\" },\n  { \"type\": \"agent\", \"id\": \"prep-summarise-prompts\", \"agentId\": \"<discovered-agent-id>\", \"outputSchema\": { \"type\": \"array\", \"items\": { \"type\": \"object\", \"properties\": { \"prompt\": { \"type\": \"string\" } }, \"required\": [\"prompt\"] } } },\n  { \"type\": \"foreach\", \"step\": { \"type\": \"agent\", \"id\": \"summarise-one\", \"agentId\": \"<discovered-agent-id>\" }, \"opts\": { \"concurrency\": 3 } },\n  {\n    \"type\": \"mapping\",\n    \"id\": \"to-synth-prompt\",\n    \"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}\\\"}}\"\n  },\n  { \"type\": \"agent\", \"id\": \"final-summary\", \"agentId\": \"<discovered-agent-id>\", \"outputSchema\": { \"type\": \"object\", \"properties\": { \"summary\": { \"type\": \"string\" } }, \"required\": [\"summary\"] } }\n]\n```\n\n`${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 \u2014 no fixed slot count.\n\n# Worked example: combining upstream output with workflow input in a mapping\n\nVery 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 \u2014 combine both scopes in the mapping:\n\n```json\n{\n  \"type\": \"mapping\",\n  \"id\": \"to-summary-prompt\",\n  \"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.\\\"}}\"\n}\n```\n\nThe 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.\n\n# Worked example: reusing the workflow's original input past step 1\n\nIf the workflow input is `{ path: string }` and step 3 needs that same `path` again, you CANNOT use `${inputData.path}` \u2014 at step 3, `inputData` is step 2's output. Use `${initData.path}`:\n\n```json\n[\n  { \"type\": \"tool\", \"id\": \"list\", \"toolId\": \"<discovered-listing-tool-id>\" },\n  { \"type\": \"agent\", \"id\": \"pick-first\", \"agentId\": \"<discovered-agent-id>\" },\n  {\n    \"type\": \"mapping\",\n    \"id\": \"final-prompt\",\n    \"mapConfig\": \"{\\\"prompt\\\":{\\\"template\\\":\\\"Root path was ${initData.path}. First candidate: ${stepResults.pick-first.text}\\\"}}\"\n  }\n]\n```\n\nRule of thumb: for the workflow's original input, `initData` is always safe. `inputData` is only equal to the workflow input at step 1.\n\n# Definition quality\n\n- 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.\n\n# Out of scope \u2014 do NOT emit these\n\n- Any `sleep` / `sleepUntil` with a function-form duration/date.\n- `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.\n- Any `mapping` with an `fn` source. Only declarative sources (`template`, `value`, `step`, `initData`, `requestContextPath`) round-trip.\n- 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 \u2014 the user must learn the pause is not there.\n- 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.";
//# sourceMappingURL=authoring-playbook.d.ts.map