mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 09:20:03 +01:00
fix(home): require --start flag to boot — naked invocations now exit cleanly
The foot-gun: \`npx tsx home/worker.ts | tail -3\` looks like a one-shot test invocation but actually starts a fs.watch + log tailer that runs forever after \`tail\` closes the pipe. Last session I tripped this twice: once with a bad self-test loop that ran the worker without --self-test (leaving a hot worker hours later), and once with a server smoke that left an orphan on port 3088. The hot worker then picked up a real bridge.dispatch from a separate smoke test and injected the next_best prompt straight into the Cockpit zellij tab — i.e. into the active agent session running the smoke test. Structural fix: all three long-running home/ processes now require an explicit --start flag. Naked invocations print a usage banner and exit 0, so accidental \`| tail -N\` or \`| head\` piping no longer leaves orphans. worker keeps its existing --self-test branch. • home/server.ts: --start guard at top, before any tailLog/listen • home/watcher.ts: --start guard before fs.watch on SESSIONS_DIR • home/worker.ts: --start | --self-test | (default: help+exit) • scripts/home-start.sh: passes --start to all three launches home/worker.ts self-tests still 12/12; tsc clean. State/decide/render/ projects suites untouched and still green.
This commit is contained in:
4 files changed
+38
-4
No files matched your search
@@ -35,6 +35,17 @@ import { renderPromptForDispatch } from "./render";
|
||||
import { resolveProjectPath, loadProjects } from "./projects";
|
||||
import { ORCHESTRATION_TASK_INTENT_IDS, type OrchestrationTaskIntentId } from "@/lib/orchestration";
|
||||
|
||||
// Boot-mode gate. Naked `npx tsx home/server.ts` (no flags) should NOT spin
|
||||
// up the HTTP server + fs.watch — otherwise a one-shot test invocation
|
||||
// (e.g. inside a `tail -3` pipe) leaves an orphan server eating port 3001
|
||||
// + watching the event log forever. Require an explicit --start.
|
||||
if (!process.argv.includes("--start")) {
|
||||
console.log(`${APP_NAME} brain — local HTTP server (Brain layer of the home/ stack).
|
||||
Usage: npx tsx home/server.ts --start
|
||||
Env: APP_HOME_PORT port to bind (default 3001)`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const LOG_PATH = path.join(os.homedir(), `.${APP_SLUG}`, "events.jsonl");
|
||||
const PORT = parseInt(process.env.APP_HOME_PORT ?? process.env.COCKPIT_HOME_PORT ?? "3001", 10);
|
||||
|
||||
|
||||
@@ -28,6 +28,16 @@ import { appendEvent } from "./emit";
|
||||
import { loadProjects, projectsConfPath } from "./projects";
|
||||
import type { Handoff } from "@/lib/events";
|
||||
|
||||
// Boot-mode gate — symmetric with server.ts + worker.ts. Naked
|
||||
// `npx tsx home/watcher.ts` is a help banner; --start opens the fs.watch.
|
||||
if (!process.argv.includes("--start")) {
|
||||
console.log(`${APP_NAME} watcher — Bridge layer of the home/ stack.
|
||||
Watches ~/.claude/sessions/*.md and emits worker.idle events into ~/.${APP_SLUG}/events.jsonl.
|
||||
Usage: npx tsx home/watcher.ts --start
|
||||
Env: APP_SESSIONS_DIR override sessions dir (default ~/.claude/sessions)`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Override via APP_SESSIONS_DIR for testing — production tails Claude's real dir.
|
||||
const SESSIONS_DIR =
|
||||
process.env.APP_SESSIONS_DIR ?? path.join(os.homedir(), ".claude", "sessions");
|
||||
|
||||
+13
-2
@@ -331,8 +331,19 @@ function selfTest() {
|
||||
if (fail > 0) process.exit(1);
|
||||
}
|
||||
|
||||
// Dispatch on argv
|
||||
// Dispatch on argv. Naked `npx tsx home/worker.ts` is the foot-gun that
|
||||
// started this whole pile-on of strays — a test loop piping its output
|
||||
// through `tail -3` would leave a hot worker injecting random prompts
|
||||
// into zellij tabs hours later. Require an explicit flag so accidental
|
||||
// one-shot invocations exit cleanly.
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
if (process.argv.includes("--self-test")) selfTest();
|
||||
else start();
|
||||
else if (process.argv.includes("--start")) start();
|
||||
else {
|
||||
console.log(`${APP_NAME} worker — Consumer layer of the home/ stack.
|
||||
Tails ~/.${APP_SLUG}/events.jsonl and injects bridge.dispatch prompts into zellij tabs.
|
||||
Usage: npx tsx home/worker.ts --start (boot the consumer)
|
||||
npx tsx home/worker.ts --self-test (run inline tests, no I/O)`);
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
@@ -51,11 +51,13 @@ trap shutdown SIGINT SIGTERM EXIT
|
||||
cd "$REPO_ROOT"
|
||||
|
||||
# Each process tags its output so the merged log is readable when grepped.
|
||||
# --start is required by all three since 2026-05 — naked `npx tsx home/X.ts`
|
||||
# now exits with a usage banner so accidental one-shots don't leave orphans.
|
||||
launch() {
|
||||
local name="$1" cmd="$2"
|
||||
( exec npx tsx "$cmd" 2>&1 | sed -u "s/^/[${name}] /" >> "$LOG_FILE" ) &
|
||||
( exec npx tsx "$cmd" --start 2>&1 | sed -u "s/^/[${name}] /" >> "$LOG_FILE" ) &
|
||||
PIDS+=("$!")
|
||||
log "started $name (pid $!) → $cmd"
|
||||
log "started $name (pid $!) → $cmd --start"
|
||||
}
|
||||
|
||||
launch server home/server.ts
|
||||
|
||||
Reference in new issue
Block a user