aither-core 0.25.0

Core trait abstractions 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
//! Model-facing ids: the words a model reads and types back to name what it
//! can address, such as a background job, a stored output or a finished
//! subagent.
//!
//! aither mints none of them itself: each is leased from an [`IdAllocator`]
//! the embedder supplies, and the lease lasts as long as what the word names
//! can still be addressed. An embedder that also names its own things for the
//! model leases their words from the same allocator, so no two live ids share
//! a word. [`WordAllocator`] is the allocator used when none is supplied.

use alloc::{
    boxed::Box,
    collections::BTreeMap,
    string::String,
    sync::{Arc, Weak},
};
use core::{
    fmt,
    ops::Deref,
    sync::atomic::{AtomicBool, AtomicUsize, Ordering},
};

/// Hands out the words that name what a model can address.
///
/// [`lease`](Self::lease) hands out a word no live lease holds, and
/// [`release`](Self::release) takes it back once what it names can no longer
/// be addressed. Callers lease through [`AnyIdAllocator::lease`], whose
/// [`IdLease`] releases the word when its last clone drops, so every lease is
/// released exactly once.
///
/// A word is one or more lowercase ASCII letters: it names files, is written
/// into commands and is recognized in text the model wrote.
pub trait IdAllocator: Send + Sync + 'static {
    /// Leases a word that no live lease holds.
    ///
    /// # Errors
    ///
    /// Returns [`IdsExhausted`] when every word the allocator can hand out is
    /// leased.
    fn lease(&self) -> Result<String, IdsExhausted>;

    /// Ends the lease of `word`, which [`lease`](Self::lease) handed out and
    /// which has not been released since.
    fn release(&self, word: &str);
}

/// Every word an [`IdAllocator`] can hand out is leased.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("every model-facing id is leased")]
pub struct IdsExhausted;

/// A shared [`IdAllocator`].
///
/// Clones lease from the same allocator. The default is a [`WordAllocator`]
/// over its built-in word list.
#[derive(Clone)]
pub struct AnyIdAllocator(Arc<dyn IdAllocator>);

impl AnyIdAllocator {
    /// Shares `allocator`.
    pub fn new(allocator: impl IdAllocator) -> Self {
        Self(Arc::new(allocator))
    }

    /// Leases a word that no live lease holds; it is released when the last
    /// clone of the returned lease drops.
    ///
    /// # Errors
    ///
    /// Returns [`IdsExhausted`] when every word is leased.
    ///
    /// # Panics
    ///
    /// Panics when the allocator hands out a word that is not one or more
    /// lowercase ASCII letters.
    pub fn lease(&self) -> Result<IdLease, IdsExhausted> {
        let word = self.0.lease()?;
        assert!(
            is_word(&word),
            "the id allocator handed out '{word}', which is not one or more lowercase ASCII letters"
        );
        Ok(IdLease(Arc::new(Held {
            word,
            allocator: self.clone(),
        })))
    }
}

/// Whether `word` has the shape every model-facing id has.
fn is_word(word: &str) -> bool {
    !word.is_empty() && word.bytes().all(|byte| byte.is_ascii_lowercase())
}

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

impl fmt::Debug for AnyIdAllocator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AnyIdAllocator").finish_non_exhaustive()
    }
}

/// A word leased from an [`AnyIdAllocator`].
///
/// Clones share the lease, which ends when the last of them drops: whatever
/// can still address the named thing holds a clone.
#[derive(Clone)]
pub struct IdLease(Arc<Held>);

/// The word of a lease and the allocator it goes back to.
struct Held {
    word: String,
    allocator: AnyIdAllocator,
}

impl Drop for Held {
    fn drop(&mut self) {
        self.allocator.0.release(&self.word);
    }
}

impl IdLease {
    /// The leased word.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0.word
    }

    /// Whether `other` is a clone of this lease, not merely a lease of the
    /// same word: a word released and leased again names something else.
    #[must_use]
    pub fn same_lease(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }

    /// A reference to this lease that does not keep the word leased.
    #[must_use]
    pub fn downgrade(&self) -> WeakIdLease {
        WeakIdLease(Arc::downgrade(&self.0))
    }
}

