pubrules 0.48.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.
- pubrules/__init__.py +14 -0
- pubrules/audit.py +116 -0
- pubrules/audit_draft.py +295 -0
- pubrules/audit_prepare.py +1128 -0
- pubrules/cache.py +319 -0
- pubrules/cbor.py +45 -0
- pubrules/ci.py +910 -0
- pubrules/cli.py +1069 -0
- pubrules/conform.py +295 -0
- pubrules/doc_coverage.py +235 -0
- pubrules/freshness.py +75 -0
- pubrules/gherkin.py +348 -0
- pubrules/hooks.py +59 -0
- pubrules/hosting.py +372 -0
- pubrules/ignores.py +86 -0
- pubrules/new.py +242 -0
- pubrules/nightly.py +1002 -0
- pubrules/paths.py +32 -0
- pubrules/protect.py +596 -0
- pubrules/providers.py +129 -0
- pubrules/refs.py +213 -0
- pubrules/release-commit +1 -0
- pubrules/releases.py +107 -0
- pubrules/repos.py +67 -0
- pubrules/ruleset.json +43 -0
- pubrules/secrets_config.py +165 -0
- pubrules/security.py +134 -0
- pubrules/service_needs.py +467 -0
- pubrules/standard.json +635 -0
- pubrules/standard.py +266 -0
- pubrules/supply_chain.py +188 -0
- pubrules/tag-ruleset.json +14 -0
- pubrules/templates/common/AGENTS.md +32 -0
- pubrules/templates/common/pubrules +26 -0
- pubrules/templates/common/tests/features/repository.feature +11 -0
- pubrules/templates/common/tests/install-lock +65 -0
- pubrules/templates/common/tests/pytest.ini +4 -0
- pubrules/templates/common/tests/requirements-pubrules.txt +17 -0
- pubrules/templates/common/tests/requirements.txt +3 -0
- pubrules/templates/common/tests/steps/test_repository.py +14 -0
- pubrules/templates/deploy/pubdeploy +28 -0
- pubrules/templates/new-rust/CHANGELOG.md +3 -0
- pubrules/templates/new-rust/Cargo.toml +46 -0
- pubrules/templates/new-rust/LICENSE +202 -0
- pubrules/templates/new-rust/Makefile +18 -0
- pubrules/templates/new-rust/README.md +34 -0
- pubrules/templates/new-rust/SECURITY.md +26 -0
- pubrules/templates/new-rust/corpus/corpus.json +11 -0
- pubrules/templates/new-rust/deny.toml +32 -0
- pubrules/templates/new-rust/fuzz/Cargo.toml +23 -0
- pubrules/templates/new-rust/fuzz/fuzz_targets/greet.rs +8 -0
- pubrules/templates/new-rust/fuzz/gitignore +4 -0
- pubrules/templates/new-rust/fuzz/regressions/README.md +4 -0
- pubrules/templates/new-rust/gitignore +10 -0
- pubrules/templates/new-rust/src/lib.rs +91 -0
- pubrules/templates/new-rust/src/main.rs +19 -0
- pubrules/templates/new-rust/supply-chain/audits.toml +4 -0
- pubrules/templates/new-rust/supply-chain/config.toml +23 -0
- pubrules/templates/new-rust/supply-chain/imports.lock +2 -0
- pubrules/templates/new-rust/tests/conftest.py +9 -0
- pubrules/templates/new-rust/tests/features/cli.feature +14 -0
- pubrules/templates/new-rust/tests/steps/test_cli.py +15 -0
- pubrules/templates/new-rust/tests/vectors.rs +30 -0
- pubrules/templates/new-rust/tools/vectors/generate.py +24 -0
- pubrules/templates/python/Makefile +5 -0
- pubrules/templates/python/pubrules-python.mk +47 -0
- pubrules/templates/rust/Makefile +9 -0
- pubrules/templates/rust/pubrules.mk +138 -0
- pubrules/templates/rust/rust-toolchain.toml +8 -0
- pubrules/templates/service/pubsec +27 -0
- pubrules/templates/service/with-secrets +20 -0
- pubrules/templates/service/with-secrets.json +8 -0
- pubrules/variables.py +45 -0
- pubrules-0.48.0.dist-info/METADATA +1241 -0
- pubrules-0.48.0.dist-info/RECORD +79 -0
- pubrules-0.48.0.dist-info/WHEEL +5 -0
- pubrules-0.48.0.dist-info/entry_points.txt +2 -0
- pubrules-0.48.0.dist-info/licenses/LICENSE +202 -0
- pubrules-0.48.0.dist-info/top_level.txt +1 -0
pubrules/__init__.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""pubrules: the setup every package shares.
|
|
2
|
+
|
|
3
|
+
A command, `pubrules`, that keeps each repository's copies of the shared files
|
|
4
|
+
in step with the version it pins. The pytest plugin and the Gherkin steps
|
|
5
|
+
every suite loads are pubtest's, which the managed tests/requirements-pubrules.txt
|
|
6
|
+
pins.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
__version__ = version("pubrules")
|
|
13
|
+
except PackageNotFoundError: # running from a source tree
|
|
14
|
+
__version__ = "0.0.0"
|
pubrules/audit.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""`pubrules audit`: a release cites a current quality audit (RS-REL-01).
|
|
2
|
+
|
|
3
|
+
The rust-crate-quality agent writes its report on a package's release to
|
|
4
|
+
`audit/<package>-<version>.md`, starting with a header:
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
package: example-crate
|
|
8
|
+
version: 0.1.1
|
|
9
|
+
standard: <sha256 of the Rust crate standard it audited against>
|
|
10
|
+
blocking: 0
|
|
11
|
+
date: 2026-10-13
|
|
12
|
+
auditor: rust-crate-quality
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
`pubrules audit --check [PACKAGE VERSION]` passes only when that report exists,
|
|
16
|
+
names that package and version, was made against the standard this pubrules
|
|
17
|
+
ships, and counts no finding that blocks a release. Without arguments it
|
|
18
|
+
checks the release `pubrel prepare` is cutting, which pubrel gives every
|
|
19
|
+
security check as `PUBREL_PACKAGE` and `PUBREL_VERSION`. A repository
|
|
20
|
+
declares it in `release.json` as a security check, so `pubrel prepare` cuts
|
|
21
|
+
no release without one, and the release's signed security record cites the
|
|
22
|
+
report. `pubrules audit --standard` prints the standard's sha256, for the
|
|
23
|
+
header.
|
|
24
|
+
|
|
25
|
+
`pubrules audit prepare` writes a report with every machine-decidable rule
|
|
26
|
+
decided and each rule that needs judgement marked `[needs review]`;
|
|
27
|
+
`pubrules audit draft` marks what Claude drafts for those rules
|
|
28
|
+
`[machine-drafted by MODEL: needs human review]`. Either mark fails the
|
|
29
|
+
check, so no report passes until a person has judged every rule.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
from __future__ import annotations
|
|
33
|
+
|
|
34
|
+
import os
|
|
35
|
+
import re
|
|
36
|
+
import sys
|
|
37
|
+
from pathlib import Path
|
|
38
|
+
|
|
39
|
+
from . import standard
|
|
40
|
+
|
|
41
|
+
DIR = "audit"
|
|
42
|
+
FIELDS = ("package", "version", "standard", "blocking", "date", "auditor")
|
|
43
|
+
# What prepare and draft leave for a person: each a rule nobody has judged.
|
|
44
|
+
PENDING = re.compile(r"\[(?:needs review|machine-drafted[^\]]*)\]")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def current() -> str:
|
|
48
|
+
"""The sha256 of the standard this pubrules ships."""
|
|
49
|
+
return standard.load()["sha256"]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def header(text: str) -> dict[str, str] | None:
|
|
53
|
+
"""The `---`-fenced header at the top of a report, or `None`."""
|
|
54
|
+
m = re.match(r"---\n(.*?)\n---\n", text, re.S)
|
|
55
|
+
if not m:
|
|
56
|
+
return None
|
|
57
|
+
fields = {}
|
|
58
|
+
for line in m.group(1).splitlines():
|
|
59
|
+
key, sep, value = line.partition(":")
|
|
60
|
+
if sep:
|
|
61
|
+
fields[key.strip()] = value.strip()
|
|
62
|
+
return fields
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def check(root: Path, package: str, version: str) -> tuple[str | None, str]:
|
|
66
|
+
"""(problem or None, the line that describes the report)."""
|
|
67
|
+
rel = f"{DIR}/{package}-{version}.md"
|
|
68
|
+
path = root / rel
|
|
69
|
+
if not path.is_file():
|
|
70
|
+
return (f"no {rel}: run `pubrules audit prepare --package {package}`, which writes it "
|
|
71
|
+
f"with every rule a machine can decide, then review the rules it marks (or have "
|
|
72
|
+
f"the rust-crate-quality agent write it)"), rel
|
|
73
|
+
text = path.read_text(encoding="utf-8")
|
|
74
|
+
fields = header(text)
|
|
75
|
+
if fields is None:
|
|
76
|
+
return f"{rel} has no header (--- package, version, standard, blocking ... ---)", rel
|
|
77
|
+
missing = [f for f in FIELDS if not fields.get(f)]
|
|
78
|
+
if missing:
|
|
79
|
+
return f"{rel}: its header gives no {', '.join(missing)}", rel
|
|
80
|
+
if fields["package"] != package or fields["version"] != version:
|
|
81
|
+
return (f"{rel} is a report on {fields['package']} {fields['version']}, "
|
|
82
|
+
f"not {package} {version}"), rel
|
|
83
|
+
if fields["standard"] != current():
|
|
84
|
+
return (f"{rel} audited against another version of the standard "
|
|
85
|
+
f"({fields['standard'][:12]}, not {current()[:12]}): audit again"), rel
|
|
86
|
+
try:
|
|
87
|
+
blocking = int(fields["blocking"])
|
|
88
|
+
except ValueError:
|
|
89
|
+
return f"{rel}: blocking is {fields['blocking']!r}, not a count", rel
|
|
90
|
+
pending = PENDING.findall(text)
|
|
91
|
+
if pending:
|
|
92
|
+
return (f"{rel} has {len(pending)} rule(s) nobody has judged yet (marked {pending[0]}): "
|
|
93
|
+
f"review each, record what you find, and remove its mark"), rel
|
|
94
|
+
if blocking:
|
|
95
|
+
return f"{rel} lists {blocking} finding(s) that block a release: resolve them and audit again", rel
|
|
96
|
+
return None, (f"{rel}: {package} {version} audited on {fields['date']} by "
|
|
97
|
+
f"{fields['auditor']} against the current standard ({current()[:12]}); "
|
|
98
|
+
f"0 findings block a release")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def run(root: Path, args: list[str], show_standard: bool) -> int:
|
|
102
|
+
if show_standard:
|
|
103
|
+
print(current())
|
|
104
|
+
return 0
|
|
105
|
+
if not args:
|
|
106
|
+
args = [os.environ.get("PUBREL_PACKAGE", ""), os.environ.get("PUBREL_VERSION", "")]
|
|
107
|
+
if len(args) != 2 or not all(args):
|
|
108
|
+
print("pubrules: audit --check takes PACKAGE VERSION, or runs as a security check of "
|
|
109
|
+
"`pubrel prepare`, which sets PUBREL_PACKAGE and PUBREL_VERSION", file=sys.stderr)
|
|
110
|
+
return 1
|
|
111
|
+
problem, line = check(root, args[0], args[1])
|
|
112
|
+
if problem:
|
|
113
|
+
print(f"pubrules: {problem}", file=sys.stderr)
|
|
114
|
+
return 1
|
|
115
|
+
print(line)
|
|
116
|
+
return 0
|
pubrules/audit_draft.py
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
"""`pubrules audit draft`: Claude drafts the rules `pubrules audit prepare` left
|
|
2
|
+
for review.
|
|
3
|
+
|
|
4
|
+
pubrules audit draft --package NAME [--report FILE] [--model ID]
|
|
5
|
+
|
|
6
|
+
It sends the Claude API the package's diff since its last release tag, the
|
|
7
|
+
judged rules the report still marks `[needs review]` (each with the
|
|
8
|
+
standard's words and what the machine found for it), and the previous
|
|
9
|
+
release's audit report when there is one. It asks for structured JSON: per
|
|
10
|
+
rule a verdict, findings with file and line, and the reasoning. Each
|
|
11
|
+
drafted rule goes back into the report marked
|
|
12
|
+
`[machine-drafted by MODEL: needs human review]`, so `pubrules audit --check`
|
|
13
|
+
still refuses the report until a person has read every draft.
|
|
14
|
+
|
|
15
|
+
The key is `ANTHROPIC_API_KEY`, from the environment, so the command runs
|
|
16
|
+
through `with-secrets` (`with-secrets ./pubrules audit draft --package NAME`)
|
|
17
|
+
or with the key exported. The model is `--model`, else `PUBRULES_AUDIT_MODEL`,
|
|
18
|
+
else claude-opus-5-5; the API is `ANTHROPIC_BASE_URL`, else
|
|
19
|
+
https://api.anthropic.com. The HTTP call is `post`, which a test replaces.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import json
|
|
25
|
+
import os
|
|
26
|
+
import re
|
|
27
|
+
import subprocess
|
|
28
|
+
import sys
|
|
29
|
+
import urllib.error
|
|
30
|
+
import urllib.request
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
from typing import Callable, Iterator
|
|
33
|
+
|
|
34
|
+
from . import __version__
|
|
35
|
+
from .audit import DIR, header
|
|
36
|
+
from .audit_prepare import Package, rule_text
|
|
37
|
+
|
|
38
|
+
DEFAULT_MODEL = "claude-opus-5-5"
|
|
39
|
+
API_VERSION = "2026-10-21"
|
|
40
|
+
# A declined request is re-run server side on the model Anthropic picks for
|
|
41
|
+
# the reason it was declined, rather than returned as a refusal.
|
|
42
|
+
FALLBACK_BETA = "server-side-fallback-2026-07-01"
|
|
43
|
+
MAX_TOKENS = 64000
|
|
44
|
+
# Beyond this the request would not fit the model's context with room to
|
|
45
|
+
# answer: about 200k tokens.
|
|
46
|
+
MAX_DIFF_CHARS = 800_000
|
|
47
|
+
# What the diff leaves out: generated, recorded or locked, never judged.
|
|
48
|
+
NOT_JUDGED = (":(exclude)*Cargo.lock", ":(exclude)*TESTING.md", ":(exclude)*SUPPLY-CHAIN.md",
|
|
49
|
+
":(exclude)*CHANGELOG.md", ":(exclude)corpus", ":(exclude)supply-chain",
|
|
50
|
+
":(exclude)audit", ":(exclude)fuzz/corpus")
|
|
51
|
+
EMPTY_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
|
|
52
|
+
RULE = re.compile(r"<!-- rule (\S+) review -->\n(.*?)<!-- /rule -->", re.S)
|
|
53
|
+
VERDICTS = ["met", "falls short", "blocks a release", "worth considering", "not applicable",
|
|
54
|
+
"cannot tell from what was sent"]
|
|
55
|
+
SCHEMA = {
|
|
56
|
+
"type": "object",
|
|
57
|
+
"properties": {
|
|
58
|
+
"rules": {
|
|
59
|
+
"type": "array",
|
|
60
|
+
"items": {
|
|
61
|
+
"type": "object",
|
|
62
|
+
"properties": {
|
|
63
|
+
"id": {"type": "string"},
|
|
64
|
+
"verdict": {"type": "string", "enum": VERDICTS},
|
|
65
|
+
"rationale": {"type": "string"},
|
|
66
|
+
"findings": {
|
|
67
|
+
"type": "array",
|
|
68
|
+
"items": {
|
|
69
|
+
"type": "object",
|
|
70
|
+
"properties": {
|
|
71
|
+
"severity": {"type": "string", "enum": ["blocks", "short", "consider"]},
|
|
72
|
+
"file": {"type": "string"},
|
|
73
|
+
"line": {"type": "integer"},
|
|
74
|
+
"text": {"type": "string"},
|
|
75
|
+
},
|
|
76
|
+
"required": ["severity", "file", "line", "text"],
|
|
77
|
+
"additionalProperties": False,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
"required": ["id", "verdict", "rationale", "findings"],
|
|
82
|
+
"additionalProperties": False,
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
"required": ["rules"],
|
|
87
|
+
"additionalProperties": False,
|
|
88
|
+
}
|
|
89
|
+
SYSTEM = """You audit a Rust crate against a written standard, for a release. You \
|
|
90
|
+
are given the rules of the standard that need judgement, each with what a \
|
|
91
|
+
machine already found for it, and the crate's changes since its last \
|
|
92
|
+
release (or all of it, when it has none). Judge each rule against what you \
|
|
93
|
+
are sent and nothing else.
|
|
94
|
+
|
|
95
|
+
For each rule, give a verdict, the findings behind it, and your reasoning in \
|
|
96
|
+
a few plain sentences. A finding names the file and the line (0 when no line \
|
|
97
|
+
is meant) and says what is there and what the standard asks; it "blocks" \
|
|
98
|
+
only for a false claim in the docs, a failing check or a public item with no \
|
|
99
|
+
example, falls "short" for a missing or imprecise section, and is worth \
|
|
100
|
+
"consider"ing otherwise. Never call a claim about another crate true unless \
|
|
101
|
+
what you were sent shows the check that was run: say it is unverified. When \
|
|
102
|
+
the diff does not show enough to judge, say "cannot tell from what was sent" \
|
|
103
|
+
and what a person should read. Do not report anything the standard lists as \
|
|
104
|
+
deliberately not adopted. A person reviews every verdict before the release."""
|
|
105
|
+
|
|
106
|
+
Post = Callable[[str, dict, bytes], Iterator[str]]
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def post(url: str, headers: dict, body: bytes) -> Iterator[str]:
|
|
110
|
+
"""POST and yield the response's lines, as a server-sent event stream."""
|
|
111
|
+
request = urllib.request.Request(url, data=body, headers=headers, method="POST")
|
|
112
|
+
try:
|
|
113
|
+
with urllib.request.urlopen(request, timeout=900) as response:
|
|
114
|
+
for raw in response:
|
|
115
|
+
yield raw.decode("utf-8").rstrip("\r\n")
|
|
116
|
+
except urllib.error.HTTPError as e:
|
|
117
|
+
detail = e.read().decode("utf-8", "replace")
|
|
118
|
+
try:
|
|
119
|
+
detail = json.loads(detail)["error"]["message"]
|
|
120
|
+
except (ValueError, KeyError, TypeError):
|
|
121
|
+
pass
|
|
122
|
+
sys.exit(f"pubrules: the Claude API refused the request ({e.code}): {detail}")
|
|
123
|
+
except urllib.error.URLError as e:
|
|
124
|
+
sys.exit(f"pubrules: cannot reach the Claude API at {url}: {e.reason}")
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def stream(lines: Iterator[str]) -> tuple[str, str, str]:
|
|
128
|
+
"""(the model that answered, its text, why it stopped) from a Messages
|
|
129
|
+
API event stream."""
|
|
130
|
+
model, text, stop, event = "", [], "", ""
|
|
131
|
+
for line in lines:
|
|
132
|
+
if line.startswith("event:"):
|
|
133
|
+
event = line[6:].strip()
|
|
134
|
+
continue
|
|
135
|
+
if not line.startswith("data:"):
|
|
136
|
+
continue
|
|
137
|
+
data = json.loads(line[5:].strip() or "{}")
|
|
138
|
+
kind = data.get("type", event)
|
|
139
|
+
if kind == "message_start":
|
|
140
|
+
model = data.get("message", {}).get("model", "")
|
|
141
|
+
elif kind == "content_block_delta" and data.get("delta", {}).get("type") == "text_delta":
|
|
142
|
+
text.append(data["delta"]["text"])
|
|
143
|
+
elif kind == "message_delta":
|
|
144
|
+
stop = data.get("delta", {}).get("stop_reason") or stop
|
|
145
|
+
elif kind == "error":
|
|
146
|
+
sys.exit(f"pubrules: the Claude API stopped with an error: "
|
|
147
|
+
f"{data.get('error', {}).get('message', data)}")
|
|
148
|
+
return model, "".join(text), stop
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def diff(pkg: Package) -> str:
|
|
152
|
+
"""What changed in the package since its last release, or all of it."""
|
|
153
|
+
base = pkg.last_tag or EMPTY_TREE
|
|
154
|
+
paths = [pkg.rel(pkg.dir) if pkg.dir != pkg.root else "."]
|
|
155
|
+
if pkg.dir != pkg.root:
|
|
156
|
+
paths += [p for p in ("SECURITY.md", "README.md", "Cargo.toml", ".github/workflows", "fuzz")
|
|
157
|
+
if (pkg.root / p).exists()]
|
|
158
|
+
proc = subprocess.run(["git", "diff", "--no-color", base, "--", *paths, *NOT_JUDGED],
|
|
159
|
+
cwd=pkg.root, capture_output=True, text=True, check=False)
|
|
160
|
+
if proc.returncode:
|
|
161
|
+
sys.exit(f"pubrules: git diff {base} failed: {proc.stderr.strip()}")
|
|
162
|
+
return proc.stdout
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def previous_report(pkg: Package) -> str:
|
|
166
|
+
"""The audit report of the release before this one, if it is here."""
|
|
167
|
+
if not pkg.last_tag:
|
|
168
|
+
return ""
|
|
169
|
+
version = pkg.last_tag.removeprefix(pkg.tag_prefix)
|
|
170
|
+
path = pkg.root / DIR / f"{pkg.name}-{version}.md"
|
|
171
|
+
return path.read_text(encoding="utf-8") if path.is_file() else ""
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def prompt(pkg: Package, pending: dict[str, str], rules: dict[str, dict], changes: str,
|
|
175
|
+
earlier: str) -> str:
|
|
176
|
+
kind = "a library" if pkg.library else "a command-line tool with no library target"
|
|
177
|
+
parts = [f"The crate is {pkg.name}, {kind}, to be released as {pkg.version}. "
|
|
178
|
+
+ (f"Its last release is {pkg.last_tag}; the diff below is everything since then."
|
|
179
|
+
if pkg.last_tag else "It has never been released; the diff below is all of it."),
|
|
180
|
+
"", "<rules>"]
|
|
181
|
+
for key, block in pending.items():
|
|
182
|
+
rule = rules[key]
|
|
183
|
+
found = block.split("What the machine found:", 1)
|
|
184
|
+
parts += [f'<rule id="{key}" section="{rule["section"]}">', rule_text(rule)]
|
|
185
|
+
if rule.get("question"):
|
|
186
|
+
parts.append(f"To judge: {rule['question']}")
|
|
187
|
+
if len(found) == 2:
|
|
188
|
+
parts += ["What the machine found:", found[1].strip()]
|
|
189
|
+
parts.append("</rule>")
|
|
190
|
+
parts.append("</rules>")
|
|
191
|
+
if earlier:
|
|
192
|
+
parts += ["", "<previous_audit>", earlier, "</previous_audit>"]
|
|
193
|
+
parts += ["", "<diff>", changes, "</diff>", "",
|
|
194
|
+
"Give one entry per rule above, by its id."]
|
|
195
|
+
return "\n".join(parts)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def merged(text: str, drafts: list[dict], model: str) -> tuple[str, int]:
|
|
199
|
+
"""The report with each drafted rule's block replaced by its draft, and
|
|
200
|
+
the count of drafted findings that block."""
|
|
201
|
+
by_id = {d["id"]: d for d in drafts}
|
|
202
|
+
blocking = 0
|
|
203
|
+
|
|
204
|
+
def replace(m: re.Match) -> str:
|
|
205
|
+
nonlocal blocking
|
|
206
|
+
key, block = m.group(1), m.group(2)
|
|
207
|
+
d = by_id.get(key)
|
|
208
|
+
if d is None:
|
|
209
|
+
return m.group(0)
|
|
210
|
+
title = block.splitlines()[0].replace(" **[needs review]**", "")
|
|
211
|
+
lines = [f"<!-- rule {key} drafted -->", title, "",
|
|
212
|
+
f"**[machine-drafted by {model}: needs human review]** Verdict: {d['verdict']}.", "",
|
|
213
|
+
d["rationale"].strip()]
|
|
214
|
+
if d["findings"]:
|
|
215
|
+
lines.append("")
|
|
216
|
+
for f in d["findings"]:
|
|
217
|
+
where = f"{f['file']}:{f['line']}" if f["line"] else f["file"]
|
|
218
|
+
lines.append(f"- **{f['severity']}**{' (`' + where + '`)' if where else ''}: {f['text'].strip()}")
|
|
219
|
+
blocking += f["severity"] == "blocks"
|
|
220
|
+
rest = "\n".join(block.splitlines()[1:]).strip()
|
|
221
|
+
lines += ["", rest, "<!-- /rule -->"]
|
|
222
|
+
return "\n".join(lines)
|
|
223
|
+
|
|
224
|
+
return RULE.sub(replace, text), blocking
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def draft(root: Path, name: str, report: str | None, model: str | None,
|
|
228
|
+
send: Post | None = None) -> None:
|
|
229
|
+
from . import standard
|
|
230
|
+
key = os.environ.get("ANTHROPIC_API_KEY", "")
|
|
231
|
+
if not key:
|
|
232
|
+
sys.exit("pubrules: audit draft calls the Claude API, which needs ANTHROPIC_API_KEY: run it "
|
|
233
|
+
"through with-secrets (with-secrets ./pubrules audit draft --package NAME), or "
|
|
234
|
+
"export the key")
|
|
235
|
+
model = model or os.environ.get("PUBRULES_AUDIT_MODEL") or DEFAULT_MODEL
|
|
236
|
+
from .paths import confined
|
|
237
|
+
path = confined(report, "the --report file") if report else None
|
|
238
|
+
version = None
|
|
239
|
+
if path and path.is_file():
|
|
240
|
+
version = (header(path.read_text(encoding="utf-8")) or {}).get("version")
|
|
241
|
+
pkg = Package(root, name, version)
|
|
242
|
+
path = path or root / DIR / f"{pkg.name}-{pkg.version}.md"
|
|
243
|
+
if not path.is_file():
|
|
244
|
+
sys.exit(f"pubrules: no {pkg.rel(path)}: run `pubrules audit prepare --package {name}` first")
|
|
245
|
+
text = path.read_text(encoding="utf-8")
|
|
246
|
+
pending = {m.group(1): m.group(2) for m in RULE.finditer(text)}
|
|
247
|
+
if not pending:
|
|
248
|
+
print(f"{pkg.rel(path)} has no rule left marked for review; nothing to draft")
|
|
249
|
+
return
|
|
250
|
+
rules = {r["id"]: r for r in standard.load()["judged"]}
|
|
251
|
+
unknown = [k for k in pending if k not in rules]
|
|
252
|
+
if unknown:
|
|
253
|
+
sys.exit(f"pubrules: {pkg.rel(path)} marks rules {', '.join(unknown)}, which this pubrules' "
|
|
254
|
+
f"standard does not have: prepare the report again")
|
|
255
|
+
changes = diff(pkg)
|
|
256
|
+
if len(changes) > MAX_DIFF_CHARS:
|
|
257
|
+
sys.exit(f"pubrules: the diff since {pkg.last_tag or 'the start'} is {len(changes):,} characters, "
|
|
258
|
+
f"more than one request can judge ({MAX_DIFF_CHARS:,}); review those rules by hand")
|
|
259
|
+
body = {
|
|
260
|
+
"model": model,
|
|
261
|
+
"max_tokens": MAX_TOKENS,
|
|
262
|
+
"stream": True,
|
|
263
|
+
"system": SYSTEM,
|
|
264
|
+
"messages": [{"role": "user", "content": prompt(pkg, pending, rules, changes, previous_report(pkg))}],
|
|
265
|
+
"output_config": {"effort": "high", "format": {"type": "json_schema", "schema": SCHEMA}},
|
|
266
|
+
"fallbacks": "default",
|
|
267
|
+
}
|
|
268
|
+
headers = {"content-type": "application/json", "x-api-key": key,
|
|
269
|
+
"anthropic-version": API_VERSION, "anthropic-beta": FALLBACK_BETA,
|
|
270
|
+
"user-agent": f"pubrules/{__version__}"}
|
|
271
|
+
base = os.environ.get("ANTHROPIC_BASE_URL", "https://api.anthropic.com").rstrip("/")
|
|
272
|
+
print(f"drafting {len(pending)} rule(s) of {pkg.name} {pkg.version} with {model} "
|
|
273
|
+
f"({len(changes):,} characters of diff since {pkg.last_tag or 'the start'})", file=sys.stderr)
|
|
274
|
+
served, answer, stop = stream((send or post)(f"{base}/v1/messages", headers, json.dumps(body).encode()))
|
|
275
|
+
if stop == "refusal":
|
|
276
|
+
sys.exit("pubrules: the model declined to draft this audit; review the rules by hand")
|
|
277
|
+
if stop == "max_tokens":
|
|
278
|
+
sys.exit("pubrules: the draft ran out of room before it finished; nothing was written")
|
|
279
|
+
try:
|
|
280
|
+
drafts = json.loads(answer)["rules"]
|
|
281
|
+
except (ValueError, KeyError, TypeError):
|
|
282
|
+
sys.exit("pubrules: the Claude API's answer is not the JSON asked for; nothing was written")
|
|
283
|
+
drafts = [d for d in drafts if d.get("id") in pending]
|
|
284
|
+
served = served or model
|
|
285
|
+
text, blocking = merged(text, drafts, served)
|
|
286
|
+
fields = header(text) or {}
|
|
287
|
+
if blocking and fields.get("blocking", "").isdigit():
|
|
288
|
+
text = re.sub(r"^blocking: \d+$", f"blocking: {int(fields['blocking']) + blocking}", text, count=1, flags=re.M)
|
|
289
|
+
if fields.get("auditor"):
|
|
290
|
+
text = re.sub(r"^auditor: .*$", f"auditor: {fields['auditor']}, drafted by {served}", text, count=1, flags=re.M)
|
|
291
|
+
path.write_text(text, encoding="utf-8")
|
|
292
|
+
missed = sorted(set(pending) - {d["id"] for d in drafts})
|
|
293
|
+
print(f"drafted {len(drafts)} rule(s) into {pkg.rel(path)}, {blocking} drafted finding(s) block"
|
|
294
|
+
+ (f"; not drafted: {', '.join(missed)}" if missed else "")
|
|
295
|
+
+ ". Each is marked for human review; `pubrules audit --check` fails until a person has read them")
|