Skip to main content

trusty_code/session/
registry_events.rs

1//! `SessionRegistry`'s #2055 tool/log/progress/message event-recording
2//! plumbing. Split out of `registry.rs` (#2344) purely to keep that
3//! production file under the crate's 500-SLOC cap as the #2344
4//! persistent-transcript methods grew it past the limit — this is a child
5//! module of `registry` (declared via `#[path = ...] mod events;`), so it
6//! shares full access to `SessionRegistry`'s private `lock`/`record` helpers
7//! exactly as if these methods were still defined in that file.
8//!
9//! Why: gives the agent loop a stable, already-wired call for each #2055
10//! event kind without it needing to touch the ring buffer, sequencing, or
11//! bus directly.
12//! What: `record_tool_started`/`record_tool_finished`/`record_tool_error`/
13//! `record_log`/`record_progress`/`record_message`, plus the shared
14//! `ensure_exists` existence guard they all call before recording.
15//! Test: `registry_tests::record_tool_started_publishes_event`,
16//! `registry_tests::record_tool_finished_publishes_event`,
17//! `registry_tests::record_tool_error_publishes_event`,
18//! `registry_tests::record_log_publishes_event`,
19//! `registry_tests::record_progress_publishes_event`,
20//! `registry_tests::record_message_publishes_event`.
21
22use super::*;
23
24impl SessionRegistry {
25    /// Record a tool invocation starting (#2055 emission plumbing for
26    /// #2056's agent loop).
27    ///
28    /// Why: gives the future agent loop a stable, already-wired call for
29    /// `ToolStarted` without it needing to touch the ring buffer, sequencing,
30    /// or bus directly.
31    /// What: errors with `session_not_found` if `id` is unknown; `args_preview`
32    /// is truncated via `crate::events::preview` before emission so a large
33    /// argument payload can't blow the ring buffer / wire size.
34    /// Test: `registry_tests::record_tool_started_publishes_event`.
35    pub fn record_tool_started(
36        &self,
37        id: &str,
38        tool: &str,
39        call_id: &str,
40        args: &str,
41    ) -> Result<(), RpcError> {
42        self.ensure_exists(id)?;
43        self.record(
44            id,
45            Event::ToolStarted {
46                session_id: id.to_string(),
47                tool: tool.to_string(),
48                call_id: call_id.to_string(),
49                args_preview: crate::events::preview(args, 500),
50            },
51        );
52        Ok(())
53    }
54
55    /// Record a tool invocation finishing (#2055 emission plumbing for
56    /// #2056's agent loop). See [`Self::record_tool_started`].
57    /// Test: `registry_tests::record_tool_finished_publishes_event`.
58    pub fn record_tool_finished(
59        &self,
60        id: &str,
61        tool: &str,
62        call_id: &str,
63        success: bool,
64        result: &str,
65    ) -> Result<(), RpcError> {
66        self.ensure_exists(id)?;
67        self.record(
68            id,
69            Event::ToolFinished {
70                session_id: id.to_string(),
71                tool: tool.to_string(),
72                call_id: call_id.to_string(),
73                success,
74                result_preview: crate::events::preview(result, 500),
75            },
76        );
77        Ok(())
78    }
79
80    /// Record a tool invocation raising an exceptional error (#2055 emission
81    /// plumbing for #2056's agent loop). See [`Self::record_tool_started`].
82    /// Test: `registry_tests::record_tool_error_publishes_event`.
83    pub fn record_tool_error(
84        &self,
85        id: &str,
86        tool: &str,
87        call_id: &str,
88        error: &str,
89    ) -> Result<(), RpcError> {
90        self.ensure_exists(id)?;
91        self.record(
92            id,
93            Event::ToolError {
94                session_id: id.to_string(),
95                tool: tool.to_string(),
96                call_id: call_id.to_string(),
97                error: error.to_string(),
98            },
99        );
100        Ok(())
101    }
102
103    /// Record a session-scoped diagnostic log line (#2055 emission plumbing).
104    /// Test: `registry_tests::record_log_publishes_event`.
105    pub fn record_log(&self, id: &str, level: &str, message: &str) -> Result<(), RpcError> {
106        self.ensure_exists(id)?;
107        self.record(
108            id,
109            Event::Log {
110                session_id: id.to_string(),
111                level: level.to_string(),
112                message: message.to_string(),
113            },
114        );
115        Ok(())
116    }
117
118    /// Record a coarse progress update (#2055 emission plumbing).
119    /// Test: `registry_tests::record_progress_publishes_event`.
120    pub fn record_progress(
121        &self,
122        id: &str,
123        message: &str,
124        percent: Option<f32>,
125    ) -> Result<(), RpcError> {
126        self.ensure_exists(id)?;
127        self.record(
128            id,
129            Event::Progress {
130                session_id: id.to_string(),
131                message: message.to_string(),
132                percent,
133            },
134        );
135        Ok(())
136    }
137
138    /// Record a generic freeform session message (#2055 emission plumbing).
139    /// Test: `registry_tests::record_message_publishes_event`.
140    pub fn record_message(&self, id: &str, text: &str) -> Result<(), RpcError> {
141        self.ensure_exists(id)?;
142        self.record(
143            id,
144            Event::Message {
145                session_id: id.to_string(),
146                text: text.to_string(),
147            },
148        );
149        Ok(())
150    }
151
152    /// Return `Ok(())` if `id` exists, `Err(session_not_found)` otherwise.
153    ///
154    /// Why: every `record_*` plumbing method needs the same existence guard
155    /// before recording; centralising it keeps each one a two-line body.
156    fn ensure_exists(&self, id: &str) -> Result<(), RpcError> {
157        if self.lock().contains_key(id) {
158            Ok(())
159        } else {
160            Err(RpcError::session_not_found(id))
161        }
162    }
163}