oino-client 1.1.0

Oino client SDK: SessionBackend contract, RPC command records, in-process and fake backends
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
//! E-LAZY lazy session hub (core spec §2.2, WP-C7): a session is a record on disk until a
//! message arrives (infinity's "spawn driver on demand" as a core type).
//!
//! [`SessionHub`] lists session records with no services, engines or leases; `open`/`attach`
//! wake one (acquire lease → repair → `AgentSessionRuntime` → [`InProcessBackend`]); a live
//! session with no run, compaction, arbiter wait, open tools, live children or subscribers
//! for `extensionSettings.hub.sessionIdleSec` (default 900 s) is evicted — disposed
//! (`session_shutdown{reason:"idle"}`, lease released, guests drop their per-session state)
//! and returns to a record that re-opens and repairs on demand. A services set with zero
//! live sessions for `servicesIdleSec` (default 900 s) is disposed (extension `shutdown`);
//! at most `maxServices` (default 4) are kept, least-recently-used first — an idle set is
//! evicted (its idle sessions return to records) to make room, and when every set has a
//! busy session the hub rather exceeds the cap by one than refuses the open.
//!
//! Settings (`extensionSettings.hub.*`, §2.2): `sessionIdleSec`/`servicesIdleSec` `0`
//! disables eviction — the TUI's single-session runtime does not use the hub at all.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError, Weak};
use std::time::{Duration, Instant};

use futures::StreamExt;
use futures::future::BoxFuture;
use futures::stream::BoxStream;
use oino_ai::{JsonObject, JsonValue};
use oino_core::agents::AgentRunState;
use oino_core::events::{ClientEvent, OinoEvent};
use oino_core::extensions::api::ExtensionMode;
use oino_core::runtime::{AgentSessionRuntime, SessionOptions, SessionSource, ToolSelection};
use oino_core::services::{ExtensionLoader, ResourceFlags, Services, ServicesOptions};
use oino_core::session::SessionOpError;
use oino_core::ui::UiResponse;
use oino_models::ModelRegistry;
use oino_session::{SessionInfo, SessionManager, lock_metadata_path};
use oino_settings::SettingsManager;

use crate::backend::{ClientError, SessionBackend};
use crate::in_process::InProcessBackend;
use crate::protocol::{RpcCommand, RpcResponseData};

/// `extensionSettings.hub.sessionIdleSec` default (ruling, core spec §7).
pub const DEFAULT_SESSION_IDLE_SECS: u64 = 900;
/// `extensionSettings.hub.servicesIdleSec` default (core spec §2.2).
pub const DEFAULT_SERVICES_IDLE_SECS: u64 = 900;
/// `extensionSettings.hub.maxServices` default (core spec §2.2).
pub const DEFAULT_MAX_SERVICES: usize = 4;

/// Lower bound of the idle monitor's poll interval (tests use sub-second idles).
const MIN_POLL: Duration = Duration::from_millis(100);
/// Upper bound of the idle monitor's poll interval.
const MAX_POLL: Duration = Duration::from_secs(2);

fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    mutex.lock().unwrap_or_else(PoisonError::into_inner)
}

/// One session as the hub sees it (E-LAZY).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HubSession {
    /// The record (path, id, name, counts) — read from disk, never woken.
    pub info: SessionInfo,
    /// Live-ness.
    pub state: HubSessionState,
}

/// Live-ness of one hub session (E-LAZY): a record until a message arrives.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum HubSessionState {
    /// On disk only: no runtime, engine or lease in this process.
    #[default]
    Record,
    /// Open in this process (runtime + lease held).
    Live,
    /// A session task crashed and was contained (X-PANIC): listed `crashed` until
    /// re-opened from the record.
    Crashed,
    /// Another runner holds the lease and did not respond to the takeover request
    /// (D-TAKEOVER deadline): the last `open` failed with `SessionLocked`.
    Locked,
}

/// Inputs of [`SessionHub::create`].
pub struct HubOptions {
    /// Primary working directory (settings project file, default services cwd).
    pub cwd: PathBuf,
    /// Agent dir (`<agent>/sessions`, settings, state).
    pub agent_dir: PathBuf,
    /// Bundled docs root; defaults to `<agent_dir>/docs`.
    pub docs_root: Option<PathBuf>,
    /// Sessions root override (`--session-dir`); the default chain mirrors
    /// [`oino_core::services::Services::sessions_root`].
    pub session_dir: Option<PathBuf>,
    /// Run mode (lease holder label, extension contexts).
    pub mode: ExtensionMode,
    /// Dialog UI available.
    pub has_ui: bool,
    /// Trust prompt override (`-a`/`-na`); `None` decides from the trust store.
    pub approve: Option<bool>,
    /// CLI resource flags.
    pub flags: ResourceFlags,
    /// Extension loader (CLI discovery today, core `sources` after WP-C10).
    pub loader: Arc<dyn ExtensionLoader>,
    /// Model registry override (tests); default reads `<agent>/models.json` + `auth.json`.
    pub models: Option<ModelRegistry>,
    /// Tool selection applied to every woken session.
    pub tools: ToolSelection,
    /// `--exclude-tools` applied to every woken session.
    pub exclude_tools: Vec<String>,
}

impl HubOptions {
    /// The sessions root of these options: `session_dir` > `OINO_SESSION_DIR` >
    /// `sessionDir` setting > `<agent_dir>/sessions` (the `Services` chain, resolved once
    /// so hub listing and the runtimes it spawns agree).
    #[must_use]
    pub fn sessions_root(&self, settings: &SettingsManager) -> PathBuf {
        if let Some(dir) = &self.session_dir {
            return dir.clone();
        }
        if let Some(dir) = std::env::var_os("OINO_SESSION_DIR").filter(|d| !d.is_empty()) {
            return PathBuf::from(dir);
        }
        settings.settings().session_dir().map_or_else(
            || self.agent_dir.join("sessions"),
            |dir| {
                if dir.is_absolute() {
                    dir
                } else {
                    self.cwd.join(dir)
                }
            },
        )
    }
}

/// Host-level limits of one hub, layered over the `extensionSettings.hub.*` values;
/// [`HubLimits::default`] keeps every setting. Kept separate from [`HubOptions`] (a
/// plain struct literal, frozen at 1.0) so new knobs here stay additive:
/// `#[non_exhaustive]`, grown by builder methods only.
#[derive(Clone, Copy, Debug, Default)]
#[non_exhaustive]
pub struct HubLimits {
    /// Overrides `extensionSettings.hub.maxServices` (`None` keeps the setting, default 4).
    /// Every cached services set holds one wasm instance per extension — preopen dirs and
    /// log descriptors — so a host with a low `RLIMIT_NOFILE` cannot afford the default.
    max_services: Option<usize>,
}

impl HubLimits {
    /// Caps this hub at `max` services sets, overriding `extensionSettings.hub.maxServices`
    /// (clamped to `1..=1024` at create). An embedding host derives this from its own
    /// file-descriptor budget, not from user settings.
    #[must_use]
    pub fn with_max_services(mut self, max: usize) -> Self {
        self.max_services = Some(max);
        self
    }
}

/// `extensionSettings.hub.*` (§2.2): idle windows (`None` = disabled) and the services LRU
/// cap. Defaults 900/900/4 (owner ruling).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct HubConfig {
    /// `sessionIdleSec` (`0` disables session eviction).
    session_idle: Option<Duration>,
    /// `servicesIdleSec` (`0` disables services disposal).
    services_idle: Option<Duration>,
    /// `maxServices`.
    max_services: usize,
}

impl HubConfig {
    fn read(merged: &JsonObject) -> Self {
        let hub = merged
            .get("extensionSettings")
            .and_then(|e| e.get("hub"))
            .and_then(JsonValue::as_object);
        let secs = |key: &str, default: u64| {
            let raw = hub
                .and_then(|h| h.get(key))
                .and_then(JsonValue::as_u64)
                .unwrap_or(default);
            (raw > 0).then(|| Duration::from_secs(raw.min(u64::from(u32::MAX))))
        };
        let max_services = hub
            .and_then(|h| h.get("maxServices"))
            .and_then(JsonValue::as_u64)
            .unwrap_or(DEFAULT_MAX_SERVICES as u64);
        Self {
            session_idle: secs("sessionIdleSec", DEFAULT_SESSION_IDLE_SECS),
            services_idle: secs("servicesIdleSec", DEFAULT_SERVICES_IDLE_SECS),
            max_services: usize::try_from(max_services.clamp(1, 1024)).unwrap_or(1),
        }
    }

    /// The monitor's poll interval: a quarter of the shortest idle window.
    fn poll(&self) -> Option<Duration> {
        let shortest = [self.session_idle, self.services_idle]
            .into_iter()
            .flatten()
            .min()?;
        Some((shortest / 4).clamp(MIN_POLL, MAX_POLL))
    }
}