/// A reference to an [`IdLease`] that does not keep its word leased, for
/// what must still tell whether the word names the same thing later.
#[derive(Clone)]
pub struct WeakIdLease(Weak<Held>);

impl WeakIdLease {
    /// The lease, while a clone of it still holds the word; `None` once the
    /// word went back to its allocator, after which it may name something
    /// else.
    #[must_use]
    pub fn upgrade(&self) -> Option<IdLease> {
        self.0.upgrade().map(IdLease)
    }

    /// Whether this refers to `lease` itself, not merely to a lease of the
    /// same word.
    #[must_use]
    pub fn refers_to(&self, lease: &IdLease) -> bool {
        // The weak reference keeps the lease's allocation, so no later lease
        // can share its address.
        core::ptr::eq(self.0.as_ptr(), Arc::as_ptr(&lease.0))
    }
}

impl fmt::Debug for WeakIdLease {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("WeakIdLease")
    }
}

impl Deref for IdLease {
    type Target = str;

    fn deref(&self) -> &str {
        self.as_str()
    }
}

impl AsRef<str> for IdLease {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Display for IdLease {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl fmt::Debug for IdLease {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("IdLease").field(&self.as_str()).finish()
    }
}

/// The words of the default [`WordAllocator`]: the BIP-39 English word list
/// (CC0), 2048 common English words, SHA-256
/// `2f5eed53a4727b4bf8880d8f3f199efc90e58503646d9ff8eff3a2ed3b24dbda`.
const ENGLISH_WORDS: &str = include_str!("ids/english.txt");

/// An [`IdAllocator`] over a fixed list of words.
///
/// It hands the words out in list order, moving on past each one it leases,
/// so a word just released is the last to come back. [`Default`] uses a
/// built-in list of 2048 common English words.
pub struct WordAllocator {
    words: Box<[Box<str>]>,
    /// Each word's position in `words`.
    positions: BTreeMap<Box<str>, usize>,
    leased: Box<[AtomicBool]>,
    /// Where the next lease starts looking.
    cursor: AtomicUsize,
}

impl WordAllocator {
    /// An allocator handing out `words`.
    ///
    /// # Panics
    ///
    /// Panics when `words` is empty, holds one word twice, or holds a word
    /// that is not one or more lowercase ASCII letters.
    pub fn new<W: Into<Box<str>>>(words: impl IntoIterator<Item = W>) -> Self {
        let words: Box<[Box<str>]> = words.into_iter().map(Into::into).collect();
        assert!(!words.is_empty(), "a word allocator needs words");
        let mut positions = BTreeMap::new();
        for (position, word) in words.iter().enumerate() {
            assert!(
                is_word(word),
                "'{word}' is not one or more lowercase ASCII letters"
            );
            assert!(
                positions.insert(word.clone(), position).is_none(),
                "the word '{word}' is listed twice"
            );
        }
        let leased = words.iter().map(|_| AtomicBool::new(false)).collect();
        Self {
            words,
            positions,
            leased,
            cursor: AtomicUsize::new(0),
        }
    }
}

impl Default for WordAllocator {
    fn default() -> Self {
        Self::new(ENGLISH_WORDS.lines())
    }
}

impl fmt::Debug for WordAllocator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("WordAllocator")
            .field("words", &self.words.len())
            .finish_non_exhaustive()
    }
}

impl IdAllocator for WordAllocator {
    fn lease(&self) -> Result<String, IdsExhausted> {
        let count = self.words.len();
        for _ in 0..count {
            let position = self.cursor.fetch_add(1, Ordering::Relaxed) % count;
            if !self.leased[position].swap(true, Ordering::AcqRel) {
                return Ok(String::from(&*self.words[position]));
            }
        }
        Err(IdsExhausted)
    }

