synaps-engine 0.10.1

Runtime engine — streaming, tools, MCP, skills, extensions, sidecar
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
//! Process-global engine host: the things exactly one of which should exist
//! per process regardless of how many sessions the process serves.
//!
//! `engine::setup::boot` used to construct all of this inside the session's
//! `Runtime`. It now builds (or reuses) the [`EngineHost`] and borrows the
//! process-global parts from it; subagent spawns borrow the same parts
//! instead of rebuilding an HTTP client + registry + token cache per spawn.

use std::sync::{Arc, OnceLock};
use tokio::sync::RwLock;

use crate::extensions::hooks::HookBus;
use crate::runtime::RuntimeParts;
use crate::session::{SessionActor, SessionConfig, SessionHandle, SessionId, SessionMeta};
use crate::tools::catalog::CatalogGeneration;
use crate::{Result, Runtime, ToolRegistry};

/// Everything a `Runtime` borrows from its host rather than constructing.
/// Cheap to clone (all `Arc`/handle types).
#[derive(Clone)]
pub struct HostParts {
    pub client: reqwest::Client,
    /// THE shared catalog.
    pub tools: Arc<RwLock<ToolRegistry>>,
    pub hook_bus: Arc<HookBus>,
    /// Resolved once from config.
    pub credential_source: crate::auth::CredentialSource,
    /// ONE cache per process.
    pub token_cache: crate::auth::TokenCache,
    pub mcp_runtime: Option<Arc<crate::mcp::McpRuntimeManager>>,
    pub extension_runtime: Option<Arc<crate::extensions::lease::ExtensionRuntimeManager>>,
    pub capture_dir: std::path::PathBuf,
    pub progressive_tool_disclosure: bool,
}

pub struct HostOpts {
    /// Applied via `config::set_profile` by the FIRST boot in a process
    /// only; a later `boot()` with a different profile is not honoured
    /// (the installed host wins — see [`EngineHost::boot_and_install`]).
    pub profile: Option<String>,
    /// Echoed to callers (`EngineBoot::no_extensions`) — extension discovery
    /// is gated by the renderer after boot, not by the host. Not read here.
    pub no_extensions: bool,
}

pub struct EngineHost {
    parts: HostParts,
    config: arc_swap::ArcSwap<crate::SynapsConfig>,
    command_registry: Arc<crate::skills::registry::CommandRegistry>,
    keybind_registry: Arc<std::sync::RwLock<crate::skills::keybinds::KeybindRegistry>>,
    ext_manager: Arc<RwLock<crate::extensions::manager::ExtensionManager>>,
    mcp_server_count: usize,
    /// Worker (subagent) registry template, cached by shared-catalog
    /// generation. `worker_runtime()` clones this instead of rebuilding
    /// `without_subagent_with_extensions` + `rebuild_schema()` per spawn.
    worker_registry: std::sync::Mutex<Option<(CatalogGeneration, ToolRegistry)>>,
    /// File-appender flush guard. Lives for the process (≥ any renderer), so
    /// log lines emitted after `boot()` returns can never be dropped by an
    /// early guard drop (the old `setup::boot` bug). Rust runs no static
    /// destructors, so it is released explicitly by [`Self::flush_logs`] at
    /// process exit — otherwise the tail of the log dies with the writer
    /// thread.
    log_guard: std::sync::Mutex<Option<tracing_appender::non_blocking::WorkerGuard>>,
    /// Live sessions (Phase 2): id → handle. The map holds one handle per
    /// actor so the actor outlives any single client; the actor task removes
    /// itself here when it finishes.
    sessions: std::sync::Mutex<std::collections::HashMap<SessionId, SessionHandle>>,
    /// Serialises `create_session`'s resolve-then-insert so two concurrent
    /// `--continue X` cannot both miss the live check and build two actors
    /// on one session id.
    create_lock: tokio::sync::Mutex<()>,
    /// (E-P2) Single-tenancy driver lock: at most one plugin can drive one
    /// session daemon-wide. Grants do NOT survive reload (§3 S2/S5).
    driver_grants: std::sync::Mutex<std::collections::HashMap<String, SessionId>>,
    /// C2: flips to `true` when extension discovery on `ext_manager` has
    /// finished (the loader sets it; `extensions_ready()` awaits it). The
    /// paired flag records that a loader was DISPATCHED, so a session
    /// created between `spawn_discover_and_load` and its first lock
    /// acquisition still waits instead of racing the walk.
    extensions_ready: tokio::sync::watch::Sender<bool>,
    extensions_loading: std::sync::atomic::AtomicBool,
}

