Files
loki/scripts/test/served-release.ts
CatoandClaude Fable 5.2 64e2c6b543 fix(builder, runs): a held task waits minutes and says so; a build says when it is live (#1663)
* docs(handoff): checkpoint for building with Loki and with a Cat bot

The objective, the two journeys that count as done, how production can be
reached from an agent session, and the state when the work began.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* docs(handoff): what the investigation found, and what it did not reach

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(builder): a task held for the runner's update waits minutes, not for ever, and says so

While the cloud runner is behind the deploy the commands route hands it no
work-starting command, so the idle moment its restart waits for comes. The
hold had no end, and on 2026-11-12 the restart did not come: two runner
releases landed 35 minutes apart, the drain read "5 agent(s) working" on
every look for an hour, and the builder claimed screen peeks and nothing
else (Sealed builds run 43165605499). Every task a person sent sat queued
while the chat said a runner would claim it shortly.

- runner-update-hold: a held command is handed over once it has waited
  five minutes, and the hold applies only within two hours of the newest
  runner release. Running on last week's runner is recoverable; a task
  that never starts is not.
- claimNextPendingCommand takes the overdue rule. While holding, an empty
  at-once list claims nothing (it used to claim everything).
- The person is told why and for how long: in the chat reply, in the
  /api/inject answer (builderUpdating, message) and as the run track's
  headline while the job is unclaimed.
- loki-loop-check.yml: the end-to-end check of the live site on demand,
  with an optional real no-change task that must come back into its
  thread. loki-e2e-check.sh --report takes the project, the wait and the
  dispatch choice from its caller.
- sealed-builds.yml mode=activity: read-only, what each agent process of
  the runner read and wrote over a minute, so the drain's "working" can be
  measured for sessions it cannot read a status for.
- pre-push: the watchdog's sleep no longer holds the caller's output open
  for fifteen minutes after the push has landed.

Server and box scripts only: no runner code, no runner version.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(runs): a build tells the truth after the agent finishes, and says when it is live

A task sent from the chat ended at "done, pull request opened". What it
said next was wrong in several ways, and the one thing a person waits for,
that it went live, was said to nobody.

- The "done" message reads the pull request the runner itself reported
  (payload.prUrl from closeRunShipped, read by nothing until now) and asks
  GitHub with the token that can read the repository. The person's own
  OAuth token is refused on the organisation's repositories, so a run that
  shipped could be posted as "No pull request or push was found". The
  reaper's evidence lookup asks the same way.
- "Live" is read from the site: a merged change is live when the commit in
  the site's /.well-known/fleet-release.json is the merge or comes after
  it (feedback/served-release). Workflow-run labels are wrong on this
  fleet: a Deploy run is labelled with main's tip at its start, a batched
  merge has no Deploy run of its own, and OrangeCat's deploy is named CD.
  It only upgrades; with no receipt the workflow-run verdict stands.
- A chat build is followed to live on the fifteen-minute cron that already
  follows feedback fixes (run-follow-notify): on the change to live, or to
  a failed deploy, one line in the thread the task came from and one push.
  GitHub and the release receipt only, no model. Old news is not announced.
- A task that waits behind the project's earlier run says so in the chat,
  the /api/inject answer (queuedBehind, message) and the track, instead of
  "will claim it shortly".
- Isolated runs ship through a pull request: worktreePromptNote told every
  lane and one-off session to push straight to main.
- The close message no longer prints "Live: <address>" beside a pull
  request that has not merged.

Server only, plus one prompt line the runner reads at its next start. No
file under desktop/, no runner version.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 09:48:29 +01:00

240 lines
7.8 KiB
TypeScript

/**
* A merged change is live when the site serves a commit that contains the
* merge (lib/feedback/served-release), whatever the workflow runs are
* labelled with.
*
* Run: npx tsx scripts/test/served-release.ts
*/
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { FIX_SHIP_STATE, type FixShipping } from "@/lib/feedback/fix-shipping";
import {
applyServedRelease,
awaitsServedRelease,
fetchServedRelease,
parseServedRelease,
relationFromCompare,
RELEASE_RECEIPT_PATH,
} from "@/lib/feedback/served-release";
let pass = 0;
async function check(name: string, fn: () => void | Promise<void>) {
await fn();
pass += 1;
console.log(` ✓ ${name}`);
}
const MERGE = "6bb3d7461c3ecdc41ed384976df0d98bd9ee1255";
const LATER = "9253f40898e3d135878ac6471ccb9ccb20979c4b";
const receipt = {
formatVersion: 1,
repo: "bitbaum/loki",
commit: LATER,
createdAt: "2026-11-12T06:02:12.497Z",
};
const merged = (state: FixShipping["state"], extra: Partial<FixShipping> = {}): FixShipping => ({
state,
pr: {
number: 1643,
url: "https://github.com/bitbaum/loki/pull/1643",
title: "x",
mergedAt: "2026-11-12T05:16:36Z",
mergeSha: MERGE,
},
checkedAt: "2026-11-12T06:10:00Z",
...extra,
});
const publicLookup = async () => [{ address: "167.233.22.31" }];
const jsonFetch =
(body: unknown, status = 200) =>
async () =>
new Response(typeof body === "string" ? body : JSON.stringify(body), { status });
async function main() {
await check("a receipt is a repo, a full commit and a time", () => {
const url = `https://loki.orangecat.ch${RELEASE_RECEIPT_PATH}`;
assert.deepEqual(parseServedRelease(receipt, url), {
repo: "bitbaum/loki",
commit: LATER,
createdAt: receipt.createdAt,
url,
});
for (const bad of [
null,
"x",
{},
{ repo: "loki", commit: LATER },
{ repo: "a/b", commit: "9253f40" },
])
assert.equal(parseServedRelease(bad, url), null, JSON.stringify(bad));
assert.equal(parseServedRelease({ ...receipt, createdAt: "soon" }, url)?.createdAt, null);
});
await check("ahead or identical contains the merge; behind or diverged does not", () => {
assert.equal(relationFromCompare("ahead"), "contains");
assert.equal(relationFromCompare("identical"), "contains");
assert.equal(relationFromCompare("behind"), "missing");
assert.equal(relationFromCompare("diverged"), "missing");
assert.equal(relationFromCompare(null), null, "unreadable is not an answer");
assert.equal(relationFromCompare("something"), null);
});
const served = parseServedRelease(receipt, "https://loki.orangecat.ch/x")!;
// 2026-11-12: #1643's merge carried no Deploy run (the deploy that shipped
// it was labelled with a later commit), so its ledger read merged for good.
await check("merged with no deploy run on record reads live once the site serves it", () => {
const fix = applyServedRelease(
merged(FIX_SHIP_STATE.MERGED),
served,
"contains",
"bitbaum/loki",
);
assert.equal(fix.state, FIX_SHIP_STATE.DEPLOYED);
assert.equal(fix.liveVia, "served_release");
assert.equal(fix.deploy?.at, receipt.createdAt, "live since the release was built");
assert.equal(fix.deploy?.url, "https://loki.orangecat.ch/x");
});
await check(
"deploying and deploy-failed are settled the same way, and the failed run is kept",
() => {
assert.equal(
applyServedRelease(merged(FIX_SHIP_STATE.DEPLOYING), served, "contains", "bitbaum/loki")
.state,
FIX_SHIP_STATE.DEPLOYED,
);
const failed = merged(FIX_SHIP_STATE.DEPLOY_FAILED, {
deploy: {
url: "https://github.com/r/1",
name: "Deploy",
conclusion: "cancelled",
status: "completed",
},
});
const fix = applyServedRelease(failed, served, "contains", "bitbaum/loki");
assert.equal(fix.state, FIX_SHIP_STATE.DEPLOYED);
assert.deepEqual(fix.ownDeploy, {
url: "https://github.com/r/1",
name: "Deploy",
conclusion: "cancelled",
});
},
);
await check("it only ever upgrades: everything else is left as it was", () => {
const m = merged(FIX_SHIP_STATE.MERGED);
assert.equal(
applyServedRelease(m, served, "missing", "bitbaum/loki"),
m,
"site runs older code",
);
assert.equal(applyServedRelease(m, served, null, "bitbaum/loki"), m, "could not compare");
assert.equal(applyServedRelease(m, null, "contains", "bitbaum/loki"), m, "no receipt");
assert.equal(
applyServedRelease(m, served, "contains", "bitbaum/orangecat"),
m,
"another repo's site",
);
const open = merged(FIX_SHIP_STATE.PR_OPEN);
assert.equal(applyServedRelease(open, served, "contains", "bitbaum/loki"), open, "not merged");
const live = merged(FIX_SHIP_STATE.DEPLOYED);
assert.equal(
applyServedRelease(live, served, "contains", "bitbaum/loki"),
live,
"already live",
);
const noSha = { ...merged(FIX_SHIP_STATE.MERGED), pr: { number: 1, url: "u", title: "t" } };
assert.equal(awaitsServedRelease(noSha), false, "no merge commit to look for");
});
await check("the receipt is read from the site's own origin, guarded", async () => {
let asked = "";
const got = await fetchServedRelease("https://loki.orangecat.ch/loki?x=1", {
lookup: publicLookup,
fetchImpl: async (url, init) => {
asked = url;
assert.equal(init.redirect, "error", "no redirect is followed");
return new Response(JSON.stringify(receipt));
},
});
assert.equal(asked, `https://loki.orangecat.ch${RELEASE_RECEIPT_PATH}`);
assert.equal(got?.commit, LATER);
const never = async () => {
throw new Error("must not be fetched");
};
for (const bad of [
null,
"",
"http://127.0.0.1/",
"https://localhost/",
"https://box.internal/",
"https://a.ch:8443/",
])
assert.equal(
await fetchServedRelease(bad, { lookup: publicLookup, fetchImpl: never }),
null,
String(bad),
);
assert.equal(
await fetchServedRelease("https://loki.orangecat.ch", {
lookup: async () => [{ address: "10.0.0.5" }],
fetchImpl: never,
}),
null,
"a name that resolves into a private network is not opened",
);
assert.equal(
await fetchServedRelease("http://loki.orangecat.ch", {
lookup: publicLookup,
fetchImpl: never,
}),
null,
"https only",
);
for (const res of [jsonFetch("not json"), jsonFetch({}, 200), jsonFetch(receipt, 404)])
assert.equal(
await fetchServedRelease("https://loki.orangecat.ch", {
lookup: publicLookup,
fetchImpl: res,
}),
null,
);
assert.equal(
await fetchServedRelease("https://loki.orangecat.ch", {
lookup: publicLookup,
fetchImpl: async () => {
throw new Error("redirect");
},
}),
null,
"never throws",
);
});
await check(
"the ledger asks the site after the workflow runs, with the project's address",
() => {
const refresh = readFileSync("src/lib/feedback/fix-shipping-refresh.ts", "utf8");
assert.match(
refresh,
/fix = await liveFromServedRelease\(fix, ref, input\.liveUrl, picked\.token\);/,
);
assert.match(refresh, /liveUrl: string \| null;/, "required, so no caller can forget it");
assert.match(readFileSync("src/lib/run-follow.ts", "utf8"), /\n\s+liveUrl,\n/);
assert.match(
readFileSync("src/lib/feedback/attach-work.ts", "utf8"),
/liveUrl: item\.liveUrl \?\? null,/,
);
},
);
console.log(`\nserved-release: ${pass} passed`);
}
main().catch((e) => {
console.error(e);
process.exit(1);
});