Routines (YAML)
A routine (a PBScript, or rutina) is a reusable automation promoted to a first-class resource. It's the same automation graph you'd embed inline in a workflow — a start stage and a list of stages[] — but with its own code, display, and declared inputs, living in its own collection. Once a routine exists, any bot, SLA, or schedule can invoke it from a stage by referencing its code. Think of routines as the shared library layer of the automation model.
The shape of a routine
kind: Routine
code: rutina_saludo_simple # upsert key — lowercase, immutable
display: "Rutina de saludo"
body:
start: send
stages:
- key: send
name: PBMessage # a bot type from the catalog
version: "2.2.0" # pinned: the default version never takes ERROR
data:
content: "Hola desde una rutina"
contentType: "text/plain"
sentBy: "6a000000000000000000beef" # the user shown as the sender
channelIds:
- "6a000000000000000000abcd"
next:
SUCCESS: ""
ERROR: notify_failure # see "Give every stage that can fail an error path"
- key: notify_failure
name: PBMessage
version: "2.2.0"
data:
content: "La rutina de saludo no pudo enviar su mensaje."
contentType: "text/plain"
sentBy: "6a000000000000000000beef"
channelIds:
- "6a000000000000000000fa11" # a channel the team watches
next:
SUCCESS: ""
ERROR: "" # the handling stage ends the run either way
| Field | Required | Notes |
|---|---|---|
kind | Yes | Always Routine |
code | Yes | Upsert key. Lowercase/underscores/digits, 3–50 chars. Immutable after creation |
display | Yes | Human-readable label (mutable) |
description | No | Free text |
type | No | normal (default), network, or flowcontrol |
isActive | No | Defaults to true on create; an update that omits it keeps the stored value. Soft-delete by re-applying the export with isActive: false |
dataType | No | Declared inputs the routine accepts (see below) |
body | Yes | The automation graph — same shape as bots embedded in workflows |
Declared inputs: dataType
A routine can declare the inputs it expects. Each entry names a key (referenced inside the graph as $INPUT#<key>), a display, and whether it's required:
dataType:
- key: monto
display: "Monto a evaluar"
required: true
- key: umbral
display: "Umbral de riesgo"
required: false
These are documentation-and-contract only — cotctl doesn't check that the graph actually consumes them, and it won't resolve $INPUT#... expressions for you. They fail at runtime if misspelled, not at apply time.
On an update, each input is matched with the stored input of the same key, wherever you place it, and keeps the fields it omits — an input that omits required keeps its stored value. An input with no stored match is sent with required: false when it omits it. dataType: [] removes every input.
What happens to an input with no stored match, or one that moved, depends on the route the update takes (see How an update reaches the server). The keyed route, which servers that have it use since 0.16.0, replaces dataType whole: such an input is written exactly as the update sends it, keeps nothing of the stored input at its position, and draws no warning. Through PATCH /api/v2/pbscripts/:id the server updates the list by position, so the input is written over the stored input at its position and keeps any other key it omits — its description or type — and apply warns on stderr which, in a --dry-run as well. Write those keys — empty where needed — to replace them.
The automation graph: body
body is a multi-stage builder — a directed graph the runtime walks one stage at a time, branching on the transition each stage emits (SUCCESS, ERROR, and type-specific branches like TRUE/FALSE). Every stage has:
key— unique within the body.name— the bot type (see Bot types for the catalog:PB*messaging/tasks,FC*flow control,NW*HTTP,PBScript, …).version— optional. On create, omit it to take the type's default; pin it (quoted!) when the type has no default, or when its default never reports a failure onERROR(see below). On an update, an omittedversionkeeps the stored stage's version, andversion: nullsends it back to the type's default.data— an opaque payload passed straight through.cotctldoes not rewrite ObjectIds or resolve COTLang expressions ($INPUT#,$VALUE#,$OUTPUT#) inside it — provide them exactly as the runtime expects.next— a map from branch name to the next stage'skey. The empty string""is a valid terminal branch.
body.start names the entry stage. body.maxIterations (default 100) caps how many transitions run — a safety net against loops.
To edit one stage without restating the whole body, apply a document with partial: true (0.14.0+) that names the routine's code and only that stage, by key — see Partial documents. A partial routine that declares dataType is refused. Since 0.16.0 cotctl completes a partial document with the stored stages before sending it, so it takes the same route as any update, with the complete body.stages, and the refusals, the script gate and the unreachable-stage warning apply before either route is tried.
Give every stage that can fail an error path
"" is a valid terminal branch, and that is the trap: a stage that fails onto ERROR: "" ends the run there, and the run is still logged as executed, so cotctl logs list --op failed does not find it. Point the ERROR of every stage that can fail — an email, a message, a request, a write the backend can refuse — at a handler stage someone will see, such as the pinned PBMessage that posts to a watched channel in the example above. The handler is the one stage that keeps ERROR: "". Not every default reports there, though: a default PBCreateTask routes CREATED / NOT-CREATED, and a request the backend refuses stops the run as UNHANDLED_ERROR without taking any branch.
The default PBEmail and PBMessage never take that branch: they return DEFAULT whether the send worked or not (the default PBEmail only records status: false in its result). Pin PBEmail "3.0.1" and PBMessage "2.2.0", and know that pinning changes two contracts cotctl does not check. Their next takes only SUCCESS and ERROR, so rename DEFAULT to SUCCESS — a stage that keeps DEFAULT never sends. And they validate data strictly, refusing any key they don't list: PBEmail needs subject, an object content and targets as email addresses; PBMessage needs content, contentType, a sentBy ObjectId and a non-empty channelIds list of ObjectIds. A {{…}} placeholder is neither: data is not a Mustache template.
The same renaming applies to any stage you pin to a version that reports on ERROR — PBCreateTask "2.0.1" and PBChangeState "2.1.0" among them: DEFAULT and CREATED become SUCCESS, and NOT-CREATED becomes ERROR. A PBCreateTask stage holding both NOT-CREATED and ERROR can keep only one of the two handlers. The pinned versions check the keys before the bot runs and refuse any other, so a stage that keeps an old key never runs its bot — and cotctl validate does not catch it.
Prove both branches before production. Apply the routine to a testing company and run cotctl routines test twice — once as it is, once with the stage made to fail by a variant you apply only for that test (PBEmail 3.0.1 refuses a malformed address in targets, and PBMessage 2.2.0 a channelIds entry that is not an ObjectId, before sending anything) — then read each run with cotctl logs:
cotctl logs list --owner TEST --since 1h # a test run carries no Routine code
cotctl logs get <logId> --parts results --stdout # each stage's status, in order
The failing run must show the stage with status: ERROR and the handler stage after it, and the handler's message must have arrived.
Invoking another routine
A stage with name: PBScript invokes a standalone routine by code — and a routine may invoke itself (a retry-with-backoff pattern) or form a cycle with another:
body:
start: try
maxIterations: 5 # caps the retries
stages:
- key: try
name: PBScript
data:
code: rutina_calcular_riesgo # must be a real routine code
data: {} # its input, one key per dataType entry — {} when none
next:
SUCCESS: ""
ERROR: retry # taken only when the routine returns no result
- key: retry
name: FCSleep
data:
milliseconds: 30000 # FCSleep waits, then takes DEFAULT; it has no ERROR
next:
DEFAULT: try
A PBScript stage takes ERROR only when the invoked routine returns no result. It does not see a failure inside that routine: a routine that ends on one of its own ERROR: "" branches still returns results, so the stage takes SUCCESS, and a crash inside it stops the run as UNHANDLED_ERROR without taking any branch. Give the invoked routine its own error paths.
cotctl validates every PBScript stage's data.code against the routines registered in the profile, so a typo fails at apply time with a "did you mean…?" suggestion. The invoked routine's input goes under data.data — required since 0.14.0, {} when the routine takes none; see Required data entries.
Self-references validate clean on the second apply. A brand-new routine that references its own code isn't in the catalog yet the first time you apply it, so you'll get a warning on that first pass. Apply again and it validates cleanly — the routine now exists. cotctl never blocks the create, so this is just noise on the first run.
Working with routines
# Read
cotctl routines list # active, company-scoped (default)
cotctl routines list --all # include inactive
cotctl routines list --search "riesgo"
cotctl routines list --include-global # include admin-only globals
cotctl routines get rutina_saludo_simple
cotctl routines export rutina_saludo_simple -o rutina.yaml
# Write
cotctl routines apply -f rutina.yaml --dry-run
cotctl routines apply -f rutina.yaml --dry-run --json # one JSON line per routine, with the server's ops
cotctl routines apply -f rutina.yaml -y
# Run for real
cotctl routines test rutina_saludo_simple --context ctx.json
apply takes -f/--file (required), --dry-run, -y/--yes, -q/--quiet, --allow-script-bots (required when the body has a PBScript, CCJS or ESMCode stage) and — new in 0.16.0 — --json, which prints one JSON object per routine on stdout, errors included, as schedules apply --json does. It handles multi-document files.
get, export and test look up exactly the code they are given, and report a routine whose code is not that one as not found. (Before 0.15.0 a code containing #, ?, /, \ or % reached the request URL unencoded and could resolve another routine: routines test 'daily#x' -y ran daily.)
Since 0.14.0, reading a routine by its code needs admin-pbscripts-read. cotctl now reads it through the endpoint the webclient uses, which checks that permission — so routines get, routines export, routines test, and routines apply or apply --dir of a Routine without id that already exists fail with API Error 403 (exit 1) for a profile without it. It's the permission routines list already needed; writing also needs admin-pbscripts-write.
routines test runs the routine for real. There is no simulation mode: test executes the routine immediately with all its side effects (it posts messages, creates tasks, writes properties, fires webhooks). It always prints a warning banner, and without -y it makes you retype the routine's code to confirm. --context <file> supplies the input JSON; --dry-run prints the payload that would be sent without calling the backend. Reserve -y for CI and scripts. Inspect the run afterward with cotctl schedules logs <TEST_...> — the backend records each test as a schedule — or download its log with cotctl logs pull --schedule <TEST_...>.
Applying a routine is a live deploy. Routines aren't versioned — re-applying mutates the live document in place, and a task currently executing it picks up the new behaviour on its next stage transition. Treat cotctl routines apply against production with the same care as shipping code.
Immutability and updates
code is immutable and identifies the routine — there's no in-place rename. On update, cotctl sends only the fields the backend allows to change (display, description, type, isActive, dataType, and body) — and of those, only what differs from the stored routine. A key you omit keeps its stored value, inside body too: each stage is paired with the stored stage of the same key. But a declared body.stages is the complete list, so a stage it leaves out is removed.
That is why a routine is retired from its export, never from a stub. The schema requires display and a full body, so a YAML can't carry isActive: false alone, and a stub with a placeholder stage would replace the routine's real stages. Export it, set isActive: false, and apply it back:
cotctl routines export rutina_obsoleta -c acme -o rutina_obsoleta.yaml
# edit rutina_obsoleta.yaml: isActive: false
cotctl routines apply -f rutina_obsoleta.yaml -c acme --dry-run
cotctl routines apply -f rutina_obsoleta.yaml -c acme -y
How an update reaches the server
New in 0.16.0. routines apply and apply --dir send an update through the server's keyed route, PATCH /api/v2/pbscripts with the code in the body, wherever the server has it, and through PATCH /api/v2/pbscripts/:id otherwise. The routine ends up the same either way, except for the dataType inputs described above.
- Fallback. The first update is the attempt: a server without the keyed route answers it with an HTML 404, which writes nothing, and
cotctlsends the same update throughPATCH /api/v2/pbscripts/:id. That costs one attempt per run, remembered per API URL; nothing is kept on disk. A JSON error from the keyed route —ENTITY_NOT_FOUNDincluded — fails without trying the other route. - Reordering stages. The keyed route keeps the stored order of
body.stages, so a YAML that reorders them is sent throughPATCH /api/v2/pbscripts/:id, the only route that writes an order, with a warning saying so. --dry-runasks the server. Where the keyed route exists, the update is sent with?dryRun=true, which validates it without saving anything. A profile without write permission, and a server without the route, getcotctl's own preview as before.routines applydry-runs every document before it writes, and so doesapply --dirwithout-y, so there an update reaches the server twice.- The server's
opscheck the diff.cotctlwarns when the keyed route announces an operation the local diff did not predict — a warning-qdoes not silence — and notes a predicted change the server does not announce. An answer with no operation saved nothing, so the routine is reportedunchanged(no-opin a dry run). The route's own warnings print too — a violation the stored routine already had, adataTypethat repeats akey— and its errors list the fields it refuses or the violations an update would introduce. With--json, a result the keyed route answered carries itsopsexactly as the server sent them (so does the Routine line ofapply --dir --json), apartial: trueupdate carries the stages it keeps aspreservedElements, and an error the route answered keeps itscodeanddatain the line'serrorobject.
A stored routine whose stages repeat a key, or lack one, can't be updated by key. The keyed route refuses such an update with a 409 STORED_KEY_CONFLICT, in a dry run too, and writes nothing — and cotctl does not retry it through the other route. It stops before sending, naming the routine, the stored stage by its index and the repeated key, and exits 1:
Error: rt_x: Routine "rt_x" cannot be updated by key (STORED_KEY_CONFLICT): the stored stage at body.stages[2] repeats the key "request" of an earlier stage, so it cannot be paired with a stage of the YAML. The update wrote nothing.
Fix the stored Routine, then re-run: give that stage a unique key in the webclient's Routine editor or through PATCH /api/v2/pbscripts/:id.
Until cotctl knows the route is there, it asks with a dry run first. Any other JSON error to that dry run fails as the route sent it; a dry run the route accepts — the routine changed after cotctl read it — stops and asks you to re-run; and a profile without write permission stops saying so, since the route could not tell. Those last two carry no code in --json. A server without the keyed route still takes such an update through the old route.
COTCTL_LEGACY_PATCH=1 sends every update through the old route, without trying the keyed one. 1, true, yes and on set it; 0, false, no, off or no value leave detection on; any other value is refused before anything is sent, and apply exits 2. It exists for the transition and will be removed in a later release.
There's no delete endpoint, and no cotctl routines logs subcommand: to read what a routine did when it ran — the context it received, what each stage returned, its error — use cotctl logs, for example cotctl logs pull --routine <code> --since 24h.
Apply order
In a directory apply, routines come after workflows and before SLAs and schedules — because an SLA or schedule may invoke a routine from a PBScript stage, and the dry-run validator needs the routine to already exist in the catalog. cotctl apply --dir enforces this ordering for you.