/// Services cache key: one `Services` per `(cwd, agent_dir)` (E-LAZY; the engine itself is
/// process-shared, E-ENGINE).
type ServicesKey = (PathBuf, PathBuf);

/// One cached services set.
struct ServicesEntry {
    /// Cache key.
    key: ServicesKey,
    /// The services (dropped on disposal).
    services: Arc<Services>,
    /// LRU clock.
    last_used: Instant,
    /// When the last live session of this group left; `None` while any is live.
    idle_since: Option<Instant>,
}

/// One live session: the runtime plus the shared bookkeeping the watcher and idle monitor
/// update (the session id changes on replacement, so it lives behind a mutex).
struct Live {
    runtime: Arc<AgentSessionRuntime>,
    services: ServicesKey,
    /// Current session id (re-keyed on `session_replaced`).
    id: Mutex<String>,
    /// Last activity: any client event, command or busy predicate.
    last_active: Mutex<Instant>,
    /// Set when the runtime's streams ended (dispose/evict/takeover).
    gone: AtomicBool,
}

/// Hub state guarded by one async mutex: open/close/evict transitions are serialised, so a
/// wake never races an eviction of the same session.
struct State {
    services: Vec<ServicesEntry>,
    live: HashMap<String, Arc<Live>>,
    crashed: HashSet<String>,
    /// Session id → (file, holder text) of a failed takeover (D-TAKEOVER deadline).
    locked: HashMap<String, (PathBuf, String)>,
}

struct Inner {
    cwd: PathBuf,
    agent_dir: PathBuf,
    docs_root: PathBuf,
    sessions_root: PathBuf,
    mode: ExtensionMode,
    has_ui: bool,
    approve: Option<bool>,
    flags: ResourceFlags,
    loader: Arc<dyn ExtensionLoader>,
    models: Option<ModelRegistry>,
    tools: ToolSelection,
    exclude_tools: Vec<String>,
    config: HubConfig,
    state: tokio::sync::Mutex<State>,
}

/// The lazy session hub (E-LAZY): records on disk until a message arrives.
///
/// Listing never creates services, engines or leases; `open`/`attach` wake a record
/// (lease + repair + runtime); idle sessions are evicted back to records. Cheap to hold:
/// the idle monitor exits when the hub is dropped.
pub struct SessionHub {
    inner: Arc<Inner>,
}

impl std::fmt::Debug for SessionHub {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SessionHub")
            .field("sessions_root", &self.inner.sessions_root)
            .field("config", &self.inner.config)
            .finish_non_exhaustive()
    }
}

impl SessionHub {
    /// Creates the hub: resolves the sessions root, reads `extensionSettings.hub.*` and
    /// starts the idle monitor (unless both idles are disabled). See [`Self::create_with`]
    /// for host-level limits over those settings.
    ///
    /// # Errors
    ///
    /// Never today; `Result` keeps room for settings failures.
    pub fn create(opts: HubOptions) -> Result<Arc<Self>, ClientError> {
        Self::create_with(opts, HubLimits::default())
    }

    /// [`Self::create`] with host-level `limits` layered over `extensionSettings.hub.*`:
    /// the embedding host's process budgets (fd limits), which the user did not set.
    ///
    /// # Errors
    ///
    /// Never today; `Result` keeps room for settings failures.
    pub fn create_with(opts: HubOptions, limits: HubLimits) -> Result<Arc<Self>, ClientError> {
        let settings = SettingsManager::create(&opts.cwd, &opts.agent_dir, false);
        let mut config = HubConfig::read(settings.settings().merged());
        if let Some(max) = limits.max_services {
            config.max_services = max.clamp(1, 1024);
        }
        let sessions_root = opts.sessions_root(&settings);
        let docs_root = opts
            .docs_root
            .clone()
            .unwrap_or_else(|| opts.agent_dir.join("docs"));
        let inner = Arc::new(Inner {
            cwd: opts.cwd,
            agent_dir: opts.agent_dir,
            docs_root,
            sessions_root,
            mode: opts.mode,
            has_ui: opts.has_ui,
            approve: opts.approve,
            flags: opts.flags,
            loader: opts.loader,
            models: opts.models,
            tools: opts.tools,
            exclude_tools: opts.exclude_tools,
            config,
            state: tokio::sync::Mutex::new(State {
                services: Vec::new(),
                live: HashMap::new(),
                crashed: HashSet::new(),
                locked: HashMap::new(),
            }),
        });
        if let Some(poll) = config.poll() {
            let weak = Arc::downgrade(&inner);
            tokio::spawn(async move { monitor(weak, poll).await });
        }
        Ok(Arc::new(Self { inner }))
    }

