Implements sharang/werkbank#3 — the control-plane pull queue behind the Werkbank runner flow.
JobQueue (over a werkbank_jobs collection)
enqueue — idempotent by job id (unique index; a duplicate is a no-op).
lease — atomic find-and-modify of the oldest queued job the runner can run, matched by executor and by labels (the job's labels must be a subset of the runner's; empty/absent matches any). Returns the job + a lease token and bumps attempts.
heartbeat — extends the lease, flips leased→running, and surfaces a cancel request. None means the lease was lost (token mismatch / already terminal) → the runner abandons the work.
complete — records the terminal result, token-guarded and only from an active state, so it's idempotent (a late/duplicate submission matches nothing).
cancel — a still-queued job goes straight to cancelled; an in-flight one is flagged so the runner sees it on its next heartbeat.
sweep_expired — the visibility timeout: expired leases return to queued, or become expired once attempts hit the max — so a crashed runner's job recovers.
Every transition is a single atomic Mongo update guarded by the lease token, so two runners can never both own a job. Every operation takes an explicit now for deterministic tests.
Contract + schema
Adds JobRecord / LeasedJob / HeartbeatAck to compliance-core::models::werkbank (timestamps persist as BSON dates so the FIFO/expiry range queries compare correctly), plus the three werkbank_jobs indexes (unique job.id; status+executor+created_at; status+lease_expires_at).
Tests
5 integration against a real MongoDB — idempotent enqueue, executor+label matching + FIFO ordering, heartbeat extend/cancel, token-guarded idempotent complete, and sweep requeue→expire. They skip cleanly when no Mongo is reachable (TEST_MONGODB_URI, defaults to the local dev cluster). Plus 2 unit tests. All green locally against certifai-mongo-1; clippy (agent + core) + fmt clean.
Note: the runner-facing HTTP transport (so a remote runner can reach this queue) and the runner itself are WB-03/WB-04.
Implements sharang/werkbank#3 — the control-plane pull queue behind the Werkbank runner flow.
## `JobQueue` (over a `werkbank_jobs` collection)
- **`enqueue`** — idempotent by job id (unique index; a duplicate is a no-op).
- **`lease`** — atomic find-and-modify of the oldest `queued` job the runner can run, matched by **executor** and by **labels** (the job's labels must be a subset of the runner's; empty/absent matches any). Returns the job + a lease token and bumps `attempts`.
- **`heartbeat`** — extends the lease, flips `leased`→`running`, and surfaces a cancel request. `None` means the lease was lost (token mismatch / already terminal) → the runner abandons the work.
- **`complete`** — records the terminal result, token-guarded and only from an active state, so it's **idempotent** (a late/duplicate submission matches nothing).
- **`cancel`** — a still-`queued` job goes straight to `cancelled`; an in-flight one is flagged so the runner sees it on its next heartbeat.
- **`sweep_expired`** — the **visibility timeout**: expired leases return to `queued`, or become `expired` once `attempts` hit the max — so a crashed runner's job recovers.
Every transition is a single atomic Mongo update guarded by the lease token, so two runners can never both own a job. Every operation takes an explicit `now` for deterministic tests.
## Contract + schema
Adds `JobRecord` / `LeasedJob` / `HeartbeatAck` to `compliance-core::models::werkbank` (timestamps persist as BSON dates so the FIFO/expiry range queries compare correctly), plus the three `werkbank_jobs` indexes (unique `job.id`; `status+executor+created_at`; `status+lease_expires_at`).
## Tests
**5 integration** against a real MongoDB — idempotent enqueue, executor+label matching + FIFO ordering, heartbeat extend/cancel, token-guarded idempotent complete, and sweep requeue→expire. They **skip cleanly** when no Mongo is reachable (`TEST_MONGODB_URI`, defaults to the local dev cluster). Plus **2 unit** tests. All green locally against `certifai-mongo-1`; clippy (agent + core) + fmt clean.
Note: the runner-facing HTTP transport (so a remote runner can reach this queue) and the runner itself are WB-03/WB-04.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
The control-plane pull queue behind the Werkbank runner flow (implements
sharang/werkbank#3). A JobQueue over a `werkbank_jobs` collection:
- enqueue — idempotent by job id (unique index; duplicate is a no-op)
- lease — atomic find-and-modify of the oldest queued job the runner can run,
matched by executor and by labels (job labels must be a subset of the runner's,
empty/absent matches any), returns the job + a lease token, bumps attempts
- heartbeat — extends the lease, flips leased→running, surfaces a cancel request;
None means the lease was lost (token mismatch / already terminal)
- complete — records the terminal result, token-guarded and only from an active
state, so it's idempotent
- cancel — queued→cancelled outright, in-flight flagged for the next heartbeat
- sweep_expired — the visibility timeout: expired leases go back to queued, or to
expired once attempts hit max, so a crashed runner's job recovers
All transitions are single atomic Mongo updates guarded by the lease token, so two
runners can never both own a job. Every op takes an explicit `now` for
deterministic tests. Adds JobRecord/LeasedJob/HeartbeatAck to the contract (BSON
datetimes so range queries compare correctly) and the werkbank_jobs indexes.
Tests: 5 integration against a real Mongo (idempotent enqueue, executor+label
matching + FIFO, heartbeat/cancel, token-guarded idempotent complete, sweep
requeue→expire; skip cleanly with no Mongo) + 2 unit. clippy + fmt clean.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
sharang
merged commit 633f945a1e into main2026-07-17 09:15:24 +00:00
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Implements sharang/werkbank#3 — the control-plane pull queue behind the Werkbank runner flow.
JobQueue(over awerkbank_jobscollection)enqueue— idempotent by job id (unique index; a duplicate is a no-op).lease— atomic find-and-modify of the oldestqueuedjob the runner can run, matched by executor and by labels (the job's labels must be a subset of the runner's; empty/absent matches any). Returns the job + a lease token and bumpsattempts.heartbeat— extends the lease, flipsleased→running, and surfaces a cancel request.Nonemeans the lease was lost (token mismatch / already terminal) → the runner abandons the work.complete— records the terminal result, token-guarded and only from an active state, so it's idempotent (a late/duplicate submission matches nothing).cancel— a still-queuedjob goes straight tocancelled; an in-flight one is flagged so the runner sees it on its next heartbeat.sweep_expired— the visibility timeout: expired leases return toqueued, or becomeexpiredonceattemptshit the max — so a crashed runner's job recovers.Every transition is a single atomic Mongo update guarded by the lease token, so two runners can never both own a job. Every operation takes an explicit
nowfor deterministic tests.Contract + schema
Adds
JobRecord/LeasedJob/HeartbeatAcktocompliance-core::models::werkbank(timestamps persist as BSON dates so the FIFO/expiry range queries compare correctly), plus the threewerkbank_jobsindexes (uniquejob.id;status+executor+created_at;status+lease_expires_at).Tests
5 integration against a real MongoDB — idempotent enqueue, executor+label matching + FIFO ordering, heartbeat extend/cancel, token-guarded idempotent complete, and sweep requeue→expire. They skip cleanly when no Mongo is reachable (
TEST_MONGODB_URI, defaults to the local dev cluster). Plus 2 unit tests. All green locally againstcertifai-mongo-1; clippy (agent + core) + fmt clean.Note: the runner-facing HTTP transport (so a remote runner can reach this queue) and the runner itself are WB-03/WB-04.
🤖 Generated with Claude Code