File size: 13,703 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
"""Initial output is released while the real upstream read remains blocked."""

import asyncio
import json

import httpx
import httpx2
import pytest

from free_claude_code.api.response_streams import (
    ManagedStreamingResponse,
    anthropic_sse_streaming_response,
    bind_response_lifetime,
)
from free_claude_code.core.anthropic.stream_contracts import parse_sse_text
from tests.api.test_delivered_stream_recovery import (
    assert_completed,
    public_text,
    text_events,
)
from tests.api.test_hidden_stream_retries import (
    assert_winning_tool,
    executed,
    partial_tool,
)
from tests.api.test_response_streams import _json_error, _serve
from tests.api.test_tool_call_buffer import _response
from tests.api.test_tool_call_buffer_transports import _tools
from tests.providers.test_history_transports import _events_for, _harness
from tests.providers.test_native_tool_arguments import tool_events


def encode(protocol, events):
    return "".join(
        (f"event: {event['type']}\n" if protocol == "messages" else "")
        + f"data: {json.dumps(event)}\n\n"
        for event in events
    ).encode()


class GatedBody(httpx.AsyncByteStream, httpx2.AsyncByteStream):
    def __init__(self, prefix, suffix):
        self.prefix = prefix
        self.suffix = suffix
        self.waiting = asyncio.Event()
        self.release = asyncio.Event()
        self.cancelled = False
        self.close_count = 0

    async def __aiter__(self):
        yield self.prefix
        self.waiting.set()
        try:
            await self.release.wait()
        except asyncio.CancelledError:
            self.cancelled = True
            raise
        yield self.suffix

    async def aclose(self):
        self.close_count += 1


@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["chat", "messages", "responses"])
@pytest.mark.parametrize("wire", ["messages", "responses"])
async def test_initial_text_reaches_http_without_another_upstream_event(protocol, wire):
    events = text_events(protocol, "buffered text")
    prefix_size = len(text_events(protocol, "buffered text", complete=False))
    body = GatedBody(
        encode(protocol, events[:prefix_size]),
        encode(protocol, events[prefix_size:]),
    )
    visible = asyncio.Event()
    sent = []

    async def capture(message):
        data = message.get("body", b"")
        sent.append(data)
        if b"buffered text" in data:
            visible.set()

    async with _harness(protocol, lambda _: (200, body)) as (send, bodies, _):

        async def serve():
            source = send(wire, [{"role": "user", "content": "hello"}])
            response = await _response(wire, executed(source, wire))
            await _serve(response, send=capture)

        serving = asyncio.create_task(serve())
        wait_visible = asyncio.create_task(visible.wait())
        try:
            await asyncio.wait_for(body.waiting.wait(), 2)
            done, _ = await asyncio.wait({wait_visible, serving}, timeout=1)
            assert wait_visible in done, "Buffered text is still waiting for upstream"
            assert not body.cancelled
            assert not body.release.is_set()
            assert not serving.done()
            assert len(bodies) == 1
        finally:
            body.release.set()
            wait_visible.cancel()
            await asyncio.gather(wait_visible, return_exceptions=True)
            await asyncio.wait_for(serving, 2)

    output = parse_sse_text(b"".join(sent).decode())
    assert public_text(output, wire) == "buffered text"
    assert_completed(output, wire)
    assert body.close_count == 1


