mindfork 0.18.3

A terminal AI chat written in Rust: local models via llama.cpp, or OpenAI, Anthropic, Gemini, Grok and OpenRouter in the cloud, with persistent memory, notes, RAG and tools.
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
//! [`EngineManager`] — the lifecycle of the inference/embedding servers: owns the
//! engines (`backend`/`imp_backend`/`embedder`), handles to managed processes
//! (`*_handle`, `kill_on_drop`), readiness statuses, and background-probe channels.
//! Extracted from the orchestrator (Phase 3): groups ~11 fields and the server logic
//! into a cohesive unit, a counterpart to the [`ServerSupervisor`] trait. The orchestrator
//! remains the sole owner of `Chat`; here — only servers, no domain state.

use std::sync::Arc;
use std::time::{Duration, Instant};

use tokio::sync::mpsc::UnboundedSender;
use tokio_util::sync::CancellationToken;

use crate::app::events::{ServerStatus, ServerStatuses};
use crate::app::supervisor::ServerSupervisor;
use crate::shared::api::{Embedder, EngineBackend, ServerHandle};
use crate::shared::config::{
    CloudProvider, EmbedSettings, EngineSettings, ImpersonationEngineSettings, ImpersonationMode,
    OpenRouterSettings, SecretSlot,
};
use crate::shared::i18n::Locale;
use crate::shared::secrets::{ApiKeyEntry, SecretKey};

/// Decrypts the stored key of whichever secret the slot's active mode reads
/// (`settings.secret_key()` — a cloud provider's key, or an external server's own;
/// see `config::SecretSlot`). `None` — the mode needs no key (managed), the key
/// isn't stored, or the entry is a foreign one → the supervisor falls back to env.
/// The resolution lives here so the supervisor doesn't need to know the
/// secret-storage format (`shared::secrets`). See docs/research/api-key-storage.md,
/// docs/history/external-api-key.md §5.3.
fn stored_key(api_keys: &[ApiKeyEntry], key: Option<SecretKey>) -> Option<String> {
    crate::shared::secrets::stored_key(api_keys, &key?.storage_name())
}

/// The gateway's provider-wide settings **as far as one slot is concerned**:
/// `Some` only while the slot speaks to the gateway.
///
/// This is what is compared to decide whether a server is current. Were the
/// settings compared for every slot, switching the attribution off would
/// relaunch a managed `llama-server` that has nothing to do with it — and a
/// relaunch there is a GGUF killed and loaded again.
fn gateway_for(
    provider: Option<CloudProvider>,
    gateway: &OpenRouterSettings,
) -> Option<OpenRouterSettings> {
    (provider == Some(CloudProvider::OpenRouter)).then(|| gateway.clone())
}

/// What the application puts around an embedder before anything uses it.
pub(super) type EmbedderDress = Box<dyn FnOnce(Arc<dyn Embedder>) -> Arc<dyn Embedder>>;

/// What a slot was last launched with: its settings, the key blob they were
/// resolved from, and the gateway's settings where they applied
/// ([`gateway_for`]).
type Applied<S> = (S, Vec<ApiKeyEntry>, Option<OpenRouterSettings>);

/// Max relaunches of one managed server within [`RESTART_WINDOW`]; beyond that it
/// stays `Disconnected` until manual intervention (editing settings re-applies it).
pub(super) const RESTART_BUDGET: usize = 3;
/// The restart budget's window.
const RESTART_WINDOW: Duration = Duration::from_secs(300);

/// Which managed server a relaunch budget belongs to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Server {
    Chat,
    Embed,
    Impersonation,
}

/// A crash-loop guard for relaunching a managed server: a dead child can only be
/// revived by launching a new process, and a server that dies *because* of its
/// configuration (a corrupt GGUF, an OOM) would otherwise be respawned forever.
///
/// The same shape as `McpManager::allow_restart` — deliberately a second small
/// implementation rather than a shared one: unifying them is a mechanical refactor
/// and shouldn't ride along with a behavior change (AGENTS.md §2).
#[derive(Default)]
struct RestartBudget {
    marks: Vec<Instant>,
}

impl RestartBudget {
    /// Whether one more relaunch is allowed now: prunes marks older than the window,
    /// records this attempt if there's room.
    fn allow(&mut self, now: Instant) -> bool {
        self.marks
            .retain(|t| now.duration_since(*t) < RESTART_WINDOW);
        if self.marks.len() < RESTART_BUDGET {
            self.marks.push(now);
            true
        } else {
            false
        }
    }

