conservation-checker
One-sided conservation laws for real systems.
Track quantities that must not decrease across operations — budgets, energy, quotas, token counts, throughput — with tolerance, drift detection, and phase analysis.
30-Second Example
use ConservationChecker;
let mut checker = new;
// Register a quantity: name, initial value, allowed decrease (tolerance)
checker.register;
// Update as your system runs
checker.update; // still conserved
checker.snapshot;
checker.update; // dropped past tolerance!
checker.snapshot;
assert!;
assert_eq!;
Why?
Conservation laws aren't just physics. Real systems have quantities that should only increase or stay flat:
- Budget tracking — spending must not exceed allocation
- API rate limits — request count must not exceed quota
- Energy monitoring — battery drain beyond expected range signals problems
- Token budgets — LLM token consumption against a cap
- Throughput quotas — data transfer against a monthly limit
conservation-checker gives you a simple, zero-dependency way to assert these invariants, detect when they're violated, and track trends over time.
Real Use Cases
Budget Tracking
let mut budget = new;
budget.register; // $100 buffer
for expense in &expenses
if !budget.violations.is_empty
API Rate Limits
let mut limits = new;
limits.register;
// After each API call:
limits.update;
limits.snapshot;
if limits.phase == Transitioning
Energy Monitoring
let mut monitor = new;
monitor.register; // 20% tolerance
monitor.update;
monitor.snapshot;
let drain = monitor.drift_rate; // % per tick
println!;
Token Budgets
let mut tokens = new;
tokens.register;
tokens.update;
if !tokens.is_conserved
API Reference
ConservationChecker
| Method | Description |
|---|---|
new() |
Create an empty tracker |
register(name, initial, tolerance) |
Register a quantity with initial value and allowed decrease |
update(name, value) |
Set the current value of a quantity |
is_conserved(name) -> bool |
Check if value ≥ initial − tolerance |
violations() -> Vec<String> |
List all quantities currently violated |
snapshot() |
Record current values into history |
phase(name) -> Phase |
Detect trajectory phase from history |
drift_rate(name) -> f64 |
Average change per snapshot |
current_value(name) -> f64 |
Get the current value |
initial_value(name) -> f64 |
Get the initial (baseline) value |
snapshot_count(name) -> usize |
Number of snapshots recorded |
registered() -> Vec<String> |
All registered quantity names |
deregister(name) -> bool |
Remove a quantity |
reset_baseline(name) |
Reset initial value to current, clearing violations |
Phase
Design Principles
- Zero dependencies — nothing to audit, nothing to break
#![deny(unsafe_code)]— memory safety guaranteed- One-sided — increases are always OK, only decreases can violate
- Tolerance-aware — real systems have acceptable ranges, not exact values
- Time-series built in — snapshot history enables drift and phase detection
Running Examples
License
MIT