1use std::path::{Path, PathBuf};
7
8use serde::{Deserialize, Serialize};
9
10use crate::error::Result;
11
12pub const HUMAN_SESSION_FILE: &str = "human-session.json";
14
15#[derive(Debug, Clone, Default, Serialize, Deserialize)]
17#[serde(default)]
18pub struct HumanSession {
19 pub active: bool,
21 pub pins: RolePins,
22 pub checkpoints: Vec<NamedCheckpoint>,
24}
25
26#[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 pub evaluator: Option<String>,
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
40pub struct NamedCheckpoint {
41 pub name: String,
42 pub trigger: CheckpointTrigger,
43}
44
45#[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 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 pub fn active_gates(&self) -> &[NamedCheckpoint] {
80 if self.active {
81 &self.checkpoints
82 } else {
83 &[]
84 }
85 }
86
87 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
127pub fn gate_ok_key(name: &str) -> String {
129 format!("gate_ok:{name}")
130}
131
132pub const GATE_ACTIVE_KEY: &str = "gate_active";
134
135pub fn gate_iter_key(name: &str) -> String {
137 format!("gate_iter:{name}")
138}