codeveto 0.1.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.
- codeveto/__init__.py +3 -0
- codeveto/__main__.py +8 -0
- codeveto/ai/__init__.py +19 -0
- codeveto/ai/harness.py +303 -0
- codeveto/ai/packets.py +855 -0
- codeveto/ai/policy.py +222 -0
- codeveto/ai/prompt.py +101 -0
- codeveto/ai/providers/__init__.py +178 -0
- codeveto/ai/providers/claude.py +247 -0
- codeveto/ai/providers/openai_compat.py +246 -0
- codeveto/ai/runner.py +293 -0
- codeveto/ai/verify.py +324 -0
- codeveto/anchor.py +211 -0
- codeveto/cli.py +247 -0
- codeveto/cli_explain.py +49 -0
- codeveto/cli_hook.py +31 -0
- codeveto/cli_init.py +254 -0
- codeveto/cli_mcp.py +29 -0
- codeveto/cli_packets.py +35 -0
- codeveto/cli_publish.py +29 -0
- codeveto/cli_review.py +362 -0
- codeveto/cli_rules.py +78 -0
- codeveto/cli_verify.py +47 -0
- codeveto/config.py +1229 -0
- codeveto/data/README.md +16 -0
- codeveto/data/ai-wire.schema.json +53 -0
- codeveto/data/prompts/system.md +57 -0
- codeveto/data/report-v1.schema.json +268 -0
- codeveto/data/templates/forgejo-codeveto.yml +104 -0
- codeveto/data/templates/github-codeveto.yml +18 -0
- codeveto/embedded.py +311 -0
- codeveto/engine.py +1570 -0
- codeveto/git.py +1549 -0
- codeveto/globs.py +348 -0
- codeveto/integrations/__init__.py +1 -0
- codeveto/integrations/hooks.py +327 -0
- codeveto/integrations/mcp.py +770 -0
- codeveto/integrations/mcp_ai.py +77 -0
- codeveto/integrations/prepush.py +222 -0
- codeveto/jsonsafe.py +60 -0
- codeveto/languages.py +539 -0
- codeveto/lexers.py +2560 -0
- codeveto/model.py +372 -0
- codeveto/publish/__init__.py +28 -0
- codeveto/publish/action.py +330 -0
- codeveto/publish/common.py +84 -0
- codeveto/publish/diff.py +208 -0
- codeveto/publish/event.py +241 -0
- codeveto/publish/forgejo.py +184 -0
- codeveto/publish/github.py +380 -0
- codeveto/publish/http.py +263 -0
- codeveto/publish/pull.py +218 -0
- codeveto/publish/untrusted.py +110 -0
- codeveto/py.typed +0 -0
- codeveto/redact.py +113 -0
- codeveto/render/__init__.py +64 -0
- codeveto/render/agent.py +43 -0
- codeveto/render/github.py +101 -0
- codeveto/render/json.py +23 -0
- codeveto/render/markdown.py +208 -0
- codeveto/render/safe.py +249 -0
- codeveto/render/sarif.py +217 -0
- codeveto/render/text.py +210 -0
- codeveto/report.py +252 -0
- codeveto/rules/__init__.py +84 -0
- codeveto/rules/agent.py +2972 -0
- codeveto/rules/base.py +1026 -0
- codeveto/rules/ci.py +1708 -0
- codeveto/rules/config.py +493 -0
- codeveto/rules/deps.py +1693 -0
- codeveto/rules/docker.py +479 -0
- codeveto/rules/examples/__init__.py +148 -0
- codeveto/rules/examples/agent.py +839 -0
- codeveto/rules/examples/ci.py +1046 -0
- codeveto/rules/examples/config.py +191 -0
- codeveto/rules/examples/deps.py +369 -0
- codeveto/rules/examples/docker.py +157 -0
- codeveto/rules/examples/go.py +241 -0
- codeveto/rules/examples/hygiene.py +466 -0
- codeveto/rules/examples/js.py +462 -0
- codeveto/rules/examples/python.py +595 -0
- codeveto/rules/examples/rust.py +268 -0
- codeveto/rules/examples/secrets.py +561 -0
- codeveto/rules/examples/security.py +608 -0
- codeveto/rules/examples/shell.py +174 -0
- codeveto/rules/examples/sql.py +194 -0
- codeveto/rules/examples/tests.py +497 -0
- codeveto/rules/go.py +328 -0
- codeveto/rules/hygiene.py +1094 -0
- codeveto/rules/js.py +768 -0
- codeveto/rules/python.py +1642 -0
- codeveto/rules/rust.py +209 -0
- codeveto/rules/secrets.py +1335 -0
- codeveto/rules/security.py +3680 -0
- codeveto/rules/shell.py +867 -0
- codeveto/rules/sql.py +629 -0
- codeveto/rules/tests.py +1404 -0
- codeveto/schema.py +384 -0
- codeveto/statements.py +607 -0
- codeveto/worktree.py +1176 -0
- codeveto/yamlish.py +1179 -0
- codeveto-0.1.0.dist-info/METADATA +253 -0
- codeveto-0.1.0.dist-info/RECORD +106 -0
- codeveto-0.1.0.dist-info/WHEEL +4 -0
- codeveto-0.1.0.dist-info/entry_points.txt +2 -0
- codeveto-0.1.0.dist-info/licenses/LICENSE +21 -0
codeveto/cli_review.py
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
"""``codeveto review``: intake, trusted config, engine, report, render (SPEC 5-8).
|
|
2
|
+
|
|
3
|
+
Order of work, so that nothing the change wrote can steer the review:
|
|
4
|
+
|
|
5
|
+
1. **Mode** from the flags: ``--base`` (range, with ``--head`` and
|
|
6
|
+
``--comparison``), ``--staged``, ``--commit``, else worktree.
|
|
7
|
+
2. **Trust anchor** (SPEC 7.1) before anything is read: ``--anchor`` if
|
|
8
|
+
given; for a range the merge base (``direct``: the base), for a commit its
|
|
9
|
+
first parent (the empty tree for a root commit), both computed from
|
|
10
|
+
hash-verified objects; for staged and worktree modes
|
|
11
|
+
:func:`codeveto.anchor.resolve_anchor`. Range and commit revisions are
|
|
12
|
+
resolved to full OIDs once, so the intake compares exactly what the
|
|
13
|
+
anchor was computed for.
|
|
14
|
+
3. **Config** (SPEC 7.2): the repository layer read from Git objects at the
|
|
15
|
+
anchor (unless ``--no-repo-config``, or a range without a unique merge
|
|
16
|
+
base, which is incomplete anyway), user config, ``--config``, then the
|
|
17
|
+
flags (``--fail-on``/``--strict``, ``--ai*``).
|
|
18
|
+
4. **Intake** with the effective limits, then the **engine** under a
|
|
19
|
+
watchdog: if :func:`codeveto.engine.review` runs past
|
|
20
|
+
:func:`codeveto.engine.hard_deadline_s`, one line goes to stderr and the
|
|
21
|
+
process exits 2 at once (a rule stuck in C code cannot be interrupted).
|
|
22
|
+
5. **Report** and **render** to stdout or ``--output`` (temp file + rename).
|
|
23
|
+
The exit code is the verdict's.
|
|
24
|
+
|
|
25
|
+
6. **AI pass** (SPEC 9), after the engine, only when ``[ai] enabled`` with a
|
|
26
|
+
network provider (``--ai claude|openai`` sets both; ``--ai none`` and
|
|
27
|
+
``--format agent`` turn it off): packets from the reviewed changeset,
|
|
28
|
+
:func:`codeveto.ai.runner.run` with the configured budgets, and the
|
|
29
|
+
verdict policy of :mod:`codeveto.ai.policy` (``blocking``, ``required``,
|
|
30
|
+
``--ai-optional``). Verified findings join the report with ``source:
|
|
31
|
+
"ai"``.
|
|
32
|
+
|
|
33
|
+
:func:`prepare` (steps 1-4 without the engine) is shared with ``codeveto
|
|
34
|
+
packets``, ``codeveto verify`` and :mod:`codeveto.ai.harness`.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
from __future__ import annotations
|
|
38
|
+
|
|
39
|
+
import argparse
|
|
40
|
+
import contextlib
|
|
41
|
+
import os
|
|
42
|
+
import shutil
|
|
43
|
+
import sys
|
|
44
|
+
import tempfile
|
|
45
|
+
import threading
|
|
46
|
+
import time
|
|
47
|
+
from collections.abc import Callable, Iterator
|
|
48
|
+
from dataclasses import dataclass
|
|
49
|
+
from pathlib import Path
|
|
50
|
+
from typing import Any
|
|
51
|
+
|
|
52
|
+
from codeveto import anchor as anchormod
|
|
53
|
+
from codeveto import engine, report, rules, worktree
|
|
54
|
+
from codeveto import git as gitmod
|
|
55
|
+
from codeveto.ai import packets as ai_packets
|
|
56
|
+
from codeveto.ai import policy as ai_policy
|
|
57
|
+
from codeveto.ai import providers as ai_providers
|
|
58
|
+
from codeveto.ai import runner as ai_runner
|
|
59
|
+
from codeveto.cli import error_line, write_stdout
|
|
60
|
+
from codeveto.config import NETWORK_PROVIDERS, Config, load_config
|
|
61
|
+
from codeveto.engine import EngineResult, Issue
|
|
62
|
+
from codeveto.git import DEFAULT_LIMITS, GitError, Limits
|
|
63
|
+
from codeveto.model import AnchorSource, Changeset
|
|
64
|
+
from codeveto.render import RenderOptions, get_renderer
|
|
65
|
+
from codeveto.rules.base import Registry
|
|
66
|
+
|
|
67
|
+
__all__ = ["Prepared", "prepare", "run", "usage_problem", "write_atomic"]
|
|
68
|
+
|
|
69
|
+
_UNICODE_PROBE = "✕▲·…"
|
|
70
|
+
#: How the watchdog ends the process; tests replace it.
|
|
71
|
+
_hard_exit: Callable[[int], object] = os._exit
|
|
72
|
+
#: The watchdog deadline in seconds; tests replace it.
|
|
73
|
+
_deadline: Callable[[], float] = engine.hard_deadline_s
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass(frozen=True, slots=True)
|
|
77
|
+
class _Plan:
|
|
78
|
+
"""What to compare and the anchor config is read from."""
|
|
79
|
+
|
|
80
|
+
mode: str
|
|
81
|
+
anchor: str | None
|
|
82
|
+
anchor_source: AnchorSource
|
|
83
|
+
anchor_notes: tuple[str, ...] = ()
|
|
84
|
+
base: str = ""
|
|
85
|
+
head: str = ""
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@dataclass(frozen=True, slots=True)
|
|
89
|
+
class Prepared:
|
|
90
|
+
"""Everything before the engine: the rules, the plan, trusted config and the changeset."""
|
|
91
|
+
|
|
92
|
+
repo: str
|
|
93
|
+
registry: Registry
|
|
94
|
+
plan: _Plan
|
|
95
|
+
config: Config
|
|
96
|
+
changeset: Changeset
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def usage_problem(args: argparse.Namespace) -> str | None:
|
|
100
|
+
"""A usage error in the revision flags (``None`` when they are consistent)."""
|
|
101
|
+
return _usage(args)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _usage(args: argparse.Namespace) -> str | None:
|
|
105
|
+
chosen = [
|
|
106
|
+
flag
|
|
107
|
+
for flag, on in (
|
|
108
|
+
("--base", args.base),
|
|
109
|
+
("--staged", args.staged),
|
|
110
|
+
("--commit", args.commit),
|
|
111
|
+
)
|
|
112
|
+
if on
|
|
113
|
+
]
|
|
114
|
+
if len(chosen) > 1:
|
|
115
|
+
return f"{' and '.join(chosen)} cannot be combined"
|
|
116
|
+
if args.base is None:
|
|
117
|
+
for flag, value in (("--head", args.head), ("--comparison", args.comparison)):
|
|
118
|
+
if value is not None:
|
|
119
|
+
return f"{flag} needs --base"
|
|
120
|
+
return None
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _mode(args: argparse.Namespace) -> str:
|
|
124
|
+
if args.base is not None:
|
|
125
|
+
return "range"
|
|
126
|
+
if args.commit is not None:
|
|
127
|
+
return "commit"
|
|
128
|
+
return "staged" if args.staged else "worktree"
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _plan(repo: str, args: argparse.Namespace, mode: str) -> _Plan:
|
|
132
|
+
if mode in ("staged", "worktree"):
|
|
133
|
+
resolved = anchormod.resolve_anchor(repo, explicit=args.anchor)
|
|
134
|
+
return _Plan(mode, resolved.oid, resolved.source, resolved.notes)
|
|
135
|
+
with gitmod._session(repo, DEFAULT_LIMITS) as git:
|
|
136
|
+
explicit = anchormod.anchor_commit(git, args.anchor) if args.anchor is not None else None
|
|
137
|
+
if mode == "commit":
|
|
138
|
+
target = git.resolve(args.commit)
|
|
139
|
+
parents = git.commit(target)[1]
|
|
140
|
+
default = parents[0] if parents else git.empty_tree
|
|
141
|
+
return _Plan(
|
|
142
|
+
mode, explicit or default, "explicit" if explicit else "parent", head=target
|
|
143
|
+
)
|
|
144
|
+
start, end = git.resolve(args.base), git.resolve(args.head or "HEAD")
|
|
145
|
+
compared: str | None = start
|
|
146
|
+
if (args.comparison or "merge-base") == "merge-base":
|
|
147
|
+
bases = git.merge_bases(start, end)
|
|
148
|
+
compared = bases[0] if len(bases) == 1 else None
|
|
149
|
+
return _Plan(
|
|
150
|
+
mode, explicit or compared, "explicit" if explicit else "base", base=start, head=end
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _intake(repo: str, args: argparse.Namespace, plan: _Plan, limits: Limits) -> Changeset:
|
|
155
|
+
if plan.mode == "range":
|
|
156
|
+
return gitmod.read_range(
|
|
157
|
+
repo,
|
|
158
|
+
plan.base,
|
|
159
|
+
plan.head,
|
|
160
|
+
comparison=args.comparison or "merge-base",
|
|
161
|
+
anchor=args.anchor,
|
|
162
|
+
limits=limits,
|
|
163
|
+
)
|
|
164
|
+
if plan.mode == "commit":
|
|
165
|
+
return gitmod.read_commit(repo, plan.head, anchor=args.anchor, limits=limits)
|
|
166
|
+
anchor = gitmod.Anchor(plan.anchor or "", plan.anchor_source) # always set here
|
|
167
|
+
if plan.mode == "staged":
|
|
168
|
+
return gitmod.read_staged(repo, anchor=anchor, limits=limits)
|
|
169
|
+
try:
|
|
170
|
+
return worktree.read_worktree(repo, anchor=anchor, limits=limits)
|
|
171
|
+
except GitError as exc:
|
|
172
|
+
if exc.reason != "unsupported-platform":
|
|
173
|
+
raise
|
|
174
|
+
return Changeset(
|
|
175
|
+
mode="worktree",
|
|
176
|
+
base=None,
|
|
177
|
+
head=None,
|
|
178
|
+
merge_base=None,
|
|
179
|
+
anchor=plan.anchor,
|
|
180
|
+
anchor_source=plan.anchor_source,
|
|
181
|
+
issues=(f"{exc}: run codeveto review --staged or codeveto review --base REF",),
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _overrides(args: argparse.Namespace) -> dict[str, Any]:
|
|
186
|
+
cli: dict[str, Any] = {}
|
|
187
|
+
if args.fail_on is not None:
|
|
188
|
+
cli["fail_on"] = args.fail_on
|
|
189
|
+
if args.strict:
|
|
190
|
+
cli["fail_on"] = "warning"
|
|
191
|
+
ai: dict[str, Any] = {}
|
|
192
|
+
if args.ai is not None:
|
|
193
|
+
ai["provider"] = args.ai
|
|
194
|
+
if args.ai != "none":
|
|
195
|
+
ai["enabled"] = True
|
|
196
|
+
elif args.format == "agent":
|
|
197
|
+
ai["provider"] = "none" # SPEC 9.6: --format agent defaults to --ai none
|
|
198
|
+
if args.ai_model is not None:
|
|
199
|
+
ai["model"] = args.ai_model
|
|
200
|
+
if args.ai_base_url is not None:
|
|
201
|
+
ai["base_url"] = args.ai_base_url
|
|
202
|
+
if ai:
|
|
203
|
+
cli["ai"] = ai
|
|
204
|
+
return cli
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
@contextlib.contextmanager
|
|
208
|
+
def _watchdog(seconds: float) -> Iterator[None]:
|
|
209
|
+
def fire() -> None:
|
|
210
|
+
error_line(f"rule run exceeded its hard deadline ({seconds:g}s); the review is incomplete")
|
|
211
|
+
with contextlib.suppress(Exception):
|
|
212
|
+
sys.stdout.flush()
|
|
213
|
+
_hard_exit(2)
|
|
214
|
+
|
|
215
|
+
timer = threading.Timer(seconds, fire)
|
|
216
|
+
timer.daemon = True
|
|
217
|
+
timer.start()
|
|
218
|
+
try:
|
|
219
|
+
yield
|
|
220
|
+
finally:
|
|
221
|
+
timer.cancel()
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _isatty(stream: object) -> bool:
|
|
225
|
+
try:
|
|
226
|
+
return bool(stream.isatty()) # type: ignore[attr-defined]
|
|
227
|
+
except (AttributeError, ValueError, OSError):
|
|
228
|
+
return False
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _options(args: argparse.Namespace) -> RenderOptions:
|
|
232
|
+
to_file = args.output is not None
|
|
233
|
+
tty = not to_file and _isatty(sys.stdout)
|
|
234
|
+
if args.color == "always":
|
|
235
|
+
color = True
|
|
236
|
+
elif args.color == "never":
|
|
237
|
+
color = False
|
|
238
|
+
else:
|
|
239
|
+
color = tty and not os.environ.get("NO_COLOR")
|
|
240
|
+
unicode = True
|
|
241
|
+
if not to_file:
|
|
242
|
+
encoding = getattr(sys.stdout, "encoding", None) or "ascii"
|
|
243
|
+
try:
|
|
244
|
+
_UNICODE_PROBE.encode(encoding)
|
|
245
|
+
except (UnicodeEncodeError, LookupError):
|
|
246
|
+
unicode = False
|
|
247
|
+
width = shutil.get_terminal_size().columns if tty else None
|
|
248
|
+
return RenderOptions(color=color, unicode=unicode, width=width)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def write_atomic(path: str, text: str) -> None:
|
|
252
|
+
"""Write ``text`` to ``path`` through a temp file and a rename."""
|
|
253
|
+
_write_atomic(path, text)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _write_atomic(path: str, text: str) -> None:
|
|
257
|
+
target = Path(path)
|
|
258
|
+
directory = target.parent if str(target.parent) else Path(".")
|
|
259
|
+
fd, tmp = tempfile.mkstemp(prefix=f".{target.name}.", suffix=".tmp", dir=directory)
|
|
260
|
+
try:
|
|
261
|
+
with os.fdopen(fd, "w", encoding="utf-8", newline="\n") as handle:
|
|
262
|
+
handle.write(text)
|
|
263
|
+
handle.flush()
|
|
264
|
+
os.fsync(handle.fileno())
|
|
265
|
+
umask = os.umask(0)
|
|
266
|
+
os.umask(umask)
|
|
267
|
+
os.chmod(tmp, 0o666 & ~umask)
|
|
268
|
+
os.replace(tmp, target)
|
|
269
|
+
except BaseException:
|
|
270
|
+
with contextlib.suppress(OSError):
|
|
271
|
+
os.unlink(tmp)
|
|
272
|
+
raise
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def prepare(args: argparse.Namespace, *, overrides: dict[str, Any] | None = None) -> Prepared:
|
|
276
|
+
"""Mode, anchor, trusted config and intake for ``args`` (revision flags, ``--repo``).
|
|
277
|
+
|
|
278
|
+
``--config`` and ``--no-repo-config`` are read when ``args`` has them;
|
|
279
|
+
``overrides`` are the command-line config overrides.
|
|
280
|
+
"""
|
|
281
|
+
repo = args.repo or "."
|
|
282
|
+
mode = _mode(args)
|
|
283
|
+
registry = rules.load_rules()
|
|
284
|
+
plan = _plan(repo, args, mode)
|
|
285
|
+
config = load_config(
|
|
286
|
+
registry,
|
|
287
|
+
repo=repo,
|
|
288
|
+
anchor=plan.anchor,
|
|
289
|
+
no_repo_config=getattr(args, "no_repo_config", False) or plan.anchor is None,
|
|
290
|
+
config_file=getattr(args, "config", None),
|
|
291
|
+
cli=overrides or {},
|
|
292
|
+
)
|
|
293
|
+
changeset = _intake(repo, args, plan, config.limits)
|
|
294
|
+
return Prepared(repo, registry, plan, config, changeset)
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def _ai_pass(
|
|
298
|
+
args: argparse.Namespace, prepared: Prepared, result: EngineResult
|
|
299
|
+
) -> tuple[dict[str, Any], list[Issue], list[dict[str, Any]]]:
|
|
300
|
+
"""(report ``ai`` section, issues, AI findings) of the configured AI pass."""
|
|
301
|
+
settings = prepared.config.ai
|
|
302
|
+
if not settings.enabled or settings.provider not in NETWORK_PROVIDERS:
|
|
303
|
+
return report.ai_off(settings.provider), [], []
|
|
304
|
+
packet_set = ai_packets.build(
|
|
305
|
+
prepared.changeset,
|
|
306
|
+
registry=prepared.registry,
|
|
307
|
+
file_class=prepared.config.policy().file_class,
|
|
308
|
+
max_packets=settings.max_packets,
|
|
309
|
+
)
|
|
310
|
+
try:
|
|
311
|
+
provider = ai_providers.create(
|
|
312
|
+
settings.provider, settings, response_format=args.ai_response_format
|
|
313
|
+
)
|
|
314
|
+
except ai_providers.ProviderUnavailable as exc:
|
|
315
|
+
run = ai_runner.failed_to_start(packet_set, str(exc), provider=settings.provider)
|
|
316
|
+
else:
|
|
317
|
+
run = ai_runner.run(provider, packet_set, settings)
|
|
318
|
+
found, _ = ai_policy.ai_findings(
|
|
319
|
+
run.verified,
|
|
320
|
+
blocking=settings.blocking,
|
|
321
|
+
fail_on=result.fail_on,
|
|
322
|
+
rule_findings=result.findings,
|
|
323
|
+
registry=prepared.registry,
|
|
324
|
+
)
|
|
325
|
+
issues = ai_policy.issues(
|
|
326
|
+
run, blocking=settings.blocking, required=settings.required, optional=args.ai_optional
|
|
327
|
+
)
|
|
328
|
+
return ai_policy.section(run), issues, found
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def run(args: argparse.Namespace, *, clock: Callable[[], float] = time.perf_counter) -> int:
|
|
332
|
+
started = clock()
|
|
333
|
+
problem = _usage(args)
|
|
334
|
+
if problem is not None:
|
|
335
|
+
error_line(f"usage: {problem}")
|
|
336
|
+
return 2
|
|
337
|
+
prepared = prepare(args, overrides=_overrides(args))
|
|
338
|
+
changeset, config = prepared.changeset, prepared.config
|
|
339
|
+
with _watchdog(_deadline()):
|
|
340
|
+
result = engine.review(changeset, registry=prepared.registry, policy=config.policy())
|
|
341
|
+
ai_section, ai_issues, ai_found = _ai_pass(args, prepared, result)
|
|
342
|
+
built = report.build_report(
|
|
343
|
+
result,
|
|
344
|
+
changeset,
|
|
345
|
+
started=started,
|
|
346
|
+
clock=clock,
|
|
347
|
+
anchor_notes=prepared.plan.anchor_notes,
|
|
348
|
+
config_notes=config.config_notes,
|
|
349
|
+
extra_issues=ai_issues,
|
|
350
|
+
ai=ai_section,
|
|
351
|
+
ai_findings=ai_found,
|
|
352
|
+
)
|
|
353
|
+
try:
|
|
354
|
+
output = get_renderer(args.format)(built, _options(args))
|
|
355
|
+
except NotImplementedError:
|
|
356
|
+
error_line(f"--format {args.format} is not implemented yet; use text, json or agent")
|
|
357
|
+
return 2
|
|
358
|
+
if args.output is not None:
|
|
359
|
+
_write_atomic(args.output, output)
|
|
360
|
+
else:
|
|
361
|
+
write_stdout(output)
|
|
362
|
+
return int(built["exit_code"])
|
codeveto/cli_rules.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""``codeveto rules [--format text|json] [--pack NAME]``: list the registered rules.
|
|
2
|
+
|
|
3
|
+
Text: one rule per line, ``<id> <severity> <title>``, aligned, with
|
|
4
|
+
``(off by default)`` and ``(non-waivable)`` marks, then a count. JSON: an
|
|
5
|
+
object ``{"rules": [...]}`` with every rule's metadata, sorted by ID. Shows
|
|
6
|
+
the rules as shipped; configuration does not apply here.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import json
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
from codeveto import __version__, rules
|
|
16
|
+
from codeveto.cli import error_line, write_stdout
|
|
17
|
+
from codeveto.render import safe
|
|
18
|
+
from codeveto.rules.base import RuleMeta
|
|
19
|
+
|
|
20
|
+
__all__ = ["meta_dict", "run"]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _sorted(values: frozenset[str] | None) -> list[str] | None:
|
|
24
|
+
return None if values is None else sorted(values)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def meta_dict(meta: RuleMeta) -> dict[str, Any]:
|
|
28
|
+
"""A rule's metadata as JSON-ready data."""
|
|
29
|
+
return {
|
|
30
|
+
"id": meta.id,
|
|
31
|
+
"pack": meta.pack,
|
|
32
|
+
"severity": str(meta.severity),
|
|
33
|
+
"title": meta.title,
|
|
34
|
+
"scope": str(meta.scope),
|
|
35
|
+
"languages": _sorted(meta.languages),
|
|
36
|
+
"classes": _sorted(meta.classes),
|
|
37
|
+
"kinds": sorted(str(k) for k in meta.kinds),
|
|
38
|
+
"introduced_only": meta.introduced_only,
|
|
39
|
+
"default_enabled": meta.default_enabled,
|
|
40
|
+
"non_waivable": meta.non_waivable,
|
|
41
|
+
"tags": sorted(meta.tags),
|
|
42
|
+
"rationale": meta.rationale,
|
|
43
|
+
"hint": meta.hint,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def run(args: argparse.Namespace) -> int:
|
|
48
|
+
registry = rules.load_rules()
|
|
49
|
+
selected = registry.all() if args.pack is None else registry.by_pack(args.pack)
|
|
50
|
+
if args.pack is not None and not selected:
|
|
51
|
+
packs = ", ".join(sorted({r.meta.pack for r in registry.all()}))
|
|
52
|
+
error_line(f"unknown pack {args.pack!r} (packs: {packs})")
|
|
53
|
+
return 2
|
|
54
|
+
if args.format == "json":
|
|
55
|
+
data = {
|
|
56
|
+
"tool": {"name": "codeveto", "version": __version__},
|
|
57
|
+
"rules": [meta_dict(r.meta) for r in selected],
|
|
58
|
+
}
|
|
59
|
+
write_stdout(safe.json_text(json.dumps(data, indent=2, ensure_ascii=False)) + "\n")
|
|
60
|
+
return 0
|
|
61
|
+
width = max((len(r.id) for r in selected), default=0)
|
|
62
|
+
lines: list[str] = []
|
|
63
|
+
for entry in selected:
|
|
64
|
+
meta = entry.meta
|
|
65
|
+
marks = "".join(
|
|
66
|
+
mark
|
|
67
|
+
for mark, on in (
|
|
68
|
+
(" (off by default)", not meta.default_enabled),
|
|
69
|
+
(" (non-waivable)", meta.non_waivable),
|
|
70
|
+
)
|
|
71
|
+
if on
|
|
72
|
+
)
|
|
73
|
+
title = safe.text(meta.title)
|
|
74
|
+
lines.append(f"{safe.text(meta.id):<{width}} {meta.severity!s:<7} {title}{marks}")
|
|
75
|
+
packs = len({r.meta.pack for r in selected})
|
|
76
|
+
lines += ["", f"{len(selected)} rules in {packs} {'pack' if packs == 1 else 'packs'}"]
|
|
77
|
+
write_stdout("\n".join(lines) + "\n")
|
|
78
|
+
return 0
|
codeveto/cli_verify.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""``codeveto verify``: verify a harness's AI responses (SPEC 9.3, 9.4 step 2).
|
|
2
|
+
|
|
3
|
+
Reads ``--responses`` as a regular file (``O_NONBLOCK``, at most 2 MiB), then
|
|
4
|
+
runs a fresh review of the selected change (same flags, anchor and trusted
|
|
5
|
+
config as ``review``), regenerates its packets and verifies the responses
|
|
6
|
+
against them (:mod:`codeveto.ai.harness`). The output is the JSON report v1
|
|
7
|
+
with the verified AI findings and the ``ai`` section; the exit code is its
|
|
8
|
+
verdict's (0 clear, 1 vetoed, 2 incomplete, also for an unreadable file).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import argparse
|
|
14
|
+
import time
|
|
15
|
+
from collections.abc import Callable
|
|
16
|
+
|
|
17
|
+
from codeveto import cli_review, engine
|
|
18
|
+
from codeveto.ai import harness
|
|
19
|
+
from codeveto.cli import error_line, write_stdout
|
|
20
|
+
from codeveto.render import RenderOptions, get_renderer
|
|
21
|
+
|
|
22
|
+
__all__ = ["run"]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def run(args: argparse.Namespace, *, clock: Callable[[], float] = time.perf_counter) -> int:
|
|
26
|
+
"""Verify the responses file against the fresh packets and print the report."""
|
|
27
|
+
started = clock()
|
|
28
|
+
problem = cli_review.usage_problem(args)
|
|
29
|
+
if problem is not None:
|
|
30
|
+
error_line(f"usage: {problem}")
|
|
31
|
+
return 2
|
|
32
|
+
try:
|
|
33
|
+
raw = harness.read_responses_file(args.responses)
|
|
34
|
+
except harness.HarnessError as exc:
|
|
35
|
+
error_line(f"--responses: {exc}")
|
|
36
|
+
return 2
|
|
37
|
+
except OSError as exc:
|
|
38
|
+
error_line(f"--responses: cannot read the file ({exc.strerror or type(exc).__name__})")
|
|
39
|
+
return 2
|
|
40
|
+
prepared = cli_review.prepare(args)
|
|
41
|
+
with cli_review._watchdog(cli_review._deadline()):
|
|
42
|
+
result = engine.review(
|
|
43
|
+
prepared.changeset, registry=prepared.registry, policy=prepared.config.policy()
|
|
44
|
+
)
|
|
45
|
+
built = harness.review_with_responses(prepared, result, raw, started=started, clock=clock)
|
|
46
|
+
write_stdout(get_renderer("json")(built, RenderOptions()))
|
|
47
|
+
return int(built["exit_code"])
|