claude-proxy / tests /core /test_messages_tool_buffer.py
dheraingoud's picture
Copilot App
Sync upstream v6.19.35 and preserve custom addons
a9e27c0
Raw History Blame Contribute Delete
3.88 kB
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": ""}},
)