harn-hostlib 0.10.29

Opt-in code-intelligence and deterministic-tool host builtins for the Harn VM
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Harn VM bridge for cancellation-safe machine-global host lease scopes.
//!
//! The lease registry and stale-recovery policy remain in [`crate::host_lease`].
//! This module owns only the Harn host-capability boundary: request validation,
//! explicit wire shaping, and backend error translation. Keeping the bridge
//! separate prevents product or orchestration callers from learning the SQLite
//! layout or parsing the `harn host lease` CLI envelope.

use std::collections::BTreeMap;
use std::time::Duration;

use harn_vm::{VmResourceGuardHandle, VmValue};

use crate::error::HostlibError;
use crate::host_lease::{
    HostLeaseAcquireReceipt, HostLeaseAcquireStatus, HostLeaseDeferReceipt, HostLeaseHandle,
    HostLeaseMetadataUpdateReceipt, HostLeasePriorityClass, HostLeaseReleaseReceipt,
    HostLeaseRequest, HostLeaseResourceClass, HostLeaseState, HostLeaseStore,
    DEFAULT_HOST_LEASE_DOMAIN,
};
use crate::registry::{BuiltinRegistry, HostlibCapability};
use crate::tools::args::{
    build_dict, dict_arg, optional_int, optional_string, require_string, str_value,
};

const STATUS_BUILTIN: &str = "hostlib_host_lease_status";
const ACQUIRE_BUILTIN: &str = "hostlib_host_lease_acquire";
const UPDATE_METADATA_BUILTIN: &str = "hostlib_host_lease_update_metadata";
const RELEASE_BUILTIN: &str = "hostlib_host_lease_release";
const MAX_WAIT_SLICE_MS: i64 = 5_000;

/// Read-only access to the authoritative local host-lease state.
///
/// The caller must name a local resource explicitly. This capability does not
/// invent remote observation, and it deliberately does not acquire, renew, or
/// release leases; those lifecycle operations need their own cancellation-safe
/// scope boundary.
#[derive(Default)]
pub struct HostLeaseCapability;

impl HostlibCapability for HostLeaseCapability {
    fn module_name(&self) -> &'static str {
        "host_lease"
    }

    fn register_builtins(&self, registry: &mut BuiltinRegistry) {
        registry.register_fn("host_lease", STATUS_BUILTIN, "status", handle_status);
        registry.register_fn("host_lease", ACQUIRE_BUILTIN, "acquire", handle_acquire);
        registry.register_fn(
            "host_lease",
            UPDATE_METADATA_BUILTIN,
            "update_metadata",
            handle_update_metadata,
        );
        registry.register_fn("host_lease", RELEASE_BUILTIN, "release", handle_release);
    }
}

fn handle_update_metadata(args: &[VmValue]) -> Result<VmValue, HostlibError> {
    let dict = dict_arg(UPDATE_METADATA_BUILTIN, args)?;
    let host = require_nonempty_string(UPDATE_METADATA_BUILTIN, &dict, "host")?;
    let resource_class = resource_class(UPDATE_METADATA_BUILTIN, dict.get("resource_class"))?;
    let domain = optional_string(UPDATE_METADATA_BUILTIN, &dict, "domain")?
        .unwrap_or_else(|| DEFAULT_HOST_LEASE_DOMAIN.to_string());
    let lease_id = require_nonempty_string(UPDATE_METADATA_BUILTIN, &dict, "lease_id")?;
    let metadata = string_map(UPDATE_METADATA_BUILTIN, dict.get("metadata"))?;
    let receipt = HostLeaseStore::from_env()
        .and_then(|store| {
            store.update_metadata_for_domain(&host, resource_class, &domain, &lease_id, metadata)
        })
        .map_err(|error| backend(UPDATE_METADATA_BUILTIN, error))?;
    metadata_update_to_value(&receipt)
}

fn handle_status(args: &[VmValue]) -> Result<VmValue, HostlibError> {
    let dict = dict_arg(STATUS_BUILTIN, args)?;
    let host = require_nonempty_string(STATUS_BUILTIN, &dict, "host")?;
    let resource_class = resource_class(STATUS_BUILTIN, dict.get("resource_class"))?;
    let domain = optional_string(STATUS_BUILTIN, &dict, "domain")?
        .unwrap_or_else(|| DEFAULT_HOST_LEASE_DOMAIN.to_string());
    let state = HostLeaseStore::from_env()
        .and_then(|store| store.status_for_domain(&host, resource_class, &domain))
        .map_err(|error| HostlibError::Backend {
            builtin: STATUS_BUILTIN,
            message: error.to_string(),
        })?;
    state_to_value(&state)
}

fn handle_acquire(args: &[VmValue]) -> Result<VmValue, HostlibError> {
    let dict = dict_arg(ACQUIRE_BUILTIN, args)?;
    let host = match optional_string(ACQUIRE_BUILTIN, &dict, "host")? {
        Some(host) if !host.trim().is_empty() => host,
        Some(_) => {
            return Err(HostlibError::InvalidParameter {
                builtin: ACQUIRE_BUILTIN,
                param: "host",
                message: "must be a non-empty string when provided".to_string(),
            });
        }
        None => HostLeaseStore::default_host(),
    };
    let owner = require_nonempty_string(ACQUIRE_BUILTIN, &dict, "owner")?;
    let resource_class = resource_class(ACQUIRE_BUILTIN, dict.get("resource_class"))?;
    let domain = optional_string(ACQUIRE_BUILTIN, &dict, "domain")?
        .unwrap_or_else(|| DEFAULT_HOST_LEASE_DOMAIN.to_string());
    let priority_class = priority_class(ACQUIRE_BUILTIN, dict.get("priority_class"))?;
    let ttl_ms = optional_positive_u64(ACQUIRE_BUILTIN, &dict, "ttl_ms")?;
    let wait_slice_ms = optional_int(ACQUIRE_BUILTIN, &dict, "wait_slice_ms", 0)?;
    if !(0..=MAX_WAIT_SLICE_MS).contains(&wait_slice_ms) {
        return Err(HostlibError::InvalidParameter {
            builtin: ACQUIRE_BUILTIN,
            param: "wait_slice_ms",
            message: format!("must be between 0 and {MAX_WAIT_SLICE_MS}"),
        });
    }
    let reason = optional_string(ACQUIRE_BUILTIN, &dict, "reason")?;
    let metadata = string_map(ACQUIRE_BUILTIN, dict.get("metadata"))?;
    let request = HostLeaseRequest {
        host,
        resource_class,
        domain,
        execution_context: None,
        owner,
        priority_class,
        ttl_ms,
        owner_pid: Some(std::process::id()),
        reason,
        metadata,
    };
    let store = HostLeaseStore::from_env().map_err(|error| backend(ACQUIRE_BUILTIN, error))?;
    let receipt = if wait_slice_ms == 0 {
        store.try_acquire(request)
    } else {
        store.acquire_wait(request, Duration::from_millis(wait_slice_ms as u64))
    }
    .map_err(|error| backend(ACQUIRE_BUILTIN, error))?;
    acquire_to_value(store, receipt)
}

fn handle_release(args: &[VmValue]) -> Result<VmValue, HostlibError> {
    let dict = dict_arg(RELEASE_BUILTIN, args)?;
    let Some(VmValue::ResourceGuard(guard)) = dict.get("guard") else {
        return Err(HostlibError::InvalidParameter {
            builtin: RELEASE_BUILTIN,
            param: "guard",
            message: "must be a resource_guard returned by host_lease.acquire".to_string(),
        });
    };
    guard
        .release()
        .map_err(|error| backend(RELEASE_BUILTIN, error))
}

fn backend(builtin: &'static str, error: impl std::fmt::Display) -> HostlibError {
    HostlibError::Backend {
        builtin,
        message: error.to_string(),
    }
}

fn resource_class(
    builtin: &'static str,
    value: Option<&VmValue>,
) -> Result<HostLeaseResourceClass, HostlibError> {
    match value {
        None | Some(VmValue::Nil) => Ok(HostLeaseResourceClass::WholeMachine),
        Some(VmValue::String(value)) if value.as_str() == "whole-machine" => {
            Ok(HostLeaseResourceClass::WholeMachine)
        }
        Some(VmValue::String(value)) if value.as_str() == "rust-heavy" => {
            Ok(HostLeaseResourceClass::RustHeavy)
        }
        _ => Err(HostlibError::InvalidParameter {
            builtin,
            param: "resource_class",
            message: "must be whole-machine or rust-heavy".to_string(),
        }),
    }
}

fn priority_class(
    builtin: &'static str,
    value: Option<&VmValue>,
) -> Result<HostLeasePriorityClass, HostlibError> {
    match value {
        None | Some(VmValue::Nil) => Ok(HostLeasePriorityClass::Deferrable),
        Some(VmValue::String(value)) => match value.as_str() {
            "interactive" => Ok(HostLeasePriorityClass::Interactive),
            "measurement" => Ok(HostLeasePriorityClass::Measurement),
            "ci-verify" => Ok(HostLeasePriorityClass::CiVerify),
            "deferrable" => Ok(HostLeasePriorityClass::Deferrable),
            _ => Err(HostlibError::InvalidParameter {
                builtin,
                param: "priority_class",
                message: "must be interactive, measurement, ci-verify, or deferrable".to_string(),
            }),
        },
        _ => Err(HostlibError::InvalidParameter {
            builtin,
            param: "priority_class",
            message: "must be a string".to_string(),
        }),
    }
}

fn optional_positive_u64(
    builtin: &'static str,
    dict: &harn_vm::value::DictMap,
    key: &'static str,
) -> Result<Option<u64>, HostlibError> {
    match dict.get(key) {
        None | Some(VmValue::Nil) => Ok(None),
        Some(VmValue::Int(value)) if *value > 0 => Ok(Some(*value as u64)),
        _ => Err(HostlibError::InvalidParameter {
            builtin,
            param: key,
            message: "must be a positive integer or nil".to_string(),
        }),
    }
}

fn string_map(
    builtin: &'static str,
    value: Option<&VmValue>,
) -> Result<BTreeMap<String, String>, HostlibError> {
    let Some(value) = value else {
        return Ok(BTreeMap::new());
    };
    if matches!(value, VmValue::Nil) {
        return Ok(BTreeMap::new());
    }
    let VmValue::Dict(entries) = value else {
        return Err(HostlibError::InvalidParameter {
            builtin,
            param: "metadata",
            message: "must be a dict of string values".to_string(),
        });
    };
    entries
        .iter()
        .map(|(key, value)| match value {
            VmValue::String(value) => Ok((key.to_string(), value.to_string())),
            _ => Err(HostlibError::InvalidParameter {
                builtin,
                param: "metadata",
                message: "must contain only string values".to_string(),
            }),
        })
        .collect()
}

fn acquire_to_value(
    store: HostLeaseStore,
    receipt: HostLeaseAcquireReceipt,
) -> Result<VmValue, HostlibError> {
    let handle = match receipt.handle.as_ref() {
        Some(handle) => handle_to_value(ACQUIRE_BUILTIN, handle)?,
        None => VmValue::Nil,
    };
    let defer = receipt
        .defer
        .as_ref()
        .map(defer_to_value)
        .transpose()?
        .unwrap_or(VmValue::Nil);
    let guard = if receipt.status == HostLeaseAcquireStatus::Acquired {
        let handle = receipt
            .handle
            .as_ref()
            .ok_or_else(|| HostlibError::Backend {
                builtin: ACQUIRE_BUILTIN,
                message: "acquired receipt omitted its lease handle".to_string(),
            })?;
        let host = handle.host.clone();
        let resource_class = handle.resource_class;
        let domain = handle.domain.clone();
        let lease_id = handle.lease_id.clone();
        VmValue::resource_guard(VmResourceGuardHandle::new("host_lease", move || {
            store
                .release_for_domain(&host, resource_class, &domain, &lease_id)
                .map(|receipt| release_to_value(&receipt))
                .map_err(|error| error.to_string())
        }))
    } else {
        VmValue::Nil
    };
    Ok(build_dict([
        (
            "schema_version",
            VmValue::Int(i64::from(receipt.schema_version)),
        ),
        (
            "status",
            str_value(match receipt.status {
                HostLeaseAcquireStatus::Acquired => "acquired",
                HostLeaseAcquireStatus::Deferred => "deferred",
            }),
        ),
        ("observed_at_ms", VmValue::Int(receipt.observed_at_ms)),
        ("waited_ms", VmValue::Int(receipt.waited_ms as i64)),
        ("handle", handle),
        ("defer", defer),
        ("guard", guard),
        (
            "recovered_stale_lease",
            VmValue::Bool(receipt.recovered_stale_lease),
        ),
        (
            "recovered",
            receipt
                .recovered
                .as_ref()
                .map(|handle| handle_to_value(ACQUIRE_BUILTIN, handle))
                .transpose()?
                .unwrap_or(VmValue::Nil),
        ),
    ]))
}

fn defer_to_value(defer: &HostLeaseDeferReceipt) -> Result<VmValue, HostlibError> {
    Ok(build_dict([
        ("host", str_value(&defer.host)),
        ("resource_class", str_value(defer.resource_class.as_str())),
        ("domain", str_value(&defer.domain)),
        ("deferred_reason", str_value(defer.deferred_reason.as_str())),
        ("observed_at_ms", VmValue::Int(defer.observed_at_ms)),
        (
            "next_wake_at_ms",
            defer
                .next_wake_at_ms
                .map(VmValue::Int)
                .unwrap_or(VmValue::Nil),
        ),
        (
            "deadline_at_ms",
            defer
                .deadline_at_ms
                .map(VmValue::Int)
                .unwrap_or(VmValue::Nil),
        ),
        (
            "active",
            defer
                .active
                .as_ref()
                .map(|handle| handle_to_value(ACQUIRE_BUILTIN, handle))
                .transpose()?
                .unwrap_or(VmValue::Nil),
        ),
    ]))
}

fn release_to_value(receipt: &HostLeaseReleaseReceipt) -> VmValue {
    build_dict([
        (
            "schema_version",
            VmValue::Int(i64::from(receipt.schema_version)),
        ),
        ("released", VmValue::Bool(receipt.released)),
        ("host", str_value(&receipt.host)),
        ("resource_class", str_value(receipt.resource_class.as_str())),
        ("domain", str_value(&receipt.domain)),
        ("lease_id", str_value(&receipt.lease_id)),
        ("observed_at_ms", VmValue::Int(receipt.observed_at_ms)),
    ])
}

fn metadata_update_to_value(
    receipt: &HostLeaseMetadataUpdateReceipt,
) -> Result<VmValue, HostlibError> {
    Ok(build_dict([
        (
            "schema_version",
            VmValue::Int(i64::from(receipt.schema_version)),
        ),
        ("updated", VmValue::Bool(receipt.updated)),
        ("observed_at_ms", VmValue::Int(receipt.observed_at_ms)),
        (
            "handle",
            receipt
                .handle
                .as_ref()
                .map(|handle| handle_to_value(UPDATE_METADATA_BUILTIN, handle))
                .transpose()?
                .unwrap_or(VmValue::Nil),
        ),
    ]))
}

fn require_nonempty_string(
    builtin: &'static str,
    dict: &harn_vm::value::DictMap,
    key: &'static str,
) -> Result<String, HostlibError> {
    let value = require_string(builtin, dict, key)?;
    if value.trim().is_empty() {
        return Err(HostlibError::InvalidParameter {
            builtin,
            param: key,
            message: "must be a non-empty string".to_string(),
        });
    }
    Ok(value)
}

fn state_to_value(state: &HostLeaseState) -> Result<VmValue, HostlibError> {
    let active = match state.active.as_ref() {
        Some(handle) => handle_to_value(STATUS_BUILTIN, handle)?,
        None => VmValue::Nil,
    };
    Ok(build_dict([
        (
            "schema_version",
            VmValue::Int(i64::from(state.schema_version)),
        ),
        ("host", str_value(&state.host)),
        ("resource_class", str_value(state.resource_class.as_str())),
        ("domain", str_value(&state.domain)),
        ("observed_at_ms", VmValue::Int(state.observed_at_ms)),
        ("active", active),
        (
            "recovered_stale_lease",
            VmValue::Bool(state.recovered_stale_lease),
        ),
        (
            "recovered",
            state
                .recovered
                .as_ref()
                .map(|handle| handle_to_value(STATUS_BUILTIN, handle))
                .transpose()?
                .unwrap_or(VmValue::Nil),
        ),
    ]))
}

fn handle_to_value(
    builtin: &'static str,
    handle: &HostLeaseHandle,
) -> Result<VmValue, HostlibError> {
    let owner_process_identity = match handle.owner_process_identity {
        Some(identity) => {
            VmValue::Int(i64::try_from(identity).map_err(|_| HostlibError::Backend {
                builtin,
                message: "owner process identity exceeds the Harn integer range".to_string(),
            })?)
        }
        None => VmValue::Nil,
    };
    let metadata = build_dict(
        handle
            .metadata
            .iter()
            .map(|(key, value)| (key.clone(), str_value(value))),
    );
    Ok(build_dict([
        (
            "schema_version",
            VmValue::Int(i64::from(handle.schema_version)),
        ),
        ("host", str_value(&handle.host)),
        ("resource_class", str_value(handle.resource_class.as_str())),
        ("domain", str_value(&handle.domain)),
        ("lease_id", str_value(&handle.lease_id)),
        ("owner", str_value(&handle.owner)),
        ("priority_class", str_value(handle.priority_class.as_str())),
        ("acquired_at_ms", VmValue::Int(handle.acquired_at_ms)),
        ("updated_at_ms", VmValue::Int(handle.updated_at_ms)),
        (
            "expires_at_ms",
            handle
                .expires_at_ms
                .map(VmValue::Int)
                .unwrap_or(VmValue::Nil),
        ),
        (
            "owner_pid",
            handle
                .owner_pid
                .map(i64::from)
                .map(VmValue::Int)
                .unwrap_or(VmValue::Nil),
        ),
        ("owner_process_identity", owner_process_identity),
        (
            "reason",
            handle
                .reason
                .as_deref()
                .map(str_value)
                .unwrap_or(VmValue::Nil),
        ),
        ("metadata", metadata),
    ]))
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use super::*;
    use crate::host_lease::{HostLeasePriorityClass, HostLeaseResourceClass};

    #[test]
    fn status_value_preserves_active_and_recovery_evidence() {
        let state = HostLeaseState {
            schema_version: 1,
            host: "mac-local".to_string(),
            resource_class: HostLeaseResourceClass::WholeMachine,
            domain: DEFAULT_HOST_LEASE_DOMAIN.to_string(),
            observed_at_ms: 42,
            active: None,
            recovered_stale_lease: true,
            recovered: Some(HostLeaseHandle {
                schema_version: 1,
                host: "mac-local".to_string(),
                resource_class: HostLeaseResourceClass::WholeMachine,
                domain: DEFAULT_HOST_LEASE_DOMAIN.to_string(),
                execution_context: None,
                lease_id: "lease-1".to_string(),
                owner: "owner".to_string(),
                priority_class: HostLeasePriorityClass::Measurement,
                acquired_at_ms: 10,
                updated_at_ms: 20,
                expires_at_ms: Some(30),
                owner_pid: Some(123),
                owner_process_identity: Some(456),
                reason: Some("measurement".to_string()),
                metadata: BTreeMap::from([("lane".to_string(), "meter".to_string())]),
            }),
        };

        let value = state_to_value(&state).expect("state converts");
        let VmValue::Dict(state) = value else {
            panic!("expected state dict");
        };
        assert!(matches!(
            state.get("recovered_stale_lease"),
            Some(VmValue::Bool(true))
        ));
        assert!(matches!(state.get("active"), Some(VmValue::Nil)));
        let Some(VmValue::Dict(recovered)) = state.get("recovered") else {
            panic!("expected recovered lease");
        };
        assert_eq!(
            recovered.get("priority_class").map(VmValue::display),
            Some("measurement".to_string())
        );
        assert_eq!(
            recovered
                .get("owner_process_identity")
                .map(VmValue::display),
            Some("456".to_string())
        );
    }
}