/// (E-P2) Returned by [`EngineHost::claim_driver`] when another live session
/// already holds the driver grant for a plugin.
#[derive(Debug)]
#[allow(dead_code)] // fields read by Display impl + P3 handler
pub struct DriverGrantError {
    pub plugin: String,
    pub held_by: SessionId,
}

impl std::fmt::Display for DriverGrantError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "plugin '{}' already driving session {}",
            self.plugin, self.held_by
        )
    }
}

static HOST: OnceLock<Arc<EngineHost>> = OnceLock::new();

/// See [`EngineHost::extensions_loading_guard`].
pub struct ExtensionsLoadingGuard {
    host: Arc<EngineHost>,
}

impl Drop for ExtensionsLoadingGuard {
    fn drop(&mut self) {
        self.host.mark_extensions_ready();
    }
}

impl EngineHost {
    /// Every step of the old `setup::boot()` that does NOT mention a session:
    /// profile, logging, HTTP client, registry, config, skills, MCP, extension
    /// manager. Touches NO process static: the OpenAI routing static is
    /// written by [`Self::install`] for the winning host only, and the global
    /// broker is installed in [`Self::foreground_runtime`] via `apply_config`,
    /// exactly where the old boot did it relative to session resolution.
    pub async fn boot(opts: HostOpts) -> Result<Arc<Self>> {
        if let Some(ref prof) = opts.profile {
            crate::config::set_profile(Some(prof.clone()));
        }
        let log_guard = crate::logging::init_logging();

        let client = crate::runtime::build_host_http_client()?;
        let capture_dir = crate::runtime::trace::default_capture_dir();
        let _ = crate::runtime::trace::sweep_expired_captures(&capture_dir);

        let config = crate::config::load_config();
        let tools = Arc::new(RwLock::new(ToolRegistry::new()));
        // Same point the old boot's `apply_config` disabled tools: on the
        // fresh registry, before skills/MCP register anything.
        if !config.disabled_tools.is_empty() {
            tools.write().await.disable(&config.disabled_tools);
        }

        // Discover plugins/skills, build command registry, register load_skill tool.
        let (command_registry, keybind_registry) = crate::skills::register(&tools, &config).await;

        // MCP loading (if configured). Flag-off keeps the legacy connect
        // gateway; progressive disclosure switches to exact descriptor-backed
        // dormant tools with no gateway.
        let mcp_server_count =
            crate::mcp::setup_lazy_mcp(&tools, config.progressive_tool_disclosure).await;
        let mcp_runtime = if config.progressive_tool_disclosure {
            Some(Arc::new(crate::mcp::McpRuntimeManager::new(
                crate::mcp::lease::config_source_from_disk(),
                crate::mcp::lease::DEFAULT_IDLE_MAX,
            )))
        } else {
            None
        };

        let hook_bus = Arc::new(HookBus::new());
        let mut ext_mgr = crate::extensions::manager::ExtensionManager::new_with_tools(
            Arc::clone(&hook_bus),
            Arc::clone(&tools),
        );
        // Per-process: tell the extension manager whether memory is exclusive
        // (Axel) so extensions spawned later inherit the right policy.
        let mem_binding = crate::memory_backend::MemoryBinding::from_config(&config.memory_backend);
        ext_mgr.bind_memory_backend(mem_binding.exclusive());
        ext_mgr.set_progressive_deferral(config.progressive_tool_disclosure);
        let extension_runtime = if config.progressive_tool_disclosure {
            Some(ext_mgr.extension_runtime())
        } else {
            None
        };
        let ext_manager = Arc::new(RwLock::new(ext_mgr));

        let parts = HostParts {
            client,
            tools,
            hook_bus,
            credential_source: config.auth.credential_source(),
            token_cache: crate::auth::TokenCache::new(),
            mcp_runtime,
            extension_runtime,
            capture_dir,
            progressive_tool_disclosure: config.progressive_tool_disclosure,
        };

        Ok(Arc::new(Self {
            parts,
            config: arc_swap::ArcSwap::from_pointee(config),
            command_registry,
            keybind_registry,
            ext_manager,
            mcp_server_count,
            worker_registry: std::sync::Mutex::new(None),
            log_guard: std::sync::Mutex::new(log_guard),
            sessions: std::sync::Mutex::new(std::collections::HashMap::new()),
            create_lock: tokio::sync::Mutex::new(()),
            driver_grants: std::sync::Mutex::new(std::collections::HashMap::new()),
            extensions_ready: tokio::sync::watch::channel(false).0,
            extensions_loading: std::sync::atomic::AtomicBool::new(false),
        }))
    }

