harn-vm 0.10.132

Async bytecode virtual machine for the Harn programming language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
use super::*;
use crate::orchestration::{CapabilityPolicy, WorkflowNode};
use std::collections::BTreeMap;

fn ceiling_with_tools(tools: &[&str]) -> CapabilityPolicy {
    CapabilityPolicy {
        tools: tools.iter().map(|t| t.to_string()).collect(),
        ..Default::default()
    }
}

fn options_with_policy(policy: &CapabilityPolicy) -> crate::value::DictMap {
    let mut options = crate::value::DictMap::new();
    insert_json_vm_option(&mut options, "policy", policy).unwrap();
    options
}

#[test]
fn ceiling_pass_through_is_within() {
    let ceiling = ceiling_with_tools(&["read", "edit"]);
    // The parity path: flattener passes the ceiling through unchanged.
    assert!(ceiling.assert_within_ceiling(&ceiling).is_ok());
    let options = options_with_policy(&ceiling);
    assert!(enforce_flattened_ceiling(&options, &ceiling).is_ok());
}

#[test]
fn narrowing_is_allowed() {
    let ceiling = ceiling_with_tools(&["read", "edit", "run_command"]);
    let narrowed = ceiling_with_tools(&["read"]);
    assert!(ceiling.assert_within_ceiling(&narrowed).is_ok());
}

#[test]
fn widening_tools_is_rejected() {
    let ceiling = ceiling_with_tools(&["read"]);
    let widened = ceiling_with_tools(&["read", "run_command"]);
    let err = ceiling.assert_within_ceiling(&widened).unwrap_err();
    assert!(
        err.contains("run_command"),
        "error names the widened tool: {err}"
    );

    // ... and surfaces as a ToolRejected VmError at the flatten seam.
    let options = options_with_policy(&widened);
    match enforce_flattened_ceiling(&options, &ceiling) {
        Err(VmError::CategorizedError { message, category }) => {
            assert_eq!(category, crate::value::ErrorCategory::ToolRejected);
            assert!(message.contains("run_command"), "message: {message}");
        }
        other => panic!("expected a ToolRejected error, got {other:?}"),
    }
}

#[test]
fn widening_capability_op_is_rejected() {
    let mut ceiling = CapabilityPolicy::default();
    ceiling
        .capabilities
        .insert("fs".to_string(), vec!["read".to_string()]);
    let mut widened = CapabilityPolicy::default();
    widened.capabilities.insert(
        "fs".to_string(),
        vec!["read".to_string(), "write".to_string()],
    );
    let err = ceiling.assert_within_ceiling(&widened).unwrap_err();
    assert!(err.contains("fs") && err.contains("write"), "error: {err}");
}

#[test]
fn adding_new_capability_is_rejected() {
    let mut ceiling = CapabilityPolicy::default();
    ceiling
        .capabilities
        .insert("fs".to_string(), vec!["read".to_string()]);
    let mut widened = ceiling.clone();
    widened
        .capabilities
        .insert("net".to_string(), vec!["connect".to_string()]);
    let err = ceiling.assert_within_ceiling(&widened).unwrap_err();
    assert!(
        err.contains("net"),
        "error names the added capability: {err}"
    );
}

#[test]
fn widening_recursion_budget_is_rejected() {
    let ceiling = CapabilityPolicy {
        recursion_limit: Some(2),
        ..Default::default()
    };
    let widened = CapabilityPolicy {
        recursion_limit: Some(9),
        ..Default::default()
    };
    assert!(ceiling.assert_within_ceiling(&widened).is_err());
    // Dropping the budget entirely is also a widening.
    let dropped = CapabilityPolicy::default();
    assert!(ceiling.assert_within_ceiling(&dropped).is_err());
    // Narrowing the budget is allowed.
    let narrowed = CapabilityPolicy {
        recursion_limit: Some(1),
        ..Default::default()
    };
    assert!(ceiling.assert_within_ceiling(&narrowed).is_ok());
}

