Public: anyone can read this; the crew can change it.

A runner's last report sends its waiting lines before its answer, so a check that ends while the forge restarts keeps its tally #17223

opened this

Merged 37h 17m ago, live in production

fix/runner-end-lines → main 0 commits 2 threads · 2 unresolved

  • Merged. fix/runner-end-lines was merged into main as dd4411c.

Review

Sign in to review this change.
2 files changed +199 −32
src/runner/main.ts +88 −32
@@ -153,6 +153,80 @@ export async function fillMirror(
153153 return undefined;
154154 }
155155
156 /** A report of a step's lines, as the runner sends them. */
157 export interface LineBatch {
158 step: string;
159 lines: string[];
160 }
161
162 /**
163 * Send the batches still waiting, in order (an empty report when none wait, to renew the lease).
164 * Unreachable, or 503 while the forge restarts, the rest wait for the next report. Refused for good
165 * (another answer that is not ok: too large, malformed, a fault of the forge's), a batch is said
166 * and let go: kept, it would hold every later report, and the job's answer behind them. A 404 is
167 * the forge dropping the job.
168 */
169 export async function flushLines(
170 waiting: LineBatch[],
171 send: (batch: LineBatch) => Promise<Response>,
172 say: (text: string) => void,
173 ): Promise<"dropped" | undefined> {
174 const batches = waiting.length === 0 ? [{ step: "", lines: [] as string[] }] : waiting.splice(0);
175 for (const [i, batch] of batches.entries()) {
176 const res = await send(batch).catch(() => undefined);
177 await res?.body?.cancel().catch(() => {});
178 if (res?.status === 404) return "dropped";
179 if (res === undefined || res.status === 503) {
180 waiting.unshift(...batches.slice(i).filter((b) => b.lines.length > 0));
181 return undefined;
182 }
183 if (!res.ok && batch.lines.length > 0)
184 say(`the forge refused a report of ${batch.lines.length} line(s) (${res.status}); they are not sent again`);
185 }
186 return undefined;
187 }
188
outdated

When lines still wait (left() > 0) the answer is skipped. On the last pass, now() >= until, the answer must still be tried once. Otherwise a permanently failing lines report means the job is never answered.

