brokk-mj-client 2.7.2

Client-facing contracts and shared presentation helpers 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
//! The session operations used by interactive control surfaces.

use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

use agent_client_protocol::schema::v1::SessionConfigOption;
use anyhow::{Context, Result, ensure};
use mj_core::config::Config;
use mj_core::elicitation::ElicitationResponse;
use mj_core::state::{ManagedSessionSnapshot, SessionRecord};

use mj_core::relay::{
    AnalyzeDeltaRepository, RelayCommand, RelayCursor, RelayEvent, RelayOperationalState, RepoDelta,
};
use mj_core::worker_launch::ReviewerLaunchConfig;

pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(tag = "kind", content = "detail", rename_all = "snake_case")]
pub enum ViewError {
    Unreachable(String),
    TargetMissing(String),
    ProjectionIntegrity(String),
}

impl ViewError {
    pub fn detail(&self) -> &str {
        match self {
            Self::Unreachable(detail)
            | Self::TargetMissing(detail)
            | Self::ProjectionIntegrity(detail) => detail,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct ManagedSessionView {
    pub snapshot: Option<ManagedSessionSnapshot>,
    pub connected: bool,
    pub error: Option<ViewError>,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct RelayAttachment {
    pub state: RelayOperationalState,
    pub events: Vec<RelayEvent>,
    pub through_ordinal: u64,
    pub through_digest: String,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct StartedReviewer {
    pub native_session_id: Option<String>,
    pub config_options: Vec<SessionConfigOption>,
    pub reused: bool,
    pub state: RelayOperationalState,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReviewerAction {
    Start {
        config: Box<ReviewerLaunchConfig>,
    },
    Submit {
        command_id: String,
        command: RelayCommand,
    },
    Attach {
        after_ordinal: u64,
        after_digest: String,
    },
    Acknowledge {
        through_ordinal: u64,
        through_digest: String,
    },
    Status,
    RespondElicitation {
        elicitation_id: String,
        response: ElicitationResponse,
    },
    Pause,
    CaptureDelta {
        baselines: std::collections::BTreeMap<std::path::PathBuf, String>,
    },
    AdvanceBaseline {
        trees: std::collections::BTreeMap<std::path::PathBuf, String>,
    },
    AnalyzeDelta {
        repositories: Vec<AnalyzeDeltaRepository>,
    },
    TakeLaneDispatches,
}

impl ReviewerAction {
    pub const fn operation_name(&self) -> &'static str {
        match self {
            Self::Start { .. } => "reviewer_start",
            Self::Submit { .. } => "reviewer_submit",
            Self::Attach { .. } => "reviewer_attach",
            Self::Acknowledge { .. } => "reviewer_acknowledge",
            Self::Status => "reviewer_status",
            Self::RespondElicitation { .. } => "reviewer_respond_elicitation",
            Self::Pause => "reviewer_pause",
            Self::CaptureDelta { .. } => "reviewer_capture_delta",
            Self::AdvanceBaseline { .. } => "reviewer_advance_baseline",
            Self::AnalyzeDelta { .. } => "reviewer_analyze_delta",
            Self::TakeLaneDispatches => "reviewer_take_lane_dispatches",
        }
    }
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReviewerOutcome {
    Started(Box<StartedReviewer>),
    Accepted {
        ordinal: u64,
    },
    Attached(Box<RelayAttachment>),
    Acknowledged(RelayCursor),
    Status(Box<RelayOperationalState>),
    ElicitationResolved,
    Paused,
    Delta {
        repositories: Vec<RepoDelta>,
    },
    BaselineAdvanced,
    ChangedFunctions {
        packet: String,
    },
    LaneDispatches {
        requests: Vec<mj_core::review::lanes::ReviewSubagentRequest>,
    },
}

pub struct PendingRelaySubmit {
    completion: BoxFuture<'static, Result<u64>>,
}

impl PendingRelaySubmit {
    pub fn new(completion: BoxFuture<'static, Result<u64>>) -> Self {
        Self { completion }
    }

    pub async fn wait(self) -> Result<u64> {
        self.completion.await
    }
}

pub struct PendingRelaySync {
    completion: BoxFuture<'static, Result<()>>,
}

impl PendingRelaySync {
    pub fn new(completion: BoxFuture<'static, Result<()>>) -> Self {
        Self { completion }
    }

    pub async fn wait(self) -> Result<()> {
        self.completion.await
    }
}

#[derive(Debug, Default)]
pub struct ReviewState {
    pub review: Option<mj_core::storage::StoredReview>,
    pub defaults: mj_core::second_opinion::ReviewerDefaults,
}

pub trait SessionHandleBackend: Send + Sync {
    fn search_prompts(
        &self,
        bundle_id: String,
        scope: mj_core::storage::HistoryScope,
        query: String,
    ) -> BoxFuture<'_, Result<Vec<mj_core::storage::PromptHistoryEntry>>>;
    fn review_state(&self) -> BoxFuture<'_, Result<ReviewState>>;

    fn config_result(&self, command_id: String) -> BoxFuture<'_, Result<Option<Option<String>>>>;

    fn clone_box(&self) -> Box<dyn SessionHandleBackend>;
    fn session_id(&self) -> &str;
    fn view(&self) -> ManagedSessionView;
    fn is_stopped(&self) -> bool;
    fn has_changed(&self) -> Result<bool>;
    fn changed(&mut self) -> BoxFuture<'_, Result<ManagedSessionView>>;
    fn enqueue_submit(
        &self,
        command_id: String,
        command: RelayCommand,
    ) -> BoxFuture<'_, Result<PendingRelaySubmit>>;
    fn enqueue_sync(&self) -> BoxFuture<'_, Result<PendingRelaySync>>;
    fn respond_elicitation(
        &self,
        elicitation_id: String,
        response: ElicitationResponse,
    ) -> BoxFuture<'_, Result<()>>;
    fn stop_background_task(&self, background_task_id: String) -> BoxFuture<'_, Result<()>>;
    fn reviewer(
        &self,
        role: Option<String>,
        action: ReviewerAction,
    ) -> BoxFuture<'_, Result<ReviewerOutcome>>;
}

pub struct SessionHandle {
    backend: Box<dyn SessionHandleBackend>,
}

impl SessionHandle {
    pub async fn search_prompts(
        &self,
        bundle_id: String,
        scope: mj_core::storage::HistoryScope,
        query: String,
    ) -> Result<Vec<mj_core::storage::PromptHistoryEntry>> {
        self.backend.search_prompts(bundle_id, scope, query).await
    }
    pub async fn review_state(&self) -> Result<ReviewState> {
        self.backend.review_state().await
    }

    pub fn new(backend: impl SessionHandleBackend + 'static) -> Self {
        Self {
            backend: Box::new(backend),
        }
    }

    pub fn session_id(&self) -> &str {
        self.backend.session_id()
    }

    pub fn view(&self) -> ManagedSessionView {
        self.backend.view()
    }

    pub fn is_stopped(&self) -> bool {
        self.backend.is_stopped()
    }

    pub fn has_changed(&self) -> Result<bool> {
        self.backend.has_changed()
    }

    pub async fn changed(&mut self) -> Result<ManagedSessionView> {
        self.backend.changed().await
    }

    pub async fn submit(&self, command_id: String, command: RelayCommand) -> Result<u64> {
        self.enqueue_submit(command_id, command).await?.wait().await
    }

    /// Apply a setting and wait for its durable success or rejection.
    pub async fn set_config(&self, key: String, value: String) -> Result<()> {
        let command_id = new_command_id("set-config")?;
        self.submit(command_id.clone(), RelayCommand::SetConfig { key, value })
            .await?;
        tokio::time::timeout(Duration::from_secs(60), async {
            loop {
                if let Some(error) = self.backend.config_result(command_id.clone()).await? {
                    if let Some(error) = error {
                        anyhow::bail!("{error}");
                    }
                    self.sync_now().await?;
                    return Ok(());
                }
                ensure!(
                    !self.is_stopped(),
                    "session stopped while applying configuration"
                );
                if let Some(error) = self.view().error {
                    anyhow::bail!("configuration connection failed: {}", error.detail());
                }
                tokio::time::sleep(Duration::from_millis(50)).await;
            }
        })
        .await
        .context("configuration command did not complete within 60 seconds")?
    }

    pub async fn enqueue_submit(
        &self,
        command_id: String,
        command: RelayCommand,
    ) -> Result<PendingRelaySubmit> {
        self.backend.enqueue_submit(command_id, command).await
    }

    pub async fn sync_now(&self) -> Result<()> {
        self.enqueue_sync().await?.wait().await
    }

    pub async fn enqueue_sync(&self) -> Result<PendingRelaySync> {
        self.backend.enqueue_sync().await
    }

    pub async fn respond_elicitation(
        &self,
        elicitation_id: String,
        response: ElicitationResponse,
    ) -> Result<()> {
        self.backend
            .respond_elicitation(elicitation_id, response)
            .await
    }

    pub async fn stop_background_task(&self, background_task_id: String) -> Result<()> {
        self.backend.stop_background_task(background_task_id).await
    }

    pub async fn reviewer(&self, action: ReviewerAction) -> Result<ReviewerOutcome> {
        self.reviewer_as(None, action).await
    }

    pub async fn reviewer_as(
        &self,
        role: Option<String>,
        action: ReviewerAction,
    ) -> Result<ReviewerOutcome> {
        self.backend.reviewer(role, action).await
    }
}

impl Clone for SessionHandle {
    fn clone(&self) -> Self {
        Self {
            backend: self.backend.clone_box(),
        }
    }
}

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

pub trait SessionControlBackend: Send + Sync {
    fn session(&self, session_id: String) -> BoxFuture<'_, Result<SessionHandle>>;
}

#[derive(Clone)]
pub struct SessionControl {
    backend: Arc<dyn SessionControlBackend>,
}

impl SessionControl {
    pub fn new(backend: impl SessionControlBackend + 'static) -> Self {
        Self {
            backend: Arc::new(backend),
        }
    }

    pub async fn session(&self, session_id: impl Into<String>) -> Result<SessionHandle> {
        self.backend.session(session_id.into()).await
    }

    pub async fn wait_for_session(
        &self,
        session_id: &str,
        timeout: Duration,
    ) -> Result<SessionHandle> {
        tokio::time::timeout(timeout, async {
            loop {
                match self.session(session_id.to_owned()).await {
                    Ok(handle) => return Ok(handle),
                    Err(error) => {
                        tracing::trace!(session_id, "waiting for session actor: {error:#}");
                        tokio::time::sleep(Duration::from_millis(25)).await;
                    }
                }
            }
        })
        .await
        .with_context(|| {
            format!(
                "session {session_id} did not become available within {} seconds",
                timeout.as_secs()
            )
        })?
    }
}

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

pub trait ReviewerStagerBackend: Send + Sync {
    fn stage(
        &self,
        config: Config,
        session: SessionRecord,
        profile_id: String,
        generation: u64,
    ) -> Result<ReviewerLaunchConfig>;
}

#[derive(Clone)]
pub struct ReviewerStager {
    backend: Arc<dyn ReviewerStagerBackend>,
}

impl ReviewerStager {
    pub fn new(backend: impl ReviewerStagerBackend + 'static) -> Self {
        Self {
            backend: Arc::new(backend),
        }
    }

    pub fn stage(
        &self,
        config: Config,
        session: SessionRecord,
        profile_id: String,
        generation: u64,
    ) -> Result<ReviewerLaunchConfig> {
        self.backend.stage(config, session, profile_id, generation)
    }

    #[doc(hidden)]
    pub fn unavailable(message: impl Into<String>) -> Self {
        Self::new(UnavailableReviewerStager(message.into()))
    }
}

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

struct UnavailableReviewerStager(String);

impl ReviewerStagerBackend for UnavailableReviewerStager {
    fn stage(
        &self,
        _config: Config,
        _session: SessionRecord,
        _profile_id: String,
        _generation: u64,
    ) -> Result<ReviewerLaunchConfig> {
        anyhow::bail!(self.0.clone())
    }
}

pub fn new_command_id(prefix: &str) -> Result<String> {
    ensure!(!prefix.trim().is_empty(), "command ID prefix is required");
    let mut random = [0_u8; 16];
    getrandom::fill(&mut random)
        .map_err(|error| anyhow::anyhow!("generate command ID: {error}"))?;
    Ok(format!("{prefix}-{}", hex(&random)))
}

fn hex(bytes: &[u8]) -> String {
    const DIGITS: &[u8; 16] = b"0123456789abcdef";
    let mut output = String::with_capacity(bytes.len() * 2);
    for byte in bytes {
        output.push(char::from(DIGITS[usize::from(byte >> 4)]));
        output.push(char::from(DIGITS[usize::from(byte & 0x0f)]));
    }
    output
}

/// A stopped session and a manager that resolves its live replacement.
///
/// Chat's cross-crate tests use this hand-written client fake to verify actor
/// replacement without depending on the controller implementation crate.
#[doc(hidden)]
pub struct ReplacementSessionTestFixture {
    pub stopped: SessionHandle,
    pub control: SessionControl,
    pub submitted: tokio::sync::mpsc::UnboundedReceiver<RelayCommand>,
}

#[derive(Clone)]
struct ReplacementTestSession {
    #[cfg(test)]
    history: Option<tokio::sync::mpsc::UnboundedSender<HistoryTestRequest>>,
    session_id: String,
    stopped: bool,
    accepted_ordinal: u64,
    submitted: Option<tokio::sync::mpsc::UnboundedSender<RelayCommand>>,
    view: tokio::sync::watch::Receiver<ManagedSessionView>,
    _view_guard: Option<Arc<tokio::sync::watch::Sender<ManagedSessionView>>>,
}

impl SessionHandleBackend for ReplacementTestSession {
    fn search_prompts(
        &self,
        _bundle_id: String,
        _scope: mj_core::storage::HistoryScope,
        _query: String,
    ) -> BoxFuture<'_, Result<Vec<mj_core::storage::PromptHistoryEntry>>> {
        #[cfg(test)]
        if let Some(history) = &self.history {
            let (response, result) = tokio::sync::oneshot::channel();
            let sent = history.send(HistoryTestRequest {
                bundle_id: _bundle_id,
                scope: _scope,
                query: _query,
                response,
            });
            return Box::pin(async move {
                sent.map_err(|_| anyhow::anyhow!("history backend closed"))?;
                result
                    .await
                    .map_err(|_| anyhow::anyhow!("history response dropped"))?
            });
        }
        Box::pin(async { Ok(Vec::new()) })
    }
    fn review_state(&self) -> BoxFuture<'_, Result<ReviewState>> {
        Box::pin(async { Ok(ReviewState::default()) })
    }

    fn config_result(&self, _command_id: String) -> BoxFuture<'_, Result<Option<Option<String>>>> {
        Box::pin(async { Ok(None) })
    }
    fn clone_box(&self) -> Box<dyn SessionHandleBackend> {
        Box::new(self.clone())
    }

    fn session_id(&self) -> &str {
        &self.session_id
    }

    fn view(&self) -> ManagedSessionView {
        self.view.borrow().clone()
    }

    fn is_stopped(&self) -> bool {
        self.stopped
    }

    fn has_changed(&self) -> Result<bool> {
        self.view.has_changed().context("session manager stopped")
    }

    fn changed(&mut self) -> BoxFuture<'_, Result<ManagedSessionView>> {
        Box::pin(async move {
            self.view
                .changed()
                .await
                .context("session manager stopped")?;
            Ok(self.view())
        })
    }

    fn enqueue_submit(
        &self,
        _command_id: String,
        command: RelayCommand,
    ) -> BoxFuture<'_, Result<PendingRelaySubmit>> {
        let submitted = self.submitted.clone();
        let stopped = self.stopped;
        let accepted_ordinal = self.accepted_ordinal;
        Box::pin(async move {
            ensure!(!stopped, "session manager stopped");
            let submitted = submitted.context("unsupported test operation")?;
            submitted
                .send(command)
                .context("test submit observer stopped")?;
            Ok(PendingRelaySubmit::new(Box::pin(async move {
                Ok(accepted_ordinal)
            })))
        })
    }

    fn enqueue_sync(&self) -> BoxFuture<'_, Result<PendingRelaySync>> {
        let stopped = self.stopped;
        Box::pin(async move {
            ensure!(!stopped, "session manager stopped");
            Ok(PendingRelaySync::new(Box::pin(async { Ok(()) })))
        })
    }

