# AiGentsy Settlement Protocol — Conformance Specification

**Status:** Production · v2.0

## Overview

Third-party implementations must produce identical hashes for identical inputs.
This document defines the canonical hash algorithms and test vectors.

## Hash Algorithms

### Idempotency Key
```
key = "idem_" + SHA256(json.dumps({deal_id, action, ...params}, sort_keys=True))[:24]
```

### Event Hash
```
hash = SHA256(json.dumps({event_id, event_type, deal_id, actor_id, timestamp, payload, prev_hash}, sort_keys=True))
```

### Scope Lock Hash
```
composite = f"{vertical}|{sku_id}|{scope_summary}|{estimated_price}|{policy_hash}|{proof_hash}"
hash = SHA256(composite)[:32]
```

### Policy Hash
```
hash = SHA256(json.dumps(predicates, sort_keys=True))[:24]
```

### Verification Receipt Hash
```
composite = f"{deal_id}|{proof_hash}|{provider}|{verified}|{confidence}"
hash = SHA256(composite)[:24]
```

### Ledger Dedup Key
```
canonical = json.dumps({deal_id, entry_type, ref, debit, credit, currency, counterparty, event_id}, sort_keys=True)
hash = SHA256(canonical)[:24]
```

## Running Conformance Tests

### Against local server
```bash
python -m pytest tests/conformance/ -v
```

### Against remote implementation
```bash
AME_BASE=https://your-server.com python -m pytest tests/conformance/test_protocol_core.py -v
```

## Test Vector Files

- `schemas/test_vectors.json` — deterministic hash vectors
- `data/conformance_vectors.json` — exportable specification for third parties

## Fee Schedule Invariants

- Protocol fee = `gross * 0.028 + $0.28`
- Volume discounts: 50k+ (0.85x), 10k+ (0.90x), 1k+ (0.95x)
- Splits invariant: `gross == platform_fee + protocol_fee + net`

## Event Chain Invariants

- Every event has a `deal_id` (required)
- `prev_hash` links to the previous event's `hash` in the same deal chain
- First event in chain has `prev_hash = ""`
- Finality events: PROOF_READY, PROOF_VERIFIED, GO_APPROVED, AUTO_GO_APPROVED, SETTLED, PAYOUT_CONFIRMED, OUTCOME_RECORDED
