harn-vm 0.10.41

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
//! Transcript history to an OpenAI-legal `messages` array.
//!
//! Every rule here exists because some OpenAI-compatible route rejects, or
//! silently mis-serves, a message sequence Harn can legitimately produce:
//! tool results that must sit adjacent to the call they answer, parallel
//! tool-call batches on single-call routes, orphaned results left behind by
//! compaction, images riding on a `role:"tool"` message, provider-private
//! fields, and reserved `<tool_call>` delimiters.

use std::collections::HashSet;

pub(super) fn enforce_tool_result_adjacency(
    messages: Vec<serde_json::Value>,
) -> Vec<serde_json::Value> {
    let mut normalized = Vec::with_capacity(messages.len());
    let mut cursor = 0;
    while cursor < messages.len() {
        let message = messages[cursor].clone();
        let Some(mut pending_ids) = assistant_tool_call_ids(&message) else {
            normalized.push(message);
            cursor += 1;
            continue;
        };

        normalized.push(message);
        cursor += 1;

        let mut results = Vec::new();
        let mut deferred = Vec::new();
        while cursor < messages.len() && !pending_ids.is_empty() {
            let next = messages[cursor].clone();
            let matching_ids = matching_tool_result_ids(&next, &pending_ids);
            if !matching_ids.is_empty() {
                for id in matching_ids {
                    pending_ids.remove(&id);
                }
                results.push(next);
                cursor += 1;
                continue;
            }
            if is_deferable_non_tool_message(&next) {
                deferred.push(next);
                cursor += 1;
                continue;
            }
            break;
        }

        normalized.extend(results);
        normalized.extend(deferred);
    }
    normalized
}

pub(super) fn drop_orphan_tool_result_messages(
    messages: Vec<serde_json::Value>,
) -> Vec<serde_json::Value> {
    let mut live_tool_call_ids = HashSet::new();
    let mut normalized = Vec::with_capacity(messages.len());
    for message in messages {
        match message.get("role").and_then(|role| role.as_str()) {
            Some("assistant") => {
                if let Some(ids) = assistant_tool_call_ids(&message) {
                    live_tool_call_ids.extend(ids);
                }
                normalized.push(message);
            }
            Some("tool") => match message.get("tool_call_id").and_then(|value| value.as_str()) {
                Some(id) if live_tool_call_ids.remove(id) => normalized.push(message),
                _ => {}
            },
            _ => normalized.push(message),
        }
    }
    normalized
}

pub(super) fn enforce_single_tool_call_history(
    messages: Vec<serde_json::Value>,
    has_native_tools: bool,
) -> Vec<serde_json::Value> {
    if has_native_tools {
        split_parallel_native_tool_call_history(messages)
    } else {
        strip_native_tool_metadata_from_text_history(messages)
    }
}

fn strip_native_tool_metadata_from_text_history(
    messages: Vec<serde_json::Value>,
) -> Vec<serde_json::Value> {
    messages
        .into_iter()
        .map(|mut message| {
            let Some(object) = message.as_object_mut() else {
                return message;
            };
            match object.get("role").and_then(|role| role.as_str()) {
                Some("assistant") => {
                    object.remove("tool_calls");
                }
                Some("tool") => {
                    object.insert(
                        "role".to_string(),
                        serde_json::Value::String("user".to_string()),
                    );
                    object.remove("name");
                    object.remove("tool_call_id");
                }
                _ => {}
            }
            message
        })
        .collect()
}

pub(super) fn split_parallel_native_tool_call_history(
    messages: Vec<serde_json::Value>,
) -> Vec<serde_json::Value> {
    let mut normalized = Vec::with_capacity(messages.len());
    let mut cursor = 0;
    while cursor < messages.len() {
        let message = messages[cursor].clone();
        let Some(tool_calls) = assistant_tool_calls(&message) else {
            normalized.push(message);
            cursor += 1;
            continue;
        };
        if tool_calls.len() <= 1 {
            normalized.push(message);
            cursor += 1;
            continue;
        }

        let ids = tool_calls
            .iter()
            .filter_map(|call| {
                call.get("id")
                    .and_then(|value| value.as_str())
                    .map(ToString::to_string)
            })
            .collect::<Vec<_>>();
        cursor += 1;

        let mut results_by_id: std::collections::BTreeMap<String, Vec<serde_json::Value>> =
            std::collections::BTreeMap::new();
        let mut deferred = Vec::new();
        let mut pending_ids = ids.iter().cloned().collect::<HashSet<_>>();
        while cursor < messages.len() && !pending_ids.is_empty() {
            let next = messages[cursor].clone();
            let matching_ids = matching_tool_result_ids(&next, &pending_ids);
            if !matching_ids.is_empty() {
                for id in matching_ids {
                    pending_ids.remove(&id);
                    results_by_id.entry(id).or_default().push(next.clone());
                }
                cursor += 1;
                continue;
            }
            if is_deferable_non_tool_message(&next) {
                deferred.push(next);
                cursor += 1;
                continue;
            }
            break;
        }

        for (idx, call) in tool_calls.into_iter().enumerate() {
            let mut assistant = message.clone();
            // Read the id from this call directly, not by positional index into
            // `ids`: `ids` was compacted with filter_map (calls lacking an id are
            // dropped), so `ids.get(idx)` misaligns once any call has no id and
            // would attach a tool result to the wrong assistant call.
            let call_id = call
                .get("id")
                .and_then(|value| value.as_str())
                .map(ToString::to_string);
            if let Some(object) = assistant.as_object_mut() {
                object.insert(
                    "tool_calls".to_string(),
                    serde_json::Value::Array(vec![call]),
                );
                if idx > 0 {
                    object.insert(
                        "content".to_string(),
                        serde_json::Value::String(String::new()),
                    );
                }
            }
            normalized.push(assistant);
            if let Some(id) = call_id {
                if let Some(results) = results_by_id.remove(&id) {
                    normalized.extend(results);
                }
            }
        }
        normalized.extend(deferred);
    }
    normalized
}

fn assistant_tool_calls(message: &serde_json::Value) -> Option<Vec<serde_json::Value>> {
    if message.get("role").and_then(|role| role.as_str()) != Some("assistant") {
        return None;
    }
    let calls = message.get("tool_calls")?.as_array()?.clone();
    if calls.is_empty() {
        None
    } else {
        Some(calls)
    }
}

fn assistant_tool_call_ids(message: &serde_json::Value) -> Option<HashSet<String>> {
    if message.get("role").and_then(|role| role.as_str()) != Some("assistant") {
        return None;
    }
    let ids: HashSet<String> = message
        .get("tool_calls")?
        .as_array()?
        .iter()
        .filter_map(|call| {
            call.get("id")
                .and_then(|value| value.as_str())
                .map(ToString::to_string)
        })
        .collect();
    if ids.is_empty() {
        None
    } else {
        Some(ids)
    }
}

fn matching_tool_result_ids(
    message: &serde_json::Value,
    pending_ids: &HashSet<String>,
) -> HashSet<String> {
    if message.get("role").and_then(|role| role.as_str()) != Some("tool") {
        return HashSet::new();
    }
    match message.get("tool_call_id").and_then(|value| value.as_str()) {
        Some(id) if pending_ids.contains(id) => HashSet::from([id.to_string()]),
        _ => HashSet::new(),
    }
}

fn is_deferable_non_tool_message(message: &serde_json::Value) -> bool {
    !matches!(
        message.get("role").and_then(|role| role.as_str()),
        Some("assistant" | "tool")
    )
}

/// Remap canonical `<tool_call>` delimiters to the non-special wire form for a
/// reserved-token model (no-op when `remap` is false). Applied to every outgoing
/// message; see [`crate::llm::tool_delimiter`].
pub(super) fn maybe_remap_tool_call_text(text: &str, remap: bool) -> String {
    if remap {
        crate::llm::tool_delimiter::canonical_to_wire(text)
    } else {
        text.to_string()
    }
}

/// Relocate image parts off `role:"tool"` messages onto a following `role:"user"`
/// message. OpenAI chat-completions rejects image content parts on a tool message
/// (`Image URLs are only allowed for messages with role 'user'`), so when a tool
/// result carries image parts (a computer-use screenshot, or any image-returning
/// tool) the tool message keeps its text parts and the images are carried by a
/// `user` message where OpenAI accepts them.
///
/// The images are NOT inserted inline right after each tool message: OpenAI
/// requires every tool message answering an assistant's `tool_calls` to be
/// contiguous, so an intervening `user` message inside a parallel tool-call batch
/// (`assistant[c1,c2]`, `tool(c1 image)`, `tool(c2)`) would be a 400. Instead the
/// stripped images are buffered across the contiguous run of tool results and
/// flushed as a single `user` message once the run ends (the next non-tool
/// message, or the end of history). Messages with no tool-message image content
/// pass through untouched.
pub(super) fn relocate_tool_message_images_to_user(
    msgs: Vec<serde_json::Value>,
) -> Vec<serde_json::Value> {
    fn is_image_part(part: &serde_json::Value) -> bool {
        matches!(
            part.get("type").and_then(|value| value.as_str()),
            Some("image_url") | Some("image")
        )
    }
    // Emit the buffered images (if any) as one `user` message; called when a
    // tool-result run ends so the images land after the whole run, never between
    // two tool messages.
    fn flush(out: &mut Vec<serde_json::Value>, pending: &mut Vec<serde_json::Value>) {
        if pending.is_empty() {
            return;
        }
        let mut user_content = vec![serde_json::json!({
            "type": "text",
            "text": "Screenshot(s) from the preceding tool result(s):",
        })];
        user_content.append(pending);
        out.push(serde_json::json!({"role": "user", "content": user_content}));
    }
    let mut out = Vec::with_capacity(msgs.len() + 1);
    let mut pending_images: Vec<serde_json::Value> = Vec::new();
    for message in msgs {
        let is_tool = message.get("role").and_then(|value| value.as_str()) == Some("tool");
        if !is_tool {
            // The tool-result run (if any) ended; land the buffered images just
            // before this non-tool message.
            flush(&mut out, &mut pending_images);
            out.push(message);
            continue;
        }
        let parts = message.get("content").and_then(|value| value.as_array());
        let Some(parts) = parts else {
            out.push(message);
            continue;
        };
        if !parts.iter().any(is_image_part) {
            out.push(message);
            continue;
        }
        let (images, text_parts): (Vec<_>, Vec<_>) = parts.iter().cloned().partition(is_image_part);
        // Keep the tool message with its text parts. OpenAI requires non-empty
        // content, so fall back to a short note when the result was image-only.
        let mut tool_message = message.clone();
        let tool_content = if text_parts.is_empty() {
            serde_json::json!("(screenshot returned; see the image in the following message)")
        } else {
            serde_json::Value::Array(text_parts)
        };
        if let Some(object) = tool_message.as_object_mut() {
            object.insert("content".to_string(), tool_content);
        }
        out.push(tool_message);
        pending_images.extend(images);
    }
    // Flush images from a tool-result run that ran to the end of history.
    flush(&mut out, &mut pending_images);
    out
}

fn remap_tool_call_content(content: &serde_json::Value) -> serde_json::Value {
    use crate::llm::tool_delimiter::canonical_to_wire;
    match content {
        serde_json::Value::String(s) => serde_json::Value::String(canonical_to_wire(s)),
        serde_json::Value::Array(parts) => serde_json::Value::Array(
            parts
                .iter()
                .map(|part| {
                    let mut part = part.clone();
                    if let Some(text) = part.get("text").and_then(|t| t.as_str()) {
                        let remapped = canonical_to_wire(text);
                        if let Some(obj) = part.as_object_mut() {
                            obj.insert("text".to_string(), serde_json::Value::String(remapped));
                        }
                    }
                    part
                })
                .collect(),
        ),
        other => other.clone(),
    }
}

