Skip to main content

meerkat_session/
lib.rs

1//! meerkat-session — Session service orchestration for Meerkat.
2//!
3//! This crate provides `EphemeralSessionService` (always available) and,
4//! behind feature gates, `PersistentSessionService` and `DefaultCompactor`.
5//!
6//! # Features
7//!
8//! - `session-store`: Enables `PersistentSessionService`.
9//! - `session-compaction`: Enables `DefaultCompactor` and `CompactionConfig`.
10
11// On wasm32, use tokio_with_wasm as a drop-in replacement for tokio.
12#[cfg(target_arch = "wasm32")]
13pub mod tokio {
14    pub use tokio_with_wasm::alias::*;
15}
16
17pub mod ephemeral;
18pub(crate) mod generated;
19pub mod staged_registry;
20pub(crate) mod turn_admission;
21
22#[cfg(feature = "session-compaction")]
23pub mod compactor;
24
25#[cfg(all(feature = "session-store", not(target_arch = "wasm32")))]
26pub mod event_store;
27
28#[cfg(all(feature = "session-store", not(target_arch = "wasm32")))]
29pub mod persistent;
30
31#[cfg(all(feature = "session-store", not(target_arch = "wasm32")))]
32pub mod projector;
33
34pub use ephemeral::{
35    EphemeralSessionService, RuntimeContextAdmissionGuard, SessionAgent, SessionAgentBuilder,
36    SessionSnapshot,
37};
38pub use staged_registry::{AdmissionOutcome, MaterializationStatus, StagedSessionRegistry};
39
40/// Metadata key used to store session labels in the `Session.metadata` map.
41///
42/// This is a persistence implementation detail — the ephemeral service stores
43/// labels directly on its in-memory handle; the persistent service reads/writes
44/// this key in the session snapshot for durable storage.
45pub const SESSION_LABELS_KEY: &str = "session_labels";
46
47/// Type alias for the raw broadcast receiver used by event subscriptions.
48/// Exported for WASM surface which needs synchronous `try_recv()`.
49pub type BroadcastEventReceiver =
50    tokio::sync::broadcast::Receiver<meerkat_core::EventEnvelope<meerkat_core::AgentEvent>>;
51
52#[cfg(feature = "session-compaction")]
53pub use compactor::DefaultCompactor;
54
55#[cfg(all(feature = "session-store", not(target_arch = "wasm32")))]
56pub use persistent::{
57    MachineServiceTurnCommitProtocol, MachineSessionArchiveProtocol, PersistentSessionService,
58};
59
60// Skill registration (inventory + meerkat-skills not available on wasm32)
61#[cfg(all(feature = "session-store", not(target_arch = "wasm32")))]
62inventory::submit! {
63    meerkat_skills::SkillRegistration {
64        id: "session-management",
65        name: "Session Management",
66        description: "Session persistence, resume patterns, event store replay, compaction tuning",
67        scope: meerkat_core::skills::SkillScope::Builtin,
68        requires_capabilities: &["session_store"],
69        body: include_str!("../skills/session-management/SKILL.md"),
70        extensions: &[],
71    }
72}
73
74// Capability registrations (inventory not available on wasm32)
75#[cfg(all(feature = "session-store", not(target_arch = "wasm32")))]
76inventory::submit! {
77    meerkat_capabilities::CapabilityRegistration {
78        id: meerkat_capabilities::CapabilityId::SessionStore,
79        description: "PersistentSessionService, SessionProjector",
80        scope: meerkat_capabilities::CapabilityScope::Universal,
81        requires_feature: Some("session-store"),
82        prerequisites: &[],
83        status_resolver: None,
84    }
85}
86
87#[cfg(all(feature = "session-compaction", not(target_arch = "wasm32")))]
88inventory::submit! {
89    meerkat_capabilities::CapabilityRegistration {
90        id: meerkat_capabilities::CapabilityId::SessionCompaction,
91        description: "DefaultCompactor: auto-compact at token threshold, LLM summary, history rebuild",
92        scope: meerkat_capabilities::CapabilityScope::Universal,
93        requires_feature: Some("session-compaction"),
94        prerequisites: &[],
95        status_resolver: None,
96    }
97}
98
99/// Convert a [`meerkat_core::service::SessionControlError`] into the public
100/// [`meerkat_core::service::SessionError`] surface: session-tier causes pass
101/// through unchanged; control-tier causes surface as typed `Unsupported`.
102///
103/// Single shared owner for the ephemeral and persistent services (the
104/// persistent twin previously carried a private copy).
105pub(crate) fn control_error_into_session_error(
106    err: meerkat_core::service::SessionControlError,
107) -> meerkat_core::service::SessionError {
108    match err {
109        meerkat_core::service::SessionControlError::Session(session_err) => session_err,
110        other => meerkat_core::service::SessionError::Unsupported(other.to_string()),
111    }
112}