File size: 18,923 Bytes
8ace4ff
 
 
 
 
4aecccf
 
8ace4ff
 
 
4aecccf
8ace4ff
4aecccf
0d0f0f9
4aecccf
8ace4ff
0602b26
fcd6f91
8ace4ff
db883b3
 
 
 
0d0f0f9
db883b3
8ace4ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db883b3
 
8ace4ff
db883b3
8ace4ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4aecccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ace4ff
 
 
 
 
 
 
 
 
 
 
 
db883b3
8ace4ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db883b3
 
 
 
 
 
 
 
8ace4ff
db883b3
8ace4ff
 
 
 
db883b3
8ace4ff
 
 
 
 
4aecccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64dfa67
fcd6f91
 
 
 
 
 
 
64dfa67
 
fcd6f91
 
 
 
 
64dfa67
 
 
 
 
8ace4ff
 
 
 
 
 
 
 
db883b3
8ace4ff
 
 
 
 
 
fcd6f91
0602b26
 
 
 
 
 
 
 
 
 
 
 
 
4aecccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0602b26
 
 
4aecccf
0602b26
 
 
 
 
4aecccf
 
0602b26
 
 
 
 
 
4aecccf
0602b26
 
 
 
 
4aecccf
0602b26
 
 
 
 
 
 
 
 
 
 
 
4aecccf
0602b26
 
 
4aecccf
 
 
 
0602b26
4aecccf
 
0602b26
4aecccf
0602b26
 
 
 
4aecccf
 
 
0602b26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4aecccf
0602b26
4aecccf
0602b26
 
 
 
 
 
 
 
4aecccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
"""Tests for public SSE response start gating."""

import asyncio
import json
from collections.abc import AsyncGenerator
from typing import Any, cast
from unittest.mock import AsyncMock, patch

import pytest
from fastapi.responses import JSONResponse, StreamingResponse
from starlette.types import Message, Scope

from free_claude_code.api.request_ids import RequestCorrelationMiddleware
from free_claude_code.api.response_streams import (
    ManagedStreamingResponse,
    anthropic_sse_streaming_response,
    bind_response_lifetime,
    terminal_execution_error_response,
)
from free_claude_code.core.anthropic import (
    anthropic_error_payload,
    anthropic_failure_payload,
)
from free_claude_code.core.anthropic.stream_contracts import parse_sse_text
from free_claude_code.core.failures import ExecutionFailure, FailureKind


async def _body_chunks(chunks: list[str]) -> AsyncGenerator[str]:
    for chunk in chunks:
        yield chunk


async def _body_raises(exc: BaseException) -> AsyncGenerator[str]:
    raise exc
    yield "unreachable"


async def _body_then_raises(
    chunks: list[str], exc: BaseException
) -> AsyncGenerator[str]:
    for chunk in chunks:
        yield chunk
    raise exc


def _json_error(exc: BaseException) -> JSONResponse:
    if isinstance(exc, ExecutionFailure):
        return terminal_execution_error_response(
            status_code=exc.status_code,
            content=anthropic_failure_payload(exc),
        )
    return JSONResponse(
        status_code=500,
        content={
            "type": "error",
            "error": {"type": "api_error", "message": "failed"},
        },
    )


async def _drain(response: StreamingResponse) -> str:
    parts = [
        chunk.decode("utf-8") if isinstance(chunk, bytes) else str(chunk)
        async for chunk in response.body_iterator
    ]
    return "".join(parts)


def _http_scope() -> Scope:
    return cast(
        Scope,
        {
            "type": "http",
            "asgi": {"version": "3.0", "spec_version": "2.4"},
            "http_version": "1.1",
            "method": "POST",
            "scheme": "http",
            "path": "/v1/messages",
            "raw_path": b"/v1/messages",
            "query_string": b"",
            "headers": [],
            "client": None,
            "server": None,
        },
    )


async def _serve(
    response: StreamingResponse,
    *,
    send: Any | None = None,
) -> list[Message]:
    messages: list[Message] = []

    async def receive() -> Message:
        raise AssertionError("ASGI spec 2.4 responses must not read receive")

    async def collect(message: Message) -> None:
        messages.append(message)

    await response(_http_scope(), receive, send or collect)
    return messages


@pytest.mark.asyncio
async def test_anthropic_response_waits_for_first_chunk_before_returning() -> None:
    ready = asyncio.Event()

    async def body() -> AsyncGenerator[str]:
        await ready.wait()
        yield 'event: message_start\ndata: {"type":"message_start"}\n\n'

    task = asyncio.create_task(
        anthropic_sse_streaming_response(
            body(),
            pre_start_error_response=_json_error,
            request_id="req_test",
        )
    )

    await asyncio.sleep(0)
    assert not task.done()

    ready.set()
    response = await asyncio.wait_for(task, timeout=1)
    assert isinstance(response, StreamingResponse)
    assert "message_start" in await _drain(response)


