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::worker::Usage;
12use crate::Result;
13
14/// The lifecycle status of a node (`MCP_SKILLS.md` §12 scheduler).
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum NodeStatus {
17    /// Not yet started.
18    Pending,
19    /// Dispatched and running (or interrupted mid-run).
20    Running,
21    /// Completed successfully.
22    Succeeded,
23    /// Ran and failed.
24    Failed,
25    /// Parked awaiting a human (checkpoint / stuck escalation).
26    Parked,
27}
28
29impl NodeStatus {
30    /// The stored string form (used by the persistence impl).
31    pub fn as_str(self) -> &'static str {
32        match self {
33            NodeStatus::Pending => "pending",
34            NodeStatus::Running => "running",
35            NodeStatus::Succeeded => "succeeded",
36            NodeStatus::Failed => "failed",
37            NodeStatus::Parked => "parked",
38        }
39    }
40
41    /// Parse a stored string form back into a status (defaults to `Pending`).
42    pub fn from_str(s: &str) -> NodeStatus {
43        match s {
44            "running" => NodeStatus::Running,
45            "succeeded" => NodeStatus::Succeeded,
46            "failed" => NodeStatus::Failed,
47            "parked" => NodeStatus::Parked,
48            _ => NodeStatus::Pending,
49        }
50    }
51
52    pub fn label(self) -> &'static str {
53        self.as_str()
54    }
55
56    pub fn is_terminal_success(self) -> bool {
57        matches!(self, NodeStatus::Succeeded)
58    }
59}
60
61/// The blackboard seam: the loop engine's only door to persistence. The concrete
62/// impl (file tree + SQLite) lives in `rinne-core`.
63///
64/// Not `Send + Sync`: the SQLite-backed impl owns a `!Sync` connection and the
65/// engine runs on a dedicated current-thread runtime, so a `&dyn Blackboard` is
66/// used single-threaded.
67pub trait Blackboard {
68    // ----- files / plan -------------------------------------------------------
69    fn workspace(&self) -> &Path;
70    fn progress_path(&self) -> PathBuf;
71    fn save_plan(&self, plan: &Plan) -> Result<()>;
72    fn write_artifact(&self, name: &str, contents: &str) -> Result<()>;
73    fn read_artifact(&self, name: &str) -> Result<String>;
74    fn artifact_exists(&self, name: &str) -> bool;
75    fn write_transcript(&self, node_id: &str, contents: &str) -> Result<()>;
76    fn write_context(&self, node_id: &str, contents: &str) -> Result<()>;
77    fn append_progress(&self, line: &str) -> Result<()>;
78
79    // ----- machine state ------------------------------------------------------
80    fn ensure_node(&self, node_id: &str) -> Result<()>;
81    fn set_status(&self, node_id: &str, status: NodeStatus) -> Result<()>;
82    fn set_worker(&self, node_id: &str, worker: &str) -> Result<()>;
83    fn status(&self, node_id: &str) -> Result<NodeStatus>;
84    fn incr_iteration(&self, node_id: &str) -> Result<u32>;
85    fn iterations(&self, node_id: &str) -> Result<u32>;
86    fn record_usage(&self, node_id: &str, worker: &str, usage: &Usage) -> Result<()>;
87    fn total_iterations(&self) -> Result<u32>;
88    fn total_usage(&self) -> Result<Usage>;
89    fn set_meta(&self, key: &str, value: &str) -> Result<()>;
90    fn meta(&self, key: &str) -> Result<Option<String>>;
91}