Files
loki/scripts/test/deploy-runner-decoupling.ts
T
cc4e892559 perf(deploy): the pipeline was waiting, not working (#293)
Merge→live measured 14–27 minutes. The irreducible work in that window is
about 80 seconds: build 66s, rsync 4s, restart 4s, verify 7s. Nearly all
of the rest was waiting, so nearly all of the fix is removing waits
rather than making anything faster.

Two waits, measured:

1. The runner drain, 480 of the ship step's 570 seconds. The deploy
   watched one in-flight agent, then restarted anyway at the cap. The
   web app had been live and verified for eight of those minutes. Two
   systems with opposite needs were sharing one job's wall-clock — the
   app wants to be live now, the runner wants to not kill anyone's work
   — and the runner won every time.

   A shorter timeout would only kill agents sooner. The wait itself is
   fine; the place it happened is what cost. Waiting is expensive in CI
   and free on the box, so it now happens on the box: the deploy
   schedules a detached systemd-run unit and returns as soon as the app
   is verified. The drain gets *longer* (1800s, not 480) and cheaper at
   the same time, because nothing is holding a runner open for it.

2. The CD reconciler, up to 10 minutes and 18.6 of #284's 26.8. Deploy
   chains off CI via workflow_run, which never fires for a CI run that
   GITHUB_TOKEN started — i.e. for every auto-merged PR, the only path
   that merges code here. The sweep covered that by reconciling on its
   next tick: correct, self-healing, and late.

   A dispatch is not a push, so it is not subject to the no-cascade
   rule. ci.yml now dispatches Deploy itself the moment main goes green.
   The sweep keeps its reconciler as the safety net, which is the right
   shape for "did something get missed?" and the wrong one for "did
   something just happen?".

Also caches .next/cache across deploys — the build is the one part of
this that is genuinely work, so it is the one part worth shrinking.

Not done, and worth saying: I thought CI and Deploy each built the app,
so the tested artifact was never the shipped one. That was wrong. ci.yml
deliberately skips `next build` because App-Router pages query Postgres
during pre-render; the build happens once, in Deploy. There was no
discarded build and no artifact to pass.

The decoupling has a test, and it asserts the property rather than the
patch: the deploy must not block on a drain, the restart must be
detached, the script must be shipped before it is invoked, and the drain
must stay bounded. Verified the first two assertions fail against the
previous version (4 blocking markers, 0 systemd-run) rather than merely
passing against this one.

Co-authored-by: t <t@t.t>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-15 11:58:29 +00:00

81 lines
3.3 KiB
TypeScript

/**
* A deploy must end when the APP is live — not when an unrelated system is.
*
* The web app and the box-runner have opposite needs. The app wants to be live
* immediately; the runner wants to not kill an agent mid-task. Both used to
* share one job's wall-clock, and the runner won: measured on 2026-08-15, the
* ship step spent 480 of its 570 seconds watching a single in-flight agent,
* then restarted anyway. The app had been live and verified for eight of those
* minutes. It was the most expensive thing in the whole pipeline and it bought
* nothing — the cost of patience with the semantics of impatience.
*
* The fix is not a shorter timeout, which would only kill agents sooner. It is
* moving the wait to the box, where waiting is free: the deploy schedules a
* detached drain-then-restart and returns. This suite guards that the wait
* cannot migrate back into the deploy, because it would look like a reasonable
* thing to do again.
*/
import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
const root = join(__dirname, "..", "..");
const deploy = readFileSync(join(root, "scripts/deploy-hetzner.sh"), "utf8");
const drainPath = join(root, "scripts/hetzner/drain-and-restart-runner.sh");
let passed = 0;
function check(name: string, fn: () => void) {
fn();
passed++;
console.log(` ✓ ${name}`);
}
function assert(cond: boolean, msg: string) {
if (!cond) throw new Error(msg);
}
check("the deploy does not wait for agents to drain", () => {
// The signature of the old loop: sleeping in a loop while counting agent
// processes. Any reintroduction reads like this, whatever it is named.
const waits = /waiting to drain|drain_box_runner_agents|drain timed out/.test(deploy);
assert(!waits, "deploy-hetzner.sh must not block on draining agents — schedule it on the box instead");
});
check("the runner restart is scheduled detached, not run inline", () => {
assert(
/systemd-run/.test(deploy),
"the runner restart must be detached via systemd-run so the deploy does not wait for it",
);
});
check("the deferred restart script exists and is shipped before it is invoked", () => {
assert(existsSync(drainPath), "scripts/hetzner/drain-and-restart-runner.sh must exist");
const syncIdx = deploy.indexOf("drain-and-restart-runner.sh");
const runIdx = deploy.indexOf("systemd-run");
assert(syncIdx !== -1, "the drain script must be rsynced to the box");
assert(
syncIdx < runIdx,
"the drain script must be synced BEFORE systemd-run invokes it, or the first deploy invokes a missing file",
);
});
check("the deferred restart cannot be killed by the restart it performs", () => {
const drain = readFileSync(drainPath, "utf8");
// systemd-run puts it in its OWN unit; restarting fleetcrown-box-runner kills
// that unit's cgroup, which must not contain this script.
assert(
/--unit=/.test(deploy),
"the drain must run in its own systemd unit, not inside the box-runner's cgroup",
);
assert(
/systemctl restart/.test(drain),
"the drain script is what actually restarts the runner",
);
});
check("the drain still has a cap, so a wedged agent cannot freeze runner code forever", () => {
const drain = readFileSync(drainPath, "utf8");
assert(/MAX=/.test(drain) && /cap/.test(drain), "the box-side drain must remain bounded");
});
console.log(`\n${passed} passed`);