@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["chat", "messages", "responses"])
@pytest.mark.parametrize("wire", ["messages", "responses"])
@pytest.mark.parametrize("ending", ["eof", "error"])
@pytest.mark.parametrize("read_state", ["pending", "between_reads"])
async def test_cutoff_continues_already_released_text(
    protocol, wire, ending, read_state
):
    error = {"type": "api_error", "message": "internal server error"}
    if protocol == "responses":
        failure = {
            "type": "response.failed",
            "sequence_number": 4,
            "response": {
                **text_events(protocol, "abandoned")[-1]["response"],
                "status": "failed",
                "error": {"code": "server_error", "message": "interrupted"},
            },
        }
    else:
        failure = {"type": "error", "error": error}
    prefix = encode(protocol, text_events(protocol, "abandoned", complete=False))
    suffix = encode(protocol, [failure]) if ending == "error" else b""
    if read_state == "pending":
        first = GatedBody(prefix, suffix)
    else:
        first = GatedBody(prefix + suffix, b"")
        first.release.set()

    def reply(bodies):
        if len(bodies) == 1:
            return 200, first
        assert first.close_count == 1
        return 200, text_events(protocol, "winning")

    sent = []

    async def capture(message):
        sent.append(message.get("body", b""))

    async with _harness(protocol, reply) as (send, bodies, _):

        async def serve():
            source = send(wire, [{"role": "user", "content": "hello"}])
            response = await _response(wire, executed(source, wire))
            await _serve(response, send=capture)

        serving = asyncio.create_task(serve())
        try:
            if read_state == "pending":
                await asyncio.wait_for(first.waiting.wait(), 2)
                first.release.set()
            await asyncio.wait_for(serving, 2)
        finally:
            first.release.set()
            if not serving.done():
                serving.cancel()
            await asyncio.gather(serving, return_exceptions=True)
        assert len(bodies) == 2
        assert bodies[1] != bodies[0]
        assert "abandoned" in json.dumps(bodies[1])

    output = parse_sse_text(b"".join(sent).decode())
    assert public_text(output, wire) == "abandonedwinning"
    assert_completed(output, wire)


@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["chat", "messages", "responses"])
@pytest.mark.parametrize("wire", ["messages", "responses"])
async def test_failure_after_immediate_release_continues_the_visible_answer(
    protocol, wire
):
    first = GatedBody(
        encode(protocol, text_events(protocol, "Hello ", complete=False)), b""
    )
    visible = asyncio.Event()
    sent = []

    async def capture(message):
        data = message.get("body", b"")
        sent.append(data)
        if b"Hello " in data:
            visible.set()

    async with _harness(
        protocol,
        lambda bodies: (
            200,
            first if len(bodies) == 1 else text_events(protocol, "world."),
        ),
    ) as (send, bodies, _):

        async def serve():
            source = send(wire, [{"role": "user", "content": "hello"}])
            response = await _response(wire, executed(source, wire))
            await _serve(response, send=capture)

        serving = asyncio.create_task(serve())
        try:
            await asyncio.wait_for(visible.wait(), 2)
            assert not first.release.is_set()
        finally:
            first.release.set()
            await asyncio.wait_for(serving, 2)
        assert len(bodies) == 2
        assert "Hello " in json.dumps(bodies[1])

    output = parse_sse_text(b"".join(sent).decode())
    assert public_text(output, wire) == "Hello world."
    assert_completed(output, wire)
    assert first.close_count == 1


@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["chat", "messages", "responses"])
@pytest.mark.parametrize("wire", ["messages", "responses"])
@pytest.mark.parametrize("released", [False, True])
async def test_cancellation_drains_the_read_before_closing_its_http_body(
    protocol, wire, released
):
    body = GatedBody(
        encode(
            protocol,
            text_events(protocol, "visible", complete=False)
            if released
            else partial_tool(protocol),
        ),
        b"",
    )
    visible = asyncio.Event()

    async def capture(message):
        if b"visible" in message.get("body", b""):
            visible.set()

    async with _harness(protocol, lambda _: (200, body)) as (send, bodies, _):

        async def serve():
            source = send(
                wire, [{"role": "user", "content": "hello"}], tools=_tools(wire)
            )
            response = await _response(wire, executed(source, wire))
            await _serve(response, send=capture)

        serving = asyncio.create_task(serve())
        try:
            await asyncio.wait_for(body.waiting.wait(), 2)
            if released:
                await asyncio.wait_for(visible.wait(), 2)
            else:
                assert not visible.is_set()
            serving.cancel()
            with pytest.raises(asyncio.CancelledError):
                await asyncio.wait_for(serving, 2)
            assert body.cancelled
            assert body.close_count == 1
            assert len(bodies) == 1
        finally:
            body.release.set()
            if not serving.done():
                serving.cancel()
            await asyncio.gather(serving, return_exceptions=True)


