verification-ledger 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- verification_ledger/__init__.py +12 -0
- verification_ledger/__main__.py +56 -0
- verification_ledger/conformance/__init__.py +7 -0
- verification_ledger/conformance/__main__.py +8 -0
- verification_ledger/conformance/adapters/__init__.py +1 -0
- verification_ledger/conformance/adapters/reference.py +70 -0
- verification_ledger/conformance/contract.py +69 -0
- verification_ledger/conformance/probes.py +224 -0
- verification_ledger/conformance/run.py +91 -0
- verification_ledger/conformance/score.py +105 -0
- verification_ledger/demo/__init__.py +4 -0
- verification_ledger/demo/__main__.py +8 -0
- verification_ledger/demo/replay.py +96 -0
- verification_ledger/demo/seed.py +45 -0
- verification_ledger/envelope.py +36 -0
- verification_ledger/gate.py +56 -0
- verification_ledger/invariants.py +55 -0
- verification_ledger/ledger.py +360 -0
- verification_ledger/model.py +54 -0
- verification_ledger/promote.py +45 -0
- verification_ledger/retention.py +35 -0
- verification_ledger/server.py +140 -0
- verification_ledger-0.1.0.dist-info/METADATA +109 -0
- verification_ledger-0.1.0.dist-info/RECORD +27 -0
- verification_ledger-0.1.0.dist-info/WHEEL +4 -0
- verification_ledger-0.1.0.dist-info/entry_points.txt +2 -0
- verification_ledger-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""verification-ledger: a governed coordination ledger for local agent fleets.
|
|
2
|
+
|
|
3
|
+
The four guarantees (VL-1 provenance typing, VL-2 promotion gate, VL-3
|
|
4
|
+
instruction-boundary envelope, VL-4 retention invariant) are defined in SPEC.md
|
|
5
|
+
and proven by the conformance suite in ``verification_ledger.conformance``.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
|
|
12
|
+
__all__ = ["__version__"]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""CLI entry point for verification-ledger.
|
|
2
|
+
|
|
3
|
+
Subcommands:
|
|
4
|
+
--version print the package version
|
|
5
|
+
promote <db_path> <record_id> the out-of-band operator promotion ceremony
|
|
6
|
+
serve [db_path] run the in-band MCP stdio server (needs the mcp extra)
|
|
7
|
+
demo run the scripted replay on synthetic data
|
|
8
|
+
|
|
9
|
+
The conformance runner is ``python -m verification_ledger.conformance``.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import sys
|
|
15
|
+
|
|
16
|
+
_USAGE = (
|
|
17
|
+
"verification-ledger — subcommands:\n"
|
|
18
|
+
" --version print the package version\n"
|
|
19
|
+
" promote <db_path> <record_id> out-of-band operator promotion (SPEC.md § VL-2)\n"
|
|
20
|
+
" serve [db_path] in-band MCP stdio server (needs the mcp extra)\n"
|
|
21
|
+
" demo scripted replay on synthetic data\n"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def main(argv: list[str] | None = None) -> int:
|
|
26
|
+
args = sys.argv[1:] if argv is None else argv
|
|
27
|
+
if args and args[0] == "--version":
|
|
28
|
+
from verification_ledger import __version__
|
|
29
|
+
|
|
30
|
+
print(__version__)
|
|
31
|
+
return 0
|
|
32
|
+
if args and args[0] == "promote":
|
|
33
|
+
from verification_ledger.promote import main as promote_main
|
|
34
|
+
|
|
35
|
+
return promote_main(args[1:])
|
|
36
|
+
if args and args[0] == "serve":
|
|
37
|
+
from verification_ledger.server import main as serve_main
|
|
38
|
+
|
|
39
|
+
try:
|
|
40
|
+
return serve_main(args[1:])
|
|
41
|
+
except ImportError:
|
|
42
|
+
print(
|
|
43
|
+
"the MCP server requires the 'mcp' extra: "
|
|
44
|
+
"pip install 'verification-ledger[mcp]'"
|
|
45
|
+
)
|
|
46
|
+
return 1
|
|
47
|
+
if args and args[0] == "demo":
|
|
48
|
+
from verification_ledger.demo.replay import main as demo_main
|
|
49
|
+
|
|
50
|
+
return demo_main()
|
|
51
|
+
print(_USAGE, end="")
|
|
52
|
+
return 0
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
if __name__ == "__main__":
|
|
56
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""The conformance suite: executable proof that a store enforces the contract.
|
|
2
|
+
|
|
3
|
+
Runs bidirectional probes (positive + adversarial) per invariant against any
|
|
4
|
+
store that implements the ``LedgerAdapter`` protocol, and reports a top-line
|
|
5
|
+
Ledger Conformance Score. The suite is the standard; the bundled reference
|
|
6
|
+
implementation is one conformant store. See SPEC.md § "Conformance scoring".
|
|
7
|
+
"""
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Adapters bind a concrete store to the ``LedgerAdapter`` protocol (Phase 4)."""
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""Adapter binding the bundled reference implementation to the LedgerAdapter protocol.
|
|
2
|
+
|
|
3
|
+
Required to score a perfect 1.0 — the reference store is the existence proof that
|
|
4
|
+
the contract is satisfiable. ``read`` reports the store's actual persisted state.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from verification_ledger.conformance.contract import StoredRecord
|
|
10
|
+
from verification_ledger.invariants import FailurePolicy
|
|
11
|
+
from verification_ledger.ledger import Ledger
|
|
12
|
+
from verification_ledger.model import Channel, Trust
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ReferenceAdapter:
|
|
16
|
+
"""Grades ``verification_ledger.ledger.Ledger`` against the conformance suite."""
|
|
17
|
+
|
|
18
|
+
def __init__(self) -> None:
|
|
19
|
+
# REPORT policy: a VL-4 violation degrades health_ok() rather than raising,
|
|
20
|
+
# so the suite reads a boolean instead of catching an exception.
|
|
21
|
+
self._led = Ledger(":memory:", policy=FailurePolicy.REPORT)
|
|
22
|
+
|
|
23
|
+
def fresh(self) -> None:
|
|
24
|
+
self._led.close()
|
|
25
|
+
self._led = Ledger(":memory:", policy=FailurePolicy.REPORT)
|
|
26
|
+
|
|
27
|
+
def write(
|
|
28
|
+
self, payload: str, *, source_trust: str, channel: str, durable: bool
|
|
29
|
+
) -> int:
|
|
30
|
+
return self._led.write(
|
|
31
|
+
payload,
|
|
32
|
+
source_trust=Trust(source_trust),
|
|
33
|
+
channel=Channel(channel),
|
|
34
|
+
durable=durable,
|
|
35
|
+
).record_id
|
|
36
|
+
|
|
37
|
+
def read(self, record_id: int) -> StoredRecord | None:
|
|
38
|
+
enveloped = self._led.read(record_id)
|
|
39
|
+
if enveloped is None:
|
|
40
|
+
return None
|
|
41
|
+
env = enveloped.envelope
|
|
42
|
+
return StoredRecord(
|
|
43
|
+
record_id=enveloped.record.id,
|
|
44
|
+
source_trust=str(enveloped.record.source_trust),
|
|
45
|
+
actionable=enveloped.record.actionable,
|
|
46
|
+
envelope={
|
|
47
|
+
"kind": env["kind"],
|
|
48
|
+
"source_trust": env["source_trust"],
|
|
49
|
+
"warning": env["warning"],
|
|
50
|
+
},
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
def activate(self, record_id: int, *, channel: str) -> bool:
|
|
54
|
+
self._led.activate(record_id, channel=Channel(channel))
|
|
55
|
+
record = self.read(record_id)
|
|
56
|
+
return record is not None and record.actionable
|
|
57
|
+
|
|
58
|
+
def promote(self, record_id: int, *, channel: str) -> bool:
|
|
59
|
+
self._led.promote(record_id, channel=Channel(channel))
|
|
60
|
+
record = self.read(record_id)
|
|
61
|
+
return record is not None and record.source_trust == str(Trust.OPERATOR)
|
|
62
|
+
|
|
63
|
+
def prune(self, *, keep: int) -> None:
|
|
64
|
+
self._led.prune(keep=keep)
|
|
65
|
+
|
|
66
|
+
def count_records(self) -> int:
|
|
67
|
+
return len(self._led.read_all())
|
|
68
|
+
|
|
69
|
+
def health_ok(self) -> bool:
|
|
70
|
+
return self._led.health().ok
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""The ``LedgerAdapter`` protocol a store implements to be graded. See SPEC.md § "The adapter contract".
|
|
2
|
+
|
|
3
|
+
The suite speaks in plain strings (``"operator"``/``"agent"``/``"ingested"`` and
|
|
4
|
+
``"in_band"``/``"out_of_band"``) so a third-party store need not import this
|
|
5
|
+
package's enums. An adapter is a thin translation from these calls to the store,
|
|
6
|
+
and ``read`` reports the store's actual persisted state so a store that lies to
|
|
7
|
+
itself cannot pass by lying to the suite.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
from typing import Protocol
|
|
14
|
+
|
|
15
|
+
# Trust labels and channels, as the suite passes them. These MUST equal the
|
|
16
|
+
# reference Trust/Channel enum values (a test asserts it).
|
|
17
|
+
OPERATOR = "operator"
|
|
18
|
+
AGENT = "agent"
|
|
19
|
+
INGESTED = "ingested"
|
|
20
|
+
IN_BAND = "in_band"
|
|
21
|
+
OUT_OF_BAND = "out_of_band"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True)
|
|
25
|
+
class StoredRecord:
|
|
26
|
+
"""What a read exposes about one record: its persisted state, as the store sees it."""
|
|
27
|
+
|
|
28
|
+
record_id: int
|
|
29
|
+
source_trust: str
|
|
30
|
+
actionable: bool
|
|
31
|
+
envelope: dict[str, str] | None
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class LedgerAdapter(Protocol):
|
|
35
|
+
"""The surface a store binds to be graded by the conformance suite."""
|
|
36
|
+
|
|
37
|
+
def fresh(self) -> None:
|
|
38
|
+
"""Reset to an empty store; each probe runs on a clean slate."""
|
|
39
|
+
...
|
|
40
|
+
|
|
41
|
+
def write(
|
|
42
|
+
self, payload: str, *, source_trust: str, channel: str, durable: bool
|
|
43
|
+
) -> int:
|
|
44
|
+
"""Store a record and return its id. Must honor the VL-1 in-band operator clamp."""
|
|
45
|
+
...
|
|
46
|
+
|
|
47
|
+
def read(self, record_id: int) -> StoredRecord | None:
|
|
48
|
+
"""Return the record's persisted trust, actionable flag, and VL-3 envelope."""
|
|
49
|
+
...
|
|
50
|
+
|
|
51
|
+
def activate(self, record_id: int, *, channel: str) -> bool:
|
|
52
|
+
"""Attempt the actionable transition; return whether the record is now actionable."""
|
|
53
|
+
...
|
|
54
|
+
|
|
55
|
+
def promote(self, record_id: int, *, channel: str) -> bool:
|
|
56
|
+
"""Attempt to mint operator trust; return whether the record is now operator."""
|
|
57
|
+
...
|
|
58
|
+
|
|
59
|
+
def prune(self, *, keep: int) -> None:
|
|
60
|
+
"""Prune non-durable records to the newest ``keep``; durable records are exempt."""
|
|
61
|
+
...
|
|
62
|
+
|
|
63
|
+
def count_records(self) -> int:
|
|
64
|
+
"""Total records currently stored (for VL-4 cap checks)."""
|
|
65
|
+
...
|
|
66
|
+
|
|
67
|
+
def health_ok(self) -> bool:
|
|
68
|
+
"""Whether the store reports itself healthy (VL-4)."""
|
|
69
|
+
...
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"""Positive and adversarial probes, per invariant. See SPEC.md § "Conformance scoring".
|
|
2
|
+
|
|
3
|
+
Positive probes assert the legitimate path still works; adversarial probes assert
|
|
4
|
+
the attack is blocked. An invariant passes only when both hold — which is what
|
|
5
|
+
makes the score un-gameable: a block-everything store fails the positives, an
|
|
6
|
+
allow-everything store fails the adversarials.
|
|
7
|
+
|
|
8
|
+
Each probe receives a store on a clean slate (the scorer calls ``fresh()`` first)
|
|
9
|
+
and returns True iff the store behaved correctly for that probe.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from collections.abc import Callable
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
|
|
17
|
+
from verification_ledger.conformance.contract import (
|
|
18
|
+
AGENT,
|
|
19
|
+
IN_BAND,
|
|
20
|
+
INGESTED,
|
|
21
|
+
OPERATOR,
|
|
22
|
+
OUT_OF_BAND,
|
|
23
|
+
LedgerAdapter,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
BOUNDARY_KIND = "stored_data_not_instructions"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True)
|
|
30
|
+
class Probe:
|
|
31
|
+
"""One conformance check against a store."""
|
|
32
|
+
|
|
33
|
+
invariant: str # "VL-1" .. "VL-4"
|
|
34
|
+
kind: str # "positive" | "adversarial"
|
|
35
|
+
name: str
|
|
36
|
+
check: Callable[[LedgerAdapter], bool]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# --- VL-1: provenance typing ---
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _vl1_pos_agent_stored(a: LedgerAdapter) -> bool:
|
|
43
|
+
r = a.read(a.write("x", source_trust=AGENT, channel=IN_BAND, durable=False))
|
|
44
|
+
return r is not None and r.source_trust == AGENT
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _vl1_pos_ingested_stored(a: LedgerAdapter) -> bool:
|
|
48
|
+
r = a.read(a.write("x", source_trust=INGESTED, channel=IN_BAND, durable=False))
|
|
49
|
+
return r is not None and r.source_trust == INGESTED
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _vl1_adv_in_band_operator_clamped(a: LedgerAdapter) -> bool:
|
|
53
|
+
# The attack: mint operator trust from the in-band channel. Must be clamped.
|
|
54
|
+
r = a.read(a.write("x", source_trust=OPERATOR, channel=IN_BAND, durable=False))
|
|
55
|
+
return r is not None and r.source_trust != OPERATOR
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# --- VL-2: promotion gate ---
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _vl2_pos_operator_activates(a: LedgerAdapter) -> bool:
|
|
62
|
+
# An operator record (seeded via the out-of-band channel) activates in one call,
|
|
63
|
+
# and the PERSISTED state reflects it — not just the return value.
|
|
64
|
+
rid = a.write(
|
|
65
|
+
"directive", source_trust=OPERATOR, channel=OUT_OF_BAND, durable=False
|
|
66
|
+
)
|
|
67
|
+
activated = a.activate(rid, channel=IN_BAND)
|
|
68
|
+
r = a.read(rid)
|
|
69
|
+
return activated is True and r is not None and r.actionable is True
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _vl2_pos_out_of_band_promotion_makes_operator(a: LedgerAdapter) -> bool:
|
|
73
|
+
rid = a.write("work", source_trust=AGENT, channel=IN_BAND, durable=False)
|
|
74
|
+
a.promote(rid, channel=OUT_OF_BAND)
|
|
75
|
+
r = a.read(rid)
|
|
76
|
+
return r is not None and r.source_trust == OPERATOR
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _vl2_adv_agent_not_activatable_in_band(a: LedgerAdapter) -> bool:
|
|
80
|
+
# Check persisted state, not only the return: a store that reports "refused"
|
|
81
|
+
# while the record is actually actionable is laundering.
|
|
82
|
+
rid = a.write("work", source_trust=AGENT, channel=IN_BAND, durable=False)
|
|
83
|
+
activated = a.activate(rid, channel=IN_BAND)
|
|
84
|
+
r = a.read(rid)
|
|
85
|
+
return activated is False and r is not None and r.actionable is False
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _vl2_adv_record_not_born_actionable(a: LedgerAdapter) -> bool:
|
|
89
|
+
# A non-operator record must not be actionable before any activation call — a
|
|
90
|
+
# store that pre-activates records launders untrusted content while activate()
|
|
91
|
+
# still politely reports "refused".
|
|
92
|
+
rid = a.write("work", source_trust=AGENT, channel=IN_BAND, durable=False)
|
|
93
|
+
r = a.read(rid)
|
|
94
|
+
return r is not None and r.actionable is False
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _vl2_adv_agent_not_promotable_in_band(a: LedgerAdapter) -> bool:
|
|
98
|
+
rid = a.write("work", source_trust=AGENT, channel=IN_BAND, durable=False)
|
|
99
|
+
a.promote(rid, channel=IN_BAND)
|
|
100
|
+
r = a.read(rid)
|
|
101
|
+
return r is not None and r.source_trust != OPERATOR
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _vl2_adv_ingested_not_activatable_in_band(a: LedgerAdapter) -> bool:
|
|
105
|
+
rid = a.write("scraped", source_trust=INGESTED, channel=IN_BAND, durable=False)
|
|
106
|
+
activated = a.activate(rid, channel=IN_BAND)
|
|
107
|
+
r = a.read(rid)
|
|
108
|
+
return activated is False and r is not None and r.actionable is False
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
# --- VL-3: instruction-boundary envelope ---
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _vl3_pos_envelope_present(a: LedgerAdapter) -> bool:
|
|
115
|
+
r = a.read(a.write("x", source_trust=AGENT, channel=IN_BAND, durable=False))
|
|
116
|
+
return (
|
|
117
|
+
r is not None
|
|
118
|
+
and r.envelope is not None
|
|
119
|
+
and r.envelope.get("kind") == BOUNDARY_KIND
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _vl3_adv_envelope_reflects_real_trust(a: LedgerAdapter) -> bool:
|
|
124
|
+
# The envelope must carry the record's real (most-suspicious) trust, not a
|
|
125
|
+
# laundered, safer-looking label.
|
|
126
|
+
r = a.read(
|
|
127
|
+
a.write("scraped", source_trust=INGESTED, channel=IN_BAND, durable=False)
|
|
128
|
+
)
|
|
129
|
+
return (
|
|
130
|
+
r is not None
|
|
131
|
+
and r.envelope is not None
|
|
132
|
+
and r.envelope.get("source_trust") == INGESTED
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
# --- VL-4: retention invariant ---
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _vl4_pos_prunes_non_durable_to_cap(a: LedgerAdapter) -> bool:
|
|
140
|
+
a.write("keep", source_trust=AGENT, channel=IN_BAND, durable=True)
|
|
141
|
+
for i in range(5):
|
|
142
|
+
a.write(f"n{i}", source_trust=AGENT, channel=IN_BAND, durable=False)
|
|
143
|
+
a.prune(keep=2)
|
|
144
|
+
# 1 durable + newest 2 non-durable = 3; and the store still reports healthy.
|
|
145
|
+
return a.count_records() == 3 and a.health_ok() is True
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _vl4_adv_durable_survives_aggressive_prune(a: LedgerAdapter) -> bool:
|
|
149
|
+
rid = a.write("shipped", source_trust=AGENT, channel=IN_BAND, durable=True)
|
|
150
|
+
a.prune(keep=0) # ask to prune everything
|
|
151
|
+
return a.read(rid) is not None # the durable record must remain
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def all_probes() -> list[Probe]:
|
|
155
|
+
"""Every probe, tagged by invariant and kind."""
|
|
156
|
+
return [
|
|
157
|
+
Probe("VL-1", "positive", "agent stored as agent", _vl1_pos_agent_stored),
|
|
158
|
+
Probe(
|
|
159
|
+
"VL-1", "positive", "ingested stored as ingested", _vl1_pos_ingested_stored
|
|
160
|
+
),
|
|
161
|
+
Probe(
|
|
162
|
+
"VL-1",
|
|
163
|
+
"adversarial",
|
|
164
|
+
"in-band operator clamped",
|
|
165
|
+
_vl1_adv_in_band_operator_clamped,
|
|
166
|
+
),
|
|
167
|
+
Probe(
|
|
168
|
+
"VL-2",
|
|
169
|
+
"positive",
|
|
170
|
+
"operator activates in one call",
|
|
171
|
+
_vl2_pos_operator_activates,
|
|
172
|
+
),
|
|
173
|
+
Probe(
|
|
174
|
+
"VL-2",
|
|
175
|
+
"positive",
|
|
176
|
+
"out-of-band promotion mints operator",
|
|
177
|
+
_vl2_pos_out_of_band_promotion_makes_operator,
|
|
178
|
+
),
|
|
179
|
+
Probe(
|
|
180
|
+
"VL-2",
|
|
181
|
+
"adversarial",
|
|
182
|
+
"agent not activatable in-band",
|
|
183
|
+
_vl2_adv_agent_not_activatable_in_band,
|
|
184
|
+
),
|
|
185
|
+
Probe(
|
|
186
|
+
"VL-2",
|
|
187
|
+
"adversarial",
|
|
188
|
+
"record not born actionable",
|
|
189
|
+
_vl2_adv_record_not_born_actionable,
|
|
190
|
+
),
|
|
191
|
+
Probe(
|
|
192
|
+
"VL-2",
|
|
193
|
+
"adversarial",
|
|
194
|
+
"agent not promotable in-band",
|
|
195
|
+
_vl2_adv_agent_not_promotable_in_band,
|
|
196
|
+
),
|
|
197
|
+
Probe(
|
|
198
|
+
"VL-2",
|
|
199
|
+
"adversarial",
|
|
200
|
+
"ingested not activatable in-band",
|
|
201
|
+
_vl2_adv_ingested_not_activatable_in_band,
|
|
202
|
+
),
|
|
203
|
+
Probe(
|
|
204
|
+
"VL-3", "positive", "read carries the envelope", _vl3_pos_envelope_present
|
|
205
|
+
),
|
|
206
|
+
Probe(
|
|
207
|
+
"VL-3",
|
|
208
|
+
"adversarial",
|
|
209
|
+
"envelope reflects real trust",
|
|
210
|
+
_vl3_adv_envelope_reflects_real_trust,
|
|
211
|
+
),
|
|
212
|
+
Probe(
|
|
213
|
+
"VL-4",
|
|
214
|
+
"positive",
|
|
215
|
+
"non-durable pruned to cap",
|
|
216
|
+
_vl4_pos_prunes_non_durable_to_cap,
|
|
217
|
+
),
|
|
218
|
+
Probe(
|
|
219
|
+
"VL-4",
|
|
220
|
+
"adversarial",
|
|
221
|
+
"durable survives aggressive prune",
|
|
222
|
+
_vl4_adv_durable_survives_aggressive_prune,
|
|
223
|
+
),
|
|
224
|
+
]
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""Conformance CLI: grade an adapter and print the report.
|
|
2
|
+
|
|
3
|
+
python -m verification_ledger.conformance [reference | module:callable] [--json]
|
|
4
|
+
|
|
5
|
+
Default adapter is ``reference`` (the bundled store, which must score 1.0). Any
|
|
6
|
+
other store is graded by passing ``module:callable`` where ``callable()`` returns
|
|
7
|
+
a LedgerAdapter. Exit code is 0 only on a perfect score.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import argparse
|
|
13
|
+
import importlib
|
|
14
|
+
import json
|
|
15
|
+
|
|
16
|
+
from verification_ledger.conformance.contract import LedgerAdapter
|
|
17
|
+
from verification_ledger.conformance.score import ConformanceReport, score_adapter
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def load_adapter(spec: str) -> LedgerAdapter:
|
|
21
|
+
if spec == "reference":
|
|
22
|
+
from verification_ledger.conformance.adapters.reference import ReferenceAdapter
|
|
23
|
+
|
|
24
|
+
return ReferenceAdapter()
|
|
25
|
+
if ":" not in spec:
|
|
26
|
+
raise SystemExit(
|
|
27
|
+
f"unknown adapter {spec!r}; use 'reference' or 'module:callable'"
|
|
28
|
+
)
|
|
29
|
+
module_name, _, attr = spec.partition(":")
|
|
30
|
+
module = importlib.import_module(module_name)
|
|
31
|
+
factory = getattr(module, attr)
|
|
32
|
+
return factory()
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def report_to_dict(report: ConformanceReport) -> dict[str, object]:
|
|
36
|
+
return {
|
|
37
|
+
"score": report.score,
|
|
38
|
+
"passed": report.passed_count,
|
|
39
|
+
"total": report.total_invariants,
|
|
40
|
+
"invariants": [
|
|
41
|
+
{
|
|
42
|
+
"invariant": s.invariant,
|
|
43
|
+
"passed": s.passed,
|
|
44
|
+
"positive": f"{s.positive_passed}/{s.positive_total}",
|
|
45
|
+
"adversarial": f"{s.adversarial_passed}/{s.adversarial_total}",
|
|
46
|
+
}
|
|
47
|
+
for s in report.invariants
|
|
48
|
+
],
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def print_report(report: ConformanceReport) -> None:
|
|
53
|
+
print("Ledger Conformance")
|
|
54
|
+
print("=" * 46)
|
|
55
|
+
for s in report.invariants:
|
|
56
|
+
mark = "PASS" if s.passed else "FAIL"
|
|
57
|
+
print(
|
|
58
|
+
f" {s.invariant} {mark} "
|
|
59
|
+
f"positive {s.positive_passed}/{s.positive_total} "
|
|
60
|
+
f"adversarial {s.adversarial_passed}/{s.adversarial_total}"
|
|
61
|
+
)
|
|
62
|
+
print("-" * 46)
|
|
63
|
+
print(
|
|
64
|
+
f" Score {report.score:.2f} "
|
|
65
|
+
f"({report.passed_count}/{report.total_invariants} invariants)"
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def main(argv: list[str] | None = None) -> int:
|
|
70
|
+
parser = argparse.ArgumentParser(
|
|
71
|
+
prog="verification-ledger conformance",
|
|
72
|
+
description="Grade a ledger store against the four-invariant contract.",
|
|
73
|
+
)
|
|
74
|
+
parser.add_argument(
|
|
75
|
+
"adapter",
|
|
76
|
+
nargs="?",
|
|
77
|
+
default="reference",
|
|
78
|
+
help="'reference' (default) or 'module:callable' returning a LedgerAdapter",
|
|
79
|
+
)
|
|
80
|
+
parser.add_argument("--json", action="store_true", help="emit the report as JSON")
|
|
81
|
+
args = parser.parse_args(argv)
|
|
82
|
+
report = score_adapter(load_adapter(str(args.adapter)))
|
|
83
|
+
if bool(args.json):
|
|
84
|
+
print(json.dumps(report_to_dict(report), indent=2))
|
|
85
|
+
else:
|
|
86
|
+
print_report(report)
|
|
87
|
+
return 0 if report.perfect else 1
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
if __name__ == "__main__":
|
|
91
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""Scoring: per-invariant pass plus the top-line Ledger Conformance Score.
|
|
2
|
+
|
|
3
|
+
An invariant passes only if its positive-probe pass rate and its adversarial-probe
|
|
4
|
+
block rate are both 1.0 — no partial credit within an invariant. The top-line
|
|
5
|
+
score is the fraction of the four invariants fully passed. See SPEC.md §
|
|
6
|
+
"Conformance scoring".
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
|
|
13
|
+
from verification_ledger.conformance.contract import LedgerAdapter
|
|
14
|
+
from verification_ledger.conformance.probes import Probe, all_probes
|
|
15
|
+
|
|
16
|
+
INVARIANTS = ("VL-1", "VL-2", "VL-3", "VL-4")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class InvariantScore:
|
|
21
|
+
"""One invariant's result across its positive and adversarial probes."""
|
|
22
|
+
|
|
23
|
+
invariant: str
|
|
24
|
+
positive_passed: int
|
|
25
|
+
positive_total: int
|
|
26
|
+
adversarial_passed: int
|
|
27
|
+
adversarial_total: int
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def positive_rate(self) -> float:
|
|
31
|
+
return (
|
|
32
|
+
self.positive_passed / self.positive_total if self.positive_total else 1.0
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def adversarial_rate(self) -> float:
|
|
37
|
+
return (
|
|
38
|
+
self.adversarial_passed / self.adversarial_total
|
|
39
|
+
if self.adversarial_total
|
|
40
|
+
else 1.0
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def passed(self) -> bool:
|
|
45
|
+
return self.positive_rate == 1.0 and self.adversarial_rate == 1.0
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
@dataclass(frozen=True)
|
|
49
|
+
class ConformanceReport:
|
|
50
|
+
"""The full result: per-invariant scores and the top-line conformance score."""
|
|
51
|
+
|
|
52
|
+
invariants: tuple[InvariantScore, ...]
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def passed_count(self) -> int:
|
|
56
|
+
return sum(1 for s in self.invariants if s.passed)
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def total_invariants(self) -> int:
|
|
60
|
+
return len(self.invariants)
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def score(self) -> float:
|
|
64
|
+
return self.passed_count / self.total_invariants if self.invariants else 0.0
|
|
65
|
+
|
|
66
|
+
@property
|
|
67
|
+
def perfect(self) -> bool:
|
|
68
|
+
return self.passed_count == self.total_invariants
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _run_probe(adapter: LedgerAdapter, probe: Probe) -> bool:
|
|
72
|
+
# Each probe runs on a clean slate; a probe that raises counts as a failure,
|
|
73
|
+
# never as a pass — a store that errors has not enforced the invariant.
|
|
74
|
+
adapter.fresh()
|
|
75
|
+
try:
|
|
76
|
+
return probe.check(adapter) is True
|
|
77
|
+
except Exception:
|
|
78
|
+
return False
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def score_adapter(
|
|
82
|
+
adapter: LedgerAdapter, probes: list[Probe] | None = None
|
|
83
|
+
) -> ConformanceReport:
|
|
84
|
+
"""Grade ``adapter`` against every probe and return the conformance report."""
|
|
85
|
+
probe_list = all_probes() if probes is None else probes
|
|
86
|
+
scores: list[InvariantScore] = []
|
|
87
|
+
for invariant in INVARIANTS:
|
|
88
|
+
pos = [
|
|
89
|
+
p for p in probe_list if p.invariant == invariant and p.kind == "positive"
|
|
90
|
+
]
|
|
91
|
+
adv = [
|
|
92
|
+
p
|
|
93
|
+
for p in probe_list
|
|
94
|
+
if p.invariant == invariant and p.kind == "adversarial"
|
|
95
|
+
]
|
|
96
|
+
scores.append(
|
|
97
|
+
InvariantScore(
|
|
98
|
+
invariant=invariant,
|
|
99
|
+
positive_passed=sum(1 for p in pos if _run_probe(adapter, p)),
|
|
100
|
+
positive_total=len(pos),
|
|
101
|
+
adversarial_passed=sum(1 for p in adv if _run_probe(adapter, p)),
|
|
102
|
+
adversarial_total=len(adv),
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
return ConformanceReport(invariants=tuple(scores))
|