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.
What “in flight” means
Section titled “What “in flight” means”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).
sefra drain
Section titled “sefra drain”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 |
Procedure
Section titled “Procedure”- 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 (/mcpand the REST mirror) out of the load balancer. Do not usesefra control pausefor this: a pause fails the next forward step of every running plan and compensates it. - Let in-flight executions end. Running steps and compensations finish by themselves. For each execution
sefra drainlists:pending_approval: have it decided, cancel it withsefra executions cancel <id>(the agent seesCANCELLEDand may submit again after the upgrade), or let it expire;needs_intervention: work it withsefra executions get <id>andaudit <id>, thensettle-step,retry-compensationorresolve.
- Wait for drain. Run
sefra drainuntil 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. - Upgrade. Stop Sefra, run
sefra migrateof the new version as the owner of the tables, start the new version with all its roles. - Check.
GET /readyzanswers 200;sefra audit verifyexits 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 keys
Section titled “Temporal payload keys”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:
- Generate a key with
openssl rand -base64 32and put it in a new environment variable. - Set
key_idto a new id andkeyto the new variable. Move the old id and its variable underprevious. - Restart every Sefra process together.
- Remove the old key only when
sefra drainreports nothing in flight and the namespace’s retention has passed since the rotation. Keep it longer if histories must stay readable.