Skip to main content

rinne_types/
blackboard.rs

1//! The `Blackboard` trait seam and node lifecycle status (`MCP_SKILLS.md` §15).
2//!
3//! The loop engine reads and writes plan, machine state, artifacts, and
4//! transcripts only through this trait — never through a concrete persistence
5//! type — so the engine can live in `rinne-loop` while the SQLite-backed impl
6//! lives in `rinne-core`, and either can be swapped behind the seam.
7
8use std::path::{Path, PathBuf};
9
10use crate::dag::Plan;
11use crate::graph::CodeGraph;
12use crate::worker::Usage;
13use crate::Result;
14
15/// The lifecycle status of a node (`MCP_SKILLS.md` §12 scheduler).
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum NodeStatus {
18    /// Not yet started.
19    Pending,
20    /// Dispatched and running (or interrupted mid-run).
21    Running,
22    /// Completed successfully.
23    Succeeded,
24    /// Ran and failed.
25    Failed,
26    /// Parked awaiting a human (checkpoint / stuck escalation).
27    Parked,
28}
29
30impl NodeStatus {
31    /// The stored string form (used by the persistence impl).
32    pub fn as_str(self) -> &'static str {
33        match self {
34            NodeStatus::Pending => "pending",
35            NodeStatus::Running => "running",
36            NodeStatus::Succeeded => "succeeded",
37            NodeStatus::Failed => "failed",
38            NodeStatus::Parked => "parked",
39        }
40    }
41
42    /// Parse a stored string form back into a status (defaults to `Pending`).
43    /// Prefer [`std::str::FromStr`] when an error on unknown values is needed;
44    /// this keeps the historical lenient mapping used by persistence.
45    pub fn parse_label(s: &str) -> NodeStatus {
46        match s {
47            "running" => NodeStatus::Running,
48            "succeeded" => NodeStatus::Succeeded,
49            "failed" => NodeStatus::Failed,
50            "parked" => NodeStatus::Parked,
51            _ => NodeStatus::Pending,
52        }
53    }
54
55    /// Backward-compatible alias for [`Self::parse_label`].
56    #[allow(clippy::should_implement_trait)]
57    pub fn from_str(s: &str) -> NodeStatus {
58        Self::parse_label(s)
59    }
60
61    pub fn label(self) -> &'static str {
62        self.as_str()
63    }
64
65    pub fn is_terminal_success(self) -> bool {
66        matches!(self, NodeStatus::Succeeded)
67    }
68}
69
70/// The blackboard seam: the loop engine's only door to persistence. The concrete
71/// impl (file tree + SQLite) lives in `rinne-core`.
72///
73/// Not `Send + Sync`: the SQLite-backed impl owns a `!Sync` connection and the
74/// engine runs on a dedicated current-thread runtime, so a `&dyn Blackboard` is
75/// used single-threaded.
76pub trait Blackboard {
77    // ----- files / plan -------------------------------------------------------
78    fn workspace(&self) -> &Path;
79    fn progress_path(&self) -> PathBuf;
80    fn save_plan(&self, plan: &Plan) -> Result<()>;
81    fn write_artifact(&self, name: &str, contents: &str) -> Result<()>;
82    fn read_artifact(&self, name: &str) -> Result<String>;
83    fn artifact_exists(&self, name: &str) -> bool;
84    fn write_transcript(&self, node_id: &str, contents: &str) -> Result<()>;
85    fn write_context(&self, node_id: &str, contents: &str) -> Result<()>;
86    fn append_progress(&self, line: &str) -> Result<()>;
87
88    // ----- machine state ------------------------------------------------------
89    fn ensure_node(&self, node_id: &str) -> Result<()>;
90    fn set_status(&self, node_id: &str, status: NodeStatus) -> Result<()>;
91    fn set_worker(&self, node_id: &str, worker: &str) -> Result<()>;
92    fn status(&self, node_id: &str) -> Result<NodeStatus>;
93    fn incr_iteration(&self, node_id: &str) -> Result<u32>;
94    fn iterations(&self, node_id: &str) -> Result<u32>;
95    fn record_usage(&self, node_id: &str, worker: &str, usage: &Usage) -> Result<()>;
96    fn total_iterations(&self) -> Result<u32>;
97    fn total_usage(&self) -> Result<Usage>;
98    fn set_meta(&self, key: &str, value: &str) -> Result<()>;
99    fn meta(&self, key: &str) -> Result<Option<String>>;
100
101    // ----- code graph (optional) ----------------------------------------------
102
103    /// Return the code graph if the blackboard was opened with graph support
104    /// enabled, or `None` if the graph is disabled (`--no-graph`).
105    fn code_graph(&self) -> Option<&dyn CodeGraph> {
106        None
107    }
108
109    /// Synchronously re-index a single file against the code graph.
110    /// A no-op when `code_graph()` is `None`.
111    fn reindex_file(&self, _abs: &Path) {}
112}