-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathhelpers.rs
More file actions
424 lines (395 loc) · 15.6 KB
/
Copy pathhelpers.rs
File metadata and controls
424 lines (395 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use ironclaw_authorization::{
CapabilityLease, CapabilityLeaseError, CapabilityLeaseStatus, CapabilityLeaseStore,
};
use ironclaw_host_api::{
Action, ApprovalRequest, CapabilityId, ExecutionContext, InvocationFingerprint, InvocationId,
Principal, ResourceEstimate, ResourceScope,
};
use ironclaw_run_state::{ApprovalStatus, RunStateError, RunStateStore};
use tracing::warn;
use crate::{CapabilityInvocationError, ResumeContextMismatchKind};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CapabilityActionKind {
Dispatch,
Spawn,
}
pub(crate) fn invocation_fingerprint_for_kind(
kind: CapabilityActionKind,
scope: &ResourceScope,
capability_id: &CapabilityId,
estimate: &ResourceEstimate,
input: &serde_json::Value,
) -> Result<InvocationFingerprint, ironclaw_host_api::HostApiError> {
match kind {
CapabilityActionKind::Dispatch => {
InvocationFingerprint::for_dispatch(scope, capability_id, estimate, input)
}
CapabilityActionKind::Spawn => {
InvocationFingerprint::for_spawn(scope, capability_id, estimate, input)
}
}
}
pub(crate) fn validate_approval_request_matches_invocation(
approval: &ApprovalRequest,
context: &ExecutionContext,
capability_id: &CapabilityId,
estimate: &ResourceEstimate,
expected_action: CapabilityActionKind,
) -> Result<(), CapabilityInvocationError> {
let action_matches = match (expected_action, approval.action.as_ref()) {
(
CapabilityActionKind::Dispatch,
Action::Dispatch {
capability,
estimated_resources,
},
)
| (
CapabilityActionKind::Spawn,
Action::SpawnCapability {
capability,
estimated_resources,
},
) => capability == capability_id && estimated_resources == estimate,
_ => false,
};
if !action_matches {
return Err(CapabilityInvocationError::ApprovalRequestMismatch {
capability: capability_id.clone(),
field: "action",
});
}
if approval.correlation_id != context.correlation_id {
return Err(CapabilityInvocationError::ApprovalRequestMismatch {
capability: capability_id.clone(),
field: "correlation_id",
});
}
let expected_requester = Principal::Extension(context.extension_id.clone());
if approval.requested_by != expected_requester {
return Err(CapabilityInvocationError::ApprovalRequestMismatch {
capability: capability_id.clone(),
field: "requested_by",
});
}
Ok(())
}
pub(crate) async fn matching_approval_lease(
capability_leases: &dyn CapabilityLeaseStore,
context: &ExecutionContext,
capability_id: &CapabilityId,
invocation_fingerprint: &InvocationFingerprint,
) -> Option<CapabilityLease> {
capability_leases
.active_leases_for_context(context)
.await
.into_iter()
.find(|lease| {
lease.scope == context.resource_scope
&& lease.grant.capability == *capability_id
&& lease.invocation_fingerprint.as_ref() == Some(invocation_fingerprint)
})
}
/// Finds a Claimed lease that was left in-flight by a prior approval-resume
/// auth bounce.
///
/// Called from `auth_resume_json` when `matching_approval_lease` (Active-only)
/// returns `None` after a `resume_json` → `AuthorizationRequiresAuth` bounce.
/// That bounce claims the lease but skips the revoke (Part A of the fix), so
/// the lease is Claimed rather than Active. This helper locates it so the
/// same invocation can continue without a second approval prompt.
pub(crate) async fn matching_claimed_approval_lease_for_auth_resume(
capability_leases: &dyn CapabilityLeaseStore,
scope: &ResourceScope,
capability_id: &CapabilityId,
invocation_fingerprint: &InvocationFingerprint,
) -> Option<CapabilityLease> {
capability_leases
.leases_for_scope(scope)
.await
.into_iter()
.find(|lease| {
lease.scope == *scope
&& lease.grant.capability == *capability_id
&& lease.invocation_fingerprint.as_ref() == Some(invocation_fingerprint)
&& lease.status == CapabilityLeaseStatus::Claimed
})
}
pub(crate) async fn fail_run_if_configured(
run_state: Option<&dyn RunStateStore>,
scope: &ResourceScope,
invocation_id: InvocationId,
error_kind: &'static str,
) {
if let Some(run_state) = run_state
&& let Err(error) = fail_run(run_state, scope, invocation_id, error_kind).await
{
warn!(
invocation_id = %invocation_id,
error_kind,
transition_error_kind = run_state_error_kind(&error),
"run-state fail transition failed; original business error is being returned to caller",
);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CapabilityRunStateTransition {
Fail { error_kind: &'static str },
BlockAuth { error_kind: &'static str },
}
impl CapabilityRunStateTransition {
pub(crate) fn error_kind(self) -> &'static str {
match self {
Self::Fail { error_kind } | Self::BlockAuth { error_kind } => error_kind,
}
}
}
impl CapabilityInvocationError {
/// Returns the run-state transition to apply for this error, or `None`
/// when no transition is appropriate at the capability-host layer.
///
/// Dispatch failures intentionally return `None`: per the
/// `capability_failure_disposition` policy introduced in PR #4236, every
/// `DispatchFailureKind` is either a `ModelVisibleToolError` (the model
/// observes the failure as an ordinary tool error and can retry with
/// corrected input) or a `RetrySameCall` (the caller retries
/// transparently). Neither path wants the run marked failed at this
/// layer; doing so would short-circuit the disposition policy and turn
/// recoverable failures (notably `InputEncode`) into terminal run
/// failures.
pub(crate) fn run_state_transition(&self) -> Option<CapabilityRunStateTransition> {
match self {
Self::UnsupportedObligations { .. } => Some(CapabilityRunStateTransition::Fail {
error_kind: "UnsupportedObligations",
}),
Self::ObligationFailed { .. } => Some(CapabilityRunStateTransition::Fail {
error_kind: "ObligationFailed",
}),
Self::AuthorizationRequiresAuth { .. } => {
Some(CapabilityRunStateTransition::BlockAuth {
error_kind: "AuthRequired",
})
}
Self::Dispatch { .. } => None,
Self::UnknownCapability { .. }
| Self::AuthorizationDenied { .. }
| Self::AuthorizationRequiresApproval { .. }
| Self::InvocationFingerprint { .. }
| Self::ApprovalRequestMismatch { .. }
| Self::ApprovalFingerprintMismatch { .. }
| Self::ApprovalNotApproved { .. }
| Self::ApprovalStoreMissing { .. }
| Self::ApprovalLeaseMissing { .. }
| Self::ResumeStoreMissing { .. }
| Self::ProcessManagerMissing { .. }
| Self::ResumeNotBlocked { .. }
| Self::ResumeContextMismatch { .. }
| Self::Lease(_)
| Self::RunState(_)
| Self::Process(_) => Some(CapabilityRunStateTransition::Fail {
error_kind: "Obligation",
}),
}
}
}
pub(crate) async fn apply_run_state_transition_if_configured(
run_state: Option<&dyn RunStateStore>,
scope: &ResourceScope,
invocation_id: InvocationId,
error: &CapabilityInvocationError,
) {
let Some(run_state) = run_state else {
return;
};
let Some(transition) = error.run_state_transition() else {
// No run-state transition at this layer; PR #4236 disposition policy
// handles the failure on the outcome path.
return;
};
match transition {
CapabilityRunStateTransition::Fail { error_kind } => {
fail_run_if_configured(Some(run_state), scope, invocation_id, error_kind).await;
}
CapabilityRunStateTransition::BlockAuth { error_kind } => {
if let Err(error) = run_state
.block_auth(scope, invocation_id, error_kind.to_string())
.await
{
warn!(
invocation_id = %invocation_id,
error_kind,
transition_error_kind = run_state_error_kind(&error),
"run-state auth block transition failed; original business error is being returned to caller",
);
}
}
}
}
pub(crate) async fn fail_run(
run_state: &dyn RunStateStore,
scope: &ResourceScope,
invocation_id: InvocationId,
error_kind: &'static str,
) -> Result<(), RunStateError> {
run_state
.fail(scope, invocation_id, error_kind.to_string())
.await?;
Ok(())
}
pub(crate) async fn complete_run_after_side_effect(
run_state: &dyn RunStateStore,
scope: &ResourceScope,
invocation_id: InvocationId,
capability_id: &CapabilityId,
side_effect: &'static str,
) {
if let Err(error) = run_state.complete(scope, invocation_id).await {
warn!(
invocation_id = %invocation_id,
capability_id = %capability_id,
side_effect,
transition_error_kind = run_state_error_kind(&error),
"run-state completion failed after successful side effect; returning successful capability result",
);
}
}
pub(crate) fn approval_not_approved_error_kind(status: ApprovalStatus) -> &'static str {
match status {
ApprovalStatus::Pending => "ApprovalPending",
ApprovalStatus::Approved => "ApprovalApproved",
ApprovalStatus::Denied => "ApprovalDenied",
ApprovalStatus::Expired => "ApprovalExpired",
ApprovalStatus::Discarded => "ApprovalDiscarded",
}
}
pub(crate) fn resume_context_mismatch_kind(
capability_mismatch: bool,
approval_request_mismatch: bool,
) -> ResumeContextMismatchKind {
debug_assert!(capability_mismatch || approval_request_mismatch);
match (capability_mismatch, approval_request_mismatch) {
(true, true) => ResumeContextMismatchKind::CapabilityAndApprovalRequestId,
(true, false) => ResumeContextMismatchKind::CapabilityId,
(false, true) => ResumeContextMismatchKind::ApprovalRequestId,
(false, false) => unreachable!("resume context mismatch kind called without mismatch"),
}
}
pub(crate) fn capability_lease_error_kind(error: &CapabilityLeaseError) -> &'static str {
match error {
CapabilityLeaseError::UnknownLease { .. } => "UnknownLease",
CapabilityLeaseError::ExpiredLease { .. } => "ExpiredLease",
CapabilityLeaseError::ExhaustedLease { .. } => "ExhaustedLease",
CapabilityLeaseError::UnclaimedFingerprintLease { .. } => "UnclaimedFingerprintLease",
CapabilityLeaseError::FingerprintMismatch { .. } => "FingerprintMismatch",
CapabilityLeaseError::InactiveLease { .. } => "InactiveLease",
CapabilityLeaseError::Persistence { .. } => "Persistence",
CapabilityLeaseError::VersionMismatch => "VersionMismatch",
CapabilityLeaseError::CasExhausted => "CasExhausted",
}
}
pub(crate) fn claim_error_may_be_concurrent_resume(error: &CapabilityLeaseError) -> bool {
matches!(
error,
CapabilityLeaseError::InactiveLease {
status: CapabilityLeaseStatus::Claimed
| CapabilityLeaseStatus::Dispatching
| CapabilityLeaseStatus::Consumed,
..
}
)
}
pub(crate) fn run_state_error_kind(error: &RunStateError) -> &'static str {
match error {
RunStateError::UnknownInvocation { .. } => "UnknownInvocation",
RunStateError::InvocationAlreadyExists { .. } => "InvocationAlreadyExists",
RunStateError::UnknownApprovalRequest { .. } => "UnknownApprovalRequest",
RunStateError::ApprovalRequestAlreadyExists { .. } => "ApprovalRequestAlreadyExists",
RunStateError::ApprovalNotPending { .. } => "ApprovalNotPending",
RunStateError::InvalidPath(_) => "InvalidPath",
RunStateError::Filesystem(_) => "Filesystem",
RunStateError::Serialization(_) => "Serialization",
RunStateError::Deserialization(_) => "Deserialization",
RunStateError::Backend(_) => "Backend",
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::CapabilityInvocationError;
use ironclaw_host_api::{CapabilityId, DispatchFailureKind, RuntimeDispatchErrorKind};
fn capability() -> CapabilityId {
CapabilityId::new("test.capability").expect("capability id")
}
/// Regression for PR #4236: dispatch failures must not transition the run
/// state at this layer. The `capability_failure_disposition` policy in
/// host_runtime maps every `DispatchFailureKind` to either
/// `ModelVisibleToolError` or `RetrySameCall`; both want the run to keep
/// going. Calling `run_state.fail()` here would short-circuit that policy
/// and turn recoverable input errors (notably `InputEncode`) into
/// terminal run failures invisible to the model.
#[test]
fn dispatch_input_encode_returns_no_run_state_transition() {
let error = CapabilityInvocationError::Dispatch {
kind: DispatchFailureKind::Runtime(RuntimeDispatchErrorKind::InputEncode),
safe_summary: None,
detail: None,
};
assert!(error.run_state_transition().is_none());
}
#[test]
fn dispatch_backend_returns_no_run_state_transition() {
let error = CapabilityInvocationError::Dispatch {
kind: DispatchFailureKind::Runtime(RuntimeDispatchErrorKind::Backend),
safe_summary: None,
detail: None,
};
assert!(error.run_state_transition().is_none());
}
#[test]
fn dispatch_unknown_capability_returns_no_run_state_transition() {
let error = CapabilityInvocationError::Dispatch {
kind: DispatchFailureKind::UnknownCapability,
safe_summary: None,
detail: None,
};
assert!(error.run_state_transition().is_none());
}
#[test]
fn unknown_capability_still_fails_run_state() {
let error = CapabilityInvocationError::UnknownCapability {
capability: capability(),
};
let transition = error
.run_state_transition()
.expect("non-dispatch errors keep their fail transition");
assert!(matches!(
transition,
CapabilityRunStateTransition::Fail { .. }
));
}
#[test]
fn authorization_requires_auth_still_blocks_auth() {
let error = CapabilityInvocationError::AuthorizationRequiresAuth {
capability: capability(),
required_secrets: Vec::new(),
credential_requirements: Vec::new(),
};
let transition = error
.run_state_transition()
.expect("auth-required errors keep their block-auth transition");
assert!(matches!(
transition,
CapabilityRunStateTransition::BlockAuth { .. }
));
}
/// Regression for the `ApprovalStatus::Discarded` arm added to
/// `approval_not_approved_error_kind`: ensures the arm maps to the
/// correct string constant and does not silently drift to another value.
#[test]
fn approval_not_approved_error_kind_maps_discarded_status() {
assert_eq!(
approval_not_approved_error_kind(ApprovalStatus::Discarded),
"ApprovalDiscarded",
);
}
}