    /// Wakes `target` (exact session id, unique id prefix ≥ 4 chars, or a session file
    /// path) and returns its backend: lease + repair + `AgentSessionRuntime`
    /// (D-TAKEOVER/D-REPAIR through the runtime) → [`InProcessBackend`]. A live session is
    /// returned as-is. A session locked by an unresponsive holder fails with the precise
    /// D-TAKEOVER text and is listed [`HubSessionState::Locked`].
    ///
    /// # Errors
    ///
    /// Unknown or ambiguous target; services or session-open failures (with
    /// [`ClientError::Command`] texts, `SessionLocked` preserved).
    pub async fn open(&self, target: &str) -> Result<Arc<dyn SessionBackend>, ClientError> {
        self.wake(target).await
    }

    /// Creates a fresh session in `cwd` (the hub's primary cwd when
    /// `None`) and returns its backend. Services come from the hub's per-cwd cache; the
    /// runtime starts over `SessionSource::New`, so core writes the session file at the
    /// first conversation entry (durability rule), and `name` is recorded as the session's
    /// `session_info` name. The live entry is keyed by the new session's id, so later
    /// [`SessionHub::open`]/[`SessionHub::attach`] calls reuse this runtime.
    ///
    /// # Errors
    ///
    /// Services creation or session-open failures.
    pub async fn create_session(
        &self,
        cwd: Option<&Path>,
        name: Option<&str>,
    ) -> Result<Arc<dyn SessionBackend>, ClientError> {
        let mut state = self.inner.state.lock().await;
        let cwd = cwd.map_or_else(|| self.inner.cwd.clone(), Path::to_path_buf);
        let services = self.services_for(&mut state, &cwd).await?;
        let options = SessionOptions {
            source: SessionSource::New,
            session_dir: Some(self.inner.sessions_root.clone()),
            name: name.map(str::to_owned),
            tools: self.inner.tools.clone(),
            exclude_tools: self.inner.exclude_tools.clone(),
            ..SessionOptions::default()
        };
        let runtime = Arc::new(
            AgentSessionRuntime::create_for_hub(Arc::clone(&services), options)
                .await
                .map_err(|error| ClientError::Command(error.to_string()))?,
        );
        let session_id = runtime.session().session_id();
        tracing::info!(session_id = %session_id, cwd = %cwd.display(), "hub created a session");
        let live = live_entry(&runtime, (cwd, self.inner.agent_dir.clone()));
        state.crashed.remove(&session_id);
        state.locked.remove(&session_id);
        state.live.insert(session_id, Arc::clone(&live));
        drop(state);
        tokio::spawn(watch(Arc::downgrade(&self.inner), Arc::clone(&live)));
        Ok(lazy_backend(live))
    }

    /// Attaches to `session_id` by exact id, waking the record if needed (E-LAZY "a record
    /// becomes live on open/attach").
    ///
    /// # Errors
    ///
    /// As [`SessionHub::open`].
    pub async fn attach(&self, session_id: &str) -> Result<Arc<dyn SessionBackend>, ClientError> {
        self.wake(session_id).await
    }

    /// Every session record, newest modified first, merged with live state — no wake, no
    /// services, no leases (the lazy listing).
    ///
    /// # Errors
    ///
    /// Listing I/O failures.
    pub async fn list(&self) -> Result<Vec<HubSession>, ClientError> {
        let records = SessionManager::list_all(&self.inner.sessions_root, None, None)
            .map_err(|error| ClientError::Command(error.to_string()))?;
        let mut state = self.inner.state.lock().await;
        // A locked marker only stands while the holder's lease metadata does.
        state
            .locked
            .retain(|_, (path, _)| lock_metadata_path(path).exists());
        Ok(records
            .into_iter()
            .map(|info| HubSession {
                state: if state.live.contains_key(&info.id) {
                    if state.crashed.contains(&info.id) {
                        HubSessionState::Crashed
                    } else {
                        HubSessionState::Live
                    }
                } else if state.crashed.contains(&info.id) {
                    HubSessionState::Crashed
                } else if state.locked.contains_key(&info.id) {
                    HubSessionState::Locked
                } else {
                    HubSessionState::Record
                },
                info,
            })
            .collect())
    }

