mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 05:17:01 +01:00
Risk 3 of the 2026-09-17 review: "Feedback attach-work falls back from closed pending command to current project routing preference. A current setting cannot prove where a historical attempt ran." Exactly right. The channel resolution was: pending row (open) ?? pickDispatchChannel(project) The open row is gone for most of a run's life — the runner stamps executedAt on its inject-ack, within ~8s of injecting — so in practice the second half decided almost every finished run. And that half is a LIVE PREFERENCE. Flip a project from This computer to Cloud and every finished local run starts claiming it ran on the box, with its offline verdict computed against a machine it never touched. The fix needed no new state, because the record already existed. The executed pending_commands row carries the channel it was handed to, in its payload, and getInjectAcksByRunIds was already reading exactly those rows for the same runs — it just never selected the field. So the immutable execution identity of the attempt was one property away the whole time. Now the order is an order of EVIDENCE: 1. the open pending row — this attempt, right now 2. the executed command — this attempt, recorded when it was handed 3. the project's setting — where a NEW dispatch would go today Only the first two are about the attempt being described. The third stays as the last resort because a run that never reached a builder has no recorded channel at all, and there the dispatcher's own rule is the best available guess. The channel lives in the jsonb payload rather than a column, so it is validated the same way the open-row reader already validates it: an unrecognised value is not a channel. Tests: two cases — a project now set to Cloud whose attempt ran local (the recorded fact wins, and the offline verdict follows it), and an attempt with no recorded channel (falls back). Proved by mutation: restoring the old expression fails "the recorded attempt outranks the project's current preference". tsc clean, lint 0 errors, test:unit 204/204. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
188 lines
6.2 KiB
TypeScript
188 lines
6.2 KiB
TypeScript
// The one answer to "which builder is this run waiting on, and is it up".
|
|
//
|
|
// Four surfaces ask it: the inbox row, Watch, Terminal's Loki rail, and
|
|
// Implement's duplicate-guard. They used to answer three different ways —
|
|
// Terminal kept a stale copy that fell back to "any builder will do", and
|
|
// Watch and the guard hydrated nothing, so neither could ever see an offline
|
|
// builder. The guard gates a RETRY, so the row said "Needs you — Retry" while
|
|
// Implement answered "Already on this". These pins exist so that cannot recur.
|
|
import assert from "node:assert/strict";
|
|
import { applyRunContext } from "../../src/lib/feedback/run-context";
|
|
import {
|
|
deriveFeedbackWork,
|
|
FEEDBACK_WORK_PHASE,
|
|
type FeedbackRunSnapshot,
|
|
} from "../../src/lib/feedback/work-phase";
|
|
import { FEEDBACK_STATUS } from "../../src/lib/constants/statuses";
|
|
import { ORCH_STATE } from "../../src/lib/orchestration/contract";
|
|
|
|
const now = Date.now();
|
|
|
|
function snap(over: Partial<FeedbackRunSnapshot> = {}): FeedbackRunSnapshot {
|
|
return {
|
|
id: "run-1",
|
|
state: ORCH_STATE.WAITING,
|
|
outcome: null,
|
|
startedAt: new Date(now - 5 * 60_000),
|
|
finishedAt: null,
|
|
deliveredAt: new Date(now - 4 * 60_000).toISOString(),
|
|
lastProgressAt: null,
|
|
error: null,
|
|
...over,
|
|
};
|
|
}
|
|
|
|
const UP = { cloud: true, local: true, any: true };
|
|
const LOCAL_DOWN = { cloud: true, local: false, any: true };
|
|
|
|
// A local project whose Fleet Runner is off, with the cloud box up. The open
|
|
// pending row is long gone (the runner acks within ~8s of injecting), so the
|
|
// channel has to come from the project's stored routing decision.
|
|
{
|
|
const s = applyRunContext(
|
|
snap(),
|
|
{ payload: null },
|
|
{
|
|
presence: LOCAL_DOWN,
|
|
project: { dirPath: "/home/g/dev/thing", gitUrl: null, builderPref: "local" },
|
|
pending: null,
|
|
},
|
|
);
|
|
assert.equal(s.builderChannel, "local", "no pending row → project's stored routing decides");
|
|
assert.equal(
|
|
s.builderOffline,
|
|
true,
|
|
"local builder down is OFFLINE even though the cloud box is up — the old !presence.any read this healthy",
|
|
);
|
|
|
|
const work = deriveFeedbackWork(FEEDBACK_STATUS.DISPATCHED, s, now);
|
|
assert.equal(work.phase, FEEDBACK_WORK_PHASE.STUCK, "row must say Needs you");
|
|
// This is the guard's own read: STUCK means Implement ALLOWS the retry.
|
|
assert.notEqual(
|
|
work.phase,
|
|
FEEDBACK_WORK_PHASE.QUEUED,
|
|
"QUEUED here is what made Implement refuse the retry",
|
|
);
|
|
assert.notEqual(
|
|
work.phase,
|
|
FEEDBACK_WORK_PHASE.WORKING,
|
|
"WORKING here is what made Implement refuse the retry",
|
|
);
|
|
}
|
|
|
|
// Same run, builders up: nothing about the above may turn an ordinary run stuck.
|
|
{
|
|
const s = applyRunContext(
|
|
snap(),
|
|
{ payload: null },
|
|
{
|
|
presence: UP,
|
|
project: { dirPath: "/home/g/dev/thing", gitUrl: null, builderPref: "local" },
|
|
pending: null,
|
|
},
|
|
);
|
|
assert.equal(s.builderOffline, false, "builders up → not offline");
|
|
const work = deriveFeedbackWork(FEEDBACK_STATUS.DISPATCHED, s, now);
|
|
assert.notEqual(
|
|
work.phase,
|
|
FEEDBACK_WORK_PHASE.STUCK,
|
|
"a healthy builder must not read as stuck",
|
|
);
|
|
}
|
|
|
|
// An open pending row outranks the project default — it names the actual queue.
|
|
{
|
|
const s = applyRunContext(
|
|
snap(),
|
|
{ payload: null },
|
|
{
|
|
presence: { cloud: false, local: true, any: true },
|
|
project: { dirPath: null, gitUrl: "https://github.com/x/y", builderPref: "local" },
|
|
pending: {
|
|
id: "cmd-1",
|
|
type: "dispatch",
|
|
claimedAt: null,
|
|
executedAt: null,
|
|
createdAt: new Date(),
|
|
channel: "cloud",
|
|
},
|
|
},
|
|
);
|
|
assert.equal(s.builderChannel, "cloud", "the open row's channel wins over the project default");
|
|
assert.equal(s.builderOffline, true, "cloud queue with the box down is offline");
|
|
assert.equal(s.commandId, "cmd-1", "the open row supplies the command id");
|
|
}
|
|
|
|
// A hosted dispatch recorded on the payload must survive a non-hosted pending
|
|
// row — overwriting it was how hostedPending got erased mid-run.
|
|
{
|
|
const s = applyRunContext(
|
|
snap({ hostedPending: true }),
|
|
{ payload: { hostedDispatchId: "hosted-1" } },
|
|
{
|
|
presence: UP,
|
|
pending: {
|
|
id: "cmd-2",
|
|
type: "dispatch",
|
|
claimedAt: new Date(),
|
|
executedAt: null,
|
|
createdAt: new Date(),
|
|
channel: "cloud",
|
|
},
|
|
},
|
|
);
|
|
assert.equal(s.hostedPending, true, "a non-hosted pending row must not erase a hosted dispatch");
|
|
}
|
|
|
|
// WHERE A PAST ATTEMPT RAN is a recorded fact, not a current setting.
|
|
//
|
|
// The open pending row is gone for most of a run's life, and the fallback after
|
|
// it was the project's routing preference — which the operator can change at any
|
|
// time. Flip a project from This computer to Cloud and every finished local run
|
|
// would start claiming it ran on the box, with its offline verdict computed
|
|
// against a machine it never touched. The executed command row still records
|
|
// where the attempt was actually handed, so ask that first.
|
|
{
|
|
const s = applyRunContext(
|
|
snap(),
|
|
{ payload: null },
|
|
{
|
|
presence: LOCAL_DOWN,
|
|
// The project says Cloud TODAY...
|
|
project: { dirPath: null, gitUrl: "https://github.com/bitbaum/loki", builderPref: "cloud" },
|
|
pending: null,
|
|
// ...but this attempt was handed to the laptop when it ran.
|
|
ack: { ok: true, verified: true, warning: null, channel: "local" },
|
|
},
|
|
);
|
|
assert.equal(
|
|
s.builderChannel,
|
|
"local",
|
|
"the recorded attempt outranks the project's current preference",
|
|
);
|
|
assert.equal(
|
|
s.builderOffline,
|
|
true,
|
|
"and the offline verdict is computed against the machine it actually ran on",
|
|
);
|
|
}
|
|
|
|
// An attempt that never reached a builder has no recorded channel, so the
|
|
// dispatcher's own rule is still the best available guess.
|
|
{
|
|
const s = applyRunContext(
|
|
snap(),
|
|
{ payload: null },
|
|
{
|
|
presence: LOCAL_DOWN,
|
|
project: { dirPath: null, gitUrl: "https://github.com/bitbaum/loki", builderPref: "cloud" },
|
|
pending: null,
|
|
ack: { ok: null, verified: null, warning: null, channel: null },
|
|
},
|
|
);
|
|
assert.equal(s.builderChannel, "cloud", "no recorded channel → fall back to the stored decision");
|
|
assert.equal(s.builderOffline, false, "the cloud box is up");
|
|
}
|
|
|
|
console.log("✓ feedback run-context tests passed");
|