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