Public: anyone can read this; the crew can change it.
Revert "Bugs in src/checks: 1 finding from the deepsec sweep" #15222
Check · Review · Land · Live — It conflicts with its base.
revert-14829 → main 1 commit
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: | ||
| 317 | 317 | }; |
| 318 | 318 | } |
| 319 | 319 | |
| 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. */ | |
| 325 | 321 | export function renewJob( |
| 326 | 322 | db: Database, |
| 327 | 323 | jobId: string, |
| 328 | presented: string, | |
| 329 | 324 | now: number, |
| 330 | 325 | leaseMs: number = LEASE_MS, |
| 331 | 326 | ): boolean { |
| 332 | 327 | return ( |
| 333 | 328 | db |
| 334 | 329 | .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", | |
| 336 | 331 | ) |
| 337 | .run(jobId, now + leaseMs, now, sha256(presented)).changes > 0 | |
| 332 | .run(jobId, now + leaseMs, now).changes > 0 | |
| 338 | 333 | ); |
| 339 | 334 | } |
| 340 | 335 | |
| 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. */ | |
| 357 | 337 | export function settleJob( |
| 358 | 338 | db: Database, |
| 359 | 339 | jobId: string, |
| 360 | presented: string, | |
| 361 | 340 | end: { done: RunnerResult } | { returned: string }, |
| 362 | 341 | ): boolean { |
| 363 | 342 | const done = "done" in end; |
| 364 | 343 | return ( |
| 365 | 344 | db |
| 366 | 345 | .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'", | |
| 369 | 347 | ) |
| 370 | 348 | .run( |
| 371 | 349 | jobId, |
| 372 | 350 | done ? "done" : "returned", |
| 373 | 351 | done ? JSON.stringify(end.done) : end.returned.slice(0, 400), |
| 374 | sha256(presented), | |
| 375 | 352 | ).changes > 0 |
| 376 | 353 | ); |
| 377 | 354 | } |
src/http/api/runners.ts +3 −11
| @@ -15,7 +15,6 @@ import { | ||
| 15 | 15 | claimJob, |
| 16 | 16 | deskOf, |
| 17 | 17 | getRunner, |
| 18 | holdsClaim, | |
| 19 | 18 | jobByToken, |
| 20 | 19 | nextOffer, |
| 21 | 20 | renewJob, |
| @@ -159,11 +158,6 @@ export function runnerRoutes(router: Router<Handler>): void { | ||
| 159 | 158 | const input = await boundedBody(ctx.request, REPORT_MAX); |
| 160 | 159 | if (input === "too_large") |
| 161 | 160 | 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(); | |
| 167 | 161 | const lines = Array.isArray(input.lines) |
| 168 | 162 | ? input.lines |
| 169 | 163 | .filter((l): l is string => typeof l === "string") |
| @@ -171,8 +165,7 @@ export function runnerRoutes(router: Router<Handler>): void { | ||
| 171 | 165 | : []; |
| 172 | 166 | if (lines.length > 0) mine.ear.lines(str(input.step), lines); |
| 173 | 167 | 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 }); | |
| 176 | 169 | mine.ear.wake(); |
| 177 | 170 | return json({ ended: true }); |
| 178 | 171 | } |
| @@ -184,12 +177,11 @@ export function runnerRoutes(router: Router<Handler>): void { | ||
| 184 | 177 | 422, |
| 185 | 178 | "done takes { steps: [{ name, ok, error?, ms }], contained, note }", |
| 186 | 179 | ); |
| 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 }); | |
| 189 | 181 | mine.ear.wake(); |
| 190 | 182 | return json({ ended: true }); |
| 191 | 183 | } |
| 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(); | |
| 193 | 185 | return json({ lease }); |
| 194 | 186 | }); |
| 195 | 187 |
test/checks-runner-placement.test.ts +2 −69
| @@ -19,9 +19,7 @@ import { | ||
| 19 | 19 | jobByToken, |
| 20 | 20 | lapseJobs, |
| 21 | 21 | offerJob, |
| 22 | removeRunner, | |
| 23 | 22 | renewJob, |
| 24 | settleJob, | |
| 25 | 23 | viewJob, |
| 26 | 24 | withdrawJob, |
| 27 | 25 | type RunnerJob, |
| @@ -273,7 +271,7 @@ describe("the desk", () => { | ||
| 273 | 271 | // A runner at work is connected, though it is not asking for more. |
| 274 | 272 | expect(getRunner(f.app.db, one.id)?.seenAt).toBe(1_001); |
| 275 | 273 | // 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); | |
| 277 | 275 | expect(lapseJobs(f.app.db, 1_000 + LEASE_MS)).not.toContain(offered.id); |
| 278 | 276 | expect(lapseJobs(f.app.db, 1_000 + 2 * LEASE_MS)).toContain(offered.id); |
| 279 | 277 | expect(viewJob(f.app.db, offered.id)).toMatchObject({ |
| @@ -283,7 +281,7 @@ describe("the desk", () => { | ||
| 283 | 281 | expect( |
| 284 | 282 | jobByToken(f.app.db, offered.id, first.token, 1_000 + 2 * LEASE_MS), |
| 285 | 283 | ).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); | |
| 287 | 285 | const second = claimJob( |
| 288 | 286 | f.app.db, |
| 289 | 287 | { id: two.id, cells: true }, |
| @@ -294,27 +292,6 @@ describe("the desk", () => { | ||
| 294 | 292 | expect(withdrawJob(f.app.db, offered.id)).toBe(false); |
| 295 | 293 | dropJob(f.app.db, offered.id); |
| 296 | 294 | }); |
| 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 | }); | |
| 318 | 295 | }); |
| 319 | 296 | |
| 320 | 297 | describe("a pull's check with FORGE_CHECKS_PLACEMENT=runner", () => { |
| @@ -496,50 +473,6 @@ describe("a pull's check with FORGE_CHECKS_PLACEMENT=runner", () => { | ||
| 496 | 473 | expect((await api(door, claim.token, { lines: ["x"] })).status).toBe(404); |
| 497 | 474 | }); |
| 498 | 475 | |
| 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 | ||
| 543 | 476 | test("a runner that says done with no steps does not make the check green: the forge runs it", async () => { |
| 544 | 477 | const lazy = enrolRunner(f.app.db, "lazy", f.alice.id); |
| 545 | 478 | const asked = api("/api/v1/runner/claim", lazy.token, { wait: 20, cells: true }); |
new above
offline