File size: 4,275 Bytes
a9e27c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The pure step that follows one failed model candidate."""

import pytest

from free_claude_code.application.model_fallback.decision import (
    CandidateFailure,
    Fail,
    Fallback,
    Handoff,
    next_step,
)
from free_claude_code.core.stream_recovery import (
    ContinuationSeed,
    DeliveredPrefix,
    RecoveryAction,
    SourceRecoverySnapshot,
    StreamFailureContext,
)

RESTART = RecoveryAction.RESTART
CONTINUE = RecoveryAction.CONTINUE
SEED = ContinuationSeed("messages", ({"type": "text", "text": "a"},))


def _prefix(*, has_calls: bool = False) -> DeliveredPrefix:
    return DeliveredPrefix(
        text="a",
        thinking="",
        has_calls=has_calls,
        eligible=True,
        can_handoff=True,
        unsafe_reason=None,
        continuation=SEED,
    )


def _failure(
    source: SourceRecoverySnapshot | None, *, produced_output: bool = False
) -> CandidateFailure:
    return CandidateFailure(
        context=StreamFailureContext(source) if source is not None else None,
        retryable=True,
        produced_output=produced_output,
    )


CLEAN = SourceRecoverySnapshot()


@pytest.mark.parametrize(
    ("failure", "released", "prefix", "seed", "has_next", "expected"),
    [
        pytest.param(
            _failure(None),
            False,
            None,
            None,
            True,
            Fallback(None, RESTART, "no_candidate_output"),
            id="no_context_before_delivery",
        ),
        pytest.param(
            _failure(None, produced_output=True),
            False,
            None,
            None,
            True,
            Fallback(None, RESTART, "no_candidate_output"),
            id="no_context_hidden_output",
        ),
        pytest.param(
            _failure(None),
            True,
            None,
            SEED,
            True,
            Fallback(SEED, CONTINUE, "no_candidate_output"),
            id="no_context_silent_continuation",
        ),
        pytest.param(
            _failure(None, produced_output=True),
            True,
            None,
            SEED,
            True,
            Fail(),
            id="no_context_after_own_output",
        ),
        pytest.param(
            _failure(None),
            True,
            None,
            None,
            True,
            Fail(),
            id="no_context_released_without_seed",
        ),
        pytest.param(
            _failure(CLEAN),
            False,
            None,
            None,
            True,
            Fallback(None, RESTART, "no_delivered_content"),
            id="restart",
        ),
        pytest.param(
            _failure(CLEAN),
            True,
            _prefix(),
            None,
            True,
            Fallback(SEED, CONTINUE, "portable_prefix"),
            id="continue",
        ),
        pytest.param(
            _failure(CLEAN),
            True,
            _prefix(has_calls=True),
            None,
            False,
            Handoff(),
            id="handoff_even_on_last_candidate",
        ),
        pytest.param(
            _failure(SourceRecoverySnapshot(normal_stop_seen=True)),
            True,
            _prefix(),
            None,
            True,
            Fail(),
            id="stop",
        ),
        pytest.param(
            _failure(CLEAN),
            True,
            _prefix(),
            None,
            False,
            Fail(),
            id="no_candidates_left",
        ),
        pytest.param(
            _failure(None),
            False,
            None,
            None,
            False,
            Fail(),
            id="no_candidates_left_without_context",
        ),
    ],
)
def test_next_step(failure, released, prefix, seed, has_next, expected):
    assert (
        next_step(
            failure,
            content_released=released,
            prefix=prefix,
            seed=seed,
            has_next=has_next,
        )
        == expected
    )


def test_prefix_is_ignored_without_stream_evidence():
    step = next_step(
        _failure(None),
        content_released=True,
        prefix=_prefix(has_calls=True),
        seed=SEED,
        has_next=True,
    )

    assert step == Fallback(SEED, CONTINUE, "no_candidate_output")