Skip to content

Upgrading Sefra by drain

A new Sefra version is deployed only when no execution is in flight: the workflow code may change between versions, and Sefra has no versioned workers today. Rolling upgrades are Planned. This page is the procedure.

An execution is in flight while its status is not final:

Status Why it is in flight How it ends
pending_approval Its workflow waits for a decision Approved and run, denied, cancelled, or expired (24 hours by default)
executing Steps are running, or it was accepted while Temporal was down and the sweeper has not started it yet Its steps finish
compensating Earlier steps are being undone Compensation ends, or enters needs_intervention
needs_intervention Its workflow waits for an operator settle-step, retry-compensation or resolve

Final statuses are succeeded, rejected, failed, rolled_back and resolved_manually. An execution with a final status is still in flight while its workflow runs: a denied, expired or cancelled approval is final as soon as its transaction commits, and the workflow closes when it next reads the approval, on its wake-up Signal or at most timings.approval_recheck later. sefra drain asks Temporal for these through GET /v1/workflows/open and lists them as workflow running (final status).

export SEFRA_OPERATOR_URL=https://sefra-operator.example
export SEFRA_OPERATOR_TOKEN=…   # a principal with sefra_operator or sefra_admin
sefra drain

It reads GET /v1/executions?status=in_flight and GET /v1/workflows/open, and changes nothing.

Exit Output Meaning
0 drained: no execution in flight Safe to upgrade, provided nothing admits a new submission (step 1)
1 in flight: N executions and one line per execution: id, status, last change Wait, or work the listed executions
1 sefra drain: cannot tell whether executions are in flight: … or … whether workflows are open: … The operator API or Temporal is unreachable, or the token was refused; do not upgrade
2 usage Wrong arguments or no token
  1. Stop admission. No new submission may be accepted between a drain that exits 0 and the stop. Either restart Sefra with sefra serve --role worker, which keeps the worker and the operator API (approvals and the approval page) but closes the agent listener, or take the agent listener (/mcp and the REST mirror) out of the load balancer. Do not use sefra control pause for this: a pause fails the next forward step of every running plan and compensates it.
  2. Let in-flight executions end. Running steps and compensations finish by themselves. For each execution sefra drain lists:
    • pending_approval: have it decided, cancel it with sefra executions cancel <id> (the agent sees CANCELLED and may submit again after the upgrade), or let it expire;
    • needs_intervention: work it with sefra executions get <id> and audit <id>, then settle-step, retry-compensation or resolve.
  3. Wait for drain. Run sefra drain until it exits 0. Temporal’s list of open workflows follows a closed workflow within a moment, so poll rather than run it once. A deployment script polls it, for example every 30 seconds, and stops with an error after a deadline of its own.
  4. Upgrade. Stop Sefra, run sefra migrate of the new version as the owner of the tables, start the new version with all its roles.
  5. Check. GET /readyz answers 200; sefra audit verify exits 0.

Accepted executions whose workflow was never started (Temporal was down at acceptance) are executing or pending_approval and therefore block the drain until the sweeper has started them and they have ended. If Temporal is down during the procedure, bring it back first.

temporal.payload_encryption (configuration) is read at start. Restart every Sefra process together after changing it, because a process without a key cannot read what another process wrote under that key.

Change Drain first? Why
Turn encryption on No Old payloads are read as they are
Rotate, with the old key under previous No Every key still in use can read
Turn encryption off Yes Open workflows hold payloads nothing could read
Remove a key from previous Yes The same, for what that key wrote
Change the material of an existing key_id Yes The same, and the old payloads no longer open under that id

Where a drain is needed, follow steps 1 to 3 of the procedure until sefra drain reports nothing in flight, then stop Sefra, change the configuration and start it.

To rotate:

  1. Generate a key with openssl rand -base64 32 and put it in a new environment variable.
  2. Set key_id to a new id and key to the new variable. Move the old id and its variable under previous.
  3. Restart every Sefra process together.
  4. Remove the old key only when sefra drain reports nothing in flight and the namespace’s retention has passed since the rotation. Keep it longer if histories must stay readable.