    /// A server that came up healthy again starts with a clean budget — otherwise a
    /// machine that goes down once a week would eventually exhaust it.
    fn reset(&mut self) {
        self.marks.clear();
    }
}

pub(super) struct EngineManager {
    /// The server supervisor (for restarting on a model/server change).
    supervisor: Arc<dyn ServerSupervisor>,
    /// The assistant's engine. `None` — external/not configured.
    pub(super) backend: Option<Arc<dyn EngineBackend>>,
    /// A handle to the managed chat process (drop → kill).
    chat_handle: Option<ServerHandle>,
    /// A handle to the managed embedding process.
    embed_handle: Option<ServerHandle>,
    /// The impersonation engine for managed/external modes (`None` in `shared` mode —
    /// then the assistant's `backend` is used). See spec §11.8.
    imp_backend: Option<Arc<dyn EngineBackend>>,
    /// A handle to the managed process of the impersonation server.
    imp_handle: Option<ServerHandle>,
    /// The current chat-server status. Generation only starts in `Ready`: a request to
    /// a still-loading (`Connecting`) managed server would return a 503 ("error
    /// status"), and for regeneration it would also wipe out the previous reply for nothing.
    pub(super) server_status: ServerStatus,
    /// Whether the slow-prefill note went out this chat-server session
    /// (docs/research/slow-prefill-detection.md §3.3): once per server, cleared
    /// when the server reaches `Ready` again — a relaunch measures afresh.
    prefill_noted: bool,
    /// Whether the window-too-small note went out this chat-server session
    /// (docs/research/ollama-window.md F3) — the same lifetime as the
    /// slow-prefill note's.
    window_noted: bool,
    /// Whether the cut-prompt note went out this chat-server session
    /// (docs/research/prompt-cut-detection.md F2) — the same lifetime again.
    cut_noted: bool,
    /// The impersonation-server status (for managed/external; in `shared` —
    /// `NotConfigured`, the chip in the status line is hidden).
    imp_status: ServerStatus,
    /// The embedding-server status: `NotConfigured` (`UnavailableEmbedder`, the chip is
    /// hidden) or, for a configured one, `Connecting` → `Ready`/`Disconnected` from the
    /// background probe (the cloud is `Ready` at once). Doesn't gate anything — RAG is
    /// lazy (ADR 0002), the status is informational.
    embed_status: ServerStatus,
    /// What each server was last **actually launched with**: its settings plus the key
    /// blob they were resolved from (`stored_key` reads the key inside `apply_*`, so
    /// both together decide what a relaunch would produce).
    ///
    /// [`Orchestrator::flush_restarts`] compares against this rather than against the
    /// previous edit: the debounce flag only says "something was edited", and an edit
    /// followed by its undo (`Ctrl+Z`, spec §11.6) raises it twice while leaving the
    /// config exactly as the server is already running. Recorded by `apply_*` itself,
    /// so a crash relaunch — which re-applies the same settings — keeps it accurate.
    ///
    /// `None` before the first apply, so the initial launch always happens.
    ///
    /// [`Orchestrator::flush_restarts`]: super::Orchestrator::flush_restarts
    applied_chat: Option<Applied<EngineSettings>>,
    applied_embed: Option<Applied<EmbedSettings>>,
    applied_imp: Option<Applied<ImpersonationEngineSettings>>,
    /// The chat-server status channel for the supervisor's background probe.
    status_tx: UnboundedSender<ServerStatus>,
    /// The impersonation-server status channel (background probe).
    imp_status_tx: UnboundedSender<ServerStatus>,
    /// The embedding-server status channel (background probe).
    embed_status_tx: UnboundedSender<ServerStatus>,
    /// The invalidation token for the current chat server's background probe: a mode/model
    /// change marks the previous probe stale, so its late result (e.g. a timeout of an
    /// intermediate external server while flipping through managed→external→openai) doesn't
    /// overwrite the new server's status.
    chat_probe_cancel: Option<CancellationToken>,
    /// The invalidation token for the impersonation server's background probe (analogous).
    imp_probe_cancel: Option<CancellationToken>,
    /// The invalidation token for the embedding server's background probe (analogous).
    embed_probe_cancel: Option<CancellationToken>,
    /// Relaunch budgets for the managed servers (chat/embed/impersonation).
    restarts: [RestartBudget; 3],
    /// Which slots' last apply was refused before anything started — a port a
    /// stranger holds, a model file that is not there: no process to revive, and
    /// a retry refuses the same way until the settings change, which applies the
    /// slot afresh. Cleared by a monitor's report, which only a started server has.
    refused: [bool; 3],
    /// The embeddings source for RAG (a dedicated server — ADR 0002).
    pub(super) embedder: Arc<dyn Embedder>,
}

impl EngineManager {
    /// Creates a manager with no servers raised (statuses `NotConfigured`, the embedder —
    /// [`UnavailableEmbedder`]). Servers are raised by the subsequent `apply_*` calls.
    pub(super) fn new(
        supervisor: Arc<dyn ServerSupervisor>,
        status_tx: UnboundedSender<ServerStatus>,
        imp_status_tx: UnboundedSender<ServerStatus>,
        embed_status_tx: UnboundedSender<ServerStatus>,
    ) -> Self {
        Self {
            supervisor,
            backend: None,
            chat_handle: None,
            embed_handle: None,
            applied_chat: None,
            applied_embed: None,
            applied_imp: None,
            imp_backend: None,
            imp_handle: None,
            server_status: ServerStatus::NotConfigured,
            prefill_noted: false,
            window_noted: false,
            cut_noted: false,
            imp_status: ServerStatus::NotConfigured,
            embed_status: ServerStatus::NotConfigured,
            status_tx,
            imp_status_tx,
            embed_status_tx,
            chat_probe_cancel: None,
            imp_probe_cancel: None,
            embed_probe_cancel: None,
            restarts: Default::default(),
            refused: [false; 3],
            embedder: Arc::new(crate::shared::api::UnavailableEmbedder),
        }
    }

    /// (Re-)raises the chat server from settings: kills the previous managed process,
    /// asks the supervisor to set up a new one, stores the immediate status. The caller
    /// takes the status snapshot for the UI via [`Self::statuses`]. `api_keys` —
    /// stored keys from the config (see [`stored_key`]). `gateway` — what is
    /// provider-wide about OpenRouter, told to the supervisor before it builds
    /// anything ([`ServerSupervisor::set_gateway`]).
    pub(super) fn apply_chat(
        &mut self,
        settings: &EngineSettings,
        api_keys: &[ApiKeyEntry],
        gateway: &OpenRouterSettings,
        loc: &'static Locale,
    ) {
        self.supervisor.set_gateway(gateway);
        self.chat_handle = None; // drop the old managed process (kill_on_drop)
        // Invalidate the previous server's probe and raise a new token.
        if let Some(tok) = self.chat_probe_cancel.take() {
            tok.cancel();
        }
        let cancel = CancellationToken::new();
        self.chat_probe_cancel = Some(cancel.clone());
        let key = stored_key(api_keys, settings.secret_key());
        let setup = self.supervisor.apply_chat(
            settings,
            key.as_deref(),
            cancel,
            self.status_tx.clone(),
            loc,
        );
        self.backend = setup.backend;
        self.chat_handle = setup.handle;
        self.refused[Server::Chat as usize] = matches!(setup.status, ServerStatus::Disconnected(_));
        self.server_status = setup.status;
        self.applied_chat = Some((
            settings.clone(),
            api_keys.to_vec(),
            gateway_for(settings.mode.cloud_provider(), gateway),
        ));
    }