#[test]
fn widening_roots_is_rejected() {
    let ceiling = CapabilityPolicy {
        workspace_roots: vec!["/repo".to_string()],
        ..Default::default()
    };
    let widened = CapabilityPolicy {
        workspace_roots: vec!["/repo".to_string(), "/etc".to_string()],
        ..Default::default()
    };
    let err = ceiling.assert_within_ceiling(&widened).unwrap_err();
    assert!(err.contains("/etc"), "error: {err}");
}

#[test]
fn widening_side_effect_level_is_rejected() {
    let ceiling = CapabilityPolicy {
        side_effect_level: Some("read_only".to_string()),
        ..Default::default()
    };
    let widened = CapabilityPolicy {
        side_effect_level: Some("network".to_string()),
        ..Default::default()
    };
    assert!(ceiling.assert_within_ceiling(&widened).is_err());
}

#[test]
fn unknown_side_effect_level_ranks_fail_closed() {
    // Canonical `rank_str` ranks an unrecognized level as `none` (0), so a
    // typo/injected level can never outrank a real ceiling. A ceiling of
    // `none` still rejects a widening to a known-higher level.
    let ceiling = CapabilityPolicy {
        side_effect_level: Some("none".to_string()),
        ..Default::default()
    };
    let widened = CapabilityPolicy {
        side_effect_level: Some("desktop_control".to_string()),
        ..Default::default()
    };
    assert!(ceiling.assert_within_ceiling(&widened).is_err());
    // An unknown requested level ranks 0 (== none), so it is within a
    // `none` ceiling rather than fail-open above it.
    let unknown = CapabilityPolicy {
        side_effect_level: Some("teleport".to_string()),
        ..Default::default()
    };
    assert!(ceiling.assert_within_ceiling(&unknown).is_ok());
}

