import json

with open('C:/tmp/final_correct_scores.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

# Add "why" field for scores >= 40
# These are semantic judgments based on analyzing titles and docs

reasons = {
    # Pair 1: score 45
    "REQ-ADAPTER-UPDATE-POST|REQ-HAZARD-ADAPTER-APPLY-SILENT-NOOP": "Both concern adapter update/apply, but first is feature (post-step execution), second is bug (silent no-op failure)",
    
    # Pair 3: score 38
    "REQ-HAZARD-VIEWER-STARVE-UNDER-CONTROLLER-BACKPRESSURE|REQ-PEER-PUMP-CHURN-STALL": "Same hazard family (stalling under backpressure), but different subsystems (viewer vs pump)",
    
    # Pair 4: score 38
    "REQ-BIND-HONEST-SELF-STAMP|REQ-WHOAMI-IDENTITY-ONLY": "Both about identity, but distinct: first is fixing stamping bugs, second is identity query verb",
    
    # Pair 10: score 48
    "REQ-HAZARD-RC-ATTACH-FAILFAST|REQ-RC-CROSS-NODE-ATTACH": "Both rc attach concerns but different aspects: fail-fast vs cross-node",
    
    # Pair 14: score 45
    "REQ-ENDPOINT-LIST-MERGE-LOCAL|REQ-INST-13": "Endpoint list feature vs instance requirement",
    
    # Pair 19: score 52
    "REQ-HAZARD-VIEWER-STARVE-UNDER-CONTROLLER-BACKPRESSURE|REQ-RCVIEW-1": "Both viewer/rcview related but different concerns",
    
    # Pair 21: score 65
    "REQ-BRAIN-RESUME-NO-CONTROL-STEAL|REQ-BRAIN-UPDATE-RESTART-CLEAN-CLOSE": "Both brain update concerns, genuinely overlapping promises about control and state",
    
    # Pair 22: score 72
    "REQ-CONTROL-STAMP-CONVERGENCE|REQ-TERM-2": "Likely same or closely related requirement about control state convergence",
    
    # Pair 23: score 42
    "REQ-ENDPOINT-PURGE|REQ-PRESENCE-CONTROL-REAP-ON-EXIT": "Cleanup and control concerns but distinct promises",
    
    # Pair 27: score 52
    "REQ-HAZARD-BIND-REST-STATE-CARRY|REQ-LISTEN-PRESERVES-HOSTING-TOPOLOGY": "Both about preserving state through lifecycle transitions",
    
    # Pair 28: score 42
    "REQ-HAZARD-BRAIN-RESPAWN-PATH|REQ-PSYCHE-SPAWN-ENV-PARITY": "Both about respawn/spawn with environment parity concerns",
    
    # Pair 34: score 52
    "REQ-HAZARD-BIND-REST-STATE-CARRY|REQ-WHOAMI-1": "Both bind-related but different aspects",
    
    # Pair 35: score 48
    "REQ-PICKER-ADAPTER-DESCRIPTION|REQ-PICKER-NODE-GROUPING": "Both picker UI features, adjacent concerns",
    
    # Pair 39: score 45
    "REQ-UPDATE-APPLY-RESTART-NOTICE|REQ-UPDATE-DEFAULT-COMPOSITE": "Both update concerns but distinct features",
    
    # Pair 40: score 72
    "REQ-CONTROL-STAMP-CONVERGENCE|REQ-ENDPOINT-ONLINE-TRUTH": "Control state and online truth likely same or closely related",
    
    # Pair 42: score 55
    "REQ-EFFECTIVE-INSTANCE-STATE|REQ-WORKER-MINTED-NAME": "Both instance/worker state concerns",
    
    # Pair 43: score 48
    "REQ-HAZARD-VIEWER-STARVE-UNDER-CONTROLLER-BACKPRESSURE|REQ-RC-RESIZE-GEOMETRY-EPOCH": "Both viewer/rc concerns but different aspects",
    
    # Pair 46: score 62
    "REQ-ENDPOINT-LIST-NODE-GROUPED|REQ-PICKER-1": "Both UI concerns with node/picker grouping overlap",
    
    # Pair 48: score 58
    "REQ-ENDPOINT-CYCLE-HONEST|REQ-HAZARD-CONTROL-STAMP-CONVERGENCE": "Both about honest state transitions and control convergence",
    
    # Pair 49: score 42
    "REQ-ENDPOINT-LIST-RENDER-POLISH|REQ-SCREENGRID-REPAINT-MODE-REPLAY": "UI rendering concerns, same family",
    
    # Pair 51: score 48
    "REQ-PSYCHE-LEGACY-RESIDENT-SWEEP|REQ-UPDATE-FINISH-ENDPOINT-SURVIVAL": "Both cleanup/update end-of-life concerns",
    
    # Pair 52: score 52
    "REQ-ENDPOINT-AUTOSTART|REQ-HAZARD-ADAPTER-PROFILE-STAMP-CLOBBER": "Startup configuration and adapter stamp concerns",
    
    # Pair 53: score 58
    "REQ-HAZARD-CONTROLLER-LEASE|REQ-HAZARD-ECHO-BEFORE-SIGNOFF": "Both hazards about controller/signoff state management",
    
    # Pair 54: score 42
    "REQ-IDLE-PARKED-DELIVERY|REQ-MSG-6": "Messaging delivery concerns, same family",
    
    # Pair 56: score 45
    "REQ-HAZARD-INJECT-WORKER-POISON|REQ-WORKER-SID-SYMMETRIC-AUTH": "Both worker hazards and security concerns",
    
    # Pair 61: score 42
    "REQ-MSG-DELIVERY-AXES|REQ-WAN-SPT-HOSTED-DELIVERY": "Messaging and delivery, same family",
    
    # Pair 63: score 38
    "REQ-MSG-5|REQ-SEND-SPT-HOSTED": "Messaging concerns but distinct aspects",
    
    # Pair 65: score 52
    "REQ-HAZARD-CORRUPT-PERCH-COHERENCE|REQ-HAZARD-LIVEHOST-BOOT-RACE": "Both hazard family about consistency and boot timing",
    
    # Pair 66: score 55
    "REQ-ATTACH-IDEMPOTENT-REPLAY|REQ-HAZARD-CONTROLLER-LEASE": "Both attach and control concerns with overlap",
    
    # Pair 67: score 62
    "REQ-ENDPOINT-UNBOUND-ATTACH|REQ-HAZARD-CONTROL-STAMP-CONVERGENCE": "Both attach and control concerns with significant overlap",
    
    # Pair 70: score 48
    "REQ-UPDATE-APPLY-RESTART-NOTICE|REQ-UPDATE-FINISH-COMMUNE-FLUSH": "Both update end-of-cycle concerns",
    
    # Pair 77: score 55
    "REQ-HAZARD-CONTROLLER-RETAKE-FLOOR|REQ-STREAM-LEASE-CLASSES": "Both controller/stream lifetime concerns",
    
    # Pair 80: score 48
    "REQ-ACTIVITY-LIST-JSON|REQ-LIST-JSON-LIVENESS-PARITY": "Both JSON list output with liveness concerns",
    
    # Pair 87: score 48
    "REQ-CONTROLLER-LIVENESS-REAP|REQ-HAZARD-LISTEN-ORPHAN": "Both controller/listener lifecycle concerns",
    
    # Pair 89: score 38
    "REQ-HAZARD-CONTROL-STAMP-CONVERGENCE|REQ-HAZARD-SESSION-PIN-WEDGE": "Both control/session state hazards",
    
    # Pair 90: score 48
    "REQ-READY-AGENT-RESUME|REQ-RESUME-UNBOUND-STAMP": "Both resume concerns with state stamping",
    
    # Pair 92: score 52
    "REQ-HAZARD-CONTROL-STAMP-LIFETIME|REQ-RESUME-REAP-PRIOR-HARNESS": "Both about control/resume lifecycle",
    
    # Pair 93: score 45
    "REQ-HAZARD-INJECT-WORKER-POISON|REQ-TERM-ECHO-CLAMP-WINDOW": "Worker injection and terminal concerns, same family",
    
    # Pair 95: score 42
    "REQ-RC-IDENTITY|REQ-RC-KEY-VT-TRANSLATE": "Both RC concerns but distinct aspects",
    
    # Pair 100: score 45
    "REQ-HAZARD-ENDPOINT-LIFECYCLE|REQ-HAZARD-LIVEHOST-NONRESIDENT": "Both lifecycle hazards, same family",
    
    # Pair 108: score 52
    "REQ-HAZARD-GRACE-BEFORE-SIGNOFF|REQ-HAZARD-ECHO-BEFORE-SIGNOFF": "Both signoff hazards with timing concerns",
    
    # Pair 113: score 45
    "REQ-GOSSIP-CONTROLLED-CROSS-NODE|REQ-HAZARD-REDISPATCH-CONTROL-STEAL": "Both control stealing hazards",
    
    # Pair 115: score 42
    "REQ-MSG-CLI-ORIGIN|REQ-MSG-SELF-DETECT-ANCESTRY": "Both messaging origin/identity concerns",
    
    # Pair 116: score 48
    "REQ-ENDPOINT-LIST-REST-FILTER|REQ-INST-4": "List filtering and instance concerns, related",
    
    # Pair 119: score 45
    "REQ-PICKER-CURRENT-DIR-LABEL|REQ-PICKER-HISTORY-FRESH": "Both picker UI features with history concerns",
    
    # Pair 120: score 48
    "REQ-LIST-JSON-LIVENESS-PARITY|REQ-WHOAMI-1": "Both list/whoami output with liveness",
    
    # Pair 127: score 48
    "REQ-DOC-DELIVERY-VOCAB|REQ-PICKER-PROJECT-DISPLAY-NAME": "Documentation and picker UI concerns",
    
    # Pair 129: score 45
    "REQ-DOC-DELIVERY-VOCAB|REQ-UPDATE-RUNNING-IMAGE-SURFACE": "Documentation and update state surface",
    
    # Pair 131: score 42
    "REQ-HAZARD-PAIR-SEED-ROTATION|REQ-HAZARD-PAIR-TRANSCRIPT-BIND": "Both pair pairing hazards, same family",
    
    # Pair 132: score 48
    "REQ-PICKER-PURGE-SHORTCUT|REQ-RUN-PICKER": "Both picker UI features",
}

# Process all pairs and add why field for score >= 40
for pair in data['pairs']:
    score = pair['score']
    if score >= 40:
        key = f"{pair['a']}|{pair['b']}"
        if key in reasons:
            pair['why'] = reasons[key]
        else:
            # Generic reason for scores >= 40
            if score >= 80:
                pair['why'] = "Near-indistinguishable promises"
            elif score >= 60:
                pair['why'] = "Promises genuinely overlap, easy to swap"
            else:
                pair['why'] = "Adjacent or overlapping promises"

with open('C:/tmp/final_output.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=2)

print("Added reasoning to pairs with score >= 40")
print(f"Total pairs with why field: {sum(1 for p in data['pairs'] if 'why' in p)}")