    fn respond_elicitation(
        &self,
        _elicitation_id: String,
        _response: ElicitationResponse,
    ) -> BoxFuture<'_, Result<()>> {
        Box::pin(async { anyhow::bail!("unsupported test operation") })
    }

    fn stop_background_task(&self, _background_task_id: String) -> BoxFuture<'_, Result<()>> {
        Box::pin(async { anyhow::bail!("unsupported test operation") })
    }

    fn reviewer(
        &self,
        _role: Option<String>,
        _action: ReviewerAction,
    ) -> BoxFuture<'_, Result<ReviewerOutcome>> {
        Box::pin(async { anyhow::bail!("unsupported test operation") })
    }
}

struct ReplacementTestControl {
    session_id: String,
    replacement: SessionHandle,
}

impl SessionControlBackend for ReplacementTestControl {
    fn session(&self, session_id: String) -> BoxFuture<'_, Result<SessionHandle>> {
        Box::pin(async move {
            ensure!(
                session_id == self.session_id,
                "session {session_id} is not managed"
            );
            Ok(self.replacement.clone())
        })
    }
}

#[doc(hidden)]
pub fn replacement_session_test_fixture(
    session_id: &str,
    accepted_ordinal: u64,
) -> ReplacementSessionTestFixture {
    let (stopped_view_tx, stopped_view) =
        tokio::sync::watch::channel(ManagedSessionView::default());
    drop(stopped_view_tx);
    let stopped = SessionHandle::new(ReplacementTestSession {
        #[cfg(test)]
        history: None,
        session_id: session_id.to_owned(),
        stopped: true,
        accepted_ordinal,
        submitted: None,
        view: stopped_view,
        _view_guard: None,
    });

    let (view_tx, view) = tokio::sync::watch::channel(ManagedSessionView::default());
    let (submitted_tx, submitted) = tokio::sync::mpsc::unbounded_channel();
    let replacement = SessionHandle::new(ReplacementTestSession {
        #[cfg(test)]
        history: None,
        session_id: session_id.to_owned(),
        stopped: false,
        accepted_ordinal,
        submitted: Some(submitted_tx),
        view,
        _view_guard: Some(Arc::new(view_tx)),
    });
    let control = SessionControl::new(ReplacementTestControl {
        session_id: session_id.to_owned(),
        replacement,
    });
    ReplacementSessionTestFixture {
        stopped,
        control,
        submitted,
    }
}