    /// Closes `session_id` if it is live (`session_shutdown{reason:"quit"}`, lease
    /// released); idempotent — closing a record or unknown id is `Ok`.
    ///
    /// # Errors
    ///
    /// Never today; `Result` keeps room for disposal failures.
    pub async fn close(&self, session_id: &str) -> Result<(), ClientError> {
        let live = self.inner.state.lock().await.live.remove(session_id);
        if let Some(live) = live {
            live.gone.store(true, Ordering::Release);
            live.runtime.dispose().await;
        }
        Ok(())
    }

    async fn wake(&self, target: &str) -> Result<Arc<dyn SessionBackend>, ClientError> {
        // The whole wake transition runs under the hub lock: opens, closes and evictions
        // serialise, so two opens of one record never race a lease against each other.
        let mut state = self.inner.state.lock().await;
        // Live by exact id: hand out a fresh backend over the shared runtime.
        if let Some(live) = state.live.get(target) {
            let live = Arc::clone(live);
            drop(state);
            *lock(&live.last_active) = Instant::now();
            return Ok(lazy_backend(live));
        }
        let records = SessionManager::list_all(&self.inner.sessions_root, None, None)
            .map_err(|error| ClientError::Command(error.to_string()))?;
        let info = resolve_target(target, &records)?;
        // Live under the resolved id (opened by prefix or path).
        if let Some(live) = state.live.get(&info.id) {
            let live = Arc::clone(live);
            drop(state);
            *lock(&live.last_active) = Instant::now();
            return Ok(lazy_backend(live));
        }
        let cwd = self.record_cwd(info);
        let services = self.services_for(&mut state, &cwd).await?;
        let options = SessionOptions {
            source: SessionSource::Open(info.path.clone()),
            session_dir: Some(self.inner.sessions_root.clone()),
            tools: self.inner.tools.clone(),
            exclude_tools: self.inner.exclude_tools.clone(),
            ..SessionOptions::default()
        };
        let runtime =
            match AgentSessionRuntime::create_for_hub(Arc::clone(&services), options).await {
                Ok(runtime) => Arc::new(runtime),
                Err(SessionOpError::SessionLocked(text)) => {
                    state
                        .locked
                        .insert(info.id.clone(), (info.path.clone(), text.clone()));
                    tracing::warn!(session_id = %info.id, "{text}");
                    return Err(ClientError::Command(text));
                }
                Err(error) => return Err(ClientError::Command(error.to_string())),
            };
        let session_id = runtime.session().session_id();
        tracing::info!(session_id = %session_id, "hub woke a session (E-LAZY)");
        let key = (cwd, self.inner.agent_dir.clone());
        let live = live_entry(&runtime, key);
        state.crashed.remove(&session_id);
        state.locked.remove(&session_id);
        state.live.insert(session_id, Arc::clone(&live));
        drop(state);
        tokio::spawn(watch(Arc::downgrade(&self.inner), Arc::clone(&live)));
        Ok(lazy_backend(live))
    }

    /// The services of the hub's primary cwd — cached and LRU-bounded like a session's —
    /// with **no session runtime**: the hub-level `_oino/*` subset (app spec §3.14,
    /// `host_cmd`, WP-A24) runs settings/auth/extension commands against it.
    ///
    /// # Errors
    ///
    /// Services creation failures.
    pub async fn services(&self) -> Result<Arc<Services>, ClientError> {
        let cwd = self.inner.cwd.clone();
        let mut state = self.inner.state.lock().await;
        self.services_for(&mut state, &cwd).await
    }

    /// Warms (or returns) the cached services of `cwd`, with no session runtime — the
    /// embedding host's background pre-warm of its recent projects' cwds, so a
    /// `session_open` there answers without paying services creation (extension load,
    /// wasm instances) on the click path.
    ///
    /// # Errors
    ///
    /// Services creation failures.
    pub async fn warm_services(&self, cwd: &Path) -> Result<Arc<Services>, ClientError> {
        let mut state = self.inner.state.lock().await;
        self.services_for(&mut state, cwd).await
    }

    /// The effective `maxServices` of this hub: the setting, or the [`HubLimits`] override.
    #[must_use]
    pub fn max_services(&self) -> usize {
        self.inner.config.max_services
    }

