fix(daemon): heartbeat push so the cloud UI doesn't show 'daemon offline' on idle

User reported cockpitapp.vercel.app/control still showing 'Local daemon
offline last seen Xm ago' even though the daemon is alive and the
FORCE_REMOTE fix landed yesterday.

Root cause: _push_loop only POSTs to /api/control/runtime-state when
the state hash changes. Between agent actions, state is stable for
minutes, so no push fires. The cloud UI's daemonOffline threshold is
90 s (src/components/control/ControlPanel.tsx:110). Idle daemon →
push gap > 90 s → UI permanently flagged offline.

Added a heartbeat: even when the hash is unchanged, force a push
every DAEMON_HEARTBEAT_S (default 60 s, env-overridable). That stays
comfortably under the 90 s threshold with one missed-push slack.
Pushes on actual state change still fire immediately (the OR
condition runs first). Net cost: 1 push/min from a healthy daemon
instead of 0 — negligible vs the inject claim-loop frequency.

Bash syntax clean. Will take effect after daemon restart on this
machine.
This commit is contained in:
g-but committed 2026-05-20 09:50:09 +02:00
1 parent 15af9d93ea
commit 247e9e0d5d
1 file changed
+11 -2
+11 -2
View File
@@ -634,13 +634,22 @@ push_runtime_state() {
_push_loop() {
local _last_hash=""
local _last_push_ts=0
# Heartbeat: even when the state hash is unchanged, force a push every
# HEARTBEAT_S so the cloud UI's "daemon offline" threshold (90 s in
# ControlPanel.tsx:daemonOffline) never trips on a healthy idle daemon.
# 60 s comfortably stays under that 90 s window with one missed-push slack.
local _heartbeat_s="$(_brand_env DAEMON_HEARTBEAT_S 60)"
while true; do
local _s _h
local _s _h _now _age
_s=$(_build_state_json 2>/dev/null) || true
if [ -n "$_s" ]; then
_h=$(printf '%s' "$_s" | md5sum | cut -d' ' -f1)
if [ "$_h" != "$_last_hash" ]; then
_now=$(date +%s)
_age=$(( _now - _last_push_ts ))
if [ "$_h" != "$_last_hash" ] || [ "$_age" -ge "$_heartbeat_s" ]; then
_last_hash="$_h"
_last_push_ts="$_now"
curl -sf --max-time 8 -X POST \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \