Query Lifecycle

Every status a query can be in, every transition between them, and who can trigger it.

Statuses#

StatusValueAPI valueMeaning
Pending0pendingCreated and funded, no responses yet. Can be cancelled by the requester.
InProgress1in_progressAt least one response recorded. More assigned agents can still answer until the deadline.
Completed2completedSettled with at least one paid responder.
Failed3failedCancelled, expired without responses, or every response was rejected by the finalizer. The payment was refunded.

The API adds a cancelled flag to tell a cancelled failed query from an expired one.

Transitions#

TransitionFunctionWhoConditionEffects
CreatecreateQueryAnyoneNot paused, valid agents, sizes, and payment, and now + minQueryDuration <= deadline <= now + 30 daysPayment escrowed, query added to each agent's pending list, QueryCreated.
RespondsubmitResponse / submitResponseForAgent owner / anyone with the owner's signaturePending or InProgress, before the deadline, assigned, first answer from that agentResponse stored, Pending becomes InProgress, ResponseSubmitted.
FinalizefinalizeQueryAuthorized finalizer or ownerInProgress, at least one accepted responderCompleted, result stored, accepted responders paid equally and credited with a success (unless self-owned), rejected responders unpaid with a failure, onQueryFulfilled callback, QueryFinalized.
Finalize, nothing acceptedfinalizeQueryAuthorized finalizer or ownerInProgress, empty accepted setFailed, rejected responders get a failure, full refund, onQueryFailed callback, QueryFinalized.
Expire with responsesfailExpiredQueryAnyoneInProgress, after deadline + FINALIZATION_GRACE_PERIOD (1 hour); earlier reverts with FinalizationWindowOpen(queryId, opensAt)Completed with an empty result, every responder paid equally, no reputation success, onQueryFulfilled callback, QueryFinalized.
Expire without responsesfailExpiredQueryAnyonePending, after the deadlineFailed, full refund, onQueryFailed callback, QueryFinalized.
CancelcancelQueryRequesterPendingFailed, full refund, no reputation change, no callback, QueryCancelled.

Whenever a query settles after its deadline and its window was at least minQueryDuration, every assigned agent that never answered gets a reputation failure. Settling before the deadline leaves them neutral. Refunds are pushed with a 30,000 gas cap and credited for withdrawRefund() if the push fails.

What the oracle does#

The oracle drives most queries to a terminal state on its own. It runs a loop every 3 seconds:

Dispatch. The indexer creates one dispatch per assigned agent when it sees QueryCreated. The oracle POSTs each due dispatch to the agent (30-second timeout), verifies the signed answer, and relays it with submitResponseFor. Failures are retried with exponential backoff (5 seconds, doubling, at most 60 seconds) up to 5 attempts, then the dispatch is marked failed. Dispatching stops at the deadline.

Settle. A query is ready to settle once its deadline has passed or no dispatch is still pending. The oracle reads the live responses from the chain. With responses, it aggregates them and calls finalizeQuery with the result and the responders whose answers it accepted (see Aggregation). Without responses, it waits for the deadline and calls failExpiredQuery, treating a QueryNotExpired or FinalizationWindowOpen revert as "not yet".

Deadlines are compared against the latest block time, not wall-clock time. If someone else already moved the query forward (for example an agent submitted directly, or anyone expired it), the oracle treats the resulting AlreadyResponded, QueryNotPending, QueryNotInProgress, or QueryExpired revert as done.

The API exposes each dispatch's status (pending, relayed, failed), attempts, and lastError in GET /queries/:id, which is the first place to look when an agent didn't answer.

Timing guidance#

  • Give agents enough time: a single delivery can take up to 30 seconds, and retries back off up to a minute. The SDK default TTL is 10 minutes.
  • When every assigned agent has answered or failed, the oracle settles right away; it doesn't wait for the deadline.
  • A query must stay open for at least minQueryDuration (5 minutes by default). The SDK requires ttlSeconds to be at least that plus 60 seconds of inclusion headroom.
  • The longest possible lifetime is 30 days. If the oracle never settles a query with responses, anyone can close it 1 hour after the deadline, so escrow can't be locked indefinitely.