harn-vm 0.10.115

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
561
562
563
564
565
566
567
568
569
570
571
572
573
use super::*;

fn json_dict_to_btree(value: &JsonValue) -> crate::value::DictMap {
    match json_to_vm_value(value) {
        VmValue::Dict(d) => (*d).clone(),
        _ => crate::value::DictMap::new(),
    }
}

fn transcript_with(messages: Vec<JsonValue>, summary: Option<&str>) -> crate::value::DictMap {
    let mut transcript = serde_json::json!({
        "_type": "transcript",
        "version": 2,
        "messages": messages,
        "events": [],
        "assets": [],
    });
    if let Some(text) = summary {
        transcript["summary"] = serde_json::Value::String(text.to_string());
    }
    json_dict_to_btree(&transcript)
}

#[tokio::test]
async fn raw_policy_is_identity_and_emits_hash() {
    let transcript = transcript_with(
        vec![
            serde_json::json!({"role": "user", "content": "hi"}),
            serde_json::json!({"role": "assistant", "content": "hey"}),
        ],
        None,
    );
    let policy = ProjectionPolicy::default_for(PolicyKind::Raw);
    let result = project_transcript(None, &transcript, &policy)
        .await
        .unwrap();
    assert_eq!(result.messages.len(), 2);
    assert_eq!(result.dropped_indices.len(), 0);
    assert!(result.prefix_hash.starts_with("sha256:"));
}

#[tokio::test]
async fn clean_tool_repair_drops_failed_then_success_pair() {
    let transcript = transcript_with(
        vec![
            serde_json::json!({"role": "user", "content": "run it"}),
            serde_json::json!({
                "role": "assistant",
                "content": "",
                "tool_calls": [{"id": "call_1", "name": "run", "arguments": {}}],
            }),
            serde_json::json!({
                "role": "tool_result",
                "tool_call_id": "call_1",
                "name": "run",
                "content": "Error: missing arg",
                "is_error": true,
            }),
            serde_json::json!({
                "role": "assistant",
                "content": "",
                "tool_calls": [{"id": "call_2", "name": "run", "arguments": {"arg": "x"}}],
            }),
            serde_json::json!({
                "role": "tool_result",
                "tool_call_id": "call_2",
                "name": "run",
                "content": "ok",
            }),
        ],
        None,
    );
    let policy = ProjectionPolicy::default_for(PolicyKind::CleanToolRepair);
    let result = project_transcript(None, &transcript, &policy)
        .await
        .unwrap();
    // Drops the failed assistant (index 1) and its error tool result (index 2).
    assert_eq!(result.dropped_indices, vec![1, 2]);
    assert_eq!(result.kept_indices, vec![0, 3, 4]);
    assert_eq!(result.messages.len(), 3);
    assert!(result
        .messages
        .last()
        .and_then(|m| m.get("content"))
        .and_then(JsonValue::as_str)
        .unwrap_or("")
        .contains("ok"));
}

#[tokio::test]
async fn squash_failed_calls_drops_orphan_failures() {
    let transcript = transcript_with(
        vec![
            serde_json::json!({"role": "user", "content": "go"}),
            serde_json::json!({
                "role": "assistant",
                "content": "",
                "tool_calls": [{"id": "a", "name": "lookup", "arguments": {}}],
            }),
            serde_json::json!({
                "role": "tool",
                "tool_call_id": "a",
                "name": "lookup",
                "content": "Error: not found",
                "is_error": true,
            }),
            serde_json::json!({"role": "assistant", "content": "Done after all."}),
        ],
        None,
    );
    let policy = ProjectionPolicy::default_for(PolicyKind::SquashFailedCalls);
    let result = project_transcript(None, &transcript, &policy)
        .await
        .unwrap();
    assert_eq!(result.dropped_indices, vec![1, 2]);
    assert_eq!(result.messages.len(), 2);
}

#[tokio::test]
async fn summary_prefix_replaces_old_history_with_synthetic_message() {
    let transcript = transcript_with(
        (0..6)
            .map(|i| {
                serde_json::json!({
                    "role": if i % 2 == 0 { "user" } else { "assistant" },
                    "content": format!("msg{}", i),
                })
            })
            .collect(),
        Some("Earlier work boiled down."),
    );
    let mut policy = ProjectionPolicy::default_for(PolicyKind::SummaryPrefix);
    policy.summary_keep_last = 2;
    let result = project_transcript(None, &transcript, &policy)
        .await
        .unwrap();
    assert_eq!(result.messages.len(), 3); // 1 summary + 2 kept
    assert_eq!(result.dropped_indices.len(), 4);
    assert_eq!(result.kept_indices, vec![4, 5]);
    let summary_msg = &result.messages[0];
    assert_eq!(
        summary_msg.get("role").and_then(JsonValue::as_str),
        Some("system")
    );
    assert_eq!(
        summary_msg.get("content").and_then(JsonValue::as_str),
        Some("Earlier work boiled down.")
    );
    assert!(summary_msg
        .get("_harn_projection")
        .and_then(|p| p.get("synthetic"))
        .and_then(JsonValue::as_bool)
        .unwrap_or(false));
}

