brokk-mj-controller 2.29.0

Daemon-side controller, session manager, and web server for Mjolnir
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
//! One daemon-owned cache, warmed independently of settings and tool calls.
//! Entries belong to configured profile identities, never a shared generation.
//! Every reader joins the owner's attempt; only its supervisor retries failures.

use std::collections::BTreeMap;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::Duration;

use anyhow::{Context, Result, bail};
use futures::future::{FutureExt, Shared, TryFutureExt, join_all};
use mj_client::session::BoxFuture;
use mj_core::config::{Config, HarnessKind};
#[cfg(test)]
use mj_core::config::{HarnessProfile, SubagentConfig};
use mj_core::profile_capabilities::{
    CapabilityState, ProfileCapabilities, ProfileCapabilitiesSnapshot,
};
use mj_core::worker_launch::ProfileConfig;
use tokio_util::sync::CancellationToken;

#[cfg(test)]
pub(crate) type Probe = dyn Fn(String) -> BoxFuture<'static, Result<ProfileConfig>> + Send + Sync;
#[cfg(test)]
type ModelProbe = dyn Fn(String, String) -> BoxFuture<'static, Result<ProfileConfig>> + Send + Sync;
type Attempt = Shared<BoxFuture<'static, Result<ProfileConfig, Arc<str>>>>;
type CacheKey = (String, Option<String>);

#[derive(Clone)]
enum Entry {
    Pending(Attempt),
    Ready(ProfileConfig),
    Failed { attempt: Attempt, error: Arc<str> },
}
impl Entry {
    fn state(&self) -> CapabilityState<ProfileConfig> {
        match self {
            Self::Pending(_) => CapabilityState::Pending,
            Self::Ready(choices) => CapabilityState::Ready(choices.clone()),
            Self::Failed { error, .. } => CapabilityState::Failed(error.to_string()),
        }
    }
    fn owns(&self, attempt: &Attempt) -> bool {
        match self {
            Self::Pending(current)
            | Self::Failed {
                attempt: current, ..
            } => current.ptr_eq(attempt),
            Self::Ready(_) => false,
        }
    }
}
#[derive(Clone)]
struct Definition {
    id: String,
    config: Arc<Config>,
    cancellation: CancellationToken,
}
#[derive(Default)]
struct Inner {
    live: Option<Config>,
    definitions: BTreeMap<String, Definition>,
    entries: BTreeMap<CacheKey, Entry>,
}

pub(crate) struct ProfileCatalog {
    cancellation: CancellationToken,
    inner: Mutex<Inner>,
    publisher: Mutex<Option<Arc<dyn Fn() + Send + Sync>>>,
    revisions: tokio::sync::watch::Sender<u64>,
    #[cfg(test)]
    probe: Option<(Arc<Probe>, Arc<ModelProbe>)>,
}
impl ProfileCatalog {
    pub(crate) fn new(cancellation: CancellationToken) -> Arc<Self> {
        Arc::new(Self {
            cancellation,
            inner: Mutex::new(Inner::default()),
            publisher: Mutex::new(None),
            revisions: tokio::sync::watch::channel(0).0,
            #[cfg(test)]
            probe: None,
        })
    }

    pub(crate) fn set_publisher(&self, publisher: Arc<dyn Fn() + Send + Sync>) {
        *self
            .publisher
            .lock()
            .unwrap_or_else(PoisonError::into_inner) = Some(publisher);
    }

    fn publish(&self) {
        self.revisions
            .send_modify(|revision| *revision = revision.wrapping_add(1));
        let publisher = self
            .publisher
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .clone();
        if let Some(publisher) = publisher {
            publisher();
        }
    }

