File size: 6,282 Bytes
ff8b94c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | from unittest.mock import patch
import pytest
from skillopt.gradient.aggregate import _merge_batch, merge_patches
def test_merge_batch_fallback_on_runtime_error():
"""Test that _merge_batch falls back to concatenation when optimizer raises RuntimeError."""
patches = [
{"reasoning": "r1", "edits": [{"id": "1", "content": "one"}]},
{"reasoning": "r2", "edits": [{"id": "2", "content": "two"}]},
]
with patch("skillopt.gradient.aggregate.chat_optimizer", side_effect=RuntimeError("API is down")):
with pytest.warns(UserWarning, match="Optimizer call or parsing failed during batch merge"):
result = _merge_batch(
skill_content="skill content",
patches=patches,
system_prompt="merge this",
update_mode="patch",
level=2,
)
assert result["reasoning"] == "fallback concatenation"
assert "edits" in result
assert len(result["edits"]) == 2
assert result["edits"][0]["id"] == "1"
assert result["edits"][0]["merge_level"] == 2
assert result["edits"][1]["id"] == "2"
assert result["edits"][1]["merge_level"] == 2
def test_merge_batch_fallback_on_malformed_json():
"""A real non-JSON optimizer response is observable and falls back."""
patches = [{"reasoning": "r1", "edits": [{"id": "1", "content": "one"}]}]
with patch("skillopt.gradient.aggregate.chat_optimizer", return_value=("not json", None)):
with pytest.warns(UserWarning, match="unusable output during batch merge"):
result = _merge_batch(
skill_content="skill content",
patches=patches,
system_prompt="merge this",
update_mode="patch",
level=1,
)
assert result["reasoning"] == "fallback concatenation"
assert len(result["edits"]) == 1
def test_merge_batch_warning_does_not_expose_exception_text():
"""Provider response bodies and credentials must not be copied to logs."""
secret = "Authorization: Bearer sk-secret-example"
patches = [{"reasoning": "r1", "edits": [{"id": "1", "content": "one"}]}]
with patch("skillopt.gradient.aggregate.chat_optimizer", side_effect=RuntimeError(secret)):
with pytest.warns(UserWarning) as caught:
result = _merge_batch(
skill_content="skill content",
patches=patches,
system_prompt="merge this",
update_mode="patch",
level=1,
)
assert result["reasoning"] == "fallback concatenation"
assert secret not in "\n".join(str(item.message) for item in caught)
def test_merge_patches_fallback_on_runtime_error():
"""Test that merge_patches falls back during the final merge if the optimizer fails."""
failure_patches = [{"reasoning": "f1", "edits": [{"id": "1", "content": "f_one"}]}]
success_patches = [{"reasoning": "s1", "edits": [{"id": "2", "content": "s_two"}]}]
# We patch _hierarchical_merge to just return the single patches to skip the batch merges,
# and then patch chat_optimizer to fail on the final merge.
def mock_hierarchical(skill_content, patches, *args, **kwargs):
return patches[0]
with patch("skillopt.gradient.aggregate._hierarchical_merge", side_effect=mock_hierarchical):
with patch("skillopt.gradient.aggregate.chat_optimizer", side_effect=RuntimeError("Timeout")):
with pytest.warns(UserWarning, match="Optimizer call or parsing failed during final merge"):
result = merge_patches(
skill_content="content",
failure_patches=failure_patches,
success_patches=success_patches,
batch_size=2,
verbose=False,
)
assert result["reasoning"] == "fallback: failure first, then success"
assert len(result["edits"]) == 2
assert result["edits"][0]["id"] == "1"
assert result["edits"][1]["id"] == "2"
def test_merge_patches_warns_on_unusable_output():
"""A non-JSON final response is observable and uses the safe fallback."""
failure_patches = [{"reasoning": "f1", "edits": [{"id": "1", "content": "f_one"}]}]
success_patches = [{"reasoning": "s1", "edits": [{"id": "2", "content": "s_two"}]}]
def first_patch(_skill_content, patches, *args, **kwargs):
return patches[0]
with patch("skillopt.gradient.aggregate._hierarchical_merge", side_effect=first_patch):
with patch("skillopt.gradient.aggregate.chat_optimizer", return_value=("not json", None)):
with pytest.warns(UserWarning, match="unusable output during final merge"):
result = merge_patches(
skill_content="content",
failure_patches=failure_patches,
success_patches=success_patches,
batch_size=2,
verbose=False,
)
assert result["reasoning"] == "fallback: failure first, then success"
assert [edit["id"] for edit in result["edits"]] == ["1", "2"]
@pytest.mark.parametrize(
"response",
['{"edits": null}', '{"edits": ["not an object"]}'],
)
def test_merge_patches_rejects_invalid_payload_shape(response):
"""A present but invalid payload must not bypass fallback in quiet mode."""
failure_patches = [{"reasoning": "f1", "edits": [{"id": "1"}]}]
success_patches = [{"reasoning": "s1", "edits": [{"id": "2"}]}]
def first_patch(_skill_content, patches, *args, **kwargs):
return patches[0]
with patch("skillopt.gradient.aggregate._hierarchical_merge", side_effect=first_patch):
with patch("skillopt.gradient.aggregate.chat_optimizer", return_value=(response, None)):
with pytest.warns(UserWarning, match="unusable output during final merge"):
result = merge_patches(
skill_content="content",
failure_patches=failure_patches,
success_patches=success_patches,
verbose=False,
)
assert result["reasoning"] == "fallback: failure first, then success"
assert [edit["id"] for edit in result["edits"]] == ["1", "2"]
|