Spaces:
Running
Running
File size: 6,202 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 | import asyncio
import json
import pytest
from free_claude_code.api.handlers.classifier_response import classifier_response
from free_claude_code.core.anthropic.stream_contracts import parse_sse_text
from free_claude_code.core.anthropic.streaming import format_sse_event
def wire(payloads):
return "".join(format_sse_event(payload["type"], payload) for payload in payloads)
@pytest.mark.asyncio
@pytest.mark.parametrize("chunk_size", [1, 17, 10000])
async def test_projection_preserves_text_extensions_and_sparse_interleaved_blocks(
chunk_size,
):
literal = "<thinking>Classifier explanation</thinking><severity>0</severity>"
start = {
"type": "message_start",
"message": {
"id": "original",
"model": "public",
"content": [],
"usage": {"input_tokens": 3},
},
"extension": {"a": 1},
}
terminal = {
"type": "message_delta",
"delta": {"stop_reason": "stop_sequence", "stop_sequence": "custom"},
"usage": {"output_tokens": 50, "extra": 7},
}
payloads = [
start,
{
"type": "content_block_start",
"index": 5,
"content_block": {"type": "thinking", "thinking": "hidden"},
},
{
"type": "content_block_start",
"index": 9,
"content_block": {
"type": "text",
"text": literal,
"citations": [],
"extension": {"index": 9},
},
},
{
"type": "content_block_delta",
"index": 5,
"delta": {"type": "signature_delta", "signature": "hidden"},
},
{
"type": "content_block_delta",
"index": 9,
"delta": {
"type": "text_delta",
"text": "<reason>fine</reason><category>read</category>",
},
},
{"type": "content_block_stop", "index": 5},
{
"type": "content_block_start",
"index": 12,
"content_block": {"type": "redacted_thinking", "data": "opaque"},
},
{"type": "content_block_stop", "index": 12},
{
"type": "content_block_start",
"index": 3,
"content_block": {
"type": "tool_use",
"id": "tool-id",
"name": "read",
"input": {},
},
},
{"type": "content_block_stop", "index": 9},
{"type": "content_block_stop", "index": 3},
{"type": "ping", "extension": 1},
terminal,
{"type": "message_stop"},
]
raw = wire(payloads)
closed = False
async def source():
nonlocal closed
try:
for offset in range(0, len(raw), chunk_size):
yield raw[offset : offset + chunk_size]
finally:
closed = True
output = "".join([chunk async for chunk in classifier_response(source())])
actual = [event.data for event in parse_sse_text(output)]
assert actual == [
start,
{**payloads[2], "index": 0},
{**payloads[4], "index": 0},
{**payloads[8], "index": 1},
{**payloads[9], "index": 0},
{**payloads[10], "index": 1},
payloads[11],
terminal,
payloads[13],
]
assert closed
@pytest.mark.asyncio
async def test_reasoning_only_completion_stays_empty_with_original_termination():
start = {"type": "message_start", "message": {"content": []}}
end = {
"type": "message_delta",
"delta": {"stop_reason": "max_tokens"},
"usage": {"output_tokens": 64},
}
async def source():
yield wire(
[
start,
{
"type": "content_block_start",
"index": 0,
"content_block": {"type": "thinking", "thinking": "analysis"},
},
{"type": "content_block_stop", "index": 0},
end,
{"type": "message_stop"},
]
)
output = [
event.data
for event in parse_sse_text(
"".join([chunk async for chunk in classifier_response(source())])
)
]
assert output == [start, end, {"type": "message_stop"}]
@pytest.mark.asyncio
async def test_cancellation_during_hidden_reasoning_closes_input():
blocked = asyncio.Event()
closed = asyncio.Event()
async def source():
try:
yield wire([{"type": "message_start", "message": {"content": []}}])
yield wire(
[
{
"type": "content_block_start",
"index": 0,
"content_block": {"type": "thinking", "thinking": ""},
}
]
)
blocked.set()
await asyncio.Event().wait()
finally:
closed.set()
filtered = classifier_response(source())
assert "message_start" in await anext(filtered)
async def advance():
return await anext(filtered)
pending = asyncio.create_task(advance())
await asyncio.wait_for(blocked.wait(), 1)
pending.cancel()
with pytest.raises(asyncio.CancelledError):
await pending
assert closed.is_set()
@pytest.mark.asyncio
@pytest.mark.parametrize("fails", [False, True])
async def test_only_normal_exhaustion_flushes_unterminated_event(fails):
failure = RuntimeError("upstream failed")
raw = "event: error\ndata: " + json.dumps(
{"type": "error", "error": {"message": "unchanged"}}
)
async def source():
yield raw
if fails:
raise failure
if fails:
with pytest.raises(RuntimeError) as caught:
_ = [chunk async for chunk in classifier_response(source())]
assert caught.value is failure
else:
output = "".join([chunk async for chunk in classifier_response(source())])
assert parse_sse_text(output)[0].data == {
"type": "error",
"error": {"message": "unchanged"},
}
|