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 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 policy;
52pub use policy::*;
53
54mod workflow;
55pub use workflow::*;
56
57mod records;
58pub use records::*;
59
60thread_local! {
61 static CURRENT_MUTATION_SESSION: RefCell<Option<MutationSessionRecord>> = const { RefCell::new(None) };
62 static CURRENT_WORKFLOW_SKILL_CONTEXT: RefCell<Option<WorkflowSkillContext>> = const { RefCell::new(None) };
68}
69
70#[derive(Clone, Default)]
76pub struct WorkflowSkillContext {
77 pub registry: Option<VmValue>,
78 pub match_config: Option<VmValue>,
79}
80
81pub fn install_workflow_skill_context(context: Option<WorkflowSkillContext>) {
82 CURRENT_WORKFLOW_SKILL_CONTEXT.with(|slot| {
83 *slot.borrow_mut() = context;
84 });
85}
86
87pub fn current_workflow_skill_context() -> Option<WorkflowSkillContext> {
88 CURRENT_WORKFLOW_SKILL_CONTEXT.with(|slot| slot.borrow().clone())
89}
90
91pub struct WorkflowSkillContextGuard;
95
96impl Drop for WorkflowSkillContextGuard {
97 fn drop(&mut self) {
98 install_workflow_skill_context(None);
99 }
100}
101
102#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
103#[serde(default)]
104pub struct MutationSessionRecord {
105 pub session_id: String,
106 pub parent_session_id: Option<String>,
107 pub run_id: Option<String>,
108 pub worker_id: Option<String>,
109 pub execution_kind: Option<String>,
110 pub mutation_scope: String,
111 pub approval_policy: Option<ToolApprovalPolicy>,
115}
116
117impl MutationSessionRecord {
118 pub fn normalize(mut self) -> Self {
119 if self.session_id.is_empty() {
120 self.session_id = new_id("session");
121 }
122 if self.mutation_scope.is_empty() {
123 self.mutation_scope = "read_only".to_string();
124 }
125 self
126 }
127}
128
129pub fn install_current_mutation_session(session: Option<MutationSessionRecord>) {
130 CURRENT_MUTATION_SESSION.with(|slot| {
131 *slot.borrow_mut() = session.map(MutationSessionRecord::normalize);
132 });
133}
134
135pub fn current_mutation_session() -> Option<MutationSessionRecord> {
136 CURRENT_MUTATION_SESSION.with(|slot| slot.borrow().clone())
137}
138pub(crate) fn parse_json_payload<T: for<'de> Deserialize<'de>>(
139 json: serde_json::Value,
140 label: &str,
141) -> Result<T, VmError> {
142 let payload = json.to_string();
143 let mut deserializer = serde_json::Deserializer::from_str(&payload);
144 let mut tracker = serde_path_to_error::Track::new();
145 let path_deserializer = serde_path_to_error::Deserializer::new(&mut deserializer, &mut tracker);
146 T::deserialize(path_deserializer).map_err(|error| {
147 let snippet = if payload.len() > 600 {
148 format!("{}...", &payload[..600])
149 } else {
150 payload.clone()
151 };
152 VmError::Runtime(format!(
153 "{label} parse error at {}: {} | payload={}",
154 tracker.path(),
155 error,
156 snippet
157 ))
158 })
159}
160
161pub(crate) fn parse_json_value<T: for<'de> Deserialize<'de>>(
162 value: &VmValue,
163) -> Result<T, VmError> {
164 parse_json_payload(vm_value_to_json(value), "orchestration")
165}
166
167#[cfg(test)]
168mod tests;