    /// (Re-)raises the embedding server from settings. Like [`Self::apply_chat`]: the
    /// previous managed process is dropped, the previous probe is invalidated, and the
    /// immediate status is stored (real readiness arrives via `embed_status_tx`).
    ///
    /// `dress` is what the application puts around the embedder the supervisor
    /// built — the input convention and the model-change guard
    /// (`Orchestrator::embedder_dress`). It is an argument, not a step a caller
    /// takes afterwards, because it used to be one: start-up took it, a
    /// settings edit and a relaunch did not, and from the first change of the
    /// embedding settings to the restart the application ran on the bare
    /// client — no `query:`/`passage:` markers, and no check that the stored
    /// vectors belong to the model now answering, at the one moment that check
    /// exists for (docs/research/openrouter-mode.md §9, A1).
    pub(super) fn apply_embed(
        &mut self,
        settings: &EmbedSettings,
        api_keys: &[ApiKeyEntry],
        gateway: &OpenRouterSettings,
        loc: &'static Locale,
        dress: EmbedderDress,
    ) {
        self.supervisor.set_gateway(gateway);
        self.embed_handle = None; // drop the old managed process (kill_on_drop)
        if let Some(tok) = self.embed_probe_cancel.take() {
            tok.cancel();
        }
        let cancel = CancellationToken::new();
        self.embed_probe_cancel = Some(cancel.clone());
        let key = stored_key(api_keys, settings.secret_key());
        let setup = self.supervisor.apply_embed(
            settings,
            key.as_deref(),
            cancel,
            self.embed_status_tx.clone(),
            loc,
        );
        self.embedder = dress(setup.embedder);
        self.embed_handle = setup.handle;
        self.refused[Server::Embed as usize] =
            matches!(setup.status, ServerStatus::Disconnected(_));
        self.embed_status = setup.status;
        self.applied_embed = Some((
            settings.clone(),
            api_keys.to_vec(),
            gateway_for(settings.mode.cloud_provider(), gateway),
        ));
    }

    /// (Re-)raises the impersonation server. In `shared` mode a separate server isn't
    /// needed — the assistant's chat server is reused.
    pub(super) fn apply_impersonation(
        &mut self,
        settings: &ImpersonationEngineSettings,
        api_keys: &[ApiKeyEntry],
        gateway: &OpenRouterSettings,
        loc: &'static Locale,
    ) {
        self.supervisor.set_gateway(gateway);
        self.imp_handle = None; // drop the previous managed process (kill_on_drop)
        // Invalidate the previous impersonation server's probe (as with the chat server).
        if let Some(tok) = self.imp_probe_cancel.take() {
            tok.cancel();
        }
        match settings.mode {
            ImpersonationMode::Shared => {
                self.imp_backend = None;
                self.refused[Server::Impersonation as usize] = false;
                self.imp_status = ServerStatus::NotConfigured;
            }
            _ => {
                let cancel = CancellationToken::new();
                self.imp_probe_cancel = Some(cancel.clone());
                let key = stored_key(api_keys, settings.secret_key());
                let setup = self.supervisor.apply_impersonation(
                    settings,
                    key.as_deref(),
                    cancel,
                    self.imp_status_tx.clone(),
                    loc,
                );
                self.imp_backend = setup.backend;
                self.imp_handle = setup.handle;
                self.refused[Server::Impersonation as usize] =
                    matches!(setup.status, ServerStatus::Disconnected(_));
                self.imp_status = setup.status;
            }
        }
        // Recorded for both arms: `shared` mode is a state the server can be *in*, so
        // switching away from it and back must not read as "nothing to do".
        self.applied_imp = Some((
            settings.clone(),
            api_keys.to_vec(),
            gateway_for(settings.mode.cloud_provider(), gateway),
        ));
    }

    /// Whether a server is already running exactly this configuration — i.e. whether
    /// re-applying it would change anything. `false` before the first apply.
    ///
    /// The comparison covers the key blob as well as the settings: the key is resolved
    /// inside `apply_*`, so equal settings with a different blob still warrant a
    /// relaunch. Comparing the stored ciphertext (never the plaintext) is deliberate —
    /// it errs towards restarting, which is the safe direction.
    ///
    /// The gateway's provider-wide settings count only for a slot that speaks to
    /// the gateway ([`gateway_for`]).
    pub(super) fn chat_is_current(
        &self,
        s: &EngineSettings,
        keys: &[ApiKeyEntry],
        gateway: &OpenRouterSettings,
    ) -> bool {
        let wanted = gateway_for(s.mode.cloud_provider(), gateway);
        self.applied_chat
            .as_ref()
            .is_some_and(|(a, k, g)| a == s && k == keys && *g == wanted)
    }

    pub(super) fn embed_is_current(
        &self,
        s: &EmbedSettings,
        keys: &[ApiKeyEntry],
        gateway: &OpenRouterSettings,
    ) -> bool {
        let wanted = gateway_for(s.mode.cloud_provider(), gateway);
        self.applied_embed
            .as_ref()
            .is_some_and(|(a, k, g)| a == s && k == keys && *g == wanted)
    }