#[tokio::test]
async fn provider_safety_blocks_dropping_signed_reasoning() {
    let transcript = transcript_with(
        vec![
            serde_json::json!({"role": "user", "content": "first"}),
            serde_json::json!({
                "role": "assistant",
                "content": [
                    {"type": "thinking", "thinking": "private chain", "signature": "abc123"},
                    {"type": "tool_use", "id": "call_x", "name": "run", "input": {}}
                ],
            }),
            serde_json::json!({
                "role": "tool",
                "tool_call_id": "call_x",
                "name": "run",
                "content": "Error: boom",
                "is_error": true,
            }),
            serde_json::json!({
                "role": "assistant",
                "content": "",
                "tool_calls": [{"id": "call_y", "name": "run", "arguments": {"v": 1}}],
            }),
            serde_json::json!({
                "role": "tool",
                "tool_call_id": "call_y",
                "name": "run",
                "content": "ok",
            }),
        ],
        None,
    );
    let policy = ProjectionPolicy::default_for(PolicyKind::CleanToolRepair);
    let result = project_transcript(None, &transcript, &policy)
        .await
        .unwrap();
    assert!(result.provider_safety_blocked);
    // Falls back to identity: all messages survive.
    assert_eq!(result.kept_indices.len(), 5);
    assert_eq!(result.dropped_indices.len(), 0);
}

#[tokio::test]
async fn provider_safety_can_be_disabled_for_local_only_previews() {
    let transcript = transcript_with(
        vec![
            serde_json::json!({"role": "user", "content": "first"}),
            serde_json::json!({
                "role": "assistant",
                "content": [
                    {"type": "thinking", "thinking": "private chain", "signature": "abc"},
                    {"type": "tool_use", "id": "c", "name": "run", "input": {}}
                ],
            }),
            serde_json::json!({
                "role": "tool",
                "tool_call_id": "c",
                "name": "run",
                "content": "Error",
                "is_error": true,
            }),
            serde_json::json!({
                "role": "assistant",
                "content": "",
                "tool_calls": [{"id": "c2", "name": "run", "arguments": {}}],
            }),
            serde_json::json!({
                "role": "tool",
                "tool_call_id": "c2",
                "name": "run",
                "content": "ok",
            }),
        ],
        None,
    );
    let mut policy = ProjectionPolicy::default_for(PolicyKind::CleanToolRepair);
    policy.respect_provider_signatures = false;
    let result = project_transcript(None, &transcript, &policy)
        .await
        .unwrap();
    assert!(!result.provider_safety_blocked);
    assert!(!result.dropped_indices.is_empty());
}

#[tokio::test]
async fn reachability_gc_redacts_only_unrooted_stale_tool_results() {
    let stale_body = format!("src/old.rs\n{}", "old line\n".repeat(180));
    let rooted_body = format!(
        "src/live.rs\nIMPORTANT_LIVE_VALUE\n{}",
        "live line\n".repeat(180)
    );
    let transcript = transcript_with(
        vec![
            serde_json::json!({"role": "user", "content": "inspect old and live files"}),
            serde_json::json!({
                "role": "assistant",
                "content": "",
                "tool_calls": [{"id": "old_call", "name": "read", "arguments": {"path": "src/old.rs"}}],
            }),
            serde_json::json!({
                "role": "tool_result",
                "tool_call_id": "old_call",
                "name": "read",
                "content": stale_body,
                "_harn": {
                    "kind": "tool_result",
                    "outcome": "ok",
                    "schema": "agent.tool_result.v1",
                },
            }),
            serde_json::json!({
                "role": "assistant",
                "content": "",
                "tool_calls": [{"id": "live_call", "name": "read", "arguments": {"path": "src/live.rs"}}],
            }),
            serde_json::json!({
                "role": "tool_result",
                "tool_call_id": "live_call",
                "name": "read",
                "content": rooted_body,
                "_harn": {
                    "kind": "tool_result",
                    "outcome": "ok",
                    "schema": "agent.tool_result.v1",
                },
            }),
            serde_json::json!({"role": "assistant", "content": "The active change is in src/live.rs."}),
            serde_json::json!({"role": "user", "content": "Continue from the live file finding."}),
        ],
        None,
    );
    let mut policy = ProjectionPolicy::default_for(PolicyKind::ReachabilityGc);
    policy.gc_root_window = 2;
    policy.gc_min_chars = 100;

    let result = project_transcript(None, &transcript, &policy)
        .await
        .unwrap();

    assert_eq!(result.dropped_indices.len(), 0);
    assert_eq!(result.redacted_indices, vec![2]);
    assert!(result.reclaimed_tokens > 0);
    assert!(result
        .roots_consulted
        .contains(&"last_2_messages".to_string()));
    assert_eq!(
        result.redaction_pointers[0]["source"],
        "transcript.messages[2].content"
    );
    assert!(result.messages[2]["content"]
        .as_str()
        .unwrap_or_default()
        .contains("reclaimed by reachability_gc"));
    assert!(result.messages[4]["content"]
        .as_str()
        .unwrap_or_default()
        .contains("IMPORTANT_LIVE_VALUE"));
    assert!(result.messages[2]["_harn_projection"]["redacted"]
        .as_bool()
        .unwrap_or(false));
}