@pytest.mark.asyncio
async def test_anthropic_pre_start_provider_error_returns_non_200_json() -> None:
    response = await anthropic_sse_streaming_response(
        _body_raises(
            ExecutionFailure(
                kind=FailureKind.RATE_LIMIT,
                status_code=429,
                message="provider says slow down",
                retryable=True,
            )
        ),
        pre_start_error_response=_json_error,
        request_id="req_test",
    )

    assert isinstance(response, JSONResponse)
    assert response.status_code == 429
    assert response.headers["x-should-retry"] == "false"
    body = json.loads(bytes(response.body))
    assert body["error"]["type"] == "rate_limit_error"
    assert body["error"]["message"] == "provider says slow down"


@pytest.mark.asyncio
async def test_pre_start_failure_closes_body_before_response_release() -> None:
    lifecycle: list[str] = []

    class FailingBody:
        def __aiter__(self):
            return self

        async def __anext__(self) -> str:
            raise RuntimeError("provider failed")

        async def aclose(self) -> None:
            lifecycle.append("body_closed")

    async def release() -> None:
        lifecycle.append("lease_released")

    response = await anthropic_sse_streaming_response(
        FailingBody(),
        pre_start_error_response=_json_error,
        request_id="req_pre_start_order",
    )
    await bind_response_lifetime(response, release)

    assert lifecycle == ["body_closed", "lease_released"]


@pytest.mark.asyncio
async def test_terminal_execution_error_response_disables_client_retry() -> None:
    response = terminal_execution_error_response(
        status_code=429,
        content=anthropic_error_payload(
            error_type="rate_limit_error",
            message="provider says slow down",
        ),
    )

    assert isinstance(response, JSONResponse)
    assert response.status_code == 429
    assert response.headers["x-should-retry"] == "false"
    body = json.loads(bytes(response.body))
    assert body["error"] == {
        "type": "rate_limit_error",
        "message": "provider says slow down",
    }


@pytest.mark.asyncio
async def test_anthropic_post_start_exception_emits_terminal_error_frame() -> None:
    response = await anthropic_sse_streaming_response(
        _body_then_raises(
            ['event: message_start\ndata: {"type":"message_start"}\n\n'],
            RuntimeError("socket cut"),
        ),
        pre_start_error_response=_json_error,
        request_id="req_test",
    )

    assert isinstance(response, StreamingResponse)
    text = await _drain(response)
    events = parse_sse_text(text)
    assert [event.event for event in events] == ["message_start", "error"]
    assert events[-1].data["error"]["message"] == "socket cut"


@pytest.mark.asyncio
async def test_non_streaming_response_releases_resource_before_return() -> None:
    release = AsyncMock()
    response = JSONResponse({"ok": True})

    result = await bind_response_lifetime(response, release)

    assert result is response
    release.assert_awaited_once()


@pytest.mark.asyncio
async def test_unmanaged_stream_is_closed_and_released_before_rejection() -> None:
    release = AsyncMock()
    close = AsyncMock()

    class Body:
        def __aiter__(self):
            return self

        async def __anext__(self) -> str:
            return "unreachable"

        async def aclose(self) -> None:
            await close()

    response = StreamingResponse(Body())

    with pytest.raises(TypeError, match="ManagedStreamingResponse"):
        await bind_response_lifetime(response, release)

    close.assert_awaited_once()
    release.assert_awaited_once()


@pytest.mark.asyncio
async def test_streaming_response_releases_after_normal_completion() -> None:
    release = AsyncMock()
    response = ManagedStreamingResponse(_body_chunks(["one", "two"]))

    result = await bind_response_lifetime(response, release)

    assert result is response
    release.assert_not_awaited()
    messages = await _serve(response)
    assert b"".join(message.get("body", b"") for message in messages) == b"onetwo"
    release.assert_awaited_once()


@pytest.mark.asyncio
async def test_streaming_response_releases_after_body_failure() -> None:
    release = AsyncMock()
    response = ManagedStreamingResponse(
        _body_then_raises(["one"], RuntimeError("stream failed"))
    )
    await bind_response_lifetime(response, release)

    with pytest.raises(RuntimeError, match="stream failed"):
        await _serve(response)

    release.assert_awaited_once()


@pytest.mark.asyncio
async def test_streaming_response_releases_when_consumer_closes_early() -> None:
    release = AsyncMock()
    source_closed = asyncio.Event()

    async def body() -> AsyncGenerator[str]:
        try:
            yield "one"
            yield "two"
        finally:
            source_closed.set()

    response = await anthropic_sse_streaming_response(
        body(),
        pre_start_error_response=_json_error,
        request_id="req_test",
    )
    assert isinstance(response, ManagedStreamingResponse)
    await bind_response_lifetime(response, release)

    await response.aclose()

    assert source_closed.is_set()
    release.assert_awaited_once()

    await response.aclose()
    release.assert_awaited_once()