    /// The services of `cwd`: cached, LRU-bounded (`maxServices`) and reset to "in use".
    /// Called with the hub lock held (the caller owns it through the whole wake).
    async fn services_for(
        &self,
        state: &mut tokio::sync::MutexGuard<'_, State>,
        cwd: &Path,
    ) -> Result<Arc<Services>, ClientError> {
        let key: ServicesKey = (cwd.to_path_buf(), self.inner.agent_dir.clone());
        if let Some(entry) = state.services.iter_mut().find(|entry| entry.key == key) {
            entry.last_used = Instant::now();
            entry.idle_since = None;
            return Ok(Arc::clone(&entry.services));
        }
        if state.services.len() >= self.inner.config.max_services {
            let victim = state
                .services
                .iter()
                .enumerate()
                .filter(|(_, entry)| services_set_idle(state, &entry.key))
                .min_by_key(|(_, entry)| entry.last_used)
                .map(|(index, _)| index);
            match victim {
                Some(index) => {
                    let entry = state.services.remove(index);
                    // Idle live sessions on the victim keep its `Services` alive inside
                    // their runtimes, so they go back to records first (exactly what the
                    // idle monitor's eviction does; the next command re-wakes them).
                    let drained: Vec<Arc<Live>> = state
                        .live
                        .values()
                        .filter(|live| live.services == entry.key)
                        .cloned()
                        .collect();
                    for live in drained {
                        let id = lock(&live.id).clone();
                        state.live.remove(&id);
                        live.gone.store(true, Ordering::Release);
                        live.runtime.dispose().await;
                        tracing::info!(
                            session_id = %id, cwd = %entry.key.0.display(),
                            "hub retired an idle session to free its services (maxServices)"
                        );
                    }
                    tracing::info!(cwd = %entry.key.0.display(), "hub evicted services (maxServices)");
                    dispose_services(&entry.services).await;
                }
                None if state.services.len() == self.inner.config.max_services => {
                    // Every cached set has a busy session (a running turn, an attached
                    // stream or an open dialog). Serving the user outranks the descriptor
                    // budget: grow one set past the cap rather than refusing the open —
                    // the next idle set trims back as soon as one frees up.
                    tracing::warn!(
                        cwd = %cwd.display(),
                        sets = state.services.len(),
                        cap = self.inner.config.max_services,
                        "all services sets are busy; exceeding maxServices by one"
                    );
                }
                None => {
                    return Err(ClientError::Command(format!(
                        "hub services cache is over budget ({} of max {}) and every set has a \
                         running turn; wait for one to finish or close a session",
                        state.services.len(),
                        self.inner.config.max_services
                    )));
                }
            }
        }
        let services = Services::create(ServicesOptions {
            cwd: key.0.clone(),
            agent_dir: key.1.clone(),
            docs_root: self.inner.docs_root.clone(),
            mode: self.inner.mode,
            has_ui: self.inner.has_ui,
            flags: self.inner.flags.clone(),
            extension_flag_values: indexmap::IndexMap::new(),
            loader: Arc::clone(&self.inner.loader),
            approve: self.inner.approve,
            models: self.inner.models.clone(),
            settings: None,
        })
        .await
        .map_err(|error| {
            ClientError::Command(format!("services for {}: {error}", key.0.display()))
        })?;
        state.services.push(ServicesEntry {
            key: key.clone(),
            services: Arc::clone(&services),
            last_used: Instant::now(),
            idle_since: None,
        });
        Ok(services)
    }

    /// The services cwd of a record: its header cwd when absolute, else the hub's primary.
    fn record_cwd(&self, info: &SessionInfo) -> PathBuf {
        let cwd = Path::new(&info.cwd);
        if info.cwd.is_empty() || !cwd.is_absolute() {
            self.inner.cwd.clone()
        } else {
            cwd.to_path_buf()
        }
    }
}

/// One live session's entry: the runtime plus the bookkeeping the watcher and idle monitor
/// update, with the hub variant of the holder side of D-TAKEOVER installed (close our side,
/// no fresh replacement session).
fn live_entry(runtime: &Arc<AgentSessionRuntime>, services: ServicesKey) -> Arc<Live> {
    let live = Arc::new(Live {
        runtime: Arc::clone(runtime),
        services,
        id: Mutex::new(runtime.session().session_id()),
        last_active: Mutex::new(Instant::now()),
        gone: AtomicBool::new(false),
    });
    let weak_runtime = Arc::downgrade(runtime);
    runtime.set_takeover_handler(Some(Arc::new(move |by| {
        let weak_runtime = weak_runtime.clone();
        Box::pin(async move {
            if let Some(runtime) = weak_runtime.upgrade() {
                runtime.evict_taken_over(by).await;
            }
        })
    })));
    live
}

