File size: 3,883 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
import pytest

from free_claude_code.core.anthropic.tool_calls import MessagesToolCallBuffer
from free_claude_code.core.failures import ExecutionFailure


def event(kind, **data):
    return kind, {"type": kind, **data}


def test_stopped_tool_waits_for_successful_message_reason():
    buffer = MessagesToolCallBuffer()
    start = event(
        "content_block_start",
        index=0,
        content_block={"type": "tool_use", "id": "a", "name": "read", "input": {}},
    )
    delta = event(
        "content_block_delta",
        index=0,
        delta={"type": "input_json_delta", "partial_json": "opaque"},
    )
    stop = event("content_block_stop", index=0)
    for item in (start, delta, stop):
        assert buffer.feed(*item) == []
    terminal = event("message_delta", delta={"stop_reason": "tool_use"})
    assert buffer.feed(*terminal) == [(start, delta, stop), (terminal,)]


@pytest.mark.parametrize("reason", ["max_tokens", "refusal", "pause_turn"])
def test_truncation_discards_stopped_tool_and_releases_later_text(reason):
    buffer = MessagesToolCallBuffer()
    buffer.feed(
        *event(
            "content_block_start",
            index=0,
            content_block={"type": "tool_use", "id": "a", "name": "read", "input": {}},
        )
    )
    buffer.feed(*event("content_block_stop", index=0))
    text = event(
        "content_block_start", index=1, content_block={"type": "text", "text": "later"}
    )
    assert buffer.feed(*text) == []
    terminal = event("message_delta", delta={"stop_reason": reason})
    assert buffer.feed(*terminal) == [(text,), (terminal,)]


def test_failure_does_not_promote_stopped_tool():
    buffer = MessagesToolCallBuffer()
    buffer.feed(
        *event(
            "content_block_start",
            index=0,
            content_block={"type": "tool_use", "id": "a", "name": "read", "input": {}},
        )
    )
    buffer.feed(*event("content_block_stop", index=0))
    assert buffer.abandon() == []


def test_confirmed_tool_cannot_emit_late_argument_fragments():
    buffer = MessagesToolCallBuffer()
    buffer.feed(
        *event(
            "content_block_start",
            index=0,
            content_block={"type": "tool_use", "id": "a", "name": "read", "input": {}},
        )
    )
    buffer.feed(*event("content_block_stop", index=0))
    buffer.feed(*event("message_delta", delta={"stop_reason": "tool_use"}))
    with pytest.raises(ExecutionFailure):
        buffer.feed(
            *event(
                "content_block_delta",
                index=0,
                delta={"type": "input_json_delta", "partial_json": "late"},
            )
        )


def test_confirmed_messages_tools_cannot_share_an_execution_id():
    buffer = MessagesToolCallBuffer()
    for index in (0, 1):
        buffer.feed(
            *event(
                "content_block_start",
                index=index,
                content_block={
                    "type": "tool_use",
                    "id": "duplicate",
                    "name": "read",
                    "input": {},
                },
            )
        )
        buffer.feed(*event("content_block_stop", index=index))
    with pytest.raises(ExecutionFailure):
        buffer.feed(*event("message_delta", delta={"stop_reason": "tool_use"}))


def test_native_messages_index_bookkeeping_has_a_fixed_record_bound():
    from free_claude_code.core.anthropic.native_stream import MessagesBlockIndexMap
    from free_claude_code.core.tool_call_buffer import (
        MAX_PENDING_RECORDS,
        ToolBufferLimitError,
    )

    indexes = MessagesBlockIndexMap()
    with pytest.raises(ToolBufferLimitError):
        for index in range(MAX_PENDING_RECORDS + 1):
            indexes.project(
                "content_block_start",
                {"index": index, "content_block": {"type": "text", "text": ""}},
            )