fix(webui): update usage and tool progress during active turns (#6553)
* fix(webui): stream model status and usage during active turns * fix(webui): keep live rounds and active-run status consistent * fix(webui): reuse existing run indicators without extra status text * fix(webui): preserve live activity and batch durable delivery Cancel and drain sibling tools when progress notifications fail, retain balanced checkpoints, and keep model telemetry separate from answer provenance. Group-commit already queued activity at the channel delivery boundary while preserving FIFO, generation ownership, per-envelope admission, and commit-before-fanout semantics. Isolate invalid members without dropping valid neighboring events. Retain tool detail lazy grouping and cover scheduling, cancellation, persistence, encoding failures, and projection regressions. * test(webui): separate standard-library imports in batch regression * test: tighten live execution checks and clarify contracts * refactor: remove redundant live execution comments * refactor: settle tool results through one output path
This commit is contained in:
1 parent
006a535537
commit
8ff04aad2b
52 files changed
+2065
-211
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
@@ -175,12 +176,172 @@ async def test_batched_file_reads_keep_each_call_and_complete_arguments() -> Non
|
||||
))
|
||||
|
||||
assert result.final_content == "Done"
|
||||
assert [(content, hint) for content, hint, _events in progress] == [("", True), ("", False)]
|
||||
for _content, _hint, events in progress:
|
||||
assert len(progress) == 4
|
||||
for phase, hint in (("start", True), ("end", False)):
|
||||
events = [item for content, is_hint, items in progress if is_hint == hint for item in items]
|
||||
assert [(item["call_id"], item["name"], item["arguments"]) for item in events] == [
|
||||
(call.id, call.name, call.arguments) for call in calls
|
||||
]
|
||||
assert [item["phase"] for item in progress[0][2]] == ["start", "start"]
|
||||
assert [(item["phase"], item["result"]) for item in progress[1][2]] == [
|
||||
("end", "file contents"), ("end", "file contents"),
|
||||
]
|
||||
assert [item["phase"] for item in events] == [phase, phase]
|
||||
if phase == "end":
|
||||
assert [item["result"] for item in events] == ["file contents", "file contents"]
|
||||
|
||||
|
||||
def _notification_run_spec(execute, on_progress, calls, checkpoints):
|
||||
provider = MagicMock()
|
||||
provider.chat_stream_with_retry = AsyncMock(side_effect=[
|
||||
LLMResponse(content=None, tool_calls=calls),
|
||||
LLMResponse(content="Done"),
|
||||
])
|
||||
tools = MagicMock()
|
||||
tools.get_definitions.return_value = []
|
||||
tools.prepare_call.return_value = (None, {}, None)
|
||||
tools.execute = execute
|
||||
|
||||
async def checkpoint(payload):
|
||||
checkpoints.append(payload)
|
||||
|
||||
return make_run_spec(
|
||||
provider, initial_messages=[], tools=tools, model="test-model",
|
||||
max_iterations=2, max_tool_result_chars=_DEFAULTS.max_tool_result_chars,
|
||||
concurrent_tools=True, hook=AgentProgressHook(output_events(on_progress=on_progress)),
|
||||
checkpoint_callback=checkpoint,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("failure_phase", ["start", "end"])
|
||||
async def test_tool_notification_failure_settles_results_and_cancels_siblings(failure_phase):
|
||||
entered = asyncio.Event()
|
||||
release = asyncio.Event()
|
||||
stopped = asyncio.Event()
|
||||
effects = []
|
||||
checkpoints = []
|
||||
error = BrokenPipeError("progress consumer closed")
|
||||
|
||||
async def execute(name, arguments):
|
||||
if name == "slow":
|
||||
entered.set()
|
||||
try:
|
||||
await release.wait()
|
||||
effects.append("slow")
|
||||
return "slow result"
|
||||
finally:
|
||||
stopped.set()
|
||||
effects.append("fast")
|
||||
return "fast result"
|
||||
|
||||
async def progress(content, *, tool_events=None, **kwargs):
|
||||
if any(event["call_id"] == "fast" and event["phase"] == failure_phase
|
||||
for event in tool_events or []):
|
||||
await entered.wait()
|
||||
raise error
|
||||
|
||||
task = asyncio.create_task(AgentRunner().run(_notification_run_spec(
|
||||
execute, progress,
|
||||
[ToolCallRequest("slow", "slow", {}), ToolCallRequest("fast", "fast", {})],
|
||||
checkpoints,
|
||||
)))
|
||||
try:
|
||||
with pytest.raises(BrokenPipeError) as raised:
|
||||
await asyncio.wait_for(task, 2)
|
||||
assert raised.value is error
|
||||
assert stopped.is_set()
|
||||
assert effects == (["fast"] if failure_phase == "end" else [])
|
||||
if failure_phase == "end":
|
||||
checkpoint = checkpoints[-1]
|
||||
assert checkpoint["phase"] == "tools_completed"
|
||||
slow, fast = checkpoint["completed_tool_results"]
|
||||
assert (fast["tool_call_id"], fast["content"]) == ("fast", "fast result")
|
||||
assert slow["tool_call_id"] == "slow"
|
||||
assert slow["_recovery_interrupted"] is True
|
||||
assert "outcome is unknown" in slow["content"]
|
||||
assert checkpoint["pending_tool_calls"] == []
|
||||
from nanobot.session.recovery import runtime_checkpoint_is_well_formed
|
||||
|
||||
assert runtime_checkpoint_is_well_formed(checkpoint)
|
||||
else:
|
||||
assert [checkpoint["phase"] for checkpoint in checkpoints] == ["awaiting_tools"]
|
||||
finally:
|
||||
release.set()
|
||||
await asyncio.gather(task, return_exceptions=True)
|
||||
await asyncio.wait_for(stopped.wait(), 2)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_tool_completion_notification_error_preserves_completed_checkpoint():
|
||||
checkpoints = []
|
||||
error = BrokenPipeError("progress consumer closed")
|
||||
|
||||
async def progress(content, *, tool_events=None, **kwargs):
|
||||
if any(event["phase"] == "end" for event in tool_events or []):
|
||||
raise error
|
||||
|
||||
with pytest.raises(BrokenPipeError) as raised:
|
||||
await AgentRunner().run(_notification_run_spec(
|
||||
AsyncMock(return_value="completed effect"), progress,
|
||||
[ToolCallRequest("fast", "fast", {})], checkpoints,
|
||||
))
|
||||
assert raised.value is error
|
||||
assert [checkpoint["phase"] for checkpoint in checkpoints] == ["awaiting_tools", "tools_completed"]
|
||||
assert checkpoints[-1]["completed_tool_results"][0]["content"] == "completed effect"
|
||||
assert checkpoints[-1]["pending_tool_calls"] == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("cancel_during", ["start", "execution", "end"])
|
||||
async def test_tool_cancellation_does_not_wait_for_blocked_progress(cancel_during):
|
||||
queue = asyncio.Queue(maxsize=1)
|
||||
if cancel_during == "start":
|
||||
queue.put_nowait([])
|
||||
entered = asyncio.Event()
|
||||
release = asyncio.Event()
|
||||
stopped = asyncio.Event()
|
||||
checkpoints = []
|
||||
effects = []
|
||||
notifications = set()
|
||||
|
||||
async def execute(name, arguments):
|
||||
if cancel_during == "execution":
|
||||
entered.set()
|
||||
try:
|
||||
await release.wait()
|
||||
finally:
|
||||
stopped.set()
|
||||
effects.append(name)
|
||||
return "completed effect"
|
||||
|
||||
async def progress(content, *, tool_events=None, **kwargs):
|
||||
if tool_events:
|
||||
notification = asyncio.current_task()
|
||||
notifications.add(notification)
|
||||
try:
|
||||
if any(event["phase"] == cancel_during for event in tool_events):
|
||||
entered.set()
|
||||
await queue.put(tool_events)
|
||||
finally:
|
||||
notifications.remove(notification)
|
||||
|
||||
task = asyncio.create_task(AgentRunner().run(_notification_run_spec(
|
||||
execute, progress, [ToolCallRequest("tool", "tool", {})], checkpoints,
|
||||
)))
|
||||
try:
|
||||
await asyncio.wait_for(entered.wait(), 2)
|
||||
task.cancel()
|
||||
done, _ = await asyncio.wait({task}, timeout=1)
|
||||
assert done, "cancellation must settle without a second cancel or queue consumer"
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await task
|
||||
assert not notifications
|
||||
assert effects == (["tool"] if cancel_during == "end" else [])
|
||||
if cancel_during == "end":
|
||||
assert checkpoints[-1]["phase"] == "tools_completed"
|
||||
assert checkpoints[-1]["completed_tool_results"][0]["content"] == "completed effect"
|
||||
elif cancel_during == "execution":
|
||||
assert stopped.is_set()
|
||||
finally:
|
||||
# Keep the failure reproduction bounded even with the old shielded send.
|
||||
if not queue.empty():
|
||||
queue.get_nowait()
|
||||
release.set()
|
||||
await asyncio.gather(task, return_exceptions=True)
|
||||
Reference in new issue
Block a user