# Author a routine
URL: /documentation/extensions/routines



## Goal [#goal]

Take a routine from an unpublished draft through validation, a dry-run preview, and human-approved publication as an immutable version, using the real action-dispatcher binding table rather than a self-reported honor system.

## Before you start [#before-you-start]

* An initialized project (`strata19.initialize_project`) and, for any step that queries the code graph, a built local index.
* Know which `actionKind`s your routine needs and whether each one really dispatches. Call `strata19.automation_author` action `dry_run` on your specific draft rather than assuming — an action kind's name does not by itself tell you whether it runs.
* Decide up front which steps are human gates. `request_approval`, `ask_question`, `request_form_input`, `create_commit`, `merge_candidate`, and `push_branch` all dispatch only through the run operation's human-gate chokepoint, and only with a real persisted approval or an active grant — an agent can never satisfy its own gate.
* This page covers authoring a routine **live, in your own project** — no plugin rebuild is needed. Bundling a routine so every install ships it by default is a separate, heavier path covered at the end of this page.

## Steps [#steps]

<Steps>
  <Step>
    ### Create an unpublished draft [#create-an-unpublished-draft]

    ```
    Call: strata19.automation_author action draft_create
      workflowKey: "routine.my-new-routine"
      definition: { actions: [...], edges: [...] }
    ```

    A draft is not an execution — nothing runs yet. A real bundled definition, for shape reference (`src/automation/routines/test-impact.ts`):

    ```ts title="src/automation/routines/test-impact.ts"
    export const testImpactRoutine: WorkflowDefinitionV1 = {
      workflowKey: 'routine.test-impact',
      version: 1,
      projectKey: 'strata19-core',
      name: 'Test Impact Analysis',
      description:
        'Analyze the impact of changes on tests by computing change impact, ' +
        'verifying the implementation, and creating work items to track test updates.',
      actions: [
        { actionId: 'step-change-impact', actionKind: 'compute_change_impact', label: 'Compute Change Impact' },
        { actionId: 'step-verify', actionKind: 'verify_implementation', label: 'Verify Implementation' },
        { actionId: 'step-work-item', actionKind: 'create_or_update_work_item', label: 'Create Work Item' },
      ],
      edges: [
        { source: 'step-change-impact', target: 'step-verify' },
        { source: 'step-verify', target: 'step-work-item' },
      ],
      triggers: [{ type: 'manual' }],
      autonomyMode: 'review',
    }
    ```

    `actionId`/`actionKind`/`label` per action node; `source`/`target` per edge (`WorkflowActionNodeV1`/`WorkflowEdgeV1`, `packages/shared-types/src/plugin/workflow.ts`). `autonomyMode` is one of `'shadow' | 'suggest' | 'review' | 'automate'`.
  </Step>

  <Step>
    ### Update the draft until it's structurally sound [#update-the-draft-until-its-structurally-sound]

    ```
    Call: strata19.automation_author action draft_update
    Call: strata19.automation_author action validate_draft
    ```

    `validate_draft` checks unique action ids, every edge endpoint resolves, no unreachable node, no start-node recursion, a bounded graph size, and every action's declared required input keys are present. Errors name the offending node or edge directly. Re-validate after every edit — a stale `expectedVersion` on `draft_update` is a typed conflict, never a silent clobber.
  </Step>

  <Step>
    ### Dry-run before publishing [#dry-run-before-publishing]

    ```
    Call: strata19.automation_author action dry_run
    ```

    Reports, per action, whether it would dispatch to a real implementation or report a typed `unsupported`, and which actions would require a human approval gate — without executing or triggering anything. Treat `unsupported` as a real, typed outcome to design around, not an error to paper over: several `unsupported` kinds have a real implementation elsewhere but are blocked for a specific, named reason (tenant scope, no network/unattended runtime in the local executor, or a contract conflict with the action's own `requiresNetwork: false`) — the response's `reasonCode` says which.
  </Step>

  <Step>
    ### Grant an approval, if a gated step needs one [#grant-an-approval-if-a-gated-step-needs-one]

    ```
    Call: strata19.automation_approvals action grant
    ```

    Only when a human has decided to pre-authorize a gated step for a specific actor and routine version. The grant store refuses a grant whose target actor resolves to the granting session's own identity — an agent cannot pre-authorize itself. `revoke` removes a grant before it expires; a revoked grant can never be reactivated.
  </Step>

  <Step>
    ### Publish [#publish]

    ```
    Call: strata19.automation_author action publish_draft
    ```

    Publishing records an immutable version — it does not start anything. An agent proposing publication cannot supply the approving decision; publication is a human decision, by design, every time. A published version never mutates: create a new draft and a new version for any behavioral change.
  </Step>

  <Step>
    ### Start, drive, and read back the run [#start-drive-and-read-back-the-run]

    ```
    Call: strata19.automation_run action start
    Call: strata19.automation_run action get_next_action
    Call: strata19.automation_run action submit_action_result
    Call: strata19.automation_run action pause      # before disconnecting
    Call: strata19.automation_run action get_receipts
    ```

    A caller-supplied result is not proof of anything — the engine re-derives the real output by calling the real tool itself and attests it (tool name, output digest, dispatch id). A hand-crafted output cannot produce a success receipt. A run is complete when its required receipts exist, not when a step returns and not when you say so.
  </Step>
</Steps>

## Bundling a routine into every install [#bundling-a-routine-into-every-install]

The steps above create a project-local, durable definition (SQLite) that only your project can run. To ship a routine as one every installed plugin offers by default, add a file under `src/automation/routines/` exporting a `WorkflowDefinitionV1` (the shape above) and add it to `BUNDLED_ROUTINES` in `src/automation/routines/index.ts`. That module is imported by `src/mcp/facade/routine-registry.ts` and `workflow-local-adapter.ts` — both part of the façade `pnpm build:plugin` bundles into `facade-stdio.js` — so this change needs the full release loop (version bump, rebuild, commit, reinstall) before an installed host sees it. See [Interface and packaging](/documentation/extensions/interface-and-packaging) for the exact steps.

Note the `.strata19/extensions/<name>/strata19-extension.json` project-local format also accepts a `routines` contribution tier. It is schema-validated and stored with provenance, but nothing in this build wires it to register a runnable routine — treat it as a declared intent, not a way to ship a working routine today.

## Verify [#verify]

```
Call: strata19.automation_run action get_receipts
```

Every receipt for the execution, including its server-computed attestation when present — receipts are not write-only. A routine is verified done when the receipts you expect exist for every step, not when the last call returned without error.

For a bundled routine, confirm the built artifact actually carries it:

```bash
node ./server/facade-stdio.js   # from plugins/strata19/, after pnpm build:plugin
```

then call `strata19.automation_author` action `list_definitions` against that process and confirm your `workflowKey` appears.

## If it doesn't work [#if-it-doesnt-work]

* **`validate_draft` fails on a specific node or edge.** Fix that node — the error already names it; do not restructure the whole graph.
* **A step reports `unsupported`.** Read the `reasonCode` (`requires_tenant_context`, `requires_network_or_unattended_runtime`, or `requires_contract_redesign`) rather than assuming the capability is simply missing — several `unsupported` kinds have a real implementation elsewhere and are blocked for a specific reason.
* **`publish_draft` or a human-gated step is rejected.** Publication and gated actions require a real human decision or an active grant issued by someone other than the calling session — an agent can never satisfy its own gate. This is enforced, not advisory.
* **A step fails partway through a run.** Earlier successful steps' receipts and canonical outputs remain visible; re-running converges on the same identities rather than duplicating them. After a restart, resume from the pending step rather than starting a new run.
* **You bundled a routine in source but an installed host doesn't offer it.** The plugin cache is version-keyed; editing source and running `pnpm build:plugin` changes nothing a running session sees until the version is bumped and the plugin is reinstalled — see [Interface and packaging](/documentation/extensions/interface-and-packaging).

## Related [#related]

* [Interface and packaging](/documentation/extensions/interface-and-packaging) — the release loop a bundled routine needs to reach an installed host.
* [Add an MCP tool](/documentation/extensions/mcp-tools) — when a routine needs an `actionKind` that doesn't dispatch to anything yet.
* [Package a skill](/documentation/extensions/skills) — for teaching a workflow over existing tools without the validation/approval/receipt machinery a routine gets.
* [`reference/routines`](/documentation/reference/routines) — the catalog of routines this install bundles today.
