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.
Files changed (106) hide show
  1. codeveto/__init__.py +3 -0
  2. codeveto/__main__.py +8 -0
  3. codeveto/ai/__init__.py +19 -0
  4. codeveto/ai/harness.py +303 -0
  5. codeveto/ai/packets.py +855 -0
  6. codeveto/ai/policy.py +222 -0
  7. codeveto/ai/prompt.py +101 -0
  8. codeveto/ai/providers/__init__.py +178 -0
  9. codeveto/ai/providers/claude.py +247 -0
  10. codeveto/ai/providers/openai_compat.py +246 -0
  11. codeveto/ai/runner.py +293 -0
  12. codeveto/ai/verify.py +324 -0
  13. codeveto/anchor.py +211 -0
  14. codeveto/cli.py +247 -0
  15. codeveto/cli_explain.py +49 -0
  16. codeveto/cli_hook.py +31 -0
  17. codeveto/cli_init.py +254 -0
  18. codeveto/cli_mcp.py +29 -0
  19. codeveto/cli_packets.py +35 -0
  20. codeveto/cli_publish.py +29 -0
  21. codeveto/cli_review.py +362 -0
  22. codeveto/cli_rules.py +78 -0
  23. codeveto/cli_verify.py +47 -0
  24. codeveto/config.py +1229 -0
  25. codeveto/data/README.md +16 -0
  26. codeveto/data/ai-wire.schema.json +53 -0
  27. codeveto/data/prompts/system.md +57 -0
  28. codeveto/data/report-v1.schema.json +268 -0
  29. codeveto/data/templates/forgejo-codeveto.yml +104 -0
  30. codeveto/data/templates/github-codeveto.yml +18 -0
  31. codeveto/embedded.py +311 -0
  32. codeveto/engine.py +1570 -0
  33. codeveto/git.py +1549 -0
  34. codeveto/globs.py +348 -0
  35. codeveto/integrations/__init__.py +1 -0
  36. codeveto/integrations/hooks.py +327 -0
  37. codeveto/integrations/mcp.py +770 -0
  38. codeveto/integrations/mcp_ai.py +77 -0
  39. codeveto/integrations/prepush.py +222 -0
  40. codeveto/jsonsafe.py +60 -0
  41. codeveto/languages.py +539 -0
  42. codeveto/lexers.py +2560 -0
  43. codeveto/model.py +372 -0
  44. codeveto/publish/__init__.py +28 -0
  45. codeveto/publish/action.py +330 -0
  46. codeveto/publish/common.py +84 -0
  47. codeveto/publish/diff.py +208 -0
  48. codeveto/publish/event.py +241 -0
  49. codeveto/publish/forgejo.py +184 -0
  50. codeveto/publish/github.py +380 -0
  51. codeveto/publish/http.py +263 -0
  52. codeveto/publish/pull.py +218 -0
  53. codeveto/publish/untrusted.py +110 -0
  54. codeveto/py.typed +0 -0
  55. codeveto/redact.py +113 -0
  56. codeveto/render/__init__.py +64 -0
  57. codeveto/render/agent.py +43 -0
  58. codeveto/render/github.py +101 -0
  59. codeveto/render/json.py +23 -0
  60. codeveto/render/markdown.py +208 -0
  61. codeveto/render/safe.py +249 -0
  62. codeveto/render/sarif.py +217 -0
  63. codeveto/render/text.py +210 -0
  64. codeveto/report.py +252 -0
  65. codeveto/rules/__init__.py +84 -0
  66. codeveto/rules/agent.py +2972 -0
  67. codeveto/rules/base.py +1026 -0
  68. codeveto/rules/ci.py +1708 -0
  69. codeveto/rules/config.py +493 -0
  70. codeveto/rules/deps.py +1693 -0
  71. codeveto/rules/docker.py +479 -0
  72. codeveto/rules/examples/__init__.py +148 -0
  73. codeveto/rules/examples/agent.py +839 -0
  74. codeveto/rules/examples/ci.py +1046 -0
  75. codeveto/rules/examples/config.py +191 -0
  76. codeveto/rules/examples/deps.py +369 -0
  77. codeveto/rules/examples/docker.py +157 -0
  78. codeveto/rules/examples/go.py +241 -0
  79. codeveto/rules/examples/hygiene.py +466 -0
  80. codeveto/rules/examples/js.py +462 -0
  81. codeveto/rules/examples/python.py +595 -0
  82. codeveto/rules/examples/rust.py +268 -0
  83. codeveto/rules/examples/secrets.py +561 -0
  84. codeveto/rules/examples/security.py +608 -0
  85. codeveto/rules/examples/shell.py +174 -0
  86. codeveto/rules/examples/sql.py +194 -0
  87. codeveto/rules/examples/tests.py +497 -0
  88. codeveto/rules/go.py +328 -0
  89. codeveto/rules/hygiene.py +1094 -0
  90. codeveto/rules/js.py +768 -0
  91. codeveto/rules/python.py +1642 -0
  92. codeveto/rules/rust.py +209 -0
  93. codeveto/rules/secrets.py +1335 -0
  94. codeveto/rules/security.py +3680 -0
  95. codeveto/rules/shell.py +867 -0
  96. codeveto/rules/sql.py +629 -0
  97. codeveto/rules/tests.py +1404 -0
  98. codeveto/schema.py +384 -0
  99. codeveto/statements.py +607 -0
  100. codeveto/worktree.py +1176 -0
  101. codeveto/yamlish.py +1179 -0
  102. codeveto-0.1.0.dist-info/METADATA +253 -0
  103. codeveto-0.1.0.dist-info/RECORD +106 -0
  104. codeveto-0.1.0.dist-info/WHEEL +4 -0
  105. codeveto-0.1.0.dist-info/entry_points.txt +2 -0
  106. codeveto-0.1.0.dist-info/licenses/LICENSE +21 -0
codeveto/embedded.py ADDED
@@ -0,0 +1,311 @@
1
+ """Scripts embedded in other files (``FileContext.embedded_scripts()``).
2
+
3
+ The masked text of a workflow or Dockerfile blanks the scripts inside it (a
4
+ YAML block scalar, a heredoc body), so rules that look at the commands read
5
+ them here instead:
6
+
7
+ - workflow ``run:`` values (``jobs.<id>.steps[*]`` and a composite action's
8
+ ``runs.steps[*]``). The language comes from the step's ``shell:``, else
9
+ ``defaults.run.shell`` of the job, else of the workflow: ``bash``/``sh`` (and
10
+ no shell at all) are ``shell``, ``python`` is ``python``, anything else
11
+ (``pwsh``, ``cmd``, a custom command) is ``text``.
12
+ - Dockerfile ``RUN`` commands in shell form, with line continuations
13
+ removed as Docker removes them (escape directive honoured, blank and comment
14
+ lines inside a continuation dropped, leading ``--flags`` skipped). They are
15
+ ``shell`` unless a ``SHELL`` instruction chose a non-POSIX shell (``text``).
16
+ - the heredoc bodies of ``RUN`` instructions. A shebang on the body's first
17
+ line, else an interpreter as the command (``RUN python3 <<EOF``) decides
18
+ the language; a bare ``RUN <<EOF`` is the instruction's shell; a heredoc
19
+ fed to another command (``RUN cat <<EOF > file``) is data, ``text``.
20
+
21
+ Every :class:`Embedded` maps offsets of its ``text`` back to exact file lines.
22
+ """
23
+
24
+ from __future__ import annotations
25
+
26
+ import bisect
27
+ import re
28
+ from dataclasses import dataclass
29
+
30
+ from codeveto import languages
31
+ from codeveto.lexers import LexResult, LineIndex, StringSpan, docker_escape
32
+ from codeveto.yamlish import YamlMap, YamlNode, YamlResult, YamlScalar, YamlSeq
33
+
34
+ __all__ = ["Embedded", "dockerfile_scripts", "workflow_scripts"]
35
+
36
+
37
+ @dataclass(frozen=True, slots=True)
38
+ class Embedded:
39
+ """A script inside another file.
40
+
41
+ ``source`` is ``workflow-run``, ``dockerfile-run`` or
42
+ ``dockerfile-heredoc``; ``shell`` is the declared shell or interpreter
43
+ word when there is one. ``line_map`` holds ``(offset in text, file line)``
44
+ pairs where each source line's text starts.
45
+ """
46
+
47
+ language: str
48
+ text: str
49
+ source: str
50
+ start_line: int
51
+ end_line: int
52
+ line_map: tuple[tuple[int, int], ...]
53
+ shell: str | None = None
54
+
55
+ def line_of(self, offset: int) -> int:
56
+ """1-based file line of ``text[offset]``."""
57
+ k = bisect.bisect_right(self.line_map, (offset, 1 << 62)) - 1
58
+ return self.line_map[max(k, 0)][1] if self.line_map else self.start_line
59
+
60
+
61
+ # --- Workflows --------------------------------------------------------------------
62
+
63
+
64
+ def _scalar(node: YamlNode | None) -> YamlScalar | None:
65
+ return node if isinstance(node, YamlScalar) else None
66
+
67
+
68
+ def _map(node: YamlNode | None) -> YamlMap | None:
69
+ return node if isinstance(node, YamlMap) else None
70
+
71
+
72
+ def _default_shell(owner: YamlMap | None, inherited: str | None) -> str | None:
73
+ defaults = _map(owner.get("defaults")) if owner is not None else None
74
+ run = _map(defaults.get("run")) if defaults is not None else None
75
+ shell = _scalar(run.get("shell")) if run is not None else None
76
+ return shell.value if shell is not None and shell.value else inherited
77
+
78
+
79
+ def _program(command: str) -> str:
80
+ words = command.split()
81
+ return words[0].replace("\\", "/").rsplit("/", 1)[-1].lower() if words else ""
82
+
83
+
84
+ def _shell_language(shell: str | None) -> str:
85
+ if shell is None:
86
+ return "shell"
87
+ program = _program(shell).removesuffix(".exe")
88
+ if program in ("bash", "sh"):
89
+ return "shell"
90
+ if program.rstrip("0123456789.") == "python":
91
+ return "python"
92
+ return "text"
93
+
94
+
95
+ def _steps(steps: YamlNode | None, shell: str | None, out: list[Embedded]) -> None:
96
+ if not isinstance(steps, YamlSeq):
97
+ return
98
+ for item in steps.items:
99
+ step = _map(item)
100
+ if step is None:
101
+ continue
102
+ run = _scalar(step.get("run"))
103
+ if run is None or not run.value:
104
+ continue
105
+ own = _scalar(step.get("shell"))
106
+ chosen = own.value if own is not None and own.value else shell
107
+ line_map = run.line_map or ((0, run.start_line),)
108
+ out.append(
109
+ Embedded(
110
+ language=_shell_language(chosen),
111
+ text=run.value,
112
+ source="workflow-run",
113
+ start_line=line_map[0][1],
114
+ end_line=run.end_line,
115
+ line_map=line_map,
116
+ shell=chosen,
117
+ )
118
+ )
119
+
120
+
121
+ def workflow_scripts(result: YamlResult | None) -> tuple[Embedded, ...]:
122
+ """The ``run:`` scripts of a workflow or composite action, in file order."""
123
+ if result is None:
124
+ return ()
125
+ out: list[Embedded] = []
126
+ for document in result.documents:
127
+ root = _map(document.root)
128
+ if root is None:
129
+ continue
130
+ top = _default_shell(root, None)
131
+ jobs = _map(root.get("jobs"))
132
+ if jobs is not None:
133
+ for entry in jobs.entries:
134
+ job = _map(entry.value)
135
+ if job is not None:
136
+ _steps(job.get("steps"), _default_shell(job, top), out)
137
+ runs = _map(root.get("runs"))
138
+ if runs is not None:
139
+ _steps(runs.get("steps"), top, out)
140
+ out.sort(key=lambda e: e.start_line)
141
+ return tuple(out)
142
+
143
+
144
+ # --- Dockerfiles ---------------------------------------------------------------------
145
+
146
+ _WORD = re.compile(r"[A-Za-z]+")
147
+ _SHELL_ARG = re.compile(r'[ \t]*\[[ \t]*"([^"\r\n]*)"')
148
+ _POSIX = frozenset({"sh", "bash", "ash", "dash", "zsh", "ksh", "mksh", "busybox"})
149
+
150
+
151
+ def _line_end(text: str, pos: int) -> tuple[int, int]:
152
+ """``(end of the line's content, start of the next line)`` for ``pos``."""
153
+ nl = text.find("\n", pos)
154
+ if nl < 0:
155
+ return len(text), len(text)
156
+ end = nl - 1 if nl > pos and text[nl - 1] == "\r" else nl
157
+ return end, nl + 1
158
+
159
+
160
+ def _blank_or_comment(text: str, start: int, end: int) -> bool:
161
+ stripped = text[start:end].lstrip(" \t")
162
+ return not stripped or stripped.startswith("#")
163
+
164
+
165
+ def _heredoc_language(command: str, body: str, posix: bool) -> tuple[str, str | None]:
166
+ from_shebang = languages.shebang_language(body)
167
+ if from_shebang is not None:
168
+ return from_shebang, None
169
+ program = _program(command)
170
+ if program.startswith("<<"):
171
+ return ("shell" if posix else "text"), None
172
+ found = languages.shebang_language(f"#!{program}\n") if program else None
173
+ if found is not None:
174
+ return found, program
175
+ return "text", program or None
176
+
177
+
178
+ def dockerfile_scripts(text: str, result: LexResult) -> tuple[Embedded, ...]:
179
+ """``RUN`` commands and ``RUN`` heredoc bodies of a Dockerfile, in order.
180
+
181
+ ``result`` is the Dockerfile's lexer output; its heredoc spans say where
182
+ the bodies are, so their lines are never read as instructions.
183
+ """
184
+ if not text:
185
+ return ()
186
+ esc = docker_escape(text)
187
+ index = LineIndex(text)
188
+ bodies = sorted(
189
+ (s for s in result.spans if isinstance(s, StringSpan) and s.kind == "heredoc"),
190
+ key=lambda s: s.start,
191
+ )
192
+ body_starts = [s.start for s in bodies]
193
+ out: list[Embedded] = []
194
+ posix = True
195
+ owner: tuple[str, str] | None = None # (keyword, command) of the last instruction
196
+ pos = 1 if text.startswith("") else 0
197
+ n = len(text)
198
+ while pos < n:
199
+ k = bisect.bisect_left(body_starts, pos)
200
+ if k < len(bodies) and bodies[k].start == pos:
201
+ span = bodies[k]
202
+ if owner is not None and owner[0] == "RUN":
203
+ _add_heredoc(text, index, span, owner[1], posix, out)
204
+ pos = _line_end(text, span.end)[1] if span.end < n else n
205
+ continue
206
+ end, nxt = _line_end(text, pos)
207
+ if _blank_or_comment(text, pos, end):
208
+ pos = nxt
209
+ continue
210
+ # One instruction: its lines up to one that does not end in the escape.
211
+ pieces: list[tuple[int, int]] = []
212
+ start = pos
213
+ while True:
214
+ end, nxt = _line_end(text, start)
215
+ content_end = start + len(text[start:end].rstrip(" \t"))
216
+ if content_end > start and text[content_end - 1] == esc:
217
+ pieces.append((start, content_end - 1))
218
+ start = nxt
219
+ while start < n: # blank and comment lines inside it are dropped
220
+ blank_end, after = _line_end(text, start)
221
+ if not _blank_or_comment(text, start, blank_end):
222
+ break
223
+ start = after
224
+ if start >= n:
225
+ pos = n
226
+ break
227
+ continue
228
+ pieces.append((start, end))
229
+ pos = nxt
230
+ break
231
+ keyword, command_pieces = _instruction(text, pieces)
232
+ if keyword == "SHELL":
233
+ q = _SHELL_ARG.match(text, pieces[0][0] + len("SHELL"), pieces[0][1])
234
+ if q is not None:
235
+ posix = _program(q.group(1)).removesuffix(".exe") in _POSIX
236
+ command = "".join(text[a:b] for a, b in command_pieces)
237
+ owner = (keyword, command)
238
+ if keyword == "RUN" and command_pieces and not command.lstrip(" \t").startswith("["):
239
+ line_map: list[tuple[int, int]] = []
240
+ length = 0
241
+ for a, b in command_pieces:
242
+ line_map.append((length, index.line_of(a)))
243
+ length += b - a
244
+ out.append(
245
+ Embedded(
246
+ language="shell" if posix else "text",
247
+ text=command,
248
+ source="dockerfile-run",
249
+ start_line=line_map[0][1],
250
+ end_line=index.line_of(max(command_pieces[-1][1] - 1, command_pieces[-1][0])),
251
+ line_map=tuple(line_map),
252
+ )
253
+ )
254
+ return tuple(out)
255
+
256
+
257
+ def _instruction(text: str, pieces: list[tuple[int, int]]) -> tuple[str, list[tuple[int, int]]]:
258
+ """The keyword (``ONBUILD`` unwrapped) and the command's pieces after it."""
259
+ if not pieces:
260
+ return "", []
261
+ first_start, first_end = pieces[0]
262
+ i = first_start
263
+ while i < first_end and text[i] in " \t":
264
+ i += 1
265
+ word = _WORD.match(text, i, first_end)
266
+ keyword = word.group().upper() if word is not None else ""
267
+ i = word.end() if word is not None else i
268
+ if keyword == "ONBUILD":
269
+ while i < first_end and text[i] in " \t":
270
+ i += 1
271
+ inner = _WORD.match(text, i, first_end)
272
+ if inner is not None:
273
+ keyword, i = inner.group().upper(), inner.end()
274
+ # Skip whitespace and RUN flags such as --mount=type=cache,target=/x.
275
+ while True:
276
+ while i < first_end and text[i] in " \t":
277
+ i += 1
278
+ if keyword == "RUN" and text.startswith("--", i):
279
+ while i < first_end and text[i] not in " \t":
280
+ i += 1
281
+ continue
282
+ break
283
+ rest = [(i, first_end), *pieces[1:]]
284
+ return keyword, [(a, b) for a, b in rest if b > a]
285
+
286
+
287
+ def _add_heredoc(
288
+ text: str, index: LineIndex, span: StringSpan, command: str, posix: bool, out: list[Embedded]
289
+ ) -> None:
290
+ body = text[span.content_start : span.content_end]
291
+ if not body:
292
+ return
293
+ language, program = _heredoc_language(command, body, posix)
294
+ line_map: list[tuple[int, int]] = []
295
+ offset = 0
296
+ for row in body.split("\n"):
297
+ if offset >= len(body):
298
+ break
299
+ line_map.append((offset, index.line_of(span.content_start + offset)))
300
+ offset += len(row) + 1
301
+ out.append(
302
+ Embedded(
303
+ language=language,
304
+ text=body,
305
+ source="dockerfile-heredoc",
306
+ start_line=line_map[0][1],
307
+ end_line=index.line_of(max(span.content_end - 1, span.content_start)),
308
+ line_map=tuple(line_map),
309
+ shell=program,
310
+ )
311
+ )