trusty_memory/lib.rs
1//! The trusty-memory daemon: one resident process, one Unix socket.
2//!
3//! Why: Claude Code and other MCP-aware clients reach trusty-memory through the
4//! Model Context Protocol, and the daemon owns redb's exclusive write lock, so
5//! every client has to go through it rather than open the store itself (#1078).
6//! `trusty-memory serve --stdio` is the canonical integration: it forwards each
7//! JSON-RPC request to the running daemon and never opens redb.
8//!
9//! #6286 (ADR-0032) retired the HTTP half. The daemon bound
10//! `127.0.0.1:7070`, walked to `:7079` when that was taken, published the
11//! winner in an `http_addr` discovery file, and served forty axum routes plus
12//! an `/sse` broadcast. It now serves `trusty_common::daemon_socket_path`'s
13//! hardened Unix socket and nothing else; `trusty-console` is the workspace's
14//! only HTTP surface.
15//!
16//! What: [`transport::uds::serve`] is the daemon body, and [`AppState`] carries
17//! the shared `PalaceRegistry`, the on-disk data root and a lazily-initialised
18//! embedder.
19//! Test: `cargo test -p trusty-memory` — `transport::uds::tests` for the wire,
20//! `tests/serve_stdio_e2e.rs` for the bridge end to end.
21
22// docs.rs builds a release's documentation once, from the uploaded tarball,
23// so a broken intra-doc link is baked into that version forever and only a new
24// release can correct it. Deny keeps this crate at zero rather than letting the
25// ratchet in `scripts/check_rustdoc_links.sh` absorb a new one.
26#![deny(rustdoc::broken_intra_doc_links)]
27
28use crate::session_store_cache::SessionStoreCache;
29use anyhow::Result;
30use serde_json::{json, Value};
31use std::net::SocketAddr;
32use std::path::PathBuf;
33use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering};
34use std::sync::{Arc, OnceLock};
35use tokio::sync::{broadcast, OnceCell, RwLock};
36use trusty_common::memory_core::embed::Embedder;
37use trusty_common::memory_core::{store::ChatSessionStore, PalaceRegistry};
38use trusty_common::ChatProvider;
39use trusty_mcp::initialize_response;
40
41/// Two-phase daemon readiness state (issues #910/#911, revised by #1970).
42///
43/// Why: The embedder cold-init (CoreML compile, 30-120 s) must never block
44/// the fast text/KG/BM25 paths that don't need it. Originally (#910/#911)
45/// this state gated a hard-error preflight that rejected every
46/// `memory_remember`/`memory_recall` call outright while `Warming` — mirrored
47/// from trusty-search's staged pipeline, #1970 replaced that with graceful
48/// degradation: writes persist immediately and defer embedding to a
49/// background task, reads return BM25 + L0/L1 results and simply omit the
50/// vector lane, all keyed off this same state.
51/// What: Two stable values stored atomically. `Warming` (0) is the initial
52/// state; `Ready` (1) is set once the embedder has been successfully
53/// initialised by `spawn_startup_tasks`. The transition is one-way and
54/// lock-free: a single `AtomicU8` compare-and-swap.
55/// Test: `daemon_readiness_transitions_warming_to_ready` in this module;
56/// degraded-path coverage in `tools::tests`
57/// (`remember_succeeds_and_defers_embedding_while_state_is_warming`,
58/// `recall_falls_back_to_bm25_and_l0_l1_while_warming`).
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum DaemonReadiness {
61 /// Embedder cold-init (and/or pin scan) still in progress.
62 Warming = 0,
63 /// Embedder initialised; all handlers may proceed normally.
64 Ready = 1,
65}
66
67impl DaemonReadiness {
68 /// Decode the raw atomic value.
69 ///
70 /// Why: centralises the `0 → Warming, else Ready` mapping so every
71 /// caller loads a meaningful enum rather than comparing raw integers.
72 /// What: returns `Warming` for `0`, `Ready` for any other value (only
73 /// `1` is ever written).
74 /// Test: `daemon_readiness_from_u8` in this module.
75 pub fn from_u8(v: u8) -> Self {
76 if v == 0 {
77 Self::Warming
78 } else {
79 Self::Ready
80 }
81 }
82}
83
84pub mod activity;
85pub mod attribution;
86pub mod authz;
87pub mod bm25_backfill;
88pub mod bm25_index;
89pub mod bm25_lane;
90pub mod bm25_repair;
91pub mod bootstrap;
92pub mod dream_scheduler;
93pub mod fd_metrics;
94pub mod idle_evict;
95pub mod worker_liveness;
96// Why (issue #226): `chat` drives an LLM provider and a tool loop that only
97// the daemon serves. Gating it behind `daemon` is what lets a library
98// consumer linking only `MemoryMcpService` — trusty-agents — keep it out
99// of its build graph.
100#[cfg(feature = "daemon")]
101pub mod chat;
102pub mod client;
103pub mod commands;
104pub mod console_metrics;
105pub mod discovery;
106mod events;
107pub mod hook_emit;
108pub mod kg_extract;
109// #5524: the single entry point every caller-supplied KG assert routes through.
110pub mod kg_write;
111pub mod mcp_service;
112pub mod messaging;
113pub mod openrpc;
114pub mod palace_id_derive;
115// #6424: the durable per-palace "last used" stamp the console column reads.
116pub mod palace_last_used;
117pub mod project_root;
118pub mod prompt_facts;
119pub mod prompt_log;
120pub mod service;
121pub mod session_store_cache;
122pub mod startup_scan;
123// A real daemon on a temp socket, shared by every in-crate test that has to
124// prove a caller and the daemon agree (#6286). Never shipped.
125#[cfg(all(test, feature = "daemon"))]
126pub(crate) mod test_daemon;
127pub mod tools;
128pub mod transport;
129pub mod ui_assets;
130pub mod wordnet_pos;
131
132pub use activity::{ActivityEntry, ActivityFilter, ActivityLog, ActivitySource};
133pub use attribution::{CreatorInfo, CreatorSource};
134
135// Re-export the event types so the crate's public API is unchanged after the
136// #1195 split (`trusty_memory::DaemonEvent`, etc.). `open_activity_log_with_fallback`
137// stays crate-internal but is re-exported so `AppState::new` and `lib_tests`
138// reach it by its bare name via `super::*`.
139pub(crate) use events::open_activity_log_with_fallback;
140// #3434: test-only seam so `lib_tests` can force the tempdir-fallback path
141// via an explicit parameter instead of mutating the process-global `TMPDIR`.
142#[cfg(test)]
143pub(crate) use events::open_activity_log_with_fallback_in;
144pub use events::{DaemonEvent, HookType, InjectionKind};
145
146// The daemon's serving surface. `run_http`, `run_http_dynamic`, `run_http_on`,
147// `bind_dynamic_port`, `http_addr_path` and `DEFAULT_HTTP_PORT` went with the
148// listener (#6286); `serve` and `socket_path` are what replace them.
149#[cfg(feature = "daemon")]
150pub use transport::serve;
151pub use transport::socket_path;
152
153/// Return `true` when a non-default data directory is in effect.
154///
155/// Why (#880): a daemon running against an isolated data root must not run the
156/// startup pin-scan, which reads project pin files from the REAL user
157/// environment (`~/Projects`, `~/Developer`, …) and would import palaces from
158/// it into the isolated root, defeating the isolation. It used to suppress a
159/// second side-effect too — the legacy `~/.trusty-memory/http_addr` dotfile
160/// write — which #6286 removed along with the file.
161///
162/// What: reads `TRUSTY_DATA_DIR_OVERRIDE`. Empty or whitespace-only counts as
163/// unset, the same rule `resolve_data_dir` applies, so a blank env var does not
164/// silently isolate a production instance.
165/// Test: `is_data_dir_override_active_when_set`,
166/// `is_data_dir_override_inactive_when_unset`,
167/// `is_data_dir_override_inactive_when_blank`.
168#[inline]
169pub fn is_data_dir_override_active() -> bool {
170 matches!(
171 std::env::var(trusty_common::DATA_DIR_OVERRIDE_ENV),
172 Ok(v) if !v.trim().is_empty()
173 )
174}
175
176/// Maximum bytes retained in the trigger-prompt excerpt embedded on a
177/// `HookFired` event.
178///
179/// Why: the full triggering prompt is sensitive and already lives in the
180/// JSONL prompt log; the activity feed only needs enough text to give an
181/// operator a glance — a single-line ~80 char preview matches the existing
182/// `drawer_content_preview` convention so dashboard rows render uniformly.
183/// What: 80 characters; longer prompts are truncated with a trailing `…`.
184/// Test: `hook_excerpt_truncates_long_prompts`.
185pub const HOOK_PROMPT_EXCERPT_CHARS: usize = 80;
186
187/// Reduce a triggering prompt to the short excerpt embedded on a
188/// `HookFired` activity event.
189///
190/// Why: see [`HOOK_PROMPT_EXCERPT_CHARS`]. Centralising the truncation rule
191/// keeps every emitter (HTTP, hook CLI handlers, future tests) producing
192/// the same preview shape so UI rendering is uniform.
193/// What: whitespace-collapses `prompt` and trims to
194/// [`HOOK_PROMPT_EXCERPT_CHARS`] chars with `…` when cut. Empty input
195/// returns an empty string.
196/// Test: `hook_excerpt_truncates_long_prompts`,
197/// `hook_excerpt_collapses_whitespace`.
198pub fn hook_prompt_excerpt(prompt: &str) -> String {
199 let normalised: String = prompt.split_whitespace().collect::<Vec<_>>().join(" ");
200 if normalised.chars().count() <= HOOK_PROMPT_EXCERPT_CHARS {
201 normalised
202 } else {
203 let kept: String = normalised
204 .chars()
205 .take(HOOK_PROMPT_EXCERPT_CHARS.saturating_sub(1))
206 .collect();
207 format!("{kept}…")
208 }
209}
210
211pub use mcp_service::MemoryMcpService;
212pub use tools::MemoryMcpServer;
213
214/// Resolve the directory that actually holds the per-palace subdirectories.
215///
216/// Why: there are two on-disk layouts in the wild. The current monorepo code
217/// treats the registry directory *itself* as the parent of per-palace dirs
218/// (`<dir>/<id>/palace.json`). The legacy standalone `trusty-memory` repo
219/// nested everything one level deeper under a `palaces/` subdirectory
220/// (`<data_dir>/palaces/<id>/palace.json`) — and that is where existing
221/// installs' data lives (e.g. 88 palaces under
222/// `~/Library/Application Support/trusty-memory/palaces/`). A daemon that uses
223/// the bare data dir as its registry root finds zero palaces because every
224/// `palace.json` sits one level below where it looked — the "palaces lost on
225/// restart" bug.
226/// What: given the standard data dir, returns `<data_dir>/palaces` when that
227/// subdirectory exists, otherwise `<data_dir>` itself. Resolving this once in
228/// `main.rs` and using the result as `AppState::data_root` keeps every call
229/// site (`status`, `palace_list`, `open_palace`, `palace_create`,
230/// `load_palaces_from_disk`) consistent without forcing a data migration.
231/// Test: `tests::resolve_palace_registry_dir_prefers_palaces_subdir` and
232/// `resolve_palace_registry_dir_falls_back_to_data_dir`.
233pub fn resolve_palace_registry_dir(data_dir: PathBuf) -> PathBuf {
234 // Issue #1939: the subdir-choice logic is hoisted into trusty-common
235 // (`palace_alias::palace_registry_dir_from`) so trusty-mpm's alias-registration
236 // path and this daemon path can never disagree on WHERE the registry (and the
237 // alias file beside it) lives. This delegates to keep a single implementation.
238 trusty_common::palace_alias::palace_registry_dir_from(data_dir)
239}
240
241/// Shared application state passed to every request handler.
242///
243/// Why: The stdio loop and HTTP server need the same handles to the registry,
244/// data root, and embedder so MCP tools can perform real reads/writes against
245/// the live trusty-memory core. The embedder is heavy (loads ONNX weights) so
246/// it is resolved lazily through the process-wide singleton on first use.
247///
248/// #4836: this struct used to carry its own `Arc<OnceCell<Arc<FastEmbedder>>>`,
249/// a SECOND cell independent of `retrieval::shared_embedder()`. Startup warmed
250/// the shared cell and latched `daemon_readiness` off it, while every recall
251/// consumed the private cell — so the flag reported one embedder's state while
252/// the request path used another's. One cell removes the disagreement (and the
253/// duplicate ~90 MB ONNX session) by construction.
254/// What: `Clone`-able via `Arc` fields. The registry / data root are eager; the
255/// embedder is reached via [`AppState::embedder`].
256/// Test: `app_state_default_constructs` confirms construction without panic.
257#[derive(Clone)]
258pub struct AppState {
259 pub version: String,
260 pub registry: Arc<PalaceRegistry>,
261 pub data_root: PathBuf,
262 /// Optional default palace applied to MCP tool calls when the caller
263 /// omits the `palace` argument. Set via `trusty-memory serve --palace`.
264 pub default_palace: Option<String>,
265 /// Active chat provider selected at startup. `None` means no upstream is
266 /// configured (no Ollama detected and no OpenRouter key) — callers must
267 /// degrade gracefully (chat endpoint returns 412).
268 pub chat_provider: Arc<OnceCell<Option<Arc<dyn ChatProvider>>>>,
269 /// Per-palace chat-session stores, opened lazily so cold-start cost is
270 /// paid only when chat-history endpoints are hit.
271 ///
272 /// #4639: was an unbounded `DashMap` with no `remove`/TTL/cap, leaking one
273 /// `chat_sessions.redb` fd per palace for the daemon's lifetime (844
274 /// measured live, all pointing at already-unlinked files). Now an
275 /// LRU-bounded cache that evicts cold, unused stores.
276 pub session_stores: Arc<SessionStoreCache>,
277 /// Broadcast sender for live `DaemonEvent` pushes to SSE subscribers.
278 ///
279 /// Why: Lets mutating handlers emit events that any connected dashboard
280 /// receives instantly. Cap of 128 buffers transient slow readers; if a
281 /// receiver lags it gets `RecvError::Lagged` and we emit a `lag` frame.
282 pub events: Arc<broadcast::Sender<DaemonEvent>>,
283 /// Instant the daemon started, used to compute `uptime_secs` on `/health`.
284 ///
285 /// Why (issue #35): `GET /health` reports how long the daemon has been
286 /// up. Capturing a monotonic `Instant` at `AppState` construction lets the
287 /// handler compute the elapsed seconds cheaply and without a clock-skew
288 /// hazard.
289 /// What: a wall-monotonic `Instant`; `AppState::new` stamps it at startup.
290 /// Test: `health_reports_idle_worker_pool`.
291 pub started_at: std::time::Instant,
292 /// In-memory ring buffer of recent tracing log lines (issue #35).
293 ///
294 /// Why: the `GET /api/v1/logs/tail` endpoint serves the last N log lines
295 /// so operators can inspect a running daemon without tailing a file. The
296 /// buffer is shared between the tracing `LogBufferLayer` (writer) and the
297 /// HTTP handler (reader).
298 /// What: a cheap `Arc`-backed clone of the buffer the subscriber writes
299 /// to. Defaults to an empty buffer for states that never install the
300 /// layer (tests, the stdio path).
301 /// Test: `rpc_logs_tail_answers_a_bounded_page`.
302 pub log_buffer: trusty_common::log_buffer::LogBuffer,
303 /// Bug-capture ERROR store (bug-reporting #478, Phase 1).
304 ///
305 /// Why: Phase 2 MCP / HTTP endpoints need to query captured errors; stashing
306 /// the `ErrorStore` handle here lets any handler reach it cheaply without
307 /// a second global or per-request construction.
308 /// What: populated by `run_serve` from the `init_tracing_with_buffer_and_capture`
309 /// result; the layer writes to this store automatically so every
310 /// `tracing::error!` call site contributes without any changes to call
311 /// sites. `None` in states that do not install the layer (tests, the
312 /// stdio path).
313 /// Test: compile-presence is verified by the `trusty-memory` build; Phase 2
314 /// will add query tests in `web.rs`.
315 pub error_store: Option<trusty_common::error_capture::ErrorStore>,
316 /// Minimal multi-tenant authorization seam (issue #1714). `false`
317 /// (single-tenant, the default) preserves today's behaviour — every
318 /// existing caller of `palace_create force=true` keeps working. `true`
319 /// opts into `authz::authorize_force_palace_create` failing closed on
320 /// every `force=true` request until a real capability check lands. Set
321 /// via `with_multi_tenant_mode_from_env` (`TRUSTY_MEMORY_MULTI_TENANT=1`);
322 /// see the `authz` module docs for the full design rationale.
323 pub multi_tenant_mode: bool,
324 /// Most recent on-disk footprint of `data_root`, in bytes (issue #35).
325 ///
326 /// Why: `GET /health` reports `disk_bytes`. Walking the data directory on
327 /// every health request would make a frequent health poll do unbounded
328 /// I/O; a background task recomputes it every 10 s and stores it here so
329 /// the handler reads it lock-free.
330 /// What: an `AtomicU64` updated by the ticker spawned in `run_http_on`.
331 /// `0` until the first walk completes.
332 /// Test: `health_reports_idle_worker_pool`.
333 pub disk_bytes: Arc<std::sync::atomic::AtomicU64>,
334 /// Per-process RSS + CPU sampler, refreshed on each `/health` request
335 /// (issue #35).
336 ///
337 /// Why: CPU usage is a delta between two `sysinfo` refreshes, so the
338 /// sampler must persist between requests — hence the shared `Mutex`.
339 /// What: a `tokio::sync::Mutex<SysMetrics>` so the async health handler
340 /// can sample without blocking the runtime.
341 /// Test: `health_reports_idle_worker_pool`.
342 pub sys_metrics: Arc<tokio::sync::Mutex<trusty_common::sys_metrics::SysMetrics>>,
343 /// HTTP listener address the daemon bound to, once `run_http_on` is running.
344 ///
345 /// Why: clients (and `/health` responses) need to advertise the live
346 /// `host:port` even though port selection happens dynamically (7070–7079
347 /// walk + OS fallback). Stashing it on `AppState` lets request handlers
348 /// surface the discovery value without re-querying the listener.
349 /// What: a `OnceLock<SocketAddr>` so `run_http_on` writes it exactly once
350 /// at bind time and every handler reads it lock-free thereafter. Empty
351 /// (`None` from `get()`) on the stdio path where no listener exists.
352 /// Test: `health_endpoint_reports_bound_addr` (added below).
353 pub bound_addr: Arc<OnceLock<SocketAddr>>,
354 /// Cached prompt-facts surface served by the MCP `get_prompt_context`
355 /// tool (issue #42).
356 ///
357 /// Why: The original session-init `prompts/get` design loaded context
358 /// once per connection; switching to a per-message tool lets the model
359 /// pull fresh, query-filtered context on demand. The cache holds both
360 /// the raw triples (for filtered lookups) and a pre-formatted Markdown
361 /// block (for the unfiltered hot path) so neither code path re-walks
362 /// the KG. The cache is rebuilt by
363 /// `prompt_facts::rebuild_prompt_cache` after any write that touches a
364 /// hot predicate. #5524: every caller-supplied assert reaches that rebuild
365 /// through `kg_write::assert_triple` rather than each surface remembering
366 /// to call it; the retract side (`remove_prompt_fact`) still calls the
367 /// rebuild directly.
368 /// What: An `Arc<tokio::sync::RwLock<PromptFactsCache>>` so the hot
369 /// read path takes a brief read lock and clones the cache; rebuilds
370 /// take a write lock for the assignment only. The async-aware lock
371 /// (issue #229) yields to the tokio runtime instead of blocking a
372 /// runtime thread for the rebuild duration. An empty `triples` vec ↔
373 /// "no context stored yet" (the tool handler renders a hint).
374 /// Test: `get_prompt_context_returns_cached_or_hint`,
375 /// `get_prompt_context_filters_by_query`.
376 pub prompt_context_cache: Arc<RwLock<prompt_facts::PromptFactsCache>>,
377 /// Serializes the Tier S check-then-write sequence (#4888).
378 ///
379 /// Why: the 20-fact cap is only a cap if it cannot be raced past.
380 /// `check_tier_s_admission` counts active facts across every palace and
381 /// then the caller writes — two callers that both observe 19 would both
382 /// pass and the surface would land at 21. Nothing else serializes them:
383 /// the KG's single-writer actor orders writes only *within* one palace,
384 /// and the count spans all of them. "Usually 20, occasionally 21" is not
385 /// the invariant ADR-0028 D8 asks for.
386 /// What: an async mutex whose guard is acquired by
387 /// `check_tier_s_admission` and returned to the caller, which must hold it
388 /// until its `kg.assert` is enqueued. Cold predicates never acquire it, so
389 /// ordinary knowledge-graph writes stay fully concurrent; hot writes are
390 /// deliberate and rare, so serializing them costs nothing measurable.
391 /// Test: `tier_s_cap_holds_under_concurrent_writes`.
392 pub tier_s_admission_lock: Arc<tokio::sync::Mutex<()>>,
393 /// Persistent activity log (issue #96).
394 ///
395 /// Why: the dashboard activity feed used to be a pure live-stream over
396 /// `/sse` — opening the UI showed an empty feed and any mutation from
397 /// the MCP path was invisible. Holding an `ActivityLog` on `AppState`
398 /// lets `emit` record an entry on every push so the
399 /// `GET /api/v1/activity` handler can return historical rows on mount
400 /// and the live SSE stream can continue prepending events on top of
401 /// the loaded history. `None` on builds that opt out (tests that use
402 /// `AppState::new` get a real log under their tempdir so behaviour
403 /// matches production).
404 /// What: an `Arc<ActivityLog>` shared with every emitter.
405 /// Test: `web::tests::activity_endpoint_lists_recent_emits`.
406 pub activity_log: Arc<ActivityLog>,
407 /// Optional in-process BM25 lexical search lane (issue #156, #5329).
408 ///
409 /// Why: this single field replaces the former `bm25_client` +
410 /// `bm25_supervisor` pair. Those existed because BM25 ran as a per-palace
411 /// subprocess — one to speak its wire protocol, one to spawn and reap it.
412 /// #5329 collapsed the subprocess into this process, so the lane IS the
413 /// index and there is nothing left to supervise.
414 /// What: `Some(lane)` only when `TRUSTY_BM25_DAEMON=1` at startup. Every
415 /// code path that uses it is gated on `is_some()` and falls back to
416 /// vector-only recall otherwise, so a deployment that never set the gate —
417 /// which is every shipped deployment (#5186) — sees no behavioural change.
418 ///
419 /// 🔴 `pub(crate)`, not `pub`, and that is load-bearing. Installing a lane
420 /// also has to rebuild the indexer worker, because `AppState::new` spawns it
421 /// before any lane exists. Assigning this field on its own produces a state
422 /// whose READS use the lane and whose WRITES the placeholder worker
423 /// discards — silently, with no error anywhere. [`Self::with_bm25_lane`] is
424 /// the only way to set it and does both halves; keeping the field private
425 /// makes the broken half-installed state unrepresentable outside this crate
426 /// rather than merely documented. Read it with [`Self::bm25_lane`].
427 /// Test: `bm25_lane_disabled_by_default`, `bm25_lane_enabled_when_env_set`,
428 /// `writes_through_the_tool_surface_survive_eviction`.
429 pub(crate) bm25: Option<Arc<bm25_lane::Bm25Lane>>,
430 /// Per-palace write serialisation locks (issue #230).
431 ///
432 /// Why: the dedup gate in `tools.rs` previously read a snapshot of
433 /// existing drawers, checked for near-duplicates via Jaro-Winkler, and
434 /// then issued the write — a classic time-of-check/time-of-use race.
435 /// Two concurrent `memory_remember` calls with the same content could
436 /// both see the pre-write snapshot, both pass the gate, and both land
437 /// duplicate drawers. Serialising the gate-then-write sequence per
438 /// palace closes the window: while one task holds the mutex, any
439 /// concurrent writer for the same palace blocks until the first write
440 /// finishes and is visible to `list_drawers`. The lock is **per
441 /// palace** (not global) so writes to different palaces continue to
442 /// run in parallel.
443 /// What: a `DashMap` keyed by palace id, where each entry is an
444 /// `Arc<tokio::sync::Mutex<()>>`. The mutex is constructed lazily by
445 /// `palace_write_lock` on first access. `Arc` lets callers hold a
446 /// clone of the lock past the lifetime of the `DashMap` entry so the
447 /// map never needs to be held across an `.await`.
448 /// Test: `tools::tests::dedup_gate_blocks_concurrent_duplicate_writes`.
449 pub palace_write_locks: Arc<dashmap::DashMap<String, Arc<tokio::sync::Mutex<()>>>>,
450 /// Counter of in-flight activity-log writes spawned by `emit`
451 /// (issue #232).
452 ///
453 /// Why: `emit` offloads the synchronous redb append to the tokio blocking
454 /// pool via `spawn_blocking` so the async runtime is never parked waiting
455 /// on fsync. The write is fire-and-forget — `emit` returns immediately
456 /// after spawning. Tests that observe the activity log right after a
457 /// burst of `emit` calls need a deterministic synchronization point;
458 /// holding an in-flight counter lets `flush_activity_writes` poll until
459 /// every spawned append has settled, which keeps the assertions
460 /// race-free without forcing every caller to `.await`.
461 /// What: an `Arc<AtomicUsize>` incremented before each `spawn_blocking`
462 /// and decremented inside the closure (after the append completes, even
463 /// if it errored). The counter is cheap (one atomic add per emit) and
464 /// stays at zero in steady-state production traffic.
465 /// Test: `web::tests::activity_endpoint_lists_recent_emits` and
466 /// `tests::emit_persists_mutations_but_skips_status_changed` call
467 /// `flush_activity_writes` to drain the counter before reading the log.
468 pub pending_activity_writes: Arc<AtomicUsize>,
469 /// Live occupancy gauge for the palace open path (issue #4001).
470 ///
471 /// Why: during the #3992 incident six daemon threads sat parked in
472 /// `concurrent_open::backoff_sleep_ms` with a `memory_remember` hung
473 /// ~1800 s, while both doctors reported HEALTHY. Every existing signal —
474 /// HTTP liveness, fastembed cache state, lock-file staleness — describes
475 /// the *process*, not the *work*. This is the one field that can answer
476 /// "is anything actually moving?", and `/health` surfaces it so an
477 /// out-of-process doctor can report what the daemon actually observed
478 /// instead of inferring health from a cheap proxy.
479 /// What: an [`worker_liveness::WorkerLiveness`] slot table; one CAS on
480 /// entry and one store on exit per tracked operation, so the gauge cannot
481 /// itself become the load problem it exists to detect.
482 /// Test: `web::tests::health_tests::health_reports_wedged_worker_pool`.
483 pub worker_liveness: Arc<worker_liveness::WorkerLiveness>,
484 /// How long an operation may run before the pool is called wedged.
485 ///
486 /// Why this is state rather than an env read on the request path: `/health`
487 /// is polled once a second, so re-reading (and re-parsing) an environment
488 /// variable per request is needless work; resolving it once at construction
489 /// also makes the value a property of the daemon instead of a process-wide
490 /// global, which is what lets tests drive the wedge condition
491 /// deterministically instead of mutating shared env state and racing each
492 /// other.
493 /// What: defaults to [`worker_liveness::wedge_threshold`].
494 /// Test: `web::tests::health_tests::health_reports_wedged_worker_pool`.
495 pub wedge_threshold: std::time::Duration,
496 /// In-memory cache mapping palace id → `Palace.name` (issue #228).
497 ///
498 /// Why: every `memory_remember` / `memory_note` write used to call
499 /// `PalaceRegistry::list_palaces` (a synchronous filesystem walk of the
500 /// data root) just to resolve a friendly palace name for the SSE
501 /// `DrawerAdded` event. With N palaces on disk the cost was O(N) opendirs
502 /// plus `palace.json` reads on every write, blocking the async runtime.
503 /// Caching the name in-memory turns the lookup into a `DashMap::get`.
504 /// What: `DashMap<String, String>` populated by `create_palace` and
505 /// `load_palaces_from_disk`, kept in sync by rename / delete paths.
506 /// Missing entries are treated as "name unknown" so callers fall back to
507 /// the palace id and the emit path never fails.
508 /// Test: `palace_name_cache_populated_after_hydration` and
509 /// `palace_name_cache_updates_on_create`.
510 pub palace_names: Arc<dashmap::DashMap<String, String>>,
511 /// Single-pass startup pin-file map: palace id → project root path (issue #470).
512 ///
513 /// Why: after daemon startup we have no record of which on-disk project
514 /// directories correspond to which palace ids — that information only
515 /// existed inside the pin files on disk. Eager-opening every palace on
516 /// startup is too expensive. This field captures the scan-only result of
517 /// `startup_scan::scan_pin_map` so handlers that want to locate a project
518 /// by its palace id (e.g. future cwd-inference, project-health checks)
519 /// can do a single `DashMap::get` instead of a filesystem walk.
520 /// Populated once, shortly after `load_palaces_from_disk` returns, by
521 /// `spawn_startup_tasks`. Never mutated after population — it is a
522 /// snapshot of what the filesystem looked like at startup.
523 /// What: `DashMap<String (palace_id), PathBuf (project root)>`.
524 /// The outer `Arc` lets `spawn_startup_tasks` (which holds only a clone
525 /// of `AppState`) write to the same backing map that request handlers
526 /// read. Population is asynchronous so callers must treat an absent entry
527 /// as "not yet scanned" (or "no pin found"), never as "palace unknown".
528 /// Test: `startup_scan::tests::scan_pin_map_*` validate the underlying
529 /// scanner function; the wiring in `spawn_startup_tasks` is covered by
530 /// the integration-test daemon start path.
531 pub pin_project_map: Arc<dashmap::DashMap<String, PathBuf>>,
532 /// Bounded sender for the BM25 index worker (issue #231).
533 ///
534 /// Why: the previous fire-and-forget design `tokio::spawn`ed one task per
535 /// `memory_remember` / `memory_note` call, so a write burst against a slow
536 /// or unreachable BM25 daemon grew an unbounded in-flight task queue. A
537 /// single long-lived worker draining a bounded mpsc channel caps that
538 /// back-pressure: writers `try_send` (never block), full-queue requests
539 /// are dropped with a `warn!`, and the worker exits cleanly when the last
540 /// sender is dropped on shutdown.
541 /// What: an `mpsc::Sender` cloned to every `AppState` clone (cheap). The
542 /// matching receiver is consumed by the worker spawned in
543 /// [`AppState::new`] via [`tools::spawn_bm25_index_worker`]. Capacity is
544 /// [`tools::BM25_INDEX_QUEUE_CAPACITY`] (256).
545 /// Test: `bm25_index_queue_drops_when_full` exercises the full-queue
546 /// branch via `bm25_index_enqueue`.
547 pub bm25_index_tx: tokio::sync::mpsc::Sender<tools::Bm25IndexRequest>,
548 /// Palaces whose BM25 coverage is known to be incomplete (#5048 review).
549 ///
550 /// Why: `bm25_index_enqueue` drops on a full queue so `memory_remember`
551 /// never waits on daemon RTT. That trade is only defensible if a drop is
552 /// actually repaired, and before this field the sole production trigger for
553 /// a backfill was daemon startup — so a drop stayed invisible until the
554 /// next restart. Every observer of lost coverage marks the palace here and
555 /// [`bm25_repair::spawn_repair_sweep`] consumes it on an interval.
556 /// What: a `DashSet` of palace ids, shared by every `AppState` clone.
557 /// Idempotent — forty drops for one palace queue one repair.
558 /// Test: `bm25_repair_tests.rs`, `bm25_index_queue_drops_when_full`.
559 pub bm25_dirty: bm25_repair::DirtyPalaces,
560 /// Cached result of the startup update check (issue #537).
561 ///
562 /// Why: `/health` should report `update_available` without hitting crates.io
563 /// on every probe. A single background check at daemon startup stores the
564 /// result here; the health handler reads it lock-free (well, a brief mutex
565 /// lock) without a network call.
566 /// What: `None` = up-to-date or check not yet done; `Some("x.y.z")` = newer
567 /// version available. The field is populated by a `tokio::spawn` in
568 /// `spawn_startup_tasks` (main.rs) after the daemon binds.
569 /// Test: indirectly by the `/health` endpoint tests in `web.rs`.
570 pub update_available: Arc<std::sync::Mutex<Option<String>>>,
571 /// Two-phase readiness state — `Warming` until the embedder is initialised,
572 /// then `Ready` (issues #910 / #911).
573 ///
574 /// Why: `AppState::embedder()` used to call `FastEmbedder::new()` without
575 /// any timeout, so the first `memory_recall`/`memory_remember` that arrived
576 /// before CoreML finished compiling would block for 5–11 hours until the
577 /// OnceCell resolved (issue #910). Exposing this state lets the preflight
578 /// guards in `tools.rs` return an explicit fast error immediately —
579 /// `"trusty-memory is warming up, retry shortly"` — instead of queueing
580 /// behind an open-ended init.
581 /// What: An `AtomicU8` starting at `DaemonReadiness::Warming` (0) and flipped
582 /// to `DaemonReadiness::Ready` (1) by `spawn_startup_tasks` after the embedder
583 /// warm-up succeeds. The transition is one-way and lock-free.
584 /// Test: `daemon_readiness_transitions_warming_to_ready`.
585 pub daemon_readiness: Arc<AtomicU8>,
586 /// Total wall-clock ceiling for one MCP write operation (issue #4002).
587 ///
588 /// Why: a write waits for the per-palace write mutex and then waits again
589 /// to enter the per-palace open queue. Each leg was bounded by its own
590 /// timeout, so the effective ceiling was their sum (60 s + ~63 s), not
591 /// either configured bound. The handlers stamp one
592 /// [`trusty_common::memory_core::timeouts::OpBudget`] from this value and
593 /// clamp every leg through it, so the later leg spends what the earlier
594 /// leg left.
595 /// What: defaults to
596 /// [`trusty_common::memory_core::timeouts::write_op_budget`]
597 /// (`TRUSTY_WRITE_OP_BUDGET_SECS`, 60 s). Stored per-instance rather than
598 /// re-read from the environment so
599 /// [`AppState::with_write_op_budget`] can inject a short deadline in tests
600 /// without mutating process-wide state, which would race parallel tests —
601 /// the same reason `PalaceRegistry::with_open_queue_timeout` exists.
602 /// Test: `tools::tests::write_budget_tests`.
603 pub write_op_budget: std::time::Duration,
604 /// When each palace's durable last-used stamp was last written (#6424).
605 ///
606 /// Why: the console's Last Used column needs a stamp that survives a
607 /// restart, and a durable write per recall is not worth a column measured
608 /// in days. This is the throttle's memory — see
609 /// [`palace_last_used`] for the cadence and what lagging it costs.
610 /// What: palace id -> unix seconds of the last PERSISTED stamp, not of the
611 /// last use. Empty at boot, so the first use of any palace writes.
612 /// Test: `palace_last_used::tests::stamp_throttles_within_the_window`.
613 pub palace_last_used: palace_last_used::StampCache,
614 /// Ceiling on the pipeline one write runs while holding the palace write
615 /// mutex (issue #6366).
616 ///
617 /// Why: [`AppState::write_op_budget`] bounds only the waits BEFORE the
618 /// mutex is held. The pipeline that runs once it IS held had no ceiling, so
619 /// a slow commit held the mutex for as long as it took and every other
620 /// writer on that palace queued behind it — three `memory_note` calls were
621 /// aborted client-side after 1800 s while the daemon stayed healthy.
622 /// What: defaults to
623 /// [`trusty_common::memory_core::timeouts::write_pipeline_timeout`]
624 /// (`TRUSTY_WRITE_PIPELINE_TIMEOUT_SECS`, 240 s), and is threaded into
625 /// `PalaceHandle::remember_with_options_within`. Stored per-instance for
626 /// the same reason as `write_op_budget`: a test can inject a short ceiling
627 /// without mutating process-wide env.
628 /// Test: `tools::tests::write_budget_tests::memory_note_surfaces_the_pipeline_ceiling`
629 /// proves the ceiling reaches the daemon's own write handler; the pipeline's
630 /// behaviour under that ceiling lives in trusty-common's retrieval
631 /// write-pipeline tests.
632 pub write_pipeline_budget: std::time::Duration,
633}
634
635impl AppState {
636 /// Construct an `AppState` rooted at the given on-disk data directory.
637 ///
638 /// Why: The CLI (`serve`) and integration tests need to point the MCP
639 /// server at different roots — production at `dirs::data_dir`, tests at a
640 /// `tempfile::tempdir()`.
641 /// What: Builds an empty `PalaceRegistry`, captures the version, and
642 /// allocates an empty `OnceCell` for the embedder. `default_palace` is
643 /// `None`; use `with_default_palace` to set it.
644 /// Test: `tools::tests::dispatch_palace_create_persists` constructs an
645 /// AppState pointed at a tempdir and round-trips a palace through it.
646 pub fn new(data_root: PathBuf) -> Self {
647 let (events_tx, _) = broadcast::channel::<DaemonEvent>(128);
648 // Issue #96: open (or create) the persistent activity log under the
649 // daemon data root. Open failure is logged but never crashes the
650 // daemon — we fall back to a per-process tempdir so emits remain
651 // best-effort and the rest of the daemon keeps working.
652 let activity_log = open_activity_log_with_fallback(&data_root);
653 // Issue #231: bounded mpsc channel + single long-lived worker
654 // replaces the per-write `tokio::spawn` fire-and-forget pattern so
655 // BM25 indexing back-pressure is capped. The worker is spawned here
656 // unconditionally so the channel always has a drain — even when
657 // the lane is off, the worker just consumes and discards
658 // each request so senders never block on a full queue.
659 let (bm25_index_tx, bm25_index_rx) =
660 tokio::sync::mpsc::channel::<tools::Bm25IndexRequest>(tools::BM25_INDEX_QUEUE_CAPACITY);
661 // `bm25` starts as `None`; the builder `with_bm25_lane_from_env`
662 // rebuilds the worker with the real lane once env-gated opt-in is
663 // resolved.
664 let bm25_dirty: bm25_repair::DirtyPalaces = Arc::new(dashmap::DashSet::new());
665 tools::spawn_bm25_index_worker(bm25_index_rx, None, Arc::clone(&bm25_dirty));
666 Self {
667 version: env!("CARGO_PKG_VERSION").to_string(),
668 // Idle-to-disk: honour TRUSTY_MEMORY_MAX_OPEN_PALACES (default 64)
669 // so operators can bound resident-palace RAM without a rebuild.
670 registry: Arc::new(PalaceRegistry::from_env()),
671 data_root,
672 default_palace: None,
673 chat_provider: Arc::new(OnceCell::new()),
674 // #4639: bounded LRU (TRUSTY_MEMORY_MAX_OPEN_SESSION_STORES,
675 // default 32) so chat_sessions.redb handles stop accumulating.
676 session_stores: Arc::new(SessionStoreCache::from_env()),
677 events: Arc::new(events_tx),
678 started_at: std::time::Instant::now(),
679 // Default to an empty buffer — `with_log_buffer` overrides this
680 // when the daemon installs the `LogBufferLayer` (HTTP mode).
681 log_buffer: trusty_common::log_buffer::LogBuffer::new(
682 trusty_common::log_buffer::DEFAULT_LOG_CAPACITY,
683 ),
684 // Bug-reporting #478: `None` until `with_error_store` is called
685 // during daemon startup (HTTP mode). Tests keep `None` so no
686 // unexpected files are written to the OS data dir.
687 error_store: None,
688 multi_tenant_mode: false,
689 disk_bytes: Arc::new(std::sync::atomic::AtomicU64::new(0)),
690 sys_metrics: Arc::new(tokio::sync::Mutex::new(
691 trusty_common::sys_metrics::SysMetrics::new(),
692 )),
693 bound_addr: Arc::new(OnceLock::new()),
694 prompt_context_cache: Arc::new(RwLock::new(prompt_facts::PromptFactsCache::default())),
695 tier_s_admission_lock: Arc::new(tokio::sync::Mutex::new(())),
696 activity_log,
697 bm25: None,
698 palace_write_locks: Arc::new(dashmap::DashMap::new()),
699 pending_activity_writes: Arc::new(AtomicUsize::new(0)),
700 worker_liveness: Arc::new(worker_liveness::WorkerLiveness::new()),
701 wedge_threshold: worker_liveness::wedge_threshold(),
702 palace_names: Arc::new(dashmap::DashMap::new()),
703 pin_project_map: Arc::new(dashmap::DashMap::new()),
704 bm25_index_tx,
705 bm25_dirty,
706 update_available: Arc::new(std::sync::Mutex::new(None)),
707 // Start in Warming state; flipped to Ready by spawn_startup_tasks
708 // once the embedder warm-up succeeds (issues #910/#911).
709 daemon_readiness: Arc::new(AtomicU8::new(DaemonReadiness::Warming as u8)),
710 // #4002: one ceiling for the whole write, read once at construction.
711 write_op_budget: trusty_common::memory_core::timeouts::write_op_budget(),
712 // #6424: empty at boot, so the first use of each palace after a
713 // restart always persists a stamp.
714 palace_last_used: Arc::new(dashmap::DashMap::new()),
715 // #6366: the ceiling on the critical section #4002's budget stops at.
716 write_pipeline_budget: trusty_common::memory_core::timeouts::write_pipeline_timeout(),
717 }
718 }
719
720 /// Override the per-operation write budget (issue #4002).
721 ///
722 /// Why: tests must prove the two write legs draw on ONE budget without
723 /// setting `TRUSTY_WRITE_OP_BUDGET_SECS`, which is process-wide and would
724 /// race any test running in parallel.
725 /// What: consuming builder that overwrites [`AppState::write_op_budget`].
726 /// Test: `tools::tests::write_budget_tests`.
727 #[must_use]
728 pub fn with_write_op_budget(mut self, budget: std::time::Duration) -> Self {
729 self.write_op_budget = budget;
730 self
731 }
732
733 /// Override the write-pipeline ceiling (issue #6366).
734 ///
735 /// Why: same reason as [`AppState::with_write_op_budget`] — a test proving
736 /// the ceiling fires must not set `TRUSTY_WRITE_PIPELINE_TIMEOUT_SECS`,
737 /// which is process-wide and would race any parallel test.
738 /// What: consuming builder that overwrites
739 /// [`AppState::write_pipeline_budget`].
740 /// Test: `tools::tests::write_budget_tests::memory_note_surfaces_the_pipeline_ceiling`.
741 #[must_use]
742 pub fn with_write_pipeline_budget(mut self, budget: std::time::Duration) -> Self {
743 self.write_pipeline_budget = budget;
744 self
745 }
746
747 /// Acquire (lazily, then clone) the per-palace write mutex.
748 ///
749 /// Why (issue #230): the dedup-check + `remember_with_options` write
750 /// sequence in `tools.rs` must be atomic per palace to prevent two
751 /// concurrent identical writes from both passing the dedup gate.
752 /// Callers hold the returned `Arc<Mutex<()>>`'s guard across the gate
753 /// check and the write so the second writer blocks until the first
754 /// write is visible to `list_drawers`. Returning a clone of the `Arc`
755 /// rather than a borrow into the `DashMap` lets the caller `.await`
756 /// while holding the lock without risking a deadlock against any
757 /// future map mutation (DashMap shards are sync mutexes).
758 /// What: looks up the palace id in `palace_write_locks` and returns
759 /// a clone of the existing mutex; on the first call for a palace,
760 /// inserts a freshly-constructed `tokio::sync::Mutex<()>` first. The
761 /// `DashMap::entry().or_insert_with` API guarantees the lazy
762 /// construction is racy-safe — only one mutex is ever inserted per
763 /// palace id.
764 /// Test: `tools::tests::dedup_gate_blocks_concurrent_duplicate_writes`.
765 pub fn palace_write_lock(&self, palace_id: &str) -> Arc<tokio::sync::Mutex<()>> {
766 if let Some(existing) = self.palace_write_locks.get(palace_id) {
767 return existing.clone();
768 }
769 self.palace_write_locks
770 .entry(palace_id.to_string())
771 .or_insert_with(|| Arc::new(tokio::sync::Mutex::new(())))
772 .clone()
773 }
774
775 /// Look up a project root path by palace id in the startup pin-scan map.
776 ///
777 /// Why: provides a stable, cheap accessor so handlers do not reach directly
778 /// into the `DashMap` field and so the accessor can be mocked in future
779 /// tests without touching `AppState` internals. The map is populated
780 /// asynchronously by `spawn_startup_tasks` — an absent entry means either
781 /// the scan has not completed yet or no pin file claimed that id.
782 /// What: returns `Some(project_path)` when the palace id was found during
783 /// startup scan; `None` otherwise.
784 /// Test: covered indirectly via the startup-scan integration path; the
785 /// underlying map data is validated by `startup_scan::tests`.
786 pub fn pinned_project_path(&self, palace_id: &str) -> Option<PathBuf> {
787 self.pin_project_map.get(palace_id).map(|e| e.clone())
788 }
789
790 /// Builder-style: opt-in to the BM25 lexical lane (issue #156, #5329).
791 ///
792 /// Why: the lane stays gated behind `TRUSTY_BM25_DAEMON=1` even though
793 /// #5329 removed the daemon that name refers to. Renaming the variable
794 /// would break the only enablement path anyone could have configured, in
795 /// the one PR whose purpose is not losing that lane — the compatibility is
796 /// worth more than the accuracy.
797 /// What: when the gate is set, builds a [`bm25_lane::Bm25Lane`] over this
798 /// state's `data_root` — which is where the retired daemon wrote its
799 /// snapshots, so an existing corpus is picked up in place — and rebuilds
800 /// the bounded indexer channel so its worker holds the lane. Returns `self`
801 /// unchanged when the var is unset or set to anything other than `1`.
802 /// Test: `bm25_lane_disabled_by_default`, `bm25_lane_enabled_when_env_set`.
803 #[must_use]
804 pub fn with_bm25_lane_from_env(self) -> Self {
805 // #5329: the gate keeps its daemon-era name so an operator who set it
806 // does not silently lose the lane this change exists to preserve.
807 if std::env::var("TRUSTY_BM25_DAEMON").as_deref() != Ok("1") {
808 return self;
809 }
810 let lane = bm25_lane::Bm25Lane::new(self.data_root.clone());
811 tracing::info!(
812 max_resident = lane.max_resident(),
813 text_budget_bytes = ?lane.text_budget_bytes(),
814 "in-process BM25 lane enabled (TRUSTY_BM25_DAEMON=1)"
815 );
816 self.with_bm25_lane(lane)
817 }
818
819 /// Builder-style: install an explicit BM25 lane, bypassing the env gate.
820 ///
821 /// Why: setting `bm25` on its own is a footgun. `AppState::new` spawns the
822 /// indexer worker with no lane, so a caller that assigns the field and
823 /// nothing else gets a state whose reads use the lane and whose WRITES are
824 /// silently discarded by the placeholder worker — which is exactly what
825 /// `writes_through_the_tool_surface_survive_eviction` caught. Every path
826 /// that installs a lane goes through here so the two cannot drift apart.
827 /// What: rebuilds the bounded indexer channel + worker so the worker holds
828 /// the lane, then stores it. The placeholder worker installed by
829 /// `AppState::new` exits cleanly when the replaced sender closes its
830 /// receiver. Tests use this to pin explicit limits without mutating
831 /// process-global env vars.
832 /// Test: `tests/bm25_lane_concurrency.rs`, `bm25_lane_enabled_when_env_set`.
833 #[must_use]
834 pub fn with_bm25_lane(mut self, lane: Arc<bm25_lane::Bm25Lane>) -> Self {
835 // Issue #231: the bounded channel is what keeps a write burst from
836 // growing an unbounded task queue; rebuilding it here is what points
837 // its single worker at the lane.
838 let (tx, rx) =
839 tokio::sync::mpsc::channel::<tools::Bm25IndexRequest>(tools::BM25_INDEX_QUEUE_CAPACITY);
840 tools::spawn_bm25_index_worker(rx, Some(Arc::clone(&lane)), Arc::clone(&self.bm25_dirty));
841 self.bm25_index_tx = tx;
842 self.bm25 = Some(lane);
843 self
844 }
845
846 /// The BM25 lane, if one is installed.
847 ///
848 /// Why: the read half of the `pub(crate)` field above. Callers outside this
849 /// crate — the integration tests, and anything that wants to flush or query
850 /// the lane directly — need to reach it without being able to swap it for
851 /// one the indexer worker has never heard of.
852 /// Test: `tests/bm25_lane_concurrency.rs`, `tests/bm25_alias_write.rs`.
853 pub fn bm25_lane(&self) -> Option<&Arc<bm25_lane::Bm25Lane>> {
854 self.bm25.as_ref()
855 }
856
857 /// Scan the palace registry directory and re-register every persisted
858 /// palace into the in-memory [`PalaceRegistry`].
859 ///
860 /// Why: `AppState::new` builds an *empty* registry, so after a daemon
861 /// restart `palace_list` / the dashboard reported zero palaces even though
862 /// dozens existed on disk — palace metadata was persisted by
863 /// `palace_create` but never re-hydrated on startup. This method closes
864 /// that gap by walking the on-disk layout (each subdirectory holding a
865 /// `palace.json` is one palace) and rebuilding a live `PalaceHandle` for
866 /// each, so recall paths see the full set immediately after a restart.
867 /// What: runs the blocking filesystem walk + per-palace
868 /// `PalaceHandle::open_with_intent` on a `spawn_blocking` thread (so it
869 /// never stalls the async runtime), registers each successfully opened
870 /// palace via `register_arc`, logs every load at `debug!`, and returns the
871 /// count loaded. A palace that fails to open (corrupt index, unreadable
872 /// `kg.db`, etc.) is logged at `warn!` and skipped — one bad palace must not
873 /// abort startup or crash the daemon — but the skip is RECORDED (#4911) and
874 /// readable via [`PalaceRegistry::unopenable`], so a palace whose bytes
875 /// survive and whose contents cannot be read stays observable instead of
876 /// reading as absent. `data_root` is expected to already be the palace
877 /// registry directory — `main.rs` resolves it via
878 /// [`resolve_palace_registry_dir`] before constructing the `AppState`, so
879 /// the flat / legacy-`palaces/` layout difference is handled exactly once.
880 ///
881 /// Intent (#1487, #4911): every open uses the registry's own
882 /// `OpenIntent`, NOT the zero-arg `PalaceHandle::open` default. This is
883 /// the path a restarting daemon takes for every palace it already has on
884 /// disk, so hardcoding `ReadOnlyClient` here made the daemon's
885 /// `with_writer_intent()` guarantee false in the common case.
886 /// Test: `tests::load_palaces_from_disk_rehydrates_registry` writes two
887 /// palaces into a tempdir, constructs an `AppState`, calls this method, and
888 /// asserts the returned count and registry contents;
889 /// `load_palaces_from_disk_honours_registry_open_intent` covers the intent
890 /// contract in both directions and
891 /// `load_palaces_from_disk_records_an_unopenable_palace` the skip record.
892 pub async fn load_palaces_from_disk(&self) -> Result<usize> {
893 let registry_dir = self.data_root.clone();
894 let registry = self.registry.clone();
895 let palace_names = self.palace_names.clone();
896 // The directory walk and each `PalaceHandle::open` perform blocking
897 // filesystem + redb/usearch I/O — run the whole hydration on the
898 // blocking pool so it never parks an async worker thread.
899 let count = tokio::task::spawn_blocking(move || -> Result<usize> {
900 let palaces = PalaceRegistry::list_palaces(®istry_dir)?;
901 let total = palaces.len();
902 let mut loaded = 0usize;
903 let mut skipped = 0usize;
904 // #4911: hydrate under the registry's own intent, not the zero-arg
905 // `PalaceHandle::open` default (`ReadOnlyClient`).
906 let intent = registry.open_intent();
907 for palace in palaces {
908 match trusty_common::memory_core::PalaceHandle::open_with_intent(&palace, intent) {
909 Ok(handle) => {
910 tracing::debug!(
911 palace = %palace.id,
912 data_dir = %palace.data_dir.display(),
913 "loaded palace from disk"
914 );
915 // Issue #228: seed the in-memory name cache so write
916 // hot paths (memory_remember / memory_note) can resolve
917 // the friendly palace name without re-walking the data
918 // root. Insert here (during hydration) is the single
919 // point of truth for restart-time population.
920 palace_names.insert(palace.id.0.clone(), palace.name.clone());
921 registry.register_arc(handle);
922 loaded += 1;
923 }
924 Err(e) => {
925 // Why (issue #467): a single bad palace (corrupt kg.db,
926 // stale WAL, EMFILE — "Too many open files", permissions)
927 // must never abort startup or block the HTTP server from
928 // binding. Log per-palace and keep going; the summary
929 // below tells operators how many were skipped without
930 // trawling the log.
931 // The palace is NOT registered in the in-memory registry,
932 // so the next `open_palace` call for this id will attempt
933 // a fresh open from disk — the lazy-reopen path. If the
934 // root cause was EMFILE and the fd-limit fix (#462) raised
935 // the soft limit to 8192, that first request will succeed.
936 tracing::warn!(
937 palace = %palace.id,
938 data_dir = %palace.data_dir.display(),
939 "skipping palace during startup hydration: {e:#}; \
940 will retry lazily on first access"
941 );
942 // #4911: a skipped palace is absent from the handle
943 // cache, so record it or it reads as never having
944 // existed. Cleared by `register_arc` if it later opens.
945 registry.record_unopenable(palace.id.clone(), format!("{e:#}"));
946 skipped += 1;
947 }
948 }
949 }
950 tracing::info!(
951 "palace hydration summary: loaded {loaded}/{total} ({skipped} skipped due to errors)"
952 );
953 Ok(loaded)
954 })
955 .await
956 .map_err(|e| anyhow::anyhow!("join load_palaces_from_disk: {e}"))??;
957 Ok(count)
958 }
959
960 /// Builder-style: attach the daemon's shared `LogBuffer` so the
961 /// `GET /api/v1/logs/tail` endpoint serves the same lines the tracing
962 /// subscriber captures (issue #35).
963 ///
964 /// Why: `main` builds the buffer (via `init_tracing_with_buffer`) before
965 /// constructing the `AppState`, then hands a clone here so the HTTP
966 /// handler and the tracing layer observe the same ring.
967 /// What: replaces the empty default buffer with the supplied one.
968 /// Test: `rpc_logs_tail_answers_a_bounded_page`.
969 #[must_use]
970 pub fn with_log_buffer(mut self, buffer: trusty_common::log_buffer::LogBuffer) -> Self {
971 self.log_buffer = buffer;
972 self
973 }
974
975 /// Builder-style: mark this daemon as the sole palace writer so palace
976 /// redb files open with `OpenIntent::Writer` (issue #1487).
977 ///
978 /// Why: The HTTP daemon owns the write lock on every palace's `kg.redb`
979 /// and `index.usearch.redb`. Before this fix, when a *second* daemon
980 /// instance opened the same store it silently degraded to a read-only
981 /// snapshot and rejected every `memory_remember` for its lifetime —
982 /// effectively silent data loss when an MCP client routed a write to the
983 /// rogue instance. Opening as `Writer` makes the second instance fail
984 /// loud (after a short handoff-retry window that absorbs a graceful
985 /// launchd `bootout`→`bootstrap` overlap) instead of serving broken
986 /// reads-only. CLI, stdio-proxy, and test code paths never call this, so
987 /// they keep the snapshot read-fallback (issue #59).
988 /// What: Replaces `self.registry` with a fresh `PalaceRegistry` carrying
989 /// `OpenIntent::Writer`.
990 ///
991 /// Invariant: MUST be called on a fresh, unhydrated, unshared registry —
992 /// during startup, before `spawn_startup_tasks`/`load_palaces_from_disk`
993 /// registers any `PalaceHandle` and before the `AppState` (hence its
994 /// `Arc<PalaceRegistry>`) is cloned to a handler. Replacing the registry
995 /// discards the prior `Arc`; doing so after hydration would silently drop
996 /// live handles (data loss), and doing so after the state is shared would
997 /// leave other clones on the stale read-only registry. The guard is a
998 /// `debug_assert!` on the strongest cheap signals the registry exposes —
999 /// `is_empty()` (no handles hydrated) and `Arc::strong_count == 1` (not yet
1000 /// shared) — so an ordering violation fails fast as the programmer error it
1001 /// is (the call site is startup-only and fixed). Release builds elide the
1002 /// assert; the real call site (`run_serve`) always satisfies it.
1003 /// Test: `with_writer_intent_marks_registry_writer` and
1004 /// `with_writer_intent_panics_on_hydrated_registry` in `lib_tests`.
1005 #[must_use]
1006 pub fn with_writer_intent(mut self) -> Self {
1007 // Fail fast on an ordering bug: a hydrated registry (`!is_empty`) or a
1008 // shared one (`strong_count > 1`) would silently drop live handles or
1009 // strand other clones on the stale read-only registry (issue #1487).
1010 debug_assert!(self.registry.is_empty() && Arc::strong_count(&self.registry) == 1);
1011 // Idle-to-disk: preserve the configurable open-handle cap
1012 // (TRUSTY_MEMORY_MAX_OPEN_PALACES) while marking the registry a writer.
1013 self.registry = Arc::new(PalaceRegistry::from_env().with_writer_intent());
1014 self
1015 }
1016
1017 /// Builder-style: attach the bug-capture `ErrorStore` handle (bug-reporting #478).
1018 ///
1019 /// Why: Phase 2 MCP / HTTP endpoints need a handle to the in-memory error
1020 /// ring so they can serve `recent_errors` / `errors_by_fingerprint`
1021 /// without disk I/O on the hot path. Installing it here — rather than
1022 /// adding it as a separate global — keeps the state graph explicit and
1023 /// lets tests skip it by never calling this method.
1024 /// What: stores `Some(store)` in `AppState::error_store`; the `BugCaptureLayer`
1025 /// that writes to this store is already installed in the tracing
1026 /// subscriber by `init_tracing_with_buffer_and_capture`. The store is
1027 /// `Clone` (cheap `Arc` clone internally) so both the layer and this
1028 /// field share the same underlying ring.
1029 /// Test: Phase 2 will add `error_store_captures_and_queries` in `web.rs`.
1030 #[must_use]
1031 pub fn with_error_store(mut self, store: trusty_common::error_capture::ErrorStore) -> Self {
1032 self.error_store = Some(store);
1033 self
1034 }
1035
1036 /// Builder-style: opt into multi-tenant authorization mode (issue #1714).
1037 ///
1038 /// Why: mirrors `with_bm25_client_from_env`'s pattern of keeping env-var
1039 /// gating in one place. Unset (the default) preserves today's
1040 /// single-tenant behaviour with zero change for existing callers. Issue
1041 /// #2522 review: activation is silent otherwise, which makes a
1042 /// misconfigured (or unexpectedly enabled) deployment hard to diagnose
1043 /// from logs alone — log once at startup when the mode flips on.
1044 /// What: sets `multi_tenant_mode` from `TRUSTY_MEMORY_MULTI_TENANT=1`; see
1045 /// the `authz` module for what the flag then enforces. Logs via
1046 /// `tracing::info!` (stderr only) when enabled; stays silent when
1047 /// disabled (the default).
1048 /// Test: `authorize_force_palace_create_denies_multi_tenant_without_capability`.
1049 #[must_use]
1050 pub fn with_multi_tenant_mode_from_env(mut self) -> Self {
1051 self.multi_tenant_mode = std::env::var("TRUSTY_MEMORY_MULTI_TENANT").as_deref() == Ok("1");
1052 if self.multi_tenant_mode {
1053 tracing::info!(
1054 "multi-tenant mode enabled (TRUSTY_MEMORY_MULTI_TENANT=1): force=true palace_create will be refused"
1055 );
1056 }
1057 self
1058 }
1059
1060 /// Send a `DaemonEvent` to all connected SSE subscribers and persist
1061 /// it to the activity log when the variant carries a source.
1062 ///
1063 /// Why: Mutating handlers call this after a successful write so the
1064 /// dashboard can update without polling. The send is best-effort —
1065 /// `broadcast::Sender::send` returns `Err` only when there are no live
1066 /// receivers, which is fine (no listeners == no work to do). Issue
1067 /// #96 additionally writes the entry to the persistent activity log
1068 /// so the feed can serve historical rows on page load and so MCP /
1069 /// HTTP / Hook origins are visible to the operator. Persistence is
1070 /// also best-effort — a write failure is logged but never blocks the
1071 /// SSE broadcast.
1072 ///
1073 /// Issue #232: the activity-log append is a synchronous redb write +
1074 /// fsync. Calling it directly on the async caller's task parked a tokio
1075 /// worker thread on disk I/O for every SSE event. We now offload the
1076 /// append to the blocking thread pool via `spawn_blocking` and return
1077 /// immediately — `emit` stays synchronous so every existing caller
1078 /// (including the sync `dispatch_hook_fired` JSON-RPC handler) keeps
1079 /// compiling unchanged. The fire-and-forget pattern matches the
1080 /// pre-fix semantics (best-effort, never blocks the SSE broadcast)
1081 /// while freeing the async runtime to do real work during the write.
1082 /// What: serialises the event for the log (skipping `StatusChanged`
1083 /// which is a recomputed aggregate, not a mutation), spawns the redb
1084 /// append on `tokio::task::spawn_blocking` keyed by a clone of the
1085 /// `Arc<ActivityLog>` and the cloned event, then sends the event over
1086 /// the broadcast channel. A `pending_activity_writes` counter is bumped
1087 /// before the spawn and decremented inside the closure so
1088 /// [`Self::flush_activity_writes`] can drain in tests.
1089 /// Test: `web::tests::sse_stream_receives_palace_created` confirms a
1090 /// subscriber observes the emitted event;
1091 /// `activity_endpoint_lists_recent_emits` confirms persistence via
1092 /// `flush_activity_writes`.
1093 pub fn emit(&self, event: DaemonEvent) {
1094 if let Some(source) = event.source() {
1095 let event_type = event.type_str();
1096 let palace_id = event.palace_id().map(|s| s.to_string());
1097 let log = Arc::clone(&self.activity_log);
1098 let event_for_log = event.clone();
1099 let pending = Arc::clone(&self.pending_activity_writes);
1100 // Pre-allocate the sequence id in the emitting thread so the
1101 // persisted order matches the emission order even when blocking-pool
1102 // workers execute the writes concurrently (issue #247). Without
1103 // this, four rapid emits would assign IDs inside their respective
1104 // `spawn_blocking` closures in a non-deterministic order.
1105 let id = log.alloc_id();
1106 pending.fetch_add(1, Ordering::SeqCst);
1107 // Why: the synchronous redb append + fsync must not park an
1108 // async worker thread (issue #232). Spawn the write on the
1109 // blocking pool; the JoinHandle is intentionally dropped —
1110 // the write is best-effort and any failure is logged below.
1111 tokio::task::spawn_blocking(move || {
1112 let result = log.append_with_id(id, source, palace_id, event_type, &event_for_log);
1113 if let Err(e) = result {
1114 tracing::warn!("activity_log.append failed for {event_type}: {e:#}");
1115 }
1116 pending.fetch_sub(1, Ordering::SeqCst);
1117 });
1118 }
1119 let _ = self.events.send(event);
1120 }
1121
1122 /// Block (asynchronously) until every in-flight activity-log write
1123 /// spawned by [`Self::emit`] has settled.
1124 ///
1125 /// Why: `emit` offloads its redb append to `tokio::task::spawn_blocking`
1126 /// and returns immediately (issue #232). Tests that observe the
1127 /// activity log right after a burst of emits would otherwise race the
1128 /// blocking-pool worker; this helper gives them a deterministic
1129 /// synchronization point. Production code never needs to call this —
1130 /// the dashboard reads through `GET /api/v1/activity`, which already
1131 /// tolerates writes settling asynchronously.
1132 /// What: spins on `pending_activity_writes` with a 1 ms yield until the
1133 /// counter is zero. Cheap: tests typically emit a handful of events
1134 /// and the loop exits within a single scheduler tick.
1135 /// Test: covered indirectly by `emit_persists_mutations_but_skips_status_changed`
1136 /// and `web::tests::activity_endpoint_lists_recent_emits`.
1137 pub async fn flush_activity_writes(&self) {
1138 while self.pending_activity_writes.load(Ordering::SeqCst) > 0 {
1139 tokio::time::sleep(std::time::Duration::from_millis(1)).await;
1140 }
1141 }
1142
1143 /// Open (or return cached) the chat-session store for a palace.
1144 ///
1145 /// Why: Chat session persistence lives in a dedicated redb file under
1146 /// the palace's data dir (`chat_sessions.redb`) so it doesn't intermingle
1147 /// with the KG's transactional load. The store is cheap to clone via
1148 /// `Arc` but the underlying connection should be reused, so cache by id.
1149 /// What: delegates to the LRU-bounded [`SessionStoreCache`], which creates
1150 /// the palace data dir if missing, opens (or reuses) a `ChatSessionStore`,
1151 /// and evicts cold, *unused* stores once more than the cap are resident.
1152 /// Callers keep the returned `Arc` for as long as they need it — eviction
1153 /// never closes a store someone still holds.
1154 /// Test: `session_store_cache::tests::open_handles_are_bounded_by_cap`,
1155 /// `session_store_cache::tests::in_use_store_is_never_evicted`; the call
1156 /// path is covered indirectly by the session HTTP handlers in `web::tests`.
1157 pub fn session_store(&self, palace_id: &str) -> Result<Arc<ChatSessionStore>> {
1158 // #4639: bounded cache replaces the unbounded, never-evicting DashMap.
1159 self.session_stores
1160 .get_or_open(palace_id, &self.data_root.join(palace_id))
1161 }
1162
1163 /// Builder-style setter for the default palace name.
1164 ///
1165 /// Why: `serve --palace <name>` wants to bind every tool call to a
1166 /// project-scoped namespace without forcing every MCP request to repeat
1167 /// the palace argument.
1168 /// What: Returns `self` with `default_palace = Some(name)`.
1169 /// Test: `default_palace_used_when_arg_omitted` covers the resolution
1170 /// path; this setter is exercised there.
1171 pub fn with_default_palace(mut self, name: Option<String>) -> Self {
1172 self.default_palace = name;
1173 self
1174 }
1175
1176 /// Resolve (or initialize) the shared embedder.
1177 ///
1178 /// Why: FastEmbedder load is expensive — we share one instance across all
1179 /// tool calls; the `OnceCell` ensures concurrent first-use races collapse
1180 /// to a single load.
1181 /// What: Returns `Arc<FastEmbedder>` on success. Errors propagate from the
1182 /// underlying ONNX load.
1183 /// Test: Indirectly via `dispatch_remember_then_recall`.
1184 /// Resolve the active chat provider, auto-detecting on first call.
1185 ///
1186 /// Why: Provider selection depends on filesystem-loaded config plus a
1187 /// network probe (Ollama liveness), so it must be lazily initialised at
1188 /// runtime. Caching the choice in a `OnceCell` keeps it stable across
1189 /// concurrent requests without re-probing on every chat call.
1190 /// What: On first use loads `~/.trusty-memory/config.toml`, prefers an
1191 /// auto-detected Ollama instance (when `local_model.enabled`), and falls
1192 /// back to OpenRouter when an API key is set. Returns `None` when neither
1193 /// is available, so `memory.chat` can refuse before opening a stream.
1194 /// Test: `rpc_chat_providers_answers_both_upstreams` covers the detection
1195 /// path indirectly through `memory.chat_providers`.
1196 pub async fn chat_provider(&self) -> Option<Arc<dyn ChatProvider>> {
1197 self.chat_provider
1198 .get_or_init(|| async {
1199 // Why (issue #226): `service::load_user_config` is the
1200 // canonical home of the loader; the former
1201 // re-export only exists for the HTTP handlers. Going
1202 // direct to `service` keeps this method usable when
1203 // the `daemon` feature is disabled.
1204 let cfg = crate::service::load_user_config().unwrap_or_default();
1205 if cfg.local_model.enabled {
1206 if let Some(mut p) =
1207 trusty_common::auto_detect_local_provider(&cfg.local_model.base_url).await
1208 {
1209 // auto_detect returns an empty model id; callers must
1210 // set the configured model name themselves.
1211 p.model = cfg.local_model.model.clone();
1212 return Some(Arc::new(p) as Arc<dyn ChatProvider>);
1213 }
1214 }
1215 if !cfg.openrouter_api_key.is_empty() {
1216 return Some(Arc::new(trusty_common::OpenRouterProvider::new(
1217 cfg.openrouter_api_key,
1218 cfg.openrouter_model,
1219 )) as Arc<dyn ChatProvider>);
1220 }
1221 None
1222 })
1223 .await
1224 .clone()
1225 }
1226
1227 /// Spawn a fire-and-forget background task that auto-discovers project
1228 /// aliases under `project_root` and asserts new ones into `palace`.
1229 ///
1230 /// Why (issue #42): Projects carry implicit shorthand — cargo package
1231 /// names that differ from their directory, binary names that differ
1232 /// from packages, first-letter abbreviations — that should be surfaced
1233 /// without a user ever calling `add_alias`. Running discovery as a
1234 /// detached task on palace-open keeps startup latency unchanged: the
1235 /// daemon binds and starts serving immediately while the discovery scan
1236 /// completes in the background, and any newly-asserted aliases land in
1237 /// the prompt cache before the model's next `get_prompt_context` call.
1238 /// What: clones `self` (cheap; `Arc`-backed), spawns a tokio task that
1239 /// invokes the `discover_aliases` tool handler directly so the
1240 /// dedup + cache-rebuild logic runs exactly the same path as the MCP
1241 /// tool call. Errors are logged at `warn!`; one failed discovery never
1242 /// destabilises the daemon.
1243 /// Test: not unit-tested (timing-dependent fire-and-forget); the
1244 /// underlying `discover_aliases` dispatch is covered by
1245 /// `dispatch_discover_aliases_inserts_new_and_dedupes` in `tools::tests`.
1246 pub fn spawn_alias_discovery(&self, palace: String, project_root: PathBuf) {
1247 let state = self.clone();
1248 tokio::spawn(async move {
1249 let args = serde_json::json!({
1250 "palace": palace,
1251 "project_root": project_root.to_string_lossy(),
1252 });
1253 match tools::dispatch_tool(&state, "discover_aliases", args).await {
1254 Ok(result) => tracing::info!(
1255 new = ?result.get("new"),
1256 already_known = ?result.get("already_known"),
1257 "alias discovery complete"
1258 ),
1259 Err(e) => tracing::warn!("alias discovery failed: {e:#}"),
1260 }
1261 });
1262 }
1263
1264 /// Return the current readiness state.
1265 ///
1266 /// Why: tool handlers and the `/health` endpoint need a cheap, lock-free
1267 /// way to check whether the embedder has been initialised yet.
1268 ///
1269 /// What: loads `daemon_readiness` with `Acquire` ordering so the caller
1270 /// sees all writes the startup task made before setting the state.
1271 ///
1272 /// #4836: the value read here is only as good as the writes that reach it.
1273 /// It used to be written exactly once, by the startup warm-up task, so a
1274 /// single failed attempt pinned the daemon at `Warming` for the rest of its
1275 /// life. [`AppState::embedder`] now also flips it on success, which is the
1276 /// signal that actually proves a vector search can run.
1277 /// Test: `daemon_readiness_transitions_warming_to_ready`,
1278 /// `resolving_the_embedder_marks_a_warming_daemon_ready`.
1279 pub fn readiness(&self) -> DaemonReadiness {
1280 DaemonReadiness::from_u8(self.daemon_readiness.load(Ordering::Acquire))
1281 }
1282
1283 /// Flip the readiness state from `Warming` to `Ready`.
1284 ///
1285 /// Why: called by `spawn_startup_tasks` in `main.rs` once the embedder
1286 /// warm-up succeeds — this is the single state-transition site.
1287 /// What: `store(Ready, Release)` so subsequent `Acquire` loads in handlers
1288 /// observe a consistent state. Idempotent: calling it multiple times is
1289 /// harmless.
1290 /// Test: `daemon_readiness_transitions_warming_to_ready`.
1291 pub fn set_ready(&self) {
1292 self.daemon_readiness
1293 .store(DaemonReadiness::Ready as u8, Ordering::Release);
1294 }
1295
1296 /// Obtain the shared `FastEmbedder` instance, initialising it on first call.
1297 ///
1298 /// Why: centralises lazy embedder access so every tool handler goes through
1299 /// one bounded init path (tracks #910 internally).
1300 /// What: wraps `OnceCell::get_or_try_init` with a timeout so a slow
1301 /// CoreML/CUDA first-compile cannot block a handler indefinitely. On
1302 /// timeout the `OnceCell` is left unresolved and the next caller retries.
1303 ///
1304 /// **Callers on the request path SHOULD check `readiness()` before this
1305 /// method** (issue #1970) — every recall handler now checks
1306 /// `readiness() == Ready` first and only calls `embedder()` on that
1307 /// branch, falling back to a BM25/L0/L1-only path while `Warming` instead
1308 /// of paying this method's cold-init cost. Reaching this method while
1309 /// still `Warming` is not a bug (the warm-up task itself calls
1310 /// `embedder()` while in `Warming` state), just unusual on the request
1311 /// path.
1312 ///
1313 /// This timeout is a backstop against a pathological init delay (e.g. the
1314 /// warm-up task's own call, or a handler that skips the `readiness()`
1315 /// check). If this timeout fires the `OnceCell` is left in the unresolved
1316 /// state and the next call retries from scratch.
1317 pub async fn embedder(&self) -> Result<Arc<dyn Embedder + Send + Sync>> {
1318 // #4836: delegate to the ONE process-wide cell instead of a private
1319 // second one, so initialising the embedder here is the same event the
1320 // startup warm-up latches readiness off. `shared_embedder` already
1321 // applies the bounded `TRUSTY_EMBEDDER_INIT_TIMEOUT_SECS` init timeout
1322 // and the CoreML auto-fallback, so no wrapper timeout is needed here.
1323 let embedder = trusty_common::memory_core::retrieval::shared_embedder().await?;
1324 // #4836: a resolved embedder is proof a vector search can run, so it is
1325 // the authoritative readiness signal — not the startup task's one-shot
1326 // attempt. Without this the daemon stays `Warming` forever whenever that
1327 // single attempt failed, and every MCP recall serves the degraded
1328 // L0/L1 fallback that ignores the query entirely.
1329 self.set_ready();
1330 Ok(embedder)
1331 }
1332}
1333
1334impl std::fmt::Debug for AppState {
1335 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1336 f.debug_struct("AppState")
1337 .field("version", &self.version)
1338 .field("data_root", &self.data_root)
1339 .field("registry_len", &self.registry.len())
1340 .finish()
1341 }
1342}
1343
1344/// Handle a single MCP JSON-RPC message and produce its response.
1345///
1346/// Why: Pulled out of the stdio loop so unit tests can drive every method
1347/// without touching real stdin/stdout.
1348/// What: Routes `initialize`, `tools/list`, `tools/call`, `ping`, and the
1349/// `notifications/initialized` notification (which returns `Value::Null`).
1350/// Test: See unit tests below — initialize/list/call all return expected
1351/// JSON-RPC envelopes; notifications return `Null` (no response written).
1352pub async fn handle_message(state: &AppState, msg: Value) -> Value {
1353 let id = msg.get("id").cloned().unwrap_or(Value::Null);
1354 let method = msg.get("method").and_then(|m| m.as_str()).unwrap_or("");
1355
1356 match method {
1357 "initialize" => {
1358 let extra = state
1359 .default_palace
1360 .as_ref()
1361 .map(|dp| json!({ "default_palace": dp }));
1362 let result = initialize_response("trusty-memory", &state.version, extra);
1363 // Why (issue #42): prompt-facts now flow through the
1364 // per-message `get_prompt_context` tool rather than MCP
1365 // prompts, so we no longer advertise the `prompts` capability.
1366 json!({
1367 "jsonrpc": "2.0",
1368 "id": id,
1369 "result": result,
1370 })
1371 }
1372 // Notifications must NOT receive a response.
1373 "notifications/initialized" | "notifications/cancelled" => Value::Null,
1374 "tools/list" => json!({
1375 "jsonrpc": "2.0",
1376 "id": id,
1377 "result": tools::tool_definitions_with(state.default_palace.is_some())
1378 }),
1379 // OpenRPC 1.3.2 discovery — see `openrpc.rs`. Returns the full
1380 // service description so orchestrators (trusty-agents, etc.) can
1381 // introspect every tool and its required `memory.read`/`memory.write`
1382 // scope without bespoke per-server adapters.
1383 "rpc.discover" => json!({
1384 "jsonrpc": "2.0",
1385 "id": id,
1386 "result": openrpc::build_discover_response(
1387 &state.version,
1388 state.default_palace.is_some(),
1389 ),
1390 }),
1391 "tools/call" => {
1392 let params = msg.get("params").cloned().unwrap_or_default();
1393 let tool_name = params
1394 .get("name")
1395 .and_then(|n| n.as_str())
1396 .unwrap_or("")
1397 .to_string();
1398 let args = params.get("arguments").cloned().unwrap_or_default();
1399 match tools::dispatch_tool(state, &tool_name, args).await {
1400 Ok(content) => {
1401 // Why: tools that return a bare JSON string (e.g.
1402 // `get_prompt_context` returning the formatted
1403 // Markdown block) should surface as plain text in the
1404 // MCP `content[0].text` field — wrapping in
1405 // `Value::to_string()` would re-quote the payload and
1406 // force every caller to strip outer quotes.
1407 let text = match &content {
1408 Value::String(s) => s.clone(),
1409 other => other.to_string(),
1410 };
1411 json!({
1412 "jsonrpc": "2.0",
1413 "id": id,
1414 "result": {
1415 "content": [{"type": "text", "text": text}]
1416 }
1417 })
1418 }
1419 Err(e) => json!({
1420 "jsonrpc": "2.0",
1421 "id": id,
1422 // Why: anyhow's `{:#}` alternate format walks the full
1423 // `Caused by:` chain so MCP clients see actionable
1424 // detail (e.g. "PalaceHandle::remember_with_options:
1425 // filter rejected: too short") instead of just the
1426 // outermost context label.
1427 "error": {"code": -32603, "message": format!("{e:#}")}
1428 }),
1429 }
1430 }
1431 "ping" => json!({"jsonrpc": "2.0", "id": id, "result": {}}),
1432 _ => json!({
1433 "jsonrpc": "2.0",
1434 "id": id,
1435 "error": {
1436 "code": -32601,
1437 "message": format!("Method not found: {method}")
1438 }
1439 }),
1440 }
1441}
1442
1443#[cfg(test)]
1444mod lib_tests;