Approval¶
ApprovalWorkflow¶
ApprovalWorkflow ¶
Manages approval requests lifecycle.
The workflow checks whether an operation requires approval based on configured rules. When approval is required, it creates a request, notifies approvers, and raises ApprovalRequired. The request can later be resolved via the resolve() method.
Example
workflow = ApprovalWorkflow(rules=[ ApprovalRule( name="write_approval", require_for_actions=["write"], approvers=["admin"], ), ])
rule = workflow.check_approval_required( intent_action="write", intent_domain="order", ) if rule is not None: request = await workflow.create_request( rule=rule, trace_id="abc123", intent_raw="Delete all cancelled orders", intent_action="write", intent_domain="order", generated_code="DELETE FROM orders WHERE status='cancelled'", ) # ApprovalRequired is raised by create_request
Source code in src/agenticapi/harness/approval/workflow.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
__init__ ¶
__init__(
*,
rules: list[ApprovalRule] | None = None,
notifier: ApprovalNotifier | None = None,
) -> None
Initialize the approval workflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rules
|
list[ApprovalRule] | None
|
List of approval rules. If None, no approvals are required. |
None
|
notifier
|
ApprovalNotifier | None
|
Notification backend. Defaults to LogNotifier. |
None
|
Source code in src/agenticapi/harness/approval/workflow.py
check_approval_required ¶
Check if any rule requires approval for the given intent.
Returns the first matching rule, or None if no approval is needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intent_action
|
str
|
The classified action type. |
required |
intent_domain
|
str
|
The domain of the request. |
required |
Returns:
| Type | Description |
|---|---|
ApprovalRule | None
|
The first matching ApprovalRule, or None. |
Source code in src/agenticapi/harness/approval/workflow.py
create_request
async
¶
create_request(
*,
rule: ApprovalRule,
trace_id: str,
intent_raw: str,
intent_action: str,
intent_domain: str,
generated_code: str,
) -> NoReturn
Create an approval request and notify approvers.
Stores the request, sends notifications, and raises ApprovalRequired so the caller can return HTTP 202.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule
|
ApprovalRule
|
The rule that triggered the approval. |
required |
trace_id
|
str
|
The execution trace ID. |
required |
intent_raw
|
str
|
The original natural language request. |
required |
intent_action
|
str
|
The classified action type. |
required |
intent_domain
|
str
|
The domain of the request. |
required |
generated_code
|
str
|
The code awaiting approval. |
required |
Returns:
| Type | Description |
|---|---|
NoReturn
|
The created ApprovalRequest. |
Raises:
| Type | Description |
|---|---|
ApprovalRequired
|
Always raised after creating the request. |
Source code in src/agenticapi/harness/approval/workflow.py
resolve
async
¶
Resolve a pending approval request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_id
|
str
|
The request to resolve. |
required |
approved
|
bool
|
Whether the request is approved or rejected. |
required |
approver
|
str
|
The identifier of the approver. |
required |
reason
|
str
|
Optional reason for the decision. |
''
|
Returns:
| Type | Description |
|---|---|
ApprovalRequest
|
The updated ApprovalRequest. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the request is not found or not pending. |
ApprovalDenied
|
If the request is rejected. |
Source code in src/agenticapi/harness/approval/workflow.py
get_request
async
¶
Retrieve an approval request by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_id
|
str
|
The request identifier. |
required |
Returns:
| Type | Description |
|---|---|
ApprovalRequest | None
|
The ApprovalRequest, or None if not found. |
Source code in src/agenticapi/harness/approval/workflow.py
get_pending
async
¶
Retrieve all pending approval requests.
Returns expired requests as expired (updates their state).
Returns:
| Type | Description |
|---|---|
list[ApprovalRequest]
|
List of pending ApprovalRequest objects. |
Source code in src/agenticapi/harness/approval/workflow.py
ApprovalRule¶
ApprovalRule ¶
Bases: BaseModel
Rule that determines if an operation requires human approval.
Each rule specifies which intent actions and domains trigger the approval requirement, who can approve, and the timeout.
Example
rule = ApprovalRule( name="write_approval", require_for_actions=["write", "execute"], approvers=["db_admin"], timeout_seconds=1800, ) if rule.requires_approval(intent_action="write", intent_domain="order"): # Trigger approval workflow ...
Source code in src/agenticapi/harness/approval/rules.py
requires_approval ¶
Check if this rule requires approval for the given intent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intent_action
|
str
|
The classified action type (e.g., "read", "write"). |
required |
intent_domain
|
str
|
The domain of the request (e.g., "order", "product"). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this rule requires approval for the given intent. |
Source code in src/agenticapi/harness/approval/rules.py
ApprovalRequest¶
ApprovalRequest
dataclass
¶
A pending or resolved approval request.
Attributes:
| Name | Type | Description |
|---|---|---|
request_id |
str
|
Unique identifier for this request. |
trace_id |
str
|
Associated execution trace ID. |
intent_raw |
str
|
The original natural language request. |
intent_action |
str
|
The classified action type. |
intent_domain |
str
|
The domain of the request. |
generated_code |
str
|
The code awaiting approval. |
rule_name |
str
|
Name of the rule that triggered approval. |
approvers |
list[str]
|
List of approver identifiers. |
created_at |
datetime
|
When the request was created. |
expires_at |
datetime
|
When the request expires. |
state |
ApprovalState
|
Current state of the request. |
resolved_by |
str | None
|
Who resolved the request (if resolved). |
resolved_at |
datetime | None
|
When the request was resolved (if resolved). |
resolution_reason |
str
|
Reason for the resolution (if any). |
Source code in src/agenticapi/harness/approval/workflow.py
ApprovalState¶
ApprovalState ¶
Bases: StrEnum
State of an approval request.