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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
"""Public API routes preserve parallel Chat calls with imperfect indexes."""

import json

import httpx2
import pytest
from fastapi.testclient import TestClient
from openai import AsyncOpenAI

from free_claude_code.application.response_collection import collect_response
from free_claude_code.config.settings import Settings
from free_claude_code.core.anthropic.stream_contracts import parse_sse_text
from free_claude_code.core.failures import ExecutionFailure
from free_claude_code.providers.google_openai import GoogleOpenAIProvider
from free_claude_code.providers.vertex.client import _PROFILE as GOOGLE_PROFILE
from tests.api.support import create_test_app
from tests.api.test_hidden_stream_retries import delivered
from tests.api.test_tool_call_buffer_transports import _tools
from tests.providers.support import immediate_admission, make_provider_config
from tests.providers.test_chat_tool_identity import call
from tests.providers.test_history_transports import _harness, _saved_reply
from tests.providers.test_native_tool_arguments import tool_events


def chat_chunk(delta, finish=None):
    return {
        "id": "chat_identity",
        "object": "chat.completion.chunk",
        "created": 0,
        "model": "test-model",
        "choices": [{"index": 0, "delta": delta, "finish_reason": finish}],
    }


def google_compat_api_response(monkeypatch, events, wire, streaming, *, max_attempts=2):
    sent = []
    upstream = (
        "".join(f"data: {json.dumps(event)}\n\n" for event in events)
        + "data: [DONE]\n\n"
    )

    def reply(request):
        sent.append(json.loads(request.content))
        return httpx2.Response(
            200, headers={"content-type": "text/event-stream"}, content=upstream
        )

    sdk = AsyncOpenAI(
        api_key="fixture",
        base_url="https://fixture.invalid/v1",
        max_retries=0,
        http_client=httpx2.AsyncClient(transport=httpx2.MockTransport(reply)),
    )
    monkeypatch.setattr(
        "free_claude_code.providers.openai_chat.client.AsyncOpenAI", lambda **_: sdk
    )
    provider = GoogleOpenAIProvider(
        make_provider_config(api_key="fixture", base_url="https://fixture.invalid/v1"),
        profile=GOOGLE_PROFILE,
        admission=immediate_admission(max_attempts=max_attempts),
    )
    app = create_test_app(
        Settings(MODEL="vertex/test-model", ENABLE_WEB_SERVER_TOOLS=False),
        providers={"vertex": provider},
    )
    schema = {"type": "object", "properties": {"path": {"type": "string"}}}
    if wire == "messages":
        body = {
            "model": "vertex/test-model",
            "max_tokens": 100,
            "stream": streaming,
            "messages": [{"role": "user", "content": "read both"}],
            "tools": [{"name": "read", "input_schema": schema}],
        }
    else:
        body = {
            "model": "vertex/test-model",
            "stream": True,
            "input": "read both",
            "tools": [{"type": "function", "name": "read", "parameters": schema}],
        }
    with TestClient(app) as client:
        response = client.post(f"/v1/{wire}", json=body)
    return response, sent


@pytest.mark.parametrize(
    "deltas,expected_ids",
    [
        (
            [
                [
                    call(first, "call_a", '{"path":"a"}'),
                    call(second, "call_b", '{"path":"b"}'),
                ]
            ],
            ("call_a", "call_b"),
        )
        for first, second in [(0, 1), (0, 0), (None, None), ("missing", "missing")]
    ]
    + [
        (
            [
                [call(None, "call_a", '{"path":"a"}')],
                [call(1, None, '{"path":"b"}')],
                [call(1, "call_b", "", name=None)],
            ],
            ("call_a", "call_b"),
        ),
        (
            [
                [call(0, None, '{"path":"a"}')],
                [call(None, "call_b", '{"path":"b"}')],
                [call(0, "call_a", "", name=None)],
            ],
            ("call_a", "call_b"),
        ),
        (
            [
                [
                    call(0, "call_a", '{"path":'),
                    call(0, None, '"a"}', name=None),
                    call(1, "call_b", '{"path":"b"}'),
                ]
            ],
            ("call_a", "call_b"),
        ),
    ],
    ids=[
        "distinct",
        "reused",
        "null",
        "missing",
        "late_id",
        "late_index",
        "same_chunk",
    ],
)
@pytest.mark.parametrize(
    "wire,streaming", [("messages", True), ("messages", False), ("responses", True)]
)
def test_google_compat_parallel_calls_reach_public_api(
    monkeypatch, deltas, expected_ids, wire, streaming
):
    events = [chat_chunk({"tool_calls": calls}) for calls in deltas]
    events.append(chat_chunk({}, "tool_calls"))
    response, sent = google_compat_api_response(monkeypatch, events, wire, streaming)
    assert response.status_code == 200, response.text
    assert len(sent) == 1

    if not streaming:
        values = [
            (item["id"], item["name"], item["input"])
            for item in response.json()["content"]
            if item["type"] == "tool_use"
        ]
    elif wire == "messages":
        calls = {}
        events = parse_sse_text(response.text)
        assert events[-1].event == "message_stop"
        for event in events:
            data = event.data
            block = data.get("content_block", {})
            if block.get("type") == "tool_use":
                calls[data["index"]] = {
                    "id": block["id"],
                    "name": block["name"],
                    "arguments": "",
                }
            if data.get("delta", {}).get("type") == "input_json_delta":
                calls[data["index"]]["arguments"] += data["delta"]["partial_json"]
        values = [
            (item["id"], item["name"], json.loads(item["arguments"]))
            for item in calls.values()
        ]
    else:
        events = parse_sse_text(response.text)
        assert events[-1].event == "response.completed"
        values = [
            (item["call_id"], item["name"], json.loads(item["arguments"]))
            for item in events[-1].data["response"]["output"]
            if item["type"] == "function_call"
        ]
    assert [(name, arguments) for _, name, arguments in values] == [
        ("read", {"path": "a"}),
        ("read", {"path": "b"}),
    ]
    assert len({tool_id for tool_id, _, _ in values}) == 2
    for (tool_id, _, _), expected_id in zip(values, expected_ids, strict=True):
        if expected_id is None:
            assert tool_id.startswith("tool_")
        else:
            assert tool_id == expected_id


@pytest.mark.parametrize("anonymous", [True, False])
@pytest.mark.parametrize(
    "wire,streaming", [("messages", True), ("messages", False), ("responses", True)]
)
def test_google_compat_unresolvable_calls_cannot_complete(
    monkeypatch, anonymous, wire, streaming
):
    if anonymous:
        events = [
            chat_chunk(
                {
                    "tool_calls": [
                        call(None, None, '{"path":"a"}'),
                        call(None, None, '{"path":"b"}'),
                    ]
                }
            )
        ]
    else:
        # Either call could own the index-only fragment. Both assignments
        # would produce valid JSON, so argument validation cannot decide.
        events = [
            chat_chunk(
                {
                    "tool_calls": [
                        call(0, "call_a", '{"path":"a'),
                        call(None, "call_b", '{"path":"b'),
                    ]
                }
            ),
            chat_chunk({"tool_calls": [call(0, None, "_part", name=None)]}),
            chat_chunk({"tool_calls": [call(0, "call_b", '"}', name=None)]}),
            chat_chunk({"tool_calls": [call(0, "call_a", '"}', name=None)]}),
        ]
    events.append(chat_chunk({}, "tool_calls"))
    response, sent = google_compat_api_response(
        monkeypatch, events, wire, streaming, max_attempts=1
    )
    assert len(sent) == 1
    if streaming:
        # Lifecycle metadata is immediate, while unfinished calls stay private.
        assert response.status_code == 200
        output = parse_sse_text(response.text)
        assert output[-1].event == (
            "error" if wire == "messages" else "response.failed"
        )
        assert "call_a" not in response.text and "call_b" not in response.text
    else:
        assert response.status_code == 502
        assert isinstance(response.json().get("error"), dict)