    pub(crate) fn snapshot(&self) -> ProfileCapabilitiesSnapshot {
        let inner = self.lock();
        ProfileCapabilitiesSnapshot {
            profiles: inner
                .definitions
                .keys()
                .map(|identity| {
                    let choices = inner
                        .entries
                        .get(&(identity.clone(), None))
                        .map_or(CapabilityState::Pending, Entry::state);
                    let efforts = match &choices {
                        CapabilityState::Ready(choices) => choices
                            .models
                            .iter()
                            .map(|model| {
                                let state = match inner
                                    .entries
                                    .get(&(identity.clone(), Some(model.value.clone())))
                                {
                                    Some(Entry::Ready(choices)) => {
                                        CapabilityState::Ready(choices.efforts.clone())
                                    }
                                    Some(Entry::Failed { error, .. }) => {
                                        CapabilityState::Failed(error.to_string())
                                    }
                                    _ => CapabilityState::Pending,
                                };
                                (model.value.clone(), state)
                            })
                            .collect(),
                        _ => BTreeMap::new(),
                    };
                    (identity.clone(), ProfileCapabilities { choices, efforts })
                })
                .collect(),
        }
    }

    /// Adoption updates eligibility without invalidating capability entries.
    pub(crate) fn sync(self: &Arc<Self>, config: &Config) {
        let changed = {
            let mut inner = self.lock();
            let changed = inner.live.as_ref() != Some(config);
            if let Some(old) = &inner.live {
                for (id, profile) in old.enabled_profiles() {
                    let identity = profile.capabilities_key(id);
                    if config
                        .enabled_profile(id)
                        .is_none_or(|new| new.capabilities_key(id) != identity)
                        && let Some(definition) = inner.definitions.get(&identity)
                    {
                        definition.cancellation.cancel();
                    }
                }
            }
            inner.live = Some(config.clone());
            changed
        };
        self.ensure(config);
        if changed {
            self.publish();
        }
    }

    /// Claim new definitions and default probes under one lock. A Settings
    /// draft uses this same path without becoming the live configuration.
    pub(crate) fn ensure(self: &Arc<Self>, config: &Config) {
        let config = Arc::new(config.clone());
        let mut started = Vec::new();
        {
            let mut inner = self.lock();
            for (id, profile) in config.enabled_profiles() {
                let identity = profile.capabilities_key(id);
                if let Some(definition) = inner.definitions.get_mut(&identity) {
                    if !definition.cancellation.is_cancelled() {
                        // Keep successful entries and the in-flight attempt.
                        // A failed owner's next retry uses refreshed credentials.
                        definition.config = config.clone();
                        continue;
                    }
                    inner.entries.retain(|(entry_identity, _), entry| {
                        entry_identity != &identity || matches!(entry, Entry::Ready(_))
                    });
                }
                let definition = Definition {
                    id: id.into(),
                    config: config.clone(),
                    cancellation: self.cancellation.child_token(),
                };
                let key = (identity.clone(), None);
                let attempt = if inner.entries.contains_key(&key) {
                    None
                } else {
                    Some(self.attempt(&definition, None))
                };
                inner
                    .definitions
                    .insert(identity.clone(), definition.clone());
                if let Some(Entry::Ready(choices)) = inner.entries.get(&key).cloned() {
                    for model in &choices.models {
                        let model_key = (identity.clone(), Some(model.value.clone()));
                        if !inner.entries.contains_key(&model_key) {
                            if choices.model.as_ref() == Some(&model.value) {
                                inner
                                    .entries
                                    .insert(model_key, Entry::Ready(choices.clone()));
                            } else {
                                let pending = self.attempt(&definition, Some(model.value.clone()));
                                inner
                                    .entries
                                    .insert(model_key.clone(), Entry::Pending(pending.clone()));
                                started.push((model_key, pending));
                            }
                        }
                    }
                }
                if let Some(attempt) = attempt {
                    inner
                        .entries
                        .insert(key.clone(), Entry::Pending(attempt.clone()));
                    started.push((key, attempt));
                }
            }
        }
        if !started.is_empty() {
            self.publish();
        }
        for (key, attempt) in started {
            self.spawn(key, attempt);
        }
    }

