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, CompiledCallableEntry, CompiledPortableModule, Compiler, CompilerOptions,
47    PortableExportKind, PortableImport, PortableSourceModule, PortableSourcePackage,
48};
49pub use execution::{
50    replay, resume, start, CapabilityRequest, CapabilityResult, DataValue, Execution, GrantSet,
51    ValueShape, PORTABLE_MAX_SNAPSHOT_BYTES,
52};
53pub use opcode::{
54    opcode_abi_fingerprint, Op, OperandKind, Portability, OPCODE_ABI_ARTIFACT_VERSION,
55    OPCODE_ABI_FINGERPRINT_V2,
56};
57pub use program::{Chunk, CompiledFunction, Constant, LocalSlotInfo, ParamSlot};
58
59mod chunk {
60    pub use crate::program::*;
61    pub use crate::Op;
62}
63
64/// Compile a checked source module with deterministic options.
65pub fn compile_source(source: &str) -> Result<Chunk, String> {
66    let program = harn_parser::check_source_strict(source).map_err(|error| error.to_string())?;
67    Compiler::new()
68        .compile(&program)
69        .map_err(|error| error.to_string())
70}
71
72pub fn compile_source_named(source: &str, pipeline_name: &str) -> Result<Chunk, String> {
73    let program = harn_parser::check_source_strict(source).map_err(|error| error.to_string())?;
74    let exists = program.iter().any(|node| {
75        let (_, inner) = harn_parser::peel_attributes(node);
76        matches!(&inner.node, harn_parser::Node::Pipeline { name, .. } if name == pipeline_name)
77    });
78    if !exists {
79        return Err(format!("no pipeline named `{pipeline_name}` in source"));
80    }
81    Compiler::new()
82        .compile_named(&program, pipeline_name)
83        .map_err(|error| error.to_string())
84}
85pub mod execution;