    pub(super) fn impersonation_is_current(
        &self,
        s: &ImpersonationEngineSettings,
        keys: &[ApiKeyEntry],
        gateway: &OpenRouterSettings,
    ) -> bool {
        let wanted = gateway_for(s.mode.cloud_provider(), gateway);
        self.applied_imp
            .as_ref()
            .is_some_and(|(a, k, g)| a == s && k == keys && *g == wanted)
    }

    /// Looks for a local Ollama or LM Studio, through the supervisor — the seam
    /// a test answers instead of the machine (docs/research/local-servers.md).
    pub(super) fn find_local_servers(
        &self,
    ) -> futures_util::future::BoxFuture<'static, Vec<crate::shared::api::local_servers::Found>>
    {
        self.supervisor.find_local_servers()
    }

    /// Updates the chat-server status (from the background monitor).
    pub(super) fn set_chat_status(&mut self, status: ServerStatus) {
        self.note_recovery(Server::Chat, &status);
        self.server_status = status;
    }

    /// Updates the impersonation-server status (from the background monitor).
    pub(super) fn set_imp_status(&mut self, status: ServerStatus) {
        self.note_recovery(Server::Impersonation, &status);
        self.imp_status = status;
    }

    /// Updates the embedding-server status (from the background monitor).
    pub(super) fn set_embed_status(&mut self, status: ServerStatus) {
        self.note_recovery(Server::Embed, &status);
        self.embed_status = status;
    }

    /// A server that reached `Ready` gets a clean relaunch budget: the budget exists
    /// to stop a crash *loop*, not to count a machine's lifetime outages. Any report
    /// at all comes from a monitor, and only a started server has one.
    fn note_recovery(&mut self, server: Server, status: &ServerStatus) {
        self.refused[server as usize] = false;
        if matches!(status, ServerStatus::Ready) {
            self.restarts[server as usize].reset();
            if matches!(server, Server::Chat) {
                // A new server session: the slow-prefill note may go out once
                // more, with the new server's own figure.
                self.prefill_noted = false;
                self.window_noted = false;
                self.cut_noted = false;
            }
        }
    }

    /// Claims the chat-server session's one slow-prefill note: `true` the
    /// first time it is asked, `false` after (docs/research/slow-prefill-detection.md
    /// §3.3).
    pub(super) fn claim_prefill_note(&mut self) -> bool {
        !std::mem::replace(&mut self.prefill_noted, true)
    }

    /// Claims the chat-server session's one window-too-small note, as
    /// [`Self::claim_prefill_note`] does its own.
    pub(super) fn claim_window_note(&mut self) -> bool {
        !std::mem::replace(&mut self.window_noted, true)
    }

    /// Claims the chat-server session's one cut-prompt note, as
    /// [`Self::claim_prefill_note`] does its own.
    pub(super) fn claim_cut_note(&mut self) -> bool {
        !std::mem::replace(&mut self.cut_noted, true)
    }

    /// Whether `server`'s last apply was refused before anything started (see
    /// [`Self::refused`]) — nothing for a relaunch to revive.
    pub(super) fn launch_refused(&self, server: Server) -> bool {
        self.refused[server as usize]
    }

    /// Whether a dead managed `server` may be relaunched right now (crash-loop guard).
    pub(super) fn allow_relaunch(&mut self, server: Server, now: Instant) -> bool {
        self.restarts[server as usize].allow(now)
    }

    /// The last published status of `server` — for deciding whether it needs reviving.
    pub(super) fn status_of(&self, server: Server) -> &ServerStatus {
        match server {
            Server::Chat => &self.server_status,
            Server::Embed => &self.embed_status,
            Server::Impersonation => &self.imp_status,
        }
    }

    /// A snapshot of all server statuses for the status bar (chat always, embeddings/
    /// impersonation — as chips hidden when `NotConfigured`). See spec §11.1.
    pub(super) fn statuses(&self) -> ServerStatuses {
        ServerStatuses {
            chat: self.server_status.clone(),
            embed: self.embed_status.clone(),
            impersonation: self.imp_status.clone(),
        }
    }