    fn attempt(&self, definition: &Definition, model: Option<String>) -> Attempt {
        #[cfg(test)]
        if let Some((probe, model_probe)) = &self.probe {
            let future = match model {
                None => probe(definition.id.clone()),
                Some(model) => model_probe(definition.id.clone(), model),
            };
            return future
                .map_err(|error| Arc::<str>::from(format!("{error:#}")))
                .boxed()
                .shared();
        }
        crate::controller::profile_config::discover_for(
            (*definition.config).clone(),
            definition.id.clone(),
            model,
            definition.cancellation.clone(),
        )
        .map_err(|error| Arc::<str>::from(format!("{error:#}")))
        .boxed()
        .shared()
    }

    fn spawn(self: &Arc<Self>, key: CacheKey, attempt: Attempt) {
        tokio::spawn(self.clone().drive(key, attempt));
    }

    fn drive(self: Arc<Self>, key: CacheKey, mut attempt: Attempt) -> BoxFuture<'static, ()> {
        Box::pin(async move {
            let cancellation = {
                let inner = self.lock();
                if !inner
                    .entries
                    .get(&key)
                    .is_some_and(|entry| entry.owns(&attempt))
                {
                    return;
                }
                inner.definitions[&key.0].cancellation.clone()
            };
            let mut failures = 0u32;
            loop {
                let result = tokio::select! {
                    _ = cancellation.cancelled() => return,
                    result = attempt.clone() => result,
                };
                let mut started = Vec::new();
                {
                    let mut inner = self.lock();
                    if !inner
                        .entries
                        .get(&key)
                        .is_some_and(|entry| entry.owns(&attempt))
                    {
                        return;
                    }
                    match &result {
                        Ok(choices) => {
                            inner
                                .entries
                                .insert(key.clone(), Entry::Ready(choices.clone()));
                            if key.1.is_none() {
                                let definition = inner.definitions[&key.0].clone();
                                for model in &choices.models {
                                    let model_key = (key.0.clone(), Some(model.value.clone()));
                                    if inner.entries.contains_key(&model_key) {
                                        continue;
                                    }
                                    if choices.model.as_ref() == Some(&model.value) {
                                        inner
                                            .entries
                                            .insert(model_key, Entry::Ready(choices.clone()));
                                    } else {
                                        let pending =
                                            self.attempt(&definition, Some(model.value.clone()));
                                        inner.entries.insert(
                                            model_key.clone(),
                                            Entry::Pending(pending.clone()),
                                        );
                                        started.push((model_key, pending));
                                    }
                                }
                            }
                        }
                        Err(error) => {
                            inner.entries.insert(
                                key.clone(),
                                Entry::Failed {
                                    attempt: attempt.clone(),
                                    error: error.clone(),
                                },
                            );
                            tracing::warn!(profile = %inner.definitions[&key.0].id, model = ?key.1, %error, "profile capability hydration failed; retrying with backoff");
                        }
                    }
                }
                self.publish();
                for (key, attempt) in started {
                    self.spawn(key, attempt);
                }
                if result.is_ok() {
                    return;
                }
                let delay = Duration::from_secs([1, 5, 30, 60][failures.min(3) as usize]);
                failures = failures.saturating_add(1);
                tokio::select! {
                    _ = cancellation.cancelled() => return,
                    _ = tokio::time::sleep(delay) => {}
                }
                {
                    let mut inner = self.lock();
                    if !inner
                        .entries
                        .get(&key)
                        .is_some_and(|entry| entry.owns(&attempt))
                    {
                        return;
                    }
                    attempt = self.attempt(&inner.definitions[&key.0], key.1.clone());
                    inner
                        .entries
                        .insert(key.clone(), Entry::Pending(attempt.clone()));
                }
                self.publish();
            }
        })
    }

    async fn fetch(
        self: &Arc<Self>,
        config: &Config,
        id: &str,
        model: Option<String>,
    ) -> Result<ProfileConfig> {
        let profile = config.enabled_profile(id).context("profile unavailable")?;
        self.ensure(config);
        let key = (profile.capabilities_key(id), model);
        let cancellation = self.lock().definitions[&key.0].cancellation.clone();
        let mut revisions = self.revisions.subscribe();
        loop {
            // Subscribe before inspecting ownership, so completion cannot be
            // missed. Readers never drive or publish the supervisor's future.
            let (entry, started) = {
                let mut inner = self.lock();
                let default = inner
                    .entries
                    .get(&(key.0.clone(), None))
                    .cloned()
                    .expect("default claimed by ensure");
                if key.1.is_some() && !matches!(default, Entry::Ready(_)) {
                    (default, None)
                } else if let Some(entry) = inner.entries.get(&key) {
                    (entry.clone(), None)
                } else {
                    let attempt = self.attempt(&inner.definitions[&key.0], key.1.clone());
                    let entry = Entry::Pending(attempt.clone());
                    inner.entries.insert(key.clone(), entry.clone());
                    (entry, Some(attempt))
                }
            };
            if let Some(attempt) = started {
                self.spawn(key.clone(), attempt);
                self.publish();
            }
            match entry {
                Entry::Ready(choices) => return Ok(choices),
                Entry::Failed { error, .. } => bail!("{error}"),
                Entry::Pending(_) => {}
            }
            // Shutdown also cancels this definition; report the owning cause
            // consistently when both tokens are ready.
            tokio::select! {
                biased;
                _ = self.cancellation.cancelled() => bail!("profile discovery cancelled by daemon shutdown"),
                _ = cancellation.cancelled() => bail!("profile definition retired"),
                result = revisions.changed() => { result.context("profile capability publisher stopped")?; }
            }
        }
    }

    fn live(&self) -> Result<Config> {
        self.lock()
            .live
            .clone()
            .context("the profile catalogue has not adopted a configuration")
    }

    pub(crate) fn candidates(&self, parent: &str) -> Result<Vec<(String, HarnessKind)>> {
        let config = self.live()?;
        Ok(config
            .enabled_profiles()
            .filter(|(id, _)| config.subagents.profile_is_eligible(parent, id))
            .map(|(id, profile)| (id.into(), profile.kind))
            .collect())
    }

    pub(crate) fn configured_candidates(&self) -> Result<Vec<(String, HarnessKind)>> {
        Ok(self
            .live()?
            .enabled_profiles()
            .map(|(id, profile)| (id.into(), profile.kind))
            .collect())
    }

    pub(crate) async fn model_capabilities(
        self: &Arc<Self>,
        profile: String,
        model: String,
    ) -> Result<ProfileConfig> {
        self.live_capabilities(&profile, Some(model)).await
    }

    async fn live_capabilities(
        self: &Arc<Self>,
        id: &str,
        model: Option<String>,
    ) -> Result<ProfileConfig> {
        loop {
            let config = self.live()?;
            let identity = config
                .enabled_profile(id)
                .context("profile unavailable")?
                .capabilities_key(id);
            let result = self.fetch(&config, id, model.clone()).await;
            let current = self.live()?;
            if current
                .enabled_profile(id)
                .map(|profile| profile.capabilities_key(id))
                .as_ref()
                == Some(&identity)
            {
                return result;
            }
            // Only this profile's configured definition changed. Follow its
            // new owner; unrelated profile updates cannot retire this read.
        }
    }

    pub(crate) async fn capabilities(
        self: &Arc<Self>,
        profiles: &[String],
    ) -> Result<Vec<ProfileConfig>> {
        self.live()?;
        join_all(profiles.iter().map(|id| self.live_capabilities(id, None)))
            .await
            .into_iter()
            .collect()
    }

    pub(crate) async fn options_for(
        self: &Arc<Self>,
        config: &Config,
        parent: &str,
        model: Option<String>,
    ) -> Result<mj_core::subagent::SubagentOptions> {
        crate::controller::profile_config::subagent_options_with(
            config,
            parent,
            model,
            |id, model| async move { self.fetch(config, &id, model).await },
        )
        .await
    }

    fn lock(&self) -> MutexGuard<'_, Inner> {
        self.inner.lock().unwrap_or_else(PoisonError::into_inner)
    }

    #[cfg(test)]
    fn build(
        cancellation: CancellationToken,
        probe: Arc<Probe>,
        model_probe: Arc<ModelProbe>,
    ) -> Arc<Self> {
        let mut catalog = Self::new(cancellation);
        Arc::get_mut(&mut catalog).unwrap().probe = Some((probe, model_probe));
        catalog
    }
    #[cfg(test)]
    pub(crate) fn with_probe(probe: Arc<Probe>) -> Arc<Self> {
        let model_probe = probe.clone();
        Self::build(
            CancellationToken::new(),
            probe,
            Arc::new(move |profile, _| model_probe(profile)),
        )
    }
    #[cfg(test)]
    pub(crate) async fn sync_now(self: &Arc<Self>, config: &Config) {
        self.sync(config);
        let discoveries = config.enabled_profiles().map(|(id, _)| async move {
            if let Ok(choices) = self.fetch(config, id, None).await {
                for model in choices.models {
                    let _ = self.fetch(config, id, Some(model.value)).await;
                }
            }
        });
        join_all(discoveries).await;
    }
}