@pytest.mark.asyncio
async def test_streaming_response_releases_when_consumer_is_cancelled() -> None:
    release = AsyncMock()
    entered = asyncio.Event()
    source_closed = asyncio.Event()

    async def body() -> AsyncGenerator[str]:
        try:
            yield "one"
            entered.set()
            await asyncio.Event().wait()
        finally:
            source_closed.set()

    response = ManagedStreamingResponse(body())
    await bind_response_lifetime(response, release)
    drain_task = asyncio.create_task(_serve(response))
    await entered.wait()

    drain_task.cancel()
    with pytest.raises(asyncio.CancelledError):
        await drain_task

    assert source_closed.is_set()
    release.assert_awaited_once()


@pytest.mark.asyncio
async def test_response_start_send_failure_closes_prefetched_tail_and_releases() -> (
    None
):
    release = AsyncMock()
    source_closed = asyncio.Event()

    async def body() -> AsyncGenerator[str]:
        try:
            yield "prefetched"
            yield "tail"
        finally:
            source_closed.set()

    response = await anthropic_sse_streaming_response(
        body(),
        pre_start_error_response=_json_error,
        request_id="req_test",
    )
    assert isinstance(response, ManagedStreamingResponse)
    await bind_response_lifetime(response, release)

    async def fail_on_start(message: Message) -> None:
        assert message["type"] == "http.response.start"
        raise RuntimeError("send start failed")

    with pytest.raises(RuntimeError, match="send start failed"):
        await _serve(response, send=fail_on_start)

    assert source_closed.is_set()
    release.assert_awaited_once()


@pytest.mark.asyncio
async def test_first_body_send_failure_closes_prefetched_tail_and_releases() -> None:
    release = AsyncMock()
    source_closed = asyncio.Event()

    async def body() -> AsyncGenerator[str]:
        try:
            yield "prefetched"
            yield "tail"
        finally:
            source_closed.set()

    response = await anthropic_sse_streaming_response(
        body(),
        pre_start_error_response=_json_error,
        request_id="req_test",
    )
    assert isinstance(response, ManagedStreamingResponse)
    await bind_response_lifetime(response, release)

    async def fail_on_first_body(message: Message) -> None:
        if message["type"] == "http.response.body":
            raise RuntimeError("send body failed")

    with pytest.raises(RuntimeError, match="send body failed"):
        await _serve(response, send=fail_on_first_body)

    assert source_closed.is_set()
    release.assert_awaited_once()


@pytest.mark.asyncio
async def test_asgi_23_correlation_boundary_preserves_response_start_cleanup() -> None:
    release = AsyncMock()
    source_closed = asyncio.Event()

    async def body() -> AsyncGenerator[str]:
        try:
            yield "prefetched"
            yield "tail"
        finally:
            source_closed.set()

    response = await anthropic_sse_streaming_response(
        body(),
        pre_start_error_response=_json_error,
        request_id="req_test",
    )
    assert isinstance(response, ManagedStreamingResponse)
    await bind_response_lifetime(response, release)

    async def app(scope, receive, send) -> None:
        await response(scope, receive, send)

    async def receive() -> Message:
        await asyncio.Event().wait()
        raise AssertionError("unreachable")

    async def fail_on_start(message: Message) -> None:
        assert message["type"] == "http.response.start"
        raise OSError("client disconnected")

    scope = _http_scope()
    scope["asgi"]["spec_version"] = "2.3"

    with pytest.raises(OSError, match="client disconnected"):
        await RequestCorrelationMiddleware(app)(scope, receive, fail_on_start)

    assert source_closed.is_set()
    release.assert_awaited_once()


@pytest.mark.asyncio
async def test_first_body_source_failure_releases_response_lifetime() -> None:
    release = AsyncMock()
    response = ManagedStreamingResponse(_body_raises(RuntimeError("source failed")))
    await bind_response_lifetime(response, release)

    with pytest.raises(RuntimeError, match="source failed"):
        await _serve(response)

    release.assert_awaited_once()


