mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 06:17:23 +01:00
- The bell counted feedback waiting for triage, which Home's "To review" already shows: "9+" beside "Feedback to triage 13" on one screen. It now counts only alerts, and exactly what its panel lists (it also counted new_feedback alerts the panel hid). - Resume continues Codex conversations too (`codex resume --last`, scoped to the folder by Codex; falls back to a fresh session), from the Sessions page and when the cloud builder reopens sessions after an update. Cursor stays Start: its resume reaches chats from every project. Runner 0.8.49. - Projects: no subtitle restating the sort; the sort is the even segmented row with its meaning under it; "Not connected" sits once in the facts line instead of orange capitals beside every name; "health 5/10" reads "5 of 10 checks", the rail's own word. Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx Co-authored-by: Claude <noreply@anthropic.com>
172 lines
6.4 KiB
TypeScript
172 lines
6.4 KiB
TypeScript
/**
|
|
* Headless Loki 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
|
|
* Loki-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 LOKI_RUNNER_VERSION). Same control-plane contract as the desktop:
|
|
* HTTP + a ck_ runner token, no direct DB. Run via tsx from
|
|
* /opt/loki/runner. See docs/architecture/box-owned-pty-executor.md.
|
|
*/
|
|
import { readFileSync } from "fs";
|
|
import { dirname, join } from "path";
|
|
import { fileURLToPath } from "url";
|
|
import {
|
|
startPoller,
|
|
stopPoller,
|
|
onPollerStatus,
|
|
formatTrayTooltip,
|
|
} from "../desktop/src/main/poller";
|
|
import { pushNow, startPusher, stopPusher } from "../desktop/src/main/pusher";
|
|
import { loadToken } from "../desktop/src/main/token-store";
|
|
import { APP_URL } from "@/config/brand";
|
|
import { startWatcher } from "../home/watcher";
|
|
import { homedir } from "os";
|
|
import { enableTabMemory, restoreTabs } from "../desktop/src/main/tab-memory";
|
|
import { isPtyBacked, launchAgentPty } from "../desktop/src/main/pty-runtime";
|
|
import { newestLog } from "../desktop/src/main/transcript-streamer";
|
|
|
|
/**
|
|
* Version comes from the deployed code itself (desktop/package.json — the
|
|
* modules this runner actually executes), never from a hand-maintained env.
|
|
* The systemd unit pinned LOKI_RUNNER_VERSION=box-0.8.9 and nobody
|
|
* bumped it across three releases, so Settings showed a version the box was
|
|
* not running. Env stays as fallback only for the odd manual invocation.
|
|
*/
|
|
function boxRunnerVersion(): string {
|
|
try {
|
|
const pkgPath = join(dirname(fileURLToPath(import.meta.url)), "..", "desktop", "package.json");
|
|
const pkg = JSON.parse(readFileSync(pkgPath, "utf8")) as { version?: string };
|
|
if (pkg.version) return `box-${pkg.version}`;
|
|
} catch {
|
|
/* fall through to env */
|
|
}
|
|
return process.env.LOKI_RUNNER_VERSION ?? "box";
|
|
}
|
|
|
|
const VERSION = boxRunnerVersion();
|
|
// The pusher reports whatever this env holds (lazily) — publish the derived
|
|
// version so the heartbeat and Settings agree with the log line below.
|
|
process.env.LOKI_RUNNER_VERSION = VERSION;
|
|
// Bridge presence must register as cloud. The systemd unit sets this too, but
|
|
// deploy syncs code without rewriting the unit — if the env is missing, a
|
|
// default of "local" in bridge-subscriber made /terminal say the cloud builder
|
|
// was offline while this process was the one shipping the work.
|
|
if (!process.env.LOKI_RUNNER_PRESENCE_CHANNEL?.trim()) {
|
|
process.env.LOKI_RUNNER_PRESENCE_CHANNEL = "cloud";
|
|
}
|
|
const WEB = (process.env.LOKI_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/loki/fleet-runner-token — cannot authenticate. Exiting.",
|
|
);
|
|
process.exit(1);
|
|
}
|
|
log(`v${VERSION} → ${WEB} (token ${token.slice(0, 9)}…)`);
|
|
|
|
// 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);
|
|
}
|
|
});
|
|
|
|
// Unlike the desktop, the box has no static projects.conf: its projects are
|
|
// cloned on demand. Watch every Loki-owned handoff and push immediately
|
|
// so a completed run never waits for the five-minute liveness heartbeat.
|
|
const watcher = startWatcher({
|
|
acceptUnregistered: true,
|
|
onIdle: () => {
|
|
void pushNow();
|
|
},
|
|
});
|
|
|
|
// Poller starts the bridge subscriber (presence + fast-path wake) itself; the
|
|
// pusher publishes runtime state and the watcher pushes completion handoffs.
|
|
startPoller();
|
|
startPusher();
|
|
log("poller + pusher started; presence via bridge SSE");
|
|
|
|
// Reopen the sessions that were open when this process last stopped — a
|
|
// deploy's restart ends them, and Claude Code resumes each conversation.
|
|
enableTabMemory(join(homedir(), ".loki", "open-tabs.json"));
|
|
void restoreTabs({
|
|
isLive: isPtyBacked,
|
|
// Codex finds its own newest conversation in the folder (resumeLatest).
|
|
launch: (tab, dir, agent, sessionId) =>
|
|
launchAgentPty(tab, dir, agent, undefined, sessionId, agent === "codex"),
|
|
sessionFor: async (dir) => {
|
|
const log = await newestLog(dir);
|
|
return log
|
|
? (log.file
|
|
.split("/")
|
|
.pop()
|
|
?.replace(/\.jsonl$/, "") ?? null)
|
|
: null;
|
|
},
|
|
log,
|
|
}).then((restored) => {
|
|
if (restored.length > 0) void pushNow();
|
|
});
|
|
|
|
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 */
|
|
}
|
|
try {
|
|
watcher.close();
|
|
} 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();
|