Skip to main content

harn_vm/
stdlib.rs

1//! Standard library builtins for the Harn VM.
2//!
3//! Every builtin is declared with the `#[harn_builtin]` proc-macro
4//! (`crate::stdlib::macros::harn_builtin`). Each annotation emits a sibling
5//! `static <FN>_DEF: VmBuiltinDef` carrying the signature, aliases, handler,
6//! and metadata, and registers it into the workspace-global
7//! [`macros::ALL_BUILTIN_DEFS`] distributed slice at link time. The CLI / LSP /
8//! lint / serve / dap binaries call [`force_link`] to defeat rlib dead-code
9//! stripping (linkme issue #36) so every static lands in the slice. Modules
10//! still expose a `register_<module>_builtins(vm)` helper for ordered eager
11//! registration (e.g. so `clock::timestamp` can override `process::timestamp`).
12//! `register_vm_stdlib` calls those helpers in order and then installs the
13//! aggregated signatures into the parser registry.
14//!
15//! See `CONTRIBUTING.md` ("Adding a stdlib builtin") for the full template.
16
17pub mod macros;
18
19mod agent_sessions;
20pub mod agent_state;
21pub(crate) mod agents;
22mod agents_daemon;
23mod artifact_emit;
24pub(crate) mod assemble;
25pub mod asset_paths;
26mod bytes;
27mod calendar;
28mod channel_guardrails;
29mod channels;
30pub(crate) mod clock;
31pub(crate) mod collections;
32mod command_policy;
33pub(crate) mod compaction;
34mod compression;
35mod concurrency;
36pub(crate) use concurrency::cancelled_vm_error;
37mod connectors;
38mod cookies;
39mod cron;
40mod crypto;
41mod csv;
42mod datetime;
43mod diff;
44pub(crate) use datetime::date_dict_from_millis;
45mod document;
46mod durable_step;
47mod event_log;
48mod external_agent;
49pub(crate) mod files;
50mod flow;
51pub(crate) mod fs;
52mod git;
53pub(crate) mod git_topology;
54mod grounding;
55pub(crate) mod harn_entry;
56pub(crate) mod hitl;
57mod hitl_read;
58pub mod host;
59pub mod http_response;
60pub(crate) mod io;
61mod iter;
62pub(crate) mod json;
63mod json_query;
64pub(crate) mod json_stream;
65mod jsonrpc;
66mod junit;
67mod lifecycle_receipts;
68mod logging;
69pub mod long_running;
70mod math;
71pub(crate) use math::call_seeded_random_method;
72pub(crate) mod memory;
73mod monitors;
74mod multipart;
75mod net;
76mod net_policy;
77mod oauth_dynreg;
78mod oauth_storage;
79pub(crate) mod observability;
80pub(crate) mod options;
81mod package_snapshot;
82pub(crate) use package_snapshot::PackageSnapshotRegistry;
83mod path;
84pub(crate) mod path_scope_guard;
85pub(crate) mod pool;
86mod portable;
87#[cfg(feature = "postgres")]
88mod postgres;
89#[cfg(feature = "postgres")]
90pub use postgres::install_shared_pool_registry;
91pub mod process;
92pub(crate) mod process_spawn;
93mod project;
94mod project_catalog;
95mod project_enrich;
96mod regex;
97mod review;
98mod runtime_scope;
99pub(crate) mod sandbox;
100pub mod secret_scan;
101pub(crate) mod session_store;
102mod sets;
103pub(crate) mod shapes;
104mod skills;
105#[cfg(feature = "sqlite")]
106mod sqlite;
107pub(crate) mod strings;
108pub(crate) mod supervisor;
109pub mod template;
110mod testbench;
111mod testing;
112mod timing;
113pub mod token_redaction;
114pub(crate) mod tool_hooks;
115pub(crate) mod tools;
116pub mod tracing;
117mod transcript_compact;
118pub(crate) mod transcript_project;
119mod triggers_stdlib;
120mod tui;
121mod types;
122mod url_parse;
123mod vision;
124pub(crate) mod waitpoint;
125mod web;
126pub mod workflow_messages;
127pub(crate) mod xml;
128
129use crate::http::register_http_builtins;
130use crate::llm::register_llm_builtins;
131use crate::mcp::register_mcp_builtins;
132use crate::mcp_server::register_mcp_server_builtins;
133use crate::vm::Vm;
134
135pub(crate) use crate::schema::{json_to_vm_value, schema_result_value};
136pub(crate) fn set_thread_source_dir(dir: &std::path::Path) {
137    process::set_thread_source_dir(dir);
138}
139
140/// Register core builtins: pure/deterministic, no I/O.
141pub fn register_core_stdlib(vm: &mut Vm) {
142    types::register_type_builtins(vm);
143    math::register_math_builtins(vm);
144    strings::register_string_builtins(vm);
145    json::register_json_builtins(vm);
146    json_stream::register_json_stream_builtins(vm);
147    xml::register_xml_builtins(vm);
148    datetime::register_datetime_builtins(vm);
149    diff::register_diff_builtins(vm);
150    document::register_document_builtins(vm);
151    calendar::register_calendar_builtins(vm);
152    cron::register_cron_builtins(vm);
153    regex::register_regex_builtins(vm);
154    bytes::register_bytes_builtins(vm);
155    compression::register_compression_builtins(vm);
156    command_policy::register_command_policy_builtins(vm);
157    runtime_scope::register_runtime_scope_builtins(vm);
158    crypto::register_crypto_builtins(vm);
159    csv::register_csv_builtins(vm);
160    junit::register_junit_builtins(vm);
161    multipart::register_multipart_builtins(vm);
162    url_parse::register_url_builtins(vm);
163    web::register_web_builtins(vm);
164    cookies::register_cookie_builtins(vm);
165    path::register_path_helper_builtins(vm);
166    sets::register_set_builtins(vm);
167    collections::register_collection_builtins(vm);
168    iter::register_iter_builtins(vm);
169    event_log::register_event_log_builtins(vm);
170    durable_step::register_durable_step_builtins(vm);
171    channels::register_channel_builtins(vm);
172    channel_guardrails::register_channel_guardrail_builtins(vm);
173    shapes::register_shape_builtins(vm);
174    testing::register_testing_builtins(vm);
175    flow::register_flow_builtins(vm);
176    lifecycle_receipts::register_lifecycle_receipt_builtins(vm);
177    net_policy::register_net_policy_builtins(vm);
178    http_response::register_http_response_builtins(vm);
179    portable::register_portable_builtins(vm);
180}
181
182/// Register I/O builtins (requires OS access).
183pub fn register_io_stdlib(vm: &mut Vm) {
184    io::register_io_builtins(vm);
185    host::register_host_builtins(vm);
186    fs::register_fs_builtins(vm);
187    package_snapshot::register_package_snapshot_builtins(vm);
188    files::register_file_builtins(vm);
189    git::register_git_builtins(vm);
190    vision::register_vision_builtins(vm);
191    agent_state::register_agent_state_builtins(vm);
192    memory::register_memory_builtins(vm);
193    session_store::register_session_store_builtins(vm);
194    net::register_net_builtins(vm);
195    process::register_process_builtins(vm);
196    process::register_path_builtins(vm);
197    sandbox::register_sandbox_builtins(vm);
198    // Clock builtins overlay process::timestamp/elapsed so they honor
199    // mock_time / advance_time. Register AFTER process to take precedence.
200    clock::register_clock_builtins(vm);
201    crate::durable_rate_limit::register_durable_rate_limit_builtins(vm);
202    testbench::register_testbench_builtins(vm);
203    project::register_project_builtins(vm);
204    grounding::register_grounding_builtins(vm);
205    tracing::register_tracing_builtins(vm);
206    observability::register_observability_builtins(vm);
207    timing::register_timing_builtins(vm);
208    tui::register_tui_builtins(vm);
209}
210
211fn register_agent_stdlib_before_llm(vm: &mut Vm) {
212    concurrency::register_concurrency_builtins(vm);
213    connectors::register_connector_builtins(vm);
214    review::register_review_builtins(vm);
215    secret_scan::register_secret_scan_builtins(vm);
216    tools::register_tool_builtins(vm);
217    tool_hooks::register_tool_hooks_builtins(vm);
218    crate::composition::register_composition_builtins(vm);
219    skills::register_skill_builtins(vm);
220    agents_daemon::register_daemon_builtins(vm);
221    triggers_stdlib::register_trigger_builtins(vm);
222    #[cfg(feature = "postgres")]
223    postgres::register_postgres_builtins(vm);
224    #[cfg(feature = "sqlite")]
225    sqlite::register_sqlite_builtins(vm);
226    monitors::register_monitor_builtins(vm);
227    hitl::register_hitl_builtins(vm);
228    hitl_read::register_hitl_read_builtins(vm);
229    waitpoint::register_waitpoint_builtins(vm);
230    supervisor::register_supervisor_builtins(vm);
231    agents::register_agent_builtins(vm);
232    pool::register_pool_builtins(vm);
233    oauth_storage::register_oauth_storage_builtins(vm);
234    oauth_dynreg::register_oauth_dynreg_builtins(vm);
235    token_redaction::register_token_redaction_builtins(vm);
236    agent_sessions::register_agent_session_builtins(vm);
237    artifact_emit::register_artifact_emit_builtins(vm);
238    external_agent::register_external_agent_builtins(vm);
239    path_scope_guard::register_path_scope_guard_builtins(vm);
240    workflow_messages::register_workflow_message_builtins(vm);
241    transcript_compact::register_transcript_compaction_builtins(vm);
242    compaction::register_compaction_builtins(vm);
243    transcript_project::register_transcript_projection_builtins(vm);
244    assemble::register_assemble_context_builtin(vm);
245    crate::egress::register_egress_builtins(vm);
246    crate::security::register_security_builtins(vm);
247    register_http_builtins(vm);
248    jsonrpc::register_jsonrpc_builtins(vm);
249}
250
251fn register_agent_stdlib_after_llm(vm: &mut Vm) {
252    register_mcp_builtins(vm);
253    register_mcp_server_builtins(vm);
254    crate::step_runtime::register_step_builtins(vm);
255}
256
257/// Register agent builtins (requires network access and async runtime).
258pub fn register_agent_stdlib(vm: &mut Vm) {
259    register_agent_stdlib_before_llm(vm);
260    register_llm_builtins(vm);
261    register_agent_stdlib_after_llm(vm);
262}
263
264/// Register all standard builtins on a VM (core + io + agent). Also
265/// installs the macro-emitted signature slice into the parser registry
266/// (idempotent under repeat calls with the same slice pointer).
267pub fn register_vm_stdlib(vm: &mut Vm) {
268    register_core_stdlib(vm);
269    register_io_stdlib(vm);
270    register_agent_stdlib(vm);
271    vm.project_declared_capability_methods();
272    if vm.harness().is_none() {
273        vm.set_harness(crate::harness::Harness::real());
274    }
275    if harn_parser::legacy_ambient_capabilities_enabled() && vm.global("harness").is_none() {
276        let harness = vm
277            .root_harness_value()
278            .expect("register_vm_stdlib installs a root Harness");
279        vm.set_global("harness", harness);
280    }
281    vm.project_legacy_capability_globals();
282    harn_builtin_registry::install_builtin_manifest(all_builtin_manifest());
283}
284
285pub(crate) fn rebind_execution_state_builtins(vm: &mut Vm) {
286    concurrency::register_concurrency_builtins(vm);
287}
288
289fn stdlib_probe_vm() -> Vm {
290    let mut vm = Vm::new();
291    register_vm_stdlib(&mut vm);
292    // Name-only/metadata introspection never accesses this path, but passing
293    // a real per-platform temp dir keeps registration logic honest if a
294    // callee someday validates its parent.
295    let tmp = std::env::temp_dir();
296    crate::store::register_store_builtins(&mut vm, &tmp);
297    crate::checkpoint::register_checkpoint_builtins(&mut vm, &tmp, "default");
298    crate::metadata::register_metadata_builtins(&mut vm, &tmp);
299    // Install the macro-emitted signatures into the parser registry so any
300    // probe-driven name/metadata query (e.g. the alignment test) sees the
301    // post-migration sig set. Idempotent under repeat install with the same
302    // pointer (which `all_builtin_manifest()` guarantees).
303    harn_builtin_registry::install_builtin_manifest(all_builtin_manifest());
304    vm
305}
306
307/// Aggregate of every `#[harn_builtin]`-emitted `VmBuiltinDef` in the stdlib.
308///
309/// Backed by the `linkme::distributed_slice` declared on
310/// [`crate::stdlib::macros::ALL_BUILTIN_DEFS`] — every annotated fn
311/// contributes one entry automatically at link time. Keep builtin registration
312/// on this distributed slice instead of per-module arrays plus a central
313/// hand-maintained aggregator.
314///
315/// **Force-link warning** (linkme issue #36): rlib dead-code stripping
316/// can drop these statics when `harn-vm` is linked transitively. Every
317/// binary that exercises builtins (`harn-cli`, `harn-lsp`, `harn-lint`,
318/// `harn-serve`, `harn-dap`) calls [`force_link`] near `main()` to defeat
319/// the stripping. The alignment test
320/// `linkme_distributed_slice_populates_with_all_builtins` catches a silent
321/// regression by asserting the slice is non-empty.
322pub fn all_builtin_defs() -> &'static [&'static macros::VmBuiltinDef] {
323    let defs = &macros::ALL_BUILTIN_DEFS;
324    validate_builtin_contracts(defs);
325    defs
326}
327
328fn validate_builtin_contracts(defs: &[&macros::VmBuiltinDef]) {
329    use harn_builtin_meta::BuiltinExposure;
330    let mut source_names = std::collections::BTreeSet::new();
331    let mut capability_methods = std::collections::BTreeSet::new();
332    for def in defs {
333        assert!(
334            def.contract.is_declared(),
335            "builtin `{}` has no typed exposure/effect contract",
336            def.sig.name
337        );
338        assert!(
339            !matches!(def.contract.exposure, BuiltinExposure::PureGlobal)
340                || def.contract.effects.is_empty(),
341            "ambient global builtin `{}` declares effects; effects must flow through Harness",
342            def.sig.name
343        );
344        if let BuiltinExposure::CapabilityFunction { authority_argument } = def.contract.exposure {
345            assert!(
346                !def.contract.effects.is_empty(),
347                "capability function `{}` must declare effects",
348                def.sig.name
349            );
350            assert!(
351                usize::from(authority_argument) < def.sig.params.len(),
352                "capability function `{}` authority argument is out of range",
353                def.sig.name
354            );
355        }
356        if def.runtime_only {
357            assert!(
358                matches!(def.contract.exposure, BuiltinExposure::RuntimeInternal),
359                "runtime_only builtin `{}` must use runtime_internal exposure",
360                def.sig.name
361            );
362        }
363        if let BuiltinExposure::HarnessMethod { method, .. } = def.contract.exposure {
364            assert!(
365                !method.is_empty(),
366                "harness method for `{}` cannot be empty",
367                def.sig.name
368            );
369            let BuiltinExposure::HarnessMethod { capability, method } = def.contract.exposure
370            else {
371                unreachable!()
372            };
373            assert!(
374                capability_methods.insert((capability, method)),
375                "duplicate contract for harness.{}.{}",
376                capability.field_name(),
377                method
378            );
379        }
380        if matches!(
381            def.contract.exposure,
382            BuiltinExposure::PureGlobal
383                | BuiltinExposure::CapabilityFunction { .. }
384                | BuiltinExposure::PrivilegedWire
385                | BuiltinExposure::HarnessMethod { .. }
386        ) {
387            assert!(
388                source_names.insert(def.sig.name),
389                "duplicate source contract name `{}`",
390                def.sig.name
391            );
392        }
393    }
394}
395
396/// Force-link entry point: a `pub fn` that touches `ALL_BUILTIN_DEFS` so
397/// the linker keeps every `#[harn_builtin]`-emitted static. Drivers
398/// (`harn-cli`, `harn-lsp`, etc.) call this once at startup. Doing nothing
399/// at runtime is fine — the side effect is purely a link-time signal.
400///
401/// See [`linkme issue #36`](https://github.com/dtolnay/linkme/issues/36)
402/// for why the explicit touch is necessary on every supported target.
403pub fn force_link() {
404    // `black_box` prevents LLVM from constant-folding the length read away.
405    // The `>= 1` guard never trips at runtime but is a load-bearing safety
406    // net: it converts a silent slice-empty regression into a panic that
407    // surfaces at the first builtin call instead of a confusing
408    // `HARN-NAM-002` somewhere down the line.
409    let len = std::hint::black_box(macros::ALL_BUILTIN_DEFS.len());
410    assert!(
411        len >= 1,
412        "linkme distributed_slice ALL_BUILTIN_DEFS is empty — \
413         the binary is missing `harn_vm::stdlib::force_link()` at startup, \
414         or the linker stripped the harn-vm rlib statics (see linkme issue #36)"
415    );
416}
417
418/// Driver-facing immutable manifest for parser, IR, policy, and docs.
419pub fn all_builtin_manifest() -> &'static [&'static harn_builtin_registry::BuiltinManifestEntry] {
420    use std::sync::OnceLock;
421    static AGG: OnceLock<Vec<&'static harn_builtin_registry::BuiltinManifestEntry>> =
422        OnceLock::new();
423    AGG.get_or_init(|| {
424        let mut out = Vec::new();
425        let mut capability_methods = std::collections::BTreeSet::new();
426        for def in all_builtin_defs() {
427            if def.runtime_only {
428                continue;
429            }
430            out.push(
431                Box::leak(Box::new(harn_builtin_registry::BuiltinManifestEntry {
432                    name: def.sig.name,
433                    canonical_name: def.sig.name,
434                    signature: &def.sig,
435                    contract: def.contract,
436                })) as &'static harn_builtin_registry::BuiltinManifestEntry,
437            );
438            if let harn_builtin_meta::BuiltinExposure::HarnessMethod { capability, method } =
439                def.contract.exposure
440            {
441                capability_methods.insert((capability, method));
442            }
443            for alias in def.aliases {
444                let signature = Box::leak(Box::new(harn_builtin_meta::BuiltinSignature {
445                    name: alias,
446                    ..def.sig
447                }));
448                out.push(
449                    Box::leak(Box::new(harn_builtin_registry::BuiltinManifestEntry {
450                        name: alias,
451                        canonical_name: def.sig.name,
452                        signature,
453                        contract: def.contract,
454                    })) as &'static harn_builtin_registry::BuiltinManifestEntry,
455                );
456            }
457        }
458        for entry in harn_capability_contracts::manifest() {
459            let harn_builtin_meta::BuiltinExposure::HarnessMethod { capability, method } =
460                entry.contract.exposure
461            else {
462                unreachable!("leaf capability manifest contains a non-method contract")
463            };
464            if !capability_methods.insert((capability, method)) {
465                let runtime_entry = out
466                    .iter()
467                    .find(|candidate| candidate.contract.exposure == entry.contract.exposure)
468                    .expect("duplicate capability key must have a runtime manifest entry");
469                assert_eq!(
470                    runtime_entry.contract,
471                    entry.contract,
472                    "runtime effect contract drift for harness.{}.{}",
473                    capability.field_name(),
474                    method
475                );
476                assert_eq!(
477                    runtime_entry.signature,
478                    entry.signature,
479                    "runtime signature drift for harness.{}.{}",
480                    capability.field_name(),
481                    method
482                );
483                continue;
484            }
485            out.push(*entry);
486        }
487        for group in harn_builtin_meta::host_capabilities::all_host_capability_groups() {
488            for method in group.methods {
489                if !capability_methods.insert((group.capability, *method)) {
490                    continue;
491                }
492                let internal_name: &'static str = Box::leak(
493                    format!("__cap_{}_{}", group.capability.field_name(), method).into_boxed_str(),
494                );
495                let params: &'static [harn_builtin_meta::Param] =
496                    Box::leak(Box::new([harn_builtin_meta::Param::new(
497                        "request",
498                        harn_builtin_meta::Ty::Named("dict"),
499                    )]));
500                let signature = Box::leak(Box::new(harn_builtin_meta::BuiltinSignature::simple(
501                    internal_name,
502                    params,
503                    harn_builtin_meta::Ty::Named("dict"),
504                )));
505                out.push(Box::leak(Box::new(
506                    harn_builtin_registry::BuiltinManifestEntry {
507                        name: internal_name,
508                        canonical_name: internal_name,
509                        signature,
510                        contract: harn_builtin_meta::BuiltinContract::harness(
511                            group.capability,
512                            method,
513                            group.effects,
514                        ),
515                    },
516                )));
517            }
518        }
519        out
520    })
521    .as_slice()
522}
523
524/// Indexed view of the authoritative builtin manifest.
525///
526/// Runtime dispatch reaches this boundary for every typed Harness call. Keep
527/// lookup policy here with the manifest owner instead of making each consumer
528/// linearly rescan the full registry. The nested capability map also accepts a
529/// borrowed `&str`, so a method call does not allocate an owned lookup key.
530struct BuiltinManifestIndex {
531    by_name: std::collections::HashMap<
532        &'static str,
533        &'static harn_builtin_registry::BuiltinManifestEntry,
534    >,
535    by_capability: std::collections::HashMap<
536        harn_builtin_meta::CapabilityId,
537        std::collections::HashMap<
538            &'static str,
539            &'static harn_builtin_registry::BuiltinManifestEntry,
540        >,
541    >,
542    recorded_effects_by_name: std::collections::HashMap<
543        &'static str,
544        &'static harn_builtin_registry::BuiltinManifestEntry,
545    >,
546}
547
548fn builtin_manifest_index() -> &'static BuiltinManifestIndex {
549    use harn_builtin_meta::BuiltinExposure;
550    use std::sync::OnceLock;
551
552    static INDEX: OnceLock<BuiltinManifestIndex> = OnceLock::new();
553    INDEX.get_or_init(|| {
554        let mut by_name = std::collections::HashMap::new();
555        let mut by_capability: std::collections::HashMap<
556            harn_builtin_meta::CapabilityId,
557            std::collections::HashMap<
558                &'static str,
559                &'static harn_builtin_registry::BuiltinManifestEntry,
560            >,
561        > = std::collections::HashMap::new();
562        let mut recorded_effects_by_name = std::collections::HashMap::new();
563        for entry in all_builtin_manifest() {
564            assert!(
565                by_name.insert(entry.name, *entry).is_none(),
566                "duplicate builtin manifest name `{}`",
567                entry.name
568            );
569            // An alias repeats its primary's contract under a second name, so
570            // only the canonical entry may claim the capability method.
571            if let BuiltinExposure::HarnessMethod { capability, method } = entry.contract.exposure {
572                if entry.is_canonical() {
573                    assert!(
574                        by_capability
575                            .entry(capability)
576                            .or_default()
577                            .insert(method, *entry)
578                            .is_none(),
579                        "duplicate Harness method manifest entry `harness.{}.{method}`",
580                        capability.field_name()
581                    );
582                }
583            }
584            if matches!(
585                entry.contract.exposure,
586                BuiltinExposure::CapabilityFunction { .. } | BuiltinExposure::PrivilegedWire
587            ) && !entry.contract.effects.is_empty()
588            {
589                recorded_effects_by_name.insert(entry.name, *entry);
590            }
591        }
592        BuiltinManifestIndex {
593            by_name,
594            by_capability,
595            recorded_effects_by_name,
596        }
597    })
598}
599
600/// Resolve the registry name behind a public `harness.<capability>.<method>`
601/// path.
602///
603/// Observation, diagnostics, and tooling classify a capability call by the
604/// same registry entry the removed ambient global resolved to, so a call
605/// through the typed handle profiles and audits identically.
606pub fn builtin_for_harness_path(path: &str) -> Option<&'static str> {
607    let (field, method) = path.strip_prefix("harness.")?.split_once('.')?;
608    let capability = harn_builtin_meta::CapabilityId::from_field_name(field)?;
609    capability_method_manifest_entry(capability, method).map(|entry| entry.name)
610}
611
612/// Resolve the typed Harness method that replaced an ambient builtin name.
613pub fn harness_method_for_builtin(
614    name: &str,
615) -> Option<(harn_builtin_meta::CapabilityId, &'static str)> {
616    match builtin_manifest_entry(name)?.contract.exposure {
617        harn_builtin_meta::BuiltinExposure::HarnessMethod { capability, method } => {
618            Some((capability, method))
619        }
620        _ => None,
621    }
622}
623
624/// Resolve a source/runtime builtin contract without rescanning the manifest.
625pub fn builtin_manifest_entry(
626    name: &str,
627) -> Option<&'static harn_builtin_registry::BuiltinManifestEntry> {
628    builtin_manifest_index().by_name.get(name).copied()
629}
630
631/// Resolve the contract for one typed Harness method without allocation.
632pub fn capability_method_manifest_entry(
633    capability: harn_builtin_meta::CapabilityId,
634    method: &str,
635) -> Option<&'static harn_builtin_registry::BuiltinManifestEntry> {
636    builtin_manifest_index()
637        .by_capability
638        .get(&capability)
639        .and_then(|methods| methods.get(method))
640        .copied()
641}
642
643/// Resolve only builtin contracts that can emit runtime effect receipts.
644///
645/// Pure builtins dominate VM call volume. Keeping them out of this projection
646/// makes their receipt check one negative hash lookup instead of a full
647/// manifest lookup plus exposure classification on every call.
648pub fn recorded_effect_builtin_manifest_entry(
649    name: &str,
650) -> Option<&'static harn_builtin_registry::BuiltinManifestEntry> {
651    builtin_manifest_index()
652        .recorded_effects_by_name
653        .get(name)
654        .copied()
655}
656
657mod harness_migration;
658
659pub use harness_migration::{
660    harness_migration_for_builtin, HarnessBuiltinArgumentMigration, HarnessBuiltinMigration,
661};
662
663/// Register every `#[harn_builtin]`-emitted def on the given VM. Drivers
664/// that build the full stdlib via `register_vm_stdlib` get this for free —
665/// each module's `register_*_builtins` walks its `MODULE_BUILTINS` slice.
666/// This helper is exposed for embedders / tests that want a one-call entry.
667pub fn register_all_macro_builtins(vm: &mut Vm) {
668    for def in all_builtin_defs() {
669        vm.register_builtin_def(def);
670    }
671}
672
673/// Return the canonical list of all stdlib builtin names. Used by
674/// harn-lint and harn-lsp to avoid hardcoded duplicate lists.
675pub fn stdlib_builtin_names() -> Vec<String> {
676    let vm = stdlib_probe_vm();
677    let mut names = vm.builtin_names();
678    // Special opcodes/keywords, not registered builtins, but linter
679    // should recognize them as valid function calls.
680    for extra in harn_parser::builtin_signatures::LANGUAGE_INTRINSICS {
681        names.push(extra.to_string());
682    }
683    names
684}
685
686/// Return discoverable metadata for registered stdlib builtins.
687pub fn stdlib_builtin_metadata() -> Vec<crate::vm::VmBuiltinMetadata> {
688    stdlib_probe_vm().builtin_metadata()
689}
690
691/// Reset thread-local stdlib state. Call between test runs.
692///
693/// Note: `long_running::reset_state()` is intentionally NOT called here
694/// because that store is process-global, not thread-local. Wiping it
695/// from a per-test reset hook lets one test cancel another test's
696/// in-flight worker thread (and lose its `agent_inbox::push`
697/// notification), which surfaces as `walk_dir_long_running` /
698/// `glob_long_running` timing out under parallel test load. The two
699/// call sites that genuinely need a clean handle store —
700/// `stdlib::fs::tests::{walk_dir_long_running,glob_long_running}` — call
701/// `long_running::reset_state()` explicitly while holding
702/// `LONG_RUNNING_TEST_LOCK`.
703pub fn reset_stdlib_state() {
704    logging::reset_logging_state();
705    process::reset_process_state();
706    clock::reset_clock_state();
707    io::reset_io_state();
708    sandbox::reset_sandbox_state();
709    git::reset_git_state();
710    fs::reset_fs_state();
711    json::reset_json_state();
712    json_stream::reset_json_stream_state();
713    host::reset_host_state();
714    host::reset_scoped_host_state();
715    observability::reset_observability_state();
716    timing::reset_timing_state();
717    durable_step::reset_durable_step_state();
718    crate::egress::reset_egress_policy_for_host();
719    hitl::reset_hitl_state();
720    crate::http::reset_http_state();
721    crate::external_agent::reset_external_agent_state();
722    monitors::reset_monitor_state();
723    waitpoint::reset_waitpoint_state();
724    triggers_stdlib::reset_auto_resume_timeouts();
725    compaction::reset_compaction_state();
726    agents::reset_agent_worker_state();
727    agents::workflow::reset_workflow_run_states();
728    pool::reset_pool_state();
729    #[cfg(feature = "postgres")]
730    postgres::reset_postgres_state();
731    #[cfg(feature = "sqlite")]
732    sqlite::reset_sqlite_state();
733    supervisor::reset_supervisor_state();
734    agents::records::reset_eval_metrics();
735    agents::records::reset_friction_events();
736    tools::clear_current_tool_registry();
737    tools::clear_tool_synthesis_cache();
738    vision::reset_vision_state();
739    crate::skills::clear_current_skill_registry();
740    template::reset_prompt_registry();
741    crate::triggers::clear_webhook_intake_state();
742    crate::llm::cache::reset_in_process_cache_state();
743}
744
745#[cfg(test)]
746mod tests {
747    use super::*;
748
749    #[tokio::test(flavor = "current_thread")]
750    async fn register_vm_stdlib_passes_default_harness_only_to_main() {
751        let chunk = crate::compile_source(
752            r"
753fn __probe_harness_clock(clock: HarnessClock) {
754  const now = clock.now_ms()
755  return now >= 0
756}
757
758fn main(harness: Harness) {
759  return __probe_harness_clock(harness.clock)
760}
761",
762        )
763        .expect("compile harness clock probe");
764        let mut vm = Vm::new();
765        register_vm_stdlib(&mut vm);
766
767        assert!(vm.root_harness_value().is_some());
768        assert!(vm.global("harness").is_none());
769        let result = vm
770            .execute(&chunk)
771            .await
772            .expect("execute harness clock probe");
773        assert!(matches!(result, crate::value::VmValue::Bool(true)));
774    }
775
776    /// `harn_stdlib::builtin_reexports` names builtins from a crate that
777    /// cannot see the builtin registry, so nothing there can catch a typo or a
778    /// rename. This is that check: every re-exported name must resolve to a
779    /// real builtin, or `import { … } from "std/…"` binds a reference to
780    /// nothing and fails at the call site instead of the import.
781    #[test]
782    fn every_stdlib_builtin_reexport_names_a_registered_builtin() {
783        let registered: std::collections::HashSet<&str> = all_builtin_defs()
784            .iter()
785            .flat_map(|def| std::iter::once(def.sig.name).chain(def.aliases.iter().copied()))
786            .collect();
787
788        let mut checked = 0;
789        for entry in harn_stdlib::STDLIB_SOURCES {
790            for name in harn_stdlib::builtin_reexports(entry.module) {
791                assert!(
792                    registered.contains(name),
793                    "std/{} re-exports '{name}', which is not a registered builtin",
794                    entry.module
795                );
796                checked += 1;
797            }
798        }
799        assert!(
800            checked > 0,
801            "no re-exports were checked — the table or the module list is not being read"
802        );
803    }
804
805    #[test]
806    fn ambient_effect_builtin_allowlist_is_empty() {
807        let offenders = all_builtin_defs()
808            .iter()
809            .filter(|def| {
810                matches!(
811                    def.contract.exposure,
812                    harn_builtin_meta::BuiltinExposure::PureGlobal
813                ) && !def.contract.effects.is_empty()
814            })
815            .map(|def| def.sig.name)
816            .collect::<Vec<_>>();
817
818        assert!(
819            offenders.is_empty(),
820            "ambient effect builtins are forbidden; route these through Harness: {offenders:?}"
821        );
822    }
823
824    #[test]
825    fn harness_builtin_migrations_preserve_canonical_call_shapes() {
826        use harn_builtin_meta::CapabilityId;
827
828        assert_eq!(
829            harness_migration_for_builtin("provider_capabilities"),
830            Some(HarnessBuiltinMigration {
831                capability: CapabilityId::Llm,
832                method: "provider_capabilities",
833                arguments: HarnessBuiltinArgumentMigration::Forward,
834            })
835        );
836        assert_eq!(
837            harness_migration_for_builtin("log_info"),
838            Some(HarnessBuiltinMigration {
839                capability: CapabilityId::Observability,
840                method: "log_info",
841                arguments: HarnessBuiltinArgumentMigration::Forward,
842            })
843        );
844        assert_eq!(
845            harness_migration_for_builtin("runtime_introspection"),
846            Some(HarnessBuiltinMigration {
847                capability: CapabilityId::Runtime,
848                method: "introspection",
849                arguments: HarnessBuiltinArgumentMigration::Forward,
850            })
851        );
852        assert_eq!(
853            harness_migration_for_builtin("project_fingerprint"),
854            Some(HarnessBuiltinMigration {
855                capability: CapabilityId::Project,
856                method: "fingerprint",
857                arguments: HarnessBuiltinArgumentMigration::Forward,
858            })
859        );
860        assert_eq!(
861            harness_migration_for_builtin("project_scan_tree_native"),
862            Some(HarnessBuiltinMigration {
863                capability: CapabilityId::Project,
864                method: "scan_tree",
865                arguments: HarnessBuiltinArgumentMigration::Forward,
866            })
867        );
868        assert_eq!(
869            harness_migration_for_builtin("llm_call"),
870            Some(HarnessBuiltinMigration {
871                capability: CapabilityId::Llm,
872                method: "call",
873                arguments: HarnessBuiltinArgumentMigration::Forward,
874            })
875        );
876        assert_eq!(
877            harness_migration_for_builtin("llm_call_structured"),
878            Some(HarnessBuiltinMigration {
879                capability: CapabilityId::Llm,
880                method: "call_structured",
881                arguments: HarnessBuiltinArgumentMigration::Forward,
882            })
883        );
884        assert_eq!(
885            harness_migration_for_builtin("security_policy"),
886            Some(HarnessBuiltinMigration {
887                capability: CapabilityId::System,
888                method: "security_policy",
889                arguments: HarnessBuiltinArgumentMigration::Forward,
890            })
891        );
892        assert_eq!(
893            harness_migration_for_builtin("llm_provider_status"),
894            Some(HarnessBuiltinMigration {
895                capability: CapabilityId::Llm,
896                method: "providers",
897                arguments: HarnessBuiltinArgumentMigration::Forward,
898            })
899        );
900        assert_eq!(
901            harness_migration_for_builtin("agent_session_current_id"),
902            Some(HarnessBuiltinMigration {
903                capability: CapabilityId::Agent,
904                method: "current_id",
905                arguments: HarnessBuiltinArgumentMigration::Forward,
906            })
907        );
908        assert_eq!(
909            harness_migration_for_builtin("metadata_set"),
910            Some(HarnessBuiltinMigration {
911                capability: CapabilityId::Project,
912                method: "metadata_set",
913                arguments: HarnessBuiltinArgumentMigration::RequestRecord(&[
914                    "dir",
915                    "namespace",
916                    "data",
917                ]),
918            })
919        );
920        assert_eq!(
921            harness_migration_for_builtin("metadata_save"),
922            Some(HarnessBuiltinMigration {
923                capability: CapabilityId::Project,
924                method: "metadata_save",
925                arguments: HarnessBuiltinArgumentMigration::RequestRecord(&[]),
926            })
927        );
928        assert_eq!(
929            harness_migration_for_builtin("platform"),
930            Some(HarnessBuiltinMigration {
931                capability: CapabilityId::System,
932                method: "platform",
933                arguments: HarnessBuiltinArgumentMigration::CallThenProperty("os"),
934            })
935        );
936        assert_eq!(
937            harness_migration_for_builtin("arch"),
938            Some(HarnessBuiltinMigration {
939                capability: CapabilityId::System,
940                method: "platform",
941                arguments: HarnessBuiltinArgumentMigration::CallThenProperty("arch"),
942            })
943        );
944        assert_eq!(
945            harness_migration_for_builtin("home_dir"),
946            Some(HarnessBuiltinMigration {
947                capability: CapabilityId::Fs,
948                method: "home_dir",
949                arguments: HarnessBuiltinArgumentMigration::Forward,
950            })
951        );
952        assert_eq!(harness_migration_for_builtin("json_parse"), None);
953    }
954
955    #[test]
956    fn every_source_named_runtime_callable_has_a_typed_contract() {
957        let manifest_names = all_builtin_manifest()
958            .iter()
959            .map(|entry| entry.name)
960            .collect::<std::collections::HashSet<_>>();
961        let offenders = stdlib_probe_vm()
962            .builtin_names()
963            .into_iter()
964            .filter(|name| {
965                !name.starts_with("__")
966                    && !manifest_names.contains(name.as_str())
967                    && !harn_parser::builtin_signatures::is_language_intrinsic(name)
968            })
969            .collect::<Vec<_>>();
970
971        assert!(
972            offenders.is_empty(),
973            "runtime callables without typed source contracts bypass the Harness gate: {offenders:?}"
974        );
975    }
976
977    #[test]
978    fn builtin_manifest_indexes_preserve_the_authoritative_contracts() {
979        use harn_builtin_meta::BuiltinExposure;
980
981        let mut capability_entries = 0;
982        for entry in all_builtin_manifest() {
983            assert!(
984                std::ptr::eq(
985                    builtin_manifest_entry(entry.name).expect("indexed builtin entry"),
986                    *entry,
987                ),
988                "name index projected a different contract for `{}`",
989                entry.name
990            );
991            let should_record = matches!(
992                entry.contract.exposure,
993                BuiltinExposure::CapabilityFunction { .. } | BuiltinExposure::PrivilegedWire
994            ) && !entry.contract.effects.is_empty();
995            assert_eq!(
996                recorded_effect_builtin_manifest_entry(entry.name).is_some(),
997                should_record,
998                "recorded-effect index drifted for `{}`",
999                entry.name
1000            );
1001            if let BuiltinExposure::HarnessMethod { capability, method } = entry.contract.exposure {
1002                capability_entries += 1;
1003                let indexed = capability_method_manifest_entry(capability, method)
1004                    .expect("indexed Harness method entry");
1005                // An alias shares its primary's contract under a second name.
1006                // The capability index answers with the primary either way.
1007                assert_eq!(
1008                    indexed.name,
1009                    entry.canonical_name,
1010                    "capability index projected a different builtin for `harness.{}.{method}`",
1011                    capability.field_name()
1012                );
1013                assert_eq!(
1014                    indexed.contract,
1015                    entry.contract,
1016                    "capability index projected a different contract for `harness.{}.{method}`",
1017                    capability.field_name()
1018                );
1019            }
1020        }
1021        assert!(capability_entries > 0, "no Harness contracts were indexed");
1022        assert!(builtin_manifest_entry("__definitely_missing_builtin").is_none());
1023        assert!(capability_method_manifest_entry(
1024            harn_builtin_meta::CapabilityId::Fs,
1025            "__definitely_missing_method",
1026        )
1027        .is_none());
1028    }
1029}
1030
1031#[cfg(test)]
1032mod ambient_host_internal_projection_tests {
1033    use super::*;
1034
1035    #[test]
1036    fn ambient_bridge_projects_host_internal_emit_event_alias() {
1037        let previous = std::env::var_os(harn_parser::HARN_LEGACY_AMBIENT_CAPABILITIES_ENV);
1038        unsafe {
1039            std::env::set_var(harn_parser::HARN_LEGACY_AMBIENT_CAPABILITIES_ENV, "1");
1040        }
1041        let mut vm = Vm::new();
1042        register_vm_stdlib(&mut vm);
1043        assert!(
1044            vm.builtin_metadata_for("agent_emit_event").is_some(),
1045            "ambient bridge must project __host_agent_emit_event as agent_emit_event"
1046        );
1047        assert!(
1048            vm.builtin_metadata_for("__host_agent_emit_event").is_some(),
1049            "canonical host internal must remain registered"
1050        );
1051        unsafe {
1052            match previous {
1053                Some(value) => {
1054                    std::env::set_var(harn_parser::HARN_LEGACY_AMBIENT_CAPABILITIES_ENV, value);
1055                }
1056                None => std::env::remove_var(harn_parser::HARN_LEGACY_AMBIENT_CAPABILITIES_ENV),
1057            }
1058        }
1059    }
1060}