Import
from vega_capability_sdk import (CancellationReason,CancellationToken,CapabilityExecutionContext,CapabilityExecutionRequest,CapabilityExecutionResult,PhysicalTerminationStatus,ProgressReporter,)
Also exported from vega_capability_sdk.execution and vega_capability_sdk.contracts.
CapabilityExecutionRequest
@dataclass(frozen=True, slots=True)class CapabilityExecutionRequest:execution_id: strcapability_id: strprovider_id: straction_id: straction_version: strinputs: Mapping[str, Any]deadline: datetime | None = Noneidempotency_key: str | None = Nonemission_id: str | None = Nonetask_graph_id: str | None = Nonenode_id: str | None = Nonetrace_id: str | None = Nonemetadata: Mapping[str, Any] = {}
Immutable request for one Action invocation. The five identity fields must be non-blank.inputs and metadata are deeply frozen JSON-compatible mappings. If supplied, deadline must be timezone-aware and is normalized to UTC. Construction raises ValueError for blank identities or a naive deadline, and TypeErrorfor non-JSON values or non-string mapping keys.
| Term | Meaning |
|---|---|
| execution_id / capability_id / provider_id / action_id / action_version | Required identity fields; must not be blank. |
| inputs | Required JSON-compatible input mapping; recursively frozen. |
| deadline | Optional UTC-aware absolute deadline; not enforced by the request object. |
| idempotency_key | Correlation value. The SDK does not deduplicate requests. |
| mission_id / task_graph_id / node_id / trace_id | Correlation-only fields; no Task or Session objects are exposed. |
| metadata | Optional JSON-compatible correlation mapping; recursively frozen. |
CapabilityExecutionContext
@dataclass(frozen=True, slots=True)class CapabilityExecutionContext:execution_id: strcancellation: CancellationTokenprogress: ProgressReporterdeadline: datetime | None = Nonelogger_name: str = "vega.capability"artifacts: ArtifactService | None = Nonelogger: StructuredLogger = StructuredLogger()clock: Clock = SystemClock()trace: TraceContext = TraceContext()resource_leases: tuple[ResourceLeaseView, ...] = ()
Restricted services and facts for a single execution. Any deadline must be UTC-aware.validate_request(request) raises ValueError unlessrequest.execution_id equals context.execution_id;BaseCapabilityProvider.execute performs this check before callingon_execute.
CapabilityExecutionResult
@dataclass(frozen=True, slots=True)class CapabilityExecutionResult:output: Mapping[str, Any]artifact_references: tuple[str | ArtifactReference, ...] = ()diagnostics: Mapping[str, Any] = {}physical_termination: PhysicalTerminationStatus | None = Nonedef to_dict(self) -> dict[str, Any]: ...
Successful Action result. output and diagnostics are deeply frozen JSON-compatible mappings. to_dict() thaws mappings and serializes eachArtifactReference; string artifact references are retained.
Cancellation
CancellationReason is a StrEnum: user_request, mission_cancelled,timeout, task_timeout, runtime_shutdown, safety_stop,operator_request, parent_cancelled.
class CancellationToken:@propertydef is_cancel_requested(self) -> bool: ...@propertydef cancellation_reason(self) -> CancellationReason | None: ...@propertydef secondary_reasons(self) -> tuple[CancellationReason, ...]: ...def request_cancel(self, reason: CancellationReason) -> bool: ...async def wait_for_cancel(self) -> CancellationReason: ...def raise_if_cancelled(self) -> None: ...
The first cancellation reason becomes the primary reason; later distinct reasons are retained as secondary. request_cancel returns True only when it establishes the primary reason. raise_if_cancelled() raises CancellationCapabilityError. The token does not cancel an asyncio task, stop hardware, or invokeCancellableProvider.cancel.
ProgressReporter
class ProgressReporter:def __init__(self,*,execution_id: str,on_progress: Callable[[dict[str, Any]], Awaitable[None]] | None = None,clock: Clock | None = None,trace: TraceContext | None = None,validate_payload: Callable[[dict[str, Any]], None] | None = None,max_payload_bytes: int = 64 * 1024,) -> None: ...async def report(self, payload: Mapping[str, Any]) -> None: ...
report freezes and validates the payload, applies the byte limit to its JSON representation, and creates an event with execution_id, monotonically increasingsequence, ISO timestamp, trace_id, and payload. RaisesTypeError for a non-JSON payload and ValueError when the encoded payload exceeds max_payload_bytes.
PhysicalTerminationStatus
StrEnum with not_applicable, unknown, confirmed, andfailed_to_confirm. Use it only for factual knowledge about the underlying physical action; Python coroutine completion is not proof of a physical stop.
Example
request = CapabilityExecutionRequest(execution_id="exec-001",capability_id="example.echo",provider_id="python",action_id="example.echo.run",action_version="1.0.0",inputs={"text": "hello"},)# Inside on_execute:context.validate_request(request)context.cancellation.raise_if_cancelled()await context.progress.report({"phase": "complete"})return CapabilityExecutionResult(output={"text": request.inputs["text"]},physical_termination=PhysicalTerminationStatus.NOT_APPLICABLE,)