def completed_arguments(events, wire):
    if wire == "messages":
        return "".join(
            event.data["delta"]["partial_json"]
            for event in events
            if event.data.get("delta", {}).get("type") == "input_json_delta"
        )
    return "".join(
        event.data["item"]["arguments"]
        for event in events
        if event.event == "response.output_item.done"
        and event.data["item"]["type"] == "function_call"
    )


def ambiguous_events(stop, *, incomplete=False, fragment_index=0):
    events = [
        chat_chunk(
            {
                "tool_calls": [
                    call(
                        0,
                        "call_abandoned_a",
                        '{"path":' if incomplete else '{"path":"a"}',
                    ),
                    call(
                        0,
                        "call_abandoned_b",
                        '{"path":' if incomplete else '{"path":"b"}',
                    ),
                ]
            }
        )
    ]
    if stop == "before":
        events.append(chat_chunk({}, "tool_calls"))
    events.append(
        chat_chunk(
            {"tool_calls": [call(fragment_index, None, "uncertain", name=None)]},
            "tool_calls" if stop == "same" else None,
        )
    )
    return events


@pytest.mark.asyncio
@pytest.mark.parametrize("wire", ["messages", "responses"])
@pytest.mark.parametrize(
    "prefix", [None, "content", "reasoning_content", "reasoning_details"]
)
@pytest.mark.parametrize("stop", [None, "same", "before"])
@pytest.mark.parametrize("max_attempts", [1, 2])
@pytest.mark.parametrize("fragment_index", [0, None])
async def test_public_identity_failure_respects_delivery_stop_and_budget(
    monkeypatch, wire, prefix, stop, max_attempts, fragment_index
):
    first = ambiguous_events(stop, fragment_index=fragment_index)
    if prefix:
        first.insert(
            0,
            chat_chunk(
                {
                    "reasoning_details": [
                        {
                            "type": "reasoning.text",
                            "text": "already visible",
                            "index": 0,
                        }
                    ]
                }
                if prefix == "reasoning_details"
                else {prefix: "already visible"}
            ),
        )
    async with _harness(
        "chat",
        lambda bodies: (
            200,
            first if len(bodies) == 1 else tool_events("chat", '{"path":"winning"}'),
        ),
        max_attempts=max_attempts,
    ) as (send, bodies, _):
        result = await delivered(
            send(wire, [{"role": "user", "content": "read"}], tools=_tools(wire)), wire
        )

    # Unsigned readable reasoning follows the same continuation policy as text.
    retry = False
    assert len(bodies) == (2 if retry else 1)
    assert ("call_abandoned" in result) == (stop == "before")
    events = parse_sse_text(result)
    visible_prefix = prefix is not None
    assert ("already visible" in result) == visible_prefix
    if retry:
        assert completed_arguments(events, wire) == '{"path":"winning"}'
        assert events[-1].event == (
            "message_stop" if wire == "messages" else "response.completed"
        )
        assert bodies[1]["tools"] == bodies[0]["tools"]
        if visible_prefix:
            assert "already visible" in str(bodies[1])
        else:
            assert bodies[0] == bodies[1]
    else:
        assert events[-1].event == (
            "error" if wire == "messages" else "response.failed"
        )
        assert "winning" not in result
        assert any(
            event.event
            in {"response.function_call_arguments.done", "response.output_item.done"}
            and (
                event.event != "response.output_item.done"
                or event.data["item"]["type"] == "function_call"
            )
            for event in events
        ) == (stop == "before" and wire == "responses")


