aither-agent 0.26.0

AI agent framework with planning, memory, and tool execution for aither
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Keeping secrets out of model requests.
//!
//! A secret that enters a model request has left the machine. Tool output
//! carries them routinely — a `.env` read, a printed config, a token a
//! failing command echoed — and the next request would forward it.
//! [`SecretGuard`] stands between every request and the provider: wrap the
//! embedder's model once with [`SecretGuard::guard`], and every agent,
//! subagent, router, compaction and side request sent on it passes the guard.
//!
//! The guard knows two kinds of secret: values the embedder registers
//! ([`SecretGuard::register`]), replaced with `<secret: NAME>`, and strings
//! in the shape of a key or token ([`SecretShape`]), replaced with `<secret>`.
//! Each request's replacements are logged through `tracing`, by name and
//! shape, never by value.
//!
//! Text the embedder is about to publish is stricter: [`SecretGuard::check`]
//! refuses it instead of replacing what it finds.
//!
//! # What is left as it is
//!
//! Content whose integrity the provider verifies passes unchanged:
//! [`ReasoningState`](aither_core::llm::ReasoningState) payloads (signed or
//! encrypted thinking), and the arguments of a tool call bound to a thought
//! signature. The provider produced that content itself, so it carries
//! nothing the provider has not already seen, and rewriting it would get the
//! turn rejected. Attachments pass unchanged too: their URLs address data,
//! and rewriting one breaks the reference.

mod shape;
#[cfg(test)]
mod tests;

use std::{borrow::Cow, ops::Range, sync::Arc};

use aho_corasick::{AhoCorasick, MatchKind};
use aither_core::{
    LanguageModel,
    llm::{
        Event, GenerateError, LLMRequest, LLMRequestWithTools, Message, RetryPolicy, model::Profile,
    },
};
use arc_swap::ArcSwap;
use askama::Template;
use futures_core::Stream;
use futures_lite::StreamExt;
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde_json::Value;

pub use shape::SecretShape;
use shape::SecretShapes;

/// The registry of secrets and the check every model request passes.
///
/// A cheap handle: clones share one registry, so a secret registered
/// through any clone is guarded from the next request on, by every model
/// the guard wraps.
#[derive(Clone)]
pub struct SecretGuard {
    shared: Arc<Shared>,
}

struct Shared {
    shapes: SecretShapes,
    registry: ArcSwap<Registry>,
}

/// The registered secrets, swapped whole on each change.
#[derive(Default)]
struct Registry {
    names: Vec<Arc<str>>,
    values: Vec<Arc<str>>,
    /// Finds every value at once; `None` while nothing is registered.
    matcher: Option<AhoCorasick>,
}

impl Registry {
    fn with(&self, name: &str, value: &str) -> Self {
        let mut names = self.names.clone();
        let mut values = self.values.clone();
        if let Some(index) = names.iter().position(|known| &**known == name) {
            values[index] = value.into();
        } else {
            names.push(name.into());
            values.push(value.into());
        }
        // Leftmost-longest: where one value contains another, the longer
        // one is replaced whole.
        let matcher = AhoCorasick::builder()
            .match_kind(MatchKind::LeftmostLongest)
            .build(values.iter().map(|value| value.as_bytes()))
            .expect("a handful of registered secrets fits the automaton");
        Self {
            names,
            values,
            matcher: Some(matcher),
        }
    }
}

impl std::fmt::Debug for SecretGuard {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // The names only: a value never reaches a log.
        f.debug_struct("SecretGuard")
            .field("registered", &self.shared.registry.load().names)
            .finish_non_exhaustive()
    }
}

impl Default for SecretGuard {
    fn default() -> Self {
        Self::new()
    }
}

/// What a secret found in text is.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SecretKind {
    /// A value registered under `name`.
    Registered {
        /// The name it was registered under.
        name: Arc<str>,
    },
    /// A string in the shape of a key or token.
    Shaped(SecretShape),
}

impl std::fmt::Display for SecretKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Registered { name } => write!(f, "secret {name}"),
            Self::Shaped(shape) => shape.fmt(f),
        }
    }
}

/// A secret found in text: what it is and where, never its value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SecretFinding {
    /// What the secret is.
    pub kind: SecretKind,
    /// Its byte range in the text checked.
    pub range: Range<usize>,
}

/// Why [`SecretGuard::check`] refused a text; the caller renders what the
/// model reads with [`Notices::secret_refusal`](crate::Notices::secret_refusal).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SecretRefusal {
    /// Every secret found, in text order.
    pub findings: Vec<SecretFinding>,
}

#[derive(Template)]
#[template(path = "secret_placeholder.txt", escape = "none")]
struct PlaceholderTemplate<'a> {
    name: &'a str,
}

