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_unix_seconds_text() -> String {
18 use std::time::{SystemTime, UNIX_EPOCH};
19 let ts = SystemTime::now()
20 .duration_since(UNIX_EPOCH)
21 .unwrap_or_default()
22 .as_secs();
23 format!("{ts}")
24}
25
26pub(crate) fn new_id(prefix: &str) -> String {
27 format!("{prefix}_{}", uuid::Uuid::now_v7())
28}
29
30pub(crate) fn default_run_dir() -> PathBuf {
31 let base = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
32 crate::runtime_paths::run_root(&base)
33}
34
35mod hooks;
36pub use hooks::*;
37#[cfg(test)]
38mod tests_lazy_hooks;
39
40mod pipeline_lifecycle;
41pub use pipeline_lifecycle::*;
42
43mod settlement_agent;
44pub use settlement_agent::*;
45
46mod lifecycle_receipts;
47pub use lifecycle_receipts::*;
48
49mod command_policy;
50pub use command_policy::*;
51
52mod tool_precheck;
53pub use tool_precheck::*;
54
55mod compaction;
56pub use compaction::*;
57
58mod repair_ledger;
59
60mod compact_lifecycle;
61pub use compact_lifecycle::*;
62
63mod compaction_receipt;
64pub use compaction_receipt::*;
65
66mod compaction_policy_registry;
67pub use compaction_policy_registry::*;
68
69pub mod agent_inbox;
70
71mod artifacts;
72pub use artifacts::*;
73
74mod assemble;
75pub use assemble::*;
76
77mod handoffs;
78pub use handoffs::*;
79
80mod friction;
81pub use friction::*;
82
83mod crystallize;
84pub use crystallize::*;
85
86mod release_fixture;
87pub use release_fixture::*;
88
89mod replay_oracle;
90pub use replay_oracle::*;
91
92mod replay_bench;
93pub use replay_bench::*;
94
95mod policy;
96#[cfg(test)]
97pub(crate) use policy::swap_execution_policy_stack;
98pub use policy::*;
99
100mod ambient_scope;
101pub use ambient_scope::blocking::run_blocking_with_ambient;
102pub(crate) use ambient_scope::{
103 scope_ambient, scope_ambient_transaction, scope_approval_policy, scope_autonomy_policy,
104 scope_command_policy, scope_dynamic_permissions, scope_inline_subtask, scope_run_event_sink,
105 AmbientExecutionScope,
106};
107pub use ambient_scope::{
108 scope_execution_policy, scope_llm_runtime_overrides,
109 scope_llm_runtime_overrides_with_provider_endpoints,
110};
111
112mod stage_options;
113pub use stage_options::*;
114
115mod workflow;
116pub use workflow::*;
117
118mod workflow_bundle;
119pub use workflow_bundle::*;
120
121mod workflow_patch;
122pub use workflow_patch::*;
123
124mod safe_function_tools;
125pub use safe_function_tools::*;
126
127mod nested_invocation;
128pub use nested_invocation::*;
129
130#[cfg(test)]
131mod workflow_test_fixtures;
132
133mod records;
134pub use records::*;
135
136mod run_review;
137pub use run_review::*;
138
139mod training_example;
140pub use training_example::*;
141
142mod context_eval;
143pub use context_eval::*;
144
145mod skill_gate;
146pub use skill_gate::*;
147
148mod merge_captain_audit;
149pub use merge_captain_audit::*;
150
151mod merge_captain_driver;
152pub use merge_captain_driver::*;
153
154mod merge_captain_ladder;
155pub use merge_captain_ladder::*;
156
157mod merge_captain_iteration;
158pub use merge_captain_iteration::*;
159
160pub mod playground;
161
162thread_local! {
163 static CURRENT_MUTATION_SESSION: RefCell<Option<MutationSessionRecord>> = const { RefCell::new(None) };
164 static CURRENT_WORKFLOW_SKILL_CONTEXT: RefCell<Option<WorkflowSkillContext>> = const { RefCell::new(None) };
170}
171
172#[derive(Clone, Default)]
177pub struct WorkflowSkillContext {
178 pub registry: Option<VmValue>,
179 pub match_config: Option<VmValue>,
180}
181
182pub fn install_workflow_skill_context(context: Option<WorkflowSkillContext>) {
183 CURRENT_WORKFLOW_SKILL_CONTEXT.with(|slot| {
184 *slot.borrow_mut() = context;
185 });
186}
187
188pub fn current_workflow_skill_context() -> Option<WorkflowSkillContext> {
189 CURRENT_WORKFLOW_SKILL_CONTEXT.with(|slot| slot.borrow().clone())
190}
191
192pub struct WorkflowSkillContextGuard;
196
197impl Drop for WorkflowSkillContextGuard {
198 fn drop(&mut self) {
199 install_workflow_skill_context(None);
200 }
201}
202
203#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
204#[serde(default)]
205pub struct MutationSessionRecord {
206 pub session_id: String,
207 pub parent_session_id: Option<String>,
208 pub run_id: Option<String>,
209 pub worker_id: Option<String>,
210 pub execution_kind: Option<String>,
211 pub mutation_scope: String,
212 pub approval_policy: Option<ToolApprovalPolicy>,
216}
217
218impl MutationSessionRecord {
219 pub fn normalize(mut self) -> Self {
220 if self.session_id.is_empty() {
221 self.session_id = new_id("session");
222 }
223 if self.mutation_scope.is_empty() {
224 self.mutation_scope = "read_only".to_string();
225 }
226 self
227 }
228}
229
230pub fn install_current_mutation_session(session: Option<MutationSessionRecord>) {
231 CURRENT_MUTATION_SESSION.with(|slot| {
232 *slot.borrow_mut() = session.map(MutationSessionRecord::normalize);
233 });
234}
235
236pub fn current_mutation_session() -> Option<MutationSessionRecord> {
237 CURRENT_MUTATION_SESSION.with(|slot| slot.borrow().clone())
238}
239
240pub(crate) fn swap_mutation_session(
248 next: Option<MutationSessionRecord>,
249) -> Option<MutationSessionRecord> {
250 CURRENT_MUTATION_SESSION.with(|slot| std::mem::replace(&mut *slot.borrow_mut(), next))
251}
252pub(crate) fn parse_json_payload<T: for<'de> Deserialize<'de>>(
253 json: serde_json::Value,
254 label: &str,
255) -> Result<T, VmError> {
256 let payload = json.to_string();
257 let mut deserializer = serde_json::Deserializer::from_str(&payload);
258 let mut tracker = serde_path_to_error::Track::new();
259 let path_deserializer = serde_path_to_error::Deserializer::new(&mut deserializer, &mut tracker);
260 T::deserialize(path_deserializer).map_err(|error| {
261 let snippet = if payload.len() > 600 {
262 format!("{}...", &payload[..600])
263 } else {
264 payload.clone()
265 };
266 VmError::Runtime(format!(
267 "{label} parse error at {}: {} | payload={}",
268 tracker.path(),
269 error,
270 snippet
271 ))
272 })
273}
274
275pub(crate) fn parse_json_value<T: for<'de> Deserialize<'de>>(
276 value: &VmValue,
277) -> Result<T, VmError> {
278 parse_json_payload(vm_value_to_json(value), "orchestration")
279}
280
281#[cfg(test)]
282mod tests;
283
284#[cfg(test)]
285mod policy_restriction_tests;
286
287#[cfg(test)]
288mod typed_options_parity;