    /// Idempotent, atomic install of the process host. Exactly one host ever
    /// wins the `OnceLock`; the winner's extension manager becomes the
    /// OpenAI routing static in the same step. A different host arriving
    /// later (or losing a race) is returned as `Err(rejected)` and never
    /// replaces the installed one — nor touches any static.
    pub fn install(host: Arc<Self>) -> std::result::Result<(), Arc<Self>> {
        let installed = HOST.get_or_init(|| {
            crate::runtime::openai::set_extension_manager_for_routing(Arc::clone(
                &host.ext_manager,
            ));
            Arc::clone(&host)
        });
        if Arc::ptr_eq(installed, &host) {
            Ok(())
        } else {
            Err(host)
        }
    }

    /// The process host: the installed one, else boot + install. Under a
    /// race the loser is discarded and the installed host returned, so a
    /// caller never ends up on a host that `current()` does not agree with.
    pub async fn boot_and_install(opts: HostOpts) -> Result<Arc<Self>> {
        if let Some(h) = HOST.get() {
            return Ok(Arc::clone(h));
        }
        let h = Self::boot(opts).await?;
        Ok(match Self::install(h) {
            Ok(()) => HOST.get().cloned().expect("just installed"),
            Err(_rejected) => HOST.get().cloned().expect("a winner exists"),
        })
    }

    pub fn current() -> Option<Arc<Self>> {
        HOST.get().cloned()
    }

    /// Flush the non-blocking log writer: drops the appender guard, which
    /// drains the channel and joins the writer thread (≤ 1 s). Idempotent.
    /// Lines logged AFTER this call are dropped, so it belongs at the very
    /// last exit point (`main` return, panic hook), not at session shutdown.
    pub fn flush_logs(&self) {
        let guard = self.log_guard.lock().unwrap_or_else(|e| e.into_inner()).take();
        drop(guard);
    }

    /// [`Self::flush_logs`] on the installed host, if any.
    pub fn flush_installed_logs() {
        if let Some(h) = HOST.get() {
            h.flush_logs();
        }
    }

    pub fn parts(&self) -> &HostParts {
        &self.parts
    }

    pub fn config(&self) -> arc_swap::Guard<Arc<crate::SynapsConfig>> {
        self.config.load()
    }

    pub fn ext_manager(&self) -> &Arc<RwLock<crate::extensions::manager::ExtensionManager>> {
        &self.ext_manager
    }

    pub fn command_registry(&self) -> &Arc<crate::skills::registry::CommandRegistry> {
        &self.command_registry
    }

    pub fn keybind_registry(
        &self,
    ) -> &Arc<std::sync::RwLock<crate::skills::keybinds::KeybindRegistry>> {
        &self.keybind_registry
    }

