Skip to content

Apply the pipelines' live and daily caps to every dispatch path - #2122

Merged
neuromechanist merged 6 commits into
devfrom
fix/pipeline-caps
Nov 10, 2026
Merged

neuromechanist merged 6 commits into
devfrom
fix/pipeline-caps

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Refs #2121 (phase P1). Goes on dev; no migration, no queue, no switch added.

What was wrong

POST /admin/compute/pipeline/run (--eligible --limit N, N up to 100) called dispatchEligible, which created pipeline jobs with no live guard and no daily guard and stopped only on a refusal such as an empty pool.
The daily dispatcher applied pipelines.maxLiveJobs and pipelines.dailyJobCap only as reads before dispatching, so an admin run racing the cron could exceed them.
The comment on COMPUTE_PIPELINE_RUN_LIMIT_MAX ("the policy's daily cap is the real ceiling") was false.

Design

  • Live cap, atomic. createJob adds PIPELINE_LIVE_JOBS_GUARD_SQL to the hold's own statement for source = 'pipeline', the pattern LIVE_JOBS_GUARD_SQL uses for a person: pipeline jobs live in the pool (literal live states, created included) must be fewer than policy.pipelines.maxLiveJobs[pool].
    A refusal reads back as the existing over_concurrency (guard_refused), so the documented order of refusals is unchanged and a refused hold still writes no job, ledger row or pseudonym.
  • Deviation from the brief, please check: the guard does not bind the pipelines account id.
    Pipeline jobs may have a null account (compute-jobs.test.ts creates them), which account_id = ? would never count, and the cron's existing read counted every pipeline job regardless of account.
    One shared statement (PIPELINE_LIVE_JOBS_SQL) is both the guard and the read, so the room read equals the room enforced.
    Without the account term the planner does not use compute_jobs_live; it scans compute_jobs_examined, which is partial on the same live-state list, so the scan is over live rows only (a test pins this: partial live index, never the table).
  • Daily cap and bound, clamped (a choice of P1, not a limit of the hold: a guard could be passed through dispatchPipelineJob into createJob, as the third-party branch does with CLIENT_DAY_GUARD_SQL; PIPELINE_JOBS_TODAY_SQL is a full scan of compute_jobs that would then run inside every pipeline hold's write, and an account-bound count or a new index needs an account bind or a migration; an atomic daily guard belongs in P2 beside the tenant-ceiling guard). pipelineRoom(db, policy, pool) -> { daily_left, live_left } (moved into compute-pipeline.ts, used by the cron, the admin route and the dry run).
    dispatchEligible tries min(limit, COMPUTE_PIPELINE_RUN_PER_INVOCATION = 10, daily_left, live_left) for every actor, and stops with daily_cap_reached (new, 409, declared in COMPUTE_PIPELINE_ONLY_REFUSAL_STATUS) or over_concurrency when a cap, not the requested number, ended the run or the room is 0.
    The per-invocation bound is not a refusal (stopped_by null, room.limited_by = per_invocation).
  • Named dataset: bounded by the live guard (409 over_concurrency, stamp refused, no ledger row) and counted by, not refused by, the daily cap (comment says the owner may rule otherwise).
  • Response: optional room { daily_left, live_left, limited_by } (limit | per_invocation | daily_cap | live_cap, first on a tie), on live eligible runs and dry runs; the dry run trims would_dispatch to it and reports blocked: over_concurrency for a named dataset on a full pool.
  • Comment on COMPUTE_PIPELINE_RUN_LIMIT_MAX fixed; route sentence for over_concurrency is now "NEMAR's pipelines already have as many live jobs as the owner allows."; CLI help and print updated; ADR 0118 addendum dated 2026-11-10; CHANGELOG ### Fixed under Unreleased.

Tests (real SQLite with every migration, real admin router, the Tapis stand-in; no mocks)

New backend/test/compute-pipeline-caps.test.ts (12 tests): (a) limit 100 over 20 datasets with cpu cap 3 makes exactly 3 live jobs, 3 holds, 3 stamps, none refused, stops over_concurrency, room.live_left 3; (b) admin run vs cron via Promise.all on roundTripD1 (a round trip each side of every statement) for one slot, same dataset and different datasets, plus ten concurrent named datasets for three slots; (c) daily cap 2 with one job today dispatches 1 then daily_cap_reached, and a named dataset counts toward it without being refused; (d) named dataset on a full pool is 409 over_concurrency, stamp refused, ledger and job counts unchanged; (e) dry run reports the room and trims; (f) 25 datasets with room for all dispatch 10; the cron still dispatches only what the live cap allows.
Also: CLI tests (test/admin-compute-pipeline-cli.test.ts: help, live print, dry-run print, per-invocation print, named dataset exit 1), the contract unit test (new word, new schema, bound), and the plan check for the guard statement in compute-jobs-migration.test.ts.

On unmodified dev the new file fails 11 of 12 (the 12th is the cron regression test, which passes on both).

Mutation checks (one line broken by hand, restored each time):

Mutation Result
guard < ? to < ? + 1000 3 fail: (b) different datasets, (b) crowd of named datasets, (d)
live clamp term removed 4 fail: (a), full pool, both (e)
daily clamp term removed 2 fail: both (c)
daily stop word dropped 2 fail: both (c)
per-invocation bound 10 to 1000 1 fail: (f)

No existing test's expectation changed.
Passing: the new file, compute-pipeline* (compute-pipeline, dispatch, queue, routes, eligibility, verify, stamps-migration), compute-jobs, compute-jobs-migration, compute-ledger-race, compute-route-inventory, compute-switches-flag, compute-source-scan, compute-jobs-admin-routes, cron-sweep-wiring, the four admin-compute-*-cli suites, adr-index, changelog-format, the compute contract and state unit tests; bun run typecheck, bun run lint.

Not verified

  • The full bun test was not run (targeted tests only, per the repo rule).
  • The daily cap is a read-then-dispatch clamp, not a guard: racing runs can pass it by up to (racing runs minus 1) times min(10, daily room, live room). Only the live cap is atomic. The money ceiling is the pipelines pool's balance, enforced atomically by the same hold.
  • The admin-route race tests run on bun:sqlite behind roundTripD1; the guard itself is now also raced on Miniflare D1 in compute-jobs.test.ts (second review round).
  • Why 10: the bound's comment gives about 25 subrequests per dispatch against about 1,000, but ADR 0118 (review fixes, item 1) records 10,000 for the compute crons and asks for a measurement. No measurement exists for this route, so 10 is conservative and the comment says raising it needs one.
  • .context/decisions/README.md index line for 0118 left as is (the index test does not require a change).

Merge note for the third-party epic (#2116, on hold)

That branch rewrites the same guards block in createJob. Whoever merges dev into it must keep the pipeline branch of the guard (source === "pipeline"); the crowd test and test (d) in compute-pipeline-caps.test.ts fail if it is dropped.

Second round (review fixes)

Daily-cap rationale corrected; tick bound tied to the contract constant; both-engine guard tests (race, cap 0, pool independence, created counts, plan check on both engines); the day's count scanned once per tick and once per admin run or dry run; CHANGELOG folded into the compute bullet under Added.

Live cap guards the hold; daily cap, live cap and a 10 per invocation
bound clamp eligible runs. Tested on real SQLite; refs #2121
The cron passes its room to dispatchEligible, so the day's count is
scanned once per tick. CRON_DISPATCH_PER_TICK is the contract bound.
Targeted tests pass; refs #2121
Race, cap 0, pool independence, created counts, plan check on both
engines; mutation-checked. Refs #2121
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review round 2, pushed as c491124, d0392d8, 900bd36. Targeted tests, typecheck and lint pass.

  1. Daily-cap rationale (ADR item 2, the dispatchEligible docblock, the daily_cap_reached contract docblock): the false "the hold cannot tell them apart" is replaced with the real reasons (full scan SCAN compute_jobs, confirmed by EXPLAIN; the alternatives need an account bind or a migration; CLIENT_DAY_GUARD_SQL exists on feature/issue-2021-epic-compute-users, which I confirmed). Worst overshoot stated as (racing runs minus 1) times min(10, daily room, live room). Added the money-ceiling sentence (HOLD_INSERT_SQL, no budget pool, overruns can go below zero), the P2 note, and that no path returns daily_cap_reached as an HTTP status (it is only a stopped_by of a 200 or 201).
  2. Two constants: CRON_DISPATCH_PER_TICK = COMPUTE_PIPELINE_RUN_PER_INVOCATION, plus a pin test; the contract comment now carries the UNVERIFIED caveat about counting D1 and R2 calls as subrequests.
  3. Tests: compute-jobs.test.ts gains 5 cases on both engines (six calls racing for 2 slots, cap 0, cpu and gpu independent, a created job counts, person and pipeline jobs do not cross-count); the plan check in compute-jobs-migration.test.ts now runs on both engines. Mutations: guard < ? + 1000 fails 8 of 10; counting created out fails 6; ignoring the pool fails the independence cases on both engines (my first version of that test did not catch it, so I strengthened it by making a CPU job live first); dropping the state predicate fails the plan check. One existing case ("a pipeline job is held on the pipelines pool...") now sets its own pipelines caps, because Miniflare's D1 is never reset and the new cases leave live CPU jobs there.
  4. CHANGELOG: the separate ### Fixed is gone; the sentence is in the compute bullet under Added, "fixed before this release", with your wording on the dispatcher. changelog-format passes.
  5. Room read once: the cron passes its room to dispatchEligible (new optional room input) and passes the day's count to pipelineRoom, so a tick scans PIPELINE_JOBS_TODAY_SQL once (before: 2; a new test pins 1) and an admin run or a dry run scans once (also pinned). Counts, words and the log line are unchanged; all cron tests pass.

Also in the PR body: the merge note for #2116.

@github-actions

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR2122

Version: 0.10.33-PR2122.900bd36
Commit: 900bd36


🤖 Updated on each push.

This branch was previously deployed

1 inactive deployment
npm — 900bd367 Deployed Nov 10, 2026 by neuromechanist via publish #4386
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