@pytest.mark.asyncio
@pytest.mark.parametrize("wire", ["messages", "responses"])
@pytest.mark.parametrize("incomplete", [False, True])
@pytest.mark.parametrize("fragment_index", [0, None])
@pytest.mark.parametrize(
    "committed,stop,max_attempts",
    [
        (False, None, 2),
        (False, None, 1),
        (True, None, 2),
        (False, "same", 2),
        (False, "before", 2),
        (True, "same", 2),
    ],
)
async def test_private_identity_failure_never_salvages_or_repairs(
    monkeypatch, wire, incomplete, committed, stop, max_attempts, fragment_index
):
    first = ambiguous_events(stop, incomplete=incomplete, fragment_index=fragment_index)
    async with _harness("chat", lambda _: (200, first), max_attempts=max_attempts) as (
        send,
        bodies,
        _,
    ):
        with pytest.raises(ExecutionFailure):
            await collect_response(
                send(wire, [{"role": "user", "content": "read"}], tools=_tools(wire)),
                protocol=wire,
            )
        assert len(bodies) == 1


@pytest.mark.asyncio
@pytest.mark.parametrize("wire", ["messages", "responses"])
@pytest.mark.parametrize(
    "indexes,ids",
    [
        ((0, 0), ("call_a", "call_b")),
        ((None, None), ("call_a", "call_b")),
        ((0, 1), ("reused", "reused")),
    ],
)
async def test_google_compat_metadata_replays_under_each_public_id(wire, indexes, ids):
    first = [
        chat_chunk(
            {
                "tool_calls": [
                    call(
                        indexes[0],
                        ids[0],
                        '{"path":"a"}',
                        metadata={"google": {"thought_signature": "sig-a"}},
                    ),
                    call(
                        indexes[1],
                        ids[1],
                        '{"path":"b"}',
                        metadata={"google": {"thought_signature": "sig-b"}},
                    ),
                ]
            }
        ),
        chat_chunk({}, "tool_calls"),
    ]

    def gemini():
        return GoogleOpenAIProvider(
            make_provider_config("fixture", "https://provider.invalid/v1"),
            profile=GOOGLE_PROFILE,
            admission=immediate_admission(max_attempts=2),
        )

    async with _harness(
        "chat",
        lambda bodies: (
            200,
            first if len(bodies) == 1 else [chat_chunk({"content": "done"}, "stop")],
        ),
        chat_provider_factory=gemini,
    ) as (send, bodies, _):
        saved = await _saved_reply(
            send(wire, [{"role": "user", "content": "read both"}], tools=_tools(wire)),
            wire,
        )
        if wire == "messages":
            calls = [item for item in saved[0]["content"] if item["type"] == "tool_use"]
            public_ids = [item["id"] for item in calls]
            followup = [
                {
                    "role": "assistant",
                    "content": [
                        {
                            "type": "tool_use",
                            "id": item["id"],
                            "name": item["name"],
                            "input": item["input"],
                        }
                        for item in calls
                    ],
                },
                {
                    "role": "user",
                    "content": [
                        {"type": "tool_result", "tool_use_id": tool_id, "content": "ok"}
                        for tool_id in public_ids
                    ],
                },
            ]
        else:
            calls = [item for item in saved if item["type"] == "function_call"]
            public_ids = [item["call_id"] for item in calls]
            followup = [
                {
                    "type": "function_call",
                    "call_id": item["call_id"],
                    "name": item["name"],
                    "arguments": item["arguments"],
                }
                for item in calls
            ] + [
                {"type": "function_call_output", "call_id": tool_id, "output": "ok"}
                for tool_id in public_ids
            ]
        assert len(set(public_ids)) == 2
        await _saved_reply(send(wire, followup, tools=_tools(wire)), wire)
        replayed = next(
            row["tool_calls"] for row in bodies[1]["messages"] if row.get("tool_calls")
        )
    assert [(item["id"], item["extra_content"]) for item in replayed] == [
        (public_ids[0], {"google": {"thought_signature": "sig-a"}}),
        (public_ids[1], {"google": {"thought_signature": "sig-b"}}),
    ]