harn-hostlib 0.10.140

Opt-in code-intelligence and deterministic-tool host builtins for the Harn VM
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
use super::*;
use filetime::FileTime;
use tempfile::tempdir;

/// A fixed instant for tests that inject `now`: the sweep compares the
/// directory mtimes the test sets against the `now` it passes, so the
/// wall clock never enters the decision.
fn fixed_now() -> SystemTime {
    std::time::UNIX_EPOCH + Duration::from_secs(1_700_000_000)
}

fn artifact_dir(parent: &Path, pid: u32, nanos: u128, counter: u64) -> PathBuf {
    parent.join(format!("harn-command-cmd_{pid}_{nanos}_{counter}"))
}

fn create_artifact_dir(parent: &Path, pid: u32, nanos: u128, counter: u64) -> PathBuf {
    let path = artifact_dir(parent, pid, nanos, counter);
    std::fs::create_dir(&path).unwrap();
    std::fs::write(path.join("combined.txt"), "output").unwrap();
    path
}

fn set_dir_mtime(path: &Path, time: SystemTime) {
    let file_time = FileTime::from_system_time(time);
    filetime::set_file_mtime(path, file_time).unwrap();
}

fn artifacts_in(dir: &Path) -> CommandArtifacts {
    CommandArtifacts {
        output_path: dir.join("combined.txt"),
        stdout_path: dir.join("stdout.txt"),
        stderr_path: dir.join("stderr.txt"),
        line_count: 0,
        byte_count: 0,
        output_sha256: String::new(),
    }
}

fn dead_pid() -> u32 {
    (900_000..=999_999)
        .find(|pid| {
            crate::process_liveness::process_liveness(*pid)
                == crate::process_liveness::ProcessLiveness::Dead
        })
        .expect("test host should have an unused high pid")
}

#[test]
fn command_artifact_sweep_deletes_stale_artifact_dirs() {
    let temp = tempdir().unwrap();
    let now = fixed_now();
    let stale = create_artifact_dir(temp.path(), dead_pid(), 100, 1);
    set_dir_mtime(&stale, now - Duration::from_secs(10));

    sweep_command_artifact_dirs(temp.path(), Duration::from_secs(5), DEFAULT_MAX_DIRS, now);

    assert!(!stale.exists());
}

#[test]
fn command_artifact_sweep_preserves_recent_artifact_dirs() {
    let temp = tempdir().unwrap();
    let now = fixed_now();
    let recent = create_artifact_dir(temp.path(), dead_pid(), 100, 1);
    set_dir_mtime(&recent, now - Duration::from_secs(3));

    sweep_command_artifact_dirs(temp.path(), Duration::from_secs(5), DEFAULT_MAX_DIRS, now);

    assert!(recent.exists());
}

#[test]
fn command_artifact_sweep_removes_completed_current_process_artifact_dirs() {
    let temp = tempdir().unwrap();
    let now = fixed_now();
    let completed = create_artifact_dir(temp.path(), std::process::id(), 100, 1);
    set_dir_mtime(&completed, now - Duration::from_secs(10));

    sweep_command_artifact_dirs(temp.path(), Duration::from_secs(5), DEFAULT_MAX_DIRS, now);

    assert!(!completed.exists());
}

#[test]
fn command_artifact_sweep_preserves_active_current_process_artifact_dirs() {
    let temp = tempdir().unwrap();
    let now = fixed_now();
    let active = create_artifact_dir(temp.path(), std::process::id(), 100, 1);
    let artifacts = CommandArtifacts {
        output_path: active.join("combined.txt"),
        stdout_path: active.join("stdout.txt"),
        stderr_path: active.join("stderr.txt"),
        line_count: 0,
        byte_count: 0,
        output_sha256: String::new(),
    };
    mark_artifacts_active(&artifacts).unwrap();
    set_dir_mtime(&active, now - Duration::from_secs(10));

    sweep_command_artifact_dirs(temp.path(), Duration::from_secs(5), DEFAULT_MAX_DIRS, now);

    assert!(active.exists());
    mark_artifacts_inactive(&artifacts);
}

#[test]
fn command_artifact_sweep_uses_cross_process_active_lease_not_pid_liveness() {
    let temp = tempdir().unwrap();
    let now = SystemTime::UNIX_EPOCH + Duration::from_hours(1);
    let active = create_artifact_dir(temp.path(), dead_pid(), 100, 1);
    let lease_path = active.join(ACTIVE_LEASE_FILE);
    let lease = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(lease_path)
        .unwrap();
    lease.lock().unwrap();
    set_dir_mtime(&active, now - Duration::from_secs(10));

    sweep_command_artifact_dirs(temp.path(), Duration::from_secs(5), DEFAULT_MAX_DIRS, now);
    assert!(active.exists());

    lease.unlock().unwrap();
    sweep_command_artifact_dirs(temp.path(), Duration::from_secs(5), DEFAULT_MAX_DIRS, now);
    assert!(!active.exists());
}

#[test]
fn contended_command_artifact_lease_names_itself() {
    let temp = tempdir().unwrap();
    let active = create_artifact_dir(temp.path(), dead_pid(), 100, 1);
    let artifacts = CommandArtifacts {
        output_path: active.join("combined.txt"),
        stdout_path: active.join("stdout.txt"),
        stderr_path: active.join("stderr.txt"),
        line_count: 0,
        byte_count: 0,
        output_sha256: String::new(),
    };
    let lease_path = active.join(ACTIVE_LEASE_FILE);
    let holder = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(&lease_path)
        .unwrap();
    holder.lock().unwrap();

    let error = mark_artifacts_active_with_timeout(&artifacts, Duration::ZERO).unwrap_err();

    assert!(error
        .to_string()
        .contains(&lease_path.display().to_string()));
    assert!(error.to_string().contains("timed out"));
}

#[test]
fn namespace_admission_precedes_artifact_directory_publication() {
    let temp = tempdir().unwrap();
    let dir = artifact_dir(temp.path(), std::process::id(), 200, 1);
    let artifacts = CommandArtifacts {
        output_path: dir.join("combined.txt"),
        stdout_path: dir.join("stdout.txt"),
        stderr_path: dir.join("stderr.txt"),
        line_count: 0,
        byte_count: 0,
        output_sha256: String::new(),
    };
    let namespace_path = artifact_namespace_lease_path(temp.path());
    let holder = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(&namespace_path)
        .unwrap();
    holder.lock().unwrap();

    let error =
        create_and_mark_artifacts_active_with_timeout(&artifacts, Duration::ZERO).unwrap_err();

    assert!(error
        .to_string()
        .contains(&namespace_path.display().to_string()));
    assert!(!dir.exists(), "directory became visible before its lease");
}

/// What this process currently holds open.
///
/// Every platform leaks the same way when a completed command keeps its
/// lease: one open handle per command. Only the way to count them differs,
/// so both platforms are measured rather than skipping one.
#[cfg(unix)]
fn open_descriptor_count() -> usize {
    let dir = if cfg!(target_os = "linux") {
        "/proc/self/fd"
    } else {
        "/dev/fd"
    };
    std::fs::read_dir(dir)
        .expect("the process must be able to enumerate its own descriptors")
        .count()
}

/// Windows has no descriptor directory, so ask the kernel for the count.
#[cfg(windows)]
fn open_descriptor_count() -> usize {
    use windows_sys::Win32::System::Threading::{GetCurrentProcess, GetProcessHandleCount};

    let mut handles: u32 = 0;
    // SAFETY: `GetCurrentProcess` returns a pseudo-handle that needs no close,
    // and `handles` is a live, correctly sized out-parameter for the call.
    let measured = unsafe { GetProcessHandleCount(GetCurrentProcess(), &mut handles) };
    assert!(
        measured != 0,
        "the process must be able to count its own handles"
    );
    handles as usize
}

/// The falsifier for the descriptor exhaustion, at the seam that owns the
/// lease.
///
/// Each completed command used to leave its active-lease descriptor open
/// until the artifact was retired, so a session paid one descriptor per
/// command. The retention cap is deliberately set far above the number of
/// commands here: if retirement were the only thing releasing descriptors
/// this would grow by one per command, which is the pre-fix behavior and
/// what the negative control shows.
#[test]
fn completed_commands_do_not_accumulate_lease_descriptors() {
    let temp = tempdir().unwrap();
    let unretired = 100_000;
    let cycle = |counter: u64| {
        let dir = artifact_dir(temp.path(), std::process::id(), 400, counter);
        let artifacts = artifacts_in(&dir);
        create_and_mark_artifacts_active_with_timeout(&artifacts, Duration::from_secs(5)).unwrap();
        register_completed_artifacts_with_guard_options(
            &format!("command-descriptors-{counter}"),
            Some(&format!("handle-descriptors-{counter}")),
            &artifacts,
            ActiveArtifactLeaseGuard::new(&artifacts),
            unretired,
            Duration::from_secs(5),
        )
        .unwrap();
        dir
    };

    // Warm up first: the namespace lease and any lazily-opened global
    // must already be counted, or their one-time cost reads as growth.
    for counter in 0..5 {
        cycle(counter);
    }
    let before = open_descriptor_count();
    for counter in 5..125 {
        cycle(counter);
    }
    let after = open_descriptor_count();

    // The gate runs each test in its own process, so this counts almost
    // nothing but the code under test. The slack covers descriptors the run
    // itself opens transiently. The defect this pins spends one per command,
    // so the bound sits far below the command count either way.
    const UNRELATED_SLACK: usize = 16;
    assert!(
        after <= before + UNRELATED_SLACK,
        "descriptor count grew from {before} to {after} across 120 completed commands"
    );
    assert!(
        ACTIVE_ARTIFACT_LEASES
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .is_empty(),
        "a completed command must hold no active lease"
    );
}

/// A dead session's lease is collected; a live one's is left alone.
///
/// Without this the namespace would gather one small file per harn
/// process that ever ran there, which is a slower version of the problem
/// the session lease exists to solve.
#[test]
fn stale_session_leases_are_collected_and_live_ones_are_kept() {
    let temp = tempdir().unwrap();
    let dead = session_lease_path(temp.path(), dead_pid());
    std::fs::write(&dead, b"").unwrap();

    // A live one, held exactly the way a running session holds it.
    let live_path = session_lease_path(temp.path(), dead_pid() - 1);
    let live = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(&live_path)
        .unwrap();
    live.lock().unwrap();

    // This process's own lease is never a candidate, held or not.
    let own = session_lease_path(temp.path(), std::process::id());
    std::fs::write(&own, b"").unwrap();

    sweep_stale_session_leases(temp.path());

    assert!(!dead.exists(), "a lease no session holds must be collected");
    assert!(live_path.exists(), "a held lease must be left alone");
    assert!(own.exists(), "this process's own lease must be left alone");
    live.unlock().unwrap();
}

#[test]
fn registration_failure_releases_active_lease_without_namespace_reacquisition() {
    let temp = tempdir().unwrap();
    let dir = artifact_dir(temp.path(), std::process::id(), 300, 1);
    let artifacts = artifacts_in(&dir);
    create_and_mark_artifacts_active_with_timeout(&artifacts, Duration::ZERO).unwrap();

    let namespace_path = artifact_namespace_lease_path(temp.path());
    let holder = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(&namespace_path)
        .unwrap();
    holder.lock().unwrap();

    let error = register_completed_artifacts_with_guard_options(
        "command-registration-failure",
        Some("handle-registration-failure"),
        &artifacts,
        ActiveArtifactLeaseGuard::new(&artifacts),
        1,
        Duration::ZERO,
    )
    .unwrap_err();

    assert!(error
        .to_string()
        .contains(&namespace_path.display().to_string()));
    assert!(
        !ACTIVE_ARTIFACT_LEASES
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .contains_key(&dir),
        "failure guard must release without waiting for the held namespace lock"
    );
    let probe = OpenOptions::new()
        .read(true)
        .write(true)
        .open(dir.join(ACTIVE_LEASE_FILE))
        .unwrap();
    probe.try_lock().unwrap();
    probe.unlock().unwrap();
    drop(probe);
    holder.unlock().unwrap();
    drop(holder);
}

#[test]
fn completed_fifo_evicts_oldest_aliases_and_releases_its_lease() {
    let temp = tempdir().unwrap();
    let first_dir = artifact_dir(temp.path(), std::process::id(), 400, 1);
    let second_dir = artifact_dir(temp.path(), std::process::id(), 400, 2);
    let first = artifacts_in(&first_dir);
    let second = artifacts_in(&second_dir);
    create_and_mark_artifacts_active_with_timeout(&first, Duration::ZERO).unwrap();
    create_and_mark_artifacts_active_with_timeout(&second, Duration::ZERO).unwrap();

    let mut store = ArtifactRegistry::default();
    store.by_id.insert("command-first".into(), first.clone());
    store.by_id.insert("handle-first".into(), first);
    store.by_id.insert("command-second".into(), second.clone());
    store.by_id.insert("handle-second".into(), second.clone());
    store.completed.push_back(CompletedArtifact {
        path: first_dir.clone(),
        completed_at: SystemTime::UNIX_EPOCH,
    });
    store.completed.push_back(CompletedArtifact {
        path: second_dir.clone(),
        completed_at: SystemTime::UNIX_EPOCH + Duration::from_secs(1),
    });

    retire_completed_artifacts_under_namespace(&mut store, 1, None);

    assert!(!store.by_id.contains_key("command-first"));
    assert!(!store.by_id.contains_key("handle-first"));
    assert!(store.by_id.contains_key("command-second"));
    assert!(store.by_id.contains_key("handle-second"));
    assert_eq!(store.completed.len(), 1);
    let active = ACTIVE_ARTIFACT_LEASES
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    assert!(!active.contains_key(&first_dir));
    assert!(active.contains_key(&second_dir));
    drop(active);
    mark_artifacts_inactive(&second);
}

#[test]
fn fallback_registration_uses_the_same_bounded_alias_fifo() {
    let temp = tempdir().unwrap();
    let first_dir = artifact_dir(temp.path(), std::process::id(), 500, 1);
    let second_dir = artifact_dir(temp.path(), std::process::id(), 500, 2);
    let first = artifacts_in(&first_dir);
    let second = artifacts_in(&second_dir);
    let mut store = ArtifactRegistry::default();

    register_completed_artifacts_in_store(
        &mut store,
        "fallback-command-first",
        Some("fallback-handle-first"),
        &first,
        1,
        ArtifactLeaseCleanup::LeaseOnly,
    );
    register_completed_artifacts_in_store(
        &mut store,
        "fallback-command-second",
        Some("fallback-handle-second"),
        &second,
        1,
        ArtifactLeaseCleanup::LeaseOnly,
    );

    assert!(!store.by_id.contains_key("fallback-command-first"));
    assert!(!store.by_id.contains_key("fallback-handle-first"));
    assert!(store.by_id.contains_key("fallback-command-second"));
    assert!(store.by_id.contains_key("fallback-handle-second"));
    assert_eq!(store.completed.len(), 1);
    assert_eq!(store.completed.front().unwrap().path, second_dir);
}

#[test]
fn command_artifact_sweep_preserves_malformed_names() {
    let temp = tempdir().unwrap();
    let now = fixed_now();
    let malformed = temp.path().join("harn-command-cmd_123_not-nanos_1");
    std::fs::create_dir(&malformed).unwrap();
    set_dir_mtime(&malformed, now - Duration::from_secs(10));

    sweep_command_artifact_dirs(temp.path(), Duration::from_secs(5), DEFAULT_MAX_DIRS, now);

    assert!(malformed.exists());
}

#[cfg(unix)]
#[test]
fn command_artifact_sweep_does_not_follow_symlinks() {
    use std::os::unix::fs::symlink;

    let temp = tempdir().unwrap();
    let now = fixed_now();
    let target = temp.path().join("target");
    std::fs::create_dir(&target).unwrap();
    std::fs::write(target.join("keep.txt"), "keep").unwrap();
    let link = artifact_dir(temp.path(), dead_pid(), 100, 1);
    symlink(&target, &link).unwrap();

    sweep_command_artifact_dirs(temp.path(), Duration::from_secs(5), DEFAULT_MAX_DIRS, now);

    assert!(link.exists());
    assert_eq!(
        std::fs::read_to_string(target.join("keep.txt")).unwrap(),
        "keep"
    );
}

#[test]
fn command_artifact_pressure_sweep_removes_oldest_dead_dirs_over_limit() {
    let temp = tempdir().unwrap();
    let now = fixed_now();
    let pid = dead_pid();
    let first = create_artifact_dir(temp.path(), pid, 100, 1);
    let second = create_artifact_dir(temp.path(), pid, 200, 1);
    let third = create_artifact_dir(temp.path(), pid, 300, 1);
    set_dir_mtime(&first, now - Duration::from_mins(30));
    set_dir_mtime(&second, now - Duration::from_mins(20));
    set_dir_mtime(&third, now - Duration::from_mins(10));

    sweep_command_artifact_dirs(temp.path(), Duration::from_hours(1), 2, now);

    assert!(!first.exists());
    assert!(second.exists());
    assert!(third.exists());
}