Files
loki/scripts/box-runner.ts
T
Mao NakamotoandClaude Fable 5 5fc3333f9a fix(control): fleet truth pass — honest hero, live janitors, actionable streaks
Dogfood session drove FleetCrown against production and found the fleet
had been silently dead for 2 days behind a green light:

- nudge-idle raw-queried beacon_settings WHERE mode != 'off', so users
  with NO row (missing row = default 'on') were invisible to the
  scheduler — autopilot dispatched nothing while the hero said
  'Autopilot on'. New SSOT getFleetAutopilotUserIds() decides
  enrollment for schedulers; missing rows count as on.
- The stale-run reaper only ran on /api/control page loads: 16 runs sat
  'waiting' for 51h until a human looked, then got stamped with
  fabricated 51h durations. New hourly reap-stale-runs cron reaps all
  users; finishedAt = startedAt + STALE_RUN_MINUTES (truthful).
- Hero headline was mode-derived ('Building' + pulsing green with 0
  working and 16/16 failed runs). deriveFleetPulse() (pure, tested)
  now derives Building / Waiting to dispatch / Stalled(+Review
  failures link) / Paused from live counts + latest outcomes.
- OutcomeStreak ✗ glyphs were unlabeled dead-ends; the streak now
  deep-links to /activity?window=week&project=<key>.
- box-runner logged 'connected · last poll 0s ago' every 2s to journald
  (~40k lines/day); now logs on state change + 15-min heartbeat.

Presenter tests 25/25 (6 new fleet-pulse cases).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-02 22:00:50 +02:00

85 lines
3.6 KiB
TypeScript

/**
* Headless FleetCrown box-runner.
*
* The desktop Fleet Runner's core — poller (drains pending_commands), pusher
* (runtime-state heartbeat), bridge subscriber (fast-path wake + presence), and
* owned PTYs (LocalPtyExecutor) — running as a systemd service on the always-on
* box, with NO Electron, window, or tray. This is the piece that deletes the
* "laptop must be on" dependency: dispatches execute server-side in
* FleetCrown-owned PTYs, presence stays online via the bridge SSE connection,
* and it survives web-app deploys because it is its own service.
*
* It reuses the EXACT desktop modules (desktop/src/main/{poller,pusher,...}) —
* that core is already Electron-free (pusher's lone app.getVersion() was lifted
* to FLEETCROWN_RUNNER_VERSION). Same control-plane contract as the desktop:
* HTTP + a ck_ runner token, no direct DB. Run via tsx from
* /opt/fleetcrown/runner. See docs/architecture/box-owned-pty-executor.md.
*/
import { startPoller, stopPoller, onPollerStatus, formatTrayTooltip } from "../desktop/src/main/poller";
import { startPusher, stopPusher } from "../desktop/src/main/pusher";
import { loadToken } from "../desktop/src/main/token-store";
import { APP_URL } from "@/config/brand";
const VERSION = process.env.FLEETCROWN_RUNNER_VERSION ?? "box";
const WEB = (process.env.FLEETCROWN_WEB_URL || "").trim() || APP_URL;
function log(msg: string): void {
// journald captures stdout; keep a stable prefix for `journalctl -u`.
console.log(`[box-runner] ${msg}`);
}
function main(): void {
const token = loadToken();
if (!token) {
console.error(
"[box-runner] no runner token at ~/.config/fleetcrown/fleet-runner-token — cannot authenticate. Exiting.",
);
process.exit(1);
}
log(`v${VERSION} → ${WEB} (token ${token.slice(0, 9)}…, PTY=${process.env.FLEETCROWN_RUNNER_PTY !== "false"})`);
// The desktop refreshes a tray tooltip from every poller status event
// (~every 2s). Headless, that verbatim stream wrote "connected · last poll
// 0s ago" to journald 40k times a day. Log only when the STATE changes
// (ignoring the volatile "last poll Ns ago" segment), plus a 15-minute
// liveness heartbeat so a quiet journal still proves the runner is alive.
let lastStatusLine = "";
let lastHeartbeatMs = 0;
onPollerStatus((s) => {
const line = formatTrayTooltip(s);
const stable = line.replace(/ · last poll \d+s ago/, "");
const now = Date.now();
if (stable !== lastStatusLine || now - lastHeartbeatMs > 15 * 60 * 1000) {
lastStatusLine = stable;
lastHeartbeatMs = now;
log(line);
}
});
// Poller starts the bridge subscriber (presence + fast-path wake) itself; the
// pusher publishes the runtime-state heartbeat. Together they are the whole
// runner: claim → execute in an owned PTY → stream → ack, presence online.
startPoller();
startPusher();
log("poller + pusher started; presence via bridge SSE");
let shuttingDown = false;
const shutdown = (sig: string): void => {
if (shuttingDown) return;
shuttingDown = true;
log(`${sig} → draining`);
try { stopPoller(); } catch { /* best-effort */ }
try { stopPusher(); } catch { /* best-effort */ }
// Let in-flight stop work settle, then exit so systemd sees a clean stop.
setTimeout(() => process.exit(0), 300);
};
process.on("SIGTERM", () => shutdown("SIGTERM"));
process.on("SIGINT", () => shutdown("SIGINT"));
// Nothing else holds the event loop open (poller/pusher run on timers), so
// keep the process alive explicitly until a signal arrives.
setInterval(() => { /* keep-alive */ }, 1 << 30);
}
main();