    pub fn mcp_server_count(&self) -> usize {
        self.mcp_server_count
    }

    /// The foreground runtime for an interactive session: shares client,
    /// tools, hook_bus, credential source, token cache, MCP/extension lease
    /// managers. Everything else is fresh (exactly `Runtime::new()`'s values).
    /// `apply_config` runs here — the ONLY place the broker is (re)installed.
    /// `disabled_tools` is NOT re-applied: `boot()` did it once on the fresh
    /// registry, before skills/MCP registered, exactly where the old boot did.
    pub async fn foreground_runtime(&self) -> Result<Runtime> {
        self.foreground_runtime_for(None).await
    }

    /// `foreground_runtime()` for a session whose cwd is not the process cwd
    /// (daemon-hosted sessions). The cwd is set BEFORE `apply_config` so the
    /// memory binding (`apply_memory_backend_config`, one-shot) scopes to the
    /// session's project, not the daemon's — A-engine-merge §4. `None` =
    /// process cwd, byte-identical to `foreground_runtime()`.
    pub async fn foreground_runtime_for(&self, cwd: Option<std::path::PathBuf>) -> Result<Runtime> {
        let mut runtime = Runtime::from_parts(RuntimeParts::with_reaper(self.parts.clone()));
        runtime.set_cwd(cwd);
        runtime.apply_config_keep_tools(&self.config());
        Ok(runtime)
    }

    /// A worker (subagent) runtime. Shares credential source and token cache
    /// (so NO broker re-install and NO cache eviction) and takes a CLONE of
    /// the cached worker registry template. Does NOT share: the HTTP client
    /// (fresh — see below), hook_bus (fresh, empty — hooks do not fire for
    /// subagent tool calls), session_manager (fresh + reaper),
    /// mcp_runtime/extension_runtime (`None`, as `Runtime::new()`).
    ///
    /// Own client: workers `block_on` a throwaway `current_thread` runtime
    /// and hyper parks each connection's I/O driver on the runtime that
    /// opened it. A pooled connection born on a worker would be handed to
    /// the foreground (LIFO checkout) and die mid-stream when that worker's
    /// runtime dropped. A per-worker pool dies with its runtime, as before.
    pub async fn worker_runtime(&self) -> Result<Runtime> {
        let mut host = self.parts.clone();
        host.client = crate::runtime::build_host_http_client()?;
        host.hook_bus = Arc::new(HookBus::new());
        host.tools = Arc::new(RwLock::new(self.worker_registry().await));
        host.mcp_runtime = None;
        host.extension_runtime = None;
        // `Runtime::new()` value. Workers never ran `apply_config`, so they
        // never had disclosure on; their registry has no discovery/activation
        // tools, so a projected schema would silently drop every extension
        // tool.
        host.progressive_tool_disclosure = false;
        Ok(Runtime::from_parts(RuntimeParts::with_reaper(host)))
    }

    /// Cached-or-rebuilt worker registry: `without_subagent_with_extensions`
    /// is a pure function of the shared registry's tool set, which is a pure
    /// function of its catalog generation, so caching by generation returns
    /// exactly what a rebuild would.
    pub async fn worker_registry(&self) -> ToolRegistry {
        let shared = self.parts.tools.read().await;
        let gen = shared.catalog().generation();
        if let Some((cached_gen, tpl)) = &*self.worker_registry.lock().unwrap() {
            if *cached_gen == gen {
                return tpl.clone();
            }
        }
        let mut fresh = ToolRegistry::without_subagent_with_extensions(&shared);
        drop(shared);
        // #112: apply after the merge too — a shared registry must not
        // reintroduce a forum tool the operator disabled. Other worker policy
        // is preserved.
        let disabled_forum: Vec<String> = self
            .config()
            .disabled_tools
            .iter()
            .filter(|name| matches!(name.as_str(), "forum_post" | "forum_read" | "forum_forget"))
            .cloned()
            .collect();
        if !disabled_forum.is_empty() {
            fresh.disable(&disabled_forum);
        }
        *self.worker_registry.lock().unwrap() = Some((gen, fresh.clone()));
        fresh
    }

