Sync with GitHub, license metadata from LICENSE files, commercial license notice
Browse filesmetadata license: None -> other (snapkitty-tri-license)
README: License section kept (already accurate) + commercial notice
README: GitHub source link added
README.md
CHANGED
|
@@ -1,362 +1,378 @@
|
|
| 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 |
-
can
|
| 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 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
| 144 |
-
|
|
| 145 |
-
|
|
| 146 |
-
|
|
| 147 |
-
|
|
| 148 |
-
|
|
| 149 |
-
|
|
| 150 |
-
|
| 151 |
-
--
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
| 159 |
-
|
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
```
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
--
|
| 224 |
-
--
|
| 225 |
-
--
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
--
|
| 234 |
-
--
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
###
|
| 248 |
-
|
| 249 |
-
```bash
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
{
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
βββ
|
| 291 |
-
β βββ
|
| 292 |
-
β βββ
|
| 293 |
-
|
| 294 |
-
β
|
| 295 |
-
β
|
| 296 |
-
β
|
| 297 |
-
β
|
| 298 |
-
β
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
βββ
|
| 302 |
-
β
|
| 303 |
-
β
|
| 304 |
-
β
|
| 305 |
-
βββ
|
| 306 |
-
βββ
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
|
| 325 |
-
|
|
| 326 |
-
|
|
| 327 |
-
|
| 328 |
-
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
|
| 336 |
-
|
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
*
|
| 362 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: other
|
| 3 |
+
license_name: snapkitty-tri-license
|
| 4 |
+
license_link: https://huggingface.co/Snapkitty/bert-agent/blob/main/LICENSE
|
| 5 |
+
tags:
|
| 6 |
+
- snapkitty
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
> Source: [github.com/SNAPKITTYWEST/bert-agent](https://github.com/SNAPKITTYWEST/bert-agent)
|
| 10 |
+
|
| 11 |
+
# BERT Cross-Encoder Entailment Agent
|
| 12 |
+
|
| 13 |
+
[](tests/)
|
| 14 |
+
[](LICENSE)
|
| 15 |
+
[](https://huggingface.co/microsoft/deberta-v3-base)
|
| 16 |
+
[](https://developer.nvidia.com/tensorrt)
|
| 17 |
+
[](daemon/)
|
| 18 |
+
[](agent/ere_gate.py)
|
| 19 |
+
[](daemon/src/ledger.rs)
|
| 20 |
+
[](Invariants.lean)
|
| 21 |
+
|
| 22 |
+
**Authors:** Ahmad Ali Parr, Jessica L. Williams (SNAPKITTYWEST)
|
| 23 |
+
**Stack:** DeBERTa-v3 Β· ONNX Β· TensorRT FP16 Β· Rust/Tokio Β· BLAKE3 Β· WORM ledger Β· ERE P1-P5
|
| 24 |
+
|
| 25 |
+
A production-grade entailment verification agent. Pass a retrieved source chunk and an
|
| 26 |
+
LLM-generated claim; get back a mathematically bounded entailment score, a verdict,
|
| 27 |
+
and a BLAKE3 cryptographic attestation sealed into an append-only WORM audit chain β
|
| 28 |
+
then passed through the ERE five-gate protocol before it leaves the system.
|
| 29 |
+
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
## What This Is
|
| 33 |
+
|
| 34 |
+
LLMs hallucinate. RAG systems retrieve chunks and generate claims against them. Without
|
| 35 |
+
a verification layer, a model can produce a plausible-sounding claim that contradicts
|
| 36 |
+
its own source β and no downstream system will catch it.
|
| 37 |
+
|
| 38 |
+
This agent is the verification layer. It does one thing:
|
| 39 |
+
|
| 40 |
+
> **Given a retrieved source and a generated claim, determine with
|
| 41 |
+
> cryptographic certainty whether the claim is entailed by the source.**
|
| 42 |
+
|
| 43 |
+
It is not a chatbot. It is not a general-purpose NLI system. It is a production daemon
|
| 44 |
+
that runs at the end of every RAG pipeline and refuses to propagate a claim until it
|
| 45 |
+
can prove the claim is entailed.
|
| 46 |
+
|
| 47 |
+
---
|
| 48 |
+
|
| 49 |
+
## How This Compares to Google BERT
|
| 50 |
+
|
| 51 |
+
| Property | Google BERT (2018) | This Agent |
|
| 52 |
+
|----------|-------------------|------------|
|
| 53 |
+
| Architecture | Bi-directional encoder | Cross-encoder (premise ++ hypothesis) |
|
| 54 |
+
| NLI task | Fine-tuned on MNLI only | ANLI + TrueTeacher + MNLI (3-source) |
|
| 55 |
+
| Hallucination detection | Not designed for it | Primary objective |
|
| 56 |
+
| Negation detection | Weak (symmetric embeddings) | Strong (joint self-attention) |
|
| 57 |
+
| Date/number flip detection | Fails | Catches (e.g. 1962 β 1926) |
|
| 58 |
+
| Runtime | Python / TF / PyTorch | Rust daemon, TensorRT FP16, GPU |
|
| 59 |
+
| Latency | ~100-300 ms (Python) | <5 ms batched (TRT) |
|
| 60 |
+
| Throughput | Single request | Dual-trigger continuous batching |
|
| 61 |
+
| Audit trail | None | BLAKE3 + WORM chain per inference |
|
| 62 |
+
| Security gates | None | ERE P1-P5 (5-gate sovereign protocol) |
|
| 63 |
+
| Formal invariants | None | Lean 4, zero sorry |
|
| 64 |
+
| License | Apache 2.0 | Tri-license (AGPL / BSL 1.1 / MIT) |
|
| 65 |
+
|
| 66 |
+
**Key difference:** BERT embeds premise and hypothesis *separately* and compares them
|
| 67 |
+
with cosine similarity. A cross-encoder concatenates them and runs joint self-attention.
|
| 68 |
+
That joint attention is what lets this agent catch subtle hallucinations β the model
|
| 69 |
+
can directly compare "born in 1962" against "born in 1926" at the token level.
|
| 70 |
+
BERT cannot do this. It sees two vectors, not two sentences in dialogue.
|
| 71 |
+
|
| 72 |
+
---
|
| 73 |
+
|
| 74 |
+
## Why Cross-Encoder, Not Bi-Encoder
|
| 75 |
+
|
| 76 |
+
A Bi-Encoder embeds premise and hypothesis separately. At inference, you compare
|
| 77 |
+
embeddings with cosine similarity. This works for semantic similarity, but it misses:
|
| 78 |
+
|
| 79 |
+
- Flipped dates: `"born in 1962"` vs `"born in 1926"` β both embed nearly identically
|
| 80 |
+
- Switched subjects: `"X defeated Y"` vs `"Y defeated X"` β same semantic field
|
| 81 |
+
- Negation: `"the vote passed"` vs `"the vote did not pass"`
|
| 82 |
+
|
| 83 |
+
A Cross-Encoder concatenates both and runs them through a single forward pass:
|
| 84 |
+
|
| 85 |
+
```
|
| 86 |
+
[CLS] retrieved_chunk [SEP] generated_claim [SEP]
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
Self-attention directly compares entities across the premise-hypothesis boundary at
|
| 90 |
+
every layer. The model learns to detect *contradiction*, not just *similarity*.
|
| 91 |
+
This is the architecture that makes hallucination detection tractable.
|
| 92 |
+
|
| 93 |
+
---
|
| 94 |
+
|
| 95 |
+
## Architecture
|
| 96 |
+
|
| 97 |
+
```
|
| 98 |
+
Training Pipeline (Python)
|
| 99 |
+
β
|
| 100 |
+
DeBERTa-v3-base + 3-label head
|
| 101 |
+
Weighted CrossEntropyLoss
|
| 102 |
+
(Contradiction=2.0, Neutral=1.5, Entailment=1.0)
|
| 103 |
+
ANLI + TrueTeacher + MNLI
|
| 104 |
+
β
|
| 105 |
+
βΌ
|
| 106 |
+
ONNX export (dynamic axes)
|
| 107 |
+
β
|
| 108 |
+
ORT graph optimization + FP16
|
| 109 |
+
β
|
| 110 |
+
βΌ
|
| 111 |
+
TensorRT engine (.plan cache)
|
| 112 |
+
β
|
| 113 |
+
Rust Inference Daemon
|
| 114 |
+
ββββββββββββββ΄ββββββββββββββββ
|
| 115 |
+
β Dual-trigger batching β
|
| 116 |
+
β MAX_BATCH or 5 ms timer β
|
| 117 |
+
β Dynamic padding/ndarray β
|
| 118 |
+
β TRT forward pass (GPU) β
|
| 119 |
+
β Softmax β score β
|
| 120 |
+
β BLAKE3 attestation seal β
|
| 121 |
+
β WORM ledger append β
|
| 122 |
+
ββββββββββββββββββββββββββββββ
|
| 123 |
+
β
|
| 124 |
+
HTTP POST /verify
|
| 125 |
+
{ score, verdict, hash }
|
| 126 |
+
β
|
| 127 |
+
βββββββββΌβββββββββ
|
| 128 |
+
β ERE P1-P5 β β agent/ere_gate.py
|
| 129 |
+
β Five gates β
|
| 130 |
+
β P5 seal added β
|
| 131 |
+
βββββββββ¬βββββββββ
|
| 132 |
+
β
|
| 133 |
+
Gated response
|
| 134 |
+
{ score, verdict, hash,
|
| 135 |
+
ere_seal, ere_gates }
|
| 136 |
+
OR { ere_halt: true }
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
---
|
| 140 |
+
|
| 141 |
+
## Milestones
|
| 142 |
+
|
| 143 |
+
| # | Milestone | Status |
|
| 144 |
+
|---|-----------|--------|
|
| 145 |
+
| M1 | DeBERTa-v3 cross-encoder + weighted 3-label loss | β
Done |
|
| 146 |
+
| M2 | ANLI + TrueTeacher + MNLI joint training dataset | β
Done |
|
| 147 |
+
| M3 | ONNX export + ORT FP16 graph optimization | β
Done |
|
| 148 |
+
| M4 | TensorRT engine with .plan caching | β
Done |
|
| 149 |
+
| M5 | Rust inference daemon (Tokio, Axum, dual-trigger batching) | β
Done |
|
| 150 |
+
| M6 | BLAKE3 attestation seal per inference | β
Done |
|
| 151 |
+
| M7 | WORM append-only audit chain (tamper-evident) | β
Done |
|
| 152 |
+
| M8 | FPR=0.0 PR-curve threshold calibration | β
Done |
|
| 153 |
+
| M9 | 11/11 tests passing (dataset, calibrate, ledger) | β
Done |
|
| 154 |
+
| M10 | Lean 4 formal invariants (zero sorry) | β
Done |
|
| 155 |
+
| M11 | ERE P1-P5 gate integration (`agent/ere_gate.py`) | β
Done |
|
| 156 |
+
| M12 | Tri-license (AGPL / BSL 1.1 / MIT) | β
Done |
|
| 157 |
+
| M13 | Sovereign Engine v2 gap integration (Gap 4 candidate) | π Planned |
|
| 158 |
+
| M14 | Rust daemon ERE gate enforcement (inline, pre-response) | π Planned |
|
| 159 |
+
| M15 | Benchmark vs NLI baselines (BERT, RoBERTa, DeBERTa-v2) | π Planned |
|
| 160 |
+
|
| 161 |
+
---
|
| 162 |
+
|
| 163 |
+
## ERE Gate Protocol
|
| 164 |
+
|
| 165 |
+
Every verdict produced by this agent passes through the **ERE (Expected Reasoning Error)**
|
| 166 |
+
five-gate protocol before leaving the system:
|
| 167 |
+
|
| 168 |
+
| Gate | Check | Failure means |
|
| 169 |
+
|------|-------|---------------|
|
| 170 |
+
| P1 | No secrets in payload | Credential leaked in model output |
|
| 171 |
+
| P2 | No eval / code injection | Adversarial input tried to inject code |
|
| 172 |
+
| P3 | Loop safety | Output contains infinite loop without exit |
|
| 173 |
+
| P4 | No telemetry beacons | Analytics SDK call in model output |
|
| 174 |
+
| P5 | SHA-256 audit seal | Commitment over `agent_id:intent:verdict` |
|
| 175 |
+
|
| 176 |
+
A verdict that fails any gate is suppressed. The caller receives `{ ere_halt: true }`.
|
| 177 |
+
The WORM ledger records the halt. The chain is not broken.
|
| 178 |
+
|
| 179 |
+
```python
|
| 180 |
+
from agent.ere_gate import gate_verdict
|
| 181 |
+
|
| 182 |
+
raw = {"score": 0.98, "verdict": "Entailment", "hash": "a3f8..."}
|
| 183 |
+
gated = gate_verdict(
|
| 184 |
+
premise="The Battle of Hastings took place in 1066.",
|
| 185 |
+
hypothesis="Hastings occurred in 1066.",
|
| 186 |
+
raw_verdict=raw,
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
if not gated.allowed:
|
| 190 |
+
raise RuntimeError(f"ERE halt: {gated.violations}")
|
| 191 |
+
|
| 192 |
+
print(gated.to_dict())
|
| 193 |
+
# { score, verdict, hash, ere_seal, ere_gates: {P1:T, P2:T, P3:T, P4:T, P5:T} }
|
| 194 |
+
```
|
| 195 |
+
|
| 196 |
+
---
|
| 197 |
+
|
| 198 |
+
## Full Pipeline
|
| 199 |
+
|
| 200 |
+
### 1. Install dependencies
|
| 201 |
+
|
| 202 |
+
```bash
|
| 203 |
+
pip install -r requirements.txt
|
| 204 |
+
```
|
| 205 |
+
|
| 206 |
+
### 2. Download datasets
|
| 207 |
+
|
| 208 |
+
```
|
| 209 |
+
data/
|
| 210 |
+
anli/R1/{train,dev,test}.jsonl
|
| 211 |
+
anli/R2/{train,dev,test}.jsonl
|
| 212 |
+
anli/R3/{train,dev,test}.jsonl
|
| 213 |
+
trueteacher/{train,dev}.jsonl # Google, 1.4M records
|
| 214 |
+
mnli/{train,dev}.jsonl
|
| 215 |
+
```
|
| 216 |
+
|
| 217 |
+
### 3. Fine-tune DeBERTa-v3
|
| 218 |
+
|
| 219 |
+
```bash
|
| 220 |
+
python -m bert.train \
|
| 221 |
+
--data_dir data/ \
|
| 222 |
+
--output_dir checkpoints/ \
|
| 223 |
+
--backbone microsoft/deberta-v3-base \
|
| 224 |
+
--epochs 5 \
|
| 225 |
+
--batch_size 32 \
|
| 226 |
+
--lr 2e-5
|
| 227 |
+
```
|
| 228 |
+
|
| 229 |
+
### 4. Export to ONNX + optimize FP16
|
| 230 |
+
|
| 231 |
+
```bash
|
| 232 |
+
python -m bert.export \
|
| 233 |
+
--checkpoint checkpoints/best_model.pt \
|
| 234 |
+
--output_dir onnx/ \
|
| 235 |
+
--device cuda
|
| 236 |
+
```
|
| 237 |
+
|
| 238 |
+
### 5. Calibrate rejection threshold
|
| 239 |
+
|
| 240 |
+
```bash
|
| 241 |
+
python -m bert.calibrate \
|
| 242 |
+
--checkpoint checkpoints/best_model.pt \
|
| 243 |
+
--data_dir data/ \
|
| 244 |
+
--output config/threshold.json
|
| 245 |
+
```
|
| 246 |
+
|
| 247 |
+
### 6. Build and run the Rust daemon
|
| 248 |
+
|
| 249 |
+
```bash
|
| 250 |
+
cd daemon
|
| 251 |
+
cargo build --release
|
| 252 |
+
RUST_LOG=info ./target/release/bert-daemon --config ../config/daemon.json
|
| 253 |
+
```
|
| 254 |
+
|
| 255 |
+
First startup: ~5 min for TensorRT engine compilation. Subsequent starts: instant from `.plan` cache.
|
| 256 |
+
|
| 257 |
+
### 7. Verify a claim
|
| 258 |
+
|
| 259 |
+
```bash
|
| 260 |
+
curl -X POST http://localhost:8080/verify \
|
| 261 |
+
-H "Content-Type: application/json" \
|
| 262 |
+
-d '{
|
| 263 |
+
"premise": "The Battle of Hastings took place in 1066.",
|
| 264 |
+
"hypothesis": "Hastings occurred in 1066.",
|
| 265 |
+
"chunk_id": "chunk-001"
|
| 266 |
+
}'
|
| 267 |
+
```
|
| 268 |
+
|
| 269 |
+
Response:
|
| 270 |
+
|
| 271 |
+
```json
|
| 272 |
+
{
|
| 273 |
+
"score": 0.9812,
|
| 274 |
+
"verdict": "Entailment",
|
| 275 |
+
"hash": "a3f8d2c1...",
|
| 276 |
+
"ere_seal": "7f3c8a19...",
|
| 277 |
+
"ere_gates": { "P1": true, "P2": true, "P3": true, "P4": true, "P5": true }
|
| 278 |
+
}
|
| 279 |
+
```
|
| 280 |
+
|
| 281 |
+
The `hash` is the BLAKE3 attestation over the inference. The `ere_seal` is the P5 SHA-256
|
| 282 |
+
commitment over `agent_id:intent:verdict`. Both are recorded in the WORM ledger.
|
| 283 |
+
|
| 284 |
+
---
|
| 285 |
+
|
| 286 |
+
## File Structure
|
| 287 |
+
|
| 288 |
+
```
|
| 289 |
+
bert-agent/
|
| 290 |
+
βββ agent/
|
| 291 |
+
β βββ __init__.py # Exports BERTEREGate, GatedVerdict, gate_verdict
|
| 292 |
+
β βββ ere_gate.py # ERE P1-P5 gate adapter for BERT verdicts
|
| 293 |
+
βββ bert/
|
| 294 |
+
β βββ dataset.py # ANLI + TrueTeacher + MNLI cross-encoder dataset
|
| 295 |
+
β βββ model.py # DeBERTa-v3 cross-encoder + weighted loss
|
| 296 |
+
β βββ train.py # Fine-tuning loop (AdamW + cosine LR)
|
| 297 |
+
β βββ export.py # ONNX export + ORT FP16 graph optimization
|
| 298 |
+
β βββ trt_session.py # TensorRT ORT session with optimization profiles
|
| 299 |
+
β βββ calibrate.py # PR curve threshold calibration
|
| 300 |
+
βββ daemon/
|
| 301 |
+
β βββ Cargo.toml
|
| 302 |
+
β βββ src/
|
| 303 |
+
β βββ main.rs # Startup, channel wiring
|
| 304 |
+
β βββ types.rs # VerifyRequest, Attestation, Config
|
| 305 |
+
β βββ session.rs # TRT ORT session init
|
| 306 |
+
β βββ inference.rs # Dual-trigger continuous batching loop
|
| 307 |
+
β βββ ledger.rs # WORM append-only audit chain
|
| 308 |
+
β βββ server.rs # Axum HTTP /verify handler
|
| 309 |
+
βββ config/
|
| 310 |
+
β βββ daemon.json
|
| 311 |
+
βββ tests/
|
| 312 |
+
β βββ test_dataset.py
|
| 313 |
+
β βββ test_calibrate.py
|
| 314 |
+
β βββ test_ledger.py
|
| 315 |
+
βββ Invariants.lean # Lean 4 formal invariants, zero sorry
|
| 316 |
+
βββ LICENSE # Tri-license: AGPL-3.0 | BSL 1.1 | MIT
|
| 317 |
+
βββ requirements.txt
|
| 318 |
+
```
|
| 319 |
+
|
| 320 |
+
---
|
| 321 |
+
|
| 322 |
+
## Design Decisions
|
| 323 |
+
|
| 324 |
+
| Decision | Why |
|
| 325 |
+
|----------|-----|
|
| 326 |
+
| DeBERTa-v3 over BERT/RoBERTa | Disentangled attention handles positional reasoning β critical for detecting reordered events |
|
| 327 |
+
| Cross-Encoder over Bi-Encoder | Cannot cache embeddings, but self-attention compares entities across premise-hypothesis directly |
|
| 328 |
+
| Weighted loss (2.0/1.5/1.0) | False-positive Entailment is the worst failure mode β weight Contradiction higher |
|
| 329 |
+
| ANLI + TrueTeacher | Standard NLI is too easy; TrueTeacher mirrors actual LLM hallucination patterns |
|
| 330 |
+
| FPR=0.0 threshold calibration | In a verification engine, precision > recall β never cite a hallucinated claim |
|
| 331 |
+
| Dual-trigger batching (batch size OR 5ms) | Bounded latency guarantee without sacrificing GPU throughput |
|
| 332 |
+
| Dynamic padding per batch | Pad to longest sequence in the batch, not global max β avoids wasted compute |
|
| 333 |
+
| BLAKE3 + bincode attestation | Memory-bandwidth hashing speed; deterministic binary serialization (no JSON ordering ambiguity) |
|
| 334 |
+
| WORM ledger as chain | Every record links to previous hash β tamper detection is immediate |
|
| 335 |
+
| ERE P1-P5 gate layer | Every verdict inspected for secrets, injection, loops, telemetry before propagation |
|
| 336 |
+
| Lean 4 invariants | Formal proof that well-formed agents satisfy trust and entropy bounds β not just assertions |
|
| 337 |
+
|
| 338 |
+
---
|
| 339 |
+
|
| 340 |
+
## Theoretical Foundation
|
| 341 |
+
|
| 342 |
+
This agent is a component of the **Sovereign Stack**. Its cryptographic and formal
|
| 343 |
+
foundations are documented in the following published papers:
|
| 344 |
+
|
| 345 |
+
| DOI | Contribution |
|
| 346 |
+
|-----|-------------|
|
| 347 |
+
| [10.5281/zenodo.21443609](https://doi.org/10.5281/zenodo.21443609) | Jordan Spectral Transformer β phi-weighted routing |
|
| 348 |
+
| [10.5281/zenodo.21132094](https://doi.org/10.5281/zenodo.21132094) | Sovereign Compute Architecture |
|
| 349 |
+
| [10.5281/zenodo.20678420](https://doi.org/10.5281/zenodo.20678420) | Attention Exhaustion Attacks β 0% detection rate |
|
| 350 |
+
| [10.5281/zenodo.21268911](https://doi.org/10.5281/zenodo.21268911) | GKN I4 Quartic Invariant and E7 Symmetry |
|
| 351 |
+
|
| 352 |
+
Unified paper: [The Sovereign Stack](https://snapkittywest.github.io/hyperkitty/papers/sovereign-stack-unified.pdf)
|
| 353 |
+
|
| 354 |
+
---
|
| 355 |
+
|
| 356 |
+
## License
|
| 357 |
+
|
| 358 |
+
Tri-license β choose any one:
|
| 359 |
+
|
| 360 |
+
- **AGPL-3.0** for open source / community use
|
| 361 |
+
- **BSL 1.1 β MIT** for commercial / production use (< 5 servers free; converts to MIT 2029-01-01)
|
| 362 |
+
- **MIT** after 2029-01-01
|
| 363 |
+
|
| 364 |
+
See [LICENSE](LICENSE) for the full text and list of six protected inventions.
|
| 365 |
+
|
| 366 |
+
Copyright (C) 2026 Ahmad Ali Parr, Jessica L. Williams / SNAPKITTYWEST
|
| 367 |
+
Bel Esprit D'Accord Irrevocable Trust
|
| 368 |
+
|
| 369 |
+
---
|
| 370 |
+
|
| 371 |
+
*Built to catch what BERT cannot see.*
|
| 372 |
+
*Every claim sealed. Every halt recorded. Nothing propagates without proof.*
|
| 373 |
+
|
| 374 |
+
### πΌ Commercial License
|
| 375 |
+
|
| 376 |
+
Snapkitty code is free and open under **AGPL-3.0** 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 AGPL's source-sharing and network-use obligations.
|
| 377 |
+
|
| 378 |
+
**[β Get a commercial license](mailto:A.parr@belespritdaccord.uk?subject=Commercial%20license:%20bert-agent)** Β· A.parr@belespritdaccord.uk
|