affidavit 26.6.22

Provenance Layer — receipt assembly and certification (verify a witness against a format standard; never decide honesty).
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
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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# affidavit — Provenance Layer Documentation

**Version:** 26.6.22  
**Project:** Receipt Assembly & Certification  
**Language:** Rust (2021 edition)  
**License:** MIT OR Apache-2.0

---

## Overview

`affidavit` implements the **Provenance Layer**: an append-only, content-addressed chain of operation-events that certify what a process did. The `affi` CLI lets you:

1. **Emit** operation-events (record what happened)
2. **Assemble** into immutable receipts (finalize the chain)
3. **Verify** receipts against a formal standard (certify without deciding)

The project's doctrine: **certify, don't decide.** The verifier checks a receipt against a format standard and never decides whether work is honest.

---

## Architecture

### High-Level Structure

```
affidavit/
├── src/
│   ├── bin/affi.rs           # CLI entrypoint
│   ├── lib.rs                # Public API
│   ├── cli.rs                # Clap configuration (noun-verb pattern)
│   ├── chain.rs              # Receipt construction & sealing
│   ├── verifier.rs           # 7-stage certify pipeline
│   ├── types.rs              # Domain types (Event, Receipt, Verdict)
│   ├── admission.rs          # Validation gates
│   ├── discovery.rs          # Type discovery & schema registry
│   ├── ocel.rs               # Object-Centric Event Logs integration
│   ├── handlers.rs           # Event dispatch & routing
│   ├── lsp.rs                # Language server integration
│   ├── tracing.rs            # Observable spans & telemetry
│   ├── quality.rs            # Western Electric SPC monitoring
│   ├── sbom.rs               # SBOM generation & parsing
│   ├── sbom_compliance.rs    # NTIA compliance checking
│   ├── sbom_vulnerability.rs # Vulnerability aggregation & risk
│   ├── verbs/                # Command implementations (65+ verbs)
│   │   ├── emit.rs           # emit — record an event
│   │   ├── assemble.rs       # assemble — finalize receipt
│   │   ├── verify.rs         # verify — certify pipeline
│   │   ├── show.rs           # show — human-readable dump
│   │   ├── inspect.rs        # inspect — detailed analysis
│   │   ├── diagnose.rs       # diagnose — troubleshoot failures
│   │   ├── stats.rs          # stats — chain metrics
│   │   ├── graph.rs          # graph — DAG visualization
│   │   ├── replay.rs         # replay — re-execute chain
│   │   ├── model.rs          # model — type schema extraction
│   │   └── conformance.rs    # conformance — profile checking
│   └── mod.rs                # Module tree
├── examples/
│   ├── golden_run.sh         # Full lifecycle (emit → assemble → verify)
│   ├── chain_build.rs        # Manual receipt construction
│   ├── full_pipeline.rs      # End-to-end example
│   ├── admission_gate.rs     # Validation patterns
│   ├── verify_stages.rs      # Verify pipeline details
│   ├── ocel_events.rs        # OCEL integration
│   ├── verdict_diagnostics.rs # Parsing verdicts
│   ├── observable_spans.rs   # Telemetry & tracing
│   ├── receipt_determinism.rs # Determinism guarantees
│   └── discover_shapeb.rs    # Type discovery
├── tests/
│   └── [integration tests]
├── benches/
│   └── receipt_operations/   # Criterion benchmarks
├── Cargo.toml
├── README.md
└── CLAUDE.md [this file]
```

### Dependency Ecosystem

**Local dependencies** (monorepo):
- `clap-noun-verb` — CLI argument parsing (noun/verb pattern)
- `clnrm-core` — Canonicalization & normalization primitives
- `wasm4pm` — Package manager & module system
- `lsp-max` — Language server extensions

**External crates**:
- `blake3` — Content addressing & chain hashing
- `serde` / `serde_json` — Serialization
- `anyhow` / `thiserror` — Error handling
- `linkme` — Plugin discovery
- `opentelemetry` — Observability (via feature gate)

---

## Key Concepts

### 1. The Receipt Chain

A receipt is a **BLAKE3 chain of operation-events**:

```json
{
  "format_version": "core/v1",
  "events": [
    {
      "seq": 0,
      "event_id": "evt-0",
      "event_type": "build",
      "objects": [{"id": "repo:main", "type": "git"}],
      "commitment": "6ef47c82..."
    },
    {
      "seq": 1,
      "event_id": "evt-1",
      "event_type": "test",
      "objects": [{"id": "suite:unit", "type": "test-suite"}],
      "commitment": "a2d95f11..."
    }
  ],
  "chain_hash": "203d3bbf...",
  "profile": "core/v1"
}
```

Each event's content-address (`blake3(payload)`) is stored as `commitment`. The rolling chain hash folds each event's bytes into the next, so **any edit propagates through all later links**.

### 2. Sealing & Immutability

Receipts are **sealed** — their `_seal` field is private and only constructible via the canonical `ChainAssembler::finalize` method. This makes struct-literal construction impossible at compile time (Rust: `E0451`).

```rust
// This fails at compile-time:
let receipt = Receipt { _seal: (), ... };  // ERROR: private field

// This works (only canonical path):
let receipt = assembler.finalize()?;       // ✓
```

### 3. The Verifier: 7-Stage Pipeline

The verifier maps 1:1 to a C4 Level-3 component view:

| # | Stage | Component | Check |
|---|-------|-----------|-------|
| 1 | `decode` | decode | Receipt is present & version field parses |
| 2 | `check_format` | check_format | `format_version == "core/v1"` |
| 3 | `chain_integrity` | check_chain_integrity | Recompute rolling BLAKE3; must match stored `chain_hash` |
| 4 | `continuity` | resolve_continuity | `seq` contiguous from 0; event IDs unique |
| 5 | `verify_commitments` | verify_commitments | Each commitment is a well-formed BLAKE3 digest |
| 6 | `evaluate_profile` | evaluate_profile | Profile `core/v1`: each event has `event_type` & commitment |
| 7 | `emit_verdict` | emit Verdict | ACCEPT iff all stages pass; else REJECT with first failure reason |

**Exit codes:**
- `0` → ACCEPT
- `2` → REJECT

### 4. Determinism Guarantees

- **No wall-clock:** Events ordered by monotonic `seq`, not timestamps
- **Canonical JSON:** Events serialized with deterministic field ordering
- **Same inputs → same receipt:** Receipt hash is reproducible

---

## CLI Surface

### 65+ Canonical Verbs

Affidavit v26.6.22 provides a comprehensive CLI organized into 9 primary verb families:

**Core Provenance (11 verbs):**
`emit`, `assemble`, `verify`, `show`, `inspect`, `diagnose`, `stats`, `graph`, `replay`, `model`, `conformance`

**Emit Variants (8 verbs):**
`emit-batch`, `emit-from-cicd`, `emit-from-cloud`, `emit-from-github`, `emit-from-gitlab`, `emit-from-monitoring`, `emit-from-sbom`, `emit-from-security`

**Assemble Variants (2 verbs):**
`assemble-and-notarize`, `assemble-with-signature`

**Verify Variants (3 verbs):**
`verify-compliance`, `verify-family`, `verify-sla`

**SBOM & Supply Chain (5 verbs):**
`sbom-scan`, `sbom-attest`, `sbom-blast-radius`, `sbom-compliance`, `sbom-ntia`

**Quality & Monitoring (6 verbs):**
`monitor`, `portfolio-health`, `trend-analysis`, `variance`, `anomaly-detect`, `predict`

**Audit & Compliance (10 verbs):**
`audit`, `attest`, `notarize`, `sign`, `gdpr-proof`, `hipaa`, `pci-dss`, `soc2-audit`, `license-compliance`, `policy-enforce`

**Analysis & Troubleshooting (14 verbs):**
`causality-chain`, `dependency-matrix`, `security-debt`, `tech-debt`, `root-cause`, `explain-incident`, `find-blast-radius`, `bus-factor`, `orphaned-code`, `coverage-analysis`, `dora-metrics`, `team-velocity`, `find-slow-test`, `regression-analysis`

**Developer Tools (6 verbs):**
`doctor`, `diff`, `visualize`, `catalog`, `search`, `query`, `timeline`, `profile`, `receipt-throughput`, `install-git-hook`, `test`

See `src/verbs/` for implementation details or run `affi --help` for full usage.

---

## Development Workflow

### Build & Test

```bash
# Build debug binary
cargo build
cargo run --bin affi -- emit --help

# Run all tests (211+ tests across unit, integration, property, and compliance suites)
cargo test

# Run with output
cargo test -- --nocapture

# Run specific test
cargo test verify_chain_integrity

# Benches (requires nightly or --release)
cargo bench --bench receipt_operations
```

### Run the Golden Example

```bash
# Full lifecycle: emit → assemble → verify (honest) → verify (tampered)
bash examples/golden_run.sh
```

Output shows:
- Event emission and sequencing
- Receipt assembly with content address
- 7-stage verify pipeline on clean receipt (ACCEPT)
- Tampered receipt detection (REJECT with reason)

### Key Test Patterns

**Unit tests** (`src/lib.rs`):
- Chain construction & sealing
- BLAKE3 commitment verification
- Event admission gates

**Dispatch tests** (`src/handlers.rs`):
- Event routing & handler dispatch
- Type discovery & schema resolution

**E2E tests** (`tests/`):
- Full CLI pipeline
- Receipt round-trip (emit → assemble → verify)
- Tampering detection

**UI tests** (`src/cli.rs`):
- CLI argument parsing
- Error messages & output formatting

---

## Integration Points

### OCEL (Object-Centric Event Logs)

`src/ocel.rs` provides integration with OCEL standards:

```rust
use affidavit::ocel::{OcelEvent, OcelAdapter};

// Convert affidavit event to OCEL
let ocel_event = OcelAdapter::from_receipt_event(&event)?;
```

See `examples/ocel_events.rs`.

### LSP (Language Server)

`src/lsp.rs` exposes receipt verification as LSP diagnostics:

- Hover over a receipt path → shows receipt summary
- Diagnostics → shows verification failures per-line
- Code actions → suggests fixes

Integrates via `lsp-max`.

### Observable Spans (Telemetry)

With the `otel` feature:

```bash
cargo build --features otel
cargo run --bin affi -- verify receipt.json
  # Emits OpenTelemetry spans to local Jaeger
```

See `examples/observable_spans.rs` for span structure.

### Type Discovery & Plugins

Via `linkme`, custom event handlers and type schemas can be discovered:

```rust
#[linkme::distributed_slice(CUSTOM_HANDLERS)]
pub static MY_HANDLER: EventHandler = EventHandler::new("my-type", handle_my_event);
```

---

## Code Conventions

### File Layout

- **Private modules** (`src/discovery.rs`): internal machinery
- **Public API** (`src/lib.rs`): re-exports & documentation
- **Verbs** (`src/verbs/*.rs`): one command per file
- **Examples** (`examples/*.rs`): runnable demonstrations
- **Tests** inline (unit) or in `tests/` (integration)

### Naming

- **Event types:** lowercase + dash (`build`, `test`, `audit-log`)
- **Object IDs:** `id:type[:qualifier]` format (`repo:main`, `suite:unit:fast`)
- **Commitment digests:** lowercase hex, no prefix
- **Receipt files:** blake3 hash of canonical bytes OR named `.json`

### Error Handling

- **Admission gates** (`src/admission.rs`): reject invalid events early
- **Verifier stages** (`src/verifier.rs`): fail fast, report first failure
- **CLI** (`src/cli.rs`): map internal errors to user-friendly messages

### No Unwrap Policy

All fallible operations use `Result<T, E>` with proper error propagation. Tests may use `.unwrap()` for brevity.

---

## Common Tasks

### Add a New Verb (Command)

1. Create `src/verbs/myverb.rs`
2. Implement `pub async fn handle_myverb(args: MyVerbArgs) -> Result<()>`
3. Add to `src/verbs/mod.rs` and `src/cli.rs`
4. Add a test in the module
5. (Optional) Add an example in `examples/`

### Extend the Verifier

1. Add a new stage struct in `src/verifier.rs`
2. Implement the `VerificationStage` trait
3. Insert into the pipeline (order matters!)
4. Update the stage table in README & this file
5. Add a unit test

### Add a Custom Event Handler

1. Create `src/handlers/myhandler.rs`
2. Implement the handler function signature
3. Register via `#[linkme::distributed_slice]` or manual registry
4. Add tests

### Integrate with New Ecosystem

1. Add dependency to `Cargo.toml`
2. Create integration module (`src/myecosystem.rs`)
3. Re-export from `src/lib.rs`
4. Add example in `examples/`
5. Document in this file (Integration Points section)

---

## Documentation Ecosystem

- **README.md** — Quick start, doctrine, CLI surface, worked example
- **CLAUDE.md** — Full project guide (this file)
- **Cargo.toml** — Package metadata, dependencies, features
- **src/lib.rs** — Public API docs & module overview
- **examples/** — Runnable demonstrations of key workflows
- **CHANGELOG.md** — Version history & breaking changes
- **LSP_MAX_INTEGRATION_*.md** — Language server integration details
- **CLNRM_INTEGRATION_*.md** — Canonicalization integration
- **WASM4PM_*.md** — Package manager integration

---

## Testing Strategy

### Test Organization

```
Unit tests (19)
├── src/chain.rs (5) — sealing, rolling hash
├── src/admission.rs (4) — gate validation
├── src/types.rs (3) — type parsing
├── src/discovery.rs (3) — schema resolution
└── src/verifier.rs (4) — stage correctness

Dispatch tests (6)
└── src/handlers.rs — handler routing

E2E tests (4)
└── tests/ — full CLI pipeline

UI tests (1)
└── src/cli.rs — argument parsing & help text
```

### Running Tests

```bash
# All tests
cargo test

# Specific test
cargo test test_chain_integrity

# With logging
RUST_LOG=debug cargo test -- --nocapture

# Single-threaded (for determinism checks)
cargo test -- --test-threads=1

# Benches
cargo bench
```

### Determinism Testing

For receipt determinism guarantees, use `--test-threads=1`:

```bash
cargo test receipt_determinism -- --test-threads=1
```

---

## Performance Characteristics

### Benchmarked Operations (see `benches/receipt_operations/`)

- **Emit event:** ~100µs (JSON parse + admission + sealing)
- **Assemble 100 events:** ~50ms (rolling BLAKE3 computation)
- **Verify 100-event receipt:** ~75ms (full 7-stage pipeline)
- **Memory (100 events):** ~500KB (typical)

### Optimization Strategies

- **Lazy chain hashing:** Recompute only on verify, not on emit
- **Streaming JSON:** For large receipts, stream decode via `serde_json::StreamDeserializer`
- **Parallel verification:** Stages 3–6 could run in parallel (stage 7 is terminal)

---

## Troubleshooting

### Receipt Fails at `chain_integrity` Stage

**Symptom:** `chain hash mismatch`

**Cause:** Receipt was tampered with (event field modified).

**Debug:**
```bash
affi inspect receipt.json --stage chain_integrity
affi diagnose receipt.json --suggest-fixes
```

### Event Rejected at Admission Gate

**Symptom:** `emit` returns "event rejected"

**Cause:** Event violates a validation rule (e.g., object ID malformed, commitment invalid).

**Fix:**
```bash
affi diagnose receipt.json --verbose
# Check object IDs are "id:type[:qualifier]"
# Check commitments are valid BLAKE3 digests (hex, 64 chars)
```

### Receipt Doesn't Verify in LSP Hover

**Symptom:** Hover on receipt path shows "failed to verify"

**Debug:**
```bash
affi verify receipt.json --format json
# Use JSON output for machine parsing; check first failure stage
```

---

## Roadmap & Future Work

- **Parallel verification:** Stages 3–6 as concurrent tasks
- **Streaming receipts:** Support event appends without re-finalization
- **Multi-profile validation:** Support multiple `core/vX` standards
- **Distributed chain:** Merkle-proof verification across shards
- **Web dashboard:** Visual receipt exploration

---

## License

MIT OR Apache-2.0

---

## References

- [BLAKE3]https://github.com/BLAKE3-team/BLAKE3 — Content addressing
- [Serde]https://serde.rs/ — Serialization framework
- [OpenTelemetry]https://opentelemetry.io/ — Observability standards
- [OCEL]https://www.ocel-standard.org/ — Object-Centric Event Logs
- [C4 Model]https://c4model.com/ — Architecture diagramming

---

**Last Updated:** 2026-06-22  
**Maintained by:** Sean Chatman (xpointsh@gmail.com)