Skip to content

agmsgd: supervise the sync engine as one child process per connected team - #2130

Merged
fujibee merged 5 commits into
integration/1.10-cloudfrom
feat/agmsgd-sync-supervisor
Nov 30, 2026
Merged

fujibee merged 5 commits into
integration/1.10-cloudfrom
feat/agmsgd-sync-supervisor

Conversation

@fujibee

@fujibee fujibee commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

What

First step of running the sync engine under agmsgd: agmsgd supervises the existing engine as a child process, one per connected team. Nothing changes for an existing install: the supervisor is inert unless run/sync-mode says agmsgd, and the default (file absent) is standalone.

  • scripts/daemon/sync-supervisor.mjs, added to agmsgd's channel list. Each poll it reads run/sync-mode; in agmsgd mode it starts, for every connected team, bash scripts/remote-sync.sh run --team <team> (the program remote.sh sync start starts, so its environment set-up, storage driver and rules are unchanged), restarts it after a failure with a doubling backoff from 5 s capped at 5 min, and ends and restarts one that has completed no cycle for 15 min (SIGTERM, then SIGKILL after 5 s; the kill deadline is set by the first request and is enforced whatever the mode or team list says). Nothing else stops by itself: a failing engine keeps being restarted and the state and agmsgd.log keep saying so (a line per change, repeated every 30 min while it is not running).
  • Starting takes the same team lock remote.sh sync start takes (teams/<team>/.config.lock with its .owner marker and holder record): the check for a running engine, the spawn and the pidfile write happen under it, so a command starting an engine at the same moment cannot produce a second writer. If the lock is held the start waits (bounded) and is reported as a backoff; a pidfile that cannot be published ends the just-started child at once. One engine per team is kept by the facts remote.sh already uses: the supervisor writes run/remote-sync.<team>.pid for its child, removes only a pidfile it wrote, and does not start a second engine when that pidfile names a live engine for the team that it did not start (held_elsewhere, asked again every 30 s).
  • Named states, each with its own retry rule and none treated as a crash: awaiting_unlock (the engine ends with exit status 12, new, when the team has no sync configuration; started again when the team config changes, else asked every 5 min), fenced_awaiting_bundle (a key rotated elsewhere), over_limit (402/413/429 or a limit code), refused (any other answer, with its status and code; only an HTTP status number and a short plain-alphabet code are ever shown, anything else the server sent is replaced by a generic phrase, so a server-supplied value never reaches a state file or agmsgd.log), removed. In all but awaiting_unlock the engine keeps running and asks at its longest interval itself; the stalled-progress kill is not applied to them.
  • Decision (2026-11-30): a device the server removed stops for good. On a 401/410 answer given to this engine the supervisor ends the engine and starts no timer for it; the state says removed by the server: ... Run remote.sh connect or pull to connect this machine again. Only a reconnect (it rewrites the team config) puts the team back. This is a stop by itself, listed as such for §10 review. An answer recorded by an earlier run (before the engine was started) is not about this connection and is ignored.
  • Per-team state file run/sync-supervisor/<team>.json (state, reason, pid, start time, restart and failure counts, next retry, last_progress_at copied from the engine's own cycle record, updated_at), refreshed at least every minute, so a CLI can read progress timestamps when agmsgd is down. The CLI views come in the next PR.
  • Only process lifecycle and the engine's existing files are used: spawn, exit status and signal, SIGTERM, the pidfile, refusal.json, cycles.json. No new channel to the child; its output is never parsed.
  • The child gets agmsgd's environment minus any AGMSG_AGE_IDENTITY* / AGMSG_SYNC_AGE_IDENTITY* / AGMSG_SYNC_TOKEN*, plus AGMSG_SYNC_SUPERVISED=agmsgd (used by the next PR). No secret is on its command line, and nothing here reads or writes a capability URL: only team names, states, pids and timestamps reach a log line or a state file.
  • Engine: ends with exit status 12 (EXIT_SYNC_CONFIG_MISSING) instead of 1 when the team has no sync configuration; every other exit is as before.

Known gap, not in this PR

A team that relied on CURL_CA_BUNDLE / NODE_EXTRA_CA_CERTS exported in the shell that started its engine would lose it under agmsgd (the child inherits agmsgd's environment, not that shell's) and fail TLS. Recording those two paths (path only) in the team's sync config at connect/start, so the supervised child gets them, is planned for the next PR.

Process spawns

  • Per poll (every 5 s) with standalone: one small file read, no process.
  • With agmsgd: per team, reads of the team config, the cycle record and the refusal record (no process); a process starts only when an engine is started or restarted (bash then exec node, as sync start does today). The pidfile liveness check of a foreign pid runs ps once per start attempt on macOS (/proc on Linux).
  • One engine process per connected team, as today.

Tests

  • tests/agmsgd_sync_supervisor.test.mjs (25, wrapped by tests/test_agmsgd_sync_supervisor.bats): standalone starts nothing; one child per connected team with exactly bash <scripts>/remote-sync.sh run --team <team>; no secret on argv or in the environment and no capability URL in a log line or state file; doubling backoff to the 5-minute cap; a restart storm in one team does not delay another; a team whose engine cannot be started is reported and the other runs; stall deadline ends, then kills, then restarts; refusal labels leave a running engine alone; removed stops for good and only a reconnect clears it; awaiting_unlock by exit status 12, not on every backoff; held_elsewhere and a pidfile naming some other process; stop and a switch back to standalone end the children; the state file's contents; and one test with a real child process (its pidfile, a kill, the backoff, the stop); and regression tests for: an engine that ignores SIGTERM is killed after the grace in standalone mode and for a disconnected team, with the deadline set once; a refusal left by an earlier run does not remove a reconnected team; a hostile refusal code (URL, secret-shaped, free text) is never copied; the team lock is respected and released; a pidfile that cannot be written ends the child; a program that cannot be run is one failed start (its error event is handled from the moment of the spawn); a stop, a mode switch or a disconnect that arrives while a start waits for the lock means nothing is started afterwards.
  • tests/remote_sync_engine.test.mjs: the missing-config error carries the code behind exit status 12.
  • Run with AGMSG_HOME and AGMSG_INSTALL_HOME unset: the engine, fence, supervisor and daemon-main node tests (224). The four .github/scripts/check-*.sh pass at their baselines.

@fujibee
fujibee merged commit 587484e into integration/1.10-cloud Nov 30, 2026
33 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