    /// Re-read config from disk into the host's snapshot. Phase 1 callers
    /// keep `load_config()` where they use it today.
    pub fn reload_config(&self) {
        self.config
            .store(Arc::new(crate::config::load_config()));
    }

    // ── sessions (Phase 2) ────────────────────────────────────────────────

    /// Create a session on this host: builds the `SessionActor` (the one
    /// `Runtime` + `ConversationState`), spawns its task, registers the
    /// handle. The task removes itself from the map when it ends.
    ///
    /// **Attach-if-live:** when `cfg.continue_session` resolves to a session
    /// that is already running on this host (`--continue X` twice,
    /// `Attach::Create` twice, `--continue` latest while latest is live),
    /// the existing handle is returned and NO second actor is built. Two
    /// actors on one id would share the session file, and the second's
    /// `spawn_session_background` would unlink the first's per-session UDS
    /// and overwrite its registry entry. `cfg`'s other fields (cwd, model
    /// override, …) are ignored in that case — the live session wins.
    pub async fn create_session(self: &Arc<Self>, cfg: SessionConfig) -> Result<SessionHandle> {
        let _serial = self.create_lock.lock().await;
        if let Some(live) = self.live_continue_target(&cfg.continue_session) {
            tracing::info!(session = %live.id, "create_session: target is live — attaching");
            return Ok(live);
        }
        let (handle, task) = SessionActor::create(self, cfg).await?;
        let id = handle.id.clone();
        self.adopt_session(handle.clone());
        let host = Arc::clone(self);
        tokio::spawn(async move {
            // A panicking actor must still leave the map (§6 #12): the
            // handle would otherwise be listed forever and attach → Closed.
            use futures::FutureExt;
            if std::panic::AssertUnwindSafe(task.run())
                .catch_unwind()
                .await
                .is_err()
            {
                tracing::error!(session = %id, "session actor panicked");
            }
            host.remove_session(&id);
        });
        Ok(handle)
    }

    /// Register a handle built elsewhere (daemon test factories). The
    /// caller owns the task; `remove_session` when it ends.
    pub fn adopt_session(&self, handle: SessionHandle) {
        self.sessions
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .insert(handle.id.clone(), handle);
    }

    /// Every registered handle (B4: THE session map; `DaemonState`
    /// delegates here). Dead handles are dropped defensively: the actor
    /// task drops `cmd_rx` when `run()` returns and `remove_session` runs
    /// *after* — a listing in that window sees `is_alive() == false`. That
    /// is an ordering window, not an invariant violation, so it is logged
    /// at debug, never asserted.
    pub fn session_handles(&self) -> Vec<SessionHandle> {
        let mut map = self.sessions.lock().unwrap_or_else(|e| e.into_inner());
        let before = map.len();
        map.retain(|_, h| h.is_alive());
        if before != map.len() {
            tracing::debug!(
                pruned = before - map.len(),
                "session_handles: pruned ended sessions ahead of their self-removal"
            );
        }
        map.values().cloned().collect()
    }

