reef-core 0.2.0

Provider-neutral primitives for building LLM agents: run lifecycle, transcript, tools, hooks
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
#![cfg(test)]

use std::collections::VecDeque;
use std::error::Error;
use std::fmt;
use std::future::{Future, ready};
use std::pin::Pin;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::task::{Context, Poll};
use std::thread;

use crate::cancel::Token;
use futures_core::Stream;

use super::agent::{
    Agent, CloseError, ClosePanickedError, Config, ConfigError, StartError, allocate_revision_id,
};
use super::hook::{self, AgentTeardown, AttributedError, HookError, HookResult};
use super::model::{self, Client, Info, Request, SamplingParams};
use super::session::{Binding, LeaseError};
use super::tool::{self, Gate, GateOperationError, GateVerdict, Requirements};
use crate::opaque::{Envelope, Payload};
use crate::redact_tests::{SECRET, assert_redacted, assert_states};
use crate::transcript::store;
use crate::value::content::Part;
use crate::value::lifecycle::CoordError;
use crate::value::tool_call::ToolCall;

#[derive(Debug)]
pub(crate) struct ModelError;

impl fmt::Display for ModelError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("model error")
    }
}

impl Error for ModelError {}

pub(crate) struct EmptyEvents(VecDeque<Result<model::Event, ModelError>>);

impl Stream for EmptyEvents {
    type Item = Result<model::Event, ModelError>;

    fn poll_next(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        Poll::Ready(self.0.pop_front())
    }
}

pub(crate) struct Model;

impl Client for Model {
    type Error = ModelError;
    type Stream = EmptyEvents;

    fn info(&self) -> Info {
        Info::new("agent-test".to_owned(), 1024).with_tools(true)
    }

    fn generate(
        &self,
        _: Request,
    ) -> impl Future<Output = Result<Self::Stream, Self::Error>> + Send {
        ready(Ok(EmptyEvents(VecDeque::new())))
    }
}

struct AllowGate;

impl Gate for AllowGate {
    fn decide<'a>(
        &'a self,
        _: &'a ToolCall,
        _: &'a Requirements,
        _: Token,
    ) -> Pin<Box<dyn Future<Output = Result<GateVerdict, GateOperationError>> + Send + 'a>> {
        Box::pin(ready(Ok(GateVerdict::Allow)))
    }
}

#[derive(Debug)]
struct TeardownError(&'static str);

impl fmt::Display for TeardownError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.0)
    }
}

impl Error for TeardownError {}

enum TeardownResult {
    Ok,
    Error(&'static str),
    Panic,
}

struct RecordingTeardown {
    name: &'static str,
    result: TeardownResult,
    calls: Arc<AtomicUsize>,
    order: Arc<Mutex<Vec<&'static str>>>,
}

impl AgentTeardown for RecordingTeardown {
    fn close(&self) -> HookResult<()> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        self.order.lock().unwrap().push(self.name);
        match self.result {
            TeardownResult::Ok => Ok(()),
            TeardownResult::Error(message) => Err(HookError::new(TeardownError(message))),
            TeardownResult::Panic => panic!("teardown panic"),
        }
    }
}

#[derive(Default)]
struct BlockState {
    entered: bool,
    released: bool,
}

#[derive(Default)]
struct Blocker {
    state: Mutex<BlockState>,
    changed: Condvar,
}

impl Blocker {
    fn wait_until_entered(&self) {
        let mut state = self.state.lock().unwrap();
        while !state.entered {
            state = self.changed.wait(state).unwrap();
        }
    }

    fn enter_and_wait(&self) {
        let mut state = self.state.lock().unwrap();
        state.entered = true;
        self.changed.notify_all();
        while !state.released {
            state = self.changed.wait(state).unwrap();
        }
    }

    fn release(&self) {
        let mut state = self.state.lock().unwrap();
        state.released = true;
        self.changed.notify_all();
    }
}

struct BlockingTeardown {
    blocker: Arc<Blocker>,
    calls: Arc<AtomicUsize>,
}

impl AgentTeardown for BlockingTeardown {
    fn close(&self) -> HookResult<()> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        self.blocker.enter_and_wait();
        Err(HookError::new(TeardownError("blocked error")))
    }
}

