← back to Wallco Ai

scripts/apply-id-collision-resolutions.py

53 lines

#!/usr/bin/env python3
# Apply id-collision resolutions per-row. Verdict is passed as arg for the F,T bucket.
# F,F bucket is always skip+log. T,F / T,T escalate (none expected in current data).
import json, sys, os, subprocess

FT_VERDICT = sys.argv[1] if len(sys.argv) > 1 else "A"  # A=no-op preserve prod / B=prod unpublish / C=escalate
AUDIT = "/Users/macstudio3/Projects/wallco-ai/data/id-collision-resolutions-2026-05-29.jsonl"

d = json.load(open('/tmp/collisions-data.json'))
collisions = d['collisions']

def prod_update_unpublish(idv):
    sql = (f"UPDATE all_designs SET is_published=FALSE WHERE id={idv};")
    r = subprocess.run(["ssh","my-server","sudo -u postgres psql -d dw_unified -c \""+sql+"\""],
                       capture_output=True, text=True)
    return r.returncode == 0, (r.stderr or r.stdout).strip()

rows = []
for c in collisions:
    i = c['id']; b = c['bucket']
    if b == "(F,F)":
        rows.append({"id": i, "bucket": b, "dtd_verdict": "skip", "action": "skip",
                     "side": None, "ok": True, "reason": "both_unpublished", "notes": ""})
    elif b == "(F,T)":
        if FT_VERDICT == "A":
            note = ("prod live+customer-visible; mac2 same-id user_removed=TRUE (different design, "
                    "forbidden to republish). preserve prod live row.")
            rows.append({"id": i, "bucket": b, "dtd_verdict": "A", "action": "no_op_preserve_prod",
                         "side": "prod", "ok": True, "reason": "prod_live_mac2_user_removed", "notes": note})
        elif FT_VERDICT == "B":
            ok, msg = prod_update_unpublish(i)
            rows.append({"id": i, "bucket": b, "dtd_verdict": "B",
                         "action": "prod_unpublished" if ok else "prod_unpublish_failed",
                         "side": "prod", "ok": ok, "reason": "verdict_B", "notes": msg})
        else:
            rows.append({"id": i, "bucket": b, "dtd_verdict": "C", "action": "escalate",
                         "side": None, "ok": False, "reason": "escalated_to_steve", "notes": ""})
    elif b == "(T,F)":
        rows.append({"id": i, "bucket": b, "dtd_verdict": "escalate", "action": "escalate",
                     "side": None, "ok": False, "reason": "mac2_live_prod_parked_minority", "notes": "escalate per spec"})
    elif b == "(T,T)":
        rows.append({"id": i, "bucket": b, "dtd_verdict": "escalate", "action": "escalate",
                     "side": None, "ok": False, "reason": "both_live_different_art", "notes": "NEVER auto-pick"})

with open(AUDIT, "w") as f:
    for r in rows:
        f.write(json.dumps(r) + "\n")

from collections import Counter
print("audit rows written:", len(rows))
print("by action:", Counter(r['action'] for r in rows))
print("by bucket:", Counter(r['bucket'] for r in rows))