fix(daemon): sync agent-projects.conf on startup + every 5 min

agent-projects.conf is the local mirror of user_projects (name, dir,
agent_pref). sync-agent-runtime-config.py fetches /api/agent/projects
and rewrites the file — but only ran from agent-hook-bridge.sh on
agent stop hooks. So a pref change made via the web Settings page sat
unapplied until the next agent stopped: could be hours.

a6b8658 just made _resolve_live_adapter read this conf as a fallback
to honor the user's agent pref. Pairing that with a daemon that never
re-reads the cloud meant the fallback would silently use stale data.

Fix: run sync once at daemon startup (non-blocking, background), and
schedule a _conf_sync_loop that re-syncs every 300s. Both also write
to the same conf file via tempfile+os.replace, so reads are atomic
and never see a half-written file. _CONF_SYNC_PID is killed in the
existing _shutdown trap so SIGTERM cleans both background loops.

Bounded staleness: any pref change in cloud Settings now propagates
to the local daemon within 5 min (or instantly on next daemon restart),
without needing an agent stop event to trigger sync.
This commit is contained in:
g-but committed 2026-05-31 01:31:35 +02:00
1 parent a6b8658be2
commit 483b9b74e2
1 file changed
+21
+21
View File
@@ -1246,12 +1246,33 @@ _init_base_url
# A former UI toggle used a local sleep sentinel outside persisted policy.
# It is no longer authoritative; discard any stranded value during upgrade.
rm -f "/tmp/cockpit-sleep-mode" 2>/dev/null || true
# Re-sync ~/.config/agent-projects.conf from cloud user_projects on startup
# so a freshly-restarted daemon picks up any agent_pref / dir changes the
# user made while the daemon was offline. Previously this only ran from
# agent-hook-bridge.sh (i.e. when an agent stopped), so a pref edit could
# sit unapplied for hours. Background — non-blocking; daemon proceeds even
# if cloud is unreachable.
python3 "$SCRIPT_DIR/sync-agent-runtime-config.py" >/dev/null 2>&1 &
# Periodic re-sync so changes made via the web UI propagate within ~5 min
# even when no agent stops happen.
_conf_sync_loop() {
while true; do
sleep 300
python3 "$SCRIPT_DIR/sync-agent-runtime-config.py" >/dev/null 2>&1 || true
done
}
_conf_sync_loop &
_CONF_SYNC_PID=$!
log "starting — long-polling $(_base_url) (local wait=25s, remote wait=${POLL_INTERVAL}s), pushing state on change (max every ${PUSH_INTERVAL}s)"
_push_loop &
_PUSH_PID=$!
_shutdown() {
kill "$_PUSH_PID" 2>/dev/null || true
kill "$_CONF_SYNC_PID" 2>/dev/null || true
rm -f "$_AUTH_HEADER"
exit 0
}