harn-vm 0.10.174

Async bytecode virtual machine for the Harn programming language
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
use super::*;

#[test]
fn compaction_policy_instructions_extend_by_default() {
    let policy = CompactionPolicy {
        instructions: Some("Keep the failing test names.".to_string()),
        ..Default::default()
    };
    let archived = [serde_json::json!({"role": "user", "content": "old context"})];
    let retained = [serde_json::json!({"role": "tool", "content": "new evidence"})];
    let prompt = render_llm_compaction_prompt(None, &archived, &retained, 1, &policy)
        .expect("prompt renders");

    assert_eq!(policy.instruction_mode(), "extend");
    assert!(prompt.contains("Preserve goals, constraints"));
    assert!(prompt.contains("Additional compaction instructions"));
    assert!(prompt.contains("Keep the failing test names."));
    assert!(prompt.contains("TOOL: new evidence"));
}

#[test]
fn compaction_policy_can_replace_default_instructions() {
    let policy = CompactionPolicy {
        instructions: Some("Only keep repro steps.".to_string()),
        extend_default_instructions: Some(false),
        ..Default::default()
    };
    let archived = [serde_json::json!({"role": "user", "content": "old context"})];
    let retained = [serde_json::json!({"role": "tool", "content": "new evidence"})];
    let prompt = render_llm_compaction_prompt(None, &archived, &retained, 1, &policy)
        .expect("prompt renders");

    assert_eq!(policy.instruction_mode(), "replace");
    assert!(prompt.contains("according to these instructions"));
    assert!(prompt.contains("Only keep repro steps."));
    assert!(!prompt.contains("Preserve goals, constraints"));
    assert!(prompt.contains("newer than every archived message"));
    assert!(prompt.contains("TOOL: new evidence"));
}

#[test]
fn auto_compact_preserves_reasoning_tool_suffix() {
    let mut messages = vec![
        serde_json::json!({"role": "user", "content": "old task"}),
        serde_json::json!({"role": "assistant", "content": "old reply"}),
        serde_json::json!({"role": "user", "content": "new task"}),
        serde_json::json!({
            "role": "assistant",
            "content": "",
            "reasoning": "think first",
            "tool_calls": [{
                "id": "call_1",
                "type": "function",
                "function": {"name": "read", "arguments": "{\"path\":\"foo.rs\"}"}
            }],
        }),
        serde_json::json!({"role": "tool", "tool_call_id": "call_1", "content": "file"}),
    ];
    let config = AutoCompactConfig {
        token_threshold: 1,
        keep_last: 2,
        ..Default::default()
    };

    let runtime = crate::test_support::current_thread_runtime();
    let summary = runtime
        .block_on(auto_compact_messages(&mut messages, &config, None))
        .expect("compaction succeeds");

    assert!(summary.is_some());
    assert_eq!(messages[1]["role"], "user");
    assert_eq!(messages[2]["role"], "assistant");
    assert_eq!(messages[2]["tool_calls"][0]["id"], "call_1");
    assert_eq!(messages[3]["role"], "tool");
    assert_eq!(messages[3]["tool_call_id"], "call_1");
}

