refactor(channels): move tool hint rendering out of core (#6498)

* refactor(channels): move tool hint rendering out of core

* test: align progress privacy check with structured tool events
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-29 10:37:29 +08:00
1 parent b1cc89e922
commit acc3750c28
30 files changed
+530 -124

No files matched your search

@@ -2,6 +2,7 @@
import asyncio
from contextlib import suppress
from copy import deepcopy
from unittest.mock import AsyncMock
import pytest
@@ -376,30 +377,101 @@ class TestProgressFiltering:
assert send_mock.await_args_list[0].args[0].content == "final answer"
@pytest.mark.asyncio
async def test_channel_override_can_enable_tool_hints(self, manager, bus):
manager.channels["mock"].send_tool_hints = True
await bus.publish_outbound(outbound_message_for_event(
channel="mock",
chat_id="chat1",
event=ProgressEvent(content="read_file(foo.py)", tool_hint=True),
))
@pytest.mark.parametrize(
"send_progress, send_tool_hints, expected_indices, expected_contents",
[
(False, True, [1, 3], ["read foo.py", "read missing.py", "final answer"]),
(True, False, [0, 2, 4], ["thinking", "", "", "final answer"]),
],
)
async def test_tool_starts_and_terminals_follow_separate_visibility_settings(
self, manager, bus, send_progress, send_tool_hints, expected_indices, expected_contents,
):
channel = manager._build_channel(
MockChannel, {"sendProgress": send_progress, "sendToolHints": send_tool_hints},
)
manager.channels["mock"] = channel
events = [
ProgressEvent(content="thinking"),
ProgressEvent(tool_hint=True, tool_events=[{
"phase": "start", "call_id": "call-1", "name": "read_file",
"arguments": {"path": "foo.py"},
}]),
ProgressEvent(tool_events=[{
"phase": "end", "call_id": "call-1", "name": "read_file", "result": "contents",
}]),
ProgressEvent(tool_hint=True, tool_events=[{
"phase": "start", "call_id": "call-2", "name": "read_file",
"arguments": {"path": "missing.py"},
}]),
ProgressEvent(tool_events=[{
"phase": "error", "call_id": "call-2", "name": "read_file", "error": "not found",
}]),
]
messages = [
outbound_message_for_event(channel="mock", chat_id="chat1", event=event)
for event in events
]
final = OutboundMessage(channel="mock", chat_id="chat1", content="final answer")
for msg in [*messages, final]:
await bus.publish_outbound(msg)
delivered = asyncio.Event()
def record_send(msg):
if msg is final:
delivered.set()
channel._send_mock.side_effect = record_send
task = asyncio.create_task(manager._dispatch_outbound())
try:
for _ in range(30):
if manager.channels["mock"]._send_mock.await_count >= 1:
break
await asyncio.sleep(0.05)
await asyncio.wait_for(delivered.wait(), timeout=2)
finally:
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
with suppress(asyncio.CancelledError):
await asyncio.wait_for(task, timeout=2)
send_mock = manager.channels["mock"]._send_mock
assert send_mock.await_count == 1
assert send_mock.await_args_list[0].args[0].content == "read_file(foo.py)"
sent = [call.args[0] for call in channel._send_mock.await_args_list]
assert [msg.content for msg in sent] == expected_contents
assert [msg.event for msg in sent] == [events[i] for i in expected_indices] + [None]
if send_progress:
assert sent[1] is messages[2]
assert sent[2] is messages[4]
assert messages[1].content == ""
assert events[1].content == ""
class TestToolEventDelivery:
@pytest.mark.asyncio
async def test_prepared_tool_hint_propagates_failure_without_mutating_core_message(self, manager):
manager.config.agents.defaults.tool_hint_max_length = 20
channel = manager._build_channel(MockChannel, {})
msg = outbound_message_for_event(
channel="mock",
chat_id="chat1",
event=ProgressEvent(tool_hint=True, stream_id="seg-1", tool_events=[{
"phase": "start", "call_id": "call-1", "name": "web_search",
"arguments": {"query": "abcdefghijklmnopqrstuvwxyz"},
}]),
metadata={"message_thread_id": 42},
)
original = deepcopy(msg)
error = RuntimeError("send failed")
channel._send_mock.side_effect = error
prepared = channel.prepare_tool_events(msg)
with pytest.raises(RuntimeError, match="send failed") as raised:
await manager._send_once(channel, prepared)
assert raised.value is error
channel._send_mock.assert_awaited_once()
sent = channel._send_mock.await_args.args[0]
assert sent is prepared
assert sent is not msg
assert sent.content == 'search "abcdefghijklmnopqrs…"'
assert sent.event is msg.event
assert sent.metadata == original.metadata
assert msg == original
class TestRetryWaitFiltering: