← back to Wallco Ai
scratch_fuzz_remediate.py
93 lines
#!/usr/bin/env python3
"""Remediate fuzzy pil-mid-heal designs via the 45842 pattern:
unpublish the blurry heal, publish its CRISP parent source as a mural_panel.
Only acts when the parent is verified crisp (low fuzz). Mac2-PG only — NO deploy.
Modes:
analyze -> report fixability for the target set (default top-204 >=45842-class)
apply <N> -> stage the swaps for the top N fixable (by loc) on Mac2 PG
"""
import os, sys, json, subprocess
import scratch_fuzz_scan as s # reuse scan()
GEN = s.GEN_DIR
J = "/Users/macstudio3/.claude/yolo-queue/output/13-fuzzy-cohort-scan-2026-05-26.json"
def psql(sql):
return subprocess.run(["psql", "dw_unified", "-tAc", sql],
capture_output=True, text=True).stdout.strip()
def parent_path(pid):
out = psql("SELECT COALESCE(local_path,'') FROM all_designs WHERE id=%d;" % pid)
if not out:
return None
p = out if os.path.exists(out) else os.path.join(GEN, os.path.basename(out))
return p if os.path.exists(p) else None
def load_targets():
r = json.load(open(J))
# >=45842-class = the clear worst; ranked by localization
sev = [x for x in r if x["band"] >= 0.25 and x["loc"] >= 50]
sev.sort(key=lambda x: x["loc"], reverse=True)
return sev
def fixability(targets):
rows = []
for x in targets:
cid = x["id"]
pid = psql("SELECT parent_design_id FROM all_designs WHERE id=%d;" % cid)
rec = {"child": cid, "loc": x["loc"], "band": x["band"], "parent": None,
"parent_crisp": False, "parent_loc": None, "status": ""}
if not pid or not pid.isdigit():
rec["status"] = "no-parent-id"; rows.append(rec); continue
pid = int(pid); rec["parent"] = pid
pp = parent_path(pid)
if not pp:
rec["status"] = "parent-png-missing"; rows.append(rec); continue
try:
b, rr, l = s.scan(pp)
rec["parent_loc"] = round(l, 1)
# crisp parent = no concentrated fuzzy cross
if l < 20 and b < 0.12:
rec["parent_crisp"] = True; rec["status"] = "FIXABLE"
else:
rec["status"] = "parent-also-fuzzy(loc%.0f)" % l
except Exception as e:
rec["status"] = "scan-err"
rows.append(rec)
return rows
def main():
mode = sys.argv[1] if len(sys.argv) > 1 else "analyze"
targets = load_targets()
print("targets (>=45842-class):", len(targets), flush=True)
rows = fixability(targets)
fixable = [r for r in rows if r["parent_crisp"]]
print("FIXABLE (crisp parent):", len(fixable))
from collections import Counter
c = Counter(r["status"] if r["status"] == "FIXABLE" else r["status"].split("(")[0] for r in rows)
for k, v in c.most_common():
print(" %-22s %d" % (k, v))
json.dump(rows, open("/tmp/fuzz_remediate_plan.json", "w"), indent=0)
print("\nTOP 8 fixable swaps (child -> parent mural):")
for r in fixable[:8]:
print(" #%d (loc%.0f) -> publish parent #%d as mural" % (r["child"], r["loc"], r["parent"]))
if mode == "apply":
N = int(sys.argv[2]) if len(sys.argv) > 2 else 0
todo = fixable[:N]
print("\n=== APPLYING %d swaps on Mac2 PG (no deploy) ===" % len(todo))
for r in todo:
cid, pid = r["child"], r["parent"]
sql = ("UPDATE all_designs SET is_published=FALSE, unpublish_reason='fuzzy band-blur cross (loc%.0f); replaced by crisp mural #%d' WHERE id=%d; "
"UPDATE all_designs SET is_published=TRUE, kind='mural_panel' WHERE id=%d;" % (r["loc"], pid, cid, pid))
psql(sql)
# verify
ids = ",".join(str(r["child"]) for r in todo) or "0"
pids = ",".join(str(r["parent"]) for r in todo) or "0"
print("children now:", psql("SELECT count(*) FROM all_designs WHERE id IN (%s) AND is_published=FALSE;" % ids), "unpublished")
print("parents now :", psql("SELECT count(*) FROM all_designs WHERE id IN (%s) AND is_published=TRUE AND kind='mural_panel';" % pids), "published murals")
if __name__ == "__main__":
main()