Skip to main content

rinne_types/
human.rs

1//! Human control session overlay (`CONDUCTOR_LOOP_PLAN.md` §4).
2//!
3//! Session-scoped role pins and flags persist under `.rinne/human-session.json`
4//! so a parked run can resume with the same overrides.
5
6use std::path::{Path, PathBuf};
7
8use serde::{Deserialize, Serialize};
9
10use crate::error::Result;
11
12/// Filename inside the blackboard directory.
13pub const HUMAN_SESSION_FILE: &str = "human-session.json";
14
15/// Per-run human control overlay, merged over config at plan/run time.
16#[derive(Debug, Clone, Default, Serialize, Deserialize)]
17#[serde(default)]
18pub struct HumanSession {
19    /// When true, pins in this file override config defaults for the run.
20    pub active: bool,
21    pub pins: RolePins,
22    /// Named review gates registered via `/human checkpoint`.
23    pub checkpoints: Vec<NamedCheckpoint>,
24}
25
26/// Role pins set via `/human` or `rinne human …`.
27#[derive(Debug, Clone, Default, Serialize, Deserialize)]
28#[serde(default)]
29pub struct RolePins {
30    pub conductor_backend: Option<String>,
31    pub conductor_model: Option<String>,
32    pub generator_worker: Option<String>,
33    pub generator_model: Option<String>,
34    /// `tool`, `human`, or `ai` (optional `ai:<worker>:<model>`).
35    pub evaluator: Option<String>,
36}
37
38/// A named human review gate.
39#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
40pub struct NamedCheckpoint {
41    pub name: String,
42    pub trigger: CheckpointTrigger,
43}
44
45/// When a named gate fires.
46#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
47#[serde(tag = "kind", rename_all = "snake_case")]
48pub enum CheckpointTrigger {
49    AfterNode { node: String },
50    BeforeNode { node: String },
51    OnBuildSuccess,
52}
53
54impl HumanSession {
55    pub fn path(blackboard_root: &Path) -> PathBuf {
56        blackboard_root.join(HUMAN_SESSION_FILE)
57    }
58
59    /// Load from disk, or default when missing.
60    pub fn load(blackboard_root: &Path) -> Self {
61        let path = Self::path(blackboard_root);
62        std::fs::read_to_string(&path)
63            .ok()
64            .and_then(|s| serde_json::from_str(&s).ok())
65            .unwrap_or_default()
66    }
67
68    pub fn save(&self, blackboard_root: &Path) -> Result<()> {
69        let path = Self::path(blackboard_root);
70        if let Some(parent) = path.parent() {
71            std::fs::create_dir_all(parent)?;
72        }
73        let json = serde_json::to_string_pretty(self)?;
74        std::fs::write(&path, json)?;
75        Ok(())
76    }
77
78    /// Active named checkpoints (session must be on).
79    pub fn active_gates(&self) -> &[NamedCheckpoint] {
80        if self.active {
81            &self.checkpoints
82        } else {
83            &[]
84        }
85    }
86
87    /// One-line summary for `/human` with no args.
88    pub fn summary(&self) -> String {
89        if !self.active {
90            return "human control: off (use `/human on` to enable session pins)".into();
91        }
92        let mut lines = vec!["human control: on".to_string()];
93        if let Some(b) = &self.pins.conductor_backend {
94            let m = self
95                .pins
96                .conductor_model
97                .as_deref()
98                .map(|x| format!(" {x}"))
99                .unwrap_or_default();
100            lines.push(format!("  conductor: {b}{m}"));
101        }
102        if let Some(w) = &self.pins.generator_worker {
103            let m = self
104                .pins
105                .generator_model
106                .as_deref()
107                .map(|x| format!(":{x}"))
108                .unwrap_or_default();
109            lines.push(format!("  generator: {w}{m}"));
110        }
111        if let Some(e) = &self.pins.evaluator {
112            lines.push(format!("  evaluator: {e}"));
113        }
114        if !self.checkpoints.is_empty() {
115            lines.push(format!("  checkpoints: {}", self.checkpoints.len()));
116            for cp in &self.checkpoints {
117                lines.push(format!("    - {} ({:?})", cp.name, cp.trigger));
118            }
119        }
120        if lines.len() == 1 {
121            lines.push("  (no pins yet)".into());
122        }
123        lines.join("\n")
124    }
125}
126
127/// Blackboard meta key: gate approved.
128pub fn gate_ok_key(name: &str) -> String {
129    format!("gate_ok:{name}")
130}
131
132/// Blackboard meta key: active gate name.
133pub const GATE_ACTIVE_KEY: &str = "gate_active";
134
135/// Blackboard meta key: gate review iteration.
136pub fn gate_iter_key(name: &str) -> String {
137    format!("gate_iter:{name}")
138}