File size: 4,403 Bytes
32b4893
 
29f097e
 
32b4893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ce526e
 
29f097e
4ce526e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29f097e
4ce526e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29f097e
4ce526e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
license: other
license_name: sovereign-leviathan-sl-agpl3-001
license_link: https://huggingface.co/Snapkitty/sparse-router-formal/blob/main/LICENSE
tags:
- snapkitty
- october-2026-drop
- rust
- pascal
- lean4
- c
- kani
- work-stealing
- scheduler
- formal-verification
---

> Mirrored from [https://github.com/SNAPKITTYWEST/sparse-router-formal](https://github.com/SNAPKITTYWEST/sparse-router-formal) at commit `5e292ee`. Part of the **SnapKitty October 2026 main drop**.

# Sparse Router β€” Formally Verified Locality-Aware Work-Stealing Scheduler

**License**: Sovereign Leviathan covenant (`LicenseRef-Sovereign-Leviathan-SL-AGPL3-001`), AGPL-3.0 based

## Architecture

```
sparse-router-formal/
β”œβ”€β”€ rust/tensor-hw/       # Production Rust: tensor core + RAW_ROUTE + Kani proofs
β”‚   β”œβ”€β”€ src/              # 10 modules: buffer, ownership, memory, view, governance...
β”‚   └── ffi/              # sparse_router.rs, kani proofs, C ABI, FFI bridge
β”œβ”€β”€ pascal/               # Reference implementation (independent conformance oracle)
β”œβ”€β”€ formal/               # Lean 4 formal proofs (convergence, invariants, scoring)
β”œβ”€β”€ c-reference/          # Standalone C reference (header-only, benchmarkable)
β”œβ”€β”€ tests/conformance/    # 52 cross-language conformance tests (7 categories)
β”œβ”€β”€ spec/                 # Normative specifications (contract, state machine, memory model)
β”œβ”€β”€ src/pascal/           # Pascal transformer sources
└── scripts/              # Clone-gate verification
```

## Novel Method: RAW_ROUTE

Three-phase routing with O(K) sparse-edge traversal (K ≀ 16), not O(N) worker scan:

```
score(w, t) = locality(w,t) + memory_affinity(w,t) + queue_pressure(w)
            + stealability(w) - migration_cost(w,t)
```

| Phase | Path | Complexity |
|-------|------|-----------|
| 1 | Region-owner fast path | O(1) |
| 2 | Sparse edge traversal | O(K) |
| 3 | Least-loaded fallback | O(N) |

## Formal Verification

### Lean 4 Proofs (`formal/`)
- **Convergence**: potential function Ξ¦ = |unowned regions| strictly decreases β†’ stable in ≀ MAX_REGIONS steps
- **Uniqueness**: every routed task lands on exactly one worker deque
- **Bounded traversal**: sparse edge fan-out ≀ MAX_SPARSE_EDGES
- **Scoring properties**: locality bonus, empty-queue bonus, zero migration cost for local tasks
- **Deque correctness**: push/pop identity, count invariants

### Kani Proofs (`rust/tensor-hw/ffi/sparse_router_kani.rs`)
- All tasks routed (progress guaranteed)
- Region owner consistency
- Locality over balance
- Migration cost avoidance
- Fallback coverage
- Route decision stability
- Score bounds (all weights finite)
- No deadlock (acyclic region ownership)

### Cross-Language Conformance (`tests/conformance/`)
52 tests across 7 categories β€” Rust and Pascal must agree:
- Buffer lifetime, ownership transitions, views, materialization
- Generation validation, governance, routing

## Build

```bash
make test              # C reference tests (7 tests)
make bench             # C benchmarks (~100M routes/sec)
make test-rust         # Rust test suite (49 tests)
make verify            # Lean 4 proofs (requires lake)
make clone-gate-check  # Verify all markers present, no MIT
```

## Clone Gate

All source files contain `CLONE_GATE` markers. See `CLONE_GATE.md` for terms.
No MIT or Apache licensing permitted. See `LICENSE`.

## Deliverables Summary

| Component | LOC | Status |
|-----------|-----|--------|
| Specifications | ~3,600 | 7 normative documents |
| Rust Core | ~2,670 | 10 modules + 49 tests |
| Pascal Reference | ~2,450 | 10 modules + 10 tests |
| Lean 4 Proofs | ~350 | 6 modules, convergence theorem |
| C Reference | ~450 | Header-only + benchmarks |
| Conformance Tests | ~2,800 | 52 tests + oracle |
| Kani Proofs | ~400 | 8 formal proofs |
| **Total** | **~12,700** | **Production ready** |

---

### πŸ’Ό Commercial License

SnapKitty code is free and open under the **Sovereign Leviathan covenant (AGPL-3.0 based)** for open-source use. Building a commercial product or service? A **proprietary commercial license** from Snapkitty Collective LLC lets you ship this code without the covenant's source-sharing and network-use obligations.

**[β†’ Get a commercial license](mailto:A.parr@belespritdaccord.uk?subject=Commercial%20license:%20sparse-router-formal)** Β· A.parr@belespritdaccord.uk