Skip to main content

hpflow: a proof gate and an audit ladder for Python code

This package has two commands.

  • hpf is the audit ladder. It takes the Python functions that changed in a git range up a ladder of evidence: a review finding with a runnable repro, a script run of the repro, two verifier agents, a fix with a test, and a gate before the release. See the section "hpflow" below.
  • hp is the proof gate. It is one proof gate for one Python function, with Lean 4. The first sections of this file are about hp.

The two come from the study "Proved is not finished" (https://puppetmaster.gg/labs/hard-proof/). The study has the data, the costs and the limits. Source: https://github.com/professorpalmer/hard-proof

hp replaces the hand-written gate scripts of the study (exp12/base/gate12.py and the scripts before it) with one command set and one domain contract. A new Python function gets a proof gate with no new gate code.

The tool uses Lean 4.36.0 with no Mathlib. lake comparator does the statement match, the axiom audit and the kernel replay. The tool is Python 3.9+ with the standard library only. It makes no model call.

Install

pip install hpflow

This gives three commands: hp (the proof gate), and hpf and hpflow (the audit ladder). The package has no Python dependency. From a checkout of the repository, ./hp and ./hpf in the folder tool do the same with no install.

The name of the package is hpflow. The name hardproof on PyPI is a different project, and it is not related to these tools.

Command Needs
hp Lean 4.36.0 through elan. No model call.
hpf audit, hpf fix The Puppetmaster command line (pip install puppetmaster-ai) and Claude Code with a login. These steps make model calls.
hpf gate, hpf waive, hpf status git and Python only. No model call.

What the gate shows

A domain has a Lean mirror ref of the Python function and a set of laws written from the documentation. The gate accepts certificates that untrusted agents write:

  • The link test shows that ref and the real function give the same output on many random legal inputs, and that the test kills mutants of the function.
  • A ref certificate proves that ref passes each law.
  • A conflict certificate proves that ref breaks one law on a legal input. The law or the code is wrong. A human decides.
  • A gap certificate gives two designs that pass each law and differ on a legal input. The laws do not decide that input. A human decides.
  • A complete certificate proves that two designs that pass each law agree on each legal input. Then the laws pin the behavior down.

The domain contract

A domain is one folder for one function under test. The function has the type Impl := Input → Output in Lean. A stateful system is out of scope; put the state into Input.

Author files (an agent or a human writes them; then pin records them):

File Content
hardproof.json {"name": "...", "namespace": "HP", "python": {"module": "pkg.mod", "function": "name"}, "banned_in_laws": ["ref", ...]}
Types.lean namespace HP: Input, Output (each with DecidableEq), abbrev Impl := Input → Output, def legal : Input → Bool, def Legal (x : Input) : Prop := legal x = true, and the words that a law can use. No ref here.
Ref.lean imports Types. def HP.ref : HP.Impl, the mirror of the Python code, and its helper functions.
Codec.lean imports Types. HP.decodeInput : String → Option Input, HP.encodeInput, HP.encodeOutput, HP.showInput, HP.showOutput. One line of text for each value.
link_adapter.py gen(rng), encode(case), decode(line), call(func, case), encode_result(result), is_legal(case).
DESCRIPTION.md The sentences of the documentation, each an exact quote with its source file. Then a map from the model words to the documentation words.
Laws.lean imports Types only. One def <Name> (f : Impl) : Prop for each law, def Passes (f : Impl) : Prop (the conjunction of all laws), and theorem some_design_fails : ∃ f : Impl, ¬ Passes f.

Solution files (an untrusted agent writes them; the comparator checks them):

File Challenge statement
RefPassesSolution.lean theorem HPRef.ref_passes : Passes ref
ConflictSolution.lean first line -- hardproof-law: <Law>; def HPConflict.input : Input, theorem HPConflict.input_legal : Legal input, theorem HPConflict.ref_breaks : ¬ <Law> ref
GapSolution.lean def HPGap.designA, def HPGap.designB : Impl, def HPGap.input : Input, theorem HPGap.a_passes : Passes designA, theorem HPGap.b_passes : Passes designB, theorem HPGap.input_legal : Legal input, theorem HPGap.differ : designA input ≠ designB input
CompleteSolution.lean theorem HPComplete.laws_complete : ∀ f g : Impl, Passes f → Passes g → ∀ x : Input, Legal x → f x = g x

Tool-owned files are in hp/: lakefile.toml, lean-toolchain, lake-manifest.json, the challenge modules, the comparator configs, Runner.lean, QuestionGap.lean and QuestionConflict.lean. The tool writes them again before each check, so an edit to one of them has no effect. The lakefile has one lean_lib for each solution, so a broken solution of one kind does not stop the check of another kind.

Quick start (on examples/clamp)

examples/clamp is a pure function clamp(x, lo, hi) in examples/clamp/pycode/clampmod.py. It needs no outside repository. The examples are in the repository, not in the installed package.

  1. Copy the example to a new folder and go to it:

    cp -R examples/clamp /tmp/clamp-demo
    cd /tmp/clamp-demo
    
  2. Set HP to the command: HP=hp after an install, or the full path of the launcher hp in the folder tool of a checkout.

  3. Check the model (types, mirror, codec, runner and the codec round trip):

    $HP check model
    
  4. Run the link test against the real function:

    $HP link --repo pycode --cases 5000
    
  5. Check the laws and the ref certificate:

    $HP check laws ref
    
  6. Pin the files, and check the pins:

    $HP pin
    $HP verify-pins
    $HP status
    

Each command prints one JSON report. The exit code is 0 for accept (or agree), 1 for reject, and 2 for a usage error.

Commands

All commands take --dir PATH (the domain folder; the default is the current folder) and --actor NAME (the name in the run log). Run them as python3 -m hpflow.proof <command> with PYTHONPATH set to this folder, or as ./hp <command>.

  • new DIR --name NAME --module PKG.MOD --function NAME: make a domain folder with author stubs (each holds short instructions) and the tool-owned files.
  • generate: write the tool-owned files again.
  • check model: Types, Ref, Codec and Runner build. No sorry, no axiom, no native_decide, no implemented_by. The declarations have the types of the contract. The codec round trip holds on 200 cases from link_adapter.gen: decode(encode(c)) == c, and the Lean runner decodes each line.
  • link --repo PATH [--cases N] [--seed N] [--mutants N] [--function-file PATH]: the link test. For each generated legal case, the Lean ref and the real function give the same output line. Then up to N mutants of the function source run on the same cases. The report gives the cases, the differences (must be 0), the first difference, the mutants made and killed, the kill rate, and each mutant that survives. The verdict is agree only when the differences are 0. The kill rate is reported, not judged. With --function-file, the function comes from that file and not from the module in hardproof.json.
  • check laws: the text rules (imports Types only; no banned word of hardproof.json before theorem some_design_fails; no sorry, axiom or native_decide), the build, the types of Passes and some_design_fails, and the axiom audit (propext, Quot.sound, Classical.choice only). The report lists the law names.
  • check ref, check conflict, check gap, check complete: build the solution, check that each required name exists, then run lake comparator. --paranoid runs the five extra kernels. --inadvisably-no-sandbox is added when bwrap is absent (macOS). For conflict, the named law must be a law of Laws.lean. You can name more than one kind; the check accepts when one kind is accepted.
  • question [gap|conflict]: for an accepted gap, the encoded and shown input and the outputs of design A, design B and ref. For an accepted conflict, the law, the input and the output of ref.
  • pin: write hardproof.lock.json with the SHA-256 of each author file and each generated file.
  • verify-pins [--expect FILE=SHA ...]: reject when a pinned file differs. With --expect, check the given values (for example from a flow graph).
  • status: what exists, and what the last check of each kind said.

Each check and each link appends one line to hardproof_runs.jsonl in the domain folder: time, actor, command, verdict and seconds.

The mutator

link mutates the source of the function with its own AST mutator. Each mutant changes one place:

  • negate the condition of an if, while, conditional expression or assert,
  • swap and and or,
  • change a comparison operator,
  • change an integer constant by one,
  • replace a return value by None,
  • delete one statement,
  • swap two positional arguments, or the values of two keyword arguments, of one call.

A mutant is killed when one case gives a different line, or when it raises or runs for more than 30 seconds. The report also says if the unchanged source, compiled the same way, agrees (identity_copy_agrees).

Examples

  • examples/clamp: clamp(x, lo, hi) over integers, with four laws and a ref certificate.

  • examples/sync: puppetmaster models sync from experiment 12. Input holds the --enable flag, the locked adapters, the starter list and the registry. Legal is the corrected rule: the starter ids are pairwise distinct. The seven laws have no law for the order of the new rows, so GapSolution.lean shows a gap. variants/order adds an order law and a completeness proof. variants/false_law adds a false law and a conflict certificate. The link test needs a Puppetmaster clone:

    ./hp --dir examples/sync link --repo ~/Projects/hp-work/Puppetmaster
    

Tests

python3 -m unittest discover tests

The tests make no model call. The Puppetmaster link test is skipped with a message when the clone at ~/Projects/hp-work/Puppetmaster is absent.

hpflow

hpflow is the second package of this folder. It runs agent workers through the Puppetmaster command line and takes a set of Python functions up an evidence ladder. The proof gate hp makes no model call. hpflow does.

Grade Evidence Who gives it
0 A finding with a runnable repro a reviewer agent
1 The repro runs on the source under audit and gives the result that the finding states a script
2 Two verifier agents on different models each say real, and name a caller that reaches it two agents
3 A fix with a regression test, a script check and an independent judge agents and a script

Quick start

./hpf audit --repo . --ref HEAD --changed main..HEAD --out /tmp/audit
./hpf fix --out /tmp/audit
./hpf status --out /tmp/audit

Then read /tmp/audit/REPORT.md and the worktrees in /tmp/audit/fix/.

Use it as a gate before a release or a merge

The audit looks only at the functions that changed in a git range, so its cost follows the size of the change. The gate is the last step. It makes no model call, and its exit code is 0 for a pass and 1 for a fail.

hpf audit --repo . --ref HEAD --changed v1.2.3..HEAD --out ~/audits/next
hpf fix   --out ~/audits/next      # only when REPORT.md has confirmed bugs
# Review each worktree in ~/audits/next/fix/, then commit and merge the fixes yourself.
hpf gate  --out ~/audits/next --ref HEAD

gate --ref REF runs the repro of each confirmed bug on the commit REF. A bug is closed when each of its repros gives a new result there. So the gate checks the code that you will release, not the worktree of a fix worker. With no --ref, a bug is closed when it has a fix with an accepted check and a PASS judge.

When the owner decides that a confirmed bug stays as it is, record it:

hpf waive --out ~/audits/next --bug ID --reason "why the behavior stays" --by NAME

The gate fails when a review or a verifier run is absent, when a confirmed bug is open, or when a repro does not run on REF. A range with no changed function passes with no worker run.

What we measured on our two products (API-equivalent cost, Claude models): about 0.47 US dollars for each changed function for the review and the verifiers with the defaults of today (9.44 US dollars for 20 functions, 8 confirmed bugs). The first form of the tool cost 28.53 US dollars for the same range and confirmed 7 bugs: each worker loaded the settings of the user, and each function with a finding had two verifier runs. One reviewer session for each file (--group file) cost 8.94 US dollars and confirmed 3 bugs, so it is not the default. On that audit, gate --ref failed on the audited release with 7 open bugs and passed on the next release with 7 closed.

Commands

All commands take --out DIR (the audit folder), --redo, --concurrency N (default 5: the most worker flows that run at one time) and --user-settings. A worker is lean by default: it does not load the Claude Code settings, rules, hooks, MCP servers and skills of the user. That start text was most of the cost of a worker run on our machine (about 83,000 tokens for each session). --user-settings gives each worker the full profile. Each command that hpflow starts (a repro, a test run, git) runs in its own process group with a timeout. At the timeout the whole group ends. Run them as ./hpf <command> or as python3 -m hpflow <command> with PYTHONPATH set to this folder. Each command prints one JSON report. The exit code is 0 on success, 1 on a failed step, and 2 on a usage error.

All state is in the audit folder, as JSON files. A command can run again: it does not repeat a worker run that has a stored result. --redo makes the runs again. A stored run with an error (launch or wait) runs again.

  • init --repo PATH --ref REF [--paths ...] [--python PATH] [--test-cmd "..."]: write audit.json and the snapshot src/ (git archive of the paths at the commit of REF). The default paths are each top-level folder with an __init__.py, and tests, docs, README.md and pyproject.toml when they are present. The default Python is .venv/bin/python of the repository, else the Python that runs hpflow. The test command is a template with {python} and {tests}; the default is {python} -m pytest -q -p no:cacheprovider {tests}.
  • targets (--changed A..B | --function FILE:NAME ... | --file FILE ...) [--max-lines 150] [--max N]: write targets.json. --changed takes the functions and methods (from the B side) of non-test Python files that overlap a hunk of git diff A..B. The report lists the skipped functions with the reason: too long, test file, removed, or over --max.
  • review [--model sonnet] [--shell | --no-shell] [--passes 1] [--repro-timeout 60] [--group file|function] [--group-size 6]: one reviewer run for the changed functions of each file (at most --group-size functions in one run), and for each pass. --group function gives one run for each function. Each finding has a repro file finding_<n>.py with run(). A script runs each repro on src/ in a separate process. A finding has grade 1 when run() returns. Else it stays at grade 0 with the reason (no repro file, an import error, a timeout, or an exception).
  • verify [--models sonnet opus] [--group file|function] [--all-verifiers]: one verifier run for each file with grade-1 findings. The first model runs for each group. The other models run only for a group in which the first model says real for one claim or more, because a claim that the first verifier does not call real cannot be confirmed. --all-verifiers runs each model for each group. The default grouping is the grouping of the stored review. A finding has grade 2 when each verifier says real. Duplicates fold into one bug (the duplicate_of field of the first model). Writes verify.json, confirmed.json and REPORT.md (one table for each class: confirmed, real but not reachable, by design, mixed).
  • fix [--base REF] [--only BUG ...] [--model opus] [--judge-model sonnet] [--rules TEXT]: for each source file with confirmed bugs, a git worktree fix/<slug> on a new branch hpfix/<slug> from --base (default: the audited commit), in the audited repository. A build worker writes the fix and the test file tests/test_hpfix_<slug>.py. A script check (python3 -m hpflow.fixcheck) runs each repro (it must give a new result), the new test file and the related tests. A related test file of more than 400 KB is not run, and the report names it: such a file can run for a long time, and the CI of the repository runs it. A judge reviews the diff with a rubric. A failed check or a FAIL or PARTIAL review goes back to the builder. hpflow never commits, pushes or merges. The report lists each worktree, its diff stat, its new files, the check verdict and the judge verdict.
  • audit ...: init, targets, review and verify in one call, with the options of each. It stops at the first step that fails.
  • status: the count of findings at each grade, the cost and time of each step, and what is not done.
  • waive --bug ID --reason TEXT [--by NAME]: record in waivers.json that a confirmed bug stays as it is. A waiver needs a reason.
  • gate [--ref REF] [--repro-timeout 60]: pass only when the audit is complete and each confirmed bug is closed or waived. The report lists each bug with its state and the reason. No model call.

Model names: haiku, sonnet and opus map to claude-haiku-5-5, claude-sonnet-5-5 and claude-opus-5-5. A full model id passes through. The adapter is claude-code.

Workers get a snapshot of the source, never the repository of the user. Each run folder is runs/<digits>, and each worker gets a copy of src/ as project/. The name holds no word and no function name.

Cost is API-equivalent, as the provider tool reports it. Plan billing can apply. The tool reports the cost of a resumed session as the total of the session so far. So the cost of a node is the largest value of its visits, and the cost of one visit is the difference from the visit before it.

Limits

  • Grade 2 is the agreement of two agents. It is not a proof.
  • A human owner of the code decides what is a bug, and reviews and commits each fix.
  • Grade 1 shows only that the repro runs and gives a result. It does not show that the result is wrong.
  • A pass of the gate says that each confirmed bug is closed or waived. It does not say that the code has no bug: in our study the best review arm found 44 of 95 known bugs.
  • The fix step can make a wrong fix that passes its check and its judge (5 of 45 in our study). Review each fix before you merge it.
  • Python only. The reviewer and the verifiers are Claude models through Claude Code. Other adapters are not tested.
  • Lean certificates (the proof gate hp) are not part of the ladder. Use hp for a small rule that the owner states, for example a decision table.

Metadata

Release files for hpflow 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hpflow 0.4.1
File Size Uploaded Provenance Actions
hpflow-0.4.1.tar.gz 69.6 kB Without provenance Details for hpflow-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hpflow 0.4.1
File Interpreter ABI Platform Actions
hpflow-0.4.1-py3-none-any.whl Python 3 none any Details for hpflow-0.4.1-py3-none-any.whl

Total release size: 129.2 kB

Release files / hpflow-0.4.1.tar.gz

Download URL hpflow-0.4.1.tar.gz
Size 69.6 kB
Tags Source
SHA-256 checksum
How to use checksums
fe87bc947183230916c79baaccaac978c999cfec7dd06599ddae737310a2f5e2
BLAKE2b-256 checksum
How to use checksums
45555729d1e7fa9b66ea7903f76013ae724002dd14998f238eac58dbbe515ccc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / hpflow-0.4.1-py3-none-any.whl

Download URL hpflow-0.4.1-py3-none-any.whl
Size 59.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
63b4b6a9769dce797f917ae63e3e904036073807c7db42aa807d0ada18b5bf8c
BLAKE2b-256 checksum
How to use checksums
613924da20cf8915b2142f0e0f1bcebbbf3fd3b972351c98da5d56a1fcc73eff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Security for hpflow 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page