aft-base 0.67.1

Shared foundation for Agent Foundational Toolkit
Documentation
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
425
426
427
428
//! Request-addressed vault use and shared, cancellable approval cards.

use super::{
    ApiKeyTarget, ApprovalRequest, ConsentOutcome, SecretConsentRequest, SecretScope, SecretVault,
    UseAnswer,
};
use std::collections::BTreeMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

test_support_visibility! {
/// The phone's approval window is independent of upstream network deadlines.
pub(crate) trait ApprovalClock: Send + Sync {
    fn deadline(&self) -> Pin<Box<dyn Future<Output = ()> + Send>>;
}
}
struct WallClock;
impl ApprovalClock for WallClock {
    fn deadline(&self) -> Pin<Box<dyn Future<Output = ()> + Send>> {
        Box::pin(tokio::time::sleep(Duration::from_secs(600)))
    }
}

/// One absolute person-wait limit, shared by every label in an HTTP request.
pub(crate) struct RequestBudget {
    deadline: Pin<Box<dyn Future<Output = ()> + Send>>,
}
impl RequestBudget {
    pub(crate) async fn expired(&mut self) {
        self.deadline.as_mut().await;
    }
}
#[rustfmt::skip]
test_support_visibility! {
#[derive(Clone, Copy, Debug)]
pub(crate) enum UseError {
    Declined,
    Expired,
    Cancelled,
    Unavailable,
    Indeterminate,
    InvalidPlan(&'static str),
}
}

#[derive(Clone, Debug)]
pub(crate) struct ApprovalMetadata {
    pub(crate) request_id: String,
    pub(crate) outcome_code: Option<&'static str>,
    pub(crate) granted: bool,
}

/// The approved local refusal text, shared by fetches and proxy requests.
pub(crate) fn consent_failure_message(label: &str, host: &str, reason: UseError) -> String {
    match reason {
        UseError::Declined => format!("secret {label}: approval was declined for {host}; the request was not sent"),
        UseError::Expired => format!("secret {label}: approval expired for {host}; the request was not sent"),
        UseError::Cancelled => format!("secret {label}: the request for {host} was cancelled; the request was not sent"),
        _ => format!("secret {label}: the approval request for {host} could not be filed; the request was not sent"),
    }
}

#[rustfmt::skip]
test_support_visibility! {
/// Shared progress for sockets whose idle budget excludes approval waits.
#[derive(Default)]
pub(crate) struct RequestActivity {
    waiting: AtomicUsize,
    last_activity: Mutex<Option<tokio::time::Instant>>,
}
}
impl RequestActivity {
    test_support_visibility! {
    pub(crate) fn begin_wait(&self) {
        self.waiting.fetch_add(1, Ordering::AcqRel);
    }
    }

    test_support_visibility! {
    pub(crate) fn finish_wait(&self) {
        // Publish progress before the count can reach zero, so a socket never
        // resumes against the deadline from before the approval completed.
        self.record_activity();
        self.waiting.fetch_sub(1, Ordering::AcqRel);
    }
    }

    test_support_visibility! {
    pub(crate) fn record_activity(&self) {
        let mut last = self
            .last_activity
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        *last = Some(tokio::time::Instant::now());
    }
    }

    test_support_visibility! {
    pub(crate) fn snapshot(&self) -> (bool, Option<tokio::time::Instant>) {
        // Read the count first: observing the last waiter leave also observes
        // its previously published completion timestamp.
        let waiting = self.waiting.load(Ordering::Acquire) != 0;
        let last = *self
            .last_activity
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        (waiting, last)
    }
    }
}

test_support_visibility! {
pub(crate) struct RequestSecrets {
    vault: Arc<dyn SecretVault>,
    scope: SecretScope,
    context: Option<(std::path::PathBuf, String)>,
    status: Mutex<BTreeMap<u64, Vec<String>>>,
    sequence: AtomicU64,
    clock: Arc<dyn ApprovalClock>,
    activity: Arc<RequestActivity>,
    once: Mutex<BTreeMap<String, bool>>,
    request_cancelled: Option<Arc<dyn Fn() -> bool + Send + Sync>>,
    local_approval_cancel: Option<super::consent::Cancellation>,
}
}
struct Waiting<'a> {
    owner: &'a RequestSecrets,
    sequence: u64,
}
impl Drop for Waiting<'_> {
    fn drop(&mut self) {
        self.owner
            .status
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .remove(&self.sequence);
        self.owner.activity.finish_wait();
    }
}
impl RequestSecrets {
    #[rustfmt::skip]
    test_support_visibility! {
    pub(crate) fn new(
        vault: Arc<dyn SecretVault>,
        scope: SecretScope,
        context: Option<(std::path::PathBuf, String)>,
    ) -> Self {
        Self {
            vault,
            scope,
            context,
            status: Mutex::default(),
            sequence: AtomicU64::new(1),
            clock: Arc::new(WallClock),
            activity: Arc::default(),
            once: Mutex::default(),
            request_cancelled: None,
            local_approval_cancel: None,
        }
    }
    }

