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