/// The backend of one woken session: an [`InProcessBackend`] wrapped with idle-clock
/// touches and post-eviction `Closed` failures.
fn lazy_backend(live: Arc<Live>) -> Arc<dyn SessionBackend> {
    let backend = Arc::new(InProcessBackend::new(Arc::clone(&live.runtime)));
    Arc::new(LazyBackend { live, backend })
}

/// One idle-monitor pass: evict idle live sessions, dispose idle services groups.
async fn tick(inner: &Inner) {
    let config = inner.config;
    let mut state = inner.state.lock().await;
    let now = Instant::now();
    if let Some(idle) = config.session_idle {
        let victims: Vec<Arc<Live>> = state
            .live
            .iter()
            .filter(|(_, live)| {
                if session_busy(&live.runtime) {
                    *lock(&live.last_active) = now;
                    return false;
                }
                now.duration_since(*lock(&live.last_active)) >= idle
            })
            .map(|(_, live)| Arc::clone(live))
            .collect();
        for live in victims {
            let id = lock(&live.id).clone();
            state.live.remove(&id);
            live.runtime.evict().await;
            // The record stays (re-listed from disk); a crash marker survives eviction.
        }
    }
    if config.services_idle.is_some() {
        let live_keys: HashSet<ServicesKey> = state
            .live
            .values()
            .map(|live| live.services.clone())
            .collect();
        let mut index = 0;
        while index < state.services.len() {
            let entry = &mut state.services[index];
            if live_keys.contains(&entry.key) {
                entry.last_used = now;
                entry.idle_since = None;
                index += 1;
                continue;
            }
            let since = *entry.idle_since.get_or_insert(now);
            if now.duration_since(since) >= config.services_idle.unwrap_or(Duration::MAX) {
                let entry = state.services.remove(index);
                tracing::info!(cwd = %entry.key.0.display(), "hub disposed idle services (E-LAZY)");
                dispose_services(&entry.services).await;
            } else {
                index += 1;
            }
        }
    }
}

/// Whether a session must stay live (E-LAZY conditions): a run, compaction, retry/arbiter
/// wait or open `!` command, a live (non-hibernated) child, or any external subscriber
/// (the runtime's own forward task is the single allowed one).
fn session_busy(runtime: &Arc<AgentSessionRuntime>) -> bool {
    let session = runtime.session();
    session.run_state() != oino_core::events::RunState::Idle
        || session.is_bash_running()
        || session
            .agent_tree()
            .iter()
            .any(|agent| matches!(agent.state, AgentRunState::Running | AgentRunState::Idle))
        || session.hub().subscriber_count() > 1
}

/// Whether every live session running on `key`'s services is at rest: no run in flight,
/// no attached stream and no open dialog. Such a set is a safe `maxServices` eviction
/// victim — its sessions return to records, exactly like the idle monitor's eviction.
fn services_set_idle(state: &State, key: &ServicesKey) -> bool {
    state
        .live
        .values()
        .filter(|live| &live.services == key)
        .all(|live| {
            !session_busy(&live.runtime) && {
                let session = live.runtime.session();
                !session
                    .ui()
                    .snapshot()
                    .iter()
                    .any(|request| oino_core::ui::is_dialog(&request.method))
            }
        })
}

/// Shuts a disposed services set's extensions down (E-LAZY: "extension shutdown, instances
/// dropped").
async fn dispose_services(services: &Arc<Services>) {
    for extension in services.extensions.extensions() {
        extension.instance.shutdown("idle").await;
    }
}

/// Resolves `target` against the records: a session file path, an exact id, or a unique
/// id prefix of at least 4 characters (the `find_by_id` rules).
fn resolve_target<'a>(
    target: &str,
    records: &'a [SessionInfo],
) -> Result<&'a SessionInfo, ClientError> {
    let path = Path::new(target);
    if path.exists() {
        return records
            .iter()
            .find(|info| info.path == path)
            .ok_or_else(|| {
                ClientError::Command(format!("Not an Oino session in the hub: {target}"))
            });
    }
    let exact: Vec<&SessionInfo> = records.iter().filter(|info| info.id == target).collect();
    if exact.len() == 1 {
        return Ok(exact[0]);
    }
    if exact.len() > 1 {
        return Err(ClientError::Command(format!(
            "Ambiguous session id {target}: it matches {} sessions",
            exact.len()
        )));
    }
    if target.len() >= 4 {
        let prefixed: Vec<&SessionInfo> = records
            .iter()
            .filter(|info| info.id.starts_with(target))
            .collect();
        match prefixed.len() {
            1 => return Ok(prefixed[0]),
            0 => {}
            n => {
                return Err(ClientError::Command(format!(
                    "Ambiguous session id {target}: it matches {n} sessions"
                )));
            }
        }
    }
    Err(ClientError::Command(format!(
        "No session found matching '{target}'"
    )))
}

