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/// Shared adapter ingress limits. Native and browser hosts enforce these
12/// before allocating or parsing untrusted wire inputs.
13pub const PORTABLE_MAX_SOURCE_BYTES: usize = 1024 * 1024;
14pub const PORTABLE_MAX_PACKAGE_BYTES: usize = 8 * 1024 * 1024;
15pub const PORTABLE_MAX_PACKAGE_MODULES: usize = 1_024;
16pub const PORTABLE_MAX_VALUE_JSON_BYTES: usize = 1024 * 1024;
17pub const PORTABLE_MAX_GRANTS_JSON_BYTES: usize = 64 * 1024;
18
19pub mod artifact;
20pub mod benchmark;
21mod builtin_id;
22pub mod compiler;
23pub mod opcode;
24mod portable_builtin;
25pub mod predicate;
26pub mod program;
27pub mod pure;
28mod runtime_limits;
29mod schema;
30pub mod type_contract;
31pub mod value;
32
33pub use artifact::{
34    compile_program, compile_program_package, compile_source_package, semantic_abi_fingerprint_hex,
35    ArtifactLimits, Diagnostic, EntryKind, PortableModuleSource, PortablePackageSource,
36    ProgramArtifact, ProgramModule, ARTIFACT_VERSION,
37};
38pub use benchmark::{
39    benchmark_terminal_digest, portable_benchmark_json_schema, BenchmarkBuildProfile,
40    BenchmarkEntryKind, BenchmarkProvenance, BenchmarkStatistics, BenchmarkStatisticsError,
41    BenchmarkTarget, CompileMeasurements, DispatchMeasurements, PortableBenchmarkReceipt,
42    PORTABLE_BENCHMARK_SCHEMA_VERSION, PORTABLE_MAX_COMPILE_ITERATIONS,
43    PORTABLE_MAX_DISPATCH_ITERATIONS, PORTABLE_MAX_WORKERS,
44};
45pub use builtin_id::BuiltinId;
46pub use compiler::{
47    CompileError, CompiledCallableBatch, CompiledCallableEntry, CompiledPortableModule, Compiler,
48    CompilerOptions, PortableExportKind, PortableImport, PortableSourceModule,
49    PortableSourcePackage,
50};
51pub use execution::{
52    replay, resume, start, CapabilityRequest, CapabilityResult, DataValue, Execution, GrantSet,
53    ValueShape, PORTABLE_MAX_SNAPSHOT_BYTES,
54};
55pub use opcode::{
56    opcode_abi_fingerprint, Op, OperandKind, Portability, OPCODE_ABI_ARTIFACT_VERSION,
57    OPCODE_ABI_FINGERPRINT_V4,
58};
59pub use program::{BindingTypeSlot, Chunk, CompiledFunction, Constant, LocalSlotInfo, ParamSlot};
60
61mod chunk {
62    pub use crate::program::*;
63    pub use crate::Op;
64}
65
66/// Compile a checked source module with deterministic options.
67pub fn compile_source(source: &str) -> Result<Chunk, String> {
68    let program = harn_parser::check_source_strict(source).map_err(|error| error.to_string())?;
69    Compiler::new()
70        .compile(&program)
71        .map_err(|error| error.to_string())
72}
73
74pub fn compile_source_named(source: &str, pipeline_name: &str) -> Result<Chunk, String> {
75    let program = harn_parser::check_source_strict(source).map_err(|error| error.to_string())?;
76    let exists = program.iter().any(|node| {
77        let (_, inner) = harn_parser::peel_attributes(node);
78        matches!(&inner.node, harn_parser::Node::Pipeline { name, .. } if name == pipeline_name)
79    });
80    if !exists {
81        return Err(format!("no pipeline named `{pipeline_name}` in source"));
82    }
83    Compiler::new()
84        .compile_named(&program, pipeline_name)
85        .map_err(|error| error.to_string())
86}
87pub mod execution;