Skip to content

remote: send each new key epoch to the server and fetch a newer one another machine published - #2110

Merged
fujibee merged 6 commits into
integration/1.10-cloudfrom
feat/epoch-fence-recovery
Nov 29, 2026
Merged

fujibee merged 6 commits into
integration/1.10-cloudfrom
feat/epoch-fence-recovery

Conversation

@fujibee

@fujibee fujibee commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

What

On a team that uses a server's key escrow, a key rotation made with key.sh rotate now reaches the server, and a second machine that has not got the new key gets it from there. Nothing in agmsg rotates a key on its own: key.sh rotate is the only thing that writes an epoch after the first one (checked: key.sh is the only script that appends a rotation to the journal; the new code only sends and fetches).

  • Send. Once a rotation is confirmed (the engine pulled and activated it), the engine runs remote.sh escrow-sync <team>, which sends the newest epoch's key and snapshot to POST <address>/v1/keys/epochs, with the same body and checks as the first epoch from the escrow connect (same key and same public key already held = a repeat, a different key = a stop that says so). The first epoch at connect now takes the same path (the epoch is read from the team's latest key snapshot instead of being fixed at 1). A team connected with its own key sends nothing: the command asks the server what it holds and leaves the team alone when it holds nothing.
  • Fetch. A machine that finds a rotation announced by another machine, and does not hold that key or its snapshot, fetches GET <address>/v1/keys/bundle, opens it with this machine's device key, and imports it through the same checks as pull (unlock --authenticated-bundle-stdin). The running engine then reloads its configuration and goes on; the same happens when the engine starts on such a team. Messages waiting to be sent are never dropped while it waits.
  • key.sh rotate on a device-approved team no longer tells only "distribute it out of band": it says the key reaches the server once the rotation is confirmed, and still prints the command that shows the key for a team that holds its own.

Not in this PR: any rotation on device removal, any handling of a fenced write (nothing fences in 1.10).

Stops and recovery (review-criteria section 10)

  • Fetch cannot get the new key (server unreachable, or it does not yet hold a newer epoch): the engine asks again every 3 s, 40 times. It then prints one line naming the two commands to run (remote.sh escrow-install <team>, then remote.sh sync start <team>), records the wait where remote.sh status shows it (a refusal record with code key-import-required, cleared by the next good cycle), and goes on asking once a minute. It never ends on this error and the queue is untouched. At engine start the same fetch is tried 10 times and then the start fails with that error naming the same two commands. Tested with the engine's own default classifiers (a run of 45 failing cycles: 45 fetches, six one-minute waits, one stated line, no exit).
  • A bundle that fails any check installs nothing (unchanged from pull).
  • Send fails (including part way through several epochs): the command prints what to run (remote.sh escrow-sync <team>). Running it again continues from the server's next epoch, so a run that stopped between epochs is finished in order; the engine also tries at its next rotation or start.

Process spawns

Measured by shimming external commands in a throwaway sandbox install against the reference server.

  • Engine, steady state: no change. The check "did a rotation become effective" compares one number already in memory after each cycle; no process is started.
  • One escrow-sync (send) with nothing missing: about 90 external launches (one node call more than before, which returns the public half of an epoch's key) (23 sed, 16 sqlite3, 12 rm, 11 mktemp, 6 cat, 5 chmod, 4 curl, ...) against 23 for remote.sh status in the same setup, about 2.3 s more (2.6 s vs 4.9 s, both from a sandbox start). It reads the server's epochs once, then sends each missing epoch (one body build and one request each) or, with none missing, repeats the newest. It runs once per confirmed rotation and once at engine start for a team that has rotated, never per cycle or per message. Connect now also reads the server's epochs once before it sends (one more request).
  • One fetch attempt: one request plus the work of pull's import when a newer epoch exists; one request when it does not.

Tests

  • tests/remote_sync_fence.test.mjs (6): the engine fetches on a missing key and asks again without a long wait; reloads its configuration after an install without sleeping; after the 40 quick tries it says what to run once, records the wait for status, and keeps asking each minute (default classifiers); sends a newly effective epoch once; passes only the team and --fetch on the child's command line with no token or identity in its environment; startup fetch is bounded and other errors pass through.
  • tests/test_remote_escrow.bats (+7, 26 in all): no external command (sqlite3, node, curl, age, ...) is started with the team's private key in its arguments, checked by spying on every launch through a connect and a send (red when the body is read through sqlite3 again); the epochs are sent in order from the server's next one, and after a failure on epoch 3 a rerun sends only epoch 3 (red when only the newest epoch is sent); a repeat of the newest epoch is not a second epoch; an own-key team sends nothing; --fetch with no newer epoch installs nothing and exits 0; a team not joined by device approval is left alone and an unknown option is refused; the key.sh rotate text on a device-approved team.
  • Run with AGMSG_HOME and AGMSG_INSTALL_HOME unset. The four .github/scripts/check-*.sh pass at their baselines.

Run against the reference server (accounts profile, key escrow with a key file)

On a throwaway PostgreSQL 14 cluster on a loopback port, two sandboxed installs of this branch, nothing outside the sandbox changed.

  1. Machine A: connect by device approval; the server holds epoch 1.
  2. Machine B: pull by device approval; the key bundle came from the server and B synced.
  3. A: key.sh rotate. The engine confirmed it and sent epoch 2; the server then held epochs 1 and 2 for the team.
  4. B (engine restarted on this branch): fetched the epoch-2 key at start, loaded it, synced; a message A wrote under epoch 2 appeared in B's history.
  5. Several epochs missing: on a third install, publishing was made to fail (the device key hidden) while two rotations were confirmed locally; the server still held only epoch 1. After the key was back, one remote.sh escrow-sync registered epochs 2 and 3, in that order.
  6. The owner removed B's device: B's engine was refused (401), B's escrow-install was refused (401, "the server did not accept this machine's device key"), and A kept writing under the same key.

The first rotation attempt also showed that a second machine started on a rotated team ended with a fatal error and no recovery; that is the start-up fetch above.

Not run: Windows, a server behind TLS, a concurrent rotation by two machines (the second to send is told the server holds a different key for that epoch and is pointed at escrow-install).

Notes for the release

  • remote: on a team that uses a server's key escrow, a key rotation made with key.sh rotate is sent to the server, and another machine fetches the new key from it. No key is rotated automatically.

@fujibee
fujibee force-pushed the feat/epoch-fence-recovery branch from c2d723b to 8c0c2d2 Compare November 29, 2026 15:09
@fujibee
fujibee merged commit 097a5f0 into integration/1.10-cloud Nov 29, 2026
14 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant