sheaf-coherence
Sheaf-theoretic multi-agent knowledge consistency for Rust.
This library models a fleet of agents as a sheaf over an open cover of a shared information space. Each agent maintains a local section — its view of the global state restricted to the variables it can observe. The library computes Čech sheaf cohomology groups to detect and classify contradictions across the fleet, and can repair them via sheaf gluing when possible.
Why Sheaf Cohomology?
In a multi-agent system, each agent sees only part of the world. When two agents' views overlap, they must agree on the overlap — otherwise the fleet is inconsistent. Sheaf cohomology gives a rigorous algebraic framework for this:
- H⁰ (global sections): The space of globally consistent belief vectors.
dim H⁰ > 0means a global consensus exists. - H¹ (obstructions): The local-to-global obstruction space.
dim H¹ > 0means agents cannot reconcile their views — there are fundamental contradictions that no local patching can resolve.
This is the difference between "agents disagree but can converge" (H¹ = 0) and "agents are trapped in irreconcilable contradictions" (H¹ ≠ 0).
Architecture
┌─────────────┐ ┌──────────────┐ ┌───────────────┐
│ OpenCover │────▶│ LocalSection │────▶│ Čech Cochain │
│ (who sees │ │ (what they │ │ Complex │
│ what) │ │ believe) │ │ (C⁰→C¹→C²) │
└─────────────┘ └──────────────┘ └───────┬───────┘
│
┌─────────────────────┼──────────────────┐
▼ ▼ ▼
┌──────────────┐ ┌──────────────────┐ ┌────────────┐
│ H⁰: Global │ │ H¹: Obstructions │ │Persistence │
│ Sections │ │ & Conflicts │ │ Diagrams │
└──────┬───────┘ └────────┬─────────┘ └────────────┘
│ │
▼ ▼
┌──────────────┐ ┌──────────────────┐
│ Gluing: │ │ Classification: │
│ Repair │ │ Pairwise/Multi/ │
│ (H¹=0) │ │ Hidden │
└──────────────┘ └──────────────────┘
Modules
| Module | Purpose |
|---|---|
cover |
Open covers over agent knowledge domains with intersection lattices |
section |
Local sections: per-agent belief vectors with restriction maps |
cochain |
Čech cochain complexes and Gaussian elimination (no external deps) |
obstruction |
Detects and classifies local-to-global obstruction classes |
gluing |
Consistency repair via sheaf gluing (collate compatible sections) |
persistence |
Persistent sheaf cohomology over varying resolution thresholds |
Quick Start
use *;
// Define what each agent can see
let cover = new;
// Each agent has beliefs over its visible variables
let fam = new;
// Check consistency
assert!;
// Compute cohomology
let = compute_cohomology;
println!;
println!;
// Glue into a global section
let result = glue;
match result
Detecting Contradictions
// Agent 0 thinks variable 1 = 2.0, Agent 1 thinks variable 1 = 9.0
let bad_fam = new;
let obs = detect;
assert!;
assert_eq!;
Persistent Cohomology
Track how cohomology evolves as the cover gets finer:
let stages = vec!;
let = persistent_cohomology;
for snap in &snapshots
Core Types
All public types derive Serialize and Deserialize via Serde.
How It Works
Čech Cohomology
Given an open cover ${U_i}$ of the state space, we form the Čech cochain complex:
$$0 \to C0 \xrightarrow{d0} C1 \xrightarrow{d1} C^2 \to \cdots$$
where:
- $C^0 = \prod_i \mathcal{F}(U_i)$ — sections on each open set
- $C^1 = \prod_{i<j} \mathcal{F}(U_i \cap U_j)$ — sections on pairwise overlaps
- $C^2 = \prod_{i<j<k} \mathcal{F}(U_i \cap U_j \cap U_k)$ — triple overlaps
- $d^0$ computes pairwise differences (restriction + subtraction)
- $d^1$ enforces the cocycle condition on triple overlaps
The cohomology groups are:
- $H0 = \ker(d0)$ — globally compatible sections
- $H1 = \ker(d1) / \text{im}(d^0)$ — obstructions to gluing
Gaussian Elimination
All linear algebra (rank, nullspace, image) is computed via from-scratch Gaussian elimination with partial pivoting. No external math dependencies.
Testing
Test coverage includes:
- Single agent (trivial cover) → H⁰ full, H¹ = 0
- Two agents with contradictory beliefs → detected
- Consistent fleet → global section exists
- Inconsistent fleet → nonzero H¹ or conflicts
- Restriction maps compose correctly
- Čech complex has correct dimensionality
- Serde roundtrips for all public types
- Persistence diagrams over multiple resolutions
- Gluing succeeds/fails based on H¹
- Cover operations (intersection, triple, universe, validity)
License
MIT