poll_next_task
Atomically claims the oldest pending contract assigned to your agent and verifies local file fingerprints, returning the claimed work or an empty-queue result.
Instructions
Atomically claim the oldest pending SUBMITTED contract assigned to this agent and verify local file fingerprints.
Purpose
Retrieves and claims next available contract work for the current agent, transitioning it from SUBMITTED to CLAIMED.
Usage Guidelines
When to use: Call periodically or when notified to discover and atomically take ownership of pending tasks assigned to your agent_id.
When NOT to use:
Do NOT use to non-destructively inspect queue depth or monitor status without claiming (use inspect_mesh_state).
Do NOT use to create contracts (use dispatch_contract).
IMPORTANT (ADR-0004): Pointers are an immutable baseline snapshot ("what I read"). Never edit pointer files directly on disk while holding a review contract; deliver any proposed code fixes via structured_output.fixes in fulfill_contract.
Behavior & Side Effects
Mutates database: Atomically claims the oldest SUBMITTED contract for agent_id using optimistic concurrency (UPDATE ... WHERE status='SUBMITTED', checked by changes===1). Starts the exec_ttl_sec window.
Lazy TTL sweep: Automatically sweeps and marks expired contracts as TIMED_OUT using lock-free read-before-write probing.
Fingerprint verification: Verifies Existence -> Byte Size -> SHA-256 for all pointers. If on-disk files were altered or missing, marks the contract POINTER_STALE and transparently advances to claim the next valid candidate in the queue.
Return Value
When work is claimed: { ok: true, has_task: true, status: 'CLAIMED', contract: { contract_id, requester_agent, target_agent, task_type, title, context_summary, workspace_root, pointers, contract_schema, claimed_at, exec_ttl_sec } }.
When queue is empty: { ok: true, has_task: false, message: 'No pending contracts for agent "<agent_id>"' }.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| agent_id | No | Worker agent identifier claiming tasks (defaults to AGENT_NAME env var, e.g., "workbuddy"). |