File size: 7,670 Bytes
c0e9d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"""Loop guards: the layer that decides a turn is not converging.

A guard bug is worse than no guard, so these tests pin the thresholds at their
edges and prove a healthy long turn is never blocked.
"""

from __future__ import annotations

import pytest

from pyUltroid.agent._loop import (
    Decision,
    LoopGuard,
    is_failure,
    signature_of,
)


def _guard(**kw):
    return LoopGuard(**kw)


def _fail(guard, tool, args, rounds=1):
    for _ in range(rounds):
        guard.before_call(tool, args)
        guard.observe(tool, args, {"error": "boom"})
        guard.begin_round(progressed=False)


# ── signature identity ──────────────────────────────────────────────────────


def test_signature_ignores_argument_key_order():
    a = signature_of("t", {"x": 1, "y": 2})
    b = signature_of("t", {"y": 2, "x": 1})
    assert a == b


def test_signature_separates_different_calls():
    assert signature_of("t", {"x": 1}) != signature_of("t", {"x": 2})
    assert signature_of("a", {"x": 1}) != signature_of("b", {"x": 1})


def test_signature_survives_unserialisable_arguments():
    assert isinstance(signature_of("t", {"o": object()}), str)


# ── failure classification ──────────────────────────────────────────────────


@pytest.mark.parametrize(
    "result",
    [
        {"error": "x"},
        {"denied": True},
        {"blocked": True},
        {"ok": False},
        {"exit_code": 1},
        "Error: nope",
    ],
)
def test_failures_are_recognised(result):
    assert is_failure(result) is True


@pytest.mark.parametrize(
    "result",
    [{}, {"ok": True}, {"exit_code": 0}, {"stdout": "x"}, "plain text", None, 0],
)
def test_non_failures_are_not_flagged(result):
    assert is_failure(result) is False


# ── thresholds ──────────────────────────────────────────────────────────────


def test_first_call_is_allowed():
    assert _guard().before_call("t", {}).decision is Decision.ALLOW


def test_identical_call_is_blocked_while_first_is_in_flight():
    g = _guard()
    first = g.before_call("send", {"chat": 1, "text": "hello"})
    duplicate = g.before_call("send", {"text": "hello", "chat": 1})
    assert first.decision is Decision.ALLOW
    assert duplicate.decision is Decision.BLOCK
    assert duplicate.code == "duplicate_in_flight"

    g.observe("send", {"chat": 1, "text": "hello"}, {"ok": True})
    assert (
        g.before_call("send", {"chat": 1, "text": "hello"}).decision is Decision.ALLOW
    )


def test_repeat_failure_warns_at_two():
    g = _guard()
    _fail(g, "t", {"a": 1}, rounds=2)
    verdict = g.before_call("t", {"a": 1})
    assert verdict.decision is Decision.WARN
    assert verdict.code == "repeated_failure"


def test_repeat_failure_blocks_at_five():
    g = _guard()
    _fail(g, "t", {"a": 1}, rounds=5)
    assert g.before_call("t", {"a": 1}).decision is Decision.BLOCK


def test_a_different_call_is_not_punished_for_the_first_one_failing():
    g = _guard()
    _fail(g, "t", {"a": 1}, rounds=5)
    assert g.before_call("t", {"a": 2}).decision is not Decision.BLOCK


def test_a_success_resets_progress_but_not_the_failure_tally():
    g = _guard()
    _fail(g, "t", {"a": 1}, rounds=3)
    g.observe("t", {"b": 2}, {"ok": True})
    g.begin_round(progressed=True)
    # the exact-failure count is per-signature, so a different arg is clean
    assert g.before_call("t", {"b": 2}).decision is not Decision.BLOCK


def test_same_tool_failures_halt():
    g = _guard()
    for i in range(8):
        g.observe("flaky", {"i": i}, {"error": "x"})
    verdict = g.before_call("flaky", {"i": 99})
    assert verdict.decision is Decision.HALT
    assert verdict.halt is True


