harn_vm/orchestration/
mod.rs1use 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 handoffs;
37pub use handoffs::*;
38
39mod policy;
40pub use policy::*;
41
42mod workflow;
43pub use workflow::*;
44
45mod records;
46pub use records::*;
47
48thread_local! {
49 static CURRENT_MUTATION_SESSION: RefCell<Option<MutationSessionRecord>> = const { RefCell::new(None) };
50 static CURRENT_WORKFLOW_SKILL_CONTEXT: RefCell<Option<WorkflowSkillContext>> = const { RefCell::new(None) };
56}
57
58#[derive(Clone, Default)]
64pub struct WorkflowSkillContext {
65 pub registry: Option<VmValue>,
66 pub match_config: Option<VmValue>,
67}
68
69pub fn install_workflow_skill_context(context: Option<WorkflowSkillContext>) {
70 CURRENT_WORKFLOW_SKILL_CONTEXT.with(|slot| {
71 *slot.borrow_mut() = context;
72 });
73}
74
75pub fn current_workflow_skill_context() -> Option<WorkflowSkillContext> {
76 CURRENT_WORKFLOW_SKILL_CONTEXT.with(|slot| slot.borrow().clone())
77}
78
79pub struct WorkflowSkillContextGuard;
83
84impl Drop for WorkflowSkillContextGuard {
85 fn drop(&mut self) {
86 install_workflow_skill_context(None);
87 }
88}
89
90#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
91#[serde(default)]
92pub struct MutationSessionRecord {
93 pub session_id: String,
94 pub parent_session_id: Option<String>,
95 pub run_id: Option<String>,
96 pub worker_id: Option<String>,
97 pub execution_kind: Option<String>,
98 pub mutation_scope: String,
99 pub approval_policy: Option<ToolApprovalPolicy>,
103}
104
105impl MutationSessionRecord {
106 pub fn normalize(mut self) -> Self {
107 if self.session_id.is_empty() {
108 self.session_id = new_id("session");
109 }
110 if self.mutation_scope.is_empty() {
111 self.mutation_scope = "read_only".to_string();
112 }
113 self
114 }
115}
116
117pub fn install_current_mutation_session(session: Option<MutationSessionRecord>) {
118 CURRENT_MUTATION_SESSION.with(|slot| {
119 *slot.borrow_mut() = session.map(MutationSessionRecord::normalize);
120 });
121}
122
123pub fn current_mutation_session() -> Option<MutationSessionRecord> {
124 CURRENT_MUTATION_SESSION.with(|slot| slot.borrow().clone())
125}
126pub(crate) fn parse_json_payload<T: for<'de> Deserialize<'de>>(
127 json: serde_json::Value,
128 label: &str,
129) -> Result<T, VmError> {
130 let payload = json.to_string();
131 let mut deserializer = serde_json::Deserializer::from_str(&payload);
132 let mut tracker = serde_path_to_error::Track::new();
133 let path_deserializer = serde_path_to_error::Deserializer::new(&mut deserializer, &mut tracker);
134 T::deserialize(path_deserializer).map_err(|error| {
135 let snippet = if payload.len() > 600 {
136 format!("{}...", &payload[..600])
137 } else {
138 payload.clone()
139 };
140 VmError::Runtime(format!(
141 "{label} parse error at {}: {} | payload={}",
142 tracker.path(),
143 error,
144 snippet
145 ))
146 })
147}
148
149pub(crate) fn parse_json_value<T: for<'de> Deserialize<'de>>(
150 value: &VmValue,
151) -> Result<T, VmError> {
152 parse_json_payload(vm_value_to_json(value), "orchestration")
153}
154
155#[cfg(test)]
156mod tests;