Files
ruflo/v3/@claude-flow
rUv c42aa265a0 fix(swarm): reconcile orphan entries on swarm-state.json load (#1799) (#1809)
`.claude-flow/swarm/swarm-state.json` was retaining entries with
`status: "running"` indefinitely after the host process exited.
Each `claude-flow start --daemon` registered a new swarm; nothing ever
reconciled those entries against PID liveness or heartbeat TTL, so
crashed/exited orchestrators left permanent ghost registrations.
Reporter measured three orphan swarms accumulated in a single afternoon
on Windows v3.7.0-alpha.8.

Two changes in `swarm-tools.ts`:

  1. `swarm_init` now records `pid: process.pid` on the new
     `SwarmState`. Backward compatible — the field is optional.

  2. `loadSwarmStore()` runs `reconcileOrphanSwarms()` on every read
     and persists if anything changed. Reconciliation marks any
     `status: 'running'` entry as `terminated` (with a
     `terminationReason`) when:

       - PID-based: `pid` is set and the process is dead
         (ESRCH from `process.kill(pid, 0)`; EPERM still counts as
         alive — different user but live)
       - TTL fallback: pre-#1799 entries with no `pid` are reaped
         when `updatedAt` is older than 24h (conservative; live but
         long-idle swarms can recover by writing a heartbeat)

`swarm_status` and `swarm_health` no longer return the ghost entries
the reporter complained about. Cost is sub-millisecond (one kill(0)
syscall per running entry).

Closes #1799.
2026-05-06 16:52:26 -04:00
..