dryrun.py¶
clew/propose/dryrun.py
Measure a candidate declaration by RUNNING it — never by projecting it.
_count
int clew.propose.dryrun._count(sqlite3.Connection conn, str sql)
Count rows in a table, tolerating its absence.
Called by _thread_metrics, index_defect, measure_shared_key, measure_threads
Calls none
Source · 8 lines
def _count(conn: sqlite3.Connection, sql: str) -> int:
"""@brief Run one COUNT, degrading to 0 on a table-less database."""
try:
return int(conn.execute(sql).fetchone()[0])
except sqlite3.Error:
return 0
Activity · 4 steps
Flows
_defect_reason
str clew.propose.dryrun._defect_reason(bool has_table, int count)
Turn a memberdef presence/row count into a defect reason.
Source · 9 lines
def _defect_reason(has_table: bool, count: int) -> str:
"""@brief Describe what is wrong with an index, if anything."""
if not has_table:
return "has no `memberdef` table — it is not a clew index"
if count == 0:
return "has an empty `memberdef` — no functions were indexed"
return ""
Activity · 5 steps
Flows
db_copy
Iterator[Path] clew.propose.dryrun.db_copy(Path db_path)
A throwaway copy of a database, deleted on exit.
Source · 14 lines
def db_copy(db_path: Path) -> Iterator[Path]:
"""`copy2` rather than a sqlite backup: the pipeline databases are not in
WAL mode, so the file IS the database, and a plain copy cannot disturb a
concurrent reader of the original.
@brief Copy a database into a temp directory for the duration of a block.
@version 3
"""
with tempfile.TemporaryDirectory(prefix="clew-dryrun-") as tmp:
copy = Path(tmp) / "clew.db"
shutil.copy2(db_path, copy)
yield copy
Activity · 4 steps
Requirements
index_defect
str clew.propose.dryrun.index_defect(Path db_path)
Why a database cannot be dry-run against, if it cannot.
Source · 34 lines
def index_defect(db_path: Path) -> str:
"""Checked BEFORE any measurement, because the pipeline importers a dry run
calls are not defensive about their inputs: `_build_function_indexes` selects
straight from `memberdef` and raises `OperationalError` on a database that
does not have it. A zero-byte `clew.db` left in a repo is enough to reach that
— and the discovery order can pick one up on its own, so the crash is not a
hypothetical.
An index with the table but NO rows is refused too. It would measure 0 for
every candidate, and every candidate would then be rejected with the gate's
own wording ("never a key across two functions") — a specific, false claim
about the repo, where the truth is that nothing was indexed.
@brief Validate that an index can be measured against.
@version 2
"""
try:
conn = sqlite3.connect(str(db_path))
except sqlite3.Error as exc:
return f"cannot be opened ({exc})"
try:
count = _count(conn, "SELECT COUNT(*) FROM memberdef")
has_table = bool(
conn.execute(
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='memberdef'"
).fetchone()
)
except sqlite3.Error as exc:
return f"is not a readable SQLite database ({exc})"
finally:
conn.close()
return _defect_reason(has_table, count)
Activity · 10 steps
Requirements
Flows
measure_threads
dict[str, int] clew.propose.dryrun.measure_threads(Path db_path, Path repo_root, list[dict] spawns, frozenset[str] param_names=frozenset())
Run extract_threads with a candidate spawn list and measure the delta.
Called by measure_candidate
Calls _count, _thread_keys, _thread_metrics, db_copy, extract_threads
Source · 32 lines
def measure_threads(
db_path: Path,
repo_root: Path,
spawns: list[dict],
param_names: frozenset[str] = frozenset(),
) -> dict[str, int]:
"""`param_named` is the fabrication metric that matters. A wrapper whose own
call sites forward a parameter rather than naming a function produces a
thread named after that parameter, with a NULL entry and therefore no
closure — measured at 1 such row for a C/POSIX library's out-of-scope
`SYSTEM_TASKCREATE` and 0 for the wrapper that is proposed.
@brief Measure a candidate spawn declaration's real yield.
@version 3
"""
from ..threads import extract_threads
with db_copy(db_path) as copy:
conn = sqlite3.connect(str(copy))
before = _thread_keys(conn)
measured = {
"threads_before": len(before),
"membership_before": _count(conn, "SELECT COUNT(*) FROM thread_membership"),
}
conn.close()
extract_threads(copy, repo_root, {"spawns": spawns}, None)
conn = sqlite3.connect(str(copy))
measured.update(_thread_metrics(conn, before, param_names))
conn.close()
return measured