# Explain a workflow

You write the workflow's code however you like. This skill is the part the platform checks: `explanation.json`, and the two small things in the code it points at. The format is in `docs/building/explanation-format.md` and the words (step, definition, rule, example, action, table) are in `docs/ontology.md`. Read both first. A complete test fixture is `tests/fixtures/merge-duplicates/`. All three ship with the platform package, at the version the workflows repo pins: in a workflows repo they are under `node_modules/xenise-platform/` (this skill is `node_modules/xenise-platform/skills/explain-workflow/`).

**You write it; the platform only checks it.** Whoever changes a workflow's code writes or updates its explanation in the same change: you have the code, its history and what was asked for, and a v1 explanation often needs a small code change too (a decision pulled out into a function the definitions export). No model writes explanations later. `xenise check` runs in CI and again at deploy; when it fails, its message says what to change and where. Fix that and push again.

The reader of the result is a business owner who doesn't read code. They should be able to say what each step decides by, and predict what it does with a record, from the screen alone.

## Steps

1. **Read the facts.** `xenise check <folder>/workflow.ts` prints the steps, kinds and tools the platform reads from the code. Never write those by hand.
2. **Find the definitions.** Go through each step's code and list every named test it decides by: a function that answers a question about a record ("is this a shared inbox?", "do these two addresses reach the same inbox?"). One definition can serve several steps. Name it in business words, not after the function.
3. **Interpret the code; don't restructure it.** The code is the builder's and can be shaped however they like. Never split, merge or rename a step so that it reads better on the screen: how the work is split for a reader is the explanation's job, and the explanation must stay true to the code it describes. If one step does several distinct jobs in order and the code already names them, describe each as a check: `step.do("hygiene", { uses: HYGIENE, checks: ["merge exact duplicates", "tidy names"] }, fn)`.
4. **Write `definitions.ts`.** Export one function per definition and one constant per table. Import the real function the step calls; export it if it wasn't exported. Where the step's test is an expression over a helper (`emailKey(a) === emailKey(b)`), export a small function that is that expression, and have the step call it too. Then add `export * as definitions from "./definitions.ts"` to `workflow.ts`.
5. **Write the rules and examples.** For each definition, read the function and write one rule per condition in it, each with at least one example: a made-up input and the answer the code gives. Include the cases it rightly rejects (`"matches": false`). If the code has no examples or tests to draw on, invent realistic ones. Never copy a real person's record.
   - Give a rule at most one short line in `says`: the nuance a reader needs ("Ignores capitals, and dots in Gmail addresses"). Leave the mechanics (loops, lookups, keys) out.
   - Where a rule needs several things to hold, list them in `all`, and give each case that fails one of them its own rule with `breaks` naming the condition. Don't also write the conditions out in the rule's name.
   - Write `show` as values separated by " · ", one per column, with `[[ ]]` around the part the rule looks at. Two things being compared are two values, never "… on both". Keep `gives` to a word or two.
6. **Write the actions.** For each step or check, list the definitions it uses, with one line for the card, the outcome (`fix`, `ask`, `leave`, `work-out`) and the result as a short action: "Merged, older record kept", "Rename?", "Left alone". Don't write the outcome or the suggestion into it: put a question's suggested answer in `suggest`. Where one rule leads somewhere different, say so under `rules`.
7. **Word it once.** Title each step by its job ("Fix company names"). Write each definition's `question` as the question a buyer would ask; it is the title of its drawer. Name the definition for the Reference list; the screen doesn't repeat it next to the question. Check that the step title, the question and the action don't say the same thing three times.
8. **Say what each step works on and reads.** Give steps an `about` ("People", "Companies") when they work on different things, so they sit side by side; give every step in that stretch one. Give a step that reads data `reads`, in words ("People · every record, about 1,500"), for each connection it reads from. Set `view` to `table` only if a table reads better than the sequence.
9. **Run it.** `xenise check <folder>/workflow.ts --explanation <folder>/explanation.json` until it reports no problems. It runs every example through the code.

**Writing the words in `prose.json` instead.** If the folder has a `prose.json`, write the words there (a step is a string, or `{ says, title?, decides?, checks?, condition?, about?, reads? }`; `format`, `title`, `view`, `definitions` and `tables` sit at the top) and make `explanation.json` with `xenise explain <folder>/workflow.ts --prose <folder>/prose.json --write <folder>/explanation.json`. Never edit that `explanation.json` by hand: `xenise check … --prose <folder>/prose.json` fails when it isn't what the prose makes.

**Examples about Attio records.** Write the input plainly and let `attioPerson` / `attioCompany` from `xenise-platform/attio` build the record the step code expects: `export const notAFit = (p: PlainPerson) => isNotAFit(attioPerson(p))`, with an example input `[{ "first": "Jo", "emails": ["info@acme.com"] }]`. Don't write your own record builders.

## When an example fails

The code gives a different answer from the one you drew. Decide which is right:

- **Your example was wrong.** Fix the example.
- **The code is wrong and you are fixing it now.** Fix the code.
- **The code is wrong and it isn't yours to fix now.** Keep the example with the answer it should give and mark it `"gap": true`. Say so in your report: a known gap is a finding the owner should hear about.

Never change `expect` to match a wrong answer just to pass.

## Limits to respect

- Examples are compared as JSON, so the exported function must return plain values.
- The platform checks an example against the function you export. It can't check that the step calls that function, so that link is yours to keep true.
- A workflow can't redefine the platform's own definitions (When it runs, Questions, Waits) or mark its own as `platform`.
- Run data never goes in an explanation, and an example is never a record from a run.

## Report

Say how many definitions, rules and examples there are, list every known gap with what should happen, and list anything in the code you had to export or wrap.
