feat: hot-reload de agentes individuales via SIGHUP

Implementa el mecanismo de hot-reload descrito en el issue 0013:

- agents/runtime.go: añade Agent.Stop() y Agent.Done() para ciclo de vida
  individual. Run() crea un contexto hijo cancelable y cierra el canal
  done al retornar.

- cmd/launcher/registry.go (nuevo): agentRegistry rastrea agentes vivos
  por ID. Métodos: register, stopAndWait, reload, reloadAll, waitAll,
  cleanupLogs. reload() sigue el flujo completo: stop→wait→unsubscribe
  →reload config→recreate→rewire bus/orch→start nueva goroutine.

- cmd/launcher/main.go: usa agentRegistry en lugar de sync.WaitGroup.
  Añade handler de SIGHUP en goroutine separada que lee run/reload.txt
  para determinar el agente objetivo (* o vacío = todos). Tras leer,
  borra run/reload.txt para no afectar el siguiente SIGHUP.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-08 18:41:32 +00:00
parent f25ebc7b79
commit 069f8758b1
3 changed files with 316 additions and 28 deletions
+64 -28
View File
@@ -12,7 +12,6 @@ import (
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"time"
@@ -72,13 +71,7 @@ func main() {
logger.Warn("could not create file logger, falling back to stdout", "err", err)
launcherCleanup = func() {}
}
var cleanups []func()
cleanups = append(cleanups, launcherCleanup)
defer func() {
for _, fn := range cleanups {
fn()
}
}()
defer launcherCleanup()
if len(configPaths) == 0 {
logger.Warn("no agent configs found — nothing to start")
@@ -96,15 +89,49 @@ func main() {
if err != nil {
// Non-fatal: orchestration is optional
logger.Warn("orchestrator not started", "err", err)
} else {
} else if orch != nil {
logger.Info("orchestrator initialized")
}
// ── Start normal agents ──
var wg sync.WaitGroup
var scannerOnce sync.Once
var scanner *mautrix.Client
// ── Shared dependencies for agent registry ──
deps := &launchDeps{
agentBus: agentBus,
orch: orch,
logDir: logDir,
logLevel: lvl,
parentCtx: ctx,
}
registry := newAgentRegistry(deps)
// ── SIGHUP: hot-reload individual agent or all agents ──
sighup := make(chan os.Signal, 1)
signal.Notify(sighup, syscall.SIGHUP)
go func() {
for {
select {
case <-ctx.Done():
return
case _, ok := <-sighup:
if !ok {
return
}
id := readReloadTarget("run/reload.txt")
// Remove the target file after reading so it doesn't
// affect the next SIGHUP.
_ = os.Remove("run/reload.txt")
if id == "" {
logger.Info("sighup: reloading all agents")
registry.reloadAll(rulesFor)
} else {
logger.Info("sighup: reloading agent", "id", id)
registry.reload(id, rulesFor)
}
}
}
}()
// ── Start normal agents ──
var scannerOnce scanOnce
for _, path := range configPaths {
path := path
cfg, err := config.Load(path)
@@ -130,11 +157,11 @@ func main() {
agentLogger = logger.With("agent", cfg.Agent.ID)
agentCleanup = func() {}
}
cleanups = append(cleanups, agentCleanup)
a, err := agents.New(cfg, rules, agentLogger)
if err != nil {
logger.Error("failed to create agent", "id", cfg.Agent.ID, "err", err)
agentCleanup()
continue
}
@@ -154,30 +181,28 @@ func main() {
})
// Grab the first available Matrix client for room scanning
scannerOnce.Do(func() {
scanner = a.RawMatrixClient()
})
scannerOnce.set(a.RawMatrixClient())
}
wg.Add(1)
go func() {
defer wg.Done()
agentLogger.Info("agent running")
if err := a.Run(ctx); err != nil {
agentLogger.Error("agent stopped with error", "err", err)
}
}()
registry.register(&runningAgent{
agent: a,
cfg: cfg,
cfgPath: path,
logger: agentLogger,
logCleanup: agentCleanup,
})
}
// ── Startup room scan (after all participants are registered) ──
if orch != nil && scanner != nil {
orch.orchestrator.SetScanner(scanner)
if orch != nil && scannerOnce.client != nil {
orch.orchestrator.SetScanner(scannerOnce.client)
scanCtx, scanCancel := context.WithTimeout(ctx, 30*time.Second)
orch.orchestrator.ScanExistingRooms(scanCtx)
scanCancel()
}
wg.Wait()
registry.waitAll()
registry.cleanupLogs()
logger.Info("all agents stopped")
return nil
},
@@ -195,6 +220,17 @@ func main() {
}
}
// scanOnce captures the first Matrix client for room scanning.
type scanOnce struct {
client *mautrix.Client
}
func (s *scanOnce) set(c *mautrix.Client) {
if s.client == nil {
s.client = c
}
}
// orchHandle wraps a running orchestrator with its config for the launcher.
type orchHandle struct {
orchestrator *orchshell.Orchestrator