    fn release(&self, word: &str) {
        let position = *self
            .positions
            .get(word)
            .unwrap_or_else(|| panic!("released '{word}', which this allocator never leased"));
        assert!(
            self.leased[position].swap(false, Ordering::AcqRel),
            "released '{word}', which is not leased"
        );
    }
}

#[cfg(test)]
mod tests {
    use alloc::string::ToString;

    use super::*;

    #[test]
    fn the_default_list_is_single_lowercase_words() {
        assert_eq!(ENGLISH_WORDS.lines().count(), 2048);
        // Building it checks every word's shape and that none is listed twice.
        let _ = WordAllocator::default();
    }

    #[test]
    #[should_panic(expected = "is not one or more lowercase ASCII letters")]
    fn a_word_of_another_shape_is_refused() {
        let _ = WordAllocator::new(["river", "task-1"]);
    }

    /// An allocator that hands out a word of the wrong shape.
    struct Numbered;

    impl IdAllocator for Numbered {
        fn lease(&self) -> Result<String, IdsExhausted> {
            Ok("7".to_string())
        }

        fn release(&self, _word: &str) {}
    }

    #[test]
    #[should_panic(expected = "handed out '7'")]
    fn an_embedder_word_of_another_shape_fails_the_lease() {
        let _ = AnyIdAllocator::new(Numbered).lease();
    }

    #[test]
    fn live_leases_never_share_a_word_and_a_released_word_comes_back() {
        let ids = AnyIdAllocator::new(WordAllocator::new(["river", "stone"]));
        let river = ids.lease().expect("a free word");
        let stone = ids.lease().expect("a free word");
        assert_eq!((river.as_str(), stone.as_str()), ("river", "stone"));
        assert_eq!(ids.lease().unwrap_err(), IdsExhausted);

        drop(river);
        assert_eq!(
            ids.lease().expect("river is free again").to_string(),
            "river"
        );
    }

    #[test]
    fn a_lease_ends_with_its_last_clone() {
        let ids = AnyIdAllocator::new(WordAllocator::new(["river"]));
        let lease = ids.lease().expect("a free word");
        let held = lease.clone();
        drop(lease);
        assert_eq!(
            ids.lease().unwrap_err(),
            IdsExhausted,
            "a clone still holds it"
        );
        drop(held);
        assert_eq!(
            &*ids.lease().expect("released with the last clone"),
            "river"
        );
    }

    #[test]
    fn only_clones_are_the_same_lease() {
        let ids = AnyIdAllocator::new(WordAllocator::new(["river"]));
        let lease = ids.lease().expect("a free word");
        assert!(lease.same_lease(&lease.clone()));
        drop(lease);
        let again = ids.lease().expect("river is free again");
        let other = AnyIdAllocator::new(WordAllocator::new(["river"]))
            .lease()
            .expect("a free word");
        assert_eq!(again.as_str(), other.as_str());
        assert!(!again.same_lease(&other), "a word alone is not the lease");

        let weak = again.downgrade();
        assert!(weak.refers_to(&again));
        assert!(!weak.refers_to(&other));
        drop(again);
        let after = ids.lease().expect("river is free again");
        assert!(
            !weak.refers_to(&after),
            "a released lease is not its successor"
        );
    }

    #[test]
    fn a_weak_lease_upgrades_only_while_the_word_is_held() {
        let ids = AnyIdAllocator::new(WordAllocator::new(["river"]));
        let lease = ids.lease().expect("a free word");
        let weak = lease.downgrade();
        assert_eq!(weak.upgrade().as_deref(), Some("river"));
        drop(lease);
        assert!(weak.upgrade().is_none(), "the word went back");
        let again = ids.lease().expect("river is free again");
        assert!(
            weak.upgrade().is_none(),
            "a word leased again names something else: {again}"
        );
    }

    #[test]
    #[should_panic(expected = "is listed twice")]
    fn a_word_listed_twice_is_refused() {
        let _ = WordAllocator::new(["river", "river"]);
    }

    #[test]
    #[should_panic(expected = "which is not leased")]
    fn releasing_a_free_word_is_a_bug() {
        WordAllocator::new(["river"]).release("river");
    }
}