#[test]
fn widening_process_sandbox_roots_is_rejected() {
    use crate::orchestration::ProcessSandboxPolicy;
    let ceiling = CapabilityPolicy {
        process_sandbox: ProcessSandboxPolicy {
            write_roots: vec!["/repo/.cache".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };
    let widened = CapabilityPolicy {
        process_sandbox: ProcessSandboxPolicy {
            write_roots: vec!["/repo/.cache".to_string(), "/etc".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };
    let err = ceiling.assert_within_ceiling(&widened).unwrap_err();
    assert!(
        err.contains("process_sandbox.write_roots") && err.contains("/etc"),
        "error: {err}"
    );
    // Narrowing (fewer roots) is allowed.
    assert!(ceiling
        .assert_within_ceiling(&CapabilityPolicy::default())
        .is_ok());
}

#[test]
fn injecting_process_sandbox_roots_into_empty_ceiling_is_rejected() {
    use crate::orchestration::ProcessSandboxPolicy;
    // The common default: a stage that never set process_sandbox has EMPTY
    // read/write roots — ZERO extra subprocess FS access (additive grants,
    // no fallback), i.e. MOST restrictive. A flattener injecting any root
    // must be rejected, not waved through as "unbounded".
    let ceiling = CapabilityPolicy::default();
    for (field, requested) in [
        (
            "process_sandbox.read_roots",
            CapabilityPolicy {
                process_sandbox: ProcessSandboxPolicy {
                    read_roots: vec!["/etc".to_string()],
                    ..Default::default()
                },
                ..Default::default()
            },
        ),
        (
            "process_sandbox.write_roots",
            CapabilityPolicy {
                process_sandbox: ProcessSandboxPolicy {
                    write_roots: vec!["/etc".to_string()],
                    ..Default::default()
                },
                ..Default::default()
            },
        ),
    ] {
        let err = ceiling.assert_within_ceiling(&requested).unwrap_err();
        assert!(
            err.contains(field) && err.contains("/etc"),
            "empty ceiling must reject injected {field}: {err}"
        );
    }
    // Empty requested against empty ceiling stays allowed (∅ ⊆ ∅).
    assert!(ceiling
        .assert_within_ceiling(&CapabilityPolicy::default())
        .is_ok());
}

#[test]
fn widening_process_sandbox_presets_is_rejected() {
    use crate::orchestration::{ProcessSandboxPolicy, ProcessSandboxPreset};
    let ceiling = CapabilityPolicy {
        process_sandbox: ProcessSandboxPolicy {
            presets: Some(vec![ProcessSandboxPreset::SystemRuntime]),
            ..Default::default()
        },
        ..Default::default()
    };
    let widened = CapabilityPolicy {
        process_sandbox: ProcessSandboxPolicy {
            presets: Some(vec![
                ProcessSandboxPreset::SystemRuntime,
                ProcessSandboxPreset::DeveloperToolchains,
            ]),
            ..Default::default()
        },
        ..Default::default()
    };
    let err = ceiling.assert_within_ceiling(&widened).unwrap_err();
    assert!(err.contains("process_sandbox presets"), "error: {err}");
}

#[test]
fn dropping_tool_arg_constraint_is_rejected() {
    use crate::orchestration::ToolArgConstraint;
    let constraint = ToolArgConstraint {
        tool: "edit".to_string(),
        arg_patterns: vec!["src/**".to_string()],
        arg_key: Some("path".to_string()),
    };
    let ceiling = CapabilityPolicy {
        tool_arg_constraints: vec![constraint],
        ..Default::default()
    };
    // A flattener that drops the scope constraint widens edit to anywhere.
    let widened = CapabilityPolicy::default();
    let err = ceiling.assert_within_ceiling(&widened).unwrap_err();
    assert!(
        err.contains("tool_arg_constraints") && err.contains("edit"),
        "error: {err}"
    );
    // Keeping it (and adding more) is allowed.
    let mut narrowed = ceiling.clone();
    narrowed.tool_arg_constraints.push(ToolArgConstraint {
        tool: "run_command".to_string(),
        arg_patterns: vec!["cargo *".to_string()],
        arg_key: None,
    });
    assert!(ceiling.assert_within_ceiling(&narrowed).is_ok());
}

#[test]
fn weakening_tool_annotation_is_rejected() {
    use crate::tool_annotations::{SideEffectLevel, ToolAnnotations, ToolArgSchema};
    let strong = ToolAnnotations {
        side_effect_level: SideEffectLevel::ReadOnly,
        arg_schema: ToolArgSchema {
            path_params: vec!["path".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };
    let mut ceiling = CapabilityPolicy {
        tools: vec!["edit".to_string(), "read".to_string()],
        ..Default::default()
    };
    ceiling
        .tool_annotations
        .insert("edit".to_string(), strong.clone());

    // Dropping the annotation for a still-granted tool (loses path_params →
    // path constraint becomes unresolvable/permissive) is a widening.
    let mut dropped = ceiling.clone();
    dropped.tool_annotations.clear();
    let err = ceiling.assert_within_ceiling(&dropped).unwrap_err();
    assert!(
        err.contains("tool_annotations") && err.contains("edit"),
        "error: {err}"
    );

    // Rewriting it (e.g. lowering the side-effect level) is also rejected.
    let mut rewritten = ceiling.clone();
    rewritten.tool_annotations.insert(
        "edit".to_string(),
        ToolAnnotations {
            side_effect_level: SideEffectLevel::None,
            ..strong
        },
    );
    assert!(ceiling.assert_within_ceiling(&rewritten).is_err());

    // But if the flattener narrows the tool set so `edit` is no longer
    // granted, dropping its annotation is fine.
    let narrowed_tools = CapabilityPolicy {
        tools: vec!["read".to_string()],
        ..Default::default()
    };
    assert!(ceiling.assert_within_ceiling(&narrowed_tools).is_ok());
}

/// The pinned pre-move Rust flattening algorithm (the deleted
/// `workflow_stage_agent_loop_options` body + helpers), preserved verbatim
/// as the parity oracle. `flatten_matches_pre_move_rust` asserts the Harn
/// flattener reproduces it dict-for-dict.
fn legacy_flatten_reference(
    node: &WorkflowNode,
    session_id: &str,
    tool_format: &str,
    mut options: crate::value::DictMap,
    tools_value: &Option<VmValue>,
    tool_names: &[String],
) -> crate::value::DictMap {
    if let Some(raw) = node.raw_model_policy.as_ref().and_then(|v| v.as_dict()) {
        for (key, value) in raw {
            if !matches!(value, VmValue::Nil) {
                options.insert(key.clone(), value.clone());
            }
        }
    }
    if !options.contains_key("command_policy") {
        if let Some(command_policy) = node
            .raw_model_policy
            .as_ref()
            .and_then(|v| v.as_dict())
            .and_then(|d| d.get("policy"))
            .and_then(|v| v.as_dict())
            .and_then(|p| p.get("command_policy"))
        {
            options.insert(
                crate::value::intern_key("command_policy"),
                command_policy.clone(),
            );
        }
    }
    if !node.auto_compact.enabled {
        options.insert(
            crate::value::intern_key("auto_compact"),
            VmValue::Bool(false),
        );
    } else {
        options.insert(
            crate::value::intern_key("auto_compact"),
            VmValue::Bool(true),
        );
        if let Some(v) = node.auto_compact.token_threshold {
            options.insert(
                crate::value::intern_key("compact_threshold"),
                VmValue::Int(v as i64),
            );
        }
        if let Some(v) = node.auto_compact.tool_output_max_chars {
            options.insert(
                crate::value::intern_key("tool_output_max_chars"),
                VmValue::Int(v as i64),
            );
        }
        if let Some(v) = node.auto_compact.hard_limit_tokens {
            options.insert(
                crate::value::intern_key("hard_limit_tokens"),
                VmValue::Int(v as i64),
            );
        }
        if let Some(s) = node.auto_compact.compact_strategy.as_ref() {
            options.put_str("compact_strategy", s.clone());
        }
        if let Some(s) = node.auto_compact.hard_limit_strategy.as_ref() {
            options.put_str("hard_limit_strategy", s.clone());
        }
        let raw = node.raw_auto_compact.as_ref().and_then(|v| v.as_dict());
        let keep = raw
            .and_then(|d| d.get("compact_keep_last"))
            .and_then(|v| v.as_int())
            .filter(|v| *v >= 0)
            .or_else(|| {
                raw.and_then(|d| d.get("keep_last"))
                    .and_then(|v| v.as_int())
                    .filter(|v| *v >= 0)
            });
        if let Some(v) = keep {
            options.insert(
                crate::value::intern_key("compact_keep_last"),
                VmValue::Int(v),
            );
        }
        if let Some(p) = raw
            .and_then(|d| d.get("summarize_prompt"))
            .and_then(|v| match v {
                VmValue::String(t) if !t.trim().is_empty() => Some(t.to_string()),
                _ => None,
            })
        {
            options.put_str("summarize_prompt", p);
        }
        if let Some(d) = raw {
            for key in ["compress_callback", "mask_callback"] {
                if let Some(cb) = d.get(key) {
                    options.insert(crate::value::intern_key(key), cb.clone());
                }
            }
            if let Some(cb) = d.get("custom_compactor") {
                options.insert(crate::value::intern_key("compact_callback"), cb.clone());
            }
        }
    }
    if !tool_names.is_empty() {
        if let Some(v) = tools_value.clone() {
            options.insert(crate::value::intern_key("tools"), v);
        }
    }
    let tool_policy = tool_capability_policy_from_spec(&node.tools);
    let effective = tool_policy.intersect(&node.capability_policy).unwrap();
    insert_json_vm_option(&mut options, "policy", &effective).unwrap();
    insert_json_vm_option(&mut options, "approval_policy", &node.approval_policy).unwrap();
    options.put_str("session_id", session_id);
    options.put_str("tool_format", tool_format);
    let label = node.id.clone().unwrap_or_else(|| session_id.to_string());
    crate::orchestration::annotate_nested_execution_options(
        &mut options,
        crate::orchestration::NestedExecutionKind::WorkflowStage,
        &label,
    );
    options
}

fn representative_node() -> WorkflowNode {
    let mut raw_model_policy = BTreeMap::new();
    raw_model_policy.insert(
        "provider".to_string(),
        VmValue::String(arcstr::ArcStr::from("anthropic")),
    );
    raw_model_policy.insert("temperature".to_string(), VmValue::Float(0.2));
    // Nested command policy hoisted to the top level by the flattener.
    let mut nested_policy = BTreeMap::new();
    nested_policy.insert(
        "command_policy".to_string(),
        VmValue::String(arcstr::ArcStr::from("worktree")),
    );
    raw_model_policy.insert("policy".to_string(), VmValue::dict(nested_policy));
    // A nil entry must be skipped by the merge.
    raw_model_policy.insert("nudge".to_string(), VmValue::Nil);

    let mut raw_auto_compact = BTreeMap::new();
    raw_auto_compact.insert("keep_last".to_string(), VmValue::Int(4));
    raw_auto_compact.insert(
        "summarize_prompt".to_string(),
        VmValue::String(arcstr::ArcStr::from("summarize tersely")),
    );

    WorkflowNode {
        id: Some("act".to_string()),
        kind: "stage".to_string(),
        mode: Some("agent".to_string()),
        tools: serde_json::json!(["read", "edit"]),
        auto_compact: crate::orchestration::AutoCompactPolicy {
            enabled: true,
            token_threshold: Some(8000),
            tool_output_max_chars: Some(2000),
            hard_limit_tokens: Some(20000),
            compact_strategy: Some("summary".to_string()),
            hard_limit_strategy: Some("truncate".to_string()),
        },
        capability_policy: CapabilityPolicy {
            tools: vec!["read".to_string(), "edit".to_string()],
            recursion_limit: Some(3),
            ..Default::default()
        },
        raw_model_policy: Some(VmValue::dict(raw_model_policy)),
        raw_auto_compact: Some(VmValue::dict(raw_auto_compact)),
        ..Default::default()
    }
}

#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn flatten_matches_pre_move_rust() {
    crate::reset_thread_local_state();
    let node = representative_node();
    let session_id = "session-parity";
    let tool_format = "text";
    let tool_names = vec!["read".to_string(), "edit".to_string()];
    let tools_value = Some(crate::stdlib::json_to_vm_value(&node.tools));

    // Base agent_loop options (as std/workflow/options would normalize).
    let mut base = crate::value::DictMap::new();
    base.insert(
        crate::value::intern_key("loop_until_done"),
        VmValue::Bool(true),
    );
    base.insert(crate::value::intern_key("max_iterations"), VmValue::Int(16));

    let stage_agent_options = super::super::WorkflowStageAgentOptions {
        run_agent_loop: true,
        tool_format: tool_format.to_string(),
        llm_options: BTreeMap::new(),
        agent_loop_options: base
            .iter()
            .map(|(k, v)| (k.to_string(), vm_value_to_json(v)))
            .collect(),
    };

    let mut vm = crate::Vm::new();
    crate::register_vm_stdlib(&mut vm);
    let ctx = crate::vm::AsyncBuiltinCtx::for_test(vm);

    let flattened = workflow_stage_agent_loop_options(
        &ctx,
        &node,
        session_id,
        &tools_value,
        &tool_names,
        &stage_agent_options,
    )
    .await
    .expect("harn flatten succeeds");

    let expected = legacy_flatten_reference(
        &node,
        session_id,
        tool_format,
        base,
        &tools_value,
        &tool_names,
    );

    let flattened_json = vm_value_to_json(&VmValue::dict(flattened));
    let expected_json = vm_value_to_json(&VmValue::dict(expected));
    assert_eq!(
        flattened_json, expected_json,
        "Harn flatten must be dict-equal to the pre-move Rust flatten"
    );
}