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 policy;
37pub use policy::*;
38
39mod workflow;
40pub use workflow::*;
41
42mod records;
43pub use records::*;
44
45thread_local! {
46 static CURRENT_MUTATION_SESSION: RefCell<Option<MutationSessionRecord>> = const { RefCell::new(None) };
47}
48
49#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
50#[serde(default)]
51pub struct MutationSessionRecord {
52 pub session_id: String,
53 pub parent_session_id: Option<String>,
54 pub run_id: Option<String>,
55 pub worker_id: Option<String>,
56 pub execution_kind: Option<String>,
57 pub mutation_scope: String,
58 pub approval_policy: Option<ToolApprovalPolicy>,
62}
63
64impl MutationSessionRecord {
65 pub fn normalize(mut self) -> Self {
66 if self.session_id.is_empty() {
67 self.session_id = new_id("session");
68 }
69 if self.mutation_scope.is_empty() {
70 self.mutation_scope = "read_only".to_string();
71 }
72 self
73 }
74}
75
76pub fn install_current_mutation_session(session: Option<MutationSessionRecord>) {
77 CURRENT_MUTATION_SESSION.with(|slot| {
78 *slot.borrow_mut() = session.map(MutationSessionRecord::normalize);
79 });
80}
81
82pub fn current_mutation_session() -> Option<MutationSessionRecord> {
83 CURRENT_MUTATION_SESSION.with(|slot| slot.borrow().clone())
84}
85pub(crate) fn parse_json_payload<T: for<'de> Deserialize<'de>>(
86 json: serde_json::Value,
87 label: &str,
88) -> Result<T, VmError> {
89 let payload = json.to_string();
90 let mut deserializer = serde_json::Deserializer::from_str(&payload);
91 let mut tracker = serde_path_to_error::Track::new();
92 let path_deserializer = serde_path_to_error::Deserializer::new(&mut deserializer, &mut tracker);
93 T::deserialize(path_deserializer).map_err(|error| {
94 let snippet = if payload.len() > 600 {
95 format!("{}...", &payload[..600])
96 } else {
97 payload.clone()
98 };
99 VmError::Runtime(format!(
100 "{label} parse error at {}: {} | payload={}",
101 tracker.path(),
102 error,
103 snippet
104 ))
105 })
106}
107
108pub(crate) fn parse_json_value<T: for<'de> Deserialize<'de>>(
109 value: &VmValue,
110) -> Result<T, VmError> {
111 parse_json_payload(vm_value_to_json(value), "orchestration")
112}
113
114#[cfg(test)]
115mod tests;