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

Revert "Bugs in src/checks: 1 finding from the deepsec sweep" #15222

opened this

Check · Review · Land · Live — It conflicts with its base. Next: builder, it conflicts with its base.

Conflicts with main

revert-14829 → main 1 commit

Review

Sign in to review this change.
3 files changed +10 −108
src/checks/runner_jobs.ts +5 −28
@@ -317,61 +317,38 @@ export function jobByToken(db: Database, jobId: string, presented: string, now:
317317 };
318318 }
319319
320 /**
321 * Push a held job's lease out; false when the claim is no longer this token's to renew. Every
322 * write after `jobByToken` matches on the token again: a request that outlived its claim (a
323 * lapsed lease, a removed runner, the job claimed since by another) changes nothing (#14790).
324 */
320 /** Push a held job's lease out; false when the claim is no longer this one's to renew. */
325321 export function renewJob(
326322 db: Database,
327323 jobId: string,
328 presented: string,
329324 now: number,
330325 leaseMs: number = LEASE_MS,
331326 ): boolean {
332327 return (
333328 db
334329 .query(
335 "UPDATE check_job SET lease_until = ?2 WHERE id = ?1 AND token_hash = ?4 AND state = 'claimed' AND lease_until > ?3",
330 "UPDATE check_job SET lease_until = ?2 WHERE id = ?1 AND state = 'claimed' AND lease_until > ?3",
336331 )
337 .run(jobId, now + leaseMs, now, sha256(presented)).changes > 0
332 .run(jobId, now + leaseMs, now).changes > 0
338333 );
339334 }
340335
341 /**
342 * Whether this token still holds the job's claim, lease or no lease: a lapsed claim stays its
343 * own until the sweep or a removal clears its token for another runner to take (#14790).
344 */
345 export function holdsClaim(db: Database, jobId: string, presented: string): boolean {
346 return (
347 db
348 .query("SELECT 1 FROM check_job WHERE id = ?1 AND token_hash = ?2 AND state = 'claimed'")
349 .get(jobId, sha256(presented)) !== null
350 );
351 }
352
353 /**
354 * Record a held job's answer: `done` with its result, or `returned` with the runner's reason.
355 * False, and nothing written, when the claim is no longer this token's (as `holdsClaim`).
356 */
336 /** Record a held job's answer: `done` with its result, or `returned` with the runner's reason. */
357337 export function settleJob(
358338 db: Database,
359339 jobId: string,
360 presented: string,
361340 end: { done: RunnerResult } | { returned: string },
362341 ): boolean {
363342 const done = "done" in end;
364343 return (
365344 db
366345 .query(
367 `UPDATE check_job SET state = ?2, result = ?3, token_hash = NULL
368 WHERE id = ?1 AND token_hash = ?4 AND state = 'claimed'`,
346 "UPDATE check_job SET state = ?2, result = ?3, token_hash = NULL WHERE id = ?1 AND state = 'claimed'",
369347 )
370348 .run(
371349 jobId,
372350 done ? "done" : "returned",
373351 done ? JSON.stringify(end.done) : end.returned.slice(0, 400),
374 sha256(presented),
375352 ).changes > 0
376353 );
377354 }
src/http/api/runners.ts +3 −11
@@ -15,7 +15,6 @@ import {
1515 claimJob,
1616 deskOf,
1717 getRunner,
18 holdsClaim,
1918 jobByToken,
2019 nextOffer,
2120 renewJob,
@@ -159,11 +158,6 @@ export function runnerRoutes(router: Router<Handler>): void {
159158 const input = await boundedBody(ctx.request, REPORT_MAX);
160159 if (input === "too_large")
161160 return apiError("too_large", 413, `at most ${REPORT_MAX} bytes`);
162 // The body may have outlived the claim (the runner removed, the job taken by another): the
163 // token is asked for again before anything it carries reaches the check, and every write
164 // below is bound to it.
165 const token = bearer(ctx.request);
166 if (!holdsClaim(ctx.app.db, mine.job.id, token)) return notFound();
167161 const lines = Array.isArray(input.lines)
168162 ? input.lines
169163 .filter((l): l is string => typeof l === "string")
@@ -171,8 +165,7 @@ export function runnerRoutes(router: Router<Handler>): void {
171165 : [];
172166 if (lines.length > 0) mine.ear.lines(str(input.step), lines);
173167 if (typeof input.abandon === "string") {
174 if (!settleJob(ctx.app.db, mine.job.id, token, { returned: input.abandon }))
175 return notFound();
168 settleJob(ctx.app.db, mine.job.id, { returned: input.abandon });
176169 mine.ear.wake();
177170 return json({ ended: true });
178171 }
@@ -184,12 +177,11 @@ export function runnerRoutes(router: Router<Handler>): void {
184177 422,
185178 "done takes { steps: [{ name, ok, error?, ms }], contained, note }",
186179 );
187 if (!settleJob(ctx.app.db, mine.job.id, token, { done: result }))
188 return notFound();
180 settleJob(ctx.app.db, mine.job.id, { done: result });
189181 mine.ear.wake();
190182 return json({ ended: true });
191183 }
192 if (!renewJob(ctx.app.db, mine.job.id, token, Date.now())) return notFound();
184 if (!renewJob(ctx.app.db, mine.job.id, Date.now())) return notFound();
193185 return json({ lease });
194186 });
195187
test/checks-runner-placement.test.ts +2 −69
@@ -19,9 +19,7 @@ import {
1919 jobByToken,
2020 lapseJobs,
2121 offerJob,
22 removeRunner,
2322 renewJob,
24 settleJob,
2523 viewJob,
2624 withdrawJob,
2725 type RunnerJob,
@@ -273,7 +271,7 @@ describe("the desk", () => {
273271 // A runner at work is connected, though it is not asking for more.
274272 expect(getRunner(f.app.db, one.id)?.seenAt).toBe(1_001);
275273 // A report in time pushes the lease out; silence past it loses the job.
276 expect(renewJob(f.app.db, offered.id, first.token, 1_000 + LEASE_MS - 1)).toBe(true);
274 expect(renewJob(f.app.db, offered.id, 1_000 + LEASE_MS - 1)).toBe(true);
277275 expect(lapseJobs(f.app.db, 1_000 + LEASE_MS)).not.toContain(offered.id);
278276 expect(lapseJobs(f.app.db, 1_000 + 2 * LEASE_MS)).toContain(offered.id);
279277 expect(viewJob(f.app.db, offered.id)).toMatchObject({
@@ -283,7 +281,7 @@ describe("the desk", () => {
283281 expect(
284282 jobByToken(f.app.db, offered.id, first.token, 1_000 + 2 * LEASE_MS),
285283 ).toBeUndefined();
286 expect(renewJob(f.app.db, offered.id, first.token, 1_000 + 2 * LEASE_MS)).toBe(false);
284 expect(renewJob(f.app.db, offered.id, 1_000 + 2 * LEASE_MS)).toBe(false);
287285 const second = claimJob(
288286 f.app.db,
289287 { id: two.id, cells: true },
@@ -294,27 +292,6 @@ describe("the desk", () => {
294292 expect(withdrawJob(f.app.db, offered.id)).toBe(false);
295293 dropJob(f.app.db, offered.id);
296294 });
297 test("a claim's writes are bound to its token: once it lapses and another runner holds the job, the old token renews and settles nothing", () => {
298 const one = enrolRunner(f.app.db, "stale", f.alice.id).runner;
299 const two = enrolRunner(f.app.db, "fresh", f.alice.id).runner;
300 const offered = offerJob(f.app.db, { runId: "run-4", repoId: "repo-1", dir: "/x", job });
301 const first = claimJob(f.app.db, { id: one.id, cells: true }, 1_000)!;
302 const later = 1_000 + LEASE_MS;
303 expect(lapseJobs(f.app.db, later)).toContain(offered.id);
304 const second = claimJob(f.app.db, { id: two.id, cells: true }, later)!;
305 expect(second.job.id).toBe(offered.id);
306 const green = { steps: [{ name: "check", ok: true, ms: 1 }], contained: true, note: "" };
307 // The row is claimed again, but not by this token: nothing it sends lands.
308 expect(renewJob(f.app.db, offered.id, first.token, later + 1)).toBe(false);
309 expect(settleJob(f.app.db, offered.id, first.token, { done: green })).toBe(false);
310 expect(settleJob(f.app.db, offered.id, first.token, { returned: "x" })).toBe(false);
311 expect(viewJob(f.app.db, offered.id)).toMatchObject({ state: "claimed", runner: "fresh", result: null });
312 // Its own claim, past its lease but not yet swept or taken, still settles its finished work.
313 expect(renewJob(f.app.db, offered.id, second.token, later + LEASE_MS)).toBe(false);
314 expect(settleJob(f.app.db, offered.id, second.token, { done: green })).toBe(true);
315 expect(viewJob(f.app.db, offered.id)).toMatchObject({ state: "done", runner: "fresh" });
316 dropJob(f.app.db, offered.id);
317 });
318295 });
319296
320297 describe("a pull's check with FORGE_CHECKS_PLACEMENT=runner", () => {
@@ -496,50 +473,6 @@ describe("a pull's check with FORGE_CHECKS_PLACEMENT=runner", () => {
496473 expect((await api(door, claim.token, { lines: ["x"] })).status).toBe(404);
497474 });
498475
499 test("a report whose body outlives its runner's removal lands nothing on the job's next claim", async () => {
500 const gone = enrolRunner(f.app.db, "revoked", f.alice.id).runner;
501 const next = enrolRunner(f.app.db, "trusted", f.alice.id).runner;
502 const offered = offerJob(f.app.db, { runId: "run-5", repoId: "repo-1", dir: "/x", job });
503 const heard: string[] = [];
504 let woken = 0;
505 deskOf(f.app).ears.set(offered.id, {
506 wake: () => void woken++,
507 lines: (_step, lines) => void heard.push(...lines),
508 });
509 const first = claimJob(f.app.db, { id: gone.id, cells: true }, Date.now())!;
510 // The body is sent in two parts, the runner removed in between.
511 let finish = (): void => {};
512 const body = new ReadableStream<Uint8Array>({
513 start(c) {
514 c.enqueue(new TextEncoder().encode('{"step":"check",'));
515 finish = () => {
516 c.enqueue(
517 new TextEncoder().encode(
518 '"lines":["forged"],"done":{"steps":[{"name":"install","ok":true,"ms":1},{"name":"check","ok":true,"ms":1}],"contained":true,"note":""}}',
519 ),
520 );
521 c.close();
522 };
523 },
524 });
525 const sent = fetch(`${f.servers.url}/api/v1/runner/jobs/${offered.id}`, {
526 method: "POST",
527 headers: { authorization: `Bearer ${first.token}`, "content-type": "application/json" },
528 body,
529 });
530 await f.until(() => (getRunner(f.app.db, gone.id)?.seenAt ?? 0) > 0, "the report past its token check");
531 expect(removeRunner(f.app.db, gone.id)).toBe(true);
532 const second = claimJob(f.app.db, { id: next.id, cells: true }, Date.now())!;
533 expect(second.job.id).toBe(offered.id);
534 finish();
535 expect((await sent).status).toBe(404);
536 expect(heard).toEqual([]);
537 expect(woken).toBe(0);
538 expect(viewJob(f.app.db, offered.id)).toMatchObject({ state: "claimed", runner: "trusted", result: null });
539 deskOf(f.app).ears.delete(offered.id);
540 dropJob(f.app.db, offered.id);
541 });
542
543476 test("a runner that says done with no steps does not make the check green: the forge runs it", async () => {
544477 const lazy = enrolRunner(f.app.db, "lazy", f.alice.id);
545478 const asked = api("/api/v1/runner/claim", lazy.token, { wait: 20, cells: true });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs