fix(feedback): tell the truth when the builder goes offline after delivery (#769)

A dispatch whose builder goes down mid-run read "Starting", then "Needs you ·
Open Terminal — or Restart Fleet Runner" — pointing the operator at a terminal
on a machine that is switched off. Loki already knew better: the snapshot
carries builderChannel and per-channel presence. Two things kept that knowledge
off the row.

The phase guard was `!run.deliveredAt && run.builderOffline`. The runner stamps
executedAt on its inject-ack, seconds after the prompt lands, so a claimed
command counts as delivered for nearly the whole run and the honest branch could
only ever fire in the opening moments. Gate it on PROGRESS instead: no bytes
have arrived and the builder is down NOW is the whole diagnosis, delivered or
not. A run that is actually streaming keeps its own reading, so a lapsed
heartbeat cannot mute a live agent.

builderChannel came only from an OPEN pending_commands row, and that row closes
on the same early ack. With the channel unknown the offline test fell through to
"any builder will do", which reported a local run healthy because the cloud box
was up — the channel-aware code added for exactly this case was unreachable in
practice. Fall back to the project's stored routing decision (locus lock →
builder_pref → cloud floor), the same answer the dispatcher itself used.

The diagnostic now separates claimed-then-silent from never-claimed: if the
prompt already reached that machine, the honest ask is to bring it back, not to
re-dispatch blind.

Pinned in scripts/test/feedback-work-phase.ts — nothing covered builderOffline
before, which is how the guard drifted into dead code.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
CatoandClaude Opus 5 authored and GitHub committed 2026-09-17 06:47:06 -07:00
1 parent 918f3c0e90
commit c116518216
3 files changed
+105 -7

No files matched your search

