# Writing a workflow

For whoever writes a client workflow, a person or an agent. Client workflows live in [xenise-workflows](https://github.com/bfish1502/xenise-workflows), not here. The words used are defined in the ontology.

```ts
import { defineWorkflow } from "xenise-platform/workflow";

export default defineWorkflow({
  name: "merge-duplicates",
  trigger: { kind: "schedule", cron: "0 7 * * *" },
  async run(step) {
    const pairs = await step.do("find duplicates", async (tools) => tools.call("attio", "ATTIO_LIST_RECORDS", { object: "people" }));
    const answer = await step.gate("review merges", { question: "Merge these people?", items: /* one per pair */ [] });
    await step.do("apply merges", async (tools) => { /* tools.call("attio", "ATTIO_MERGE_RECORDS", …) for each approved pair */ });
  },
});
```

For a complete, real workflow with its explanation and saved questions, see [crm-daily in xenise-workflows](https://github.com/bfish1502/xenise-workflows/tree/main/workflows/crm-daily). Client workflows and deployable platform smoke workflows belong in that repository.

What the static read needs, and a deploy fails without:

- Step names are plain strings, each used once. No step inside a loop or inside another step: put the loop inside the step.
- Tools are called as `tools.call("connection", "TOOL", args)` with plain strings. An agent step lists its tools in place.
- A model is called only in a model step (`step.model`: one call, no tools, it can't act) or an agent step (`step.agent`: a loop over the tools it lists, at least one). The model is a plain string in the step. Files are handed over by address (`files: [{ url, type }]`) and fetched by the platform; an `output` schema gives back a parsed, checked answer. The outbound Worker holds each call to the deployed model, the operator's file hosts and the client's spending caps: see Model steps and agent steps.
- No `fetch`. All outside calls go through a connection.
- `step` stays in `run`: it can't be passed to a helper or renamed, because steps could hide there.
- The workflow's name is lower-case letters, digits and dashes.
- A step that does several distinct things can name them, `step.do("hygiene", { uses, checks: ["merge exact duplicates", "tidy names"] }, fn)`, and is drawn as a box with a card for each. The names are plain strings.
- A workflow whose explanation has examples or tables exports the functions and constants they point at: `export * as definitions from "./definitions.ts"`.
- The built code must have the same steps and tools as the source, and must not be minified.

A step that hands `tools` to a helper function still deploys. The static read can't follow it, so the explanation may name more tools than the read found, and the runtime trace covers the rest. Better: list them, `step.do("name", { uses: [{ connection, tool }, …] }, fn)`, written in place or as a const at the top of the file. Then the list is the whole truth: the read takes it as the step's tools and the runtime stops a call that isn't on it. A review's apply function always lists its tools this way.

A schedule can name its time zone: `{ kind: "schedule", cron: "0 7 * * *", timezone: "Europe/London" }`. Without one it is UTC.

## The explanation

An explanation is JSON. It says what the workflow does in plain English, and it is what the workflow screen draws: for each step its name, kind, plain English and tools, the definitions it decides by, and for each definition its rules, examples and tables. The format is in [explanation-format.md](/docs/building/explanation-format/), the words in the ontology, and how a builder writes one in [skills/explain-workflow](/docs/building/explain-workflow/).

- **A deploy fails** if the explanation skips or invents a step, gets a kind or the order wrong, or leaves out or invents a tool.
- **A deploy fails** if it skips or invents a check the code names, leaves a step with no definition, or uses a definition or rule that doesn't exist.
- **A deploy fails** if any example gives a different answer when run through the workflow's own code, unless it is marked a known gap. The examples run in `xenise check` and `xenise deploy`, and again in the version's own Worker before it goes live.
- **A deploy fails** if a saved question (`[{ "question": "Does this ever delete a record?", "answer": "no" }]`) now gets a different answer from the explanation.
- **A run raises a flag** if a step calls a tool the explanation doesn't mention.

## Checking and deploying

```sh
xenise check <workflow.ts> --explanation <file>   # the static read, the explanation check, and its examples
xenise deploy <workflow.ts> --explanation <file> [--evals <file>]
```

The MCP server is the same API at `POST /mcp`. Building happens there: an agent calls `check_explanation` until the explanation matches the code, then `deploy_version`.

To test a workflow without Cloudflare, use the stand-ins in [Testing workflows](/docs/building/testing-workflows/).
