fix(broker): preserve node tokens across rotation races

Merged
AG
2mo ago
+1524-87

Summary

  • keep environment-supplied credentials process-local when they differ from the durable node-token cache
  • on node_token_proof_required, adopt a different scoped cache value once and reconnect; otherwise publish a terminal reason through /api/session and emit one secret-free terminal log
  • retain the existing X-Relaycast-Node-Token and Idempotency-Key wire behavior for cache-backed rotations

Tests

  • cargo test -p agent-relay-broker stale_env_token_never_replaces_cached_token_across_restarts -- --nocapture
  • cargo test -p agent-relay-broker node_control_adopts_sibling_rotated_cache_after_proof_conflict -- --nocapture
  • cargo test -p agent-relay-broker node_control_stops_after_server_rejects_rejected_token_as_rotation_proof -- --nocapture
  • cargo test -p agent-relay-broker node_token -- --nocapture
  • cargo clippy -p agent-relay-broker --lib -- -D warnings
  • mutation checks: removing either the cache-preservation guard or the sibling-cache adoption made its named regression test fail

Follow-ups

  • Finding 3: bind replay identity to request-body inputs that may change during the 24-hour window.
  • Finding 4: make post-rotation cache persistence failure actionable rather than warn-only.
  • Finding 5: strengthen remint-cap coverage so it cannot pass vacuously.

Do not merge or release; requester approval is required.


[!NOTE]
High Risk
Changes authentication credential persistence, concurrent rotation semantics, and terminal failure modes for realtime node control—mistakes could strand brokers or adopt wrong tokens.

Overview
Hardens broker node token rotation and recovery when Relaycast returns node_token_proof_required or rejects credentials, and exposes lifecycle state on /api/session via node_control_health (connecting, backoff, ok, or terminal with node_token_proof_required / env_override_rejected).

Recovery behavior: On proof conflict, the node-control client may adopt a different scoped on-disk token once (sibling rotation race) unless RELAY_NODE_TOKEN is active—in that case the cache is not adopted and terminal reason env_override_rejected is reported. Rotations gain file locking, request-body digests bound to durable idempotency keys, process-local key reuse for env tokens after lost responses, and reconciliation so late mint responses do not overwrite a newer cached credential.

Operator docs: CHANGELOG and CLI README describe the new session fields and how to fix stale env overrides vs Cloud enrollment.

Reviewed by Cursor Bugbot for commit b37a9c599d79c3e432982b3a6736746311b4805e. Bugbot is set up for automated code reviews on this repo. Configure here.


Agent Relay sessions

  • codex session 01a10a77 · opened via gh pr create · last active 2026-10-05
  • claude session 228bd896 · contributor · pushed commits · last active 2026-10-07