/// Regression (transcript integrity): a tool-heavy transcript whose only
/// user message is the pinned head has no interior user boundary, so the
/// split falls back to the naive `len - keep_last` index — which can land
/// BETWEEN an assistant tool_use message and its tool_result, orphaning
/// the result at the kept-window head. The split must snap to the start
/// of the request/result pair instead.
#[test]
fn auto_compact_never_splits_assistant_tool_use_from_its_result() {
    let tool_call = |id: &str| {
        serde_json::json!({
            "id": id,
            "type": "function",
            "function": {"name": "run", "arguments": "{}"}
        })
    };
    let mut messages = vec![
        serde_json::json!({"role": "user", "content": "task"}),
        serde_json::json!({"role": "assistant", "content": "", "tool_calls": [tool_call("c0")]}),
        serde_json::json!({"role": "tool", "tool_call_id": "c0", "content": "r0"}),
        serde_json::json!({"role": "assistant", "content": "", "tool_calls": [tool_call("c1")]}),
        serde_json::json!({"role": "tool", "tool_call_id": "c1", "content": "r1"}),
        serde_json::json!({"role": "assistant", "content": "", "tool_calls": [tool_call("c2")]}),
        serde_json::json!({"role": "tool", "tool_call_id": "c2", "content": "r2"}),
    ];
    // keep_last: 3 puts the naive split at index 4 — the tool_result for
    // c1 — exactly mid-pair.
    let config = AutoCompactConfig {
        token_threshold: 1,
        keep_first: 0,
        keep_last: 3,
        ..Default::default()
    };

    let runtime = crate::test_support::current_thread_runtime();
    let summary = runtime
        .block_on(auto_compact_messages(&mut messages, &config, None))
        .expect("compaction succeeds");
    assert!(summary.is_some(), "compaction should trigger");

    // Kept window: summary, then the INTACT c1 pair, then the c2 pair.
    assert_eq!(messages[0]["role"], "user", "summary head");
    assert_eq!(messages[1]["role"], "assistant");
    assert_eq!(messages[1]["tool_calls"][0]["id"], "c1");
    assert_eq!(messages[2]["role"], "tool");
    assert_eq!(messages[2]["tool_call_id"], "c1");
    assert_eq!(messages[3]["tool_calls"][0]["id"], "c2");
    assert_eq!(messages[4]["tool_call_id"], "c2");
    // No kept tool_result may reference a drained (missing) request.
    for (idx, message) in messages.iter().enumerate() {
        if message["role"] == "tool" {
            let id = message["tool_call_id"].as_str().expect("tool_call_id");
            let paired = messages[..idx].iter().any(|prev| {
                prev["tool_calls"]
                    .as_array()
                    .is_some_and(|calls| calls.iter().any(|call| call["id"] == id))
            });
            assert!(paired, "tool_result {id} orphaned in kept window");
        }
    }
}

#[test]
fn auto_compact_clamps_oversized_tool_output_to_max_chars() {
    // A large tool result in the *kept* window must be clamped to honor
    // `tool_output_max_chars`.
    let big = "x".repeat(4000);
    let big_len = big.len();
    let mut messages = vec![
        serde_json::json!({"role": "user", "content": "old task"}),
        serde_json::json!({"role": "assistant", "content": "old reply"}),
        serde_json::json!({"role": "user", "content": "new task"}),
        serde_json::json!({"role": "assistant", "content": "calling tool"}),
        serde_json::json!({"role": "tool", "tool_call_id": "call_1", "content": big}),
    ];
    let config = AutoCompactConfig {
        token_threshold: 1,
        keep_last: 2,
        tool_output_max_chars: 500,
        ..Default::default()
    };

    let runtime = crate::test_support::current_thread_runtime();
    let result = runtime
        .block_on(auto_compact_messages(&mut messages, &config, None))
        .expect("compaction succeeds");
    assert!(result.is_some(), "compaction should trigger");

    let tool_msg = messages
        .iter()
        .find(|message| message["role"] == "tool")
        .expect("tool message kept in window");
    // Pairing preserved...
    assert_eq!(tool_msg["tool_call_id"], "call_1");
    // ...and the oversized body was clamped well below its original size.
    let content = tool_msg["content"].as_str().expect("string content");
    assert!(
        content.len() < big_len,
        "tool output should be clamped: {} vs {}",
        content.len(),
        big_len
    );
    assert!(content.len() < 2000, "clamped near tool_output_max_chars");
}