/// What stands in for a secret found by its shape.
const SHAPE_PLACEHOLDER: &str = include_str!("texts/secret_shape_placeholder.txt");

/// The replacements made in one request or text, for its log line.
#[derive(Default)]
struct Tally {
    registered: Vec<Arc<str>>,
    shaped: Vec<SecretShape>,
}

impl Tally {
    fn record(&mut self, kind: &SecretKind) {
        match kind {
            SecretKind::Registered { name } => self.registered.push(Arc::clone(name)),
            SecretKind::Shaped(shape) => self.shaped.push(*shape),
        }
    }

    fn log(self, subject: &str) {
        if self.registered.is_empty() && self.shaped.is_empty() {
            return;
        }
        let registered: Vec<&str> = self.registered.iter().map(|name| &**name).collect();
        tracing::info!(
            registered = ?registered,
            shaped = ?self.shaped,
            "replaced secrets in {subject}",
        );
    }
}

impl SecretGuard {
    /// Creates a guard with no registered secrets; it already replaces
    /// strings in the shape of a key or token.
    #[must_use]
    pub fn new() -> Self {
        Self {
            shared: Arc::new(Shared {
                shapes: SecretShapes::new(),
                registry: ArcSwap::from_pointee(Registry::default()),
            }),
        }
    }

    /// Registers `value` as the secret `name`, replacing the value `name`
    /// had. From the next request on, every occurrence of it is replaced
    /// with `<secret: NAME>`.
    ///
    /// # Panics
    ///
    /// Panics when `value` is empty: an empty secret occurs everywhere, so
    /// registering one is a mistake in the calling program.
    pub fn register(&self, name: impl AsRef<str>, value: impl AsRef<str>) {
        let (name, value) = (name.as_ref(), value.as_ref());
        assert!(!value.is_empty(), "secret {name} is registered empty");
        self.shared
            .registry
            .rcu(|registry| Arc::new(registry.with(name, value)));
    }

    /// Wraps `model` so every request sent on it passes this guard.
    pub fn guard<M>(&self, model: M) -> Guarded<M> {
        Guarded {
            model,
            guard: self.clone(),
        }
    }

    /// Every secret in `text`, in text order, registered values ahead of
    /// shapes where they overlap.
    #[must_use]
    pub fn find(&self, text: &str) -> Vec<SecretFinding> {
        let registry = self.shared.registry.load();
        let mut findings: Vec<SecretFinding> = registry
            .matcher
            .as_ref()
            .map(|matcher| {
                matcher
                    .find_iter(text)
                    .map(|hit| SecretFinding {
                        kind: SecretKind::Registered {
                            name: Arc::clone(&registry.names[hit.pattern().as_usize()]),
                        },
                        range: hit.range(),
                    })
                    .collect()
            })
            .unwrap_or_default();
        // Registered findings never overlap one another; a shape is kept
        // only where nothing found before it lies.
        let mut taken: Vec<Range<usize>> = findings
            .iter()
            .map(|finding| finding.range.clone())
            .collect();
        let mut shaped = Vec::new();
        self.shared.shapes.find(text, |shape, range| {
            if !taken.iter().any(|kept| overlaps(kept, &range)) {
                taken.push(range.clone());
                shaped.push(SecretFinding {
                    kind: SecretKind::Shaped(shape),
                    range,
                });
            }
        });
        findings.extend(shaped);
        findings.sort_by_key(|finding| finding.range.start);
        findings
    }

    /// Refuses `text` when it carries a secret, for text about to be
    /// published.
    ///
    /// # Errors
    ///
    /// Returns every secret found when there is one.
    pub fn check(&self, text: &str) -> Result<(), SecretRefusal> {
        let findings = self.find(text);
        if findings.is_empty() {
            Ok(())
        } else {
            Err(SecretRefusal { findings })
        }
    }

