hpflow: a proof gate and an audit ladder for Python code
This package has two commands.
hpfis 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.hpis the proof gate. It is one proof gate for one Python function, with Lean 4. The first sections of this file are abouthp.
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
refand the real function give the same output on many random legal inputs, and that the test kills mutants of the function. - A
refcertificate proves thatrefpasses each law. - A
conflictcertificate proves thatrefbreaks one law on a legal input. The law or the code is wrong. A human decides. - A
gapcertificate gives two designs that pass each law and differ on a legal input. The laws do not decide that input. A human decides. - A
completecertificate 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.
-
Copy the example to a new folder and go to it:
cp -R examples/clamp /tmp/clamp-demo cd /tmp/clamp-demo
-
Set
HPto the command:HP=hpafter an install, or the full path of the launcherhpin the foldertoolof a checkout. -
Check the model (types, mirror, codec, runner and the codec round trip):
$HP check model
-
Run the link test against the real function:
$HP link --repo pycode --cases 5000
-
Check the laws and the
refcertificate:$HP check laws ref
-
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,CodecandRunnerbuild. Nosorry, noaxiom, nonative_decide, noimplemented_by. The declarations have the types of the contract. The codec round trip holds on 200 cases fromlink_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 Leanrefand 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 isagreeonly 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 inhardproof.json.check laws: the text rules (importsTypesonly; no banned word ofhardproof.jsonbeforetheorem some_design_fails; nosorry,axiomornative_decide), the build, the types ofPassesandsome_design_fails, and the axiom audit (propext,Quot.sound,Classical.choiceonly). The report lists the law names.check ref,check conflict,check gap,check complete: build the solution, check that each required name exists, then runlake comparator.--paranoidruns the five extra kernels.--inadvisably-no-sandboxis added whenbwrapis absent (macOS). Forconflict, the named law must be a law ofLaws.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 andref. For an accepted conflict, the law, the input and the output ofref.pin: writehardproof.lock.jsonwith 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 orassert, - swap
andandor, - change a comparison operator,
- change an integer constant by one,
- replace a
returnvalue byNone, - 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 arefcertificate. -
examples/sync:puppetmaster models syncfrom experiment 12.Inputholds the--enableflag, the locked adapters, the starter list and the registry.Legalis the corrected rule: the starter ids are pairwise distinct. The seven laws have no law for the order of the new rows, soGapSolution.leanshows a gap.variants/orderadds an order law and a completeness proof.variants/false_lawadds 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 "..."]: writeaudit.jsonand the snapshotsrc/(git archiveof the paths at the commit ofREF). The default paths are each top-level folder with an__init__.py, andtests,docs,README.mdandpyproject.tomlwhen they are present. The default Python is.venv/bin/pythonof the repository, else the Python that runshpflow. 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]: writetargets.json.--changedtakes the functions and methods (from theBside) of non-test Python files that overlap a hunk ofgit 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-sizefunctions in one run), and for each pass.--group functiongives one run for each function. Each finding has a repro filefinding_<n>.pywithrun(). A script runs each repro onsrc/in a separate process. A finding has grade 1 whenrun()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 saysrealfor one claim or more, because a claim that the first verifier does not callrealcannot be confirmed.--all-verifiersruns each model for each group. The default grouping is the grouping of the stored review. A finding has grade 2 when each verifier saysreal. Duplicates fold into one bug (theduplicate_offield of the first model). Writesverify.json,confirmed.jsonandREPORT.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 worktreefix/<slug>on a new branchhpfix/<slug>from--base(default: the audited commit), in the audited repository. A build worker writes the fix and the test filetests/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 judge reviews the diff with a rubric. A failed check or a FAIL or PARTIAL review goes back to the builder.hpflownever 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,reviewandverifyin 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 inwaivers.jsonthat 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. Usehpfor a small rule that the owner states, for example a decision table.
Metadata
Release files for hpflow 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| hpflow-0.4.0.tar.gz | 69.0 kB | Without provenance | Details for hpflow-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| hpflow-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for hpflow-0.4.0-py3-none-any.whl |
Total release size: 128.4 kB
Release files / hpflow-0.4.0.tar.gz
| Download URL | hpflow-0.4.0.tar.gz |
|---|---|
| Size | 69.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a77cf3e33e8b02b9db7e4a8aa4b308e9f64e54098e8177621d3598259fc101d0
|
|
BLAKE2b-256 checksum How to use checksums |
7790abfa81d00e5ed0b21f459ec06e6bcc7191e852d319ebd3f11ef3d6063358
|
| 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.0-py3-none-any.whl
| Download URL | hpflow-0.4.0-py3-none-any.whl |
|---|---|
| Size | 59.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a648cb8039174a2c9074af87334f143d17dd6f595b8eac75084e72e129b3ae42
|
|
BLAKE2b-256 checksum How to use checksums |
b4b1c11a65a0ded842d34727b02b4cf8abd1a074e924915fd716618a01986642
|
| 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.0
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.