    pub(crate) fn with_local_approval_cancel(
        mut self,
        cancel: super::consent::Cancellation,
    ) -> Self {
        self.local_approval_cancel = Some(cancel);
        self
    }
    pub(crate) fn with_request_cancellation(
        mut self,
        cancelled: impl Fn() -> bool + Send + Sync + 'static,
    ) -> Self {
        self.request_cancelled = Some(Arc::new(cancelled));
        self
    }

    fn cancelled(&self) -> bool {
        self.request_cancelled.as_ref().is_some_and(|check| check())
    }
    pub(crate) fn budget(&self) -> RequestBudget {
        RequestBudget {
            deadline: self.clock.deadline(),
        }
    }
    #[rustfmt::skip]
    test_support_visibility! {
    #[cfg(unix)]
    pub(crate) fn status(&self) -> Vec<String> {
        self.status
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .values()
            .flatten()
            .cloned()
            .collect()
    }
    }
    #[rustfmt::skip]
    test_support_visibility! {
    pub(crate) fn activity(&self) -> Arc<RequestActivity> {
        self.activity.clone()
    }
    }
    #[cfg(any(test, feature = "test-support"))]
    pub(crate) fn with_clock(mut self, clock: Option<Arc<dyn ApprovalClock>>) -> Self {
        if let Some(clock) = clock {
            self.clock = clock;
        }
        self
    }

    pub(crate) async fn ask(&self, label: &str, target: &ApiKeyTarget) -> UseAnswer {
        let (vault, scope, label, target) = (
            self.vault.clone(),
            self.scope.clone(),
            label.to_owned(),
            target.clone(),
        );
        let cancelled = self.request_cancelled.clone();
        tokio::task::spawn_blocking(move || {
            if cancelled.as_ref().is_some_and(|check| check()) {
                UseAnswer::Unavailable
            } else {
                vault.use_secret(&scope, &label, &target)
            }
        })
        .await
        .unwrap_or(UseAnswer::Unavailable)
    }

    #[rustfmt::skip]
    test_support_visibility! {
    #[cfg(any(test, feature = "test-support"))]
    pub(crate) async fn approve(
        &self,
        label: &str,
        target: &ApiKeyTarget,
        pending: ApprovalRequest,
    ) -> Result<ConsentOutcome, UseError> {
        self.approve_with_metadata(label, target, pending, &|_| {})
            .await
    }
}

    async fn approve_with_metadata(
        &self,
        label: &str,
        target: &ApiKeyTarget,
        pending: ApprovalRequest,
        observe: &(impl Fn(ApprovalMetadata) + Sync),
    ) -> Result<ConsentOutcome, UseError> {
        let request_id = pending.request_id.clone();
        observe(ApprovalMetadata {
            request_id: request_id.clone(),
            outcome_code: None,
            granted: false,
        });
        let Some((project_root, session)) = &self.context else {
            return Err(UseError::Unavailable);
        };
        let sequence = self.sequence.fetch_add(1, Ordering::Relaxed);
        self.activity.begin_wait();
        // Drop also runs when a client cancels, the command stops, or another
        // label refuses; none of those paths may leave a waiting status behind.
        let _waiting = Waiting {
            owner: self,
            sequence,
        };
        let mut lines = vec![format!(
            "secret {label}: waiting for your approval for {} (requests wait up to 10 minutes)",
            target.host
        )];
        if self
            .once
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .get(label)
            == Some(&true)
        {
            lines.push(format!(
                "secret {label}: the approval covered one request; another card was sent"
            ));
        }
        self.status
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .insert(sequence, lines);
        let (answer, outcome_code) = super::consent::wait_consent_with_code(
            SecretConsentRequest {
                scope: self.scope.clone(),
                project_root: project_root.clone(),
                session: session.clone(),
                label: label.into(),
                request_id: pending.request_id,
                statement_b64url: pending.statement_b64url,
                expires_at_ms: Some(pending.expires_at_ms),
            },
            self.local_approval_cancel.as_ref(),
        )
        .await;
        observe(ApprovalMetadata {
            request_id,
            outcome_code,
            granted: matches!(
                answer,
                ConsentOutcome::Answered | ConsentOutcome::AnsweredOnce
            ),
        });
        match answer {
            ConsentOutcome::Answered | ConsentOutcome::AnsweredOnce => Ok(answer),
            ConsentOutcome::Declined => Err(UseError::Declined),
            ConsentOutcome::Expired => Err(UseError::Expired),
            ConsentOutcome::Cancelled => Err(UseError::Cancelled),
            ConsentOutcome::Indeterminate => Err(UseError::Indeterminate),
            _ => Err(UseError::Unavailable),
        }
    }

    /// The caller supplies a fresh verified connection before a releasing retry.
    /// No mutex is held while asking the vault or waiting for a person's answer.
    #[cfg(all(any(test, feature = "test-support"), unix))]
    #[cfg_attr(not(test), allow(dead_code))]
    pub(crate) async fn use_in_request<F, Fut>(
        &self,
        label: &str,
        target: &ApiKeyTarget,
        before_retry: F,
    ) -> Result<UseAnswer, UseError>
    where
        F: FnMut() -> Fut,
        Fut: Future<Output = Result<(), UseError>>,
    {
        self.use_planned_in_request(label, target, |_| Ok(()), |_| Ok(()), before_retry)
            .await
    }

    pub(crate) async fn use_planned_in_request<F, Fut, P, O>(
        &self,
        label: &str,
        target: &ApiKeyTarget,
        plan: P,
        observe: O,
        before_retry: F,
    ) -> Result<UseAnswer, UseError>
    where
        F: FnMut() -> Fut,
        Fut: Future<Output = Result<(), UseError>>,
        P: Fn(&str) -> Result<(), UseError>,
        O: Fn(&[u8]) -> Result<(), UseError>,
    {
        self.use_planned_with_metadata(label, target, plan, observe, before_retry, |_| {})
            .await
    }

    pub(crate) async fn use_planned_with_metadata<F, Fut, P, O, M>(
        &self,
        label: &str,
        target: &ApiKeyTarget,
        plan: P,
        observe: O,
        mut before_retry: F,
        metadata: M,
    ) -> Result<UseAnswer, UseError>
    where
        F: FnMut() -> Fut,
        Fut: Future<Output = Result<(), UseError>>,
        P: Fn(&str) -> Result<(), UseError>,
        O: Fn(&[u8]) -> Result<(), UseError>,
        M: Fn(ApprovalMetadata) + Sync,
    {
        let mut answered = std::collections::HashSet::new();
        let mut once = false;
        loop {
            if self.cancelled() {
                return Err(UseError::Unavailable);
            }
            let answer = self.ask(label, target).await;
            if self.cancelled() {
                return Err(UseError::Unavailable);
            }
            let UseAnswer::ApprovalRequired(pending) = answer else {
                if let UseAnswer::Released(released) = &answer {
                    observe(released.material())?;
                    plan(&released.location)?;
                }
                if matches!(answer, UseAnswer::Released(_)) {
                    self.once
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .insert(label.into(), once);
                }
                return Ok(answer);
            };
            // An allowed card that still cannot release fails closed, but only
            // recorded decline/expiry (or the request deadline) means denial.
            if !answered.insert(pending.request_id.clone()) {
                return Err(UseError::Unavailable);
            }
            // A pending statement already names the location. Check its plan
            // before asking the person or consuming a subsequent release.
            use base64::Engine;
            if let Ok(bytes) =
                base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(&pending.statement_b64url)
            {
                if let Ok(statement) = serde_json::from_slice::<serde_json::Value>(&bytes) {
                    if let Some(location) = statement["use"]["location"].as_str() {
                        plan(location)?;
                    }
                }
            }
            if once {
                self.once
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner)
                    .insert(label.into(), true);
            }
            once = self
                .approve_with_metadata(label, target, pending, &metadata)
                .await?
                == ConsentOutcome::AnsweredOnce;
            before_retry().await?;
        }
    }
}