    /// `text` with each secret replaced by its placeholder, logging the
    /// replacements.
    #[must_use]
    pub fn redact<'a>(&self, text: &'a str) -> Cow<'a, str> {
        let mut tally = Tally::default();
        let redacted = self.redact_text(text, &mut tally);
        tally.log("a text");
        redacted
    }

    /// Replaces each secret in `request` in place, logging the replacements.
    fn redact_request(&self, request: &mut LLMRequest) {
        let mut tally = Tally::default();
        for message in request.messages_mut() {
            match message {
                Message::User { content, .. }
                | Message::System { content }
                | Message::Tool { content, .. } => self.redact_in_place(content, &mut tally),
                // Reasoning state is the provider's own, signed or
                // encrypted, and passes as it is; so do the arguments of a
                // call bound to a thought signature.
                Message::Assistant {
                    content,
                    tool_calls,
                    reasoning: _,
                } => {
                    self.redact_in_place(content, &mut tally);
                    for call in tool_calls
                        .iter_mut()
                        .filter(|call| call.reasoning_state.is_none())
                    {
                        self.redact_value(&mut call.arguments, &mut tally);
                    }
                }
                // An effort level carries no text.
                Message::Configuration { .. } => {}
            }
        }
        tally.log("a model request");
    }

    fn redact_value(&self, value: &mut Value, tally: &mut Tally) {
        match value {
            Value::String(text) => self.redact_in_place(text, tally),
            Value::Array(items) => {
                for item in items {
                    self.redact_value(item, tally);
                }
            }
            Value::Object(fields) => {
                for field in fields.values_mut() {
                    self.redact_value(field, tally);
                }
            }
            Value::Null | Value::Bool(_) | Value::Number(_) => {}
        }
    }

    fn redact_in_place(&self, text: &mut String, tally: &mut Tally) {
        if let Cow::Owned(redacted) = self.redact_text(text, tally) {
            *text = redacted;
        }
    }

    fn redact_text<'a>(&self, text: &'a str, tally: &mut Tally) -> Cow<'a, str> {
        let findings = self.find(text);
        if findings.is_empty() {
            return Cow::Borrowed(text);
        }
        let mut redacted = String::with_capacity(text.len());
        let mut copied = 0;
        for finding in findings {
            redacted.push_str(&text[copied..finding.range.start]);
            match &finding.kind {
                SecretKind::Registered { name } => PlaceholderTemplate { name }
                    .render_into(&mut redacted)
                    .expect("the secret placeholder renders"),
                SecretKind::Shaped(_) => redacted.push_str(SHAPE_PLACEHOLDER),
            }
            copied = finding.range.end;
            tally.record(&finding.kind);
        }
        redacted.push_str(&text[copied..]);
        Cow::Owned(redacted)
    }
}

const fn overlaps(a: &Range<usize>, b: &Range<usize>) -> bool {
    a.start < b.end && b.start < a.end
}

/// A model whose every request passes a [`SecretGuard`] before it reaches
/// the provider.
///
/// Made with [`SecretGuard::guard`].
#[derive(Debug, Clone)]
pub struct Guarded<M> {
    model: M,
    guard: SecretGuard,
}

impl<M> Guarded<M> {
    /// The wrapped model.
    pub const fn inner(&self) -> &M {
        &self.model
    }

    /// The guard every request passes.
    pub const fn guard(&self) -> &SecretGuard {
        &self.guard
    }
}

impl<M: LanguageModel> LanguageModel for Guarded<M> {
    type Error = M::Error;

    fn respond(
        &self,
        mut request: LLMRequest,
    ) -> impl Stream<Item = Result<Event, Self::Error>> + Send {
        self.guard.redact_request(&mut request);
        self.model.respond(request)
    }

    fn respond_with_tools(
        &self,
        mut request: LLMRequestWithTools<'_>,
    ) -> impl Stream<Item = Result<Event, Self::Error>> + Send {
        self.guard.redact_request(request.request_mut());
        self.model.respond_with_tools(request)
    }

    fn generate<T: JsonSchema + DeserializeOwned + 'static>(
        &self,
        mut request: LLMRequest,
    ) -> impl Future<Output = Result<T, GenerateError<Self::Error>>> + Send {
        self.guard.redact_request(&mut request);
        self.model.generate(request)
    }

    fn complete(&self, prefix: &str) -> impl Stream<Item = Result<Event, Self::Error>> + Send {
        let prefix = self.guard.redact(prefix);
        async_stream::stream! {
            let mut events = std::pin::pin!(self.model.complete(&prefix));
            while let Some(event) = events.next().await {
                yield event;
            }
        }
    }

    fn summarize(&self, text: &str) -> impl Stream<Item = Result<Event, Self::Error>> + Send {
        let text = self.guard.redact(text);
        async_stream::stream! {
            let mut events = std::pin::pin!(self.model.summarize(&text));
            while let Some(event) = events.next().await {
                yield event;
            }
        }
    }

    fn categorize<T: JsonSchema + DeserializeOwned + 'static>(
        &self,
        text: &str,
    ) -> impl Future<Output = Result<T, GenerateError<Self::Error>>> + Send {
        let text = self.guard.redact(text);
        async move { self.model.categorize(&text).await }
    }

    fn profile(&self) -> impl Future<Output = Result<Profile, Self::Error>> + Send {
        self.model.profile()
    }

    fn retry_policy(&self) -> RetryPolicy {
        self.model.retry_policy()
    }
}