189 /** What {@link reportEnd} needs of a job's reporting. */
190 export interface EndReport {
191 /** Sends the lines still waiting; on a forge it cannot reach, they stay waiting. */
192 readonly flush: () => Promise<void>;
193 /** How many reports of lines still wait. */
194 readonly left: () => number;
195 /** Whether the forge dropped the job (a 404): nothing more is said. */
196 readonly dropped: () => boolean;
197 /** Sends the job's answer (done or abandon). */
198 readonly answer: () => Promise<Response>;
199 readonly waitMs: number;
200 readonly pause: () => Promise<void>;
201 readonly now?: () => number;
202 }
203
204 /**
205 * A job's last word: its lines first, then its answer. The answer is the job: a forge that did not
206 * hear it is told again, and a forge restarting (unreachable, or 503 while no forge waits on the
207 * job) is waited for up to `waitMs`, since the one that comes up takes the job back
208 * (`checks/adopt.ts`). The lines still waiting go before the answer on every try: an answer that
209 * arrived alone, its lines dropped, left a red check naming nothing, its tally lost with the
210 * restart (#17173, 2026-12-01). But the answer always lands: once the wait is spent it is sent
211 * whatever still waits, since a red check that names nothing beats a job that never reports.
212 */
213 export async function reportEnd(io: EndReport): Promise<void> {
214 const now = io.now ?? Date.now;
215 const until = now() + io.waitMs;
216 for (let tries = 1; ; tries++) {
217 await io.flush();
218 if (io.dropped()) return;
219 const spent = now() >= until;
220 const res = io.left() > 0 && !spent ? undefined : await io.answer().catch(() => undefined);
221 if (res !== undefined && (res.ok || res.status === 404 || res.status === 422)) return;
222 await res?.body?.cancel().catch(() => {});
223 const restarting = res === undefined || res.status === 503;
224 // Spent, this try sent the answer whatever still waited; a refusal had sent it too.
225 if (restarting ? spent : tries >= 5) return;
226 await io.pause();
227 }
228 }
229
156230 /** A name of the forge's as a directory name of ours. */
157231 const dirName = (raw: string): string =>
158232 raw.replace(/[^A-Za-z0-9_-]/g, "").slice(0, 80) || "none";
@@ -224,24 +298,12 @@ export async function runRunner(options: RunnerOptions): Promise<RunnerEnd> {
224298 * Send what has been said, in order; with nothing to say, the report is the lease's renewal.
225299 */
226300 const flush = async (): Promise<void> => {
227 const batches =
228 waiting.length === 0
229 ? [{ step: "", lines: [] as string[] }]
230 : waiting.splice(0);
231 for (const [i, batch] of batches.entries()) {
232 try {
233 const res = await post(`/api/v1/runner/jobs/${job.id}`, token, batch);
234 if (res.status === 404) {
235 dropped = true;
236 stop.abort();
237 return;
238 }
239 if (!res.ok) throw new Error(`the forge answered ${res.status}`);
240 } catch {
241 // The forge could not be reached: these lines wait for the next report.
242 waiting.unshift(...batches.slice(i));
243 return;
244 }
301 const said = await flushLines(waiting, (batch) => post(`/api/v1/runner/jobs/${job.id}`, token, batch), (text) =>
302 say(`job ${job.id}: ${text}`),
303 );
304 if (said === "dropped") {
305 dropped = true;
306 stop.abort();
245307 }
246308 };
247309 let reporting = true;
@@ -258,20 +320,14 @@ export async function runRunner(options: RunnerOptions): Promise<RunnerEnd> {
258320 reporting = false;
259321 quiet.abort();
260322 await reports;
261 await flush();
262 if (dropped) return;
263 // The answer is the job: a forge that did not hear it is told again. A forge restarting (not
264 // reachable, or 503 while no one waits on the job) is waited for up to `HANDOVER_WAIT_MS`:
265 // the forge that comes up takes the job back and wants this answer (`checks/adopt.ts`).
266 const until = Date.now() + HANDOVER_WAIT_MS;
267 for (let tries = 1; ; tries++) {
268 const res = await post(`/api/v1/runner/jobs/${job.id}`, token, body).catch(() => undefined);
269 if (res !== undefined && (res.ok || res.status === 404 || res.status === 422)) return;
270 await res?.body?.cancel().catch(() => {});
271 const restarting = res === undefined || res.status === 503;
272 if (restarting ? Date.now() >= until : tries >= 5) return;
273 await pause(REPORT_MS, new AbortController().signal);
274 }
323 await reportEnd({
324 flush,
325 left: () => waiting.length,
326 dropped: () => dropped,
327 answer: () => post(`/api/v1/runner/jobs/${job.id}`, token, body),
328 waitMs: HANDOVER_WAIT_MS,
329 pause: () => pause(REPORT_MS, new AbortController().signal),
330 });
275331 };
276332 try {
277333 if (
test/runner-end-lines.test.ts added +111
@@ -0,0 +1,111 @@
1 /**
2 * A job's last word is its lines, then its answer (`reportEnd`, `runner/main.ts`). On 2026-12-01 a
3 * check finished on a runner while the forge restarted: its waiting lines (the tally of what
4 * failed) were never sent again, only the answer was, and the check was red with no test named.
5 * The answer itself must always land: a batch refused for good is let go, and a spent wait sends
6 * it whatever still waits.
7 */
8 import { expect, test } from "bun:test";
9 import { flushLines, reportEnd, type LineBatch } from "../src/runner/main.ts";
10
11 /** A forge that is restarting for `downFor` tries, then up; what reached it, in order. */
12 function forge(downFor: number) {
13 const heard: string[] = [];
14 let waiting = ["(fail) grp > red", "1 of 8 shards failed:"];
15 let tries = 0;
16 return {
17 heard,
18 io: {
19 flush: async () => {
20 if (tries < downFor) return; // unreachable: the lines stay waiting
21 heard.push(...waiting);
22 waiting = [];
23 },
24 left: () => waiting.length,
25 dropped: () => false,
26 answer: async () => {
27 if (tries < downFor) return new Response(null, { status: 503 });
28 heard.push("done");
29 return new Response(null, { status: 200 });
30 },
31 waitMs: 60_000,
32 pause: async () => {
33 tries++;
34 },
35 now: () => tries * 1000,
36 },
37 };
38 }
39
40 test("lines still waiting when a forge restarts reach the forge that comes up, before the answer", async () => {
41 const f = forge(3);
42 await reportEnd(f.io);
43 expect(f.heard).toEqual(["(fail) grp > red", "1 of 8 shards failed:", "done"]);
44 });
45
46 test("with the forge up, the lines and the answer go at once", async () => {
47 const f = forge(0);
48 await reportEnd(f.io);
49 expect(f.heard).toEqual(["(fail) grp > red", "1 of 8 shards failed:", "done"]);
50 });
51
outdated

This test asserts that nothing is sent past the wait, which makes an unanswered job the specified behaviour. Expect ["done"] here instead: the answer is the job, and lines that cannot be delivered must not hold it back.

52 test("lines that cannot be sent before the wait is spent do not cost the answer: it is sent last, the lines unsent", async () => {
53 // The forge takes answers but never the lines (unreachable for them all along).
54 const heard: string[] = [];
55 let tries = 0;
56 await reportEnd({
57 flush: async () => {},
58 left: () => 2,
59 dropped: () => false,
60 answer: async () => {
61 heard.push("done");
62 return new Response(null, { status: 200 });
63 },
64 waitMs: 5_000,
65 pause: async () => {
66 tries++;
67 },
68 now: () => tries * 1000,
69 });
70 expect(heard).toEqual(["done"]);
71 });
72
73 test("a batch the forge refuses for good is said and let go, and the answer still arrives after it", async () => {
74 const heard: string[] = [];
75 const said: string[] = [];
76 const waiting: LineBatch[] = [{ step: "check", lines: ["x".repeat(10)] }, { step: "check", lines: ["(fail) grp > red"] }];
77 // The forge is up: it refuses the first batch (too large) and takes the rest and the answer.
78 const send = async (batch: LineBatch) => {
79 if (batch.lines[0]!.startsWith("x")) return new Response(null, { status: 413 });
80 heard.push(...batch.lines);
81 return new Response(null, { status: 200 });
82 };
83 await reportEnd({
84 flush: async () => {
85 await flushLines(waiting, send, (text) => said.push(text));
86 },
87 left: () => waiting.length,
88 dropped: () => false,
89 answer: async () => {
90 heard.push("done");
91 return new Response(null, { status: 200 });
92 },
93 waitMs: 60_000,
94 pause: async () => {},
95 });
96 expect(heard).toEqual(["(fail) grp > red", "done"]);
97 expect(said).toEqual(["the forge refused a report of 1 line(s) (413); they are not sent again"]);
98 });
99
100 test("a restarting forge (503, or no answer) keeps the batches waiting, in order; a 404 drops the job", async () => {
101 const waiting: LineBatch[] = [{ step: "check", lines: ["a"] }, { step: "check", lines: ["b"] }];
102 expect(await flushLines(waiting, async () => new Response(null, { status: 503 }), () => {})).toBeUndefined();
103 expect(waiting.map((b) => b.lines[0])).toEqual(["a", "b"]);
104 expect(await flushLines(waiting, () => Promise.reject(new Error("down")), () => {})).toBeUndefined();
105 expect(waiting.map((b) => b.lines[0])).toEqual(["a", "b"]);
106 expect(await flushLines(waiting, async () => new Response(null, { status: 404 }), () => {})).toBe("dropped");
107 // A lease renewal (an empty report) that could not be sent is not left waiting.
108 const none: LineBatch[] = [];
109 await flushLines(none, async () => new Response(null, { status: 503 }), () => {});
110 expect(none).toEqual([]);
111 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs