harn-vm 0.7.56

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
407
408
409
410
//! Builtins for first-class sessions.
//!
//! Sessions are the north-star replacement for the `transcript_policy`
//! config dict. Each builtin is an explicit verb over the session
//! store in `crate::agent_sessions`. There is no policy-as-verb
//! pattern; unknown inputs are hard errors.

use std::collections::BTreeMap;
use std::rc::Rc;

use crate::agent_sessions;
use crate::value::{VmError, VmValue};
use crate::vm::Vm;

pub fn register_agent_session_builtins(vm: &mut Vm) {
    register_open(vm);
    register_exists(vm);
    register_length(vm);
    register_snapshot(vm);
    register_ancestry(vm);
    register_current_id(vm);
    register_tool_format(vm);
    register_claim_tool_format(vm);
    register_reset(vm);
    register_fork(vm);
    register_fork_at(vm);
    register_close(vm);
    register_trim(vm);
    register_inject(vm);
    register_compact(vm);
}

fn err(msg: impl Into<String>) -> VmError {
    VmError::Thrown(VmValue::String(Rc::from(msg.into())))
}

fn arg_string_opt(
    args: &[VmValue],
    idx: usize,
    fn_name: &str,
    arg_name: &str,
) -> Result<Option<String>, VmError> {
    match args.get(idx) {
        None | Some(VmValue::Nil) => Ok(None),
        Some(VmValue::String(s)) => Ok(Some(s.to_string())),
        _ => Err(err(format!(
            "{fn_name}: `{arg_name}` must be a string or nil"
        ))),
    }
}

fn arg_string_required(
    args: &[VmValue],
    idx: usize,
    fn_name: &str,
    arg_name: &str,
) -> Result<String, VmError> {
    match args.get(idx) {
        Some(VmValue::String(s)) => Ok(s.to_string()),
        _ => Err(err(format!("{fn_name}: `{arg_name}` must be a string"))),
    }
}

fn arg_int_required(
    args: &[VmValue],
    idx: usize,
    fn_name: &str,
    arg_name: &str,
) -> Result<i64, VmError> {
    args.get(idx)
        .and_then(VmValue::as_int)
        .ok_or_else(|| err(format!("{fn_name}: `{arg_name}` must be an int")))
}

fn register_open(vm: &mut Vm) {
    vm.register_builtin("agent_session_open", |args, _out| {
        let id = arg_string_opt(args, 0, "agent_session_open", "id")?;
        let resolved = agent_sessions::open_or_create(id);
        Ok(VmValue::String(Rc::from(resolved)))
    });
}

fn register_exists(vm: &mut Vm) {
    vm.register_builtin("agent_session_exists", |args, _out| {
        let id = arg_string_required(args, 0, "agent_session_exists", "id")?;
        Ok(VmValue::Bool(agent_sessions::exists(&id)))
    });
}

fn register_length(vm: &mut Vm) {
    vm.register_builtin("agent_session_length", |args, _out| {
        let id = arg_string_required(args, 0, "agent_session_length", "id")?;
        match agent_sessions::length(&id) {
            Some(n) => Ok(VmValue::Int(n as i64)),
            None => Err(err(format!(
                "agent_session_length: unknown session id '{id}'"
            ))),
        }
    });
}

fn register_snapshot(vm: &mut Vm) {
    vm.register_builtin("agent_session_snapshot", |args, _out| {
        let id = arg_string_required(args, 0, "agent_session_snapshot", "id")?;
        Ok(agent_sessions::snapshot(&id).unwrap_or(VmValue::Nil))
    });
}

fn register_ancestry(vm: &mut Vm) {
    vm.register_builtin("agent_session_ancestry", |args, _out| {
        let id = arg_string_required(args, 0, "agent_session_ancestry", "id")?;
        let Some(ancestry) = agent_sessions::ancestry(&id) else {
            return Ok(VmValue::Nil);
        };
        Ok(VmValue::Dict(Rc::new(BTreeMap::from([
            (
                "parent_id".to_string(),
                ancestry
                    .parent_id
                    .map(|value| VmValue::String(Rc::from(value)))
                    .unwrap_or(VmValue::Nil),
            ),
            (
                "child_ids".to_string(),
                VmValue::List(Rc::new(
                    ancestry
                        .child_ids
                        .into_iter()
                        .map(|value| VmValue::String(Rc::from(value)))
                        .collect(),
                )),
            ),
            (
                "root_id".to_string(),
                VmValue::String(Rc::from(ancestry.root_id)),
            ),
        ]))))
    });
}

/// Return the innermost active agent session id for the currently
/// executing thread, or `nil` when no session is active.
fn register_current_id(vm: &mut Vm) {
    vm.register_builtin("agent_session_current_id", |_args, _out| {
        Ok(agent_sessions::current_session_id()
            .map(|id| VmValue::String(Rc::from(id)))
            .unwrap_or(VmValue::Nil))
    });
}

fn register_tool_format(vm: &mut Vm) {
    vm.register_builtin("agent_session_tool_format", |args, _out| {
        let id = arg_string_required(args, 0, "agent_session_tool_format", "id")?;
        if !agent_sessions::exists(&id) {
            return Err(err(format!(
                "agent_session_tool_format: unknown session id '{id}'"
            )));
        }
        Ok(agent_sessions::tool_format(&id)
            .map(|value| VmValue::String(Rc::from(value)))
            .unwrap_or(VmValue::Nil))
    });
}

fn register_claim_tool_format(vm: &mut Vm) {
    vm.register_builtin("agent_session_claim_tool_format", |args, _out| {
        let id = arg_string_required(args, 0, "agent_session_claim_tool_format", "id")?;
        let tool_format =
            arg_string_required(args, 1, "agent_session_claim_tool_format", "tool_format")?;
        agent_sessions::claim_tool_format(&id, &tool_format)
            .map_err(|message| err(format!("agent_session_claim_tool_format: {message}")))?;
        Ok(VmValue::Nil)
    });
}

fn register_reset(vm: &mut Vm) {
    vm.register_builtin("agent_session_reset", |args, _out| {
        let id = arg_string_required(args, 0, "agent_session_reset", "id")?;
        if !agent_sessions::reset_transcript(&id) {
            return Err(err(format!(
                "agent_session_reset: unknown session id '{id}'"
            )));
        }
        Ok(VmValue::Nil)
    });
}

fn register_fork(vm: &mut Vm) {
    vm.register_builtin("agent_session_fork", |args, _out| {
        let src = arg_string_required(args, 0, "agent_session_fork", "src")?;
        let dst = arg_string_opt(args, 1, "agent_session_fork", "dst")?;
        if !agent_sessions::exists(&src) {
            return Err(err(format!(
                "agent_session_fork: unknown session id '{src}'"
            )));
        }
        match agent_sessions::fork(&src, dst) {
            Some(new_id) => Ok(VmValue::String(Rc::from(new_id))),
            None => Err(err(format!(
                "agent_session_fork: failed to fork session '{src}'"
            ))),
        }
    });
}

fn register_fork_at(vm: &mut Vm) {
    vm.register_builtin("agent_session_fork_at", |args, _out| {
        let src = arg_string_required(args, 0, "agent_session_fork_at", "src")?;
        let keep_first = arg_int_required(args, 1, "agent_session_fork_at", "keep_first")?;
        if keep_first < 0 {
            return Err(err("agent_session_fork_at: `keep_first` must be >= 0"));
        }
        let dst = arg_string_opt(args, 2, "agent_session_fork_at", "dst")?;
        if !agent_sessions::exists(&src) {
            return Err(err(format!(
                "agent_session_fork_at: unknown session id '{src}'"
            )));
        }
        match agent_sessions::fork_at(&src, keep_first as usize, dst) {
            Some(new_id) => Ok(VmValue::String(Rc::from(new_id))),
            None => Err(err(format!(
                "agent_session_fork_at: failed to fork session '{src}'"
            ))),
        }
    });
}

fn register_close(vm: &mut Vm) {
    vm.register_builtin("agent_session_close", |args, _out| {
        let id = arg_string_required(args, 0, "agent_session_close", "id")?;
        if !agent_sessions::exists(&id) {
            return Err(err(format!(
                "agent_session_close: unknown session id '{id}'"
            )));
        }
        agent_sessions::close(&id);
        Ok(VmValue::Nil)
    });
}

fn register_trim(vm: &mut Vm) {
    vm.register_builtin("agent_session_trim", |args, _out| {
        let id = arg_string_required(args, 0, "agent_session_trim", "id")?;
        let keep_last = args
            .get(1)
            .and_then(|v| v.as_int())
            .ok_or_else(|| err("agent_session_trim: `keep_last` must be an int"))?;
        if keep_last < 0 {
            return Err(err("agent_session_trim: `keep_last` must be >= 0"));
        }
        let Some(kept) = agent_sessions::trim(&id, keep_last as usize) else {
            return Err(err(format!(
                "agent_session_trim: unknown session id '{id}'"
            )));
        };
        Ok(VmValue::Int(kept as i64))
    });
}

fn register_inject(vm: &mut Vm) {
    vm.register_builtin("agent_session_inject", |args, _out| {
        let id = arg_string_required(args, 0, "agent_session_inject", "id")?;
        if !agent_sessions::exists(&id) {
            return Err(err(format!(
                "agent_session_inject: unknown session id '{id}'"
            )));
        }
        let message = args
            .get(1)
            .cloned()
            .ok_or_else(|| err("agent_session_inject: `message` required"))?;
        agent_sessions::inject_message(&id, message).map_err(err)?;
        Ok(VmValue::Nil)
    });
}

fn register_compact(vm: &mut Vm) {
    vm.register_async_builtin("agent_session_compact", |args| async move {
        let id = arg_string_required(&args, 0, "agent_session_compact", "id")?;
        if !agent_sessions::exists(&id) {
            return Err(err(format!(
                "agent_session_compact: unknown session id '{id}'"
            )));
        }
        let opts_dict = match args.get(1) {
            Some(VmValue::Dict(d)) => (**d).clone(),
            None | Some(VmValue::Nil) => BTreeMap::new(),
            _ => return Err(err("agent_session_compact: `opts` must be a dict or nil")),
        };
        let config = build_compact_config(&opts_dict)?;
        let mut messages = agent_sessions::messages_json(&id);
        let original = messages.len();
        crate::orchestration::auto_compact_messages(&mut messages, &config, None).await?;
        let kept = messages.len();
        agent_sessions::replace_messages(&id, &messages);
        let _ = original;
        Ok(VmValue::Int(kept as i64))
    });
}

const COMPACT_OPT_KEYS: &[&str] = &[
    "keep_last",
    "token_threshold",
    "tool_output_max_chars",
    "compact_strategy",
    "hard_limit_tokens",
    "hard_limit_strategy",
    "custom_compactor",
    "mask_callback",
    "compress_callback",
];

fn build_compact_config(
    opts: &BTreeMap<String, VmValue>,
) -> Result<crate::orchestration::AutoCompactConfig, VmError> {
    for key in opts.keys() {
        if !COMPACT_OPT_KEYS.contains(&key.as_str()) {
            let expected = COMPACT_OPT_KEYS.join(", ");
            return Err(err(format!(
                "agent_session_compact: unknown option key '{key}' (expected one of: {expected})"
            )));
        }
    }
    let mut cfg = crate::orchestration::AutoCompactConfig::default();
    if let Some(v) = opts.get("keep_last").and_then(|v| v.as_int()) {
        if v < 0 {
            return Err(err("agent_session_compact: `keep_last` must be >= 0"));
        }
        cfg.keep_last = v as usize;
    }
    if let Some(v) = opts.get("token_threshold").and_then(|v| v.as_int()) {
        cfg.token_threshold = v as usize;
    }
    if let Some(v) = opts.get("tool_output_max_chars").and_then(|v| v.as_int()) {
        cfg.tool_output_max_chars = v as usize;
    }
    if let Some(VmValue::String(s)) = opts.get("compact_strategy") {
        cfg.compact_strategy = crate::orchestration::parse_compact_strategy(s)?;
    }
    if let Some(v) = opts.get("hard_limit_tokens").and_then(|v| v.as_int()) {
        cfg.hard_limit_tokens = Some(v as usize);
    }
    if let Some(VmValue::String(s)) = opts.get("hard_limit_strategy") {
        cfg.hard_limit_strategy = crate::orchestration::parse_compact_strategy(s)?;
    }
    if let Some(v) = opts.get("custom_compactor").cloned() {
        if !matches!(v, VmValue::Closure(_)) {
            return Err(err(
                "agent_session_compact: `custom_compactor` must be a closure",
            ));
        }
        cfg.custom_compactor = Some(v);
    }
    if let Some(v) = opts.get("mask_callback").cloned() {
        if !matches!(v, VmValue::Closure(_)) {
            return Err(err(
                "agent_session_compact: `mask_callback` must be a closure",
            ));
        }
        cfg.mask_callback = Some(v);
    }
    if let Some(v) = opts.get("compress_callback").cloned() {
        if !matches!(v, VmValue::Closure(_)) {
            return Err(err(
                "agent_session_compact: `compress_callback` must be a closure",
            ));
        }
        cfg.compress_callback = Some(v);
    }
    Ok(cfg)
}

#[cfg(test)]
mod tests {
    use crate::value::VmValue;

    fn call_current_id_builtin() -> VmValue {
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("runtime");
        rt.block_on(async {
            let local = tokio::task::LocalSet::new();
            local
                .run_until(async {
                    let mut vm = crate::Vm::new();
                    crate::register_vm_stdlib(&mut vm);
                    vm.call_named_builtin("agent_session_current_id", Vec::new())
                        .await
                        .expect("builtin call")
                })
                .await
        })
    }

    #[test]
    fn current_id_returns_nil_outside_active_session() {
        crate::reset_thread_local_state();
        assert!(matches!(call_current_id_builtin(), VmValue::Nil));
    }

    #[test]
    fn current_id_returns_active_session_id() {
        crate::reset_thread_local_state();
        crate::agent_sessions::push_current_session("unit-test-session".to_string());
        let current = call_current_id_builtin();
        crate::agent_sessions::pop_current_session();
        assert!(matches!(current, VmValue::String(value) if value.as_ref() == "unit-test-session"));
    }
}