/// A configuration with the named profiles enabled and, when `eligible` names
/// them, admitted for every parent. Shared with the API's `list_profiles`
/// test so both drive the same candidate filter.
#[cfg(test)]
pub(crate) fn test_config(profiles: &[(&str, HarnessKind)], eligible: &[&str]) -> Config {
    Config {
        profiles: profiles
            .iter()
            .map(|(id, kind)| {
                (
                    (*id).to_owned(),
                    HarnessProfile {
                        enabled: true,
                        kind: *kind,
                        home: std::path::PathBuf::from("/home/agent").join(id),
                        environment: Default::default(),
                        context_window_bytes: None,
                        subagents: Default::default(),
                        guardian_review_model: None,
                    },
                )
            })
            .collect(),
        subagents: SubagentConfig {
            eligible_profiles: eligible.iter().map(|id| ((*id).to_owned(), true)).collect(),
            ..SubagentConfig::default()
        },
        ..Config::default()
    }
}

/// A probe that answers with the profile's own id and counts its calls, so a
/// test can prove that a warm answer runs none.
#[cfg(test)]
pub(crate) fn counting_probe(calls: Arc<std::sync::atomic::AtomicUsize>) -> Arc<Probe> {
    Arc::new(move |profile| {
        calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        Box::pin(async move { Ok(test_choices(&profile)) })
    })
}

