trusty_code/session/registry_transcript.rs
1//! `SessionRegistry::get_transcript` (#2058), split out of `registry.rs` for
2//! the 500-SLOC cap — a child module of `registry` (declared via
3//! `#[path = ...] mod transcript_ops;`), so it keeps full access to
4//! `SessionRegistry`'s private `lock` helper and `SessionEntry`'s private
5//! fields exactly as if the method were still defined there.
6//!
7//! Why: #2425 added `SessionEntry::memory_durability`/`memory_outcomes` and the
8//! reconciler module declaration, which pushed `registry.rs` over its cap. This
9//! projection is the natural thing to move: it reads `SessionEntry` and builds
10//! a DTO, touching none of the registry's own state machine.
11//! What: [`SessionRegistry::get_transcript`].
12//! Test: `registry_tests::get_transcript_*`.
13
14use super::*;
15use crate::session::transcript::TranscriptRecord;
16
17impl SessionRegistry {
18 /// `session.get_transcript`: fetch the stored run record for a session
19 /// (#2058).
20 ///
21 /// Why: [`Self::set_run_outcome`] populates the storage; this is its read
22 /// counterpart — the M1 cut-line's "inspect transcript" verb. A never-run
23 /// session is a valid, empty transcript, not an error: `SessionEntry`'s
24 /// `transcript`/`usage`/`cost_usd` fields already default to
25 /// empty/zero/`None` at `create` time, so returning them unconditionally
26 /// (once the session itself is confirmed to exist) is correct with no
27 /// extra branching.
28 /// What: `Err(session_not_found)` if `id` is unknown; otherwise a clone of
29 /// whatever is currently stored, wrapped in a self-describing
30 /// [`TranscriptRecord`]. Never recomputes cost, usage, or durability —
31 /// exposes exactly what [`Self::set_run_outcome`] and (#2425)
32 /// [`Self::record_memory_durability`] last stored. `compaction_events`
33 /// (#2349) reads `entry.pm_transcript`'s own cumulative counter — `0` when
34 /// the session has never run (`pm_transcript` still `None`).
35 /// Test: `registry_tests::get_transcript_returns_stored_record`,
36 /// `registry_tests::get_transcript_on_never_run_session_is_empty`,
37 /// `registry_tests::get_transcript_unknown_session_errors`,
38 /// `registry_tests::get_transcript_reports_compaction_events`,
39 /// `registry_tests::get_transcript_round_trips_goal_state`,
40 /// `registry_tests::memory_durability_retains_counts_resets_streak_and_warns_at_one_and_three`.
41 pub fn get_transcript(&self, id: &str) -> Result<TranscriptRecord, RpcError> {
42 let sessions = self.lock();
43 let entry = sessions
44 .get(id)
45 .ok_or_else(|| RpcError::session_not_found(id))?;
46 Ok(TranscriptRecord {
47 session_id: id.to_string(),
48 turns: entry.transcript.clone(),
49 usage: entry.usage,
50 cost_usd: entry.cost_usd,
51 mode: entry.session.mode,
52 compaction_events: entry
53 .pm_transcript
54 .as_ref()
55 .map(Transcript::compaction_events)
56 .unwrap_or(0),
57 goals: goal_ops::goal_records(entry),
58 memory_durability: entry.memory_durability.clone(),
59 })
60 }
61}