codecop 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.
- codecop/__init__.py +7 -0
- codecop/access.py +140 -0
- codecop/access_postgres.py +284 -0
- codecop/advisor.py +1512 -0
- codecop/advisor_bench.py +493 -0
- codecop/advisor_ch.py +2080 -0
- codecop/advisor_view.py +381 -0
- codecop/advisors.py +184 -0
- codecop/agent.py +221 -0
- codecop/agentfacts.py +745 -0
- codecop/apply/__init__.py +327 -0
- codecop/apply/check.py +344 -0
- codecop/apply/cli.py +162 -0
- codecop/apply/clickhouse.py +379 -0
- codecop/apply/common.py +238 -0
- codecop/apply/html.py +264 -0
- codecop/apply/patch.py +213 -0
- codecop/apply/postgres.py +102 -0
- codecop/apply/trial.py +166 -0
- codecop/assemble.py +104 -0
- codecop/baseline.py +1043 -0
- codecop/bench.py +2464 -0
- codecop/blockers.py +935 -0
- codecop/budget.py +980 -0
- codecop/catalog_profile.py +525 -0
- codecop/changed.py +261 -0
- codecop/cicmd.py +1002 -0
- codecop/cli.py +5421 -0
- codecop/client.py +729 -0
- codecop/coverage.py +1067 -0
- codecop/coverage_reasons.py +116 -0
- codecop/crashlog.py +70 -0
- codecop/datagen.py +766 -0
- codecop/datagen_postgres.py +630 -0
- codecop/datagen_sqlite.py +323 -0
- codecop/dbcli.py +885 -0
- codecop/definitions.py +457 -0
- codecop/discover/__init__.py +702 -0
- codecop/discover/arms.py +160 -0
- codecop/discover/base.py +234 -0
- codecop/discover/dialect.py +296 -0
- codecop/discover/elixir.py +64 -0
- codecop/discover/engine_go.py +320 -0
- codecop/discover/go.py +1947 -0
- codecop/discover/java.py +1528 -0
- codecop/discover/loops.py +1671 -0
- codecop/discover/named_go.py +258 -0
- codecop/discover/origins.py +4565 -0
- codecop/discover/origins_ts.py +1584 -0
- codecop/discover/orm_django.py +2772 -0
- codecop/discover/orm_django_models.py +814 -0
- codecop/discover/orm_django_sql.py +662 -0
- codecop/discover/orm_ent.py +565 -0
- codecop/discover/orm_ex_ecto.py +2138 -0
- codecop/discover/orm_go.py +2134 -0
- codecop/discover/orm_go_bun.py +602 -0
- codecop/discover/orm_go_run.py +636 -0
- codecop/discover/orm_go_types.py +1082 -0
- codecop/discover/orm_java.py +1450 -0
- codecop/discover/orm_jdbi.py +743 -0
- codecop/discover/orm_jooq.py +806 -0
- codecop/discover/orm_jpa.py +1307 -0
- codecop/discover/orm_mybatis.py +730 -0
- codecop/discover/orm_py.py +557 -0
- codecop/discover/orm_py_peewee.py +1266 -0
- codecop/discover/orm_py_prisma.py +298 -0
- codecop/discover/orm_rb_activerecord.py +1906 -0
- codecop/discover/orm_rust.py +1248 -0
- codecop/discover/orm_rust_seaorm.py +734 -0
- codecop/discover/orm_sqlalchemy.py +2475 -0
- codecop/discover/orm_stats.py +307 -0
- codecop/discover/orm_tables.py +29 -0
- codecop/discover/orm_ts.py +503 -0
- codecop/discover/orm_ts_drizzle.py +697 -0
- codecop/discover/orm_ts_knex.py +1117 -0
- codecop/discover/orm_ts_kysely.py +861 -0
- codecop/discover/orm_ts_prisma.py +1045 -0
- codecop/discover/orm_ts_schema.py +169 -0
- codecop/discover/orm_ts_sequelize.py +1401 -0
- codecop/discover/orm_ts_typeorm.py +1067 -0
- codecop/discover/probes.py +323 -0
- codecop/discover/python.py +595 -0
- codecop/discover/ruby.py +793 -0
- codecop/discover/rust.py +2378 -0
- codecop/discover/schema.py +520 -0
- codecop/discover/schema_ent.py +145 -0
- codecop/discover/schema_ts.py +420 -0
- codecop/discover/tsresolve.py +257 -0
- codecop/discover/typescript.py +1728 -0
- codecop/discover/xml_mapper.py +526 -0
- codecop/engines/__init__.py +515 -0
- codecop/engines/base.py +498 -0
- codecop/engines/clickhouse.py +1558 -0
- codecop/engines/pg_ddl.py +444 -0
- codecop/engines/pg_hints.py +137 -0
- codecop/engines/postgres.py +1060 -0
- codecop/engines/sqlite.py +742 -0
- codecop/families/__init__.py +3 -0
- codecop/families/build.py +280 -0
- codecop/families/cli.py +177 -0
- codecop/families/coverage_hook.py +62 -0
- codecop/families/driver.py +221 -0
- codecop/families/goslice.py +229 -0
- codecop/families/goslice_tool/go.mod +3 -0
- codecop/families/goslice_tool/main.go +403 -0
- codecop/families/holes.py +451 -0
- codecop/families/images/Dockerfile.go +5 -0
- codecop/families/images/Dockerfile.python +4 -0
- codecop/families/images/Dockerfile.rust +4 -0
- codecop/families/isolate.py +171 -0
- codecop/families/locate.py +270 -0
- codecop/families/model.py +301 -0
- codecop/families/plangate.py +271 -0
- codecop/families/tagpatch.py +174 -0
- codecop/fidelity.py +671 -0
- codecop/fixgroups.py +130 -0
- codecop/formerly.py +91 -0
- codecop/grantscript.py +254 -0
- codecop/groupbench.py +1375 -0
- codecop/groups.py +594 -0
- codecop/groups_view.py +186 -0
- codecop/heat.py +1405 -0
- codecop/heatcli.py +219 -0
- codecop/joins.py +1749 -0
- codecop/joins_view.py +127 -0
- codecop/kinds/__init__.py +507 -0
- codecop/kinds/calls/__init__.py +501 -0
- codecop/kinds/calls/extract.py +1097 -0
- codecop/kinds/calls/harness.py +408 -0
- codecop/kinds/calls/measure.py +342 -0
- codecop/kinds/calls/rules.py +139 -0
- codecop/kinds/calls/runners/RunCalls.java +167 -0
- codecop/kinds/calls/runners/run_go.go +141 -0
- codecop/kinds/calls/runners/run_node.js +186 -0
- codecop/kinds/calls/runners/run_python.py +319 -0
- codecop/kinds/calls/runners/run_rust.rs +200 -0
- codecop/kinds/calls/standin.py +340 -0
- codecop/kinds/pipeline.py +288 -0
- codecop/kinds/regex/__init__.py +1167 -0
- codecop/kinds/regex/engines/RegexHarness.java +129 -0
- codecop/kinds/regex/engines/run_go.go +294 -0
- codecop/kinds/regex/engines/run_node.js +99 -0
- codecop/kinds/regex/engines/run_python.py +295 -0
- codecop/kinds/regex/engines/run_rust.rs +149 -0
- codecop/kinds/regex/extract.py +1438 -0
- codecop/kinds/regex/harness.py +235 -0
- codecop/kinds/regex/measure.py +270 -0
- codecop/kinds/regex/shapes.py +418 -0
- codecop/kinds/regex/syntax.py +1721 -0
- codecop/kinds/testkind.py +107 -0
- codecop/kinds/transaction/__init__.py +228 -0
- codecop/kinds/transaction/bind.py +355 -0
- codecop/kinds/transaction/measure.py +264 -0
- codecop/kinds/transaction/plan.py +462 -0
- codecop/kinds/transaction/rules.py +540 -0
- codecop/kinds/transaction/sandbox.py +94 -0
- codecop/kinds/transaction/scenarios.py +520 -0
- codecop/kinds/transaction/schedule.py +483 -0
- codecop/kinds/transaction/shapes.py +418 -0
- codecop/kinds/transaction/verify.py +293 -0
- codecop/live.py +237 -0
- codecop/loopbench.py +2071 -0
- codecop/loopschema.py +591 -0
- codecop/mcpserver.py +120 -0
- codecop/mechanism.py +214 -0
- codecop/members.py +200 -0
- codecop/multi.py +443 -0
- codecop/observed.py +1868 -0
- codecop/parts.py +102 -0
- codecop/patterns.py +830 -0
- codecop/patterns_view.py +559 -0
- codecop/plangate.py +242 -0
- codecop/profile.py +439 -0
- codecop/prompts/areas.md +19 -0
- codecop/prompts/observed_variants.md +20 -0
- codecop/prompts/profile.md +60 -0
- codecop/prompts/profile_postgres.md +68 -0
- codecop/prompts/profile_sizes.md +26 -0
- codecop/prompts/profile_sqlite.md +69 -0
- codecop/prompts/rank.md +84 -0
- codecop/prompts/rank_postgres.md +96 -0
- codecop/prompts/rank_sqlite.md +94 -0
- codecop/prompts/scenario_variants.md +20 -0
- codecop/rank.py +1473 -0
- codecop/realdb/__init__.py +688 -0
- codecop/realdb/clickhouse.py +1223 -0
- codecop/realdb/facts.py +815 -0
- codecop/realdb/locality.py +964 -0
- codecop/realdb/locks.py +355 -0
- codecop/realdb/match.py +531 -0
- codecop/realdb/postgres.py +643 -0
- codecop/realdb/ratios.py +77 -0
- codecop/realdb/sample.py +126 -0
- codecop/relations.py +227 -0
- codecop/report.py +1854 -0
- codecop/resources.py +332 -0
- codecop/resources_view.py +266 -0
- codecop/rules/entry_points.toml +1013 -0
- codecop/rules/sinks.toml +362 -0
- codecop/runsonce.py +8 -0
- codecop/sandboxschema.py +555 -0
- codecop/scenario_agent.py +172 -0
- codecop/scenario_bind.py +507 -0
- codecop/scenario_sql.py +971 -0
- codecop/scenarios.py +675 -0
- codecop/scenarios_view.py +274 -0
- codecop/schemacli.py +67 -0
- codecop/schemas/areas_output.schema.json +21 -0
- codecop/schemas/profile_output.schema.json +362 -0
- codecop/schemas/rank_output.schema.json +385 -0
- codecop/sizes.py +200 -0
- codecop/sqlinfo.py +558 -0
- codecop/typedparams.py +576 -0
- codecop/verdicts.py +212 -0
- codecop/writebench.py +720 -0
- codecop/writecost.py +440 -0
- codecop/writes.py +1208 -0
- codecop-0.1.0.dist-info/METADATA +42 -0
- codecop-0.1.0.dist-info/RECORD +222 -0
- codecop-0.1.0.dist-info/WHEEL +4 -0
- codecop-0.1.0.dist-info/entry_points.txt +2 -0
- codecop-0.1.0.dist-info/licenses/LICENSE +21 -0
codecop/verdicts.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"""What the agent said, stored per statement, so a judged statement is never paid for twice.
|
|
2
|
+
|
|
3
|
+
A verdict is keyed by what the answer depends on: the kind of question, the statement's normalized SQL (its sql_hash),
|
|
4
|
+
the fingerprint of the definitions of the tables it reads (the real catalog's when there is a pull, else the
|
|
5
|
+
repository's own), the engine and version, and the prompt version (the template and the output schema the agent was
|
|
6
|
+
given). So adding one query re-pays for that one query, a later scan pays only for what changed, and a changed table
|
|
7
|
+
definition invalidates only the statements that read that table. Rejections are stored too, with their reason.
|
|
8
|
+
|
|
9
|
+
`agent_verdicts.json` in the out dir. Old out dirs kept their answers per batch of about 40 queries
|
|
10
|
+
(rank_batches/batch-*.json, keyed by the sorted ids of the whole batch); those are read once and migrated into
|
|
11
|
+
per-statement verdicts (`migrated_from`), never deleted. `--fresh` (everything) or `--fresh Q-014,app/api` (a finding or a
|
|
12
|
+
path) asks again and replaces the stored verdict.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import hashlib
|
|
18
|
+
import json
|
|
19
|
+
import os
|
|
20
|
+
import threading
|
|
21
|
+
from datetime import datetime, timezone
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
|
|
24
|
+
FILE = "agent_verdicts.json"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _h(text: str, n: int = 16) -> str:
|
|
28
|
+
return hashlib.sha256(text.encode("utf-8", "replace")).hexdigest()[:n]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _short(name: str) -> str:
|
|
32
|
+
return str(name or "").replace('"', "").replace("`", "").split(".")[-1].lower()
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def table_fingerprints(inventory: dict | None, facts=None, engine: str = "") -> dict[str, str]:
|
|
36
|
+
"""table (short, lower-case name) -> fingerprint of its definition: the real catalog's when the pull has it, else every
|
|
37
|
+
definition the repository's code holds for that name."""
|
|
38
|
+
by: dict[str, list[str]] = {}
|
|
39
|
+
for t in (inventory or {}).get("tables") or []:
|
|
40
|
+
name = _short(t.get("name"))
|
|
41
|
+
if not name or name.startswith("?"):
|
|
42
|
+
continue
|
|
43
|
+
text = json.dumps({k: t.get(k) for k in ("columns", "engine", "engine_args", "order_by", "partition_by", "primary_key", "indexes")},
|
|
44
|
+
sort_keys=True, default=str)
|
|
45
|
+
by.setdefault(name, []).append(text)
|
|
46
|
+
out = {n: _h("\n".join(sorted(v))) for n, v in by.items()}
|
|
47
|
+
if facts is not None and engine:
|
|
48
|
+
try:
|
|
49
|
+
real = facts.tables(engine) or {}
|
|
50
|
+
except Exception:
|
|
51
|
+
real = {}
|
|
52
|
+
for name in real:
|
|
53
|
+
try:
|
|
54
|
+
d = facts.definition(engine, name)
|
|
55
|
+
except Exception:
|
|
56
|
+
d = None
|
|
57
|
+
if d and d.get("create_sql"):
|
|
58
|
+
out[_short(name)] = "real:" + _h(d["create_sql"])
|
|
59
|
+
return out
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def tables_fp(tables: list[str], fps: dict[str, str]) -> str:
|
|
63
|
+
return _h("|".join(f"{_short(t)}={fps.get(_short(t), 'unknown')}" for t in sorted(set(tables or []), key=_short)))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def key(kind: str, sql_hash: str, tables: list[str], fps: dict[str, str], engine: str, version: str, prompt_version: str,
|
|
67
|
+
extra: str = "") -> str:
|
|
68
|
+
return _h("\n".join([kind, sql_hash or "", tables_fp(tables, fps), engine or "", str(version or ""), prompt_version or "", extra]), 24)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def prompt_version(*parts: str) -> str:
|
|
72
|
+
return _h("\n".join(parts), 12)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def now() -> str:
|
|
76
|
+
return datetime.now(timezone.utc).isoformat(timespec="seconds")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
_FILE_LOCK = threading.RLock() # one writer of a verdicts file at a time in this process (stores of one scan's steps)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _stamp(out_dir: Path | None) -> tuple | None:
|
|
83
|
+
"""When the verdicts file last changed (None: no file)."""
|
|
84
|
+
try:
|
|
85
|
+
st = (Path(out_dir) / FILE).stat() if out_dir else None
|
|
86
|
+
except OSError:
|
|
87
|
+
return None
|
|
88
|
+
return (st.st_ino, st.st_mtime_ns, st.st_size) if st else None
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class Store:
|
|
92
|
+
"""The verdicts of one out dir. Thread-safe; written after every put."""
|
|
93
|
+
|
|
94
|
+
def __init__(self, out_dir: Path | None):
|
|
95
|
+
self.out_dir = out_dir
|
|
96
|
+
self.lock = threading.Lock()
|
|
97
|
+
self.seen = _stamp(out_dir) # taken before the read: a write after it is merged in on save
|
|
98
|
+
data = None
|
|
99
|
+
if out_dir and (out_dir / FILE).exists():
|
|
100
|
+
try:
|
|
101
|
+
data = json.loads((out_dir / FILE).read_text())
|
|
102
|
+
except (OSError, ValueError):
|
|
103
|
+
data = None
|
|
104
|
+
self.data = data if isinstance(data, dict) and isinstance(data.get("verdicts"), dict) else \
|
|
105
|
+
{"schema_version": 1, "verdicts": {}, "migrated_batches": []}
|
|
106
|
+
self.data.setdefault("migrated_batches", [])
|
|
107
|
+
self.hits = 0
|
|
108
|
+
self.puts = 0
|
|
109
|
+
self.mine: set[str] = set() # keys this store put: a save keeps what another store of the out dir wrote since
|
|
110
|
+
|
|
111
|
+
def get(self, k: str) -> dict | None:
|
|
112
|
+
v = self.data["verdicts"].get(k)
|
|
113
|
+
return v if isinstance(v, dict) else None
|
|
114
|
+
|
|
115
|
+
def put(self, k: str, record: dict, save: bool = True) -> None:
|
|
116
|
+
with self.lock:
|
|
117
|
+
self.data["verdicts"][k] = {**record, "at": record.get("at") or now()}
|
|
118
|
+
self.mine.add(k)
|
|
119
|
+
self.puts += 1
|
|
120
|
+
if save:
|
|
121
|
+
self.save()
|
|
122
|
+
|
|
123
|
+
def save(self) -> None:
|
|
124
|
+
"""Write the file. Steps of one scan run side by side, each with a store of its own over the same file: what another
|
|
125
|
+
store wrote since this one read it is merged in first (this store's own puts win), so no answer is lost."""
|
|
126
|
+
if not self.out_dir:
|
|
127
|
+
return
|
|
128
|
+
with _FILE_LOCK, self.lock:
|
|
129
|
+
try:
|
|
130
|
+
if _stamp(self.out_dir) != self.seen:
|
|
131
|
+
try:
|
|
132
|
+
disk = json.loads((self.out_dir / FILE).read_text())
|
|
133
|
+
except (OSError, ValueError):
|
|
134
|
+
disk = None
|
|
135
|
+
if isinstance(disk, dict) and isinstance(disk.get("verdicts"), dict):
|
|
136
|
+
own = {k: self.data["verdicts"][k] for k in self.mine if k in self.data["verdicts"]}
|
|
137
|
+
self.data["verdicts"] = {**self.data["verdicts"], **disk["verdicts"], **own}
|
|
138
|
+
done = list(self.data.get("migrated_batches") or [])
|
|
139
|
+
self.data["migrated_batches"] = done + [b for b in disk.get("migrated_batches") or [] if b not in done]
|
|
140
|
+
self.out_dir.mkdir(parents=True, exist_ok=True)
|
|
141
|
+
tmp = self.out_dir / f"{FILE}.{os.getpid()}.{threading.get_ident()}.tmp"
|
|
142
|
+
tmp.write_text(json.dumps(self.data, indent=1, ensure_ascii=False))
|
|
143
|
+
tmp.replace(self.out_dir / FILE)
|
|
144
|
+
self.seen = _stamp(self.out_dir)
|
|
145
|
+
except OSError:
|
|
146
|
+
pass
|
|
147
|
+
|
|
148
|
+
def count(self, kind: str | None = None) -> int:
|
|
149
|
+
return sum(1 for v in self.data["verdicts"].values() if kind is None or v.get("kind") == kind)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def text_hash(text: str) -> str:
|
|
153
|
+
"""A statement's text with its whitespace normalized: the same statement written over other lines keeps its answer."""
|
|
154
|
+
return _h(" ".join(str(text or "").split()))
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
class Answers:
|
|
158
|
+
"""The per-site answers of the paid steps after rank (loop members, scan groups, observed and scenario variants), in
|
|
159
|
+
the same store as rank's verdicts. A key holds what the answer depends on: the kind of question, the statement's text,
|
|
160
|
+
the definitions of the tables it reads, the engine and version and the prompt version. So a changed statement or a
|
|
161
|
+
changed table definition is asked again, and nothing else is."""
|
|
162
|
+
|
|
163
|
+
def __init__(self, out_dir: Path | None, inventory: dict | None = None, facts=None, engine: str = "", version: str = "",
|
|
164
|
+
store: "Store | None" = None):
|
|
165
|
+
self.store = store or Store(out_dir)
|
|
166
|
+
self.fps = table_fingerprints(inventory, facts, engine)
|
|
167
|
+
self.engine, self.version = engine or "", str(version or "")
|
|
168
|
+
self.hits = 0
|
|
169
|
+
|
|
170
|
+
def key(self, kind: str, text: str, tables: list[str], prompt: str, extra: str = "") -> str:
|
|
171
|
+
return key(kind, text_hash(text), list(tables or []), self.fps, self.engine, self.version, prompt, extra)
|
|
172
|
+
|
|
173
|
+
def get(self, k: str, fresh=False, *, ref: str = "", label: str = "", file: str = "") -> dict | None:
|
|
174
|
+
if wants_fresh(fresh, ref=ref, label=label, file=file):
|
|
175
|
+
return None
|
|
176
|
+
v = self.store.get(k)
|
|
177
|
+
if v is not None:
|
|
178
|
+
self.hits += 1
|
|
179
|
+
return v
|
|
180
|
+
|
|
181
|
+
def put(self, k: str, record: dict, save: bool = True) -> None:
|
|
182
|
+
self.store.put(k, record, save=save)
|
|
183
|
+
|
|
184
|
+
def save(self) -> None:
|
|
185
|
+
self.store.save()
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def wants_fresh(fresh, *, ref: str = "", label: str = "", file: str = "") -> bool:
|
|
189
|
+
"""Whether `--fresh` asks again about this item: True (everything), or a set of findings, ids and path prefixes."""
|
|
190
|
+
if fresh is True:
|
|
191
|
+
return True
|
|
192
|
+
if not fresh:
|
|
193
|
+
return False
|
|
194
|
+
for t in fresh:
|
|
195
|
+
t = str(t).strip()
|
|
196
|
+
if not t:
|
|
197
|
+
continue
|
|
198
|
+
if t in (ref, label) or (file and (file == t or file.startswith(t.rstrip("/") + "/"))):
|
|
199
|
+
return True
|
|
200
|
+
return False
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def parse_fresh(value) -> bool | set[str]:
|
|
204
|
+
"""`--fresh` with no value or `all`: True. A comma list: those findings, ids or paths. Nothing: False."""
|
|
205
|
+
if value is None or value is False:
|
|
206
|
+
return False
|
|
207
|
+
if value is True:
|
|
208
|
+
return True
|
|
209
|
+
text = str(value).strip()
|
|
210
|
+
if not text or text.lower() in ("all", "true", "yes", "1"):
|
|
211
|
+
return True
|
|
212
|
+
return {t.strip() for t in text.split(",") if t.strip()}
|