QoL and metric value inverter

This commit is contained in:
2026-03-04 11:10:45 -06:00
parent 8dfe3607b1
commit f57ac7d6dc
30 changed files with 556 additions and 118 deletions

View File

@@ -193,6 +193,17 @@ class Repository:
self.conn.commit()
return row["id"]
def backfill_lower_is_better(self, domain_name: str, metric_name: str) -> None:
"""Set lower_is_better=1 for an existing domain-metric row that still has the default 0."""
self.conn.execute(
"""UPDATE domain_metric_weights SET lower_is_better = 1
WHERE lower_is_better = 0
AND domain_id = (SELECT id FROM domains WHERE name = ?)
AND metric_id = (SELECT id FROM metrics WHERE name = ?)""",
(domain_name, metric_name),
)
self.conn.commit()
def add_domain(self, domain: Domain) -> Domain:
cur = self.conn.execute(
"INSERT INTO domains (name, description) VALUES (?, ?)",
@@ -204,9 +215,10 @@ class Repository:
mb.metric_id = metric_id
self.conn.execute(
"""INSERT INTO domain_metric_weights
(domain_id, metric_id, weight, norm_min, norm_max)
VALUES (?, ?, ?, ?, ?)""",
(domain.id, metric_id, mb.weight, mb.norm_min, mb.norm_max),
(domain_id, metric_id, weight, norm_min, norm_max, lower_is_better)
VALUES (?, ?, ?, ?, ?, ?)""",
(domain.id, metric_id, mb.weight, mb.norm_min, mb.norm_max,
int(mb.lower_is_better)),
)
self.conn.commit()
return domain
@@ -216,7 +228,8 @@ class Repository:
if not row:
return None
weights = self.conn.execute(
"""SELECT m.name, m.unit, dmw.weight, dmw.norm_min, dmw.norm_max, dmw.metric_id
"""SELECT m.name, m.unit, dmw.weight, dmw.norm_min, dmw.norm_max,
dmw.metric_id, dmw.lower_is_better
FROM domain_metric_weights dmw
JOIN metrics m ON dmw.metric_id = m.id
WHERE dmw.domain_id = ?""",
@@ -231,6 +244,7 @@ class Repository:
metric_name=w["name"], weight=w["weight"],
norm_min=w["norm_min"], norm_max=w["norm_max"],
metric_id=w["metric_id"], unit=w["unit"] or "",
lower_is_better=bool(w["lower_is_better"]),
)
for w in weights
],
@@ -245,7 +259,8 @@ class Repository:
if not row:
return None
weights = self.conn.execute(
"""SELECT m.name, m.unit, dmw.weight, dmw.norm_min, dmw.norm_max, dmw.metric_id
"""SELECT m.name, m.unit, dmw.weight, dmw.norm_min, dmw.norm_max,
dmw.metric_id, dmw.lower_is_better
FROM domain_metric_weights dmw
JOIN metrics m ON dmw.metric_id = m.id
WHERE dmw.domain_id = ?""",
@@ -260,6 +275,7 @@ class Repository:
metric_name=w["name"], weight=w["weight"],
norm_min=w["norm_min"], norm_max=w["norm_max"],
metric_id=w["metric_id"], unit=w["unit"] or "",
lower_is_better=bool(w["lower_is_better"]),
)
for w in weights
],
@@ -277,21 +293,23 @@ class Repository:
mb.metric_id = metric_id
self.conn.execute(
"""INSERT OR REPLACE INTO domain_metric_weights
(domain_id, metric_id, weight, norm_min, norm_max)
VALUES (?, ?, ?, ?, ?)""",
(domain_id, metric_id, mb.weight, mb.norm_min, mb.norm_max),
(domain_id, metric_id, weight, norm_min, norm_max, lower_is_better)
VALUES (?, ?, ?, ?, ?, ?)""",
(domain_id, metric_id, mb.weight, mb.norm_min, mb.norm_max,
int(mb.lower_is_better)),
)
self.conn.commit()
return mb
def update_metric_bound(
self, domain_id: int, metric_id: int, weight: float, norm_min: float, norm_max: float, unit: str
self, domain_id: int, metric_id: int, weight: float, norm_min: float, norm_max: float,
unit: str, lower_is_better: bool = False,
) -> None:
self.conn.execute(
"""UPDATE domain_metric_weights
SET weight = ?, norm_min = ?, norm_max = ?
SET weight = ?, norm_min = ?, norm_max = ?, lower_is_better = ?
WHERE domain_id = ? AND metric_id = ?""",
(weight, norm_min, norm_max, domain_id, metric_id),
(weight, norm_min, norm_max, int(lower_is_better), domain_id, metric_id),
)
if unit:
self.conn.execute(
@@ -330,6 +348,21 @@ class Repository:
self.conn.execute("DELETE FROM combination_scores WHERE domain_id = ?", (domain.id,))
self.conn.execute("DELETE FROM combination_results WHERE domain_id = ?", (domain.id,))
self.conn.execute("DELETE FROM pipeline_runs WHERE domain_id = ?", (domain.id,))
# Delete orphaned combos (no results left in any domain) and all their
# related rows — scores, entity links — so FK constraints don't block.
orphan_sql = """SELECT c.id FROM combinations c
WHERE c.id NOT IN (
SELECT DISTINCT combination_id FROM combination_results
)"""
self.conn.execute(
f"DELETE FROM combination_scores WHERE combination_id IN ({orphan_sql})"
)
self.conn.execute(
f"DELETE FROM combination_entities WHERE combination_id IN ({orphan_sql})"
)
self.conn.execute(
f"DELETE FROM combinations WHERE id IN ({orphan_sql})"
)
self.conn.commit()
return count
@@ -371,12 +404,15 @@ class Repository:
self, combo_id: int, status: str, block_reason: str | None = None
) -> None:
# Don't downgrade from higher pass states — preserves human/LLM review data
if status in ("scored", "llm_reviewed"):
if status in ("scored", "llm_reviewed") or status.endswith("_fail"):
row = self.conn.execute(
"SELECT status FROM combinations WHERE id = ?", (combo_id,)
).fetchone()
if row:
cur = row["status"]
# Fail statuses should not overwrite llm_reviewed or reviewed
if status.endswith("_fail") and cur in ("llm_reviewed", "reviewed"):
return
if status == "scored" and cur in ("llm_reviewed", "reviewed"):
return
if status == "llm_reviewed" and cur == "reviewed":
@@ -550,12 +586,12 @@ class Repository:
).fetchone()
if not row or row["total"] == 0:
return None
blocked = self.conn.execute(
failed = self.conn.execute(
"""SELECT COUNT(*) as cnt
FROM combinations c
JOIN combination_results cr ON cr.combination_id = c.id
JOIN domains d ON cr.domain_id = d.id
WHERE c.status = 'blocked' AND d.name = ?""",
WHERE c.status LIKE '%\\_fail' ESCAPE '\\' AND d.name = ?""",
(domain_name,),
).fetchone()
return {
@@ -564,7 +600,7 @@ class Repository:
"max_score": row["max_score"],
"min_score": row["min_score"],
"last_pass": row["last_pass"],
"blocked": blocked["cnt"] if blocked else 0,
"failed": failed["cnt"] if failed else 0,
}
def get_result(self, combo_id: int, domain_id: int) -> dict | None: