Skip to main content

harn_vm/orchestration/
mod.rs

1use std::path::PathBuf;
2use std::{cell::RefCell, thread_local};
3
4use serde::{Deserialize, Serialize};
5
6use crate::llm::vm_value_to_json;
7use crate::value::{VmError, VmValue};
8
9pub(crate) fn now_rfc3339() -> String {
10    use std::time::{SystemTime, UNIX_EPOCH};
11    let ts = SystemTime::now()
12        .duration_since(UNIX_EPOCH)
13        .unwrap_or_default()
14        .as_secs();
15    format!("{ts}")
16}
17
18pub(crate) fn new_id(prefix: &str) -> String {
19    format!("{prefix}_{}", uuid::Uuid::now_v7())
20}
21
22pub(crate) fn default_run_dir() -> PathBuf {
23    let base = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
24    crate::runtime_paths::run_root(&base)
25}
26
27mod hooks;
28pub use hooks::*;
29
30mod command_policy;
31pub use command_policy::*;
32
33mod compaction;
34pub use compaction::*;
35
36pub mod agent_inbox;
37
38mod artifacts;
39pub use artifacts::*;
40
41mod assemble;
42pub use assemble::*;
43
44mod handoffs;
45pub use handoffs::*;
46
47mod friction;
48pub use friction::*;
49
50mod crystallize;
51pub use crystallize::*;
52
53mod release_fixture;
54pub use release_fixture::*;
55
56mod replay_oracle;
57pub use replay_oracle::*;
58
59mod replay_bench;
60pub use replay_bench::*;
61
62mod policy;
63pub use policy::*;
64
65mod stage_options;
66pub use stage_options::*;
67
68mod workflow;
69pub use workflow::*;
70
71mod workflow_bundle;
72pub use workflow_bundle::*;
73
74mod workflow_patch;
75pub use workflow_patch::*;
76
77mod safe_function_tools;
78pub use safe_function_tools::*;
79
80mod nested_invocation;
81pub use nested_invocation::*;
82
83#[cfg(test)]
84mod workflow_test_fixtures;
85
86mod records;
87pub use records::*;
88
89mod merge_captain_audit;
90pub use merge_captain_audit::*;
91
92mod merge_captain_driver;
93pub use merge_captain_driver::*;
94
95mod merge_captain_ladder;
96pub use merge_captain_ladder::*;
97
98mod merge_captain_iteration;
99pub use merge_captain_iteration::*;
100
101pub mod playground;
102
103thread_local! {
104    static CURRENT_MUTATION_SESSION: RefCell<Option<MutationSessionRecord>> = const { RefCell::new(None) };
105    /// Workflow-level skill context, installed by `workflow_execute` so
106    /// every per-node agent loop constructed inside `execute_stage_node`
107    /// can pick up the same `skills:` / `skill_match:` registry without
108    /// threading a new parameter through every helper. Cleared on
109    /// workflow exit (success or error) by `WorkflowSkillContextGuard`.
110    static CURRENT_WORKFLOW_SKILL_CONTEXT: RefCell<Option<WorkflowSkillContext>> = const { RefCell::new(None) };
111}
112
113/// Skill wiring threaded from `workflow_execute` into the per-stage
114/// agent loops via thread-local context. `VmValue` wraps `Rc` and is
115/// not `Send`, so we store it in a thread-local rather than a mutex —
116/// the workflow runner pins itself to one task via `LocalSet`, so
117/// every stage observes the same context.
118#[derive(Clone, Default)]
119pub struct WorkflowSkillContext {
120    pub registry: Option<VmValue>,
121    pub match_config: Option<VmValue>,
122}
123
124pub fn install_workflow_skill_context(context: Option<WorkflowSkillContext>) {
125    CURRENT_WORKFLOW_SKILL_CONTEXT.with(|slot| {
126        *slot.borrow_mut() = context;
127    });
128}
129
130pub fn current_workflow_skill_context() -> Option<WorkflowSkillContext> {
131    CURRENT_WORKFLOW_SKILL_CONTEXT.with(|slot| slot.borrow().clone())
132}
133
134/// RAII guard that clears the workflow skill context on drop. Paired
135/// with `install_workflow_skill_context` at the top of `execute_workflow`
136/// so the context never leaks past a workflow's scope.
137pub struct WorkflowSkillContextGuard;
138
139impl Drop for WorkflowSkillContextGuard {
140    fn drop(&mut self) {
141        install_workflow_skill_context(None);
142    }
143}
144
145#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
146#[serde(default)]
147pub struct MutationSessionRecord {
148    pub session_id: String,
149    pub parent_session_id: Option<String>,
150    pub run_id: Option<String>,
151    pub worker_id: Option<String>,
152    pub execution_kind: Option<String>,
153    pub mutation_scope: String,
154    /// Declarative per-tool approval policy for this session. When `None`,
155    /// no policy-driven approval is requested; the session update stream
156    /// remains the only host-observable surface for tool dispatch.
157    pub approval_policy: Option<ToolApprovalPolicy>,
158}
159
160impl MutationSessionRecord {
161    pub fn normalize(mut self) -> Self {
162        if self.session_id.is_empty() {
163            self.session_id = new_id("session");
164        }
165        if self.mutation_scope.is_empty() {
166            self.mutation_scope = "read_only".to_string();
167        }
168        self
169    }
170}
171
172pub fn install_current_mutation_session(session: Option<MutationSessionRecord>) {
173    CURRENT_MUTATION_SESSION.with(|slot| {
174        *slot.borrow_mut() = session.map(MutationSessionRecord::normalize);
175    });
176}
177
178pub fn current_mutation_session() -> Option<MutationSessionRecord> {
179    CURRENT_MUTATION_SESSION.with(|slot| slot.borrow().clone())
180}
181pub(crate) fn parse_json_payload<T: for<'de> Deserialize<'de>>(
182    json: serde_json::Value,
183    label: &str,
184) -> Result<T, VmError> {
185    let payload = json.to_string();
186    let mut deserializer = serde_json::Deserializer::from_str(&payload);
187    let mut tracker = serde_path_to_error::Track::new();
188    let path_deserializer = serde_path_to_error::Deserializer::new(&mut deserializer, &mut tracker);
189    T::deserialize(path_deserializer).map_err(|error| {
190        let snippet = if payload.len() > 600 {
191            format!("{}...", &payload[..600])
192        } else {
193            payload.clone()
194        };
195        VmError::Runtime(format!(
196            "{label} parse error at {}: {} | payload={}",
197            tracker.path(),
198            error,
199            snippet
200        ))
201    })
202}
203
204pub(crate) fn parse_json_value<T: for<'de> Deserialize<'de>>(
205    value: &VmValue,
206) -> Result<T, VmError> {
207    parse_json_payload(vm_value_to_json(value), "orchestration")
208}
209
210#[cfg(test)]
211mod tests;