Work Orders
Work orders are transient operations that Brokkr routes to agents for execution. Unlike deployment objects which represent persistent desired state, work orders are one-time operations such as container builds, tests, or backups.
An admin or a generator creates a work order. To launch one with the CLI, see Launch a Job with a Work Order. The commands are in the CLI reference.
Concepts
Section titled “Concepts”Work Order vs Deployment Object
Section titled “Work Order vs Deployment Object”| Aspect | Deployment Object | Work Order |
|---|---|---|
| Purpose | Persistent state | One-time operation |
| Lifecycle | Applied, reconciled, deleted | Created, claimed, completed |
| Examples | Deployments, ConfigMaps | Container builds, tests |
| Storage | Permanent in stack | Moved to log after completion |
| Start | When an agent reconciles | As soon as an agent claims it. A work order has no start time and no schedule |
Work Order Lifecycle
Section titled “Work Order Lifecycle”PENDING -> CLAIMED -> (success) -> work_order_log \-> (failure) -> RETRY_PENDING -> PENDING (retry) \-> work_order_log (max retries)- PENDING: Work order created, waiting for an agent to claim
- CLAIMED: Agent has claimed the work order and is executing
- RETRY_PENDING: Execution failed, waiting for retry backoff
- Completed: Moved to
work_order_log(success or max retries exceeded)
Targeting
Section titled “Targeting”Work orders are routed to agents using the same targeting mechanisms as stacks:
- Direct agent IDs: Route to specific agents by UUID
- Labels: Route to agents with matching labels (OR logic). A label is one string, for example
env:prod - Annotations: Route to agents with matching annotations (OR logic). An annotation is a key and a value
An agent can claim a work order if it matches ANY of the specified targeting criteria.
When a work order is created, the broker sends it on the WebSocket channel to each connected agent that it reaches by any of these criteria. An agent that is not connected finds the work order on its next poll. A work order that becomes claimable again later (a retry that comes due, a claim that timed out) is not sent again: the agent finds it on its next poll.
An agent runs one work order at a time. A work order that arrives while the agent is busy waits. The agent claims it when the work order before it ends, not at its next poll.
A work order that a generator owns reaches only the agents registered with that generator. The rule is the same for each targeting method: an agent that is not registered with the owner does not get the work order in its pending list and cannot claim it, also when it has a matching label or annotation, or a target row from before it was deregistered. A target agent that is not registered with the owner is refused at creation with 403 agent_not_registered. A work order that an admin owns has no such limit.
Each work order has an owner: who created it. owner_generator_id names the generator. No value means an admin owns it. The owner is set at creation and does not change, and the work order log entry keeps it.
When a generator is deleted, the work orders it owns that still wait are deleted with it. A claimed work order runs to its end, and the log entries stay.
When a Target Agent Is Deleted
Section titled “When a Target Agent Is Deleted”A work order cannot name a deleted agent as a target: the create request answers 404 agent_not_found.
When an agent is deleted, the broker removes it from the targets of each work order and gives back the work orders it had claimed. A waiting work order that then has no target agent, no label and no annotation can be claimed by no agent. The delete of the agent deletes that work order too. It gets no entry in the work order log.
Work Types
Section titled “Work Types”work_type accepts exactly two values. Any other value fails the work order with Unknown work type.
Build (build)
Section titled “Build (build)”Container image builds using Shipwright. The yaml_content should contain a Shipwright Build specification.
The agent watches the resulting BuildRun to a terminal state, so success means the image was built.
The watch has a limit: the smaller of 15 minutes and the window from claim_timeout_seconds (10% less, minimum 60 seconds less, as for a Job). A BuildRun that is still running at the end of the window fails the work order terminally. The work order is not retried, and the agent does not cancel the BuildRun.
See Container Builds with Shipwright for details.
Custom (custom)
Section titled “Custom (custom)”Arbitrary Kubernetes YAML, applied by the agent with server-side apply. yaml_content may hold multiple documents.
Completion depends on what the work order contains:
| Content | What “success” means |
|---|---|
One or more batch/v1 Job resources |
Every Job reached a terminal state and succeeded. A Job that runs and fails is a terminal failure, not retried |
| Any other kind (Deployment, ConfigMap, CronJob, custom resources) | The resources were applied. Nothing is watched, so a workload that later fails is still recorded as a successful work order. The result message names the kinds that were applied without monitoring |
Additional constraints on the Job path:
- Each watched Job must have an explicit
metadata.name.generateNameis not supported and fails the work order terminally. - The watch is bounded by a budget derived from
claim_timeout_seconds, held back by 10% (minimum 60 seconds) so the watch cannot outlive the claim. A Job still running when the budget expires fails the work order terminally rather than being retried, because a retry would re-dispatch the order while the original Job is still executing. - A Job that is deleted before reaching a terminal state fails the work order terminally; its outcome is unknown.
API Reference
Section titled “API Reference”Who May Call
Section titled “Who May Call”| Route | Admin PAK | Generator PAK | Agent PAK |
|---|---|---|---|
POST /work-orders |
Yes. The work order is admin-owned. | Yes. The generator owns it, and each target agent must be registered with it. | No |
GET /work-orders |
Each work order | The work orders it owns | No |
GET /work-orders/{id} |
Yes | Only if it owns the work order | No |
DELETE /work-orders/{id} |
Yes | Only if it owns the work order | No |
GET /work-order-log |
Each entry | The entries of the work orders it owned | No |
GET /work-order-log/{id} |
Yes | Only if it owned the work order | No |
GET /agents/{id}/work-orders/pending, claim, complete |
Yes | No | The agent itself |
A generator that asks for a work order of another generator, or of an admin, gets 403 work_order_not_owned. This is also the answer when the admin’s work order targets the generator’s own agents. An agent PAK on a route for people gets 403 work_order_access_denied.
The examples below use an admin PAK. A generator PAK works the same way on the routes of the table.
Create Work Order
Section titled “Create Work Order”POST /api/v1/work-ordersAuthorization: Bearer <admin-pak>Content-Type: application/json
{ "work_type": "build", "yaml_content": "<shipwright-build-yaml>", "max_retries": 3, "backoff_seconds": 60, "claim_timeout_seconds": 3600, "targeting": { "labels": ["env:dev"], "annotations": {"capability": "builder"} }}Parameters:
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
work_type |
string | Yes | - | build or custom (see Work Types) |
yaml_content |
string | Yes | - | YAML content for the work |
max_retries |
integer | No | 3 | Maximum retry attempts |
backoff_seconds |
integer | No | 60 | Base backoff for exponential retry |
claim_timeout_seconds |
integer | No | 3600 | Seconds before claimed work is considered stale |
targeting |
object | Yes | - | Targeting configuration |
targeting.agent_ids |
array | No | - | Direct agent UUIDs |
targeting.labels |
array | No | - | Agent labels to match |
targeting.annotations |
object | No | - | Agent annotations to match |
List Work Orders
Section titled “List Work Orders”GET /api/v1/work-orders?status=PENDING&work_type=buildAuthorization: Bearer <admin-pak>Query Parameters:
| Parameter | Description |
|---|---|
status |
Filter by status (PENDING, CLAIMED, RETRY_PENDING) |
work_type |
Filter by work type |
Get Work Order
Section titled “Get Work Order”GET /api/v1/work-orders/:idAuthorization: Bearer <admin-pak>Cancel Work Order
Section titled “Cancel Work Order”DELETE /api/v1/work-orders/:idAuthorization: Bearer <admin-pak>Deletes a work order that has not ended. It gets no entry in the work order log. A work order that an agent has claimed is deleted too, but the agent does not stop the job it started: the job runs to its end in the cluster.
Get Pending Work Orders (Agent)
Section titled “Get Pending Work Orders (Agent)”GET /api/v1/agents/:agent_id/work-orders/pending?work_type=buildAuthorization: Bearer <agent-pak>Returns work orders that the agent can claim based on targeting rules.
Claim Work Order (Agent)
Section titled “Claim Work Order (Agent)”POST /api/v1/work-orders/:id/claimAuthorization: Bearer <agent-pak>Content-Type: application/json
{ "agent_id": "<agent-uuid>"}Atomically claims the work order. Returns 404 Not Found if the work order does not exist or is not in a claimable state.
Complete Work Order (Agent)
Section titled “Complete Work Order (Agent)”POST /api/v1/work-orders/:id/completeAuthorization: Bearer <agent-pak>Content-Type: application/json
{ "success": true, "message": "sha256:abc123..."}Parameters:
| Field | Type | Description |
|---|---|---|
success |
boolean | Whether the work completed successfully |
message |
string | Optional result message (image digest on success, error on failure) |
retryable |
boolean | Whether the work order can be retried on failure (default: true) |
Responses: this endpoint has two success shapes, and a client must branch on the status code.
| Status | Body | When |
|---|---|---|
| 200 OK | The WorkOrderLog entry |
The work order reached a terminal state — success, a non-retryable failure, or a failure with no retries left. The work order has moved to the log |
| 202 Accepted | {"status": "retry_scheduled"} |
A retryable failure with retries remaining. The work order stays in the queue as RETRY_PENDING and no log entry exists yet |
The 202 is deliberately omitted from the OpenAPI document, so SDKs generated from the spec do not model it.
Get Work Order Details
Section titled “Get Work Order Details”When retrieving a work order, the response includes error tracking fields:
| Field | Type | Description |
|---|---|---|
last_error |
string | Error message from the most recent failed attempt (null if no failures) |
last_error_at |
timestamp | When the last error occurred (null if no failures) |
retry_count |
integer | Number of retry attempts so far |
next_retry_after |
timestamp | When the work order will be eligible for retry (null if not in retry) |
These fields help diagnose failed work orders without needing to check the work order log.
Work Order Log
Section titled “Work Order Log”Completed work orders are moved to the log for audit purposes. A log entry keeps the id and the owner of its work order.
# List completed work ordersGET /api/v1/work-order-log?work_type=build&success=true&limit=100Authorization: Bearer <admin-pak>
# Get specific completed work orderGET /api/v1/work-order-log/:idAuthorization: Bearer <admin-pak>Query Parameters:
| Parameter | Description |
|---|---|
work_type |
Filter by work type |
success |
Filter by success status (true/false) |
agent_id |
Filter by agent that executed |
limit |
Maximum results to return |
Pod Logs and Events of a Work Order
Section titled “Pod Logs and Events of a Work Order”The agent that claimed a work order sends the pod log lines and the Kubernetes events of its job. They stay readable after the work order ends: the id is the same in the work order log.
What the agent sends:
- The log lines of each container of each pod of a
Jobin acustomwork order, and of the pods of theBuildRunof abuildwork order. No opt-in annotation is needed. The agent starts when the pod appears and reads each container to its end. - The Kubernetes events about the objects of the work order and about those pods.
- At most 100 lines each second for one container. Lines over that limit are dropped, with no gap marker.
What it does not send:
- The logs of a pod with any other owner, for example the pods of a Deployment in a
customwork order. - The lines of a work order that waits for a retry: the broker stores a line only from the agent that holds the claim, or that held it when the work order ended.
- Anything to a broker from before 0.12.0. Such a broker cannot read these frames. The agent sends them only when the broker’s answer to the WebSocket upgrade has
x-brokkr-wire-capabilities: work-order-telemetry.
See Agent Annotations and Labels for the mark brokkr.io/work-order-id.
# The pod log lines of the job, newest firstGET /api/v1/work-orders/:id/logs?since=<ISO-8601>&limit=500Authorization: Bearer <admin-pak or the owner's generator-pak>
# The Kubernetes events of the job, newest firstGET /api/v1/work-orders/:id/events?since=<ISO-8601>&limit=500Authorization: Bearer <admin-pak or the owner's generator-pak>The query parameters and the response are those of GET /api/v1/stacks/:id/logs and GET /api/v1/stacks/:id/events. Each row has a work_order_id and no stack_id.
| Who | Result |
|---|---|
| An admin | Reads each work order. |
| The generator that owns the work order | Reads it. |
| Another generator, or any generator for a work order that an admin owns | 403 work_order_not_owned |
| An id that no work order and no log entry has | 404 work_order_not_found |
The broker stores a line or an event of a work order only from the agent that claimed it. It drops any other.
Retention is the same as for a stack: at most 6 hours. Brokkr is not a log store.
Timing
Section titled “Timing”Each number that decides when a work order runs, how long it can run, and when Brokkr tries again.
| What | Value | Where it is set |
|---|---|---|
| Start | No start time and no schedule. A work order can be claimed as soon as it exists | Not configurable |
| Time until an agent claims it | The agent’s next poll: 10 seconds by default. At creation the broker also sends a WebSocket message to each connected agent that the work order reaches, and the agent then polls | agent.polling_interval in the agent configuration |
| Expiry of a work order that waits | None. A PENDING work order waits until an agent claims it or someone deletes it |
Not configurable |
| Claim timeout | claim_timeout_seconds, default 3600. After this time the broker releases the claim |
Per work order, at creation (--claim-timeout in the CLI) |
Watch window of a custom Job |
The claim timeout, less 10% and never less than 60 seconds less. 3240 seconds at the default | Follows claim_timeout_seconds |
Watch window of a build |
The same window, but never more than 900 seconds (15 minutes) | The 900 seconds are a constant of the agent |
| Retries | max_retries, default 3 |
Per work order, at creation (--max-retries) |
| Wait before a retry | backoff_seconds, default 60, times 2 for each try so far. The first retry waits 2 × backoff_seconds, the second 4 ×, the third 8 × |
Per work order, at creation (--backoff) |
| Sweep for retries that are due and for stale claims | Every 10 seconds | A constant of the broker |
| Retention of pod logs and events | At most 6 hours | A constant of the broker |
| Pod log lines sent | At most 100 lines each second for one container | A constant of the agent |
A retry that comes due and a claim that the broker releases are not pushed to agents. The agent finds the work order on its next poll, so add the sweep and one poll interval to the waits above.
Retry Behavior
Section titled “Retry Behavior”When a work order fails:
- Agent reports failure via
/completewithsuccess: false - Broker increments
retry_count - If
retry_count < max_retries:- Status set to
RETRY_PENDING next_retry_aftercalculated with exponential backoff- After backoff period, status returns to
PENDING
- Status set to
- If
retry_count >= max_retries:- Work order moved to
work_order_logwithsuccess: false
- Work order moved to
Backoff Formula:
next_retry_after = now + (backoff_seconds * 2^retry_count)retry_count here is the count after the failure, so it is 1 for the first retry: the first wait is 2 × backoff_seconds. A failure that the agent reports as not retryable goes to the log at once, whatever max_retries is. A Job that ran and failed, and a Job or a BuildRun that did not finish in its watch window, are such failures.
Stale Claim Detection
Section titled “Stale Claim Detection”The broker runs a background job every 10 seconds to detect and recover stale claims. A claim is considered stale when an agent has held a work order for longer than claim_timeout_seconds without completing it.
When a stale claim is detected:
- The work order’s
claimed_attimestamp is compared against the current time - If the elapsed time exceeds
claim_timeout_seconds, the claim is released - The work order status returns to
PENDING - The
claimed_byandclaimed_atfields are cleared, allowing any eligible agent to claim it
Reclamation does not increment retry_count; a stale claim does not count as a failed attempt.
This mechanism handles several failure scenarios:
- Agent crashes: If an agent crashes mid-execution, the work order becomes claimable again
- Network partitions: If an agent loses connectivity, work doesn’t remain stuck indefinitely
- Slow operations: Legitimate long-running operations should set an appropriate
claim_timeout_secondsvalue
The default claim_timeout_seconds is 3600 (1 hour). For a long custom Job, increase this value in the work order creation request: the Job watch window grows with it. A build cannot run longer than 15 minutes, whatever the claim timeout. A claim timeout below 1000 seconds makes the build window shorter than 15 minutes (see Build).
Example: Container Build
Section titled “Example: Container Build”# Create a build work ordercurl -X POST http://localhost:3000/api/v1/work-orders \ -H "Authorization: Bearer $ADMIN_PAK" \ -H "Content-Type: application/json" \ -d '{ "work_type": "build", "yaml_content": "apiVersion: shipwright.io/v1beta1\nkind: Build\nmetadata:\n name: my-build\nspec:\n source:\n type: Git\n git:\n url: https://github.com/org/repo\n strategy:\n name: buildah\n kind: ClusterBuildStrategy\n output:\n image: ttl.sh/my-image:latest", "targeting": { "labels": ["capability:builder"] } }'
# Check statuscurl http://localhost:3000/api/v1/work-orders/$WORK_ORDER_ID \ -H "Authorization: Bearer $ADMIN_PAK"
# View completed buildscurl "http://localhost:3000/api/v1/work-order-log?work_type=build" \ -H "Authorization: Bearer $ADMIN_PAK"Database Schema
Section titled “Database Schema”Work orders use a two-table design:
work_orders: Active queue for routing and retry managementwork_order_log: Audit trail of completed work
This separation optimizes queue operations while maintaining complete execution history. An entry leaves the log only when its owner generator is hard-deleted. A soft delete of the generator, which is what DELETE /api/v1/generators/:id does, keeps the entries.