/// (2) A pinned large tool-output is NOT clamped, while an unpinned one of
/// the same size IS.
#[test]
fn clamp_exempts_pinned_tool_output() {
    let pinned_big = format!(
        "## Exact current file text {}\n{}",
        NO_COMPACT_MARKER,
        "x".repeat(4000)
    );
    let pinned_len = pinned_big.len();
    let unpinned_big = "y".repeat(4000);
    let unpinned_len = unpinned_big.len();
    let mut messages = vec![
        serde_json::json!({"role": "user", "content": "old task"}),
        serde_json::json!({"role": "assistant", "content": "reply"}),
        serde_json::json!({"role": "user", "content": "new task"}),
        serde_json::json!({"role": "assistant", "content": "calling tools"}),
        serde_json::json!({"role": "tool", "tool_call_id": "c0", "content": unpinned_big}),
        serde_json::json!({"role": "tool", "tool_call_id": "c1", "content": pinned_big}),
        serde_json::json!({"role": "user", "content": "continue"}),
    ];
    let config = AutoCompactConfig {
        token_threshold: 1,
        keep_last: 4,
        tool_output_max_chars: 500,
        ..Default::default()
    };
    let runtime = crate::test_support::current_thread_runtime();
    runtime
        .block_on(auto_compact_messages(&mut messages, &config, None))
        .expect("compaction succeeds");

    let pinned_msg = messages
        .iter()
        .find(|m| m["tool_call_id"] == "c1")
        .expect("pinned tool message kept");
    assert_eq!(
        pinned_msg["content"].as_str().map(str::len),
        Some(pinned_len),
        "pinned output must be intact (unclamped)"
    );
    let unpinned_msg = messages
        .iter()
        .find(|m| m["tool_call_id"] == "c0")
        .expect("unpinned tool message kept");
    assert!(
        unpinned_msg["content"].as_str().map(str::len).unwrap() < unpinned_len,
        "unpinned output of the same size must be clamped"
    );
}

/// (3) Bounded policy: with MANY pinned outputs, only the latest
/// MAX_PINNED_SEGMENTS survive verbatim; older pinned duplicates compact —
/// so the pin can't prevent all compaction (and can't overflow the window
/// on a very long session).
#[test]
fn pin_bound_keeps_only_latest_segments() {
    // Build 6 distinct pinned, oversized edited-window snapshots
    // (gen 0 = oldest .. gen 5 = newest), each tagged with the marker and
    // long enough that masking would otherwise truncate it.
    let make = |gen: usize| {
        let body = (0..40)
            .map(|i| format!("marker-gen-{gen} body line {i}"))
            .collect::<Vec<_>>()
            .join("\n");
        serde_json::json!({
            "role": "user",
            "content": format!(
                "## Edited region now reads (gen {gen}) {}\n{}",
                NO_COMPACT_MARKER, body
            ),
        })
    };
    let archived: Vec<_> = (0..6).map(make).collect();

    // Unit-level: the index selection keeps exactly the latest N.
    let pinned = latest_pinned_indices(archived.iter(), |m| {
        m.get("content").and_then(|c| c.as_str())
    });
    assert_eq!(
        pinned.len(),
        MAX_PINNED_SEGMENTS,
        "only the latest MAX_PINNED_SEGMENTS are pinned"
    );
    assert!(pinned.contains(&5) && pinned.contains(&4) && pinned.contains(&3));
    assert!(!pinned.contains(&0) && !pinned.contains(&1) && !pinned.contains(&2));

    // End-to-end through the mask pass: the 3 newest snapshots survive
    // verbatim; the 3 oldest are masked, proving the pin cannot defeat all
    // compaction.
    let summary = observation_mask_compaction(&archived, archived.len());
    assert!(
        summary.contains("marker-gen-5")
            && summary.contains("marker-gen-4")
            && summary.contains("marker-gen-3"),
        "latest {MAX_PINNED_SEGMENTS} pinned snapshots survive verbatim: {summary}"
    );
    assert!(
        !summary.contains("marker-gen-0")
            && !summary.contains("marker-gen-1")
            && !summary.contains("marker-gen-2"),
        "older pinned snapshots are masked (bound enforced)"
    );
    assert!(summary.contains("masked]"), "older snapshots were masked");
}

