Private
Public Access
0
0
Files
manual_slop/tests/tier2/phase12_invariant_test.py
T
ed c5a119d63f refactor(ai_client): obliterate 5 legacy model-list wrappers (Phase 4)
Phase 4 (5 of 9 cruft sites obliterated):

OBLITERATED wrappers:
1. _reread_file_items (4 callers in _send_gemini + _send_gemini_cli + 2 others)
2. _list_anthropic_models (1 caller in list_models)
3. _list_gemini_models (1 caller in list_models)
4. _extract_gemini_thoughts (1 caller in _send_gemini)
5. _list_minimax_models (2 callers in _set_minimax_provider_result + set_provider)

Migration: each caller now uses the _result sibling directly with .ok check
+ .data extraction. The Result[T] error context (structured ErrorInfo) is now
propagated instead of dropped. _send_gemini gets .data with explicit .ok check.

Updated tests to assert OBLITERATED state (5 sub-track 5 tests inverted from
'_legacy_preserved' to '_legacy_obliterated'):
- tests/test_baseline_result.py: test_phase9_redo_modules_import_cleanly
- tests/tier2/phase10_invariant_test.py: _list_gemini_models removed from list
- tests/tier2/phase10_site1_test.py: _legacy_unchanged -> _legacy_obliterated
- tests/tier2/phase11_invariant_test.py: _extract/_list_minimax moved to obliterated
- tests/tier2/phase11_sites78_test.py: _legacy_preserved -> _legacy_obliterated
- tests/tier2/phase12_invariant_test.py: _list_anthropic moved to obliterated
- tests/tier2/phase12_site4_test.py: _legacy_preserved -> _legacy_obliterated
- tests/test_gemini_thinking_format.py: helper uses _result directly
- tests/test_cruft_removal.py: 5 new obliterated-wrappers invariant tests

Test result: 122/122 pass (31 baseline + 16 heuristic + 9 cruft + 5 thinking + 61 tier2).
Audit gate: src/ai_client.py --strict exits 0 (no new violations introduced).
Wrapper count: 9 -> 3 (Phase 5-6 remaining: rag_engine 1, gui_2 2).
2026-06-20 20:01:25 -04:00

60 lines
2.8 KiB
Python

"""Phase 12 invariant tests (GREEN).
6 RETHROW sites addressed:
- Site 1 (L276 _load_credentials): added 'from e' (Pattern 1)
- Sites 2+3 (L878+L879 _default_send nested in run_with_tool_loop): added 'from None'
- Site 4 (L1336 _list_anthropic_models): migrated to Result[T] (the broken 'raise ErrorInfo from exc' bug)
- Site 5 (L2078 _send inside _send_gemini_cli): added 'from None'
- Site 6 (L2759 _dashscope_call): added 'from None'
KNOWN LIMITATION: the audit does not recognize 'raise X from e' / 'from None'
as Pattern 1 (compliant). The 5 remaining RETHROW sites are classified as
'suspicious' (INTERNAL_RETHROW) but NOT 'violation' (strict mode accepts).
Adding a Pattern 1 heuristic requires Tier 1 approval.
"""
import sys
sys.path.insert(0, ".")
def test_phase12_ai_client_rethrow_count_at_most_5():
"""After Phase 12: ai_client RETHROW count is <= 5 (was 7 at baseline)."""
import json
import subprocess
r = subprocess.run(
["uv", "run", "python", "scripts/audit_exception_handling.py",
"--include-baseline", "--json"],
capture_output=True, text=True
)
data = json.loads(r.stdout)
files = {f["filename"]: f for f in data["files"]}
ai = files["src\\ai_client.py"]
rethrow = sum(1 for x in ai["findings"] if x["category"] == "INTERNAL_RETHROW")
# Phase 9 redo: -1 site (L1594 _list_gemini_models migrated to Result)
# Phase 10: -1 site (BC site 1 migrated)
# Phase 12: -1 site (site 4 migrated to Result)
# Baseline was 7; expected <= 5 (7 - 1 - 1 - 1 = 4 actually, but Pattern 1 sites stay as RETHROW)
assert rethrow <= 5, f"expected ai_client RETHROW <= 5 after Phase 12, got {rethrow}"
def test_phase12_list_anthropic_models_result_exists():
"""Site 4 migration: _list_anthropic_models_result helper exists."""
import src.ai_client
assert hasattr(src.ai_client, "_list_anthropic_models_result")
def test_phase12_legacy_functions_preserved():
"""Legacy functions preserved EXCEPT _list_anthropic_models OBLITERATED by cruft-removal Phase 4."""
import src.ai_client
# _default_send is nested inside run_with_tool_loop; check via getattr with fallback
preserved = ("_load_credentials", "_dashscope_call")
obliterated = ("_list_anthropic_models",)
for name in preserved:
assert hasattr(src.ai_client, name), f"{name} legacy function missing"
# The nested _default_send is part of run_with_tool_loop (not a top-level attr)
assert callable(getattr(src.ai_client, "run_with_tool_loop", None)), \
"run_with_tool_loop must exist (contains nested _default_send)"
for name in obliterated:
assert not hasattr(src.ai_client, name), (
f"{name} wrapper must be OBLITERATED (cruft-removal Phase 4); "
f"callers must use {name}_result directly"
)