#[tokio::test]
async fn reachability_gc_keeps_domain_kind_identifiers_outside_harn_metadata() {
    let transcript = transcript_with(
        vec![
            serde_json::json!({"role": "user", "content": "Inspect the stored record."}),
            serde_json::json!({
                "role": "assistant",
                "content": "",
                "tool_calls": [{"id": "record_call", "name": "lookup", "arguments": {}}],
            }),
            serde_json::json!({
                "role": "tool",
                "tool_call_id": "record_call",
                "name": "lookup",
                "content": "record payload\n".repeat(180),
                "data": {"kind": "CustomerRecord"},
                "_harn": {
                    "kind": "tool_result",
                    "outcome": "ok",
                    "schema": "agent.tool_result.v1",
                },
            }),
            serde_json::json!({"role": "assistant", "content": "CustomerRecord is the active domain object."}),
            serde_json::json!({"role": "user", "content": "Continue with CustomerRecord."}),
        ],
        None,
    );
    let mut policy = ProjectionPolicy::default_for(PolicyKind::ReachabilityGc);
    policy.gc_root_window = 2;
    policy.gc_min_chars = 100;

    let result = project_transcript(None, &transcript, &policy)
        .await
        .unwrap();

    assert!(result.redacted_indices.is_empty());
    assert!(result.messages[2]["content"]
        .as_str()
        .unwrap_or_default()
        .contains("record payload"));
}

#[tokio::test]
async fn reachability_gc_preserves_structured_ids_without_spelling_heuristics() {
    let uuid = "8f14e45f-ea5e-4dab-8c71-6f589afb8e17";
    let sha = "deadbeef1234567890abcdef1234567890abcdef";
    let transcript = transcript_with(
        vec![
            serde_json::json!({"role": "user", "content": "Inspect the selected objects."}),
            serde_json::json!({
                "role": "tool",
                "name": "lookup",
                "content": "lowercase object payload\n".to_string() + &"value\n".repeat(180),
                "data": {"object_id": "widget"},
            }),
            serde_json::json!({
                "role": "tool",
                "name": "lookup",
                "content": "UUID payload\n".to_string() + &"value\n".repeat(180),
                "data": {"record_id": uuid},
            }),
            serde_json::json!({
                "role": "tool",
                "name": "lookup",
                "content": "commit payload\n".to_string() + &"value\n".repeat(180),
                "data": {"commit_sha": sha},
            }),
            serde_json::json!({
                "role": "tool",
                "name": "lookup",
                "content": "issue payload\n".to_string() + &"value\n".repeat(180),
                "data": {"issue_number": 12},
            }),
            serde_json::json!({
                "role": "user",
                "content": format!("Continue with widget, {uuid}, {sha}, and issue 12."),
            }),
        ],
        None,
    );
    let mut policy = ProjectionPolicy::default_for(PolicyKind::ReachabilityGc);
    policy.gc_root_window = 1;
    policy.gc_min_chars = 100;

    let result = project_transcript(None, &transcript, &policy)
        .await
        .unwrap();

    assert!(result.redacted_indices.is_empty());
}

#[tokio::test]
async fn reachability_gc_matches_references_as_tokens_not_substrings() {
    let transcript = transcript_with(
        vec![
            serde_json::json!({"role": "user", "content": "Inspect two old objects."}),
            serde_json::json!({
                "role": "tool",
                "name": "lookup",
                "content": "old issue payload\n".to_string() + &"value\n".repeat(180),
                "data": {"issue_number": 2},
            }),
            serde_json::json!({
                "role": "tool",
                "name": "lookup",
                "content": "old commit payload\n".to_string() + &"value\n".repeat(180),
                "data": {"commit_sha": "abc1234"},
            }),
            serde_json::json!({
                "role": "user",
                "content": "Continue with issue 12 and xabc1234y.",
            }),
        ],
        None,
    );
    let mut policy = ProjectionPolicy::default_for(PolicyKind::ReachabilityGc);
    policy.gc_root_window = 1;
    policy.gc_min_chars = 100;

    let result = project_transcript(None, &transcript, &policy)
        .await
        .unwrap();

    assert_eq!(result.redacted_indices, vec![1, 2]);
}

