Skip to content

Runner queue: claim, heartbeat, complete, leases, settlement (DM-07) - #2154

Merged
neuromechanist merged 13 commits into
feature/portable-compute-epicfrom
feature/pc-dm07-runner-queue
Nov 14, 2026
Merged

neuromechanist merged 13 commits into
feature/portable-compute-epicfrom
feature/pc-dm07-runner-queue

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Card DM-07 (portable compute): the runner queue, claim, heartbeat, complete, leases, deadlines and the terminal settlement of accounting = 'none' jobs. Needs the Opus review (claim atomicity, lease semantics, at-most-once acceptance, terminal settlement of none jobs).

The claim statement, literally

CLAIM_RUNNER_JOB_SQL (backend/src/services/compute-runner-queue.ts), one conditional UPDATE per job, meta.changes = 1 is the winner test.
SET: runner_claimed_at = datetime('now'), runner_id = ?1, runner_backend = ?2 (the registration's), lease_id = ?3, lease_expires_at = datetime('now', '+600 seconds'), claim_attempts = claim_attempts + 1, webhook_nonce = COALESCE(webhook_nonce, ?4).
WHERE id = (SELECT j.id ... ORDER BY j.created_at ASC, j.id ASC LIMIT 1) AND the same terms again on the outer row, so the statement is atomic by itself, AND EXISTS (SELECT 1 FROM compute_runners r JOIN users u ON u.id = r.registered_by WHERE r.id = ?1 AND r.revoked_at IS NULL AND u.role = 'owner' AND u.status = 'approved' AND u.deleted_at IS NULL).
The terms (alias a): a.executor = 'runner' AND a.accounting = 'none' AND a.state = 'created' AND a.runner_claimed_at IS NULL AND a.records_written_at IS NOT NULL AND a.cancel_requested_at IS NULL AND a.claim_attempts < ?5 AND a.pool = ?6 AND a.cores_per_node <= ?7 AND COALESCE(a.memory_mb, 0) <= ?8 AND a.gpus <= ?9 AND EXISTS (SELECT 1 FROM compute_pipeline_stamps s JOIN datasets d ON d.dataset_id = s.dataset_id WHERE s.job_id = a.id AND <PIPELINE_ELIGIBLE_SQL>).
The dataset is found through the job's stamp (a runner job has no dataset column and a Zarr recipe stages no dataset); a job whose stamp is not linked yet is simply not claimed that round.
Each winner is found by its own random lease id, its launch specification is built with the Tapis path's buildLaunch, and launch_spec_digest is written by LAUNCH_DIGEST_UNDER_LEASE_SQL (guard: job, lease, runner, state = 'created', cancel_requested_at IS NULL); a winner whose digest write changes no row (a revoke or a cancel got in) is not answered.

Statements added or changed

Added in compute-runner-queue.ts: CLAIM_RUNNER_JOB_SQL, CLAIMED_BY_LEASE_SQL, LAUNCH_DIGEST_UNDER_LEASE_SQL, RELEASE_CLAIM_SQL, HEARTBEAT_STARTED_SQL (created to running, cancel guard), HEARTBEAT_UPLOADING_SQL (running to archiving, cancel guard), HEARTBEAT_RENEW_SQL, RECORD_USAGE_SQL (once, under the lease), CANCELLED_ROWS_SQL, RELEASE_EXPIRED_CLAIMS_SQL, EXHAUSTED_ROWS_SQL, UNREPORT_EXPIRED_SQL (cancel guard), UNREPORTED_ROWS_SQL, DEADLINE_ROWS_SQL, REPLAY_ROWS_SQL, RUNNER_UNCOPIED_SQL, RUNNER_CANCEL_PENDING_FAILED_SQL, RUNNER_COPY_ABANDONED_SQL.
Added in compute-lifecycle.ts: NONE_SETTLED_STAMP_SQL, CANCEL_WAITING_RUNNER_SQL, END_CANCELLED_STAMP_SQL, endWaitingRunnerJob.
Changed in compute-lifecycle.ts: commitTerminal is exported and, for job.accounting === 'none', builds NONE_SETTLED_STAMP_SQL instead of the ledger statements and SETTLED_STAMP_SQL (every other row's batch is unchanged); buildLaunch is exported and also returns the image reference, digest and SIF name; afterTerminal, recordsRetentionYears, launcherDocumentLimit are exported; requestComputeCancel ends a waiting runner job in one batch and leaves a claimed one with its recorded intent.
Changed in compute-jobs.ts: writeRecords is exported (the replay uses it).
Changed in compute-job-callbacks.ts: authorize also requires X-Compute-Lease equal to the row's lease_id for executor = 'runner', both present and matching LEASE_ID_RE; a missing, malformed or NULL value is the existing single refusal (job_not_found).
Changed in compute-status.ts, compute-admin.ts (contract words COMPUTE_RUNNER_ATTENTION_WORDS), admin-compute.ts (the two attention lines with their hints), index.ts (the tick), compute-runners.ts (routes POST /claim, /heartbeat, /complete).

Decisions the card left open

A started job whose lease expires goes unreported KEEPING its lease id (so a partitioned runner can still land outputs under the same lease); a heartbeat renews an unreported job's lease, so a live runner is not given up on.
The sweep order is replay, cancels, release and exhaustion, unreport, settle unreported, deadline, copy; one budget of RUNNER_SWEEP_BATCH rows is shared by the steps.
A job that is unclaimed with its claims used up (a revoke gave the last claim back) fails claims_exhausted instead of waiting for ever.
A claimed job whose specification can never be built ends failed launch_failed invalid_spec; one that cannot be built for want of configuration is given back.
The waiting-job cancel ends its pipeline stamp in the same batch (state failed, word job_cancelled, exactly what the stamp sync writes), so no change to syncPipelineStamps was needed.
RUNNER_MIN_VERSION = "0.1.0" lives in the queue module because the contract file is out of this card's scope.
The runner_lost details are not in the ComputeFailureDetail type (DM-04's), so the queue casts them in one place.

Gates

bun run typecheck, bun run lint, bunx biome check on the touched files: clean (the warnings printed are older any and ! uses in files this PR does not otherwise change).
bun test backend/test/compute-runner-queue.test.ts backend/test/cron-sweep-wiring.test.ts backend/test/compute-route-inventory.test.ts backend/test/compute-e2e.test.ts: 287 pass, 0 fail.
bun test backend/test/compute-lifecycle*.test.ts backend/test/compute-sweep.test.ts: 236 pass, 0 fail.
All backend/test/compute-* files: 1870 pass, 2 skip, 0 fail (run before the last two test cases were added; that file alone is 61 pass).
Races: 50 rounds on bun:sqlite with round-trip latency and 50 rounds on Miniflare's D1, two runners each, one waiting job per round: 100 rounds, exactly one winner in every one, claim_attempts 1.
Mutations (each run alone, then reverted, tree clean): 26 one-line mutations of the claim, lease, sweep, settlement, cancel, usage, copy and attention statements, every one killed by a named case (the cases name their mutation); the two first survivors (the usage guard and the lease compare of complete) got cases of their own.
Not run: the full bun test, would check: the epic gate, because it takes about 80 minutes and is the manager's.

Other

One existing DM-06 case changed: "the cancel sweep selects no runner row" now claims the job by SQL first, because a waiting runner job's cancel now ends in SQL (this card's rule) and only a claimed one keeps the intent.
UNCOPIED_TERMINAL_SQL still selects Tapis rows (DM-06 pins it), so the runner copy has its own list, RUNNER_UNCOPIED_SQL, called by the sweep.
Overlap with the epic D PR #2116 (files both touch): backend/src/index.ts, backend/src/services/compute-jobs.ts, backend/src/services/compute-lifecycle.ts, backend/src/services/compute-status.ts, backend/test/compute-route-inventory.test.ts, backend/test/compute-status-route.test.ts.
A waiting row whose specification can no longer be rebuilt to the recorded digest (the image or the dataset's commit changed) is left alone by the replay and stays waiting until cancelled.

Card DM-07.

Review round 1 (added)

Merged origin/feature/portable-compute-epic (DM-06b) before the final gates; the merge was clean. DM-06b changed the Zarr spec, so the records replay now rebuilds a Zarr specification from the same source check the dispatch uses (a call to GitHub and the serving index, in the slow phase), and accepts it only when its digest is the recorded one.

Fixes, by finding:

  1. A configuration gap no longer burns claims: launchConfigured is asked before any claim UPDATE (secret, API base address, records bucket); every give-back (RELEASE_CLAIM_SQL) now restores the counter (claim_attempts = MAX(claim_attempts - 1, 0)). Tests: no claim statement runs and five polls leave claim_attempts 0; an unbuildable specification ends failed launch_failed invalid_spec and the next job is still answered.
  2. A throw at claim (a records bucket that throws after the UPDATE committed) is caught per job: the claim goes back uncounted, the jobs that did build are answered, the loop stops. Test with a bucket that throws for one job.
  3. At most once acceptance: RUNNER_TERMINAL_TRANSITION_SQL (the Tapis statement plus AND lease_id IS ?17, bound to the lease the writer read, used for runner rows only) in commitTerminal; completeRunnerJob writes the usage AFTER the end and answers lease_lost when the row is no longer the body's runner and lease; RECORD_USAGE_SQL has no state term and keeps the lease and runner terms; COMPLETE_OUTPUTS_SQL, CLAIM_RESULTS_BYTES_SQL and RECORD_REFUSAL_SQL carry (?n = 0 OR lease_id IS ?m), bound for any job that is not a Tapis job. Race cases (an exact interleaving injected between two real statements of one real database): a completion whose claim was released and taken again (same runner and another), a completion that passed the check before a revoke, outputs-complete, the declared-bytes claim and the launch digest each under a replaced lease; each writes nothing and records nothing.
  4. The sweep cannot be starved: every step has its own budget of RUNNER_SWEEP_BATCH rows; every examined row is stamped (examined_at) and the steps order by it (rotation); a step whose batch was full is counted (saturated, in the log line); a waiting row whose records never landed and could not be replayed for 60 minutes ends failed records_unavailable (detail records_unreadable; the contract allows it and it says what is true), which frees its slot and tuple; a new attention word runner_records_unwritten (such a row still there after 120 minutes) is counted in the status and printed by the CLI. Test: 11 unreplayable rows do not stop a cancelled-and-expired job being ended, rotate over two calls, and are ended after the window.
  5. The lease check fails closed: job.executor !== "tapis" && !holdsCurrentLease(...); the outputs statements bind the lease for any non-Tapis row too. Conflict sites to watch when Epic: Third-party compute: access, user API, nemar compute, partner contract (#2021) #2116 is merged: JOB_COLUMNS in compute-jobs.ts (keep executor, accounting, lease_id and the other runner columns); commitTerminal in compute-lifecycle.ts versus Epic: Third-party compute: access, user API, nemar compute, partner contract (#2021) #2116's capPartnerCharge (keep the accounting === "none" branch and the runnerRow statement choice, or runner jobs are never settled); the CANCEL_PENDING_FAILED_SQL line carrying executor = 'tapis'; the ATTENTION_COUNTS insert next to partner_capped_no_final (keep ...RUNNER_ATTENTION_COUNTS); the same cancel-sweep statements in compute-jobs.ts and the two status tests.
  6. An uploading heartbeat starts a created job (with started_at); a claim whose outputs completed is never released (RELEASE_EXPIRED_CLAIMS_SQL) nor failed claims_exhausted: it is moved to unreported (UNREPORT_EXPIRED_SQL takes an unstarted claim with outputs) and verified. Tests for both.
  7. DEADLINE_ROWS_SQL no longer excludes a recorded cancel: such a job is ended cancelled at the deadline (without a cancel: deadline_exceeded). Test.
  8. A claim is bounded by the registration: each of cores, memory and GPUs is summed over the runner's live claims inside the claim statement (held(...)), so free_slots 32 on 8 cores and 40 GB gets two 4-core 16 GB jobs and the third waits for room. Test.
  9. The claim route runs only the cheap steps before answering; the verification of outputs, the records replay and the copies go to waitUntil (sweepRunnerLeases(env, db, { defer }), which logs its own line when the slow part ends). Test: a claim is answered while a verification waits on a gated results bucket, and the job is finished after the gate opens.
  10. A waiting runner job's cancel answers outcome: cancelled (a new word in COMPUTE_CANCEL_OUTCOMES); a claimed one still answers cancel_requested. Tests for both words.
  11. Follow-up (out of this card's scope): move RUNNER_MIN_VERSION, RUNNER_RECORDS_REPLAY_MINUTES and RUNNER_RECORDS_GIVE_UP_MINUTES into shared/contract/compute-runner.ts, where ADR 0121 says every number lives.
  12. New cases for the surviving mutants: executor = 'runner' of RUNNER_UNCOPIED_SQL, the lease term of LAUNCH_DIGEST_UNDER_LEASE_SQL, the lease term of RECORD_USAGE_SQL, claim_attempts < ?1 of the release (via the released count); the claim-statement test now checks every term for both the picked row and the row written.

Gates after the merge: bun run typecheck and bun run lint clean; biome clean on the touched files; the acceptance files 307 pass, 0 fail; compute-lifecycle* and compute-sweep 236 pass, 0 fail; all backend/test/compute-* 1898 pass, 2 skip, 0 fail; contract and CLI tests 218 pass, 0 fail.
Mutations after the fixes: the earlier 26 were rerun and killed (the M18 case by removing both terms of the unreport), and 24 new ones (N1 to N24) were run alone and reverted: 21 killed on the first run; the three survivors, N2 (the configuration check), N14 (the release outputs term, which is in two places) and N24 (the exhausted outputs term), got a new case each (N2: one that watches for a claim statement) and are killed.
Not run: the full bun test, would check: the epic gate.

Review round 2 (added)

Fixes, by finding (each has a case that fails without it; the mutation list is below):

  1. A stale outputs request no longer records under the new lease: in handleOutputs, when the declared-bytes claim changes no row and the row's lease is no longer the one the route proved, the request is refused bad_state without recording or presigning. The race case now asserts failure_detail is null and that the new holder's own outputs call succeeds.
  2. A throw while building a claimed job COUNTS the claim (RELEASE_CLAIM_COUNTED_SQL) and the loop goes on to the next job, so a poisoned row ends claims_exhausted after three claims and cannot stall the pool; only the Worker-wide unconfigured gap is given back uncounted. Case: a bucket that always throws for the middle job; the first and third are answered over two calls and the poisoned one ends after three counted claims.
  3. The claim UPDATE is inside its own try: a database error on the second statement ends the loop and the job already built is answered (no 500). Case: a database whose second claim statement throws.
  4. The replay of records is throttled over time (REPLAY_ROWS_SQL takes only rows not examined in the last 5 minutes, so ten unwritten rows with the store down cost one pass of GitHub requests per five minutes, not one per poll), runs LAST in the slow phase (verifications and copies first), each replay under its own bound (RUNNER_REPLAY_BOUND_MS, 8 s) and no replay starts after the phase's allowance (RUNNER_REPLAY_PHASE_MS, 20 s, under the 30 s waitUntil cap). Cases: a second poll within five minutes makes no GitHub request and one five minutes later does; a verification and its copy finish while a replay still waits on a stalled GitHub; a stalled replay holds the phase for the bound only; no replay starts after the allowance.
  5. The give-up is folded into the replay: a row aged 60 minutes is ended failed records_unavailable only when a replay in the same call finds a real mismatch (a stamp or recipe that is gone, a source that says another commit, a rebuilt specification that is not the recorded one); an unreachable source or a store that is down is not a mismatch. RECORDS_GIVE_UP_ROWS_SQL is gone. Cases for a replay that now succeeds (replayed, not given up on, records written on a live row), an unreachable source (left alone) and a mismatch (ended in the same call).
  6. RELEASE_EXPIRED_CLAIMS_SQL skips a claim whose outputs were declared (results_bytes); UNREPORT_EXPIRED_SQL takes such an unstarted claim (verified if the outputs completed, given up on after 60 minutes like any silent job otherwise), and EXHAUSTED_ROWS_SQL applies the outputs and declared terms to a claimed row only (an unclaimed row whose claims are used up is still failed). Cases for both.
  7. commitTerminal chooses the lease statement with job.executor !== "tapis", like the launcher routes (source-search case).
  8. launchConfigured includes the runtime image, so no poll claims a job only to give it back (case: no claim statement runs without an image).
  9. completeRunnerJob decides the plan (and its slow verification) once, commits it, and when that commit loses to a heartbeat that moved the state it commits the same plan once more from the row read anew while the lease is still the call's. Case: an uploading heartbeat injected between the verification and the commit; the answer is failed with the reported reason, not archiving.

Gates: bun run typecheck, bun run lint, biome on the touched files clean; acceptance files 319 pass, 0 fail; compute-lifecycle* and compute-sweep 236 pass, 0 fail; the queue file is 93 cases (was 80); all backend/test/compute-*: 1910 pass, 2 skip, 0 fail.
Mutations, each run alone and reverted (tree clean): P1 to P16 (stale record, uncounted give-back, stop at first throw, claim outside the try, no throttle, replays first, unbounded replay, no phase allowance, unreachable as mismatch, give up a fresh row, release declared claims (both copies of the term together), exhausted declared term, no second commit, fail-open lease statement, no image check, unreport of declared claims): all killed.

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR2154

Version: 0.10.34-PR2154.229470c
Commit: 229470c


🤖 Updated on each push.

@neuromechanist
neuromechanist merged commit ec2772a into feature/portable-compute-epic Nov 14, 2026
1 check passed
@neuromechanist
neuromechanist deleted the feature/pc-dm07-runner-queue branch November 14, 2026 02:08

This branch was previously deployed

1 inactive deployment
npm — 229470c7 Deployed Nov 14, 2026 by neuromechanist via publish #4435
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant