harn-vm 0.10.49

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
use super::*;
use crate::event_log::{FileEventLog, MemoryEventLog};
use crate::orchestration::{save_run_record, RunRecord};
use futures::StreamExt;
use harn_session_store::{
    AppendEvent, CreateSession, ImportSession, SessionEventKind, SessionImporter, SessionStore,
    SqliteSessionStore,
};
use serde_json::json;

fn span(span_id: u64, parent_id: Option<u64>, metadata: serde_json::Value) -> RunTraceSpanRecord {
    RunTraceSpanRecord {
        trace_id: "trace-1".to_string(),
        span_id,
        parent_id,
        kind: if parent_id.is_some() {
            "tool_call".to_string()
        } else {
            "pipeline".to_string()
        },
        name: format!("span-{span_id}"),
        start_ms: span_id * 10,
        duration_ms: 5,
        ttft_ms: None,
        metadata: serde_json::from_value(metadata).unwrap_or_default(),
        links: Vec::new(),
        cost_usd: None,
    }
}

#[tokio::test]
async fn persisted_transcript_projects_stable_tool_revision_and_identity_links() {
    let temp = tempfile::tempdir().expect("project root");
    std::fs::create_dir(temp.path().join(".harn")).expect("state dir");
    let store = SqliteSessionStore::open(temp.path().join(".harn/session-store.sqlite"))
        .expect("canonical store");
    store
        .create(CreateSession {
            id: Some("session-1".to_string()),
            project_scope: Some(temp.path().to_string_lossy().into_owned()),
            ..CreateSession::default()
        })
        .await
        .expect("create session");
    let mut call = AppendEvent::new(
        SessionEventKind::ToolCall,
        json!({
            "transcript_event": {
                "text": "Read source",
                "metadata": {
                    "tool_name": "read_file",
                    "input": {"path": "src/lib.rs"}
                }
            }
        }),
    );
    call.headers = BTreeMap::from([
        ("run_id".to_string(), "run-1".to_string()),
        ("turn_id".to_string(), "turn-1".to_string()),
        ("source_event_id".to_string(), "source-1".to_string()),
        ("tool_call_id".to_string(), "tool-1".to_string()),
    ]);
    store.append("session-1", call).await.expect("append call");
    let mut result = AppendEvent::new(
        SessionEventKind::ToolResult,
        json!({
            "transcript_event": {
                "text": "source contents",
                "metadata": {
                    "is_error": false,
                    "output": {"bytes": 42}
                }
            }
        }),
    );
    result.headers = BTreeMap::from([
        ("run_id".to_string(), "run-1".to_string()),
        ("turn_id".to_string(), "turn-1".to_string()),
        ("source_event_id".to_string(), "source-2".to_string()),
        ("tool_call_id".to_string(), "tool-1".to_string()),
    ]);
    store
        .append("session-1", result)
        .await
        .expect("append result");

    let snapshot = query_persisted_session_timeline(
        temp.path(),
        SessionTimelineQuery::for_session("session-1"),
    )
    .await
    .expect("query timeline")
    .expect("persisted session");

    assert_eq!(snapshot.nodes.len(), 1);
    assert_eq!(
        snapshot.nodes[0].id,
        "session:session-1:run:run-1:turn:turn-1:tool:tool-1"
    );
    assert_eq!(snapshot.nodes[0].status, "completed");
    assert_eq!(snapshot.nodes[0].name, "read_file");
    assert_eq!(
        snapshot.nodes[0].attributes["input"],
        json!({"path": "src/lib.rs"})
    );
    assert_eq!(snapshot.nodes[0].attributes["output"], json!({"bytes": 42}));
    assert_eq!(snapshot.nodes[0].attributes["isError"], json!(false));
    assert!(snapshot.nodes[0].start_ms.is_some());
    assert!(snapshot.nodes[0].duration_ms.is_some());
    assert_eq!(snapshot.nodes[0].attributes["revision"], json!(2));
    assert!(snapshot.nodes[0]
        .links
        .iter()
        .any(|link| link.kind == "turn" && link.target_id.as_deref() == Some("turn-1")));
    assert_eq!(
        snapshot.nodes[0]
            .links
            .iter()
            .map(|link| (link.kind.as_str(), link.target_id.as_deref()))
            .collect::<Vec<_>>(),
        vec![
            ("run", Some("run-1")),
            ("turn", Some("turn-1")),
            ("source_event", Some("source-2")),
            ("tool_call", Some("tool-1")),
        ]
    );
    assert_eq!(
        snapshot
            .cursor
            .topics
            .get("session-store:session-1")
            .copied(),
        Some(2)
    );
}

#[tokio::test]
async fn persisted_timeline_distinguishes_an_empty_session_from_a_missing_one() {
    let store = SqliteSessionStore::open_in_memory().expect("canonical store");
    let missing =
        query_session_store_timeline(&store, SessionTimelineQuery::for_session("missing-session"))
            .await
            .expect("query missing session");
    assert!(missing.is_none());

    store
        .create(CreateSession {
            id: Some("empty-session".to_string()),
            ..CreateSession::default()
        })
        .await
        .expect("create empty session");
    let empty =
        query_session_store_timeline(&store, SessionTimelineQuery::for_session("empty-session"))
            .await
            .expect("query empty session")
            .expect("empty session exists");
    assert!(empty.nodes.is_empty());
}

#[tokio::test]
async fn persisted_10k_event_tool_timeline_opens_under_500ms() {
    let temp = tempfile::tempdir().expect("project root");
    let store = SqliteSessionStore::open(temp.path().join("session-store.sqlite"))
        .expect("canonical store");
    let mut events = Vec::with_capacity(10_000);
    for index in 0..5_000 {
        let tool_call_id = format!("tool-{index}");
        let mut call = AppendEvent::new(
            SessionEventKind::ToolCall,
            json!({"transcript_event": {"metadata": {"tool_name": "read_file"}}}),
        );
        call.headers
            .insert("tool_call_id".to_string(), tool_call_id.clone());
        call.headers
            .insert("run_id".to_string(), "perf-run".to_string());
        call.headers
            .insert("turn_id".to_string(), "perf-turn".to_string());
        let mut result = AppendEvent::new(
            SessionEventKind::ToolResult,
            json!({"transcript_event": {"metadata": {"is_error": false}}}),
        );
        result
            .headers
            .insert("tool_call_id".to_string(), tool_call_id);
        result
            .headers
            .insert("run_id".to_string(), "perf-run".to_string());
        result
            .headers
            .insert("turn_id".to_string(), "perf-turn".to_string());
        events.extend([call, result]);
    }
    store
        .import(ImportSession {
            source_id: "timeline-perf-corpus".to_string(),
            source_digest: "sha256:timeline-perf-corpus".to_string(),
            session: CreateSession {
                id: Some("timeline-perf-session".to_string()),
                project_scope: Some(temp.path().to_string_lossy().into_owned()),
                ..CreateSession::default()
            },
            events,
        })
        .await
        .expect("import timeline corpus");

    let started = std::time::Instant::now();
    let snapshot = query_session_store_timeline(
        &store,
        SessionTimelineQuery {
            limit: Some(10_000),
            ..SessionTimelineQuery::for_session("timeline-perf-session")
        },
    )
    .await
    .expect("project timeline")
    .expect("session exists");
    let elapsed = started.elapsed();
    eprintln!("10k-event tool timeline elapsed: {elapsed:?}");

    assert_eq!(snapshot.nodes.len(), 5_000);
    assert!(snapshot.nodes[0].id.ends_with(":tool:tool-0"));
    assert!(snapshot.nodes[4_999].id.ends_with(":tool:tool-4999"));
    let timing_covered = snapshot
        .nodes
        .iter()
        .filter(|node| node.start_ms.is_some() && node.duration_ms.is_some())
        .count();
    assert!(
        timing_covered * 100 >= snapshot.nodes.len() * 99,
        "completed-tool timing coverage was {timing_covered}/{}",
        snapshot.nodes.len()
    );
    assert!(
        elapsed < std::time::Duration::from_millis(500),
        "10k-event timeline took {elapsed:?}"
    );
}

#[test]
fn run_record_spans_project_parent_child_tree_and_redact_metadata() {
    let mut run = RunRecord {
        id: "run-1".to_string(),
        trace_spans: vec![
            span(
                2,
                Some(1),
                json!({"status": "ok", "api_key": "should-redact"}),
            ),
            span(1, None, json!({"session_id": "s1"})),
        ],
        ..RunRecord::default()
    };
    run.metadata
        .insert("project_id".to_string(), json!("project-1"));

    let snapshot = timeline_from_run_record(
        &run,
        SessionTimelineQuery {
            session_id: Some("s1".to_string()),
            run_id: Some("run-1".to_string()),
            project_id: Some("project-1".to_string()),
            ..SessionTimelineQuery::default()
        },
    );

    assert_eq!(snapshot.nodes.len(), 2);
    let root = snapshot
        .nodes
        .iter()
        .find(|node| node.id == "span:trace-1:1")
        .expect("root span node");
    assert_eq!(root.children, vec!["span:trace-1:2"]);
    let child = snapshot
        .nodes
        .iter()
        .find(|node| node.id == "span:trace-1:2")
        .expect("child span node");
    assert_eq!(child.parent_id.as_deref(), Some("span:trace-1:1"));
    assert_eq!(
        child.attributes["api_key"],
        json!(crate::redact::REDACTED_PLACEHOLDER)
    );
}

#[tokio::test]
async fn channel_emit_and_match_project_causal_links() {
    let log = AnyEventLog::Memory(MemoryEventLog::new(16));
    let topic = static_topic(crate::channels::CHANNEL_TRANSCRIPT_TOPIC);
    log.append(
        &topic,
        LogEvent::new(
            crate::channels::CHANNEL_EMIT_TRANSCRIPT_KIND,
            json!({
                "event_id": "evt-1",
                "name_resolved": "session:s1:updates",
                "scope": "session",
                "scope_id": "s1",
                "session_id": "s1",
                "payload_summary": {"kind": "object"},
                "span_id": 7
            }),
        ),
    )
    .await
    .unwrap();
    log.append(
        &topic,
        LogEvent::new(
            crate::channels::CHANNEL_MATCH_TRANSCRIPT_KIND,
            json!({
                "event_id": "evt-1",
                "name_resolved": "session:s1:updates",
                "scope": "session",
                "scope_id": "s1",
                "trigger_id": "trigger-1",
                "matched_in_session_id": "s1",
                "batch": {
                    "count": 2,
                    "constituent_event_ids": ["evt-a", "evt-b"]
                },
                "span_id": 8
            }),
        ),
    )
    .await
    .unwrap();

    let snapshot =
        query_session_timeline(Some(&log), None, SessionTimelineQuery::for_session("s1"))
            .await
            .unwrap();

    let emit = snapshot
        .nodes
        .iter()
        .find(|node| node.id == "channel:evt-1:emit")
        .expect("emit node");
    assert_eq!(emit.category, "channel");
    let matched = snapshot
        .nodes
        .iter()
        .find(|node| node.id == "channel:evt-1:match:trigger-1")
        .expect("match node");
    assert_eq!(
        matched.links[0].target_id.as_deref(),
        Some("channel:evt-1:emit")
    );
    assert!(matched.links.iter().any(|link| {
        link.kind == "channel_batch_member" && link.event_id.as_deref() == Some("evt-a")
    }));
    assert!(matched.links.iter().any(|link| {
        link.kind == "channel_batch_member" && link.event_id.as_deref() == Some("evt-b")
    }));
}

#[tokio::test]
async fn persisted_file_log_reads_agent_events() {
    let temp = tempfile::tempdir().unwrap();
    let log = AnyEventLog::File(FileEventLog::open(temp.path().to_path_buf(), 16).unwrap());
    let topic = agent_events_topic("s1");
    log.append(
        &topic,
        LogEvent::new(
            "tool_call",
            json!({
                "session_id": "s1",
                "event": {
                    "type": "tool_call",
                    "session_id": "s1",
                    "tool_call_id": "tool-1",
                    "tool_name": "read",
                    "status": "pending",
                    "raw_input": {"token": "should-redact"}
                }
            }),
        )
        .with_headers(BTreeMap::from([(
            "session_id".to_string(),
            "s1".to_string(),
        )])),
    )
    .await
    .unwrap();
    log.flush().await.unwrap();

    let snapshot = query_session_timeline(
        Some(&log),
        None,
        SessionTimelineQuery {
            session_id: Some("s1".to_string()),
            run_id: Some("run_session_timeline_filter_00000000".to_string()),
            ..SessionTimelineQuery::default()
        },
    )
    .await
    .unwrap();

    assert_eq!(snapshot.nodes.len(), 1);
    assert_eq!(snapshot.nodes[0].category, "agent_event");
    assert_eq!(
        snapshot.nodes[0].attributes["event"]["raw_input"]["token"],
        json!(crate::redact::REDACTED_PLACEHOLDER)
    );
}

#[tokio::test]
async fn persisted_run_record_reads_nested_spans() {
    let temp = tempfile::tempdir().unwrap();
    let run_path = temp.path().join("run-1.json");
    let mut run = RunRecord {
        id: "run-1".to_string(),
        trace_spans: vec![
            span(1, None, json!({"session_id": "s1"})),
            span(2, Some(1), json!({"session_id": "s1"})),
        ],
        ..RunRecord::default()
    };
    run.metadata
        .insert("project_id".to_string(), json!("project-1"));
    save_run_record(&run, Some(run_path.to_str().unwrap())).unwrap();

    let snapshot = query_session_timeline(
        None,
        None,
        SessionTimelineQuery {
            session_id: Some("s1".to_string()),
            run_id: Some("run-1".to_string()),
            run_path: Some(run_path.display().to_string()),
            project_id: Some("project-1".to_string()),
            ..SessionTimelineQuery::default()
        },
    )
    .await
    .unwrap();

    assert_eq!(snapshot.nodes.len(), 2);
    assert_eq!(snapshot.nodes[0].id, "span:trace-1:1");
    assert_eq!(snapshot.nodes[0].children, vec!["span:trace-1:2"]);
    assert_eq!(
        snapshot.nodes[1].parent_id.as_deref(),
        Some("span:trace-1:1")
    );
}

#[tokio::test]
async fn subscription_streams_live_appends_without_polling() {
    let log = Arc::new(AnyEventLog::Memory(MemoryEventLog::new(16)));
    let mut stream =
        subscribe_session_timeline(log.clone(), SessionTimelineQuery::for_session("s1"))
            .await
            .unwrap();
    let topic = agent_events_topic("s1");
    log.append(
        &topic,
        LogEvent::new(
            "agent_message_chunk",
            json!({
                "session_id": "s1",
                "event": {
                    "type": "agent_message_chunk",
                    "session_id": "s1",
                    "content": "hello"
                }
            }),
        )
        .with_headers(BTreeMap::from([(
            "session_id".to_string(),
            "s1".to_string(),
        )])),
    )
    .await
    .unwrap();

    let update = stream.next().await.unwrap().unwrap();
    assert_eq!(update.node.category, "agent_event");
    assert_eq!(update.node.name, "agent_message_chunk");
    assert_eq!(update.cursor.topics.get(topic.as_str()).copied(), Some(1));
}