Files
loki/home/worker.ts
T
g-but 27f31cb664 feat(handoff): agent-driven auto-inject gate via status field — model-agnostic
User asked: stop firing auto-inject prompts while the agent is mid-task;
inject should only fire when the agent has done everything and now
needs a nudge. They also called out the broader goal that this should
work for any model (claude, codex, gemini, openclaw, or future
adapters) — not just claude — since today's behavior is implicitly
claude-coupled through ~/.claude/sessions/*.md and the stop hook.

Added a new \`status\` field to the canonical handoff format. The
agent self-reports lifecycle state per turn:

  status: ready    — task fully done, auto-inject may proceed
  status: working  — agent is mid-task, suppress auto-inject

Missing / undefined / any-other-value is treated as NOT ready. This
inverts today's implicit default (auto-fire fires on every turn end)
so the agent must explicitly signal it's done with everything before
the beacon/ReadyBanner countdown fires the next prompt. Matches the
user's stated intent literally.

Why this is model-agnostic: the field lives in the canonical
ORCHESTRATION_TASK_SUMMARY_FIELDS constant that ALL adapters' handoff
templates read from (claude via buildPromptWithSession, openclaw via
renderSharedHandoffBlock, codex/gemini will inherit when their
handoff plumbing is wired). The parsers (parseSessionFile,
parseSessionText, home/watcher.ts:parseHandoff) all pick it up
automatically. Any new adapter that writes the standard handoff
format gets the same gating.

Files touched:

  • src/lib/orchestration/contract.ts — added 'status' to
    ORCHESTRATION_TASK_SUMMARY_FIELDS; OrchestrationTaskSummary makes
    status optional for back-compat.
  • src/lib/events.ts — Handoff zod schema gains status (defaults to
    "") so home/ events with status round-trip cleanly.
  • src/lib/control-types.ts — SessionState.status is optional;
    legacy construction sites that don't yet plumb session_status can
    omit it and naturally suppress auto-fire.
  • src/lib/agent-config.ts:buildPromptWithSession — new status line
    in the handoff template prose (the one claude reads each turn).
  • src/lib/orchestration/renderers.ts:renderSharedHandoffBlock —
    same line for openclaw.
  • src/lib/session-content.ts:parseSessionText — beacon DB parser
    extracts status.
  • home/watcher.ts:FIELDS — home/ watcher's handoff parser includes
    status (cascades to worker.idle events).
  • src/hooks/use-project-card-actions.ts:handleAutoInject —
    explicit gate: only fires when status === 'ready' (or
    health-critical for recovery dispatches).
  • src/app/beacon/[id]/BeaconClient.tsx:handleAutoInject — same
    gate, parsing status out of session.sessionContent via
    parseSessionText (BeaconSession carries raw md text).
  • src/app/api/orchestration/runs/[id]/finish/route.ts — finish
    body summary preserves status.
  • home/{decide,emit,state,worker}.ts test fixtures — added
    status: "" to all Handoff literals so the new required-after-
    default field type-checks.

tsc clean, eslint clean, npm run test:home 82/82.

Transition note: until adapters start emitting status in their
handoffs, auto-inject will suppress for everyone. This is the user's
explicit choice ("only fire when the agent has done everything").
The handoff template prose now tells the agent to write the field,
so going forward agents will include it.

NOT shipped in this commit (deliberate scope cut, queued for next):
  • DB column session_status on project_states (mirror of the new
    field) — without it, control/stream/route.ts can't surface the
    field server-side. Today the BeaconClient parses status directly
    from raw session.md content; the broader cloud /control flow
    will get accurate status once the column lands.
  • Bash side (agent-hook-bridge.sh:emit_worker_finished) reading
    status from session.md and including in worker.finished events.
    The user has denied prior edits to that file; happy to ship if
    they want or leave it for them to land.
2026-05-20 09:23:41 +02:00

392 lines
16 KiB
TypeScript

#!/usr/bin/env -S npx tsx
/**
* Worker — M8 + M9 fix.
*
* Tails the same JSONL log the Brain reads, but with a different lens: when
* a bridge.dispatch event arrives that hasn't already been started, inject
* the prompt into the right zellij tab and emit a worker.started event back
* to the log. On injection failure, emit worker.crashed.
*
* This is the consumer half of the dispatch loop. Together with M3's
* watcher (worker.idle from session.md changes) and M4's stop-hook
* emission (worker.finished), the loop now closes locally:
*
* user/UI → POST /api/dispatch → bridge.dispatch event
* → worker (this process) → injectIntoTab → claude runs
* → claude writes session.md → M3 watcher → worker.idle event
* → claude stop hook → M4 emit_worker_finished → worker.finished event
* → Brain projects all of it back into state
*
* Idempotency: on boot, the replay phase builds a set of runIds that
* already have a worker.started event in the log. The live phase only acts
* on dispatches whose runId is NOT in that set — so restarting the worker
* never re-injects.
*
* Run: npx tsx home/worker.ts
* Test: npx tsx home/worker.ts --self-test
*/
import fs from "node:fs";
import path from "node:path";
import { tailLog, type EventPhase } from "./log";
import { appendEvent, LOG_PATH } from "./emit";
import { injectIntoTab, sendRawKey } from "@/lib/zellij";
import { APP_NAME, APP_SLUG } from "@/config/brand";
import type { Event } from "@/lib/events";
/** Linux Ctrl+C raw key code. Same value used by switch-agent for hard interrupt. */
const CTRL_C = 3;
/**
* /tmp/<slug>-run-<tab> sentinel: agent-hook-bridge.sh:emit_worker_finished
* reads the runId from this file at session-stop time and includes it on
* the worker.finished event. Without that, state.applyEvent has no runId
* to match against cancelledRunIds, so a user-cancelled run gets recorded
* as whatever infer_outcome_v1 inferred (typically "partial") instead of
* user_abort — confidence drops on every cancel. Mirror the legacy
* cockpit-daemon.sh write so the home/ stack speaks the same protocol.
*/
function runSentinelPath(project: string): string {
return path.join("/tmp", `${APP_SLUG}-run-${project}`);
}
// ── State held by the worker process ────────────────────────────────────────
export type WorkerState = {
/** runIds that have a worker.started event downstream — already handled. */
startedRunIds: Set<string>;
/** bridge.dispatch events seen during replay with no matching worker.started yet.
* Anything still here after replay completes is genuine crash recovery. */
pendingDispatches: Map<string, Event>;
};
export function makeWorkerState(): WorkerState {
return { startedRunIds: new Set(), pendingDispatches: new Map() };
}
/**
* Pure state-transition for an incoming event. No I/O, no injection.
* Replay events build state. Live events return a `dispatch` request
* the caller is supposed to execute.
*/
export function applyEvent(
state: WorkerState,
event: Event,
phase: EventPhase,
): { dispatch?: Event; cancel?: Event } {
// worker.started AND worker.crashed both terminate a runId from the
// worker's POV — one succeeded, the other failed permanently. Either
// way, don't re-inject. Without crashed-as-terminator, a failed
// dispatch (e.g. wrong tab name) gets retried on every worker restart
// and emits a fresh worker.crashed each time.
if ((event.kind === "worker.started" || event.kind === "worker.crashed") && event.runId) {
state.startedRunIds.add(event.runId);
state.pendingDispatches.delete(event.runId);
return {};
}
// bridge.cancel ALSO terminates a runId: drop it from pendingDispatches so
// crash-recovery on next boot doesn't re-fire a dispatch the user already
// killed. Live cancels also signal the caller to send Ctrl+C to the tab.
if (event.kind === "bridge.cancel") {
state.startedRunIds.add(event.runId);
state.pendingDispatches.delete(event.runId);
return phase === "live" ? { cancel: event } : {};
}
if (event.kind !== "bridge.dispatch") return {};
if (state.startedRunIds.has(event.runId)) return {}; // already handled
if (phase === "replay") {
// Track dispatches without a matching worker.started yet. If one
// appears later in the replay, the branch above clears it; what's
// left at replay's end is genuine crash recovery.
state.pendingDispatches.set(event.runId, event);
return {};
}
// Live event — caller should inject now.
state.pendingDispatches.delete(event.runId);
return { dispatch: event };
}
// ── Side-effect: actually inject + emit worker.started ──────────────────────
/**
* Side-effect: send Ctrl+C to the project's tab. Symmetric to executeDispatch —
* worker is the only process that touches zellij, regardless of intent.
*
* The stop hook that runs when the agent shuts down will still emit a
* worker.finished event with whatever outcome infer_outcome_v1 derives from
* the session.md at that moment (typically "partial" when done is empty),
* so the brain's state catches up naturally without us forging a finish here.
*/
function executeCancel(event: Event) {
if (event.kind !== "bridge.cancel") return;
try {
sendRawKey(event.project, CTRL_C);
console.log(`[worker] sent Ctrl+C to project=${event.project} runId=${event.runId} reason=${event.reason}`);
} catch (e) {
// Symmetric with executeDispatch's inject-failure branch: emit
// worker.crashed so the brain's state.applyEvent sets lastError and
// the UI surfaces a red error message. Without this, a failed cancel
// is silent — state.applyEvent already cleared currentRun
// optimistically on the bridge.cancel, so the UI would show the
// project as idle while the agent might still be running.
const msg = e instanceof Error ? e.message : String(e);
appendEvent({
kind: "worker.crashed",
project: event.project,
runId: event.runId,
error: `cancel failed: ${msg}`,
});
console.error(`[worker] cancel failed runId=${event.runId}: ${msg}`);
}
}
function executeDispatch(state: WorkerState, event: Event) {
if (event.kind !== "bridge.dispatch") return;
try {
// Drop the runId where the stop hook will read it. Fire-and-forget —
// failure here doesn't block the inject; the stop hook just falls
// back to a runId-less worker.finished and the relabel doesn't match
// (degraded mode, not broken).
try { fs.writeFileSync(runSentinelPath(event.project), event.runId); }
catch { /* read-only /tmp or weird filesystem — keep going */ }
injectIntoTab(event.project, event.prompt);
appendEvent({
kind: "worker.started",
project: event.project,
// Echo the brain's adapter choice if it set one, else claude default
// (matches the existing default agent in src/lib/agent-preferences.ts).
adapter: event.adapter ?? "claude",
intent: event.intent,
runId: event.runId,
});
state.startedRunIds.add(event.runId);
console.log(`[worker] injected runId=${event.runId} intent=${event.intent} project=${event.project}`);
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
appendEvent({
kind: "worker.crashed",
project: event.project,
runId: event.runId,
error: `inject failed: ${msg}`,
});
// Inject failed → no agent is running → no stop hook will ever fire to
// consume this sentinel. Without cleanup, the stale runId sits in /tmp
// forever; the next manual claude session in this tab would write a
// session.md, the stop hook would read this sentinel, and tag an
// unrelated worker.finished with our crashed runId. Same trap that
// motivated agent-hook-bridge.sh:finish_orchestration_run's rm — apply
// it here too so the dispatch-fail path doesn't leak state.
try { fs.rmSync(runSentinelPath(event.project), { force: true }); }
catch { /* sentinel write may have failed too; nothing to clean up */ }
console.error(`[worker] inject failed runId=${event.runId}: ${msg}`);
}
}
// ── Main boot ────────────────────────────────────────────────────────────────
function start() {
const state = makeWorkerState();
const handle = tailLog(
LOG_PATH,
(event, phase) => {
const { dispatch, cancel } = applyEvent(state, event, phase);
if (dispatch) executeDispatch(state, dispatch);
if (cancel) executeCancel(cancel);
},
(err) => console.error("[worker]", err.message),
);
// After replay completes, fire any pending dispatches (genuine crash
// recovery). tailLog's replay is synchronous, so by the time this runs
// the state map is settled.
setImmediate(() => {
if (state.pendingDispatches.size === 0) return;
console.log(`[worker] ${state.pendingDispatches.size} dispatch(es) pending from previous boot — executing`);
for (const event of state.pendingDispatches.values()) executeDispatch(state, event);
state.pendingDispatches.clear();
});
console.log(`[worker] ${APP_NAME} dispatch consumer tailing ~/.${APP_SLUG}/events.jsonl`);
const shutdown = (sig: string) => {
console.log(`[worker] ${sig} — shutting down`);
handle.close();
process.exit(0);
};
process.on("SIGINT", () => shutdown("SIGINT"));
process.on("SIGTERM", () => shutdown("SIGTERM"));
}
// ── Self-test ────────────────────────────────────────────────────────────────
// Run with: npx tsx home/worker.ts --self-test
// Drives applyEvent() with canned event sequences and asserts on the state.
// Doesn't boot the tail or call injectIntoTab — pure state-transition only.
function selfTest() {
const fresh = () => makeWorkerState();
const dispatchEvent = (runId: string): Event => ({
v: 1, id: runId, ts: "2026-01-01T00:00:00Z",
kind: "bridge.dispatch", project: "Test", intent: "next_best",
prompt: "go", runId, autonomy: "confirm",
});
const startedEvent = (runId: string): Event => ({
v: 1, id: `started-${runId}`, ts: "2026-01-01T00:01:00Z",
kind: "worker.started", project: "Test", adapter: "claude",
intent: "next_best", runId,
});
const crashedEvent = (runId: string): Event => ({
v: 1, id: `crashed-${runId}`, ts: "2026-01-01T00:01:00Z",
kind: "worker.crashed", project: "Test", runId,
error: "inject failed: tab not found",
});
const cancelEvent = (runId: string): Event => ({
v: 1, id: `cancel-${runId}`, ts: "2026-01-01T00:02:00Z",
kind: "bridge.cancel", project: "Test", runId, reason: "user clicked Cancel",
});
type Case = { name: string; run: () => boolean };
const cases: Case[] = [
{
name: "live dispatch returns the event for caller to execute",
run: () => {
const s = fresh();
const r = applyEvent(s, dispatchEvent("a"), "live");
return r.dispatch?.kind === "bridge.dispatch" && s.pendingDispatches.size === 0;
},
},
{
name: "replay dispatch goes to pending, not executed",
run: () => {
const s = fresh();
const r = applyEvent(s, dispatchEvent("a"), "replay");
return r.dispatch === undefined && s.pendingDispatches.has("a");
},
},
{
name: "BUG FIX — replayed dispatch followed by replayed worker.started is cleared from pending",
run: () => {
const s = fresh();
applyEvent(s, dispatchEvent("a"), "replay");
applyEvent(s, startedEvent("a"), "replay");
return s.pendingDispatches.size === 0 && s.startedRunIds.has("a");
},
},
{
name: "replayed dispatch without a worker.started stays in pending (crash recovery)",
run: () => {
const s = fresh();
applyEvent(s, dispatchEvent("a"), "replay");
applyEvent(s, dispatchEvent("b"), "replay");
applyEvent(s, startedEvent("a"), "replay");
// 'a' resolved, 'b' is the crash-recovery candidate.
return s.pendingDispatches.size === 1 && s.pendingDispatches.has("b");
},
},
{
name: "live dispatch is skipped if startedRunIds already has it",
run: () => {
const s = fresh();
applyEvent(s, startedEvent("a"), "replay");
const r = applyEvent(s, dispatchEvent("a"), "live");
return r.dispatch === undefined;
},
},
{
name: "BUG FIX — worker.crashed terminates the runId so replay doesn't re-inject failed dispatches",
run: () => {
const s = fresh();
applyEvent(s, dispatchEvent("a"), "replay");
applyEvent(s, crashedEvent("a"), "replay");
// After a crashed event, the run is done — no pending recovery, no
// chance of a live re-dispatch firing again.
return s.pendingDispatches.size === 0 && s.startedRunIds.has("a");
},
},
{
name: "live bridge.dispatch is skipped when a prior crash already terminated the runId",
run: () => {
const s = fresh();
applyEvent(s, crashedEvent("a"), "replay");
const r = applyEvent(s, dispatchEvent("a"), "live");
return r.dispatch === undefined;
},
},
{
name: "live bridge.cancel returns the event so caller sends Ctrl+C",
run: () => {
const s = fresh();
applyEvent(s, startedEvent("a"), "live");
const r = applyEvent(s, cancelEvent("a"), "live");
return r.cancel?.kind === "bridge.cancel" && r.cancel.runId === "a";
},
},
{
name: "replay bridge.cancel does NOT emit a Ctrl+C (history, not now)",
run: () => {
const s = fresh();
const r = applyEvent(s, cancelEvent("a"), "replay");
return r.cancel === undefined && s.startedRunIds.has("a");
},
},
{
name: "bridge.cancel during replay drops a pending dispatch (don't crash-recover a cancelled run)",
run: () => {
const s = fresh();
applyEvent(s, dispatchEvent("a"), "replay");
applyEvent(s, cancelEvent("a"), "replay");
return s.pendingDispatches.size === 0 && s.startedRunIds.has("a");
},
},
{
name: "live dispatch is skipped when a prior cancel already terminated the runId",
run: () => {
const s = fresh();
applyEvent(s, cancelEvent("a"), "live");
const r = applyEvent(s, dispatchEvent("a"), "live");
return r.dispatch === undefined;
},
},
{
name: "unrelated event types are no-ops on state",
run: () => {
const s = fresh();
const idle: Event = {
v: 1, id: "i1", ts: "2026-01-01T00:00:00Z",
kind: "worker.idle", project: "Test",
handoff: { status: "", done:"", next: "", tests: "", todos: "", health: "good" },
};
applyEvent(s, idle, "live");
return s.startedRunIds.size === 0 && s.pendingDispatches.size === 0;
},
},
];
let pass = 0, fail = 0;
for (const c of cases) {
if (c.run()) { console.log(` ✓ ${c.name}`); pass++; }
else { console.log(` ✗ ${c.name}`); fail++; }
}
console.log(`\n${pass}/${pass + fail} passed`);
if (fail > 0) process.exit(1);
}
// 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 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);
}
}