fn empty_tools() -> tool::Registry {
    tool::RegistryBuilder::new().build().unwrap()
}

fn empty_config() -> Config<Model> {
    Config::new(Arc::new(Model), empty_tools(), hook::RegistryBuilder::new())
}

fn registration(
    name: &'static str,
    result: TeardownResult,
    calls: &Arc<AtomicUsize>,
    order: &Arc<Mutex<Vec<&'static str>>>,
) -> hook::Registration {
    hook::Registration::new(name.to_owned()).teardown(Arc::new(RecordingTeardown {
        name,
        result,
        calls: Arc::clone(calls),
        order: Arc::clone(order),
    }))
}

fn agent_with_registrations(registrations: Vec<hook::Registration>) -> Agent<Model> {
    let mut hooks = hook::RegistryBuilder::new();
    for registration in registrations {
        hooks.register(registration);
    }
    Agent::new(Config::new(Arc::new(Model), empty_tools(), hooks)).unwrap()
}

fn close_on_thread(agent: Agent<Model>) -> Result<(), CloseError> {
    tokio::runtime::Builder::new_current_thread()
        .build()
        .unwrap()
        .block_on(agent.close())
}

#[test]
fn config_consumes_builder_validates_defaults_and_freezes_close_free_metadata() {
    let model = Arc::new(Model);
    let binding = Binding::new("memory".to_owned(), "v1".to_owned()).unwrap();
    let mut hooks = hook::RegistryBuilder::new();
    hooks.register(
        hook::Registration::new("metadata".to_owned())
            .binding(binding)
            .requires_session_state(true),
    );
    let mut config = Config::new(Arc::clone(&model), empty_tools(), hooks);
    config
        .set_tool_gate(Some(Arc::new(AllowGate)))
        .push_system_instruction(Part::text("be precise".to_owned()));
    *config.sampling_mut() = SamplingParams {
        temperature: Some(0.25),
        reasoning_effort: Some("high".to_owned()),
        max_output_tokens: Some(99),
        parallel_tool_calls: true,
        output_schema: Some(Payload::copy_from_slice(b"{}")),
        stop_sequences: vec!["stop".to_owned()],
        tool_choice: None,
    };
    config.push_model_extension(
        Envelope::try_new(
            "provider".to_owned(),
            "defaults".to_owned(),
            Payload::copy_from_slice(br#"{"enabled":true}"#),
        )
        .unwrap(),
    );

    let agent = Agent::new(config).unwrap();
    let revision = agent.revision();
    let cloned = revision.clone();
    assert_ne!(revision.id().get(), 0);
    assert_eq!(revision.id(), cloned.id());
    assert!(Arc::ptr_eq(revision.model(), &model));
    let _ = revision.tools();
    assert_eq!(revision.hook_names(), ["metadata"]);
    assert_eq!(revision.hook_bindings()[0].name(), "memory");
    assert!(revision.hooks_require_session_state());
    assert!(revision.tool_gate().is_some());
    assert_eq!(
        revision.system_instruction()[0].fields().text(),
        "be precise"
    );
    assert_eq!(revision.sampling().temperature, Some(0.25));
    assert_eq!(revision.model_extensions()[0].namespace(), "provider");

    let next = Agent::new(empty_config()).unwrap().revision().id();
    assert!(next > revision.id());
}

#[test]
fn config_assembly_reads_and_clears_without_exposing_its_containers() {
    let extension = |kind: &str| {
        Envelope::try_new(
            "provider".to_owned(),
            kind.to_owned(),
            Payload::copy_from_slice(b"{}"),
        )
        .unwrap()
    };

    let mut config = empty_config();
    assert!(config.tool_gate().is_none());
    assert!(config.system_instruction().is_empty());
    assert!(config.model_extensions().is_empty());

    config
        .push_system_instruction(Part::text("first".to_owned()))
        .extend_system_instruction([
            Part::text("second".to_owned()),
            Part::text("third".to_owned()),
        ])
        .push_model_extension(extension("one"))
        .extend_model_extensions([extension("two"), extension("three")])
        .set_tool_gate(Some(Arc::new(AllowGate)));

    assert_eq!(
        config
            .system_instruction()
            .iter()
            .map(|part| part.fields().text())
            .collect::<Vec<_>>(),
        ["first", "second", "third"]
    );
    assert_eq!(
        config
            .model_extensions()
            .iter()
            .map(Envelope::kind)
            .collect::<Vec<_>>(),
        ["one", "two", "three"]
    );
    assert!(config.tool_gate().is_some());

    config
        .clear_system_instruction()
        .clear_model_extensions()
        .set_tool_gate(None);
    assert!(config.system_instruction().is_empty());
    assert!(config.model_extensions().is_empty());
    assert!(config.tool_gate().is_none());

    // A cleared assembly still freezes into a valid revision.
    let revision = Agent::new(config).unwrap().revision().clone();
    assert!(revision.system_instruction().is_empty());
    assert!(revision.model_extensions().is_empty());
    assert!(revision.tool_gate().is_none());
}

#[test]
fn config_reports_hook_error_before_invalid_defaults_and_id_exhaustion_is_explicit() {
    let mut invalid_hooks = hook::RegistryBuilder::new();
    invalid_hooks.register(hook::Registration::new(String::new()));
    let mut config = Config::new(Arc::new(Model), empty_tools(), invalid_hooks);
    config.push_system_instruction(Part::text(String::new()));
    let error = Agent::new(config).err().unwrap();
    assert!(matches!(error, ConfigError::Hooks(_)));
    assert_eq!(error.to_string(), "hook name must be non-empty");
    assert!(error.source().is_some());

    let mut config = empty_config();
    config.push_system_instruction(Part::text(String::new()));
    let error = Agent::new(config).err().unwrap();
    assert!(matches!(
        error,
        ConfigError::InvalidDefaults(super::model::RequestError::EmptySystemInstructionText {
            index: 0
        })
    ));
    assert_eq!(
        error.to_string(),
        "system instruction text at index 0 is empty"
    );
    assert!(error.source().is_some());

    let counter = AtomicU64::new(7);
    assert_eq!(allocate_revision_id(&counter).unwrap().get(), 7);
    let exhausted = AtomicU64::new(u64::MAX);
    let error = Agent::new_with_counter(empty_config(), &exhausted)
        .err()
        .unwrap();
    assert!(matches!(error, ConfigError::RevisionIdExhausted));
    assert_eq!(error.to_string(), "Agent revision identity exhausted");
    assert!(error.source().is_none());
    let zero = AtomicU64::new(0);
    assert!(matches!(
        allocate_revision_id(&zero),
        Err(ConfigError::RevisionIdExhausted)
    ));
}

#[tokio::test]
async fn zero_hooks_and_registration_without_teardown_close_once_and_cache_success() {
    let agent = Agent::new(empty_config()).unwrap();
    assert!(agent.close().await.is_ok());
    assert!(agent.close().await.is_ok());

    let agent = agent_with_registrations(vec![hook::Registration::new("observer-only".to_owned())]);
    assert!(agent.close().await.is_ok());
}

#[tokio::test]
async fn ordered_errors_and_panic_are_aggregated_without_short_circuiting_peers() {
    let calls = Arc::new(AtomicUsize::new(0));
    let order = Arc::new(Mutex::new(Vec::new()));
    let agent = agent_with_registrations(vec![
        registration(
            "first",
            TeardownResult::Error("first error"),
            &calls,
            &order,
        ),
        registration("panic", TeardownResult::Panic, &calls, &order),
        registration("peer", TeardownResult::Ok, &calls, &order),
        registration("last", TeardownResult::Error("last error"), &calls, &order),
    ]);

    let error = agent.close().await.unwrap_err();
    let CloseError::Hooks(errors) = &error else {
        panic!("expected hook failures");
    };
    assert_eq!(*order.lock().unwrap(), ["first", "panic", "peer", "last"]);
    assert_eq!(calls.load(Ordering::SeqCst), 4);
    let entries = errors.entries();
    assert_eq!(
        entries
            .iter()
            .map(|failure| failure.name())
            .collect::<Vec<_>>(),
        ["first", "panic", "last"]
    );
    assert_eq!(entries[0].error().to_string(), "first error");
    // Isolation still reads the panic from the ordered list. A `source()` walk
    // stops at `"first"` and never sees it.
    assert!(
        entries[1]
            .error()
            .source_ref()
            .downcast_ref::<ClosePanickedError>()
            .is_some()
    );
    assert_eq!(entries[2].error().to_string(), "last error");
    assert_eq!(errors.to_string(), "3 Agent OnClose callback(s) failed");
    assert_eq!(
        ClosePanickedError.to_string(),
        "Agent OnClose callback panicked"
    );

    let teardown = error
        .source()
        .expect("CloseError::Hooks sources TeardownError")
        .downcast_ref::<super::agent::TeardownError>()
        .expect("the sourced value is TeardownError");
    assert!(std::ptr::eq(teardown, errors));
    let first = teardown
        .source()
        .expect("TeardownError sources the first AttributedError")
        .downcast_ref::<AttributedError>()
        .expect("the sourced value is AttributedError");
    assert_eq!(first.name(), "first");
    assert_eq!(first.error().to_string(), "first error");
    assert!(std::ptr::eq(first, &entries[0]));
    assert!(
        first
            .source()
            .and_then(Error::source)
            .and_then(|error| error.downcast_ref::<ClosePanickedError>())
            .is_none()
    );
}

#[tokio::test]
async fn busy_reports_exact_count_runs_no_hook_and_remains_retryable() {
    let calls = Arc::new(AtomicUsize::new(0));
    let order = Arc::new(Mutex::new(Vec::new()));
    let agent = agent_with_registrations(vec![registration(
        "close",
        TeardownResult::Ok,
        &calls,
        &order,
    )]);
    let first = agent.reserve_attachment().unwrap();
    let second = agent.reserve_attachment().unwrap();

    assert!(matches!(
        agent.close().await,
        Err(CloseError::Busy { active_runs: 2 })
    ));
    assert_eq!(calls.load(Ordering::SeqCst), 0);
    drop(first);
    let error = agent.close().await.unwrap_err();
    assert!(matches!(error, CloseError::Busy { active_runs: 1 }));
    assert_eq!(error.to_string(), "Agent has 1 active Run(s)");
    assert!(error.source().is_none());
    drop(second);

    assert!(agent.close().await.is_ok());
    assert!(agent.close().await.is_ok());
    assert_eq!(calls.load(Ordering::SeqCst), 1);
}

#[tokio::test]
async fn close_first_never_touches_session_and_reservation_first_forces_busy() {
    let blocker = Arc::new(Blocker::default());
    let calls = Arc::new(AtomicUsize::new(0));
    let mut hooks = hook::RegistryBuilder::new();
    hooks.register(
        hook::Registration::new("closing".to_owned()).teardown(Arc::new(BlockingTeardown {
            blocker: Arc::clone(&blocker),
            calls,
        })),
    );
    let closing = Agent::new(Config::new(Arc::new(Model), empty_tools(), hooks)).unwrap();
    let close_handle = {
        let closing = closing.clone();
        thread::spawn(move || close_on_thread(closing))
    };
    blocker.wait_until_entered();

    let session_touches = AtomicUsize::new(0);
    let reservation = closing.reserve_attachment();
    if reservation.is_ok() {
        session_touches.fetch_add(1, Ordering::SeqCst);
    }
    assert!(matches!(reservation, Err(StartError::AgentClosed)));
    assert_eq!(session_touches.load(Ordering::SeqCst), 0);
    blocker.release();
    assert!(close_handle.join().unwrap().is_err());

    let open = Agent::new(empty_config()).unwrap();
    let reservation = open.reserve_attachment().unwrap();
    session_touches.fetch_add(1, Ordering::SeqCst);
    assert!(matches!(
        open.close().await,
        Err(CloseError::Busy { active_runs: 1 })
    ));
    drop(reservation);
    open.close().await.unwrap();
    assert_eq!(session_touches.load(Ordering::SeqCst), 1);
    assert!(matches!(
        open.reserve_attachment(),
        Err(StartError::AgentClosed)
    ));
}

#[tokio::test]
async fn concurrent_and_repeated_close_wait_for_and_share_one_cached_result() {
    let blocker = Arc::new(Blocker::default());
    let calls = Arc::new(AtomicUsize::new(0));
    let mut hooks = hook::RegistryBuilder::new();
    hooks.register(
        hook::Registration::new("blocking".to_owned()).teardown(Arc::new(BlockingTeardown {
            blocker: Arc::clone(&blocker),
            calls: Arc::clone(&calls),
        })),
    );
    let agent = Agent::new(Config::new(Arc::new(Model), empty_tools(), hooks)).unwrap();
    let first_agent = agent.clone();
    let first = thread::spawn(move || close_on_thread(first_agent));
    blocker.wait_until_entered();

    const WAITERS: usize = 2;
    let probe = agent.install_close_wait_probe(WAITERS);
    let waiters = (0..WAITERS)
        .map(|_| {
            let waiter = agent.clone();
            thread::spawn(move || close_on_thread(waiter))
        })
        .collect::<Vec<_>>();

    // close() must enable each Notified before entering close_decision(). The
    // helper observes Closing under the state lock, releases that lock, then
    // pauses here before returning Wait. Two notify_one calls wake both enabled
    // waiters independently; without enable, or with enable after the helper,
    // they coalesce into one permit and only one waiter can recheck Closing.
    probe.wait_until_closing_observed();
    probe.notify_waiters_one_by_one(&agent);
    probe.resume_waiters();
    probe.wait_until_closing_rechecked();

    blocker.release();
    let first = first.join().unwrap().unwrap_err();
    let mut waiter_results = waiters
        .into_iter()
        .map(|waiter| waiter.join().unwrap().unwrap_err());
    let second = waiter_results.next().unwrap();
    let third = waiter_results.next().unwrap();
    assert!(waiter_results.next().is_none());
    let fourth = agent.close().await.unwrap_err();

    let CloseError::Hooks(first_errors) = &first else {
        panic!("close unexpectedly busy");
    };
    let CloseError::Hooks(second_errors) = &second else {
        panic!("close unexpectedly busy");
    };
    let CloseError::Hooks(third_errors) = &third else {
        panic!("close unexpectedly busy");
    };
    let CloseError::Hooks(fourth_errors) = &fourth else {
        panic!("close unexpectedly busy");
    };
    assert!(std::ptr::eq(
        first_errors.entries().as_ptr(),
        second_errors.entries().as_ptr()
    ));
    assert!(std::ptr::eq(
        second_errors.entries().as_ptr(),
        third_errors.entries().as_ptr()
    ));
    assert!(std::ptr::eq(
        third_errors.entries().as_ptr(),
        fourth_errors.entries().as_ptr()
    ));
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    assert_eq!(second.to_string(), "1 Agent OnClose callback(s) failed");
    let teardown = second
        .source()
        .expect("CloseError::Hooks sources TeardownError")
        .downcast_ref::<super::agent::TeardownError>()
        .expect("the sourced value is TeardownError");
    assert!(std::ptr::eq(teardown, second_errors));
    let first = teardown
        .source()
        .expect("TeardownError sources the first AttributedError")
        .downcast_ref::<AttributedError>()
        .expect("the sourced value is AttributedError");
    assert_eq!(first.name(), "blocking");
    assert_eq!(first.error().to_string(), "blocked error");
}

#[tokio::test]
async fn unrelated_public_registry_does_not_share_private_agent_authority() {
    let private_calls = Arc::new(AtomicUsize::new(0));
    let external_calls = Arc::new(AtomicUsize::new(0));
    let private_order = Arc::new(Mutex::new(Vec::new()));
    let external_order = Arc::new(Mutex::new(Vec::new()));
    let agent = agent_with_registrations(vec![registration(
        "private",
        TeardownResult::Ok,
        &private_calls,
        &private_order,
    )]);

    let mut external = hook::RegistryBuilder::new();
    external.register(registration(
        "external",
        TeardownResult::Ok,
        &external_calls,
        &external_order,
    ));
    let external = external.build().unwrap();
    assert!(external.close().is_empty());
    assert_eq!(external_calls.load(Ordering::SeqCst), 1);
    assert_eq!(private_calls.load(Ordering::SeqCst), 0);

    let attachment = agent.reserve_attachment().unwrap();
    assert!(matches!(
        agent.close().await,
        Err(CloseError::Busy { active_runs: 1 })
    ));
    assert_eq!(private_calls.load(Ordering::SeqCst), 0);
    drop(attachment);
    agent.close().await.unwrap();
    assert_eq!(private_calls.load(Ordering::SeqCst), 1);
    assert_eq!(external_calls.load(Ordering::SeqCst), 1);
}

#[test]
fn frozen_error_shapes_display_sources_and_clone_without_flattening() {
    let simple = [
        StartError::AgentClosed,
        StartError::SessionBusy,
        StartError::Cancelled,
        StartError::RunIdExhausted,
    ];
    assert_eq!(
        simple.iter().map(ToString::to_string).collect::<Vec<_>>(),
        [
            "Agent is closed",
            "Session already has an active Run",
            "Run admission canceled",
            "Run identity exhausted"
        ]
    );
    assert!(simple.iter().all(|error| error.source().is_none()));
    for error in &simple {
        let cloned = error.clone();
        assert_eq!(cloned.to_string(), error.to_string());
    }

    let identity = StartError::InvalidIdentity(CoordError::EmptySessionId);
    assert_eq!(identity.to_string(), "session ID must not be empty");
    assert!(identity.source().is_some());
    assert_eq!(identity.clone().to_string(), identity.to_string());

    let admission = StartError::Admission(LeaseError::BindingMismatch);
    assert_eq!(admission.to_string(), "session binding mismatch");
    assert!(admission.source().is_some());
    assert_eq!(admission.clone().to_string(), admission.to_string());
    let transcript = StartError::Transcript(store::Error::Poisoned);
    assert_eq!(transcript.to_string(), "transcript store lock poisoned");
    assert!(transcript.source().is_some());
    assert_eq!(transcript.clone().to_string(), transcript.to_string());

    let calls = Arc::new(AtomicUsize::new(0));
    let order = Arc::new(Mutex::new(Vec::new()));
    let mut builder = hook::RegistryBuilder::new();
    builder.register(registration(
        "prepare-source",
        TeardownResult::Error("prepare error"),
        &calls,
        &order,
    ));
    let failure = builder.build().unwrap().close().pop().unwrap();
    let prepare = StartError::Prepare(failure);
    assert_eq!(prepare.to_string(), "hook prepare-source prepare failed");
    assert!(prepare.source().is_some());
    assert_eq!(prepare.clone().to_string(), prepare.to_string());

    let config = [
        ConfigError::Hooks(hook::RegistryError::EmptyName),
        ConfigError::InvalidDefaults(model::RequestError::ToolExchangeNotClosed),
        ConfigError::RevisionIdExhausted,
    ];
    for error in &config {
        let cloned = error.clone();
        assert_eq!(cloned.to_string(), error.to_string());
        assert_eq!(cloned.source().is_some(), error.source().is_some());
    }
}

#[test]
fn config_and_revision_debug_show_everything_but_the_model() {
    // Neither type carries an `M: Debug` bound. The trailing `..` is what says
    // a field was left out, rather than quietly implying none was.
    let mut hooks = hook::RegistryBuilder::new();
    hooks.register(hook::Registration::new("recorder".to_owned()));
    let mut config = Config::new(Arc::new(Model), empty_tools(), hooks);
    config.push_system_instruction(Part::text(SECRET.into()));
    config.set_tool_gate(Some(Arc::new(AllowGate)));
    config.push_model_extension(
        Envelope::try_new(
            "example".to_owned(),
            "compat".to_owned(),
            Payload::copy_from_slice(format!(r#"{{"t":"{SECRET}"}}"#).as_bytes()),
        )
        .expect("fixture is valid JSON"),
    );
    config.sampling_mut().stop_sequences = vec![SECRET.into()];

    assert_redacted(&config);
    assert_states(
        &config,
        &[
            "Config",
            r#"hooks: RegistryBuilder { hooks: ["recorder"] }"#,
            "tool_gate: <present>",
            "text: <21 bytes>",
            "stop_sequences: <1 items>",
            "..",
        ],
    );

    let agent = Agent::new(config).expect("a valid configuration builds");
    let revision = agent.revision();
    assert_redacted(&revision);
    assert_states(
        &revision,
        &[
            "Revision",
            r#"hook_names: ["recorder"]"#,
            "tool_gate: <present>",
            "text: <21 bytes>",
            "hooks_require_session_state: false",
            "..",
        ],
    );

    // An agent is identified by the revision every Run of it will freeze.
    assert_eq!(
        format!("{agent:?}"),
        format!("Agent {{ revision: {} }}", revision.id())
    );
}