    /// Returns the assistant's engine if the chat server is ready (`Ready`); otherwise — `Err`
    /// with a clear message (not configured / still connecting / unavailable), localized in
    /// the interface language (`loc`, axis B — this is shown to the human, not the model).
    /// Emits nothing itself — the caller decides where to route the error. See spec §7.
    pub(super) fn backend_if_ready(
        &self,
        loc: &'static Locale,
    ) -> Result<Arc<dyn EngineBackend>, String> {
        match &self.server_status {
            ServerStatus::Ready => self
                .backend
                .clone()
                .ok_or_else(|| loc.t("ui.err.server.not_configured").to_string()),
            ServerStatus::Connecting => Err(loc.t("ui.err.server.connecting").to_string()),
            ServerStatus::NotConfigured => Err(loc.t("ui.err.server.not_configured").to_string()),
            ServerStatus::Disconnected(reason) => {
                Err(loc.tf("ui.err.server.unavailable", &[("reason", reason)]))
            }
        }
    }

    /// Returns the impersonation engine if it's ready; otherwise — `Err` with a clear,
    /// localized message (axis B). In `shared` mode the assistant's chat server is used.
    pub(super) fn impersonation_backend_if_ready(
        &self,
        mode: ImpersonationMode,
        loc: &'static Locale,
    ) -> Result<Arc<dyn EngineBackend>, String> {
        if mode == ImpersonationMode::Shared {
            return self.backend_if_ready(loc);
        }
        match &self.imp_status {
            ServerStatus::Ready => self
                .imp_backend
                .clone()
                .ok_or_else(|| loc.t("ui.err.server.imp_not_configured").to_string()),
            ServerStatus::Connecting => Err(loc.t("ui.err.server.imp_connecting").to_string()),
            ServerStatus::NotConfigured => {
                Err(loc.t("ui.err.server.imp_not_configured").to_string())
            }
            ServerStatus::Disconnected(reason) => {
                Err(loc.tf("ui.err.server.imp_unavailable", &[("reason", reason)]))
            }
        }
    }

    /// A clone of the embeddings source for RAG background tasks / the tool context.
    pub(super) fn embedder(&self) -> Arc<dyn Embedder> {
        self.embedder.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The guard's whole job: allow a few relaunches, then stop. Without the cap, a
    /// server that dies *because* of its configuration would be respawned forever.
    #[test]
    fn budget_allows_up_to_the_cap_then_refuses() {
        let mut b = RestartBudget::default();
        let now = Instant::now();
        for i in 0..RESTART_BUDGET {
            assert!(b.allow(now), "relaunch {i} should be allowed");
        }
        assert!(!b.allow(now), "the cap should stop the crash loop");
    }

    /// The cap is per window, not per lifetime: an outage a week later starts fresh.
    #[test]
    fn budget_forgets_marks_older_than_the_window() {
        let mut b = RestartBudget::default();
        let now = Instant::now();
        for _ in 0..RESTART_BUDGET {
            b.allow(now);
        }
        assert!(!b.allow(now));
        assert!(
            b.allow(now + RESTART_WINDOW + Duration::from_secs(1)),
            "marks older than the window should be pruned"
        );
    }

    /// Reaching `Ready` clears the budget — otherwise a machine that reboots once a
    /// month would eventually exhaust it and stop recovering.
    #[test]
    fn reaching_ready_clears_the_budget() {
        let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
        let (tx2, _rx2) = tokio::sync::mpsc::unbounded_channel();
        let (tx3, _rx3) = tokio::sync::mpsc::unbounded_channel();
        let mut m = EngineManager::new(
            Arc::new(crate::app::supervisor::MockSupervisor::with_backend(None)),
            tx,
            tx2,
            tx3,
        );
        let now = Instant::now();
        for _ in 0..RESTART_BUDGET {
            assert!(m.allow_relaunch(Server::Chat, now));
        }
        assert!(!m.allow_relaunch(Server::Chat, now));
        m.set_chat_status(ServerStatus::Ready);
        assert!(
            m.allow_relaunch(Server::Chat, now),
            "a recovered server should get a clean budget"
        );
    }

    /// Budgets are per server: a flapping embedding server mustn't spend the chat
    /// server's allowance.
    #[test]
    fn budgets_are_independent_per_server() {
        let mut m = EngineManager::new(
            Arc::new(crate::app::supervisor::MockSupervisor::with_backend(None)),
            tokio::sync::mpsc::unbounded_channel().0,
            tokio::sync::mpsc::unbounded_channel().0,
            tokio::sync::mpsc::unbounded_channel().0,
        );
        let now = Instant::now();
        for _ in 0..RESTART_BUDGET {
            m.allow_relaunch(Server::Embed, now);
        }
        assert!(!m.allow_relaunch(Server::Embed, now));
        assert!(m.allow_relaunch(Server::Chat, now));
    }
}