+79
View File
@@ -474,3 +474,82 @@ console.log("✓ feedback work-phase tests passed");
"verified:true alone (no progress beat yet) is Working — post-prompt evidence",
);
}
// Builder offline outranks silence — and stays reachable after delivery.
//
// The runner stamps executedAt on its inject-ack, so a claimed command is
// "delivered" for nearly the whole run. Gating the offline branch on
// `!deliveredAt` made it unreachable in practice: a laptop closed one minute
// into a run read "Starting", then "Open Terminal — or Restart Fleet Runner",
// pointing the operator at a machine that was off. Pinned here because nothing
// pinned it before, which is how the guard drifted into dead code.
{
const now = Date.now();
const offlineAfterDelivery = deriveFeedbackWork(
FEEDBACK_STATUS.DISPATCHED,
snap({
startedAt: new Date(now - 5 * 60_000),
deliveredAt: new Date(now - 4 * 60_000).toISOString(),
lastProgressAt: null,
builderOffline: true,
builderChannel: "local",
}),
now,
);
assert.equal(
offlineAfterDelivery.phase,
FEEDBACK_WORK_PHASE.STUCK,
"delivered + builder offline + no output is STUCK, not Starting",
);
assert.match(
offlineAfterDelivery.detail ?? "",
/Fleet Runner/,
"a local run on an offline computer asks for Fleet Runner, never a terminal on a dead machine",
);
assert.match(
offlineAfterDelivery.diagnostic ?? "",
/already handed to it/,
"claimed-then-silent says the prompt already reached that machine",
);
const offlineBeforeDelivery = deriveFeedbackWork(
FEEDBACK_STATUS.DISPATCHED,
snap({
startedAt: new Date(now - 60_000),
builderOffline: true,
builderChannel: "cloud",
}),
now,
);
assert.equal(
offlineBeforeDelivery.phase,
FEEDBACK_WORK_PHASE.STUCK,
"never-claimed + builder offline is still STUCK",
);
assert.doesNotMatch(
offlineBeforeDelivery.diagnostic ?? "",
/already handed to it/,
"never-claimed must not claim the prompt reached the builder",
);
// The guard is about SILENCE, not about presence: a run that is streaming
// keeps its own reading even if the presence table says offline (a heartbeat
// can lapse while the PTY is very much alive).
const streamingDespiteOffline = deriveFeedbackWork(
FEEDBACK_STATUS.DISPATCHED,
snap({
startedAt: new Date(now - 5 * 60_000),
deliveredAt: new Date(now - 4 * 60_000).toISOString(),
lastProgressAt: new Date(now - 5_000).toISOString(),
injectVerified: true,
builderOffline: true,
builderChannel: "local",
}),
now,
);
assert.equal(
streamingDespiteOffline.phase,
FEEDBACK_WORK_PHASE.WORKING,
"bytes now outrank a stale presence row — offline must not mute a live agent",
);
}
+10 -4
View File
@@ -3,6 +3,7 @@ import { getLatestRunEventKinds } from "@/db/queries/run-events";
import { getOpenPendingByRunIds, getInjectAcksByRunIds } from "@/db/queries/pending-commands";
import { getBuilderPresence } from "@/db/queries/runner-presence";
import { getUserProjectsByEntityIds } from "@/db/queries/user-projects";
import { pickDispatchChannel } from "@/lib/execution-access";
import type { FeedbackListItem } from "@/db/queries/site-feedback";
import {
deriveFeedbackWork,
@@ -194,10 +195,15 @@ export async function attachFeedbackWork<T extends FeedbackListItem>(
snap.localOnline = presence.local;
snap.cloudOnline = presence.cloud;
// Channel-aware offline: local queue → need Fleet Runner; cloud → need box.
// Unknown channel → any builder. Matches claimNextPendingCommand filters.
const ch = snap.builderChannel;
snap.builderOffline =
ch === "local" ? !presence.local : ch === "cloud" ? !presence.cloud : !presence.any;
// The open pending row is the truth while it exists, but the runner stamps
// executedAt on its inject-ack, so for most of a run there is no row left
// to ask and the channel read back as unknown — which fell through to "any
// builder will do" and reported a local run HEALTHY because the cloud box
// was up. The project's stored routing decision (locus lock → builder_pref
// → cloud floor) is the same answer the dispatcher used, so ask it.
const ch = snap.builderChannel ?? pickDispatchChannel(projects.get(item.projectId));
snap.builderChannel = ch;
snap.builderOffline = ch === "local" ? !presence.local : !presence.cloud;
const ack = injectAcks.get(row.id);
if (ack) {
if (snap.injectVerified == null && ack.verified != null) snap.injectVerified = ack.verified;
+16 -3
View File
@@ -376,8 +376,21 @@ function derivePhase(
const ageMs = now - run.startedAt.getTime();
// Builder offline is known NOW — do not park under "moving on its own".
// Telegram + dig-in; auto-retry cron may cold-start once Hermes/cloud returns.
if (!run.deliveredAt && run.builderOffline) {
//
// Gated on PROGRESS, not on delivery. The runner stamps executedAt on its
// inject-ack (early, right after the prompt lands), so a claimed command is
// "delivered" for almost the whole run; guarding on `!deliveredAt` meant the
// branch could only ever fire in the first seconds, and a laptop closed one
// minute in read "Starting" and then "Open Terminal — or Restart Fleet
// Runner" — a terminal on a machine that is off. No bytes ever arrived and
// the builder is down NOW: that is the whole diagnosis, delivered or not.
// A run that did stream keeps its own reading below; this never touches it.
if (!run.lastProgressAt && run.builderOffline) {
const localQueue = run.builderChannel === "local";
// Claimed-then-silent is a different sentence from never-claimed: the work
// may have started on that machine, so the honest ask is to bring it back
// rather than to re-dispatch blind.
const claimed = !!run.deliveredAt;
return {
phase: FEEDBACK_WORK_PHASE.STUCK,
label: "Needs you",
@@ -388,8 +401,8 @@ function derivePhase(
diagnostic: run.hostedPending
? "Cloud builder offline; hosted Hermes was queued but has not claimed yet."
: localQueue
? "This computer (Fleet Runner) is offline — no agent session until it reconnects, or switch the project to Cloud."
: "Cloud builder offline — no agent session will appear until loki-box-runner is online (or Hermes accepts).",
? `This computer (Fleet Runner) is offline${claimed ? " and the prompt was already handed to it — no output since" : " — no agent session until it reconnects"}, or switch the project to Cloud.`
: `Cloud builder offline${claimed ? " and the prompt was already handed to it — no output since" : " — no agent session will appear until loki-box-runner is online (or Hermes accepts)"}.`,
};
}
if (!run.deliveredAt && ageMs > STARTING_MS) {