mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 09:20:03 +01:00
fix(home): wire runId end-to-end so cancelled runs actually get relabelled
The cancelledRunIds → user_abort relabel I shipped ine81d9eematches on event.runId. But the chain that produces worker.finished didn't preserve the runId: 1. home/worker.ts:executeDispatch injected the prompt but never wrote the /tmp/<slug>-run-<tab> sentinel that the stop hook reads to learn the runId. Cloud's cockpit-daemon.sh writes it; the local worker had been silently skipping it. 2. scripts/agent-hook-bridge.sh:emit_worker_finished didn't include runId on the event at all — the JSON template only had {v,id,ts,kind,project,outcome,durationMs,handoff}. It DID read the sentinel for duration_ms, then threw the runId on the floor. Net effect: every user-cancelled run was missing runId in worker.finished, the relabel never matched, and the cancel was recorded as whatever infer_outcome_v1 inferred (typically "partial").e81d9eewas working in tests but dead in production. Fix: • home/worker.ts: write event.runId to the sentinel before injectIntoTab. Wrapped in try/catch since failure to write a /tmp file shouldn't block dispatch — degrades gracefully to runId-less worker.finished. • scripts/agent-hook-bridge.sh: read the sentinel into run_id alongside the existing duration_ms read, build a separate {runId:...} object via jq, and merge it into the worker.finished via jq's `+` operator. Empty sentinel → empty object → merge is a no-op (matches the legacy shape, parseEvent accepts both). Validated the jq merge output through parseEvent: both shapes parse clean as worker.finished events. tsc + all home/ self-test suites still green.
This commit is contained in:
2 files changed
+38
-3
No files matched your search
@@ -26,6 +26,8 @@
|
||||
* 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";
|
||||
@@ -35,6 +37,19 @@ 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 = {
|
||||
@@ -130,6 +145,12 @@ function executeCancel(event: Event) {
|
||||
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",
|
||||
|
||||
@@ -106,25 +106,39 @@ emit_worker_finished() {
|
||||
todos_line=$(grep -m1 '^todos:' "$session_file" 2>/dev/null | sed 's/^todos:[[:space:]]*//')
|
||||
health_line=$(grep -m1 '^health:' "$session_file" 2>/dev/null | sed 's/^health:[[:space:]]*//')
|
||||
fi
|
||||
# Duration from /tmp/<slug>-run-<tab> sentinel mtime if present, else 0.
|
||||
local duration_ms=0 run_sentinel
|
||||
# Read duration AND runId from the /tmp/<slug>-run-<tab> sentinel that
|
||||
# cockpit-daemon.sh:execute_inject wrote at dispatch time. The runId is
|
||||
# required for state.applyEvent's cancelledRunIds → user_abort relabel
|
||||
# to match (otherwise a user-cancelled run gets recorded as partial in
|
||||
# recentOutcomes and drags computeConfidence down). Sentinel file
|
||||
# contains the runId on a single line; mtime is the start time.
|
||||
local duration_ms=0 run_id="" run_sentinel
|
||||
run_sentinel="$(_brand_tmp "run-${tab_name}")"
|
||||
if [ -f "$run_sentinel" ]; then
|
||||
local start_s now_s
|
||||
start_s=$(stat -c %Y "$run_sentinel" 2>/dev/null || echo 0)
|
||||
now_s=$(date +%s)
|
||||
if [ "$start_s" -gt 0 ]; then duration_ms=$(( (now_s - start_s) * 1000 )); fi
|
||||
run_id=$(cat "$run_sentinel" 2>/dev/null | tr -d '[:space:]')
|
||||
fi
|
||||
local outcome id ts
|
||||
outcome=$(infer_outcome_v1 "$done_line" "$tests_line" "$health_line" "$duration_ms")
|
||||
id=$(uuidgen 2>/dev/null || cat /proc/sys/kernel/random/uuid 2>/dev/null || echo "00000000-0000-4000-8000-000000000000")
|
||||
ts=$(date -u +%Y-%m-%dT%H:%M:%S.000Z)
|
||||
# runId is optional in the schema (parseEvent accepts missing). Build the
|
||||
# event with or without it via jq's `+` so an empty run_id (no sentinel)
|
||||
# produces a valid event sans runId — matches the legacy stop-hook shape.
|
||||
local runid_obj="{}"
|
||||
if [ -n "$run_id" ]; then
|
||||
runid_obj=$(jq -nc --arg runId "$run_id" '{runId:$runId}')
|
||||
fi
|
||||
jq -nc \
|
||||
--arg id "$id" --arg ts "$ts" --arg project "$tab_name" \
|
||||
--argjson durationMs "$duration_ms" --arg outcome "$outcome" \
|
||||
--arg done "$done_line" --arg next "$next_line" --arg tests "$tests_line" \
|
||||
--arg todos "$todos_line" --arg health "$health_line" \
|
||||
'{v:1, id:$id, ts:$ts, kind:"worker.finished", project:$project, outcome:$outcome, durationMs:$durationMs, handoff:{done:$done, next:$next, tests:$tests, todos:$todos, health:$health}}' \
|
||||
--argjson runid "$runid_obj" \
|
||||
'{v:1, id:$id, ts:$ts, kind:"worker.finished", project:$project, outcome:$outcome, durationMs:$durationMs, handoff:{done:$done, next:$next, tests:$tests, todos:$todos, health:$health}} + $runid' \
|
||||
>> "$log_path" 2>/dev/null || true
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user