Skip to main content
Glama
README.md
# delval-gate

[![CI](https://github.com/iannil/delval-gate/actions/workflows/ci.yml/badge.svg)](https://github.com/iannil/delval-gate/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/delval-gate.svg)](https://pypi.org/project/delval-gate/)
[![DOI](https://img.shields.io/badge/Zenodo%20DOI-10.5281%2Fzenodo.24455837-blue)](https://doi.org/10.5281/zenodo.24455837)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
[![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
[![Zero Dependencies](https://img.shields.io/badge/dependencies-0%20(stdlib%20only)-brightgreen.svg)](pyproject.toml)

**Fail-Closed Delivery Permission & Judgment Ledger for Long-Running AI Agents**  
*(Zero-dependency Python 3 library, CLI, and Model Context Protocol (MCP) stdio server)*

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.

`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}$).

---

## Core Guarantees (Formally Verified Invariants)

| Invariant | What `delval-gate` Enforces |
|---|---|
| **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. |
| **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. |
| **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`. |
| **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. |
| **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. |
| **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`. |

---

## Installation

Zero third-party dependencies (Python 3.10+ stdlib only):

```bash
pip install delval-gate
```

Or install from source:

```bash
git clone https://github.com/iannil/delval-gate.git
cd delval-gate
pip install -e .
```

Or run directly from source without installing:
```bash
PYTHONPATH=src python3 -m delval_gate.cli --help
```

---

## Quickstart (CLI & CI Gate)

```bash
# 1. Initialize a ledger with a frozen JSON reuse contract
delval-gate --ledger .delval.json init --contract examples/reuse-contract.json

# 2. Register an upstream evidence-bound judgment
delval-gate --ledger .delval.json register \
  --jid "build:api-schema" \
  --evidence "src/schema.py" "pyproject.toml" \
  --digest "sha256:abc123" --passed

# 3. Register a downstream derived judgment depending on build:api-schema
delval-gate --ledger .delval.json register \
  --jid "release:v2-ready" \
  --evidence "docs/api.md" \
  --deps "build:api-schema" \
  --digest "sha256:def456" --passed

# 4. Structural change -> automatic transitive invalidation
delval-gate --ledger .delval.json change "src/schema.py"

# 5. Delivery gate FAILS CLOSED (exit code 2 + exact blocking reasons)
delval-gate --ledger .delval.json gate "release:v2-ready"

# 6. Trusted recheck in dependency order -> gate ALLOWS (exit code 0)
delval-gate --ledger .delval.json recheck --jid "build:api-schema" --passed --digest "sha256:new1"
delval-gate --ledger .delval.json recheck --jid "release:v2-ready" --passed --digest "sha256:new2"
delval-gate --ledger .delval.json gate "release:v2-ready"
```

Run the full interactive demo script:
```bash
bash examples/demo.sh
```

---

## MCP Server Mode (Claude Code / Cursor / Any MCP Agent)

`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:

```json
{
  "mcpServers": {
    "delval-gate": {
      "command": "delval-gate",
      "args": ["--ledger", ".delval.json", "mcp"]
    }
  }
}
```

### Exposed MCP Tools
- `delval_register` — register an evidence-bound judgment (echo-immune)
- `delval_notify_change` — notify changed paths (`path` or `path@change_class`) and propagate staleness
- `delval_recheck` — record a trusted verifier recheck outcome (sole recovery gate)
- `delval_raise_objection` / `delval_resolve_objection` — open or evidence-backed close an objection
- `delval_withdraw` / `delval_supersede` — permanently retire or replace a judgment
- `delval_gate` — fail-closed delivery-permission check over cited judgments and their transitive closure
- `delval_status` — inspect statuses, distinct root counts, open objections, and audit trail

---

## Python API

```python
from delval_gate import JudgmentLedger

ledger = JudgmentLedger()
ledger.register("build:api", ["src/api.py"], passed=True, digest="sha256:111")
ledger.register("release:v1", ["docs/api.md"], deps=["build:api"], passed=True, digest="sha256:222")

ledger.notify_change(["src/api.py"])
decision = ledger.gate(["release:v1"])
assert not decision.allow
print(decision.blocked_reasons)
# ('claim-not-valid:build:api:stale', 'claim-not-valid:release:v1:stale')
```

---

## Running Tests

```bash
python3 -m unittest discover -s tests -v
```

---

## Academic Papers & Formal Verification Archive

- **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/
- **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/
- **Isabelle/HOL 2026 Proofs, Alloy Models & 728-Trial Dataset (Zenodo DOI):** [10.5281/zenodo.24455837](https://doi.org/10.5281/zenodo.24455837)
- **Author ORCID:** [0009-0002-6890-085X](https://orcid.org/0009-0002-6890-085X)