/// Follows one live session's client events (on the runtime stream, which survives session
/// replacements): refreshes its activity clock, re-keys it on replacement, records
/// contained crashes (X-PANIC), and removes it when the stream ends (dispose/evict/
/// takeover all close the runtime hub).
async fn watch(inner: Weak<Inner>, live: Arc<Live>) {
    let mut stream = live.runtime.subscribe();
    while let Some(event) = stream.next().await {
        *lock(&live.last_active) = Instant::now();
        match &event {
            ClientEvent::Oino(OinoEvent::SessionReplaced { session_id, .. }) => {
                if let Some(inner) = inner.upgrade() {
                    rekey(&inner, &live, session_id.clone()).await;
                }
            }
            ClientEvent::Oino(OinoEvent::SessionCrashed { .. }) => {
                if let Some(inner) = inner.upgrade() {
                    let id = lock(&live.id).clone();
                    inner.state.lock().await.crashed.insert(id);
                }
            }
            _ => {}
        }
    }
    // Stream end: the runtime was disposed, evicted or taken over.
    live.gone.store(true, Ordering::Release);
    if let Some(inner) = inner.upgrade() {
        let id = lock(&live.id).clone();
        let mut state = inner.state.lock().await;
        if state
            .live
            .get(&id)
            .is_some_and(|entry| Arc::ptr_eq(entry, &live))
        {
            state.live.remove(&id);
        }
    }
}

/// Moves a live entry to its replacement session's id (new/fork/switch through a backend).
async fn rekey(inner: &Inner, live: &Arc<Live>, new_id: String) {
    let mut state = inner.state.lock().await;
    let old = std::mem::replace(&mut *lock(&live.id), new_id.clone());
    if state
        .live
        .get(&old)
        .is_some_and(|entry| Arc::ptr_eq(entry, live))
    {
        state.live.remove(&old);
        state.crashed.remove(&old);
        state.locked.remove(&old);
    }
    state.live.insert(new_id, Arc::clone(live));
}

/// The idle monitor: polls until the hub is dropped (both idles disabled ⇒ never started).
async fn monitor(inner: Weak<Inner>, poll: Duration) {
    loop {
        tokio::time::sleep(poll).await;
        let Some(hub) = inner.upgrade() else {
            return;
        };
        tick(&hub).await;
    }
}

/// A hub backend: the [`InProcessBackend`] of a woken session, with every command, event
/// subscription or dialog answer refreshing the session's idle clock ("a record becomes
/// live … on any inbound message"), and post-eviction commands failing
/// [`ClientError::Closed`] instead of reaching a disposed session.
struct LazyBackend {
    live: Arc<Live>,
    backend: Arc<InProcessBackend>,
}

impl LazyBackend {
    fn touch(&self) {
        *lock(&self.live.last_active) = Instant::now();
    }
}

impl SessionBackend for LazyBackend {
    fn send(&self, command: RpcCommand) -> BoxFuture<'_, Result<RpcResponseData, ClientError>> {
        self.touch();
        if self.live.gone.load(Ordering::Acquire) {
            return Box::pin(futures::future::ready(Err(ClientError::Closed)));
        }
        self.backend.send(command)
    }

    fn send_with_id(
        &self,
        request_id: Option<String>,
        command: RpcCommand,
    ) -> BoxFuture<'_, Result<RpcResponseData, ClientError>> {
        self.touch();
        if self.live.gone.load(Ordering::Acquire) {
            return Box::pin(futures::future::ready(Err(ClientError::Closed)));
        }
        self.backend.send_with_id(request_id, command)
    }

    fn events(&self) -> BoxStream<'static, ClientEvent> {
        self.touch();
        self.backend.events()
    }

    fn ui_respond(&self, response: UiResponse) {
        self.touch();
        self.backend.ui_respond(response);
    }

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

    fn shutdown(&self) -> BoxFuture<'_, ()> {
        self.backend.shutdown()
    }

    fn shutdown_requested(&self) -> BoxFuture<'static, ()> {
        self.backend.shutdown_requested()
    }
}