#[cfg(test)]
struct HistoryTestRequest {
    bundle_id: String,
    scope: mj_core::storage::HistoryScope,
    query: String,
    response: tokio::sync::oneshot::Sender<Result<Vec<mj_core::storage::PromptHistoryEntry>>>,
}

#[cfg(test)]
mod storage_tests {
    use super::*;
    use mj_core::storage::{HistoryScope, PromptHistoryEntry};

    #[tokio::test]
    async fn history_search_yields_until_backend_responds_and_propagates_failures() {
        let (history, mut requests) = tokio::sync::mpsc::unbounded_channel();
        let (view_guard, view) = tokio::sync::watch::channel(ManagedSessionView::default());
        let session = SessionHandle::new(ReplacementTestSession {
            history: Some(history),
            session_id: "session".into(),
            stopped: false,
            accepted_ordinal: 0,
            submitted: None,
            view,
            _view_guard: Some(Arc::new(view_guard)),
        });
        let search =
            session.search_prompts("bundle".into(), HistoryScope::Project, "needle".into());
        tokio::pin!(search);
        let request = tokio::select! {
            biased;
            result = &mut search => panic!("search completed before storage replied: {result:?}"),
            request = requests.recv() => request.unwrap(),
        };
        assert_eq!(request.bundle_id, "bundle");
        assert_eq!(request.scope, HistoryScope::Project);
        assert_eq!(request.query, "needle");
        request
            .response
            .send(Err(anyhow::anyhow!("storage unavailable")))
            .unwrap();
        assert!(
            search
                .await
                .unwrap_err()
                .to_string()
                .contains("storage unavailable")
        );

        let search = session.search_prompts("bundle".into(), HistoryScope::Project, "retry".into());
        tokio::pin!(search);
        let request = tokio::select! {
            biased;
            result = &mut search => panic!("retry completed before storage replied: {result:?}"),
            request = requests.recv() => request.unwrap(),
        };
        request
            .response
            .send(Ok(vec![PromptHistoryEntry {
                id: 1,
                session_id: "session".into(),
                text: "retry works".into(),
            }]))
            .unwrap();
        assert_eq!(search.await.unwrap()[0].text, "retry works");
    }
}