def test_same_tool_warns_before_halting():
    g = _guard()
    for i in range(3):
        g.observe("flaky", {"i": i}, {"error": "x"})
    assert g.before_call("flaky", {"i": 99}).decision is Decision.WARN


def test_no_progress_blocks():
    """A stall is re-running a bad call, not merely elapsed rounds."""
    g = _guard()
    for _ in range(5):
        g.before_call("t", {"a": 1})  # same call, already failed -> repeat
        g.observe("t", {"a": 1}, {"error": "x"})
        g.begin_round(progressed=False)
    assert g.before_call("t", {"a": 1}).decision is Decision.BLOCK


def test_trying_new_calls_is_not_a_stall():
    """A model investigating with fresh calls must not be cut off."""
    g = _guard()
    for i in range(12):
        verdict = g.before_call("t", {"a": i})
        assert verdict.decision is not Decision.BLOCK
        g.observe("t", {"a": i}, {"error": "x"})
        g.begin_round(progressed=False)


def test_progress_interrupts_the_stall_counter():
    g = _guard()
    for _ in range(4):
        g.before_call("t", {"a": 1})
        g.observe("t", {"a": 1}, {"error": "x"})
        g.begin_round(progressed=False)
    g.begin_round(progressed=True)
    assert g.before_call("t", {"a": 1}).decision is not Decision.BLOCK


def test_a_healthy_long_turn_is_never_blocked():
    """Six successful rounds must not trip anything - the deep-research case."""
    g = _guard()
    for i in range(20):
        verdict = g.before_call("search", {"q": i})
        assert verdict.decision is Decision.ALLOW
        g.observe("search", {"q": i}, {"ok": True})
        g.begin_round(progressed=True)


def test_alternating_success_and_failure_is_allowed():
    g = _guard()
    for i in range(10):
        g.before_call("t", {"i": i})
        g.observe("t", {"i": i}, {"ok": True} if i % 2 else {"error": "x"})
        g.begin_round(progressed=True)
    assert g.before_call("t", {"i": 99}).decision is not Decision.BLOCK


# ── blocked results ─────────────────────────────────────────────────────────


def test_blocked_result_is_shaped_like_a_tool_result():
    g = _guard()
    _fail(g, "t", {"a": 1}, rounds=6)
    payload = g.before_call("t", {"a": 1}).as_result()
    assert payload["blocked"] is True
    assert payload["guardrail"] == "repeated_failure"
    assert payload["tool"] == "t"
    assert payload["occurrences"] == 6
    assert payload["error"]


def test_blocked_result_tells_the_model_not_to_retry():
    g = _guard()
    _fail(g, "t", {"a": 1}, rounds=6)
    assert "will not help" in g.before_call("t", {"a": 1}).as_result()["error"]


# ── halt / telemetry ────────────────────────────────────────────────────────


def test_halt_is_sticky():
    g = _guard()
    assert g.halted is False
    g.halt("tool_misbehaving")
    assert g.halted is True
    assert g.halted_reason == "tool_misbehaving"


def test_snapshot_reports_state():
    g = _guard()
    g.observe("t", {"a": 1}, {"error": "x"})
    g.observe("t", {"a": 2}, {"ok": True})
    snap = g.snapshot()
    assert snap["tool_calls"] == {"t": 2}
    assert snap["tool_fail_counts"] == {"t": 1}
    assert snap["stale_rounds"] == 0


def test_thresholds_are_overridable():
    g = LoopGuard(exact_fail_warn=1, exact_fail_block=2)
    _fail(g, "t", {"a": 1}, rounds=1)
    assert g.before_call("t", {"a": 1}).decision is Decision.WARN
    _fail(g, "t", {"a": 1}, rounds=1)
    assert g.before_call("t", {"a": 1}).decision is Decision.BLOCK