Skip to main content

trusty_code/session/
registry_task_result.rs

1//! `SessionRegistry::set_task_result` (#4351), split out of `registry.rs` for
2//! the 500-SLOC cap — a child module of `registry` (declared via
3//! `#[path = ...] mod task_result_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: `registry.rs` sits within a few lines of its cap; the same reason
8//! `registry_transcript.rs` and `registry_events.rs` exist.
9//! What: [`SessionRegistry::set_task_result`] and the `session.cancel`
10//! convenience [`SessionRegistry::set_cancelled_result`].
11//! Test: `registry_tests::set_task_result_stores_the_result`,
12//! `registry_tests::set_task_result_on_unknown_session_is_a_no_op`,
13//! `registry_tests::cancel_records_a_refless_cancelled_result`.
14
15use super::*;
16use crate::session::task_result::{TaskResult, TaskResultStatus};
17
18impl SessionRegistry {
19    /// Record this session's actionable run result (#4351).
20    ///
21    /// Why: `session.status` must be able to hand a caller the branch/ref/
22    /// pass-fail story, not just an opaque id — see
23    /// `crate::session::task_result` for why. `crate::task::executor` is the
24    /// only production caller; it fires exactly once, immediately before
25    /// `finish`, so the result and the terminal status become visible in the
26    /// same snapshot.
27    /// What: overwrites `Session::result` wholesale. A session that has
28    /// vanished (never created, or already pruned) is a silent no-op — the
29    /// same contract [`Self::set_run_outcome`] uses, and for the same reason:
30    /// this is called from a detached task that cannot meaningfully handle
31    /// "the session went away while I was running".
32    /// Test: `registry_tests::set_task_result_stores_the_result`,
33    /// `registry_tests::set_task_result_on_unknown_session_is_a_no_op`.
34    pub fn set_task_result(&self, id: &str, result: TaskResult) {
35        let mut sessions = self.lock();
36        if let Some(entry) = sessions.get_mut(id) {
37            entry.session.result = Some(result);
38        }
39    }
40
41    /// Record the result for a user-initiated `session.cancel` (#4351).
42    ///
43    /// Why: [`Self::cancel`] transitions a session straight to `Cancelled`
44    /// without going through `crate::task::executor`, so without this a
45    /// cancelled session was the ONE terminal state answering `session.status`
46    /// with no `result` at all — a caller that reads the field to tell what
47    /// happened would see nothing for the one outcome it most likely caused.
48    /// A session with a LIVE run also gets a result from the executor when its
49    /// loop unwinds; that one arrives later and knows more, so it correctly
50    /// overwrites this one. A session with no live run gets only this.
51    /// What: a refless `Cancelled` result. There is nothing on disk to point
52    /// at — cancellation is a status transition, not a run outcome (vision
53    /// spec §12 11.6) — so the refs stay `None` rather than being guessed.
54    /// Test: `registry_tests::cancel_records_a_refless_cancelled_result`.
55    pub(super) fn set_cancelled_result(&self, id: &str) {
56        self.set_task_result(
57            id,
58            TaskResult::new(TaskResultStatus::Cancelled)
59                .with_summary(Some("cancelled: no changes recorded".to_string())),
60        );
61    }
62}