@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["chat", "messages", "responses"])
@pytest.mark.parametrize("wire", ["messages", "responses"])
async def test_closing_prefetched_response_closes_without_starting_another_read(
    protocol, wire
):
    body = GatedBody(
        encode(protocol, text_events(protocol, "held", complete=False)), b""
    )
    released = []

    async def release():
        released.append("released")

    async with _harness(protocol, lambda _: (200, body)) as (send, bodies, _):
        source = send(wire, [{"role": "user", "content": "hello"}])
        response = await asyncio.wait_for(_response(wire, executed(source, wire)), 2)
        await bind_response_lifetime(response, release)
        try:
            assert not body.waiting.is_set()
            assert not body.release.is_set()
            await response.aclose()
            await response.aclose()
            assert not body.cancelled
            assert body.close_count == 1
            assert released == ["released"]
            assert len(bodies) == 1
        finally:
            body.release.set()
            await response.aclose()


@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["chat", "messages", "responses"])
@pytest.mark.parametrize("wire", ["messages", "responses"])
async def test_immediate_delivery_keeps_incomplete_tools_hidden_and_retryable(
    protocol, wire
):
    first = GatedBody(encode(protocol, partial_tool(protocol)), b"")
    started = asyncio.Event()
    sent = []

    async def capture(message):
        if message["type"] == "http.response.start":
            started.set()
        sent.append(message.get("body", b""))

    async with _harness(
        protocol,
        lambda bodies: (
            200,
            first if len(bodies) == 1 else tool_events(protocol, '{"path":"winning"}'),
        ),
    ) as (send, bodies, _):

        async def serve():
            source = send(
                wire, [{"role": "user", "content": "read"}], tools=_tools(wire)
            )
            response = await _response(wire, executed(source, wire))
            await _serve(response, send=capture)

        serving = asyncio.create_task(serve())
        try:
            await asyncio.wait_for(started.wait(), 2)
            assert not first.release.is_set()
            assert b"call_abandoned" not in b"".join(sent)
        finally:
            first.release.set()
            await asyncio.wait_for(serving, 2)
        assert len(bodies) == 2
        assert bodies[0] == bodies[1]
    assert_winning_tool(b"".join(sent).decode(), wire)
    assert first.close_count == 1


@pytest.mark.asyncio
@pytest.mark.parametrize("protocol", ["chat", "messages", "responses"])
async def test_immediate_delivery_keeps_projected_reasoning_hidden_and_retryable(
    protocol,
):
    prefix_size = {"chat": 1, "messages": 3, "responses": 4}[protocol]
    first = GatedBody(encode(protocol, _events_for(protocol)[:prefix_size]), b"")
    started = asyncio.Event()
    sent = []

    async def capture(message):
        if message["type"] == "http.response.start":
            started.set()
        sent.append(message.get("body", b""))

    async with _harness(
        protocol,
        lambda bodies: (
            200,
            first if len(bodies) == 1 else text_events(protocol, "winning"),
        ),
    ) as (send, bodies, _):

        async def serve():
            source = send("messages", [{"role": "user", "content": "hello"}])
            response = await anthropic_sse_streaming_response(
                executed(source, "messages"),
                pre_start_error_response=_json_error,
                request_id="hidden-reasoning",
                hide_reasoning=True,
            )
            assert isinstance(response, ManagedStreamingResponse)
            await _serve(response, send=capture)

        serving = asyncio.create_task(serve())
        try:
            await asyncio.wait_for(started.wait(), 2)
            assert not first.release.is_set()
            assert b"Find 17." not in b"".join(sent)
        finally:
            first.release.set()
            await asyncio.wait_for(serving, 2)
        assert len(bodies) == 2
        assert bodies[0] == bodies[1]
    raw = b"".join(sent).decode()
    assert "Find 17." not in raw
    output = parse_sse_text(raw)
    assert public_text(output, "messages") == "winning"
    assert_completed(output, "messages")