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