Skip to main content

How workflows execute

Why device provisioning stopped being a set of bespoke endpoints and became a catalog of workflows you run through one route.

Background​

Onboarding once exposed one endpoint per operation. Activating a device was POST /api/activations; tearing one down was POST /api/deactivations. Each endpoint carried its own request shape, its own error vocabulary, and its own copy of the gates that decide whether the operation is allowed at all — is the device registered, is its enrollment group enabled, is it in a state where this makes sense.

That worked while there were two operations. It stopped working as soon as the platform needed variants: activation without a per-customer organization, a provider client with no asset, a read-only query, a GDPR erasure. Every variant meant another endpoint, another request DTO, and another transcription of the same gate logic — which drifted, because transcriptions do.

The gates were the tell. They were identical in every flow, they were the part most likely to be wrong, and they were the part duplicated most.

How it works​

A workflow is an ordered list of steps plus a declaration of the inputs those steps need. The engine owns everything the steps have in common: resolving inputs, taking a lock, running steps in order, recording what happened, and mapping the outcome onto a response.

The gates became steps. require_device_registered and require_state are entries in the step list, exactly like create_provider_client is. There is one implementation of each, shared by every workflow that needs it, and the catalog shows you which workflows those are.

Inputs resolve in layers​

A run is armed from four sources, each overriding the one before it:

  1. The workflow's declared defaults
  2. The enrollment group's projection — only on the group-scoped route
  3. The caller's params
  4. Structural values the route itself owns

The group layer is what makes the scoped route usable. A group knows its own routing target, organization, model, and provider template, so a caller running through it supplies only what is genuinely per-device. The generic route has no group, so the caller supplies everything — which is why it reads as an administrative surface rather than a consumer one.

Required inputs are checked before any step runs. A missing input is a flat rejection with the offending keys named: no partial execution, no record, nothing to clean up.

The subject is the unit of concurrency​

Each workflow declares a subject key — the input naming the thing the run is about. Device flows declare device.id; erasure declares customer.id. The engine locks on the subject's value, so two runs against the same device serialize while runs against different devices do not.

A read-only workflow declares no subject. It takes no lock and writes no record, so concurrent queries never contend. This is also why a query run returns no runId — there is nothing to look up later.

Kind is a label, not a behavior switch​

The kind field — activation, deactivation, query, erasure — exists so a catalog is readable and a response can be shaped appropriately. It does not drive execution. What actually determines behavior is the declared subject and whether the workflow is read-only. Two workflows of the same kind can behave quite differently; two of different kinds can behave identically.

Activation grants almost no access​

It is tempting to read an activation flow as the thing that decides who can see the device. It mostly isn't.

Creating a resource writes its own owner relation to the organization it is created in. The asset API does this for assets, core does it for provider clients. By the time a workflow's create_asset step returns, the asset is already owned by the organization it was created under — no explicit grant required.

That is why basic-device-activation writes no relations at all. Everything it creates is owned by the configured organization already.

The sub-organization flows are the exception, and for a specific reason: they mint a new organization mid-flow. Resources created before that organization existed are owned elsewhere, and shared resources — the model, the provider, the provider template — are owned by whoever set them up. So those flows write four explicit viewer relations from the new customer organization outward, making the sub-organization able to see things it does not own.

The practical consequence is that access to shared resources is a setup-time concern. Activation never grants it. If a customer organization cannot see a model, no amount of re-running activation will fix it — the grant has to exist before the flow runs.

Failure converges rather than rolls back​

When a step fails partway through, the engine stops and records where. It does not compensate: a provider client created by step 8 stays created when step 11 fails.

That is deliberate. Compensation requires every step to have a correct inverse, and to run that inverse reliably during the exact conditions — provider outage, partial network partition — under which failures actually happen. Instead the steps are written to converge: creating something that exists succeeds, deleting something absent succeeds. Recovery is re-running, not unwinding.

The cost is real. A failed activation can leave resources behind that a subsequent run adopts rather than recreates, and an operator reading the provider's console mid-failure sees a half-built device. The benefit is that recovery has one mechanism instead of two, and the mechanism is the same code path that succeeded the first time — so it is exercised constantly rather than only during incidents.

Retry resumes, and takes nothing from the caller​

Retrying a run restarts it at the first step that did not succeed. Steps that already succeeded are never re-executed against external systems.

The retry route accepts no body and reads no query overrides. That is a safety property, not an omission. The run is re-armed from its own recorded inputs and replays the original route's scope, so a group-scoped run re-applies its group's gates. Identity values come from the device record rather than the recording, which means a stale recorded provider cannot be used to mint a second credential.

A retry writes a new record pointing at the run it re-drove. Retries can themselves be retried.

Retention bounds what can be recovered​

Run records expire on a configured window — 90 days by default. This is the mechanism behind two otherwise unrelated facts: a run older than the window returns "not found" rather than "not retryable", and the same window is the erasure SLA for run history.

The erasure workflow deliberately does not purge onboarding's own run records. Doing so would mean a caller could erase the audit trail of the erasure. Instead records age out, and there is no purge-by-id — a record inside the window cannot be removed early through the API.

Why not the alternatives​

Keeping bespoke endpoints and factoring out a shared gate library. This was the obvious smaller step, and it fixes the duplication without any new concepts. It was rejected because it leaves the shape of an operation implicit in code. There is no way to ask the system what a flow requires, so every consumer hard-codes its knowledge of the request body. The catalog exists so that a client can discover the input contract at runtime rather than compile it in.

A general workflow engine with branching and conditionals. Rejected as more machinery than the domain justifies. Every flow here is a straight line; the branching that does exist is a gate that either passes or aborts. Adding conditionals would buy expressiveness nobody has asked for and would make the resume semantics considerably harder to reason about — resuming a linear list is obvious, resuming a graph is not.

Compensating transactions. Covered above: the inverse-operation burden lands on every step author, and the compensation path runs precisely when the system is least healthy.

Consequences​

What this makes easy:

  • Adding a variant is a new step list, not a new endpoint. basic-device-activation shipped as a reordering of existing steps plus one omission.
  • Clients discover input requirements instead of hard-coding them, so adding an optional input breaks nobody.
  • Every run is queryable after the fact, with the exact inputs it was armed with and the outcome of each step.

What this makes hard:

  • The indirection is real. Answering "what does activation do" means reading a step list and then reading the steps, rather than reading one method.
  • Errors are step-attributed, so a caller wanting a single actionable message has to interpret failedStep alongside errorCode.
  • Because failures do not roll back, the provider's view of a device can be ahead of onboarding's until a re-run reconciles them.

See also​