#[tokio::test]
async fn reachability_gc_redacts_only_selected_tool_result_blocks() {
    let stale_body = format!("src/old.rs\n{}", "old line\n".repeat(180));
    let rooted_body = format!(
        "src/live.rs\nIMPORTANT_LIVE_VALUE\n{}",
        "live line\n".repeat(180)
    );
    let transcript = transcript_with(
        vec![
            serde_json::json!({"role": "user", "content": "inspect old and live files"}),
            serde_json::json!({
                "role": "assistant",
                "content": [
                    {"type": "tool_use", "id": "old_call", "name": "read", "input": {"path": "src/old.rs"}},
                    {"type": "tool_use", "id": "live_call", "name": "read", "input": {"path": "src/live.rs"}}
                ],
            }),
            serde_json::json!({
                "role": "user",
                "content": [
                    {"type": "tool_result", "tool_use_id": "old_call", "content": stale_body},
                    {"type": "tool_result", "tool_use_id": "live_call", "content": rooted_body}
                ],
            }),
            serde_json::json!({"role": "assistant", "content": "The active change is in src/live.rs."}),
            serde_json::json!({"role": "user", "content": "Continue from the live file finding."}),
        ],
        None,
    );
    let mut policy = ProjectionPolicy::default_for(PolicyKind::ReachabilityGc);
    policy.gc_root_window = 2;
    policy.gc_min_chars = 100;

    let result = project_transcript(None, &transcript, &policy)
        .await
        .unwrap();

    assert_eq!(result.redacted_indices, vec![2]);
    assert_eq!(result.redaction_pointers.len(), 1);
    assert_eq!(
        result.redaction_pointers[0]["source"],
        "transcript.messages[2].content[0].content"
    );
    let blocks = result.messages[2]["content"].as_array().unwrap();
    assert!(blocks[0]["content"]
        .as_str()
        .unwrap_or_default()
        .contains("reclaimed by reachability_gc"));
    assert!(blocks[1]["content"]
        .as_str()
        .unwrap_or_default()
        .contains("IMPORTANT_LIVE_VALUE"));
    assert_eq!(
        result.messages[2]["_harn_projection"]["redaction_pointers"]
            .as_array()
            .map(Vec::len),
        Some(1)
    );
}

#[tokio::test]
async fn reachability_gc_honors_required_write_barrier() {
    let transcript = transcript_with(
        vec![
            serde_json::json!({"role": "user", "content": "read stale"}),
            serde_json::json!({
                "role": "assistant",
                "content": "",
                "tool_calls": [{"id": "call", "name": "read", "arguments": {"path": "src/stale.rs"}}],
            }),
            serde_json::json!({
                "role": "tool",
                "tool_call_id": "call",
                "name": "read",
                "content": "src/stale.rs\n".to_string() + &"stale line\n".repeat(180),
            }),
            serde_json::json!({"role": "user", "content": "new task"}),
        ],
        None,
    );
    let mut policy = ProjectionPolicy::default_for(PolicyKind::ReachabilityGc);
    policy.gc_root_window = 1;
    policy.gc_min_chars = 100;
    policy.gc_require_write_barrier = true;

    let result = project_transcript(None, &transcript, &policy)
        .await
        .unwrap();

    assert!(result.redacted_indices.is_empty());
    assert_eq!(result.reason, "reachability_gc_write_barrier_missing");
    assert!(result
        .roots_consulted
        .contains(&"write_barrier_required".to_string()));
}

#[tokio::test]
async fn hash_changes_when_messages_change() {
    let raw_msgs = vec![
        serde_json::json!({"role": "user", "content": "hi"}),
        serde_json::json!({"role": "assistant", "content": "hey"}),
    ];
    let h1 = hash_messages(&raw_msgs);
    let h2 = hash_messages(&[
        serde_json::json!({"role": "user", "content": "hi"}),
        serde_json::json!({"role": "assistant", "content": "different"}),
    ]);
    assert_ne!(h1, h2);
    let h3 = hash_messages(&raw_msgs);
    assert_eq!(h1, h3);
}

#[test]
fn parse_policy_accepts_string_shorthand() {
    let policy =
        parse_projection_options(&VmValue::String(arcstr::ArcStr::from("clean_tool_repair")))
            .unwrap();
    assert_eq!(policy.kind, PolicyKind::CleanToolRepair);
}

#[test]
fn parse_policy_rejects_unknown_kind() {
    let err =
        parse_projection_options(&VmValue::String(arcstr::ArcStr::from("bogus"))).unwrap_err();
    match err {
        VmError::Runtime(msg) => assert!(msg.contains("bogus")),
        _ => panic!("expected runtime error"),
    }
}