from pathlib import Path

p = Path(r"C:\projects\manual_slop_tier2\tests\test_audit_exception_handling_heuristics.py")
with open(p, "rb") as f:
    existing = f.read()

# New tests content. Use byte concatenation to avoid Python string escaping.
nl = b"\r\n"  # match CRLF
new = b""

new += nl * 2
new += b"# ---------------------------------------------------------------------------" + nl
new += b"# Phase 12.1: Heuristic #19 REMOVED - narrow except + log is INTERNAL_SILENT_SWALLOW" + nl
new += b"# ---------------------------------------------------------------------------" + nl
new += b"def test_narrow_except_with_log_only_is_silent_swallow():" + nl
new += b' """try: ...; except (SpecificError): sys.stderr.write(...) is INTERNAL_SILENT_SWALLOW (a violation).' + nl
new += b"" + nl
new += b' Per error_handling.md "The Broad-Except Distinction" table and the user\'s' + nl
new += b' principle (2026-06-17): "logging is NOT a drain". sys.stderr.write alone' + nl
new += b" loses the error context; the propagation does NOT terminate visibly to" + nl
new += b" the user. The convention requires Result[T] propagation to a true drain" + nl
new += b" point. Heuristic #19 (which classified this as compliant) was REMOVED" + nl
new += b" in Phase 12.1." + nl
new += b' """' + nl
new += b" src = (" + nl
new += b"  'def log_failure(path, e):\\n'" + nl
new += b"  ' try:\\n'" + nl
new += b"  '  path.write_text(\"x\", encoding=\"utf-8\")\\n'" + nl
new += b"  ' except (OSError, UnicodeEncodeError):\\n'" + nl
new += b"  '  sys.stderr.write(f\"write failed: {e}\")\\n'" + nl
new += b" )" + nl
new += b" data = _run_audit_on_fixture(src)" + nl
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
new += b" assert len(excepts) == 1" + nl
new += b' assert excepts[0]["category"] == "INTERNAL_SILENT_SWALLOW", (' + nl
new += b'  f"narrow except + log only should be INTERNAL_SILENT_SWALLOW (logging is NOT a drain), got {excepts[0][\'category\']}"' + nl
new += b" )" + nl
new += nl * 2
new += b"def test_narrow_except_with_logging_error_is_silent_swallow():" + nl
new += b' """try: ...; except (SpecificError): logging.error(...) is INTERNAL_SILENT_SWALLOW (a violation).' + nl
new += b"" + nl
new += b" Same principle as test_narrow_except_with_log_only_is_silent_swallow" + nl
new += b" but with the logging module. Logging alone loses the error context." + nl
new += b' """' + nl
new += b" src = (" + nl
new += b"  'def log_failure_via_logging(path):\\n'" + nl
new += b"  ' try:\\n'" + nl
new += b"  '  path.write_text(\"x\", encoding=\"utf-8\")\\n'" + nl
new += b"  ' except (OSError, UnicodeEncodeError) as e:\\n'" + nl
new += b"  '  logging.error(f\"write failed: {e}\")\\n'" + nl
new += b" )" + nl
new += b" data = _run_audit_on_fixture(src)" + nl
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
new += b" assert len(excepts) == 1" + nl
new += b' assert excepts[0]["category"] == "INTERNAL_SILENT_SWALLOW", (' + nl
new += b'  f"narrow except + logging.error should be INTERNAL_SILENT_SWALLOW, got {excepts[0][\'category\']}"' + nl
new += b" )" + nl
new += nl * 2
new += b"# ---------------------------------------------------------------------------" + nl
new += b"# Phase 12.2: visit_Try recursion fix - nested Trys in try body are visited" + nl
new += b"# ---------------------------------------------------------------------------" + nl
new += b"def test_visit_try_recurses_into_try_body():" + nl
new += b' """A nested try inside the try body should be visited and its handlers recorded.' + nl
new += b"" + nl
new += b" The audit's visit_Try had a bug where it did NOT recurse into node.body." + nl
new += b" This test constructs a source with an outer try containing an inner try," + nl
new += b" and asserts BOTH outer and inner handlers appear in the findings." + nl
new += b' """' + nl
new += b" src = (" + nl
new += b"  'def outer():\\n'" + nl
new += b"  ' try:\\n'" + nl
new += b"  '  try:\\n'" + nl
new += b"  '   do_inner()\\n'" + nl
new += b"  '  except ValueError:\\n'" + nl
new += b"  '   handle_inner()\\n'" + nl
new += b"  '  do_outer_thing()\\n'" + nl
new += b"  ' except (OSError, IOError):\\n'" + nl
new += b"  '  handle_outer()\\n'" + nl
new += b" )" + nl
new += b" data = _run_audit_on_fixture(src)" + nl
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
new += b" assert len(excepts) == 2, (" + nl
new += b'  f"visit_Try should recurse into try body; expected 2 EXCEPT findings, got {len(excepts)}: {excepts}"' + nl
new += b" )" + nl
new += nl * 2
new += b"# ---------------------------------------------------------------------------" + nl
new += b"# Phase 12.3: Heuristic D.1 - HTTP error response drain point" + nl
new += b"# ---------------------------------------------------------------------------" + nl
new += b"def test_drain_point_http_error_response_is_compliant():" + nl
new += b' """try: ...; except (SpecificError): self.send_response(500, ...) is INTERNAL_COMPLIANT (drain point D.1).' + nl
new += b"" + nl
new += b" Per error_handling.md Drain Points section, Pattern 1: HTTP error" + nl
new += b" response in a BaseHTTPRequestHandler subclass IS a drain point. The" + nl
new += b" HTTP status code IS the visible user feedback; the propagation" + nl
new += b" terminates at the HTTP response. Heuristic D.1 recognizes this pattern." + nl
new += b' """' + nl
new += b" src = (" + nl
new += b"  'class Handler(BaseHTTPRequestHandler):\\n'" + nl
new += b"  ' def do_GET(self):\\n'" + nl
new += b"  '  try:\\n'" + nl
new += b"  '   self._read_body()\\n'" + nl
new += b"  '  except (OSError, ValueError) as e:\\n'" + nl
new += b"  '   self.send_response(500)\\n'" + nl
new += b"  '   self.send_header(\"Content-Type\", \"application/json\")\\n'" + nl
new += b"  '   self.wfile.write(b\\'{\"error\": \"internal\"}\\')\\n'" + nl
new += b"  )" + nl
new += b" data = _run_audit_on_fixture(src)" + nl
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
new += b" assert len(excepts) == 1" + nl
new += b' assert excepts[0]["category"] == "INTERNAL_COMPLIANT", (' + nl
new += b'  f"HTTP error response should be INTERNAL_COMPLIANT (drain point D.1), got {excepts[0][\'category\']}: {excepts[0].get(\'note\', \'\')}"' + nl
new += b" )" + nl
new += nl * 2
new += b"# ---------------------------------------------------------------------------" + nl
new += b"# Phase 12.3: Heuristic D.2 - GUI error display drain point" + nl
new += b"# ---------------------------------------------------------------------------" + nl
new += b"def test_drain_point_gui_error_display_is_compliant():" + nl
new += b' """try: ...; except (SpecificError): imgui.open_popup(...) is INTERNAL_COMPLIANT (drain point D.2).' + nl
new += b"" + nl
new += b" Per error_handling.md Drain Points section, Pattern 2: GUI error" + nl
new += b" display via imgui.open_popup IS a drain point. The user sees the" + nl
new += b" error modal." + nl
new += b' """' + nl
new += b" src = (" + nl
new += b"  'def show_load_error():\\n'" + nl
new += b"  ' try:\\n'" + nl
new += b"  '  do_load()\\n'" + nl
new += b"  ' except (OSError, ValueError):\\n'" + nl
new += b"  '  imgui.open_popup(\"Load Error\")\\n'" + nl
new += b" )" + nl
new += b" data = _run_audit_on_fixture(src)" + nl
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
new += b" assert len(excepts) == 1" + nl
new += b' assert excepts[0]["category"] == "INTERNAL_COMPLIANT", (' + nl
new += b'  f"GUI error display should be INTERNAL_COMPLIANT (drain point D.2), got {excepts[0][\'category\']}"' + nl
new += b" )" + nl
new += nl * 2
new += b"# ---------------------------------------------------------------------------" + nl
new += b"# Phase 12.3: Heuristic D.3 - Intentional app termination drain point" + nl
new += b"# ---------------------------------------------------------------------------" + nl
new += b"def test_drain_point_app_termination_is_compliant():" + nl
new += b' """try: ...; except (SpecificError): sys.exit(1) is INTERNAL_COMPLIANT (drain point D.3).' + nl
new += b"" + nl
new += b" Per error_handling.md Drain Points section, Pattern 3: intentional" + nl
new += b" app termination via sys.exit IS a drain point. The process exit IS" + nl
new += b" the termination of the propagation." + nl
new += b' """' + nl
new += b" src = (" + nl
new += b"  'def critical_init():\\n'" + nl
new += b"  ' try:\\n'" + nl
new += b"  '  load_config()\\n'" + nl
new += b"  ' except (OSError, ValueError):\\n'" + nl
new += b"  '  sys.stderr.write(\"FATAL: config missing\\n\")\\n'" + nl
new += b"  '  sys.exit(1)\\n'" + nl
new += b" )" + nl
new += b" data = _run_audit_on_fixture(src)" + nl
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
new += b" assert len(excepts) == 1" + nl
new += b' assert excepts[0]["category"] == "INTERNAL_COMPLIANT", (' + nl
new += b'  f"app termination should be INTERNAL_COMPLIANT (drain point D.3), got {excepts[0][\'category\']}"' + nl
new += b" )" + nl
new += nl * 2
new += b"# ---------------------------------------------------------------------------" + nl
new += b"# Phase 12.3: Heuristic D.4 - Telemetry emission drain point" + nl
new += b"# ---------------------------------------------------------------------------" + nl
new += b"def test_drain_point_telemetry_emit_is_compliant():" + nl
new += b' """try: ...; except (SpecificError): telemetry.emit_error(...) is INTERNAL_COMPLIANT (drain point D.4).' + nl
new += b"" + nl
new += b" Per error_handling.md Drain Points section, Pattern 4: telemetry" + nl
new += b" emission IS a drain point. The error reaches the monitoring system." + nl
new += b' """' + nl
new += b" src = (" + nl
new += b"  'def report_failure():\\n'" + nl
new += b"  ' try:\\n'" + nl
new += b"  '  do_thing()\\n'" + nl
new += b"  ' except (OSError, ValueError):\\n'" + nl
new += b"  '  telemetry.emit_error(operation=\"do_thing\", kind=\"INTERNAL\", message=\"failed\")\\n'" + nl
new += b" )" + nl
new += b" data = _run_audit_on_fixture(src)" + nl
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
new += b" assert len(excepts) == 1" + nl
new += b' assert excepts[0]["category"] == "INTERNAL_COMPLIANT", (' + nl
new += b'  f"telemetry emit should be INTERNAL_COMPLIANT (drain point D.4), got {excepts[0][\'category\']}"' + nl
new += b" )" + nl
new += nl * 2
new += b"# ---------------------------------------------------------------------------" + nl
new += b"# Phase 12.3: Heuristic D.5 - Bounded retry drain point" + nl
new += b"# ---------------------------------------------------------------------------" + nl
new += b"def test_drain_point_bounded_retry_is_compliant():" + nl
new += b' """try: ...; except (SpecificError): for attempt in range(3): ...; return None is INTERNAL_COMPLIANT (drain point D.5).' + nl
new += b"" + nl
new += b" Per error_handling.md Drain Points section, Pattern 5: bounded retry" + nl
new += b" followed by return None IS a drain point. The retry is bounded (no" + nl
new += b" infinite loop); the final None propagates to a visible error UI." + nl
new += b' """' + nl
new += b" src = (" + nl
new += b"  'def load_with_retry():\\n'" + nl
new += b"  ' for attempt in range(3):\\n'" + nl
new += b"  '  try:\\n'" + nl
new += b"  '   do_load()\\n'" + nl
new += b"  '   return \"ok\"\\n'" + nl
new += b"  '  except (OSError, ValueError):\\n'" + nl
new += b"  '   time.sleep(1)\\n'" + nl
new += b"  ' return None\\n'" + nl
new += b" )" + nl
new += b" data = _run_audit_on_fixture(src)" + nl
new += b' findings = _classifications_for_file(data, "audit_heuristic_fixture.py")' + nl
new += b' excepts = [f for f in findings if f["kind"] == "EXCEPT"]' + nl
new += b" assert len(excepts) == 1" + nl
new += b' assert excepts[0]["category"] == "INTERNAL_COMPLIANT", (' + nl
new += b'  f"bounded retry should be INTERNAL_COMPLIANT (drain point D.5), got {excepts[0][\'category\']}"' + nl
new += b" )" + nl

# Append
result = existing + new
with open(p, "wb") as f:
    f.write(result)

print(f"wrote {len(result)} chars (added {len(new)} chars)")
# Verify parses
import ast
ast.parse(result.decode("utf-8"))
print("parses ok")