    /// Resolve a `continue_session` request against the LIVE sessions
    /// first — actor id, current journal id (a LinkedSuccessor compaction
    /// moves it off the actor id), or name (`--name` / `saveas`, which may
    /// not be on disk yet) — then the way `setup::resolve_or_create_session`
    /// will (chain → name → partial id; `Some(None)` = latest by mtime) and
    /// match the resolved id against id *and* journal id. Anything that is
    /// not running here returns `None` so the real boot path builds/reports.
    ///
    /// The daemon's `Attach::Create{continue}` goes through here too
    /// (`host_factory` → `create_session`): a live journal is always one
    /// actor, never a second one on the same file / UDS / registry entry.
    fn live_continue_target(
        &self,
        continue_session: &Option<Option<String>>,
    ) -> Option<SessionHandle> {
        let query = continue_session.as_ref()?;
        let handles: Vec<SessionHandle> = {
            let sessions = self
                .sessions
                .lock()
                .unwrap_or_else(|e| e.into_inner());
            sessions.values().filter(|h| h.is_alive()).cloned().collect()
        };
        if handles.is_empty() {
            return None;
        }
        let by_id = |id: &str| {
            handles
                .iter()
                .find(|h| h.id.as_str() == id || h.journal_id() == id)
                .cloned()
        };
        let id = match query {
            Some(q) => {
                if let Some(h) = by_id(q) {
                    return Some(h);
                }
                if let Some(h) = handles.iter().find(|h| h.name().as_deref() == Some(q.as_str())) {
                    return Some(h.clone());
                }
                // F24: resolve on disk and follow compacted_into so
                // `--continue <old>` finds the live successor.
                let session = crate::core::session::resolve_session(q).ok()?;
                let resolved = crate::core::session::follow_compaction_chain(session).ok()?;
                resolved.session.id
            }
            None => {
                let session = crate::core::session::latest_session().ok()?;
                let resolved = crate::core::session::follow_compaction_chain(session).ok()?;
                resolved.session.id
            }
        };
        by_id(&id)
    }

    /// Handle for a live session, if any.
    pub fn attach(&self, id: &SessionId) -> Option<SessionHandle> {
        self.sessions
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .get(id)
            .cloned()
    }

    /// Metadata of every live session, with the live cells filled
    /// (lifecycle, clients, input_owner, awaiting_input, journal_id).
    pub fn sessions(&self) -> Vec<SessionMeta> {
        self.session_handles()
            .iter()
            .map(|h| h.meta_live())
            .collect()
    }

    /// Drop the host's handle for a session (the actor keeps running until
    /// its command queue closes or it receives `End`).
    pub fn remove_session(&self, id: &SessionId) -> Option<SessionHandle> {
        let handle = self.sessions
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .remove(id);
        // E-P2: release any driver grants held by this session.
        if handle.is_some() {
            self.release_all_driver_grants(id);
        }
        handle
    }

    // ── driver single-tenancy (E-P2, §3 S2/S5) ──────────────────────────

    /// Claim the driver grant for `plugin` on behalf of `session`.
    /// Returns `Ok(())` if the grant was acquired (or is idempotent for the
    /// same session). Returns `Err` if another live session holds it.
    #[allow(dead_code)] // consumed by P3 driver_arm
    pub(crate) fn claim_driver(
        &self,
        plugin: &str,
        session: &SessionId,
    ) -> std::result::Result<(), DriverGrantError> {
        let mut grants = self.driver_grants.lock().unwrap_or_else(|e| e.into_inner());
        if let Some(existing) = grants.get(plugin) {
            if existing != session {
                return Err(DriverGrantError {
                    plugin: plugin.into(),
                    held_by: existing.clone(),
                });
            }
            return Ok(()); // idempotent
        }
        grants.insert(plugin.into(), session.clone());
        Ok(())
    }

    /// Release the driver grant for `plugin` if held by `session`.
    #[allow(dead_code)] // consumed by P3 driver_revoke
    pub(crate) fn release_driver(&self, plugin: &str, session: &SessionId) {
        let mut grants = self.driver_grants.lock().unwrap_or_else(|e| e.into_inner());
        if grants.get(plugin).is_some_and(|s| s == session) {
            grants.remove(plugin);
        }
    }

    /// Release all driver grants held by `session` (called from `remove_session`).
    fn release_all_driver_grants(&self, session: &SessionId) {
        let mut grants = self.driver_grants.lock().unwrap_or_else(|e| e.into_inner());
        grants.retain(|_, s| s != session);
    }

    /// Clear all driver grants (called on reload — §3 S2/S5: plugin process
    /// died with the old image).
    #[allow(dead_code)] // consumed by reload path in P3+
    pub(crate) fn clear_all_driver_grants(&self) {
        self.driver_grants
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .clear();
    }

    // ── shared extension host (Phase 2, C) ────────────────────────────────

    /// Loader seam (C2): a discovery pass for this host's manager has been
    /// dispatched. Called synchronously by `spawn_discover_and_load` before
    /// its task runs, so `extensions_ready()` cannot slip through the gap.
    pub fn note_extensions_loading(&self) {
        self.extensions_loading
            .store(true, std::sync::atomic::Ordering::Release);
    }

    /// Loader seam (C2): discovery finished — every `extensions_ready()`
    /// waiter proceeds. Idempotent.
    pub fn mark_extensions_ready(&self) {
        self.extensions_ready.send_replace(true);
    }

    /// Loader seam (C2): `note_extensions_loading()` now, and
    /// `mark_extensions_ready()` when the guard drops — however the loader
    /// task ends (return, cancel, panic). Hold it inside the task so a
    /// panicking walk cannot strand `extensions_ready()` waiters.
    pub fn extensions_loading_guard(self: &Arc<Self>) -> ExtensionsLoadingGuard {
        self.note_extensions_loading();
        ExtensionsLoadingGuard {
            host: Arc::clone(self),
        }
    }

    /// Resolve once extension discovery on this host is known-finished, so a
    /// session's `on_session_start` lands on subscribed extensions. Resolves
    /// IMMEDIATELY when no loader was dispatched (hosts that never load
    /// extensions, `--no-extensions`, tests) or discovery already completed
    /// on the manager (in-process `discover_and_load()` callers such as
    /// `synaps chat`); otherwise awaits `mark_extensions_ready()`.
    ///
    /// The watch sender is a field of the host, so "sender dropped" is NOT a
    /// wake-up path while the host lives. Liveness comes from
    /// [`ExtensionsLoadingGuard`] (the loader task marks ready on any exit,
    /// including panic); callers that must not trust the loader at all
    /// (`SessionActor::create`) additionally bound the await with
    /// `budgets::EXTENSIONS_READY_TIMEOUT`.
    pub async fn extensions_ready(&self) {
        let mut rx = self.extensions_ready.subscribe();
        if *rx.borrow() {
            return;
        }
        // A running walk holds the manager write lock; this read waits it out.
        if self.ext_manager.read().await.discovery_done().is_some() {
            return;
        }
        if !self
            .extensions_loading
            .load(std::sync::atomic::Ordering::Acquire)
        {
            return;
        }
        while !*rx.borrow_and_update() {
            if rx.changed().await.is_err() {
                return;
            }
        }
    }

