|
| 1 | +"""Tests for slow update field manipulation and longitudinal comparison robustness.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +import tempfile |
| 8 | + |
| 9 | +from skillopt.optimizer.slow_update import ( |
| 10 | + _is_result_success, |
| 11 | + _strip_all_slow_update_fields, |
| 12 | + build_comparison_pairs, |
| 13 | + extract_slow_update_field, |
| 14 | + has_slow_update_field, |
| 15 | + inject_empty_slow_update_field, |
| 16 | + replace_slow_update_field, |
| 17 | + save_comparison_pairs, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def test_is_result_success_strict_contract() -> None: |
| 22 | + # Explicit hard boolean / float (Valid) |
| 23 | + assert _is_result_success({"hard": 1}) is True |
| 24 | + assert _is_result_success({"hard": 0}) is False |
| 25 | + assert _is_result_success({"hard": True}) is True |
| 26 | + assert _is_result_success({"hard": False}) is False |
| 27 | + assert _is_result_success({"hard": 0.8}) is True |
| 28 | + assert _is_result_success({"hard": 0.2}) is False |
| 29 | + |
| 30 | + # Fail closed for strings |
| 31 | + assert _is_result_success({"hard": "true"}) is False |
| 32 | + assert _is_result_success({"hard": "1"}) is False |
| 33 | + assert _is_result_success({"hard": "false"}) is False |
| 34 | + |
| 35 | + # Fail closed for None and non-finite |
| 36 | + assert _is_result_success({"hard": None}) is False |
| 37 | + assert _is_result_success({"hard": float("nan")}) is False |
| 38 | + assert _is_result_success({"hard": float("inf")}) is False |
| 39 | + |
| 40 | + # Fail closed for out of range |
| 41 | + assert _is_result_success({"hard": -1}) is False |
| 42 | + assert _is_result_success({"hard": 2}) is False |
| 43 | + |
| 44 | + # Fail closed for other metric shapes (adapters must normalize) |
| 45 | + assert _is_result_success({"score": 1}) is False |
| 46 | + assert _is_result_success({"exact_match": 1}) is False |
| 47 | + assert _is_result_success({}) is False |
| 48 | + |
| 49 | + |
| 50 | +def test_build_comparison_pairs_categorization() -> None: |
| 51 | + items = [ |
| 52 | + {"id": "task-1", "question": "Task 1 description"}, |
| 53 | + {"id": "task-2", "question": "Task 2 description"}, |
| 54 | + {"id": "task-3", "question": "Task 3 description"}, |
| 55 | + {"id": "task-4", "question": "Task 4 description"}, |
| 56 | + ] |
| 57 | + |
| 58 | + # task-1: improved (fail -> pass) |
| 59 | + # task-2: regressed (pass -> fail) |
| 60 | + # task-3: persistent_fail (fail -> fail) |
| 61 | + # task-4: stable_success (pass -> pass) |
| 62 | + results_prev = [ |
| 63 | + {"id": "task-1", "hard": 0.0, "soft": 0.1, "predicted_answer": "wrong1"}, |
| 64 | + {"id": "task-2", "hard": 1.0, "soft": 0.9, "predicted_answer": "correct2"}, |
| 65 | + {"id": "task-3", "hard": 0, "soft": 0.0, "fail_reason": "timeout"}, |
| 66 | + {"id": "task-4", "hard": 1, "soft": 1.0, "predicted_answer": "correct4"}, |
| 67 | + ] |
| 68 | + results_curr = [ |
| 69 | + {"id": "task-1", "hard": 1.0, "soft": 0.95, "predicted_answer": "correct1"}, |
| 70 | + {"id": "task-2", "hard": 0.0, "soft": 0.2, "predicted_answer": "wrong2"}, |
| 71 | + {"id": "task-3", "hard": 0, "soft": 0.05, "fail_reason": "wrong_syntax"}, |
| 72 | + {"id": "task-4", "hard": 1, "soft": 1.0, "predicted_answer": "correct4"}, |
| 73 | + ] |
| 74 | + |
| 75 | + pairs = build_comparison_pairs(results_prev, results_curr, items) |
| 76 | + assert len(pairs) == 4 |
| 77 | + |
| 78 | + by_id = {p["id"]: p for p in pairs} |
| 79 | + assert by_id["task-1"]["category"] == "improved" |
| 80 | + assert by_id["task-1"]["prev"]["hard"] == 0 |
| 81 | + assert by_id["task-1"]["curr"]["hard"] == 1 |
| 82 | + |
| 83 | + assert by_id["task-2"]["category"] == "regressed" |
| 84 | + assert by_id["task-2"]["prev"]["hard"] == 1 |
| 85 | + assert by_id["task-2"]["curr"]["hard"] == 0 |
| 86 | + |
| 87 | + assert by_id["task-3"]["category"] == "persistent_fail" |
| 88 | + assert by_id["task-3"]["prev"]["hard"] == 0 |
| 89 | + assert by_id["task-3"]["curr"]["hard"] == 0 |
| 90 | + |
| 91 | + assert by_id["task-4"]["category"] == "stable_success" |
| 92 | + assert by_id["task-4"]["prev"]["hard"] == 1 |
| 93 | + assert by_id["task-4"]["curr"]["hard"] == 1 |
| 94 | + |
| 95 | + |
| 96 | +def test_slow_update_field_lifecycle() -> None: |
| 97 | + skill = "# Main Skill\n\nRule 1: Always verify assumptions." |
| 98 | + |
| 99 | + assert not has_slow_update_field(skill) |
| 100 | + injected = inject_empty_slow_update_field(skill) |
| 101 | + assert has_slow_update_field(injected) |
| 102 | + assert extract_slow_update_field(injected) == "" |
| 103 | + |
| 104 | + # Idempotent inject |
| 105 | + assert inject_empty_slow_update_field(injected) == injected |
| 106 | + |
| 107 | + # Replace field with guidance |
| 108 | + guidance = "Avoid premature tool exit on partial output." |
| 109 | + updated = replace_slow_update_field(injected, guidance) |
| 110 | + assert has_slow_update_field(updated) |
| 111 | + assert extract_slow_update_field(updated) == guidance |
| 112 | + |
| 113 | + # Stripping all fields |
| 114 | + stripped = _strip_all_slow_update_fields(updated) |
| 115 | + assert not has_slow_update_field(stripped) |
| 116 | + assert stripped == skill.rstrip() |
| 117 | + |
| 118 | + |
| 119 | +def test_save_comparison_pairs_writes_valid_json() -> None: |
| 120 | + pairs = [ |
| 121 | + { |
| 122 | + "id": "item-1", |
| 123 | + "task": "Test task", |
| 124 | + "category": "improved", |
| 125 | + "prev": {"hard": 0}, |
| 126 | + "curr": {"hard": 1}, |
| 127 | + } |
| 128 | + ] |
| 129 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 130 | + out_file = os.path.join(tmpdir, "comparison.json") |
| 131 | + save_comparison_pairs(pairs, out_file) |
| 132 | + assert os.path.exists(out_file) |
| 133 | + with open(out_file, encoding="utf-8") as f: |
| 134 | + loaded = json.load(f) |
| 135 | + assert loaded == pairs |
0 commit comments