kolu
Docs

How to recycle and upgrade a daemon

When you ship a new daemon build, an older one may still be holding the gate with clients attached. converge() detects the mismatch, decides what to do by your declared policy, and enacts it through the endpoint. The client sees no more than a brief reconnect.

1. Declare a policy

A ConvergencePolicy<Cap> holds everything a consumer declares about convergence: who I am (baked), how I converge per trigger (contract skew against same-contract build change), and, when drainable, the Cap-gated drainBudget. Two shapes ship:

  • recycle-on-skew (kaval): a mismatched daemon is killed and respawned. It has no budget, because a policy that is not drainable cannot spell one.
  • drain-newer-else-refuse (padi): the daemon with the strictly newer contract version wins. It is drained in and replaces the old one; an older or equal contract refuses. The budget survives adopts, so a cross-supervisor fight terminates as cross-supervisor rather than looping.

2. Converge before you connect

State the policy (and probe) on createEndpoint, then call converge(endpoint), the only boot verb:

// The only boot verb — policy (who I am + how I converge) is fixed on the endpoint.
const outcome = yield* converge(endpoint);

converge runs three steps:

  1. probe the running identity, over the version-agnostic channel, so a skew cannot hide it;
  2. decide with the pure decide fold (no I/O, just the policy table);
  3. enact through the endpoint’s private boot methods, budget-gated when drainable.

It returns a ConvergenceOutcome with an optional ConvergenceAnomaly. Wire that to your own status surface and logs. Connector arms use convergeAdmit with the same policy.

probeDaemonIdentity({ capability: "not-drainable" })(socketPath) owns a fresh local socket dial; the drainable form also requires drainCeilingMs. A connector that has already dialled a combined client uses probeDaemonIdentityFrom instead, supplying that client, its disposer, and the transport’s stronger exit oracle. Both forms assemble the same ConvergenceProbe, and the client form never turns a lost link into daemon absence.

3. What a recycle does

When the decision is to recycle, the endpoint:

  1. reads the gate under the pid-first law (readGateIdentity plus injected readProcessIdentity, with a ±2 s start-time match) to see whether a live survivor holds it. A one-field gate falls back to isHolderLive and a socket accept.
  2. kills it, then runs waitForPidGone, so the respawn never races a live gate holder.
  3. calls driver.spawn(), a survivable spawn (systemd-run --user, or detached +unref).
  4. runs waitForSocket, then connect() and the identity handshake.

A connected client has its daemon replaced underneath, and its session loop reconnects, re-running converge, with an onStatus(degraded) bridging the gap. recycle(endpoint, steps) wraps this as capture → drain → recycle → reattach, so a session can be captured and re-adopted live across the kill.