    /// C3: push one extension notification frame to every live session
    /// (non-blocking; a session whose command queue is full drops it with a
    /// warning — widget upserts are idempotent last-writer-wins). Frames
    /// carry no session id today, hence broadcast. Returns how many
    /// sessions accepted it.
    pub async fn broadcast_extension_notification(
        &self,
        ext_id: &str,
        method: &str,
        params: serde_json::Value,
    ) -> usize {
        let handles: Vec<SessionHandle> = self
            .sessions
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .values()
            .cloned()
            .collect();
        let mut delivered = 0;
        for handle in handles {
            let cmd = crate::session::SessionCommand::HostEvent(
                crate::session::HostEvent::ExtensionNotification {
                    extension_id: ext_id.to_string(),
                    method: method.to_string(),
                    params: params.clone(),
                },
            );
            match handle.send(cmd).await {
                Ok(()) => delivered += 1,
                Err(err) => tracing::warn!(
                    session = %handle.id,
                    extension = %ext_id,
                    method = %method,
                    error = %err,
                    "dropping extension notification for session"
                ),
            }
        }
        delivered
    }
}

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

    /// Build a minimal EngineHost for unit testing driver grants.
    /// Only the driver_grants, sessions, and adjacent fields are initialized;
    /// nothing boots a runtime or loads extensions.
    fn test_host() -> Arc<EngineHost> {
        // Use the real boot path's struct literal so the test fails to compile
        // if a field is added and not initialised here — belt and suspenders.
        // We can't call boot() without full config, so we manufacture one.
        // Instead, reach into the same fields and just test the grant logic.
        Arc::new_cyclic(|_| {
            // Minimal construction: only the fields we touch. Others are default.
            // This is intentionally a helper, not the full EngineHost::boot path.
            use std::collections::HashMap;
            use std::sync::Mutex;

            // We cannot construct EngineHost without private fields, so we
            // test the grant logic via standalone functions that mirror the
            // Mutex<HashMap> pattern.
            unreachable!()
        })
    }

    // Since EngineHost has many private fields we cannot construct in a unit
    // test without the full boot path, test the grant logic on a standalone
    // map that mirrors the real implementation exactly.

    type Grants = std::sync::Mutex<std::collections::HashMap<String, SessionId>>;

    fn claim(grants: &Grants, plugin: &str, session: &SessionId) -> std::result::Result<(), DriverGrantError> {
        let mut g = grants.lock().unwrap();
        if let Some(existing) = g.get(plugin) {
            if existing != session {
                return Err(DriverGrantError {
                    plugin: plugin.into(),
                    held_by: existing.clone(),
                });
            }
            return Ok(());
        }
        g.insert(plugin.into(), session.clone());
        Ok(())
    }

    fn release(grants: &Grants, plugin: &str, session: &SessionId) {
        let mut g = grants.lock().unwrap();
        if g.get(plugin).is_some_and(|s| s == session) {
            g.remove(plugin);
        }
    }

    fn release_all(grants: &Grants, session: &SessionId) {
        grants.lock().unwrap().retain(|_, s| s != session);
    }

    fn clear_all(grants: &Grants) {
        grants.lock().unwrap().clear();
    }

    fn sid(s: &str) -> SessionId {
        SessionId::from(s)
    }

    #[test]
    fn single_tenancy_second_session_refused() {
        let grants: Grants = Default::default();
        let a = sid("session-a");
        let b = sid("session-b");
        claim(&grants, "autonomous", &a).unwrap();
        let err = claim(&grants, "autonomous", &b).unwrap_err();
        assert_eq!(err.plugin, "autonomous");
        assert_eq!(err.held_by, a);
    }

    #[test]
    fn single_tenancy_released_on_end() {
        let grants: Grants = Default::default();
        let a = sid("session-a");
        let b = sid("session-b");
        claim(&grants, "autonomous", &a).unwrap();
        release(&grants, "autonomous", &a);
        // Now session B can claim it.
        claim(&grants, "autonomous", &b).unwrap();
    }

    #[test]
    fn single_tenancy_same_session_idempotent() {
        let grants: Grants = Default::default();
        let a = sid("session-a");
        claim(&grants, "autonomous", &a).unwrap();
        // Same session, same plugin — idempotent.
        claim(&grants, "autonomous", &a).unwrap();
        assert_eq!(grants.lock().unwrap().len(), 1);
    }

    #[test]
    fn grants_cleared_on_reload_record() {
        let grants: Grants = Default::default();
        let a = sid("session-a");
        let b = sid("session-b");
        claim(&grants, "plugin1", &a).unwrap();
        claim(&grants, "plugin2", &b).unwrap();
        assert_eq!(grants.lock().unwrap().len(), 2);
        // Simulated reload: all grants cleared.
        clear_all(&grants);
        assert!(grants.lock().unwrap().is_empty());
        // Both can re-claim.
        claim(&grants, "plugin1", &a).unwrap();
        claim(&grants, "plugin2", &b).unwrap();
    }

    #[test]
    fn release_all_for_session_leaves_other_sessions() {
        let grants: Grants = Default::default();
        let a = sid("session-a");
        let b = sid("session-b");
        claim(&grants, "plugin1", &a).unwrap();
        claim(&grants, "plugin2", &b).unwrap();
        release_all(&grants, &a);
        assert_eq!(grants.lock().unwrap().len(), 1);
        assert!(grants.lock().unwrap().contains_key("plugin2"));
    }
}