@pytest.mark.asyncio
async def test_repeated_cancellation_waits_for_close_and_release_completion() -> None:
    close_started = asyncio.Event()
    allow_close = asyncio.Event()
    close_finished = asyncio.Event()
    release_started = asyncio.Event()
    allow_release = asyncio.Event()
    release_finished = asyncio.Event()

    class GatedBody:
        def __aiter__(self):
            return self

        async def __anext__(self) -> str:
            raise StopAsyncIteration

        async def aclose(self) -> None:
            close_started.set()
            await allow_close.wait()
            close_finished.set()

    async def release() -> None:
        release_started.set()
        await allow_release.wait()
        release_finished.set()

    response = ManagedStreamingResponse(GatedBody())
    await bind_response_lifetime(response, release)
    closing = asyncio.create_task(response.aclose())
    await close_started.wait()

    closing.cancel()
    allow_close.set()
    await release_started.wait()
    closing.cancel()
    allow_release.set()

    with pytest.raises(asyncio.CancelledError):
        await closing

    assert close_finished.is_set()
    assert release_finished.is_set()


@pytest.mark.asyncio
async def test_repeated_pre_start_cancellation_waits_for_body_close() -> None:
    iteration_started = asyncio.Event()
    close_started = asyncio.Event()
    allow_close = asyncio.Event()
    close_finished = asyncio.Event()

    class GatedPreStartBody:
        def __aiter__(self):
            return self

        async def __anext__(self) -> str:
            iteration_started.set()
            await asyncio.Event().wait()
            raise AssertionError("unreachable")

        async def aclose(self) -> None:
            close_started.set()
            await allow_close.wait()
            close_finished.set()

    response_task = asyncio.create_task(
        anthropic_sse_streaming_response(
            GatedPreStartBody(),
            pre_start_error_response=_json_error,
            request_id="req_pre_start_cancel",
        )
    )
    await iteration_started.wait()

    response_task.cancel()
    await close_started.wait()
    response_task.cancel()
    allow_close.set()

    with pytest.raises(asyncio.CancelledError):
        await response_task

    assert close_finished.is_set()


@pytest.mark.asyncio
async def test_cancellation_during_pre_start_error_cleanup_waits_for_close() -> None:
    close_started = asyncio.Event()
    allow_close = asyncio.Event()
    close_finished = asyncio.Event()

    class FailingPreStartBody:
        def __aiter__(self):
            return self

        async def __anext__(self) -> str:
            raise RuntimeError("provider failed")

        async def aclose(self) -> None:
            close_started.set()
            await allow_close.wait()
            close_finished.set()

    response_task = asyncio.create_task(
        anthropic_sse_streaming_response(
            FailingPreStartBody(),
            pre_start_error_response=_json_error,
            request_id="req_pre_start_error_cancel",
        )
    )
    await close_started.wait()

    response_task.cancel()
    allow_close.set()

    with pytest.raises(asyncio.CancelledError):
        await response_task

    assert close_finished.is_set()


@pytest.mark.asyncio
async def test_cleanup_failures_are_trace_only_and_do_not_replace_success() -> None:
    class CloseFails:
        def __init__(self) -> None:
            self._yielded = False

        def __aiter__(self):
            return self

        async def __anext__(self) -> str:
            if self._yielded:
                raise StopAsyncIteration
            self._yielded = True
            return "ok"

        async def aclose(self) -> None:
            raise RuntimeError("secret close detail")

    release = AsyncMock(side_effect=RuntimeError("secret release detail"))
    response = ManagedStreamingResponse(CloseFails())
    await bind_response_lifetime(response, release)

    with (
        patch("free_claude_code.core.trace.trace_event") as close_trace,
        patch("free_claude_code.api.response_streams.trace_event") as release_trace,
    ):
        messages = await _serve(response)

    assert b"".join(message.get("body", b"") for message in messages) == b"ok"
    close_trace.assert_called_once()
    assert close_trace.call_args.kwargs["owner"] == "ManagedStreamingResponse"
    assert close_trace.call_args.kwargs["close_exc_type"] == "RuntimeError"
    assert release_trace.call_args.kwargs["operation"] == "release_resource"
    trace_blob = " ".join(
        str(call)
        for call in [*close_trace.call_args_list, *release_trace.call_args_list]
    )
    assert "secret close detail" not in trace_blob
    assert "secret release detail" not in trace_blob


@pytest.mark.asyncio
async def test_body_close_cancellation_propagates_without_releasing() -> None:
    class CloseIsCancelled:
        def __aiter__(self):
            return self

        async def __anext__(self) -> str:
            raise StopAsyncIteration

        async def aclose(self) -> None:
            raise asyncio.CancelledError

    release = AsyncMock()
    response = ManagedStreamingResponse(CloseIsCancelled())
    await bind_response_lifetime(response, release)

    with pytest.raises(asyncio.CancelledError):
        await response.aclose()

    release.assert_not_awaited()


@pytest.mark.asyncio
async def test_lease_release_cancellation_propagates() -> None:
    release = AsyncMock(side_effect=asyncio.CancelledError)
    response = ManagedStreamingResponse(_body_chunks([]))
    await bind_response_lifetime(response, release)

    with pytest.raises(asyncio.CancelledError):
        await response.aclose()

    release.assert_awaited_once()