harn-vm 0.10.166

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
use super::*;
use crate::event_log::{
    install_active_event_log, install_memory_for_current_thread, open_event_log,
    pin_test_occurred_at_ms, reset_active_event_log, EventLogBackendKind, EventLogConfig,
};
use crate::events::{add_event_sink, clear_event_sinks, CollectorSink, EventLevel};
use std::path::Path;
use std::rc::Rc;

/// Build the `OffsetDateTime` that corresponds to a pinned event-log
/// `occurred_at_ms`, so `as_of` cutoffs can be expressed in the same
/// reference frame as `pin_test_occurred_at_ms` without touching the wall
/// clock.
fn offset_from_ms(ms: i64) -> OffsetDateTime {
    OffsetDateTime::from_unix_timestamp_nanos((ms as i128) * 1_000_000)
        .expect("epoch ms within OffsetDateTime range")
}

fn manifest_spec(id: &str, fingerprint: &str) -> TriggerBindingSpec {
    TriggerBindingSpec {
        id: id.to_string(),
        source: TriggerBindingSource::Manifest,
        ingress_subscription: false,
        kind: "webhook".to_string(),
        provider: ProviderId::from("github"),
        autonomy_tier: crate::AutonomyTier::ActAuto,
        handler: TriggerHandlerSpec::Worker {
            queue: format!("{id}-queue"),
        },
        dispatch_priority: crate::WorkerQueuePriority::Normal,
        when: None,
        when_budget: None,
        retry: TriggerRetryConfig::default(),
        match_events: vec!["issues.opened".to_string()],
        dedupe_key: Some("event.dedupe_key".to_string()),
        dedupe_retention_days: crate::triggers::DEFAULT_INBOX_RETENTION_DAYS,
        filter: Some("event.kind".to_string()),
        daily_cost_usd: Some(5.0),
        hourly_cost_usd: None,
        max_autonomous_decisions_per_hour: None,
        max_autonomous_decisions_per_day: None,
        on_budget_exhausted: crate::TriggerBudgetExhaustionStrategy::False,
        max_concurrent: Some(10),
        flow_control: crate::triggers::TriggerFlowControlConfig::default(),
        aggregation: None,
        manifest_path: None,
        package_name: Some("workspace".to_string()),
        definition_fingerprint: fingerprint.to_string(),
    }
}

fn install_test_memory_event_log() -> Arc<AnyEventLog> {
    install_memory_for_current_thread(512)
}

fn install_test_sqlite_event_log(base_dir: &Path) -> Arc<AnyEventLog> {
    let config = EventLogConfig {
        backend: EventLogBackendKind::Sqlite,
        file_dir: base_dir.join("events"),
        sqlite_path: base_dir.join("events.sqlite"),
        queue_depth: 512,
    };
    let log = open_event_log(&config).expect("open isolated sqlite event log");
    install_active_event_log(log)
}

fn dynamic_spec(id: &str) -> TriggerBindingSpec {
    TriggerBindingSpec {
        id: id.to_string(),
        source: TriggerBindingSource::Dynamic,
        ingress_subscription: false,
        kind: "webhook".to_string(),
        provider: ProviderId::from("github"),
        autonomy_tier: crate::AutonomyTier::ActAuto,
        handler: TriggerHandlerSpec::Worker {
            queue: format!("{id}-queue"),
        },
        dispatch_priority: crate::WorkerQueuePriority::Normal,
        when: None,
        when_budget: None,
        retry: TriggerRetryConfig::default(),
        match_events: vec!["issues.opened".to_string()],
        dedupe_key: None,
        dedupe_retention_days: crate::triggers::DEFAULT_INBOX_RETENTION_DAYS,
        filter: None,
        daily_cost_usd: None,
        hourly_cost_usd: None,
        max_autonomous_decisions_per_hour: None,
        max_autonomous_decisions_per_day: None,
        on_budget_exhausted: crate::TriggerBudgetExhaustionStrategy::False,
        max_concurrent: None,
        flow_control: crate::triggers::TriggerFlowControlConfig::default(),
        aggregation: None,
        manifest_path: None,
        package_name: None,
        definition_fingerprint: format!("dynamic:{id}"),
    }
}

#[tokio::test(flavor = "current_thread")]
async fn manifest_loaded_trigger_registers_with_zeroed_metrics() {
    clear_trigger_registry();

    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .expect("manifest trigger installs");

    let snapshots = snapshot_trigger_bindings();
    assert_eq!(snapshots.len(), 1);
    let binding = &snapshots[0];
    assert_eq!(binding.id, "github-new-issue");
    assert_eq!(binding.version, 1);
    assert_eq!(binding.state, TriggerState::Active);
    assert_eq!(binding.metrics, TriggerMetricsSnapshot::default());

    clear_trigger_registry();
}

#[tokio::test(flavor = "current_thread")]
async fn dynamic_register_assigns_unique_ids_and_rejects_duplicates() {
    clear_trigger_registry();

    let first = dynamic_register(dynamic_spec("dynamic-a"))
        .await
        .expect("first dynamic trigger");
    let second = dynamic_register(dynamic_spec("dynamic-b"))
        .await
        .expect("second dynamic trigger");
    assert_ne!(first, second);

    let error = dynamic_register(dynamic_spec("dynamic-a"))
        .await
        .expect_err("duplicate id should fail");
    assert!(matches!(error, TriggerRegistryError::DuplicateId(_)));

    clear_trigger_registry();
}

#[test]
fn expected_predicate_cost_average_does_not_overflow() {
    let binding = TriggerBinding::new(manifest_spec("costed", "v1"), 1);
    record_predicate_cost_sample(&binding, u64::MAX);
    record_predicate_cost_sample(&binding, u64::MAX);

    assert_eq!(expected_predicate_cost_usd_micros(&binding), u64::MAX);
}

#[tokio::test(flavor = "current_thread")]
async fn drain_waits_for_in_flight_events_before_terminating() {
    clear_trigger_registry();

    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .expect("manifest trigger installs");
    begin_in_flight("github-new-issue", 1).expect("start in-flight event");

    drain("github-new-issue").await.expect("drain succeeds");
    let binding = snapshot_trigger_bindings()
        .into_iter()
        .find(|binding| binding.id == "github-new-issue" && binding.version == 1)
        .expect("binding snapshot");
    assert_eq!(binding.state, TriggerState::Draining);
    assert_eq!(binding.metrics.in_flight, 1);

    finish_in_flight("github-new-issue", 1, TriggerDispatchOutcome::Dispatched)
        .await
        .expect("finish in-flight event");
    let binding = snapshot_trigger_bindings()
        .into_iter()
        .find(|binding| binding.id == "github-new-issue" && binding.version == 1)
        .expect("binding snapshot");
    assert_eq!(binding.state, TriggerState::Terminated);
    assert_eq!(binding.metrics.in_flight, 0);

    clear_trigger_registry();
}

#[tokio::test(flavor = "current_thread")]
async fn hot_reload_registers_new_version_while_old_binding_drains() {
    clear_trigger_registry();

    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .expect("initial manifest trigger installs");
    begin_in_flight("github-new-issue", 1).expect("start in-flight event");

    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v2")])
        .await
        .expect("updated manifest trigger installs");

    let snapshots = snapshot_trigger_bindings();
    assert_eq!(snapshots.len(), 2);
    let old = snapshots
        .iter()
        .find(|binding| binding.id == "github-new-issue" && binding.version == 1)
        .expect("old binding");
    let new = snapshots
        .iter()
        .find(|binding| binding.id == "github-new-issue" && binding.version == 2)
        .expect("new binding");
    assert_eq!(old.state, TriggerState::Draining);
    assert_eq!(new.state, TriggerState::Active);

    finish_in_flight("github-new-issue", 1, TriggerDispatchOutcome::Dispatched)
        .await
        .expect("finish old in-flight event");
    let old = snapshot_trigger_bindings()
        .into_iter()
        .find(|binding| binding.id == "github-new-issue" && binding.version == 1)
        .expect("old binding");
    assert_eq!(old.state, TriggerState::Terminated);

    clear_trigger_registry();
}

#[tokio::test(flavor = "current_thread")]
async fn restoring_a_draining_definition_keeps_in_flight_generations_distinct() {
    clear_trigger_registry();
    reset_active_event_log();
    let tempdir = tempfile::tempdir().unwrap();
    install_test_sqlite_event_log(tempdir.path());
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .unwrap();
    begin_in_flight("github-new-issue", 1).unwrap();
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v2")])
        .await
        .unwrap();
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .unwrap();
    let restored = resolve_live_trigger_binding("github-new-issue", None).unwrap();
    assert_eq!(restored.version, 3);
    begin_in_flight("github-new-issue", 3).unwrap();
    finish_in_flight("github-new-issue", 1, TriggerDispatchOutcome::Dispatched)
        .await
        .unwrap();
    assert_eq!(restored.metrics_snapshot().in_flight, 1);
    assert_eq!(restored.state_snapshot(), TriggerState::Active);
    finish_in_flight("github-new-issue", 3, TriggerDispatchOutcome::Dispatched)
        .await
        .unwrap();
    assert_eq!(restored.metrics_snapshot().dispatched, 1);
    clear_trigger_registry();
    reset_active_event_log();
    install_test_sqlite_event_log(tempdir.path());
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .unwrap();
    assert_eq!(
        resolve_live_trigger_binding("github-new-issue", None)
            .unwrap()
            .version,
        3
    );
    reset_active_event_log();
    clear_trigger_registry();
}

#[tokio::test(flavor = "current_thread")]
async fn gc_drops_terminated_versions_beyond_retention_limit() {
    clear_trigger_registry();

    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .expect("install v1");
    begin_in_flight("github-new-issue", 1).expect("pin v1");

    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v2")])
        .await
        .expect("install v2");
    finish_in_flight("github-new-issue", 1, TriggerDispatchOutcome::Dispatched)
        .await
        .expect("finish v1");

    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v3")])
        .await
        .expect("install v3");

    let snapshots = snapshot_trigger_bindings();
    let versions: Vec<u32> = snapshots
        .into_iter()
        .filter(|binding| binding.id == "github-new-issue")
        .map(|binding| binding.version)
        .collect();
    assert_eq!(versions, vec![2, 3]);

    clear_trigger_registry();
}

#[tokio::test(flavor = "current_thread")]
async fn current_binding_prefers_restored_definition_over_newer_history() {
    clear_trigger_registry();
    reset_active_event_log();
    install_test_memory_event_log();
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .unwrap();
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v2")])
        .await
        .unwrap();
    begin_in_flight("github-new-issue", 2).unwrap();
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .unwrap();
    let restored = resolve_live_trigger_binding("github-new-issue", None).unwrap();
    assert_eq!(restored.version, 1);
    assert_eq!(current_trigger_binding_snapshots().len(), 1);
    assert_eq!(current_trigger_binding_snapshots()[0].version, 1);
    assert!(snapshot_trigger_bindings()
        .iter()
        .any(|binding| binding.version == 2 && binding.state == TriggerState::Draining));
    finish_in_flight("github-new-issue", 2, TriggerDispatchOutcome::Dispatched)
        .await
        .unwrap();
    let current = current_trigger_binding_snapshots();
    assert_eq!(current.len(), 1);
    assert_eq!(current[0].version, 1);
    assert_eq!(current[0].state, TriggerState::Active);
    assert_eq!(
        resolve_live_trigger_binding("github-new-issue", None)
            .unwrap()
            .version,
        1
    );
    reset_active_event_log();
    clear_trigger_registry();
}

#[tokio::test(flavor = "current_thread")]
async fn lifecycle_transitions_append_to_event_log() {
    clear_trigger_registry();
    reset_active_event_log();
    let log = install_test_memory_event_log();

    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .expect("manifest trigger installs");
    begin_in_flight("github-new-issue", 1).expect("start in-flight event");
    drain("github-new-issue").await.expect("drain succeeds");
    finish_in_flight("github-new-issue", 1, TriggerDispatchOutcome::Dispatched)
        .await
        .expect("finish event");

    let topic = Topic::new("triggers.lifecycle").expect("valid lifecycle topic");
    let events = log
        .read_range(&topic, None, 32)
        .await
        .expect("read lifecycle events");
    let states: Vec<String> = events
        .into_iter()
        .filter_map(|(_, event)| {
            event
                .payload
                .get("to_state")
                .and_then(|value| value.as_str())
                .map(|value| value.to_string())
        })
        .collect();
    assert_eq!(
        states,
        vec![
            "registering".to_string(),
            "active".to_string(),
            "draining".to_string(),
            "terminated".to_string(),
        ]
    );

    reset_active_event_log();
    clear_trigger_registry();
}

#[tokio::test(flavor = "current_thread")]
async fn version_history_reuses_historical_version_after_restart() {
    clear_trigger_registry();
    reset_active_event_log();
    let tempdir = tempfile::tempdir().expect("tempdir");
    install_test_sqlite_event_log(tempdir.path());

    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .expect("initial manifest trigger installs");
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v2")])
        .await
        .expect("updated manifest trigger installs");

    clear_trigger_registry();
    reset_active_event_log();
    install_test_sqlite_event_log(tempdir.path());

    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v2")])
        .await
        .expect("manifest reload reuses historical version");

    let binding = snapshot_trigger_bindings()
        .into_iter()
        .find(|binding| binding.id == "github-new-issue")
        .expect("binding snapshot");
    assert_eq!(binding.version, 2);

    reset_active_event_log();
    clear_trigger_registry();
}

#[tokio::test(flavor = "current_thread")]
async fn binding_version_as_of_reports_historical_active_version() {
    clear_trigger_registry();
    reset_active_event_log();
    install_test_memory_event_log();

    // Pin the event-log timestamp instead of sleeping between captures:
    // v1's lifecycle events stamp at T1, v2's at T2 > T1, with zero
    // wall-clock dependence. `before_reload` sits at T1 (so v1 is active
    // but v2 is not yet), `after_reload` at T2 (so v2 is active).
    const T1_MS: i64 = 1_700_000_000_000;
    const T2_MS: i64 = T1_MS + 50;
    let before_reload = offset_from_ms(T1_MS);
    let after_reload = offset_from_ms(T2_MS);

    let clock = pin_test_occurred_at_ms(T1_MS);
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .expect("initial manifest trigger installs");
    drop(clock);

    let clock = pin_test_occurred_at_ms(T2_MS);
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v2")])
        .await
        .expect("updated manifest trigger installs");
    drop(clock);

    assert_eq!(
        binding_version_as_of("github-new-issue", before_reload).expect("version before reload"),
        1
    );
    assert_eq!(
        binding_version_as_of("github-new-issue", after_reload).expect("version after reload"),
        2
    );

    reset_active_event_log();
    clear_trigger_registry();
}

#[tokio::test(flavor = "current_thread")]
async fn resolve_live_or_as_of_logs_structured_gc_fallback() {
    clear_trigger_registry();
    reset_active_event_log();
    let sink = Rc::new(CollectorSink::new());
    clear_event_sinks();
    add_event_sink(sink.clone());
    install_test_memory_event_log();

    // Pin the event-log timestamps: v1..v3 land at T1, v4 at T2 > T1, with
    // `received_at` at T1 so the gc fallback resolves to v3 (the latest
    // binding active at-or-before `received_at`) while v4 is strictly later.
    const T1_MS: i64 = 1_700_000_000_000;
    const T2_MS: i64 = T1_MS + 50;
    let received_at = offset_from_ms(T1_MS);

    let clock = pin_test_occurred_at_ms(T1_MS);
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v1")])
        .await
        .expect("install v1");
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v2")])
        .await
        .expect("install v2");
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v3")])
        .await
        .expect("install v3");
    drop(clock);

    let clock = pin_test_occurred_at_ms(T2_MS);
    install_manifest_triggers(vec![manifest_spec("github-new-issue", "v4")])
        .await
        .expect("install v4");
    drop(clock);

    let binding = resolve_live_or_as_of(
        "github-new-issue",
        RecordedTriggerBinding {
            version: 1,
            received_at,
        },
    )
    .expect("resolve fallback binding");
    assert_eq!(binding.version, 3);

    let warning = sink
        .logs
        .borrow()
        .iter()
        .find(|log| log.category == "replay.binding_version_gc_fallback")
        .cloned()
        .expect("gc fallback warning");
    assert_eq!(warning.level, EventLevel::Warn);
    assert_eq!(
        warning.metadata.get("trigger_id"),
        Some(&serde_json::json!("github-new-issue"))
    );
    assert_eq!(
        warning.metadata.get("recorded_version"),
        Some(&serde_json::json!(1))
    );
    assert_eq!(
        warning.metadata.get("received_at"),
        Some(&serde_json::json!(received_at
            .format(&time::format_description::well_known::Rfc3339)
            .unwrap_or_else(|_| received_at.to_string())))
    );
    assert_eq!(
        warning.metadata.get("resolved_version"),
        Some(&serde_json::json!(3))
    );

    clear_event_sinks();
    crate::events::reset_event_sinks();
    reset_active_event_log();
    clear_trigger_registry();
}