Skip to content

[T-012] RunQueue: atomic claim with SKIP LOCKED, priorities, and reaper SQL #64

Closed

Description

@CodewithJha

Task T-012 · Milestone M3 — Oracles + runner + store · Size M · Priority P0 · Epic #3

Implements FR-RUN-001 · FR-RUN-002 · NFR-REL-001

Branch feat/t-012-runqueue-atomic-claim-with-skip — one task = one branch = one PR (doc 32 §6)

Summary

Implement PostgresRunQueue (doc 05 §13 port RunQueue) with the exact claim and reap statements of doc 11 §4: backend-filtered, priority-ordered, exactly-once claims and heartbeat-based re-queueing.

Context / motivation

This is the heart of FR-RUN-001's acceptance criterion: 4 workers × 100 runs, every run executes exactly once.

Scope

In scope

  • Port RunQueue in backend/src/roboprobe/runner/queue.py (Protocol: claim(worker_id, backend) -> ClaimedRun | None, mark_running, mark_evaluating, complete(run_id, outcome), fail(run_id, infra_error), reap(now, heartbeat_timeout_s, max_attempts) -> int, depth(backend) -> int).
  • Adapter backend/src/roboprobe/store/queue_pg.py with the SQL from doc 11 §4 (claim joins campaign status='RUNNING'; reap uses worker heartbeat).
  • State transitions follow doc 11 §5 run state machine; illegal transitions raise ConflictError.
  • Emits run.started/run.finished outbox events (via T-011c).

Out of scope

  • Reaper scheduling loop in orchestrator (T-042a)
  • Worker process (T-040a)

Implementation guidance

  • Use text() SQL for claim/reap to keep the exact semantics; return a typed ClaimedRun (run id, campaign id, scenario, seed, attempt, timeouts). Priority: lower = sooner (manual/confirm runs = 10).
  • Standards: respect the layering contracts (doc 16 §2) and conventions in doc 34; no hard-coded tunables — thresholds/defaults come from typed settings (doc 27); no magic numbers (ruff PLR2004); public APIs typed (mypy strict) and documented.

Configuration keys

From the configuration reference (doc 27). Keys marked (new) must be added to the typed settings models and appear in the generated reference (T-003c).

  • runner.heartbeat_timeout_s
  • runner.max_attempts

Acceptance criteria

  • Integration test: 4 concurrent claimers × 100 runs → each run claimed exactly once, none lost.
  • Priority ordering respected; only runs of RUNNING campaigns and matching backend are claimed.
  • Reap: runs of a stale worker return to PENDING while attempt < max_attempts, else ERROR + verdict INVALID (fake clock / explicit now).
  • Queue contract tests (T-015) pass.
  • Tests listed below added and green in CI; coverage of new code meets the gate in doc 18 §3.
  • Docs, config reference and CHANGELOG Unreleased updated where behaviour, keys or interfaces changed.
  • Definition of Done (doc 32 §3) satisfied.

Tests required

  • tests/integration/store/test_queue_exactly_once.py (threads/processes)
  • tests/integration/store/test_reap.py.

Dependencies

Blocked by: #61 (T-011a), #62 (T-011b)

Blocks: #80 (T-040a), #82 (T-042a)

Parallelization notes

Files / paths owned by this task — avoid editing other paths; shared files (pyproject.toml entry points, cli/main.py, api/routers/__init__.py, router.tsx, docker/compose.yaml, ci.yml) are append-only:

  • backend/src/roboprobe/runner/queue.py
  • backend/src/roboprobe/store/queue_pg.py
  • backend/tests/integration/store/test_queue_*.py

Owns queue modules only.

Definition of Done

Per doc 32 §3 and AGENTS.md §5: lint, format, mypy strict and import-linter green; tests added and passing; docs updated; PR template completed with Closes #<this issue>; Conventional Commit messages referencing T-012 (doc 16 §6); commits authored only by the repository owner identity (AGENTS.md §4); CI green; reviewer checklist (doc 32 §5.1) satisfied.

References

Design docs: doc 11 §4 · doc 11 §5 · doc 05 §5.3 · doc 05 §13 · ADR-0007 · doc 02 §3.3

Requirements: FR-RUN-001 · FR-RUN-002 · NFR-REL-001 (traceability: doc 28)

External:

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:runnerQueue, workers, campaign orchestrationarea:storagePostgres schema, repositories, artifactsmilestone:M3M3 — Oracles + runner + storepriority:P0Critical path / blocks otherssize:M1-3 daystaskChild task of a milestone epictype:featureNew functionality

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions