Skip to main content

harn_kernel/
lib.rs

1//! Portable Harn compiler and deterministic execution kernel.
2//!
3//! This crate is deliberately a dependency leaf with no filesystem, network,
4//! process, clock, random, model, or async-runtime authority.
5
6/// Version of the crate that owns portable artifact and execution semantics.
7/// Adapters use this value directly so benchmark provenance cannot drift to
8/// the version of whichever host happens to emit a receipt.
9pub const KERNEL_VERSION: &str = env!("CARGO_PKG_VERSION");
10
11/// Crate-local source contribution to compiler and check cache identities.
12pub const SOURCE_FINGERPRINT: &str = env!("HARN_SOURCE_FINGERPRINT");
13
14/// Shared adapter ingress limits. Native and browser hosts enforce these
15/// before allocating or parsing untrusted wire inputs.
16pub const PORTABLE_MAX_SOURCE_BYTES: usize = 1024 * 1024;
17pub const PORTABLE_MAX_PACKAGE_BYTES: usize = 8 * 1024 * 1024;
18pub const PORTABLE_MAX_PACKAGE_MODULES: usize = 1_024;
19pub const PORTABLE_MAX_VALUE_JSON_BYTES: usize = 1024 * 1024;
20pub const PORTABLE_MAX_GRANTS_JSON_BYTES: usize = 64 * 1024;
21
22pub mod artifact;
23pub mod benchmark;
24mod builtin_id;
25pub mod compiler;
26pub mod opcode;
27mod portable_builtin;
28pub mod predicate;
29pub mod program;
30pub mod pure;
31mod runtime_limits;
32mod schema;
33pub mod type_contract;
34pub mod value;
35
36pub use artifact::{
37    compile_program, compile_program_package, compile_source_package, semantic_abi_fingerprint_hex,
38    ArtifactLimits, Diagnostic, EntryKind, PortableModuleSource, PortablePackageSource,
39    ProgramArtifact, ProgramModule, ARTIFACT_VERSION,
40};
41pub use benchmark::{
42    benchmark_terminal_digest, portable_benchmark_json_schema, BenchmarkBuildProfile,
43    BenchmarkEntryKind, BenchmarkProvenance, BenchmarkStatistics, BenchmarkStatisticsError,
44    BenchmarkTarget, CompileMeasurements, DispatchMeasurements, PortableBenchmarkReceipt,
45    PORTABLE_BENCHMARK_SCHEMA_VERSION, PORTABLE_MAX_COMPILE_ITERATIONS,
46    PORTABLE_MAX_DISPATCH_ITERATIONS, PORTABLE_MAX_WORKERS,
47};
48pub use builtin_id::BuiltinId;
49pub use compiler::{
50    CompileError, CompiledCallableBatch, CompiledCallableEntry, CompiledPortableModule, Compiler,
51    CompilerOptions, PortableExportKind, PortableImport, PortableSourceModule,
52    PortableSourcePackage,
53};
54pub use execution::{
55    replay, resume, start, CapabilityRequest, CapabilityResult, DataValue, Execution, GrantSet,
56    ValueShape, PORTABLE_MAX_SNAPSHOT_BYTES,
57};
58pub use opcode::{
59    opcode_abi_fingerprint, Op, OperandKind, Portability, OPCODE_ABI_ARTIFACT_VERSION,
60    OPCODE_ABI_FINGERPRINT_V4, OPCODE_ABI_FINGERPRINT_V5,
61};
62pub use program::{BindingTypeSlot, Chunk, CompiledFunction, Constant, LocalSlotInfo, ParamSlot};
63
64mod chunk {
65    pub use crate::program::*;
66    pub use crate::Op;
67}
68
69/// Compile a checked source module with deterministic options.
70pub fn compile_source(source: &str) -> Result<Chunk, String> {
71    let program = harn_parser::check_source_strict(source).map_err(|error| error.to_string())?;
72    Compiler::new()
73        .compile(&program)
74        .map_err(|error| error.to_string())
75}
76
77pub fn compile_source_named(source: &str, pipeline_name: &str) -> Result<Chunk, String> {
78    let program = harn_parser::check_source_strict(source).map_err(|error| error.to_string())?;
79    let exists = program.iter().any(|node| {
80        let (_, inner) = harn_parser::peel_attributes(node);
81        matches!(&inner.node, harn_parser::Node::Pipeline { name, .. } if name == pipeline_name)
82    });
83    if !exists {
84        return Err(format!("no pipeline named `{pipeline_name}` in source"));
85    }
86    Compiler::new()
87        .compile_named(&program, pipeline_name)
88        .map_err(|error| error.to_string())
89}
90pub mod execution;