/// (4) Regression: with NO pins, compaction behaves exactly as before.
#[test]
fn no_pins_preserves_prior_clamp_behavior() {
    let big = "x".repeat(4000);
    let big_len = big.len();
    let mut messages = vec![
        serde_json::json!({"role": "user", "content": "old task"}),
        serde_json::json!({"role": "assistant", "content": "old reply"}),
        serde_json::json!({"role": "user", "content": "new task"}),
        serde_json::json!({"role": "assistant", "content": "calling tool"}),
        serde_json::json!({"role": "tool", "tool_call_id": "call_1", "content": big}),
    ];
    let config = AutoCompactConfig {
        token_threshold: 1,
        keep_last: 2,
        tool_output_max_chars: 500,
        ..Default::default()
    };
    let runtime = crate::test_support::current_thread_runtime();
    let result = runtime
        .block_on(auto_compact_messages(&mut messages, &config, None))
        .expect("compaction succeeds");
    assert!(result.is_some());
    let tool_msg = messages
        .iter()
        .find(|m| m["role"] == "tool")
        .expect("tool kept");
    let content = tool_msg["content"].as_str().expect("string content");
    assert!(content.len() < big_len, "unpinned output clamped as before");
    assert!(content.len() < 2000, "clamped near tool_output_max_chars");
}

/// Harn records native results as `role: "tool_result"` (string or block
/// content) and text-channel results as framed `user` strings. All of them
/// clamp; a person's own message of the same size does not.
#[test]
fn clamp_rewrites_harn_tool_result_shapes_but_not_a_person_message() {
    let big = "z".repeat(4000);
    let framed = format!("[result of read_file]\n{big}\n[end of read_file result]\n");
    let person = format!("Please review this log:\n{big}");
    let typed_frame = format!("[result of pasted]\n{big}\n[end of pasted result]\n");
    let mut messages = vec![
        serde_json::json!({"role": "user", "content": "old task"}),
        serde_json::json!({"role": "assistant", "content": "old reply"}),
        serde_json::json!({"role": "user", "content": "new task"}),
        serde_json::json!({
            "role": "assistant",
            "content": "calling tools",
            "tool_calls": [
                {"id": "native", "type": "function", "function": {"name": "read_file", "arguments": "{}"}},
                {"id": "blocks", "type": "function", "function": {"name": "read_file", "arguments": "{}"}},
            ],
        }),
        serde_json::json!({"role": "tool_result", "tool_call_id": "native", "name": "read_file", "content": big}),
        serde_json::json!({
            "role": "tool_result",
            "tool_call_id": "blocks",
            "name": "read_file",
            "content": [{"type": "text", "text": big}],
        }),
        serde_json::json!({"role": "assistant", "content": "calling read_file in text"}),
        serde_json::json!({"role": "user", "content": framed, "_harn": {"kind": "tool_result"}}),
        serde_json::json!({"role": "user", "content": person}),
        serde_json::json!({"role": "user", "content": typed_frame}),
    ];
    let config = AutoCompactConfig {
        token_threshold: 1,
        keep_last: 8,
        tool_output_max_chars: 500,
        ..Default::default()
    };
    let runtime = crate::test_support::current_thread_runtime();
    runtime
        .block_on(auto_compact_messages(&mut messages, &config, None))
        .expect("compaction succeeds")
        .expect("compaction triggers");

    let native = messages
        .iter()
        .find(|m| m["tool_call_id"] == "native")
        .expect("native result kept");
    assert!(native["content"].as_str().expect("string").len() < 2000);
    let blocks = messages
        .iter()
        .find(|m| m["tool_call_id"] == "blocks")
        .expect("block result kept");
    assert!(
        blocks["content"][0]["text"]
            .as_str()
            .expect("text block")
            .len()
            < 2000
    );
    let text_channel = messages
        .iter()
        .find(|m| {
            m["content"]
                .as_str()
                .is_some_and(|c| c.starts_with("[result of read_file]"))
        })
        .expect("text-channel result kept");
    assert!(text_channel["content"].as_str().expect("string").len() < 2000);
    let kept_person = messages
        .iter()
        .find(|m| {
            m["content"]
                .as_str()
                .is_some_and(|c| c.starts_with("Please review"))
        })
        .expect("person message kept");
    assert_eq!(
        kept_person["content"].as_str().map(str::len),
        Some(person.len())
    );
    // A person who types the frame is still a person; only `_harn` facts mark a result.
    assert!(messages
        .iter()
        .any(|m| m["content"].as_str() == Some(typed_frame.as_str())));
}