Kernel - Pure Functional State Machine
On this page
- Core Principle: Functional Core / Imperative Shell
- State Machine Interface
- Command Types
- Effect Types
- State Structure
- Determinism Guarantees
- No Clocks
- No Random Numbers
- No IO
- Bounded Loops
- Testing the Kernel
- Property-Based Testing
- Idempotency
- Error Handling
- Assertion Density
- Performance
- Future Work
- Related Documentation
The kernel is the heart of Kimberlite: a pure, deterministic state machine.
Core Principle: Functional Core / Imperative Shell
The kernel is pure. No IO, no clocks, no randomness. All side effects live at the edges.
// Core (pure) - The kernel
This separation makes the kernel:
- Testable: No mocking required
- Deterministic: Same inputs → same outputs (always)
- Verifiable: Easier to prove correctness
- Reproducible: Replay logs to debug issues
See Pressurecraft for the philosophy behind this pattern.
State Machine Interface
The kernel exposes a simple interface:
Key properties:
- Pure function: No side effects during
apply() - Deterministic: Same inputs produce same outputs
- Returns effects: Side effects are data, not actions
Command Types
Commands represent operations to perform:
Each command is validated before entering the kernel:
Effect Types
Effects represent side effects to execute after the state transition:
Why effects?
Instead of performing IO directly, the kernel returns a list of effects. The runtime executes them:
// Kernel (pure)
// Runtime (impure)
This separation allows:
- Testing kernel without IO
- Replaying state transitions
- Auditing side effects
- Deferring IO for batching
State Structure
The kernel maintains minimal state:
Key principle: State is derived from the log. If you delete the state and replay the log, you get identical state back.
Determinism Guarantees
The kernel is deterministic by construction:
No Clocks
// BAD: Non-deterministic
let timestamp = now;
// GOOD: Deterministic
let timestamp = command.timestamp; // Provided by runtime
Timestamps come from outside the kernel (VSR assigns them during consensus).
No Random Numbers
// BAD: Non-deterministic
let id = ;
// GOOD: Deterministic
let id = command.id; // Provided by client
IDs are generated outside the kernel (clients generate idempotency IDs).
No IO
// BAD: Non-deterministic
let data = read?;
// GOOD: Deterministic
let data = state.config.clone; // State passed in
All data comes through function parameters, not IO.
Bounded Loops
// BAD: Unbounded
while condition
// GOOD: Bounded
for _ in 0..MAX_ITERATIONS
Prevents infinite loops that could hang the system.
Testing the Kernel
Because the kernel is pure, tests are simple:
No mocks, no async, no flaky tests. Just pure functions.
Property-Based Testing
The kernel uses property-based testing (proptest) to find edge cases:
proptest!
See Property Testing for more examples.
Idempotency
The kernel tracks idempotency IDs to prevent duplicate operations:
See Compliance for why idempotency matters.
Error Handling
The kernel uses typed errors:
No panics in the kernel. All errors are explicit and recoverable.
Assertion Density
The kernel has high assertion density (2+ assertions per function):
See Assertions for the complete assertion strategy.
Performance
The kernel is optimized for throughput:
- Zero-copy: Use
BytesandArc<[u8]>to avoid cloning - Minimal allocations: Reuse buffers where possible
- No async: Purely synchronous (async is in the runtime layer)
- Small state: Keep only what’s necessary in memory
Benchmark: 100k+ applies/sec on commodity hardware.
Future Work
- Snapshots: Checkpoint state to avoid replaying entire log
- Parallel apply: Apply independent commands in parallel
- WASM kernel: Compile kernel to WebAssembly for portability
See ROADMAP.md for details.
Related Documentation
- Pressurecraft - Philosophy behind FCIS pattern
- Testing Overview - How we test the kernel
- Property Testing - Property-based testing strategies
- Assertions - Assertion density and safety
Key Takeaway: The kernel is a pure, deterministic state machine. It takes commands and state, returns new state and effects. No IO, no clocks, no randomness—just pure functions.