delval-gate 0.1.0__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Tao Shi (Zhurong)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,173 @@
1
+ Metadata-Version: 2.4
2
+ Name: delval-gate
3
+ Version: 0.1.0
4
+ Summary: Fail-closed Delivery Permission & Judgment Ledger action gate for long-running LLM agents (Python API, CLI, and zero-dependency MCP stdio server).
5
+ Author: Tao Shi (Zhurong)
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/iannil/delval-gate
8
+ Project-URL: Repository, https://github.com/iannil/delval-gate
9
+ Project-URL: Protocol Paper, https://zhurongshuo.com/en/papers/delval-protocol/
10
+ Project-URL: Experiment Paper, https://zhurongshuo.com/en/papers/delval-experiment/
11
+ Project-URL: Zenodo Formal Proofs & Dataset, https://doi.org/10.5281/zenodo.24455837
12
+ Keywords: llm,agents,mcp,verification,judgment-ledger,delivery-gate,delval
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Intended Audience :: Science/Research
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Topic :: Software Development :: Quality Assurance
18
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
19
+ Requires-Python: >=3.10
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Dynamic: license-file
23
+
24
+ # delval-gate
25
+
26
+ [![CI](https://github.com/iannil/delval-gate/actions/workflows/ci.yml/badge.svg)](https://github.com/iannil/delval-gate/actions/workflows/ci.yml)
27
+ [![PyPI](https://img.shields.io/pypi/v/delval-gate.svg)](https://pypi.org/project/delval-gate/)
28
+ [![DOI](https://img.shields.io/badge/Zenodo%20DOI-10.5281%2Fzenodo.24455837-blue)](https://doi.org/10.5281/zenodo.24455837)
29
+ [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
30
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
31
+ [![Zero Dependencies](https://img.shields.io/badge/dependencies-0%20(stdlib%20only)-brightgreen.svg)](pyproject.toml)
32
+
33
+ **Fail-Closed Delivery Permission & Judgment Ledger for Long-Running AI Agents**
34
+ *(Zero-dependency Python 3 library, CLI, and Model Context Protocol (MCP) stdio server)*
35
+
36
+ When AI coding and ops agents work across sessions—modifying configs, migrating schemas, publishing releases—stale judgments lingering in ordinary text or vector memory silently trigger unsafe deliveries. Even retraction-based memory layers degrade stale records at read time without blocking delivery when an agent cites them anyway.
37
+
38
+ `delval-gate` implements the **DelVal** five-state, ten-rule delivery-permission protocol—machine-checked in **Isabelle/HOL 2026** (session `RC_DelVal`, zero `sorry`) and evaluated across **728 live LLM agent trials** (`0/293` unsafe published deliveries vs. `68/150` in a version-bound retraction control without a delivery gate, McNemar $p = 6.776 \times 10^{-21}$).
39
+
40
+ ---
41
+
42
+ ## Core Guarantees (Formally Verified Invariants)
43
+
44
+ | Invariant | What `delval-gate` Enforces |
45
+ |---|---|
46
+ | **I-1a · Recovery Gate** | Once a judgment turns `stale`, **only** an explicit passing `recheck` restores it to `valid`. Duplicate `register` echoes are logged and ignored—never reviving stale claims. |
47
+ | **I-1b · Closure Coherence** | Object/file changes propagate `stale` along the full transitive dependency closure. A derived judgment cannot return to `valid` while any upstream dependency remains non-valid. |
48
+ | **I-2 · Objection Blocking** | Open objections transition a judgment to `blocked` and fail-close the delivery gate until closed with explicit `audit_evidence` followed by a fresh passing `recheck`. |
49
+ | **I-3 · Pure-Citation Invariance** | Repeated citations of the same evidence root never inflate independent root support (`roots` is a set of distinct non-empty digests) and never flip gate decisions. |
50
+ | **I-4 · Frozen Reuse Contract** | Preregistered harmless-change rules (SHA-256 hashed at initialization) preserve existing validity without false invalidation, and raise `ContractTamperedError` if modified mid-run. |
51
+ | **5-State Retirement Lifecycle** | Full five-state machine (`valid`, `stale`, `blocked`, `withdrawn`, `superseded`). Retired claims (`withdraw` / `supersede`) stay permanently blocked and cannot be revived by `recheck`. |
52
+
53
+ ---
54
+
55
+ ## Installation
56
+
57
+ Zero third-party dependencies (Python 3.10+ stdlib only):
58
+
59
+ ```bash
60
+ pip install delval-gate
61
+ ```
62
+
63
+ Or install from source:
64
+
65
+ ```bash
66
+ git clone https://github.com/iannil/delval-gate.git
67
+ cd delval-gate
68
+ pip install -e .
69
+ ```
70
+
71
+ Or run directly from source without installing:
72
+ ```bash
73
+ PYTHONPATH=src python3 -m delval_gate.cli --help
74
+ ```
75
+
76
+ ---
77
+
78
+ ## Quickstart (CLI & CI Gate)
79
+
80
+ ```bash
81
+ # 1. Initialize a ledger with a frozen JSON reuse contract
82
+ delval-gate --ledger .delval.json init --contract examples/reuse-contract.json
83
+
84
+ # 2. Register an upstream evidence-bound judgment
85
+ delval-gate --ledger .delval.json register \
86
+ --jid "build:api-schema" \
87
+ --evidence "src/schema.py" "pyproject.toml" \
88
+ --digest "sha256:abc123" --passed
89
+
90
+ # 3. Register a downstream derived judgment depending on build:api-schema
91
+ delval-gate --ledger .delval.json register \
92
+ --jid "release:v2-ready" \
93
+ --evidence "docs/api.md" \
94
+ --deps "build:api-schema" \
95
+ --digest "sha256:def456" --passed
96
+
97
+ # 4. Structural change -> automatic transitive invalidation
98
+ delval-gate --ledger .delval.json change "src/schema.py"
99
+
100
+ # 5. Delivery gate FAILS CLOSED (exit code 2 + exact blocking reasons)
101
+ delval-gate --ledger .delval.json gate "release:v2-ready"
102
+
103
+ # 6. Trusted recheck in dependency order -> gate ALLOWS (exit code 0)
104
+ delval-gate --ledger .delval.json recheck --jid "build:api-schema" --passed --digest "sha256:new1"
105
+ delval-gate --ledger .delval.json recheck --jid "release:v2-ready" --passed --digest "sha256:new2"
106
+ delval-gate --ledger .delval.json gate "release:v2-ready"
107
+ ```
108
+
109
+ Run the full interactive demo script:
110
+ ```bash
111
+ bash examples/demo.sh
112
+ ```
113
+
114
+ ---
115
+
116
+ ## MCP Server Mode (Claude Code / Cursor / Any MCP Agent)
117
+
118
+ `delval-gate` includes a zero-dependency JSON-RPC 2.0 **Model Context Protocol (MCP)** stdio server (`delval-gate --ledger .delval.json mcp`). Add it to your agent's MCP configuration:
119
+
120
+ ```json
121
+ {
122
+ "mcpServers": {
123
+ "delval-gate": {
124
+ "command": "delval-gate",
125
+ "args": ["--ledger", ".delval.json", "mcp"]
126
+ }
127
+ }
128
+ }
129
+ ```
130
+
131
+ ### Exposed MCP Tools
132
+ - `delval_register` — register an evidence-bound judgment (echo-immune)
133
+ - `delval_notify_change` — notify changed paths (`path` or `path@change_class`) and propagate staleness
134
+ - `delval_recheck` — record a trusted verifier recheck outcome (sole recovery gate)
135
+ - `delval_raise_objection` / `delval_resolve_objection` — open or evidence-backed close an objection
136
+ - `delval_withdraw` / `delval_supersede` — permanently retire or replace a judgment
137
+ - `delval_gate` — fail-closed delivery-permission check over cited judgments and their transitive closure
138
+ - `delval_status` — inspect statuses, distinct root counts, open objections, and audit trail
139
+
140
+ ---
141
+
142
+ ## Python API
143
+
144
+ ```python
145
+ from delval_gate import JudgmentLedger
146
+
147
+ ledger = JudgmentLedger()
148
+ ledger.register("build:api", ["src/api.py"], passed=True, digest="sha256:111")
149
+ ledger.register("release:v1", ["docs/api.md"], deps=["build:api"], passed=True, digest="sha256:222")
150
+
151
+ ledger.notify_change(["src/api.py"])
152
+ decision = ledger.gate(["release:v1"])
153
+ assert not decision.allow
154
+ print(decision.blocked_reasons)
155
+ # ('claim-not-valid:build:api:stale', 'claim-not-valid:release:v1:stale')
156
+ ```
157
+
158
+ ---
159
+
160
+ ## Running Tests
161
+
162
+ ```bash
163
+ python3 -m unittest discover -s tests -v
164
+ ```
165
+
166
+ ---
167
+
168
+ ## Academic Papers & Formal Verification Archive
169
+
170
+ - **Protocol Semantics & Isabelle/HOL–Alloy Formalization:** Tao Shi (Zhurong), *Delivery Permission: Protocol Semantics for Judgment Ledger, Action Gate, and Bounded Separation*, https://zhurongshuo.com/en/papers/delval-protocol/
171
+ - **Preregistered 150-Pair & 728-Trial Empirical Evaluation:** Tao Shi (Zhurong), *Does an Action Gate Reduce Unsafe Delivery? A 150-Pair Preregistered Experiment*, https://zhurongshuo.com/en/papers/delval-experiment/
172
+ - **Isabelle/HOL 2026 Proofs, Alloy Models & 728-Trial Dataset (Zenodo DOI):** [10.5281/zenodo.24455837](https://doi.org/10.5281/zenodo.24455837)
173
+ - **Author ORCID:** [0009-0002-6890-085X](https://orcid.org/0009-0002-6890-085X)
@@ -0,0 +1,150 @@
1
+ # delval-gate
2
+
3
+ [![CI](https://github.com/iannil/delval-gate/actions/workflows/ci.yml/badge.svg)](https://github.com/iannil/delval-gate/actions/workflows/ci.yml)
4
+ [![PyPI](https://img.shields.io/pypi/v/delval-gate.svg)](https://pypi.org/project/delval-gate/)
5
+ [![DOI](https://img.shields.io/badge/Zenodo%20DOI-10.5281%2Fzenodo.24455837-blue)](https://doi.org/10.5281/zenodo.24455837)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
7
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
8
+ [![Zero Dependencies](https://img.shields.io/badge/dependencies-0%20(stdlib%20only)-brightgreen.svg)](pyproject.toml)
9
+
10
+ **Fail-Closed Delivery Permission & Judgment Ledger for Long-Running AI Agents**
11
+ *(Zero-dependency Python 3 library, CLI, and Model Context Protocol (MCP) stdio server)*
12
+
13
+ When AI coding and ops agents work across sessions—modifying configs, migrating schemas, publishing releases—stale judgments lingering in ordinary text or vector memory silently trigger unsafe deliveries. Even retraction-based memory layers degrade stale records at read time without blocking delivery when an agent cites them anyway.
14
+
15
+ `delval-gate` implements the **DelVal** five-state, ten-rule delivery-permission protocol—machine-checked in **Isabelle/HOL 2026** (session `RC_DelVal`, zero `sorry`) and evaluated across **728 live LLM agent trials** (`0/293` unsafe published deliveries vs. `68/150` in a version-bound retraction control without a delivery gate, McNemar $p = 6.776 \times 10^{-21}$).
16
+
17
+ ---
18
+
19
+ ## Core Guarantees (Formally Verified Invariants)
20
+
21
+ | Invariant | What `delval-gate` Enforces |
22
+ |---|---|
23
+ | **I-1a · Recovery Gate** | Once a judgment turns `stale`, **only** an explicit passing `recheck` restores it to `valid`. Duplicate `register` echoes are logged and ignored—never reviving stale claims. |
24
+ | **I-1b · Closure Coherence** | Object/file changes propagate `stale` along the full transitive dependency closure. A derived judgment cannot return to `valid` while any upstream dependency remains non-valid. |
25
+ | **I-2 · Objection Blocking** | Open objections transition a judgment to `blocked` and fail-close the delivery gate until closed with explicit `audit_evidence` followed by a fresh passing `recheck`. |
26
+ | **I-3 · Pure-Citation Invariance** | Repeated citations of the same evidence root never inflate independent root support (`roots` is a set of distinct non-empty digests) and never flip gate decisions. |
27
+ | **I-4 · Frozen Reuse Contract** | Preregistered harmless-change rules (SHA-256 hashed at initialization) preserve existing validity without false invalidation, and raise `ContractTamperedError` if modified mid-run. |
28
+ | **5-State Retirement Lifecycle** | Full five-state machine (`valid`, `stale`, `blocked`, `withdrawn`, `superseded`). Retired claims (`withdraw` / `supersede`) stay permanently blocked and cannot be revived by `recheck`. |
29
+
30
+ ---
31
+
32
+ ## Installation
33
+
34
+ Zero third-party dependencies (Python 3.10+ stdlib only):
35
+
36
+ ```bash
37
+ pip install delval-gate
38
+ ```
39
+
40
+ Or install from source:
41
+
42
+ ```bash
43
+ git clone https://github.com/iannil/delval-gate.git
44
+ cd delval-gate
45
+ pip install -e .
46
+ ```
47
+
48
+ Or run directly from source without installing:
49
+ ```bash
50
+ PYTHONPATH=src python3 -m delval_gate.cli --help
51
+ ```
52
+
53
+ ---
54
+
55
+ ## Quickstart (CLI & CI Gate)
56
+
57
+ ```bash
58
+ # 1. Initialize a ledger with a frozen JSON reuse contract
59
+ delval-gate --ledger .delval.json init --contract examples/reuse-contract.json
60
+
61
+ # 2. Register an upstream evidence-bound judgment
62
+ delval-gate --ledger .delval.json register \
63
+ --jid "build:api-schema" \
64
+ --evidence "src/schema.py" "pyproject.toml" \
65
+ --digest "sha256:abc123" --passed
66
+
67
+ # 3. Register a downstream derived judgment depending on build:api-schema
68
+ delval-gate --ledger .delval.json register \
69
+ --jid "release:v2-ready" \
70
+ --evidence "docs/api.md" \
71
+ --deps "build:api-schema" \
72
+ --digest "sha256:def456" --passed
73
+
74
+ # 4. Structural change -> automatic transitive invalidation
75
+ delval-gate --ledger .delval.json change "src/schema.py"
76
+
77
+ # 5. Delivery gate FAILS CLOSED (exit code 2 + exact blocking reasons)
78
+ delval-gate --ledger .delval.json gate "release:v2-ready"
79
+
80
+ # 6. Trusted recheck in dependency order -> gate ALLOWS (exit code 0)
81
+ delval-gate --ledger .delval.json recheck --jid "build:api-schema" --passed --digest "sha256:new1"
82
+ delval-gate --ledger .delval.json recheck --jid "release:v2-ready" --passed --digest "sha256:new2"
83
+ delval-gate --ledger .delval.json gate "release:v2-ready"
84
+ ```
85
+
86
+ Run the full interactive demo script:
87
+ ```bash
88
+ bash examples/demo.sh
89
+ ```
90
+
91
+ ---
92
+
93
+ ## MCP Server Mode (Claude Code / Cursor / Any MCP Agent)
94
+
95
+ `delval-gate` includes a zero-dependency JSON-RPC 2.0 **Model Context Protocol (MCP)** stdio server (`delval-gate --ledger .delval.json mcp`). Add it to your agent's MCP configuration:
96
+
97
+ ```json
98
+ {
99
+ "mcpServers": {
100
+ "delval-gate": {
101
+ "command": "delval-gate",
102
+ "args": ["--ledger", ".delval.json", "mcp"]
103
+ }
104
+ }
105
+ }
106
+ ```
107
+
108
+ ### Exposed MCP Tools
109
+ - `delval_register` — register an evidence-bound judgment (echo-immune)
110
+ - `delval_notify_change` — notify changed paths (`path` or `path@change_class`) and propagate staleness
111
+ - `delval_recheck` — record a trusted verifier recheck outcome (sole recovery gate)
112
+ - `delval_raise_objection` / `delval_resolve_objection` — open or evidence-backed close an objection
113
+ - `delval_withdraw` / `delval_supersede` — permanently retire or replace a judgment
114
+ - `delval_gate` — fail-closed delivery-permission check over cited judgments and their transitive closure
115
+ - `delval_status` — inspect statuses, distinct root counts, open objections, and audit trail
116
+
117
+ ---
118
+
119
+ ## Python API
120
+
121
+ ```python
122
+ from delval_gate import JudgmentLedger
123
+
124
+ ledger = JudgmentLedger()
125
+ ledger.register("build:api", ["src/api.py"], passed=True, digest="sha256:111")
126
+ ledger.register("release:v1", ["docs/api.md"], deps=["build:api"], passed=True, digest="sha256:222")
127
+
128
+ ledger.notify_change(["src/api.py"])
129
+ decision = ledger.gate(["release:v1"])
130
+ assert not decision.allow
131
+ print(decision.blocked_reasons)
132
+ # ('claim-not-valid:build:api:stale', 'claim-not-valid:release:v1:stale')
133
+ ```
134
+
135
+ ---
136
+
137
+ ## Running Tests
138
+
139
+ ```bash
140
+ python3 -m unittest discover -s tests -v
141
+ ```
142
+
143
+ ---
144
+
145
+ ## Academic Papers & Formal Verification Archive
146
+
147
+ - **Protocol Semantics & Isabelle/HOL–Alloy Formalization:** Tao Shi (Zhurong), *Delivery Permission: Protocol Semantics for Judgment Ledger, Action Gate, and Bounded Separation*, https://zhurongshuo.com/en/papers/delval-protocol/
148
+ - **Preregistered 150-Pair & 728-Trial Empirical Evaluation:** Tao Shi (Zhurong), *Does an Action Gate Reduce Unsafe Delivery? A 150-Pair Preregistered Experiment*, https://zhurongshuo.com/en/papers/delval-experiment/
149
+ - **Isabelle/HOL 2026 Proofs, Alloy Models & 728-Trial Dataset (Zenodo DOI):** [10.5281/zenodo.24455837](https://doi.org/10.5281/zenodo.24455837)
150
+ - **Author ORCID:** [0009-0002-6890-085X](https://orcid.org/0009-0002-6890-085X)
@@ -0,0 +1,37 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "delval-gate"
7
+ version = "0.1.0"
8
+ description = "Fail-closed Delivery Permission & Judgment Ledger action gate for long-running LLM agents (Python API, CLI, and zero-dependency MCP stdio server)."
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ authors = [
14
+ {name = "Tao Shi (Zhurong)"}
15
+ ]
16
+ keywords = ["llm", "agents", "mcp", "verification", "judgment-ledger", "delivery-gate", "delval"]
17
+ classifiers = [
18
+ "Development Status :: 4 - Beta",
19
+ "Intended Audience :: Developers",
20
+ "Intended Audience :: Science/Research",
21
+ "Programming Language :: Python :: 3",
22
+ "Topic :: Software Development :: Quality Assurance",
23
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
24
+ ]
25
+
26
+ [project.urls]
27
+ Homepage = "https://github.com/iannil/delval-gate"
28
+ Repository = "https://github.com/iannil/delval-gate"
29
+ "Protocol Paper" = "https://zhurongshuo.com/en/papers/delval-protocol/"
30
+ "Experiment Paper" = "https://zhurongshuo.com/en/papers/delval-experiment/"
31
+ "Zenodo Formal Proofs & Dataset" = "https://doi.org/10.5281/zenodo.24455837"
32
+
33
+ [project.scripts]
34
+ delval-gate = "delval_gate.cli:main"
35
+
36
+ [tool.setuptools.packages.find]
37
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,23 @@
1
+ """delval-gate: Fail-closed delivery-permission ledger & action gate."""
2
+
3
+ from .ledger import (
4
+ ContractRule,
5
+ ContractTamperedError,
6
+ GateDecision,
7
+ Judgment,
8
+ JudgmentLedger,
9
+ JudgmentStatus,
10
+ ReuseContract,
11
+ )
12
+
13
+ __all__ = [
14
+ "ContractRule",
15
+ "ContractTamperedError",
16
+ "GateDecision",
17
+ "Judgment",
18
+ "JudgmentLedger",
19
+ "JudgmentStatus",
20
+ "ReuseContract",
21
+ ]
22
+
23
+ __version__ = "0.1.0"
@@ -0,0 +1,159 @@
1
+ """Command-line interface for delval-gate."""
2
+ from __future__ import annotations
3
+
4
+ from pathlib import Path
5
+ import argparse
6
+ import json
7
+ import sys
8
+
9
+ from .ledger import JudgmentLedger, ReuseContract
10
+ from .mcp_server import DelValMCPServer
11
+
12
+
13
+ def build_parser() -> argparse.ArgumentParser:
14
+ ap = argparse.ArgumentParser(prog="delval-gate", description="DelVal fail-closed delivery permission gate")
15
+ ap.add_argument("--ledger", default=".delval.json", help="Path to persistent JSON ledger file")
16
+ sub = ap.add_subparsers(dest="cmd", required=True)
17
+
18
+ p_init = sub.add_parser("init", help="Initialize a new ledger (optionally binding a frozen JSON reuse contract)")
19
+ p_init.add_argument("--contract", help="Path to JSON reuse contract file")
20
+
21
+ p_reg = sub.add_parser("register", help="Register an evidence-bound judgment")
22
+ p_reg.add_argument("--jid", required=True)
23
+ p_reg.add_argument("--evidence", nargs="+", required=True)
24
+ p_reg.add_argument("--deps", nargs="*", default=[])
25
+ p_reg.add_argument("--digest", default="")
26
+ p_reg.add_argument("--passed", action="store_true", default=False)
27
+ p_reg.add_argument("--failed", action="store_true", default=False)
28
+
29
+ p_chg = sub.add_parser("change", help="Notify changed file/object paths (path or path@class)")
30
+ p_chg.add_argument("paths", nargs="+")
31
+
32
+ p_rec = sub.add_parser("recheck", help="Record a trusted verifier recheck outcome")
33
+ p_rec.add_argument("--jid", required=True)
34
+ p_rec.add_argument("--passed", action="store_true", default=False)
35
+ p_rec.add_argument("--failed", action="store_true", default=False)
36
+ p_rec.add_argument("--digest", default="")
37
+ p_rec.add_argument("--detail", default="")
38
+
39
+ p_obj = sub.add_parser("object", help="Raise an open objection against a judgment")
40
+ p_obj.add_argument("--jid", required=True)
41
+ p_obj.add_argument("--note", required=True)
42
+ p_obj.add_argument("--id", dest="objection_id")
43
+
44
+ p_res = sub.add_parser("resolve", help="Resolve an open objection with audit evidence")
45
+ p_res.add_argument("--jid", required=True)
46
+ p_res.add_argument("--id", dest="objection_id", required=True)
47
+ p_res.add_argument("--evidence", required=True)
48
+
49
+ p_wd = sub.add_parser("withdraw", help="Permanently retire a judgment (Withdrawn)")
50
+ p_wd.add_argument("--jid", required=True)
51
+ p_wd.add_argument("--reason", default="")
52
+
53
+ p_sup = sub.add_parser("supersede", help="Permanently supersede old_jid with new_jid")
54
+ p_sup.add_argument("--old", required=True)
55
+ p_sup.add_argument("--new", required=True)
56
+ p_sup.add_argument("--reason", default="")
57
+
58
+ p_gate = sub.add_parser("gate", help="Run fail-closed delivery gate (exit 0 on ALLOW, exit 2 on BLOCK)")
59
+ p_gate.add_argument("cited", nargs="*")
60
+
61
+ p_st = sub.add_parser("status", help="Show ledger status as JSON")
62
+ p_st.add_argument("jids", nargs="*")
63
+
64
+ sub.add_parser("mcp", help="Start zero-dependency JSON-RPC 2.0 MCP stdio server")
65
+ return ap
66
+
67
+
68
+ def main(argv: list[str] | None = None) -> int:
69
+ ap = build_parser()
70
+ args = ap.parse_args(argv)
71
+ ledger_path = Path(args.ledger)
72
+
73
+ if args.cmd == "mcp":
74
+ DelValMCPServer(ledger_path).serve_stdio()
75
+ return 0
76
+
77
+ if args.cmd == "init":
78
+ contract = ReuseContract.from_json_file(Path(args.contract)) if args.contract else None
79
+ ledger = JudgmentLedger(contract=contract)
80
+ ledger.save(ledger_path)
81
+ print(json.dumps({"initialized": str(ledger_path), "contract_id": ledger.contract.contract_id}))
82
+ return 0
83
+
84
+ ledger = JudgmentLedger.load(ledger_path)
85
+
86
+ if args.cmd == "register":
87
+ passed = not args.failed if (args.passed or args.failed) else True
88
+ st = ledger.register(args.jid, args.evidence, passed=passed, digest=args.digest, deps=args.deps)
89
+ ledger.save(ledger_path)
90
+ print(json.dumps({"jid": args.jid, "status": st.value}))
91
+ return 0
92
+
93
+ if args.cmd == "change":
94
+ inv = ledger.notify_change(args.paths)
95
+ ledger.save(ledger_path)
96
+ print(json.dumps({"invalidated": inv}))
97
+ return 0
98
+
99
+ if args.cmd == "recheck":
100
+ if not (args.passed or args.failed):
101
+ ap.error("recheck requires --passed or --failed")
102
+ st = ledger.recheck(args.jid, passed=bool(args.passed and not args.failed), digest=args.digest, detail=args.detail)
103
+ ledger.save(ledger_path)
104
+ print(json.dumps({"jid": args.jid, "status": st.value if st else "unknown"}))
105
+ return 0
106
+
107
+ if args.cmd == "object":
108
+ obj = ledger.raise_objection(args.jid, args.note, objection_id=args.objection_id)
109
+ ledger.save(ledger_path)
110
+ print(json.dumps({"jid": args.jid, "objection_id": obj.objection_id}))
111
+ return 0
112
+
113
+ if args.cmd == "resolve":
114
+ st = ledger.resolve_objection(args.jid, args.objection_id, audit_evidence=args.evidence)
115
+ ledger.save(ledger_path)
116
+ print(json.dumps({"jid": args.jid, "status": st.value}))
117
+ return 0
118
+
119
+ if args.cmd == "withdraw":
120
+ st = ledger.withdraw(args.jid, reason=args.reason)
121
+ ledger.save(ledger_path)
122
+ print(json.dumps({"jid": args.jid, "status": st.value}))
123
+ return 0
124
+
125
+ if args.cmd == "supersede":
126
+ st = ledger.supersede(args.old, args.new, reason=args.reason)
127
+ ledger.save(ledger_path)
128
+ print(json.dumps({"old": args.old, "new": args.new, "status": st.value}))
129
+ return 0
130
+
131
+ if args.cmd == "gate":
132
+ dec = ledger.gate(args.cited)
133
+ ledger.save(ledger_path)
134
+ print(json.dumps(dec.to_dict(), indent=2))
135
+ return 0 if dec.allow else 2
136
+
137
+ if args.cmd == "status":
138
+ jids = args.jids or sorted(ledger.judgments.keys())
139
+ out = {
140
+ "contract_id": ledger.contract.contract_id,
141
+ "judgments": {
142
+ j: {
143
+ "status": ledger.judgments[j].status.value,
144
+ "roots": sorted(ledger.judgments[j].roots),
145
+ "open_objections": [o.objection_id for o in ledger.judgments[j].open_objections()],
146
+ }
147
+ if j in ledger.judgments
148
+ else {"status": "unknown"}
149
+ for j in jids
150
+ },
151
+ }
152
+ print(json.dumps(out, indent=2))
153
+ return 0
154
+
155
+ return 1
156
+
157
+
158
+ if __name__ == "__main__":
159
+ raise SystemExit(main())