> ## Documentation Index
> Fetch the complete documentation index at: https://docs.canary.effectiveai.app/llms.txt
> Use this file to discover all available pages before exploring further.

# task node

> Ask people to decide or approve something, and wait for an answer that matches a schema.

A `task` node creates a task in Effective, assigns it to users or groups, and by default waits for
one answer that matches a result schema. Use it for approvals and other decisions a person must
make. Use an [`agent`](https://docs.canary.effectiveai.app/procedures/reference/nodes/agent) when an agent session should do the work
instead.

## Syntax

```yaml task id=approve-payout theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
mode: await
assignees:
  - group:claims-managers
  - user:1042
title: Approve the payout on {{ $inputs.claim.claimNumber }}?
instructions: |
  Check the adjuster's estimate against the invoice, then approve or deny
  the payout of {{ $inputs.claim.amount }}.
input:
  schema: payout-input
  value: $inputs.claim
attachments: $inputs.documents
labels:
  - claims
  - payout-approval
result:
  schema: payout-decision
  prefill:
    approved: $inputs.proposal.recommendApproval
output: payout_approval
```

## Fields

| Field | Type | Required | Default | What it does |
| - | - | - | - | - |
| `mode` | string | No | `await` | `await` creates the task and waits for the answer. `create` creates the task and continues at once. See [Create without waiting](#create-without-waiting) |
| `assignees` | list or expression | Yes | None | The people and groups the task is assigned to: a nonempty list of up to 100 unique `user:` or `group:` selectors |
| `title` | string | Yes | None | The task's title. One line, not empty. Can interpolate values with `{{ }}` |
| `instructions` | Markdown string | Yes | None | What the assignee is asked to do. Can interpolate values with `{{ }}` |
| `input.schema` | schema id | No | None | An object schema for data shown with the task |
| `input.value` | data expression | With `input` | None | The data itself. It must match `input.schema` |
| `attachments` | expression | No | None | Up to 100 unique, nonempty strings: IDs of files or records to show the assignee as inputs |
| `labels` | list of strings | No | None | Unique, nonempty labels on the task |
| `result.schema` | schema id | Yes | None | The schema an answer must match. It also drives the task's form |
| `result.prefill` | data expression | No | None | Values to fill into the form when the task opens. See [Prefill](#prefill) |
| `output` | name | Yes | None | A new name for the [answer envelope](#the-answer-envelope), or for the task handle in `create` mode |

The fence takes no attributes after `id`, and its body takes no other fields. There is no timeout,
checkpoint or auto-approval field.

### Assignees

Each selector is `user:<user-id>` or `group:<group-id>`. IDs contain letters, digits, `.`, `_`,
`-` and `@`; spaces, a second colon and empty IDs are invalid. Agent sessions can't be
assignees.

A literal list is checked when you compile. `assignees` can also be an expression, such as
`$inputs.reviewers` or a list with a template. The resolved values are checked before the task is
created, and Effective checks that each assignee exists and is allowed when it creates the task.

To assign the task to the person the run acts as, use a template:

```yaml theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
assignees:
  - 'user:{{ $run.principal.id }}'
```

This works when `$run.principal.type` is `product-user`. A run started by a trigger acts as the
trigger's run-as identity.

A group stays assigned as a group. It isn't expanded to its members when the task is created, so
membership changes still apply. A procedure's task can't be reassigned by hand: its assignees are
the ones the procedure names. Only a task's assignees can answer it.

### Title and instructions

`title` and `instructions` interpolate `{{ $name.path }}` references. Each must be a required
string, number or boolean; an optional, object, array or null value fails before the task is
created. Interpolation doesn't recurse, and the rendered Markdown is sanitized. Prose elsewhere in
the document never becomes part of the instructions. The rendered title must still be one line
and not empty.

### Prefill

`result.prefill` fills the form with values the run already has, such as a recommendation from an
earlier agent step. It is a reference to an object or a built object:

```yaml theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
result:
  schema: payout-decision
  prefill: $recommendation.value.proposed
```

The result schema must be an object. Each key in the prefill must be a property of that schema,
and each value must match that property. The prefill doesn't need to include required properties.
A JSON Schema `default` in the result schema still applies; a prefill value wins for the same
property.

Prefill only fills the form. It doesn't submit anything or count as the person's answer. The run
continues only when someone submits a complete answer that matches the schema.

### Choices from earlier steps

To offer a pick list whose options come from the run, put `x-effective-options` on a string inside
the result schema:

```yaml schema id=product-selection theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
type: object
additionalProperties: false
required: [products, filing_states]
properties:
  products:
    type: array
    uniqueItems: true
    items:
      type: string
      x-effective-options: $analysis.value.proposedProducts
  filing_states:
    type: array
    uniqueItems: true
    items:
      type: string
      x-effective-options: '@effective.us-states-and-dc'
```

The value is either a reference to a list of strings produced before the task, or one of these
quoted built-in lists:

| Reference | Options |
| - | - |
| `@effective.us-states` | The 50 US states |
| `@effective.us-states-and-dc` | The 50 states and the District of Columbia |
| `@effective.us-jurisdictions` | Those 51, plus AS, GU, MP, PR and VI |

Built-in options are two-letter codes, shown with their full names. A string field shows a single
select; an array with `uniqueItems: true` shows a multi-select.

* The options are read just before the task is created and stored with it. Answers are checked
  against that exact list.
* A list must have 1 to 1,000 unique, nonempty strings. Each option is at most 1,024 bytes, and
  all options together at most 65,536 bytes.
* `x-effective-options` works only on a `type: string` schema, and not together with `enum` or
  `const`.
* A schema that uses it may be used only as a task's `result.schema`. Using it anywhere else, such
  as a task input, an agent result, a `value` node or a procedure output, fails with
  `PROC-SCHEMA-004`. Use a plain `enum` there.

## The answer envelope

In `await` mode, the output is an envelope, not the bare answer. Read the answer through
`$output.value`, for example `$payout_approval.value.approved`.

| Member | Type | What it holds |
| - | - | - |
| `value` | per `result.schema` | The answer, checked against `result.schema` |
| `taskId` | string | The task's ID |
| `submissionId` | string | The ID of the submission that answered it |
| `note` | string or null | The note sent with the answer |
| `attachments` | list of strings | IDs the person attached to the answer |
| `outputArtifactId` | string or null | The ID of the primary output file, if the submitter named one |
| `actor.type` | string or null | The kind of actor that answered |
| `actor.id` | string or null | The answerer's ID |
| `actor.label` | string or null | The answerer's display name |
| `actor.sessionId` | string or null | The agent session's ID when an agent step answered; null for a person |
| `submittedAt` | string | When the answer was submitted |

The fence's `attachments` are inputs shown to the assignee. The envelope's `attachments` and
`outputArtifactId` are what the person attached when answering. If later steps must use a file,
ask for its ID in the result schema too.

## Create without waiting

With `mode: create`, the node creates the task and continues as soon as the task exists. Its
output is a task handle, not an answer. The handle's one member for authors is `url`, a link to
the task in the run, which you can post to a chat channel or send by email.

A later `wait` with `task:` takes the handle and waits for the answer. It doesn't create a second
task, and its output is the same answer envelope as `await` mode:

```yaml wait id=wait-for-refund theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
task: $refund_task
output: refund_decision
```

The handle can only be passed to a task wait. You can't build one by hand or read other members
from it. If someone answers before the run reaches the wait, the answer is kept and used when the
wait is reached.

## Example

An underwriter approves cancelling a policy for nonpayment and picks the states that must get a
notice. The note starts with the billing system's note. The group ID and policy values are
illustrative.

````markdown nonpayment-cancellation.procedure.md theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
---
kind: procedure@v0
id: nonpayment-cancellation
inputs:
  policy: { schema: policy-ref }
outputs:
  result: { schema: cancellation-result }
---

# Nonpayment cancellation

Ask an underwriter to approve cancelling a policy for nonpayment.

## Approve the cancellation

The underwriting group sees the policy and the past-due amount, picks the
states the notice must go to, and approves or declines.

```yaml task id=approve-cancellation
assignees:
  - group:underwriting
title: Cancel {{ $inputs.policy.policyNumber }} for nonpayment?
instructions: |
  Policy **{{ $inputs.policy.policyNumber }}** is past due. Check the billing
  history, choose the states that must receive a notice, then approve or
  decline the cancellation.
input:
  schema: policy-ref
  value: $inputs.policy
labels:
  - cancellation
result:
  schema: cancellation-decision
  prefill:
    note: $inputs.policy.billingNote
output: approval
```

## Route the decision

```yaml choice id=route-decision
on: $approval.value.approved
cases:
  'true': '#record-approval'
  'false':
    complete:
      policyNumber: $inputs.policy.policyNumber
      cancelled: false
      noticeStates: []
```

### Record the approval {#record-approval}

```yaml complete id=finish
output: result
value:
  policyNumber: $inputs.policy.policyNumber
  cancelled: true
  noticeStates: $approval.value.notice_states
```

## Resources

```yaml schema id=policy-ref
type: object
additionalProperties: false
required: [policyNumber, pastDue, billingNote]
properties:
  policyNumber: { type: string }
  pastDue: { type: string, format: decimal }
  billingNote: { type: string }
```

```yaml schema id=cancellation-decision
type: object
additionalProperties: false
required: [approved, notice_states]
properties:
  approved:
    type: boolean
    default: false
  notice_states:
    type: array
    uniqueItems: true
    items:
      type: string
      x-effective-options: '@effective.us-states-and-dc'
  note:
    type: string
```

```yaml schema id=cancellation-result
type: object
additionalProperties: false
required: [policyNumber, cancelled, noticeStates]
properties:
  policyNumber: { type: string }
  cancelled: { type: boolean }
  noticeStates:
    type: array
    items: { type: string }
```
````

Start it with this input:

```json theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
{
  "policy": {
    "policyNumber": "NW-100234",
    "pastDue": "412.50",
    "billingNote": "Two missed installments."
  }
}
```

The run opens one task for the underwriting group. An answer of
`{ "approved": true, "notice_states": ["OH", "PA"] }` ends the run with `cancelled: true`. A real cancellation would put the write to your policy system in the approved
branch, after the approval.

### Post the link, then wait

This version assigns the task to the person who started the run, posts the task's link, and only
then waits. `@local.post-to-channel` is an effect declared in the same document; its schemas and
code are left out here.

```yaml task id=approve-refund theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
mode: create
assignees:
  - 'user:{{ $run.principal.id }}'
title: Approve refund of {{ $inputs.refund_amount }} on {{ $inputs.policy_number }}
instructions: Approve or decline the return-premium refund.
result:
  schema: refund-decision
output: refund_task
```

```yaml call id=post-link theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
function: '@local.post-to-channel'
input:
  text: 'Refund approval needed: {{ $refund_task.url }}'
output: posted
```

```yaml wait id=wait-for-refund theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
task: $refund_task
output: refund_decision
```

## On retry and resume

* **One task per step.** Effective creates the task once for the step. A resumed run doesn't
  create it again. Inside a [`repeat`](https://docs.canary.effectiveai.app/procedures/reference/nodes/repeat) or
  [`for-each`](https://docs.canary.effectiveai.app/procedures/reference/nodes/for-each), each round or item gets its own task.
* **Fixed at creation.** The input, attachments, options from `x-effective-options` and the
  prefill are resolved once, just before the task is created, and stored with it. Nothing done
  to the task later recomputes them.
* **The first valid answer wins.** The first answer that matches the schema becomes the step's
  result. A duplicate or late answer can't move the run forward twice. Through the API, an answer
  that doesn't match is refused with `422 invalid_answer`, and a second answer to a finished task
  with `409 task_not_open`. See [Answer a task](https://docs.canary.effectiveai.app/guides/procedures.md#answer-a-task).
* **The record doesn't change.** The task ID, submission ID, answerer and time are recorded and
  never rewritten.
* **Early answers wait.** In `create` mode, an answer that arrives before the run reaches its
  `wait` is kept and used when the wait is reached.

## Common errors

| Code | Cause | Fix |
| - | - | - |
| `PROC-TASK-001` | An unknown or missing field, a bad `mode`, or a bad assignee, such as `agent:reviewer` (`assignees must be a nonempty user/group selector list`) | Use only the fields above, and `user:` or `group:` selectors |
| `PROC-SCHEMA-004` | `result.schema` doesn't name a schema, or a schema with `x-effective-options` is used outside a task result (`x-effective-options is only supported when the schema is used exclusively as an assigned task result`) | Declare the schema, and use a separate `enum` schema for other uses |
| `PROC-SCHEMA-005` | `input.value` or `result.prefill` doesn't match its schema, an options source isn't a list of strings, or a template reference isn't a scalar | Make the value match its schema |
| `PROC-DATA-003` to `PROC-DATA-005` | A reference in `title` or `instructions` is malformed, unknown or optional | Point at a required member of an earlier step's output or an input |
| `PROC-GRAPH-005` | `output` reuses a name that is already declared | Pick a new name |
| `PROC-GRAPH-006` | A referenced value isn't produced before the task on every path | Move the producing step earlier |

Two prefill cases also fail with `PROC-SCHEMA-005`:

* A prefill value for a field with `x-effective-options`, because the options aren't known until
  the task is created. Prefill other fields instead.
* A whole-object prefill, such as `prefill: $recommendation.value.proposed`, whose schema doesn't
  declare one of the result's properties (`source schema does not declare the target property`).
  Declare that property in the source schema, optional is fine, or build the prefill field by
  field.

## See also

* [agent node](https://docs.canary.effectiveai.app/procedures/reference/nodes/agent.md): the same envelope, produced by an agent session
* [wait node](https://docs.canary.effectiveai.app/procedures/reference/nodes/wait.md): wait for a task created with `mode: create`
* [choice node](https://docs.canary.effectiveai.app/procedures/reference/nodes/choice.md): route on the answer
* [People and agents](https://docs.canary.effectiveai.app/procedures/concepts/people-and-agents.md)
* [Answer a task](https://docs.canary.effectiveai.app/guides/procedures.md#answer-a-task) from your own system


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.