TaskContext Class
The single parameter to a resilient task function.
Provides access to the task's identity, typed input, cancellation signals (with cause booleans), and the ability to suspend or exit-for-recovery.
Constructor
TaskContext(*, task_id: str, session_id: str, input: Input, retry_attempt: int = 0, recovery_count: int = 0, cancel: Event | None = None, shutdown: Event | None = None, entry_mode: Literal['fresh', 'resumed', 'recovered'] = 'fresh', is_steered_turn: bool = False, pending_count_provider: Callable[[], int] | None = None, input_id: str | None = None)
Parameters
| Name | Description |
|---|---|
|
task_id
Required
|
Unique task identifier. |
|
input
Required
|
<xref:Input>
Typed, validated input value. |
|
retry_attempt
Required
|
Resilient retry attempt counter. Survives crashes; increments only on failure-retries, never on crash recovery. |
|
recovery_count
Required
|
Crash-recovery counter. Increments each time the framework re-enters this task after a lease loss or stale detection. |
|
cancel
Required
|
Request-level cancellation event. The framework sets
this from multiple causes; observe |
|
shutdown
Required
|
Container-level shutdown event. Precondition for exit_for_recovery. |
Keyword-Only Parameters
| Name | Description |
|---|---|
|
task_id
Required
|
|
|
session_id
Required
|
|
|
input
Required
|
|
|
retry_attempt
|
Default value: 0
|
|
recovery_count
|
Default value: 0
|
|
cancel
|
Default value: None
|
|
shutdown
|
Default value: None
|
|
entry_mode
|
Default value: fresh
|
|
is_steered_turn
|
Default value: False
|
|
pending_count_provider
|
Default value: None
|
|
input_id
|
Default value: None
|
Methods
| exit_for_recovery |
: graceful-shutdown shape. Callable ONLY when When called during shutdown, the framework:
The recovery scan on the next process startup re-enters the
handler with Use as |
exit_for_recovery
: graceful-shutdown shape.
Callable ONLY when ctx.shutdown.is_set() == True. Calling it
outside shutdown raises RuntimeError at the call site
(visible in user-code tracebacks; the task ends in failed).
When called during shutdown, the framework:
Releases the lease on the persisted record.
Leaves the stored
statusasin_progress(NOT transitions tosuspended).Signals in-process awaiters with the standard cooperative- cancel
TaskCancelledresult.Preserves any queued steering inputs in the persisted state.
The recovery scan on the next process startup re-enters the
handler with ctx.entry_mode == "recovered".
Use as return await ctx.exit_for_recovery().
async exit_for_recovery() -> Any
Returns
| Type | Description |
|---|---|
|
The <xref:azure.ai.agentserver.core.tasks._ExitForRecovery> sentinel. |
Exceptions
| Type | Description |
|---|---|
|
If called outside |
Attributes
cancel
cancel_requested
cancel_requested: bool
entry_mode
entry_mode: Literal['fresh', 'resumed', 'recovered']
input
input_id
is_steered_turn
is_steered_turn: bool
pending_input_count
live count of queued steering inputs.
Reflects the current backlog including inputs queued mid-handler.
Reads as 0 for non-steerable tasks (where the provider
returns 0). Replaces the legacy ctx.pending_inputs: Sequence[Any]
snapshot.
Returns
| Type | Description |
|---|---|
|
Number of queued steering inputs. |
recovery_count
retry_attempt
shutdown
task_id
timeout_exceeded
timeout_exceeded: bool