/// The capabilities a fixture probe reports for a profile.
#[cfg(test)]
fn test_choices(profile: &str) -> ProfileConfig {
    ProfileConfig {
        model: Some(format!("{profile}-model")),
        models: vec![mj_core::acp::SessionConfigChoice {
            value: format!("{profile}-model"),
            name: format!("{profile} model"),
            description: None,
        }],
        efforts: Vec::new(),
        observed_at: 1_700_000_000,
    }
}

/// A probe that fails while the flag is set and counts its calls, so a test
/// can drive the background pass, the call that retries after it, and the
/// cache in between.
#[cfg(test)]
fn flag_probe(
    fails: Arc<std::sync::atomic::AtomicBool>,
    calls: Arc<std::sync::atomic::AtomicUsize>,
) -> Arc<Probe> {
    Arc::new(move |profile| {
        let fails = fails.clone();
        let calls = calls.clone();
        Box::pin(async move {
            calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            if fails.load(std::sync::atomic::Ordering::SeqCst) {
                anyhow::bail!("harness is not installed")
            }
            Ok(test_choices(&profile))
        })
    })
}

/// A probe that reports each discovery it starts and then waits for the test
/// to release it, so a test can hold a discovery in flight and see who shares
/// it. The release is a `watch` channel rather than a `Notify` so that a
/// release arriving before a waiter does is still observed.
#[cfg(test)]
fn gated_probe(
    calls: Arc<std::sync::atomic::AtomicUsize>,
    started: tokio::sync::mpsc::UnboundedSender<String>,
    gate: tokio::sync::watch::Receiver<bool>,
) -> Arc<Probe> {
    Arc::new(move |profile| {
        let calls = calls.clone();
        let started = started.clone();
        let mut gate = gate.clone();
        Box::pin(async move {
            calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
            let _ = started.send(profile.clone());
            if !*gate.borrow_and_update() {
                let _ = gate.changed().await;
            }
            Ok(test_choices(&profile))
        })
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

    fn calls() -> Arc<AtomicUsize> {
        Arc::new(AtomicUsize::new(0))
    }

    #[tokio::test]
    async fn pending_model_efforts_share_one_attempt_and_unchanged_profiles_stay_warm() {
        let calls = calls();
        let (started_tx, mut started) = tokio::sync::mpsc::unbounded_channel();
        let (release, gate) = tokio::sync::watch::channel(false);
        let model_calls = calls.clone();
        let catalog = ProfileCatalog::build(
            CancellationToken::new(),
            Arc::new(|id| {
                Box::pin(async move {
                    let mut choices = test_choices(&id);
                    choices.models.push(mj_core::acp::SessionConfigChoice {
                        value: "other".into(),
                        name: "Other".into(),
                        description: None,
                    });
                    Ok(choices)
                })
            }),
            Arc::new(move |id, model| {
                model_calls.fetch_add(1, Ordering::SeqCst);
                let started = started_tx.clone();
                let mut gate = gate.clone();
                Box::pin(async move {
                    started.send(id.clone()).unwrap();
                    if !*gate.borrow_and_update() {
                        gate.changed().await.unwrap();
                    }
                    let mut choices = test_choices(&id);
                    choices.models.push(mj_core::acp::SessionConfigChoice {
                        value: model.clone(),
                        name: model.clone(),
                        description: None,
                    });
                    choices.model = Some(model);
                    Ok(choices)
                })
            }),
        );
        let mut config = test_config(&[("parent", HarnessKind::Codex)], &[]);
        catalog.sync(&config);
        assert_eq!(started.recv().await.as_deref(), Some("parent"));
        assert!(
            catalog
                .snapshot()
                .options(&config, "parent", None)
                .is_some()
        );
        assert!(
            catalog
                .snapshot()
                .options(&config, "parent", Some("other"))
                .is_none()
        );
        let reader = {
            let catalog = catalog.clone();
            let config = config.clone();
            tokio::spawn(async move {
                catalog
                    .options_for(&config, "parent", Some("other".into()))
                    .await
            })
        };
        // Adding a different profile must leave this model's pending owner intact.
        config.profiles.insert(
            "sibling".into(),
            test_config(&[("sibling", HarnessKind::Claude)], &[])
                .profiles
                .remove("sibling")
                .unwrap(),
        );
        catalog.sync(&config);
        assert_eq!(started.recv().await.as_deref(), Some("sibling"));
        assert!(!reader.is_finished());
        assert_eq!(calls.load(Ordering::SeqCst), 2);
        release.send(true).unwrap();
        reader.await.unwrap().unwrap();
        catalog.sync_now(&config).await;
        catalog
            .options_for(&config, "parent", Some("other".into()))
            .await
            .unwrap();
        config.profiles.remove("sibling");
        catalog.sync_now(&config).await;
        catalog
            .options_for(&config, "parent", Some("other".into()))
            .await
            .unwrap();
        assert_eq!(
            calls.load(Ordering::SeqCst),
            2,
            "membership changes retain unchanged efforts"
        );
    }

    #[tokio::test]
    async fn concurrent_readers_and_eligibility_edits_join_startup_hydration() {
        let calls = calls();
        let (started_tx, mut started) = tokio::sync::mpsc::unbounded_channel();
        let (release, gate) = tokio::sync::watch::channel(false);
        let catalog = ProfileCatalog::with_probe(gated_probe(calls.clone(), started_tx, gate));
        let mut config = test_config(
            &[
                ("parent", HarnessKind::Codex),
                ("helper", HarnessKind::Claude),
            ],
            &[],
        );
        catalog.sync(&config);
        started.recv().await.unwrap();
        started.recv().await.unwrap();
        let reader = {
            let catalog = catalog.clone();
            tokio::spawn(async move {
                catalog
                    .capabilities(&["parent".into(), "helper".into()])
                    .await
            })
        };
        config
            .subagents
            .eligible_profiles
            .insert("helper".into(), true);
        config.profiles.get_mut("parent").unwrap().subagents =
            mj_core::subagent::SubagentPolicy::SingleModel {
                model: "parent-model".into(),
                effort: None,
            };
        catalog.sync(&config);
        assert_eq!(calls.load(Ordering::SeqCst), 2);
        assert_eq!(catalog.candidates("parent").unwrap().len(), 2);
        assert!(
            catalog
                .snapshot()
                .options(&config, "parent", None)
                .is_none()
        );
        release.send(true).unwrap();
        assert_eq!(reader.await.unwrap().unwrap().len(), 2);
        assert_eq!(calls.load(Ordering::SeqCst), 2);
    }

    #[tokio::test]
    async fn profile_definition_changes_do_not_retire_unrelated_reads_or_publish_old_choices() {
        let calls = calls();
        let (started_tx, mut started) = tokio::sync::mpsc::unbounded_channel();
        let (release, gate) = tokio::sync::watch::channel(false);
        let probe_calls = calls.clone();
        let probe: Arc<Probe> = Arc::new(move |id| {
            let number = probe_calls.fetch_add(1, Ordering::SeqCst);
            let mut gate = gate.clone();
            let started = started_tx.clone();
            Box::pin(async move {
                started.send(id.clone()).unwrap();
                if number == 0 {
                    gate.changed().await.unwrap();
                }
                let mut choices = test_choices(&id);
                choices.observed_at = number as i64;
                Ok(choices)
            })
        });
        let catalog = ProfileCatalog::with_probe(probe);
        let mut config = test_config(&[("parent", HarnessKind::Codex)], &[]);
        catalog.sync(&config);
        started.recv().await.unwrap();
        let old_key = config.profiles["parent"].capabilities_key("parent");
        let pending = {
            let catalog = catalog.clone();
            tokio::spawn(async move { catalog.capabilities(&["parent".into()]).await })
        };
        tokio::task::yield_now().await;
        config.profiles.get_mut("parent").unwrap().home = "/changed".into();
        config.profiles.insert(
            "sibling".into(),
            test_config(&[("sibling", HarnessKind::Claude)], &[])
                .profiles
                .remove("sibling")
                .unwrap(),
        );
        catalog.sync(&config);
        let current = pending.await.unwrap().unwrap();
        assert_eq!(current[0].observed_at, 1);
        let choices = catalog.capabilities(&["sibling".into()]).await.unwrap();
        assert_eq!(choices[0].model.as_deref(), Some("sibling-model"));
        release.send(true).unwrap();
        assert_ne!(
            old_key,
            config.profiles["parent"].capabilities_key("parent")
        );
        assert_eq!(
            catalog.capabilities(&["parent".into()]).await.unwrap()[0].observed_at,
            1
        );
        assert_eq!(calls.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn drafts_share_global_entries_without_adopting_their_profile_list() {
        let calls = calls();
        let catalog = ProfileCatalog::with_probe(counting_probe(calls.clone()));
        let live = test_config(&[("parent", HarnessKind::Codex)], &[]);
        catalog.sync_now(&live).await;
        let mut draft = live.clone();
        draft.profiles.get_mut("parent").unwrap().home = "/draft".into();
        catalog.ensure(&draft);
        catalog.ensure(&draft);
        let (left, right) = tokio::join!(
            catalog.options_for(&draft, "parent", None),
            catalog.options_for(&draft, "parent", None)
        );
        left.unwrap();
        right.unwrap();
        assert_eq!(calls.load(Ordering::SeqCst), 2);
        assert_eq!(
            catalog.live().unwrap().profiles["parent"].home,
            live.profiles["parent"].home
        );
        catalog.options_for(&live, "parent", None).await.unwrap();
        assert_eq!(calls.load(Ordering::SeqCst), 2);
    }

    #[tokio::test(start_paused = true)]
    async fn failures_are_reported_and_only_the_owner_retries_after_backoff() {
        let calls = calls();
        let failing = Arc::new(AtomicBool::new(true));
        let catalog = ProfileCatalog::with_probe(flag_probe(failing.clone(), calls.clone()));
        let config = test_config(&[("parent", HarnessKind::Codex)], &[]);
        catalog.sync_now(&config).await;
        for _ in 0..3 {
            assert!(catalog.capabilities(&["parent".into()]).await.is_err());
        }
        assert_eq!(
            calls.load(Ordering::SeqCst),
            1,
            "failed UI queries cannot launch retries"
        );
        let options = catalog.snapshot().options(&config, "parent", None).unwrap();
        assert!(options.unavailable[0].contains("harness is not installed"));
        failing.store(false, Ordering::SeqCst);
        tokio::time::advance(Duration::from_secs(1)).await;
        for _ in 0..10 {
            tokio::task::yield_now().await;
        }
        catalog.capabilities(&["parent".into()]).await.unwrap();
        assert_eq!(calls.load(Ordering::SeqCst), 2);
    }

    #[tokio::test]
    async fn shutdown_cancels_readers_and_background_hydration() {
        let calls = calls();
        let (started_tx, mut started) = tokio::sync::mpsc::unbounded_channel();
        let (_release, gate) = tokio::sync::watch::channel(false);
        let cancellation = CancellationToken::new();
        let probe = gated_probe(calls, started_tx, gate);
        let model_probe = probe.clone();
        let catalog = ProfileCatalog::build(
            cancellation.clone(),
            probe,
            Arc::new(move |id, _| model_probe(id)),
        );
        catalog.sync(&test_config(&[("parent", HarnessKind::Codex)], &[]));
        started.recv().await.unwrap();
        cancellation.cancel();
        assert!(
            catalog
                .capabilities(&["parent".into()])
                .await
                .unwrap_err()
                .to_string()
                .contains("shutdown")
        );
    }

    #[tokio::test]
    async fn answers_before_adoption_report_that_without_launching_a_probe() {
        let calls = calls();
        let catalog = ProfileCatalog::with_probe(counting_probe(calls.clone()));
        assert!(
            catalog
                .candidates("parent")
                .unwrap_err()
                .to_string()
                .contains("not adopted")
        );
        assert!(catalog.capabilities(&["parent".into()]).await.is_err());
        assert_eq!(calls.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn configured_candidates_include_enabled_profiles_outside_subagent_eligibility() {
        let catalog = ProfileCatalog::new(CancellationToken::new());
        let mut config = test_config(
            &[
                ("codex", HarnessKind::Codex),
                ("helper", HarnessKind::Codex),
                ("claude4", HarnessKind::Claude),
                ("disabled", HarnessKind::Kimi),
            ],
            &["helper"],
        );
        config.profiles.get_mut("disabled").unwrap().enabled = false;
        catalog.sync(&config);

        let subagent_ids = catalog
            .candidates("codex")
            .unwrap()
            .into_iter()
            .map(|(id, _)| id)
            .collect::<Vec<_>>();
        let configured_ids = catalog
            .configured_candidates()
            .unwrap()
            .into_iter()
            .map(|(id, _)| id)
            .collect::<Vec<_>>();

        assert_eq!(subagent_ids, vec!["codex".to_owned(), "helper".to_owned()]);
        assert_eq!(
            configured_ids,
            vec![
                "claude4".to_owned(),
                "codex".to_owned(),
                "helper".to_owned()
            ]
        );
    }
}