Przeglądaj źródła

fix(integrations): resume enabled local pushers on boot and reconcile dead or stale states (#1704)

Nacho 1 miesiąc temu
rodzic
commit
e76ae17164

+ 3 - 1
app/public/locales/en/translation.json

@@ -718,7 +718,9 @@
       "running": "Running",
       "stopped": "Stopped",
       "error": "Error",
-      "installing": "Installing"
+      "installing": "Installing",
+      "stale": "Stale",
+      "unverified": "Unverified"
     },
     "ago": {
       "s": "{{count}}s ago",

+ 3 - 1
app/public/locales/es/translation.json

@@ -718,7 +718,9 @@
       "running": "En ejecución",
       "stopped": "Detenido",
       "error": "Error",
-      "installing": "Instalando"
+      "installing": "Instalando",
+      "stale": "Desactualizado",
+      "unverified": "Sin verificar"
     },
     "ago": {
       "s": "hace {{count}} s",

+ 93 - 1
server-go/internal/httpapi/integrations.go

@@ -7,6 +7,7 @@ package httpapi
 
 import (
 	"fmt"
+	"log"
 	"net/http"
 	"path/filepath"
 	"sort"
@@ -109,10 +110,18 @@ func (s *server) integrationItemOf(row rintegration.Row) integrationItem {
 	if lastPush > 0 && row.IntervalS > 0 {
 		fresh = time.Now().UnixMilli()-lastPush <= int64(row.IntervalS)*3*1000
 	}
+	// #1696: reconciliar al leer. Una fila en running cuyo último push es más
+	// viejo que 3 intervalos no puede ir de sana: la vista la reporta como
+	// "stale". Es solo vista (la fila no se toca): en cuanto el pusher vuelva
+	// a empujar, el propio cálculo la devuelve a "running".
+	state := row.State
+	if state == "running" && lastPush > 0 && row.IntervalS > 0 && !fresh {
+		state = "stale"
+	}
 	return integrationItem{
 		Slug: row.Slug, Vendor: row.Vendor, Mode: row.Mode, Host: row.Host,
 		Username: row.Username, Version: row.Version, Arch: row.Arch,
-		IntervalS: row.IntervalS, Enabled: row.Enabled, State: row.State,
+		IntervalS: row.IntervalS, Enabled: row.Enabled, State: state,
 		LastError: row.LastError, LastStartMs: row.LastStartMs,
 		LastPushMs: lastPush, Fresh: fresh,
 	}
@@ -139,6 +148,79 @@ func (s *server) broadcastIntegrations() {
 	}
 }
 
+// watchIntegrations (#1696) registra el callback de salida inesperada de los
+// pushers locales (#1674) y reanuda al arrancar las integraciones habilitadas.
+// No-op sin supervisor o sin base de datos (demo). Los paros intencionales no
+// pasan por el callback: los gestiona el handler de stop.
+func (s *server) watchIntegrations() {
+	if s.integrations == nil || s.db == nil {
+		return
+	}
+	s.integrations.SetOnExit(func(slug string, exitErr error) {
+		// Un pusher es un daemon de larga vida: también la salida limpia
+		// (código 0) es inesperada y deja la fila en estado error con su
+		// motivo, igual que un arranque fallido.
+		reason := "pusher exited unexpectedly (exit code 0)"
+		if exitErr != nil {
+			reason = exitErr.Error()
+		}
+		_ = rintegration.NewStore(s.db.DB).SetState(slug, "error", reason)
+		s.broadcastIntegrations()
+	})
+	s.resumeIntegrations()
+}
+
+// resumeIntegrations (#1696) reanuda al arrancar cada integración habilitada.
+// En modo local arranca el pusher con el binario ya instalado y el fichero de
+// entorno 0600 ya existente (el token y la contraseña solo viven ahí); en
+// modo ssh el pusher corre dentro del router y no hay nada que arrancar en el
+// servidor, pero el estado guardado deja de decir running (no verificado
+// hasta el próximo check/start). Un fallo se loga y deja la fila en estado
+// error: nunca tira el arranque del servidor.
+func (s *server) resumeIntegrations() {
+	if s.integrations == nil || s.db == nil {
+		return
+	}
+	store := s.integrationsStore()
+	rows, err := store.List()
+	if err != nil {
+		log.Printf("[netpulse] aviso: integraciones: no se pudo leer la tabla al arrancar (%v)", err)
+		return
+	}
+	for _, row := range rows {
+		if !row.Enabled {
+			continue
+		}
+		if row.Mode != "local" {
+			// El proceso vive en el router: sin verificar tras el reinicio
+			// del servidor; check/start lo reconcilian.
+			if row.State == "running" {
+				_ = store.SetState(row.Slug, "unverified", "")
+			}
+			continue
+		}
+		if s.integrations.Running(row.Slug) {
+			continue
+		}
+		vendor, ok := rintegration.LookupVendor(row.Vendor)
+		if !ok {
+			_ = store.SetState(row.Slug, "error", "unknown vendor "+row.Vendor)
+			log.Printf("[netpulse] aviso: integración %s con vendor desconocido %q al arrancar", row.Slug, row.Vendor)
+			continue
+		}
+		binPath := filepath.Join(s.integrations.Dir(), row.Slug, vendor.Binary)
+		if err := s.integrations.Start(row.Slug, nil, binPath); err != nil {
+			_ = store.SetState(row.Slug, "error", err.Error())
+			log.Printf("[netpulse] aviso: integración %s no se reanudó al arrancar (%v)", row.Slug, err)
+			continue
+		}
+		_ = store.SetState(row.Slug, "running", "")
+		_ = store.SetLastStart(row.Slug, time.Now().UnixMilli())
+		log.Printf("[netpulse] integración %s reanudada al arrancar", row.Slug)
+	}
+	s.broadcastIntegrations()
+}
+
 func (s *server) handleIntegrationsList(w http.ResponseWriter, _ *http.Request) {
 	writeJSON(w, http.StatusOK, map[string]any{"integrations": s.integrationList()})
 }
@@ -430,6 +512,16 @@ func (s *server) handleIntegrationCheck(w http.ResponseWriter, r *http.Request)
 		_, err := s.pool.Run(row.Host, sshServiceScript(vendor, "check"), 30*time.Second)
 		running = err == nil
 	}
+	// #1696: un check verificado reconcilia el estado guardado: el unverified
+	// que deja un reinicio del servidor (o una fila desfasada) vuelve a
+	// running cuando el proceso está demostradamente vivo.
+	if running && row.State != "running" {
+		_ = s.integrationsStore().SetState(slug, "running", "")
+		s.broadcastIntegrations()
+		if fresh, ok := s.getIntegrationRow(slug); ok {
+			row = fresh
+		}
+	}
 	writeJSON(w, http.StatusOK, map[string]any{
 		"integration": s.integrationItemOf(row),
 		"running":     running,

+ 406 - 0
server-go/internal/httpapi/integrations_boot_test.go

@@ -0,0 +1,406 @@
+// integrations_boot_test.go - #1696: reanudación al arrancar de las
+// integraciones locales habilitadas, detección de hijos muertos (la fila pasa
+// a estado error con su motivo) y conciliación de estado running huérfano
+// (sin push en varios intervalos la vista lo reporta como "stale", no sano).
+package httpapi_test
+
+import (
+	"net/http"
+	"os"
+	"path/filepath"
+	"strconv"
+	"strings"
+	"testing"
+	"time"
+
+	"github.com/gnacho/netpulse/server-go/internal/adapters"
+	"github.com/gnacho/netpulse/server-go/internal/apitoken"
+	"github.com/gnacho/netpulse/server-go/internal/auth"
+	"github.com/gnacho/netpulse/server-go/internal/channelplan"
+	"github.com/gnacho/netpulse/server-go/internal/config"
+	"github.com/gnacho/netpulse/server-go/internal/configbackup"
+	"github.com/gnacho/netpulse/server-go/internal/db"
+	"github.com/gnacho/netpulse/server-go/internal/httpapi"
+	"github.com/gnacho/netpulse/server-go/internal/orchestr"
+	"github.com/gnacho/netpulse/server-go/internal/rintegration"
+	"github.com/gnacho/netpulse/server-go/internal/sse"
+	"net/http/httptest"
+)
+
+// makeIntegrationsTestServerSeeded replica makeTestServerWithMode pero con el
+// supervisor de integraciones cableado (como en producción). seed corre antes
+// de construir el handler: las filas de router_integrations deben existir al
+// "arrancar" para que el resume del boot las vea.
+func makeIntegrationsTestServerSeeded(t *testing.T, seed func(d *db.DB, mgr *rintegration.Manager)) (*testServer, *rintegration.Manager) {
+	t.Helper()
+	auth.SetTrustProxy(true)
+	t.Cleanup(func() { auth.SetTrustProxy(false) })
+	dataDir := t.TempDir()
+	cfg, err := config.Load(map[string]string{
+		"AUTH_USER": "admin", "AUTH_PASS": "test123456",
+		"DEMO_MODE": "0", "DATA_DIR": dataDir, "NODE_ENV": "test",
+	}, dataDir)
+	if err != nil {
+		t.Fatalf("config: %v", err)
+	}
+	d, err := db.Open(dataDir)
+	if err != nil {
+		t.Fatalf("db: %v", err)
+	}
+	secret, err := auth.EnsureSessionSecret(d, cfg)
+	if err != nil {
+		t.Fatalf("secret: %v", err)
+	}
+	if err := auth.EnsureUsers(d, cfg); err != nil {
+		t.Fatalf("users: %v", err)
+	}
+	if err := apitoken.EnsureSchema(d); err != nil {
+		t.Fatalf("api_tokens schema: %v", err)
+	}
+	configBackup, err := configbackup.NewStore(d)
+	if err != nil {
+		t.Fatalf("config backup store: %v", err)
+	}
+	hub := sse.NewHub(d, cfg.MaxSSEClients, func() any { return nil })
+	mgr := rintegration.NewManager(filepath.Join(cfg.DataDir, "integrations"))
+	if seed != nil {
+		seed(d, mgr)
+	}
+	handler := httpapi.NewHandler(httpapi.Deps{
+		Config: cfg, DB: d, Adapter: adapters.NewDemo(), Hub: hub, Secret: secret,
+		Started:      time.Now(),
+		TokenStore:   apitoken.NewStore(d, secret),
+		ConfigBackup: configBackup,
+		Orchestr:     orchestr.New(d),
+		Agents:       adapters.NewAgentRegistry(0),
+		ChannelPlan:  channelplan.NewStore(d.DB),
+		Integrations: mgr,
+	})
+	srv := httptest.NewServer(handler)
+	ts := &testServer{Server: srv, db: d, secret: secret}
+	t.Cleanup(func() {
+		srv.Close()
+		d.Close()
+	})
+	return ts, mgr
+}
+
+func makeIntegrationsTestServer(t *testing.T) (*testServer, *rintegration.Manager) {
+	t.Helper()
+	return makeIntegrationsTestServerSeeded(t, nil)
+}
+
+func bootIntegrationRow(slug, vendor, mode string, enabled bool, state string) rintegration.Row {
+	return rintegration.Row{
+		Slug: slug, Vendor: vendor, Mode: mode, Host: "10.0.0.1", Username: "admin",
+		Version: "0.1.1", Asset: vendor + ".tar.gz",
+		Arch: "amd64", IntervalS: 45, Enabled: enabled, State: state,
+	}
+}
+
+// installFakePusher escribe el "binario" instalado del pusher (script sh)
+// dentro del directorio de la integración y su fichero de entorno 0600, el
+// mismo layout que deja un alta real en disco.
+func installFakePusher(t *testing.T, mgr *rintegration.Manager, slug, binary, body string) string {
+	t.Helper()
+	installDir := filepath.Join(mgr.Dir(), slug)
+	if err := os.MkdirAll(installDir, 0o755); err != nil {
+		t.Fatalf("mkdir %s: %v", installDir, err)
+	}
+	bin := filepath.Join(installDir, binary)
+	if err := os.WriteFile(bin, []byte("#!/bin/sh\n"+body), 0o755); err != nil {
+		t.Fatalf("write %s: %v", bin, err)
+	}
+	if err := mgr.WriteEnv(slug, map[string]string{"NETPULSE_SLUG": slug}); err != nil {
+		t.Fatalf("write env %s: %v", slug, err)
+	}
+	return bin
+}
+
+func integrationsBySlug(t *testing.T, ts *testServer) map[string]map[string]any {
+	t.Helper()
+	_, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
+	res := get(t, ts.URL, "/api/integrations", cookie)
+	if res.StatusCode != http.StatusOK {
+		t.Fatalf("GET /api/integrations: status %d", res.StatusCode)
+	}
+	body := readJSON(t, res)
+	rawItems, ok := body["integrations"].([]any)
+	if !ok {
+		t.Fatalf("GET /api/integrations: integrations missing: %v", body)
+	}
+	out := map[string]map[string]any{}
+	for _, raw := range rawItems {
+		it, ok := raw.(map[string]any)
+		if !ok {
+			t.Fatalf("integration item no es un objeto: %v", raw)
+		}
+		slug, _ := it["slug"].(string)
+		out[slug] = it
+	}
+	return out
+}
+
+func waitIntegrationState(t *testing.T, store *rintegration.Store, slug, state string) rintegration.Row {
+	t.Helper()
+	deadline := time.Now().Add(5 * time.Second)
+	for {
+		row, ok := store.Get(slug)
+		if ok && row.State == state {
+			return row
+		}
+		if time.Now().After(deadline) {
+			row, _ = store.Get(slug)
+			t.Fatalf("la fila %s no llegó a estado %q a tiempo (estado %q, lastError %q)", slug, state, row.State, row.LastError)
+		}
+		time.Sleep(5 * time.Millisecond)
+	}
+}
+
+// TestIntegrationsResumeOnBoot: al arrancar el servidor, la integración local
+// habilitada se reanuda sola con el binario y el env ya en disco; la que no
+// tiene binario queda en error sin tirar el arranque; la deshabilitada no se
+// toca; la de modo ssh deja de decir running (no verificada desde aquí).
+func TestIntegrationsResumeOnBoot(t *testing.T) {
+	v, ok := rintegration.LookupVendor("routeros")
+	if !ok {
+		t.Fatal("vendor routeros missing")
+	}
+	ts, mgr := makeIntegrationsTestServerSeeded(t, func(d *db.DB, mgr *rintegration.Manager) {
+		store := rintegration.NewStore(d.DB)
+		installFakePusher(t, mgr, "lab", v.Binary, "sleep 30")
+		_ = store.Upsert(bootIntegrationRow("lab", "routeros", "local", true, "running"))
+		_ = mgr.WriteEnv("broken", map[string]string{"NETPULSE_SLUG": "broken"})
+		_ = store.Upsert(bootIntegrationRow("broken", "routeros", "local", true, "running"))
+		_ = store.Upsert(bootIntegrationRow("off", "routeros", "local", false, "stopped"))
+		_ = store.Upsert(bootIntegrationRow("wrt", "asuswrt", "ssh", true, "running"))
+	})
+	store := rintegration.NewStore(ts.db.DB)
+
+	deadline := time.Now().Add(5 * time.Second)
+	for !mgr.Running("lab") {
+		if time.Now().After(deadline) {
+			t.Fatal("la integración local habilitada no se reanudó al arrancar")
+		}
+		time.Sleep(5 * time.Millisecond)
+	}
+	t.Cleanup(func() { _ = mgr.Stop("lab") })
+
+	row := waitIntegrationState(t, store, "lab", "running")
+	if row.LastStartMs == 0 {
+		t.Fatalf("lab reanudada sin last_start_ms: %+v", row)
+	}
+	if row.LastError != "" {
+		t.Fatalf("lab reanudada con lastError %q", row.LastError)
+	}
+	broken := waitIntegrationState(t, store, "broken", "error")
+	if broken.LastError == "" {
+		t.Fatal("broken quedó en error sin lastError")
+	}
+	if mgr.Running("broken") {
+		t.Fatal("broken no debe tener proceso")
+	}
+	if row, _ := store.Get("off"); row.State != "stopped" || mgr.Running("off") {
+		t.Fatalf("la deshabilitada no se debe tocar: state=%q running=%v", row.State, mgr.Running("off"))
+	}
+	if row, _ := store.Get("wrt"); row.State != "unverified" {
+		t.Fatalf("la ssh habilitada debe dejar running tras el arranque, state=%q", row.State)
+	}
+	if mgr.Running("wrt") {
+		t.Fatal("la ssh no debe arrancar en el servidor")
+	}
+
+	items := integrationsBySlug(t, ts)
+	if it := items["lab"]; it == nil || it["state"] != "running" {
+		t.Fatalf("API lab: %v", items["lab"])
+	}
+	if it := items["broken"]; it == nil || it["state"] != "error" || it["lastError"] == "" {
+		t.Fatalf("API broken: %v", items["broken"])
+	}
+	if it := items["wrt"]; it == nil || it["state"] != "unverified" {
+		t.Fatalf("API wrt: %v", items["wrt"])
+	}
+}
+
+// TestIntegrationsDeadChildFlipsToError: un pusher que muere solo (código de
+// error, salida limpia o un kill externo) deja la fila en estado error con su
+// motivo y deja de reportarse como running.
+func TestIntegrationsDeadChildFlipsToError(t *testing.T) {
+	for _, tc := range []struct {
+		name, script, wantErr string
+	}{
+		{"crash", "exit 3", "exit status 3"},
+		{"clean-exit", "exit 0", "exit code 0"},
+		{"killed", `kill -9 $$`, "signal: killed"},
+	} {
+		t.Run(tc.name, func(t *testing.T) {
+			v, ok := rintegration.LookupVendor("routeros")
+			if !ok {
+				t.Fatal("vendor routeros missing")
+			}
+			slug := "dead-" + tc.name
+			ts, mgr := makeIntegrationsTestServer(t)
+			store := rintegration.NewStore(ts.db.DB)
+			bin := installFakePusher(t, mgr, slug, v.Binary, tc.script)
+			if err := store.Upsert(bootIntegrationRow(slug, "routeros", "local", true, "running")); err != nil {
+				t.Fatal(err)
+			}
+			if err := mgr.Start(slug, nil, bin); err != nil {
+				t.Fatalf("start: %v", err)
+			}
+			row := waitIntegrationState(t, store, slug, "error")
+			if row.LastError == "" || !strings.Contains(row.LastError, tc.wantErr) {
+				t.Fatalf("lastError = %q, want contener %q", row.LastError, tc.wantErr)
+			}
+			if mgr.Running(slug) {
+				t.Fatal("el hijo muerto sigue reportándose como running")
+			}
+			items := integrationsBySlug(t, ts)
+			if it := items[slug]; it == nil || it["state"] != "error" {
+				t.Fatalf("API %s: %v", slug, items[slug])
+			}
+		})
+	}
+}
+
+// TestIntegrationsIntentionalStopDoesNotError: parar por API es un paro
+// intencional: la fila queda stopped y el callback de muerte no la marca error.
+func TestIntegrationsIntentionalStopDoesNotError(t *testing.T) {
+	v, ok := rintegration.LookupVendor("routeros")
+	if !ok {
+		t.Fatal("vendor routeros missing")
+	}
+	ts, mgr := makeIntegrationsTestServer(t)
+	store := rintegration.NewStore(ts.db.DB)
+	bin := installFakePusher(t, mgr, "manual", v.Binary, "sleep 30")
+	if err := store.Upsert(bootIntegrationRow("manual", "routeros", "local", true, "running")); err != nil {
+		t.Fatal(err)
+	}
+	if err := mgr.Start("manual", nil, bin); err != nil {
+		t.Fatalf("start: %v", err)
+	}
+	t.Cleanup(func() { _ = mgr.Stop("manual") })
+
+	_, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
+	res, body := wanSpeedRequest(t, "POST", ts.URL, "/api/integrations/manual/stop", cookie, "")
+	if res.StatusCode != http.StatusOK {
+		t.Fatalf("POST stop: status %d (%v)", res.StatusCode, body)
+	}
+	if it := body["integration"].(map[string]any); it["state"] != "stopped" {
+		t.Fatalf("estado tras stop: %v", it["state"])
+	}
+	// Ventana corta de ausencia: ningún callback tardío debe convertir el
+	// paro intencional en error.
+	deadline := time.Now().Add(300 * time.Millisecond)
+	for time.Now().Before(deadline) {
+		if row, _ := store.Get("manual"); row.State == "error" {
+			t.Fatal("el paro intencional acabó en estado error")
+		}
+		time.Sleep(20 * time.Millisecond)
+	}
+}
+
+// TestIntegrationsCheckReconcilesState: un check con el proceso
+// demostradamente vivo saca la fila de un estado desfasado (unverified tras un
+// reinicio del servidor) y la devuelve a running; sin proceso, la fila no se
+// toca (el error sigue siendo error).
+func TestIntegrationsCheckReconcilesState(t *testing.T) {
+	v, ok := rintegration.LookupVendor("routeros")
+	if !ok {
+		t.Fatal("vendor routeros missing")
+	}
+	ts, mgr := makeIntegrationsTestServer(t)
+	store := rintegration.NewStore(ts.db.DB)
+	bin := installFakePusher(t, mgr, "verify", v.Binary, "sleep 30")
+	if err := store.Upsert(bootIntegrationRow("verify", "routeros", "local", true, "unverified")); err != nil {
+		t.Fatal(err)
+	}
+	if err := store.Upsert(bootIntegrationRow("dead-check", "routeros", "local", true, "error")); err != nil {
+		t.Fatal(err)
+	}
+	if err := mgr.Start("verify", nil, bin); err != nil {
+		t.Fatalf("start: %v", err)
+	}
+	t.Cleanup(func() { _ = mgr.Stop("verify") })
+
+	_, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
+	res, body := wanSpeedRequest(t, "POST", ts.URL, "/api/integrations/verify/check", cookie, "")
+	if res.StatusCode != http.StatusOK {
+		t.Fatalf("POST check: status %d (%v)", res.StatusCode, body)
+	}
+	if body["running"] != true {
+		t.Fatalf("check running: %v", body["running"])
+	}
+	if it := body["integration"].(map[string]any); it["state"] != "running" {
+		t.Fatalf("estado tras check: %v", it["state"])
+	}
+	if row, _ := store.Get("verify"); row.State != "running" {
+		t.Fatalf("la fila no se reconcilió con el check: %q", row.State)
+	}
+
+	res, body = wanSpeedRequest(t, "POST", ts.URL, "/api/integrations/dead-check/check", cookie, "")
+	if res.StatusCode != http.StatusOK {
+		t.Fatalf("POST check dead: status %d (%v)", res.StatusCode, body)
+	}
+	if body["running"] != false {
+		t.Fatalf("check running dead: %v", body["running"])
+	}
+	if row, _ := store.Get("dead-check"); row.State != "error" {
+		t.Fatalf("el check sin proceso no debe tocar la fila: %q", row.State)
+	}
+}
+
+// TestIntegrationsStaleNotReportedRunning: conciliación al leer. Una fila en
+// running cuyo último push es más viejo que 3 intervalos se reporta como
+// "stale" en la API (y no fresh), sin tocar la fila; con push reciente sigue
+// siendo running/fresh; sin push nunca no se puede juzgar y sigue running.
+func TestIntegrationsStaleNotReportedRunning(t *testing.T) {
+	ts, _ := makeIntegrationsTestServer(t)
+	store := rintegration.NewStore(ts.db.DB)
+	now := time.Now().Unix()
+	rows := []struct {
+		slug, vendor, mode, state string
+		pushSec                   int64
+	}{
+		{"fresh-push", "routeros", "local", "running", now - 10},
+		{"old-push", "routeros", "local", "running", now - 200},
+		{"no-push", "routeros", "local", "running", 0},
+		{"errored", "routeros", "local", "error", now - 200},
+		{"stale-ssh", "asuswrt", "ssh", "running", now - 200},
+	}
+	for _, r := range rows {
+		if err := store.Upsert(bootIntegrationRow(r.slug, r.vendor, r.mode, true, r.state)); err != nil {
+			t.Fatal(err)
+		}
+		if r.pushSec > 0 {
+			if _, err := ts.db.Exec(
+				"INSERT INTO kv (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value",
+				"agent.last_push_ts."+r.slug, strconv.FormatInt(r.pushSec, 10),
+			); err != nil {
+				t.Fatal(err)
+			}
+		}
+	}
+
+	items := integrationsBySlug(t, ts)
+	if it := items["fresh-push"]; it == nil || it["state"] != "running" || it["fresh"] != true {
+		t.Fatalf("fresh-push: %v", items["fresh-push"])
+	}
+	if it := items["old-push"]; it == nil || it["state"] != "stale" || it["fresh"] != false {
+		t.Fatalf("old-push debe salir stale sin fresh: %v", items["old-push"])
+	}
+	if it := items["no-push"]; it == nil || it["state"] != "running" {
+		t.Fatalf("no-push (sin push aún) no se puede juzgar: %v", items["no-push"])
+	}
+	if it := items["errored"]; it == nil || it["state"] != "error" {
+		t.Fatalf("errored: %v", items["errored"])
+	}
+	if it := items["stale-ssh"]; it == nil || it["state"] != "stale" {
+		t.Fatalf("stale-ssh: %v", items["stale-ssh"])
+	}
+	// La conciliación es de vista: la fila sigue en running en la base de
+	// datos y el propio cálculo la devuelve a running cuando vuelva a empujar.
+	if row, _ := store.Get("old-push"); row.State != "running" {
+		t.Fatalf("la fila no se debe tocar: state=%q", row.State)
+	}
+}

+ 4 - 12
server-go/internal/httpapi/server.go

@@ -369,18 +369,10 @@ func NewHandler(d Deps) http.Handler {
 	if d.Config != nil {
 		s.rtlConsole = newRtlConsoleCache(d.Config.RTLConsolePass, d.Config.RTLConsolePollSec)
 	}
-	// #1674: al morir un pusher local de forma inesperada, marcar el estado
-	// error y re-emitir la lista por SSE. Los paros intencionales no pasan por
-	// aquí (los gestiona el handler).
-	if s.integrations != nil && s.db != nil {
-		s.integrations.SetOnExit(func(slug string, exitErr error) {
-			if exitErr == nil {
-				return
-			}
-			_ = rintegration.NewStore(s.db.DB).SetState(slug, "error", exitErr.Error())
-			s.broadcastIntegrations()
-		})
-	}
+	// #1696: callback de muerte inesperada de pushers locales (#1674) más
+	// reanudación al arrancar de las integraciones habilitadas. Los paros
+	// intencionales no pasan por aquí (los gestiona el handler).
+	s.watchIntegrations()
 
 	mode := d.Adapter.Mode()
 

+ 64 - 0
server-go/internal/rintegration/manager_test.go

@@ -4,6 +4,7 @@ import (
 	"os"
 	"path/filepath"
 	"testing"
+	"time"
 )
 
 func TestEscapeEnvValueRoundTrip(t *testing.T) {
@@ -51,3 +52,66 @@ func TestWriteReadEnvFile(t *testing.T) {
 		}
 	}
 }
+
+func writeScript(t *testing.T, dir, name, body string) string {
+	t.Helper()
+	p := filepath.Join(dir, name)
+	if err := os.WriteFile(p, []byte("#!/bin/sh\n"+body), 0o755); err != nil {
+		t.Fatalf("write %s: %v", name, err)
+	}
+	return p
+}
+
+// TestManagerNotifiesCleanExit: un hijo que termina solo (código 0) también
+// notifica a onExit con err nil (#1696). El pusher es un daemon de larga
+// vida: su consumidor decide que una salida limpia es inesperada.
+func TestManagerNotifiesCleanExit(t *testing.T) {
+	dir := t.TempDir()
+	m := NewManager(dir)
+	type exitEvent struct {
+		slug string
+		err  error
+	}
+	ch := make(chan exitEvent, 4)
+	m.SetOnExit(func(slug string, err error) { ch <- exitEvent{slug, err} })
+	if err := m.WriteEnv("clean", map[string]string{"X": "1"}); err != nil {
+		t.Fatalf("write env: %v", err)
+	}
+	if err := m.Start("clean", nil, writeScript(t, dir, "clean-pusher", "exit 0")); err != nil {
+		t.Fatalf("start: %v", err)
+	}
+	select {
+	case ev := <-ch:
+		if ev.slug != "clean" || ev.err != nil {
+			t.Fatalf("onExit = (%q, %v), want (clean, nil)", ev.slug, ev.err)
+		}
+	case <-time.After(5 * time.Second):
+		t.Fatal("onExit no se disparó en una salida limpia (código 0)")
+	}
+	if m.Running("clean") {
+		t.Fatal("el hijo muerto sigue reportándose como running")
+	}
+}
+
+// TestManagerNoOnExitAfterIntentionalStop: un Stop no dispara onExit; el
+// estado de un paro intencional lo escribe el consumidor, no el manager.
+func TestManagerNoOnExitAfterIntentionalStop(t *testing.T) {
+	dir := t.TempDir()
+	m := NewManager(dir)
+	fired := make(chan string, 4)
+	m.SetOnExit(func(slug string, err error) { fired <- slug })
+	if err := m.WriteEnv("sleeper", map[string]string{"X": "1"}); err != nil {
+		t.Fatalf("write env: %v", err)
+	}
+	if err := m.Start("sleeper", nil, writeScript(t, dir, "sleeper-pusher", "sleep 30")); err != nil {
+		t.Fatalf("start: %v", err)
+	}
+	if err := m.Stop("sleeper"); err != nil {
+		t.Fatalf("stop: %v", err)
+	}
+	select {
+	case slug := <-fired:
+		t.Fatalf("onExit se disparó en un paro intencional (slug %q)", slug)
+	case <-time.After(300 * time.Millisecond):
+	}
+}