"""Fix the deprecation section in error_handling.md to reflect historical state.

This uses a marker-based replacement to avoid encoding issues with unicode
characters in PowerShell output.
"""
from __future__ import annotations

import sys
from pathlib import Path

DOC = Path("conductor/code_styleguides/error_handling.md")

# We use the start and end markers that are unique to the deprecation section.
START_MARKER = "## Deprecation: `ai_client."
END_MARKER = "transition; new tests for the new API should\nassert the warning is NOT emitted by `send()`.\n\n"


def main() -> int:
 with DOC.open("r", encoding="utf-8", newline="") as f:
  content = f.read()
 has_crlf = "\r\n" in content
 nl = "\r\n" if has_crlf else "\n"
 start_marker = START_MARKER.replace("\n", nl)
 end_marker = END_MARKER.replace("\n", nl)
 i = content.find(start_marker)
 if i < 0:
  print(f"Start marker not found", file=sys.stderr)
  return 1
 j = content.find(end_marker, i)
 if j < 0:
  print(f"End marker not found", file=sys.stderr)
  return 1
 end_of_section = j + len(end_marker)
 section_text = content[i:end_of_section]
 replacement = """## Historical deprecation (added 2026-06-15, reverted 2026-06-16)

The public `ai_client.send()` was briefly marked `@deprecated` in favor of
`ai_client.send_result()` on 2026-06-15 by the
`public_api_migration_and_ui_polish_20260615` track. The decision was
reverted on 2026-06-16 by `send_result_to_send_20260616` after the
Tier 2 autonomous sandbox proved capable of doing the rename safely.

`ai_client.send(...) -> Result[str, ErrorInfo]` is the canonical public API.
No deprecation is in effect. For the historical record of the brief
deprecation cycle, see
`conductor/tracks/public_api_migration_and_ui_polish_20260615/spec.md`
and `conductor/tracks/send_result_to_send_20260616/spec.md`.

""".replace("\n", nl)
 new_content = content[:i] + replacement + content[end_of_section:]
 with DOC.open("w", encoding="utf-8", newline="") as f:
  f.write(new_content)
 print(f"Replaced {len(section_text)} chars of deprecation section with {len(replacement)} chars of historical note.")
 return 0


if __name__ == "__main__":
 raise SystemExit(main())
