Work Items

Process batches with per-item results and retries, and limit concurrency to protect downstream capacity.

What Are Work Items?

Use for_each when the same capability must process several orders, documents, or records. Each item gets its own progress and retry history, so a temporary failure in one item can retry while completed results are retained.

Marking an input for_each: true tells Argyll to create one Work Item per collected value accepted by the Step’s Predicate, or every collected value when no Predicate is configured. Each Work Item executes independently with its own inputs, status, retries, and receipt token.

for_each requires the all or some Collection Policy. The input’s match script, if present, removes nonmatching candidates before Work Items are expanded. A Step Predicate, when present, then evaluates each complete Work Item candidate after all for_each inputs have been combined.

Basic Example

{
  "id": "process-item",
  "name": "Process Item",
  "type": "service",
  "http": {
    "invoke": {
      "endpoint": "https://api.example.com/items/process",
      "timeout": 5000
    }
  },
  "attributes": {
    "items": {
      "role": "required",
      "required": { "for_each": true, "collect": "all" }
    },
    "processed": { "role": "output" }
  }
}

Flow init:

{
  "items": [
    {"id": "item-1", "value": 10},
    {"id": "item-2", "value": 20}
  ]
}

Each entry in the init list is a candidate value for items. This example creates two Work Items, one per entry.

Multiple for_each Inputs

Multiple for_each Attributes produce every combination of their collected values (the Cartesian product):

{
  "attributes": {
    "regions": {
      "role": "required",
      "required": { "for_each": true, "collect": "all" }
    },
    "products": {
      "role": "required",
      "required": { "for_each": true, "collect": "all" }
    }
  }
}

With regions collecting ["US", "EU"] and products collecting ["A", "B"], the engine creates 4 Work Items: US×A, US×B, EU×A, EU×B.

Parallelism

Work Items execute sequentially when parallelism uses its default value of 1. Increase it through work_config to run several items concurrently:

{
  "work_config": {
    "parallelism": 5,
    "max_retries": 3,
    "init_backoff": 100,
    "max_backoff": 5000,
    "backoff_type": "exponential"
  }
}

Set parallelism to the number of concurrent requests your downstream service can handle.

Output Aggregation

Step outputs are only available once all created Work Items succeed. When multiple Work Items succeed, each aggregated output is an array, where each element includes the for_each input values alongside the output:

Work item 1 (item-1): processed = 100
Work item 2 (item-2): processed = 200

Aggregated "processed" attribute:
[
  { "items": {"id": "item-1", "value": 10}, "processed": 100 },
  { "items": {"id": "item-2", "value": 20}, "processed": 200 }
]

With one Work Item, the Step uses that item’s outputs directly. With zero collected candidates, the Step completes without creating Work Items or outputs, as when the Flow’s initial state declares the input with an empty array or an unfulfilled optional collection falls back to an empty default. A closed required match gate, or a Step Predicate that rejects every candidate, skips the Step instead.

Idempotency

HTTP invocations carry an Argyll-Receipt-Token header that stays the same across retries of a Work Item. Use it to make the handler idempotent: repeated delivery must produce one external effect. Store the result alongside your idempotency record so a repeated request can return it without repeating the action.

Invocation and compensation share the token. Keep their idempotency records separate, for example by keying on both the token and action. See Service Steps for callback responses and retry handling.

Partial Failure

A permanent Work Item failure marks the Step failed and abandons its pending items. Work Items already in progress still report their results and may have produced external effects. Configure compensation to reverse those effects after partial success.

Interaction with Predicates

A Step Predicate is evaluated once per candidate Work Item, after for_each expansion. See Predicates.