pub(super) fn sanitize_openai_message_for_request(
    message: &mut serde_json::Value,
    remap_tool_call: bool,
    reasoning_history_wire_field: Option<crate::llm::capabilities::ReasoningHistoryWireField>,
) {
    let Some(object) = message.as_object_mut() else {
        return;
    };
    let role = object
        .get("role")
        .and_then(serde_json::Value::as_str)
        .map(str::to_string);

    // Harn stores private reasoning under its canonical `reasoning` key and
    // strips every provider-private field by default. A small number of routes
    // reject a valid tool-result follow-up unless their exact prior assistant
    // reasoning field is replayed. Project only Harn-owned canonical reasoning
    // through the typed catalog mode; never pass through arbitrary seeded
    // `reasoning_content` from transcript/history input.
    let replay_reasoning = (role.as_deref() == Some("assistant"))
        .then(|| object.get("reasoning").and_then(serde_json::Value::as_str))
        .flatten()
        .filter(|value| !value.is_empty())
        .map(str::to_owned);

    object.retain(|key, _| openai_message_key_allowed(role.as_deref(), key));

    if let (Some(field), Some(reasoning)) = (reasoning_history_wire_field, replay_reasoning) {
        object.insert(
            field.as_str().to_string(),
            serde_json::Value::String(reasoning),
        );
    }

    if let Some(content) = object.get("content").cloned() {
        let content = if remap_tool_call {
            remap_tool_call_content(&content)
        } else {
            content
        };
        object.insert(
            "content".to_string(),
            crate::llm::content::openai_content(&content),
        );
    }
    if let Some(tool_calls) = object.get_mut("tool_calls") {
        sanitize_openai_tool_calls_for_request(tool_calls);
    }
}

fn openai_message_key_allowed(role: Option<&str>, key: &str) -> bool {
    matches!(key, "role" | "content" | "name")
        || (key == "tool_calls" && role == Some("assistant"))
        || (key == "tool_call_id" && role == Some("tool"))
}

fn sanitize_openai_tool_calls_for_request(tool_calls: &mut serde_json::Value) {
    let Some(calls) = tool_calls.as_array_mut() else {
        return;
    };
    for call in calls {
        *call = normalize_openai_tool_call_for_request(call);
    }
}

fn normalize_openai_tool_call_for_request(call: &serde_json::Value) -> serde_json::Value {
    let Some(object) = call.as_object() else {
        return call.clone();
    };

    let mut normalized = serde_json::Map::new();
    if let Some(id) = object.get("id").cloned() {
        normalized.insert("id".to_string(), id);
    }
    normalized.insert(
        "type".to_string(),
        serde_json::Value::String("function".to_string()),
    );

    let function = object
        .get("function")
        .and_then(serde_json::Value::as_object);
    let name = function
        .and_then(|function| function.get("name"))
        .or_else(|| object.get("name"));
    let arguments = function
        .and_then(|function| function.get("arguments"))
        .or_else(|| object.get("arguments"));

    if name.is_some() || arguments.is_some() {
        let mut normalized_function = serde_json::Map::new();
        if let Some(name) = name.cloned() {
            normalized_function.insert("name".to_string(), name);
        }
        if let Some(arguments) = arguments {
            normalized_function.insert(
                "arguments".to_string(),
                openai_tool_arguments_string(arguments),
            );
        }
        normalized.insert(
            "function".to_string(),
            serde_json::Value::Object(normalized_function),
        );
    }

    serde_json::Value::Object(normalized)
}

fn openai_tool_arguments_string(arguments: &serde_json::Value) -> serde_json::Value {
    match arguments {
        serde_json::Value::String(_) => arguments.clone(),
        serde_json::Value::Null => serde_json::Value::String("{}".to_string()),
        other => serde_json::Value::String(
            serde_json::to_string(other).unwrap_or_else(|_| "{}".to_string()),
        ),
    }
}