trusty_code/session/registry_memory_sink.rs
1//! `SessionRegistry`'s #2345 turn-recorder sink lazy-init/lookup. Split out
2//! of `registry.rs` for the same 500-SLOC-cap reason `registry_events.rs`
3//! was split out (#2344) — a child module of `registry` (declared via
4//! `#[path = ...] mod memory_sink_ext;`), so it shares full access to
5//! `SessionRegistry`'s private `lock` helper and `SessionEntry`'s private
6//! `memory_sink` field exactly as if this method were still defined in
7//! `registry.rs`.
8//!
9//! Why: the turn recorder's sink (and its background drain task) must be
10//! constructed exactly ONCE per session — see the `memory_sink` module's own
11//! docs — not once per `task.run`; this is the single lazy-init/lookup entry
12//! point `task::executor::run_and_record` calls at each run's turn boundary.
13//! What: [`SessionRegistry::memory_sink_for`].
14//! Test: `registry_tests::memory_sink_for_reuses_the_same_sink_across_calls`,
15//! `registry_tests::memory_sink_for_unknown_session_returns_none`.
16
17use std::path::Path;
18
19use crate::session::memory_sink::{TurnMemorySink, derive_palace_id_for_project};
20
21use super::*;
22
23impl SessionRegistry {
24 /// Lazily construct (on first call) or return (on every later call) this
25 /// session's durable turn-recorder sink.
26 ///
27 /// Why: `task::executor::run_and_record` needs the SAME
28 /// `Arc<TurnMemorySink>` — and therefore the same background drain task
29 /// and bounded queue — across every `task.run` on one session, not a
30 /// fresh one per run (a fresh sink per run would spawn a new drain task
31 /// each time, multiplying in-flight writers with no bound). Constructing
32 /// it here, inside the session-map lock, means two calls can never build
33 /// two different sinks for the same session even under a hypothetical
34 /// race (in practice `Self::begin_execution`'s single-in-flight-run guard
35 /// already prevents concurrent calls per session).
36 /// What: `None` if `id` is unknown (best-effort — mirrors
37 /// `Self::set_run_outcome`'s framing; the caller has already validated
38 /// the session's existence earlier in the same run via
39 /// `begin_execution`/`begin_pm_transcript`). On the FIRST call for a
40 /// session, derives the palace id from `project_dir` via
41 /// [`derive_palace_id_for_project`], resolves trusty-memory's base URL
42 /// via `trusty_common::mcp::memory_rpc::resolve_memory_base_url_or_unreachable`
43 /// (fail-open — the sink is built regardless of whether the daemon is
44 /// currently reachable; every write attempt after that is independently
45 /// fail-open, see `memory_sink::write_turn`), and stores the constructed
46 /// `Arc`. On every SUBSEQUENT call, `project_dir` is ignored and the
47 /// already-built sink is returned unchanged — a session's palace/base-url
48 /// binding is fixed for its lifetime, mirroring
49 /// `Self::begin_pm_transcript`'s analogous "the system prompt is fixed
50 /// after the first call" rule.
51 /// Test: `registry_tests::memory_sink_for_reuses_the_same_sink_across_calls`,
52 /// `registry_tests::memory_sink_for_unknown_session_returns_none`.
53 pub fn memory_sink_for(&self, id: &str, project_dir: &Path) -> Option<Arc<TurnMemorySink>> {
54 let mut sessions = self.lock();
55 let entry = sessions.get_mut(id)?;
56 if entry.memory_sink.is_none() {
57 let palace = derive_palace_id_for_project(project_dir);
58 let base_url = trusty_common::mcp::memory_rpc::resolve_memory_base_url_or_unreachable();
59 entry.memory_sink = Some(Arc::new(TurnMemorySink::new(base_url, palace)));
60 }
61 entry.memory_sink.clone()
62 }
63}