refactor: remove redundant comments and docstrings (#6527)

* refactor: remove redundant comments and docstrings

* fix(signal): annotate async context manager as a generator
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-30 13:07:03 +08:00
1 parent cb3ba45317
commit a4d2430140
922 files changed
+406 -7738

No files matched your search

-46
View File
@@ -1,5 +1,3 @@
"""Tests for the mid-turn injection system: drain, checkpoints, pending queues, error paths."""
from __future__ import annotations
import asyncio
@@ -50,7 +48,6 @@ def _make_loop(tmp_path, *, recovery_admission=None):
@pytest.mark.asyncio
async def test_drain_injections_returns_empty_when_no_callback():
"""No injection_callback → empty list."""
from nanobot.agent.runner import AgentRunner
provider = MagicMock()
@@ -68,7 +65,6 @@ async def test_drain_injections_returns_empty_when_no_callback():
@pytest.mark.asyncio
async def test_drain_injections_extracts_content_from_inbound_messages():
"""Should extract .content from InboundMessage objects."""
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -99,7 +95,6 @@ async def test_drain_injections_extracts_content_from_inbound_messages():
@pytest.mark.asyncio
async def test_drain_injections_keeps_entire_callback_snapshot():
"""A callback snapshot is never split by an arbitrary message count."""
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -129,7 +124,6 @@ async def test_drain_injections_keeps_entire_callback_snapshot():
@pytest.mark.asyncio
async def test_drain_injections_skips_empty_content():
"""Messages with blank content should be filtered out."""
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -158,7 +152,6 @@ async def test_drain_injections_skips_empty_content():
@pytest.mark.asyncio
async def test_drain_injections_filters_empty_dict_payloads():
"""Pre-normalized dict injections should obey the same empty-content guard."""
from nanobot.agent.runner import AgentRunner
provider = MagicMock()
@@ -194,7 +187,6 @@ async def test_drain_injections_filters_empty_dict_payloads():
@pytest.mark.asyncio
async def test_drain_injections_skips_objects_with_none_content():
"""Objects exposing content=None should be skipped rather than stringified."""
from types import SimpleNamespace
from nanobot.agent.runner import AgentRunner
@@ -222,7 +214,6 @@ async def test_drain_injections_skips_objects_with_none_content():
@pytest.mark.asyncio
async def test_drain_injections_handles_callback_exception():
"""If the callback raises, return empty list (error is logged)."""
from nanobot.agent.runner import AgentRunner
provider = MagicMock()
@@ -244,7 +235,6 @@ async def test_drain_injections_handles_callback_exception():
@pytest.mark.asyncio
async def test_checkpoint1_injects_after_tool_execution():
"""Follow-up messages are injected after tool execution, before next LLM call."""
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -291,7 +281,6 @@ async def test_checkpoint1_injects_after_tool_execution():
assert result.had_injections is True
assert result.final_content == "final answer"
# The second call should have the injected user message
assert call_count["n"] == 2
last_messages = captured_messages[-1]
injected = [m for m in last_messages if m.get("role") == "user" and m.get("content") == "follow-up question"]
@@ -300,7 +289,6 @@ async def test_checkpoint1_injects_after_tool_execution():
@pytest.mark.asyncio
async def test_terminal_wait_does_not_block_next_iteration_after_tools():
"""Background waits begin only after a no-tool response is ready to finish."""
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -379,7 +367,6 @@ async def test_terminal_wait_does_not_block_next_iteration_after_tools():
@pytest.mark.asyncio
async def test_goal_continuation_precedes_terminal_wait():
"""An active sustained goal keeps running without joining background work."""
from nanobot.agent.runner import AgentRunner
provider = MagicMock()
@@ -424,7 +411,6 @@ async def test_goal_continuation_precedes_terminal_wait():
@pytest.mark.asyncio
async def test_checkpoint2_injects_after_final_response_with_resuming_stream():
"""After final response, if injections exist, stream_end should get resuming=True."""
from nanobot.agent.hook import AgentHook, AgentHookContext
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -475,15 +461,12 @@ async def test_checkpoint2_injects_after_final_response_with_resuming_stream():
assert result.had_injections is True
assert result.final_content == "second answer"
assert call_count["n"] == 2
# First stream_end should have resuming=True (because injections found)
assert stream_end_calls[0] is True
# Second (final) stream_end should have resuming=False
assert stream_end_calls[-1] is False
@pytest.mark.asyncio
async def test_injected_followup_starts_new_length_recovery_chain():
"""A follow-up gets a fresh recovery budget and no content from the prior answer."""
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -588,7 +571,6 @@ async def test_truncated_answer_followup_requires_remaining_iteration(max_iterat
@pytest.mark.asyncio
async def test_checkpoint2_preserves_final_response_in_history_before_followup():
"""A follow-up injected after a final answer must still see that answer in history."""
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -644,7 +626,6 @@ async def test_checkpoint2_preserves_final_response_in_history_before_followup()
@pytest.mark.asyncio
async def test_loop_injected_followup_preserves_image_media(tmp_path):
"""Mid-turn follow-ups with images should keep multimodal content."""
from nanobot.agent.loop import AgentLoop
from nanobot.bus.events import InboundMessage
from nanobot.bus.queue import MessageBus
@@ -901,7 +882,6 @@ async def test_subagent_pending_injection_is_hidden_history_and_not_merged(tmp_p
@pytest.mark.asyncio
async def test_model_request_merges_injected_user_messages_without_losing_media():
"""The model copy may merge follow-ups while the raw transcript keeps each event."""
from nanobot.agent.runner import AgentRunner
provider = MagicMock()
@@ -970,7 +950,6 @@ async def test_model_request_merges_injected_user_messages_without_losing_media(
def test_runner_append_keeps_recovery_followups_separate() -> None:
"""Each raw follow-up keeps its own recovery identity."""
from nanobot.agent.runner import AgentRunner
from nanobot.session.recovery import PENDING_FOLLOWUP_ID_KEY
@@ -1040,7 +1019,6 @@ def test_model_request_merge_preserves_runtime_markers_with_media() -> None:
@pytest.mark.asyncio
async def test_injection_cycles_are_not_stopped_by_an_arbitrary_cap():
"""Every pending snapshot runs until the normal iteration budget is reached."""
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -1081,7 +1059,6 @@ async def test_injection_cycles_are_not_stopped_by_an_arbitrary_cap():
@pytest.mark.asyncio
async def test_no_injections_flag_is_false_by_default():
"""had_injections should be False when no injection callback or no messages."""
from nanobot.agent.runner import AgentRunner
provider = MagicMock()
@@ -1107,7 +1084,6 @@ async def test_no_injections_flag_is_false_by_default():
@pytest.mark.asyncio
async def test_followup_routed_to_pending_queue(tmp_path):
"""Unified-session follow-ups should route into the active pending queue."""
from nanobot.bus.events import InboundMessage
from nanobot.session.keys import UNIFIED_SESSION_KEY
@@ -1133,7 +1109,6 @@ async def test_followup_routed_to_pending_queue(tmp_path):
@pytest.mark.asyncio
async def test_websocket_followup_is_admitted_before_recovery_queue(tmp_path):
"""Recovery admission runs before a newer WebUI message is injected."""
from nanobot.bus.events import InboundMessage
admission = MagicMock()
@@ -1164,7 +1139,6 @@ async def test_websocket_followup_is_admitted_before_recovery_queue(tmp_path):
@pytest.mark.asyncio
async def test_unified_websocket_followup_admits_effective_session(tmp_path):
"""Recovery admission and the pending queue must use the same session key."""
from nanobot.bus.events import InboundMessage
from nanobot.session.keys import UNIFIED_SESSION_KEY
@@ -1199,7 +1173,6 @@ async def test_unified_websocket_followup_admits_effective_session(tmp_path):
@pytest.mark.asyncio
async def test_mid_turn_subagent_result_does_not_resolve_a_new_turn_route(tmp_path):
"""Injected results stay inside the active turn instead of opening a side turn."""
from nanobot.bus.events import InboundMessage
loop = _make_loop(tmp_path)
@@ -1236,7 +1209,6 @@ async def test_mid_turn_subagent_result_does_not_resolve_a_new_turn_route(tmp_pa
@pytest.mark.asyncio
async def test_pending_queue_batches_full_snapshot_before_first_model_call(tmp_path):
"""All messages waiting at the checkpoint enter one model request in order."""
from nanobot.agent.loop import AgentLoop
from nanobot.bus.events import InboundMessage
from nanobot.bus.queue import MessageBus
@@ -1297,7 +1269,6 @@ async def test_pending_queue_batches_full_snapshot_before_first_model_call(tmp_p
@pytest.mark.asyncio
async def test_pending_queue_snapshot_excludes_messages_arriving_during_conversion(tmp_path):
"""A checkpoint is a finite snapshot even when message conversion awaits."""
from nanobot.agent.loop import AgentLoop
from nanobot.bus.events import InboundMessage
from nanobot.bus.queue import MessageBus
@@ -1475,7 +1446,6 @@ async def test_persistent_conversion_error_does_not_drop_later_session_inputs(tm
@pytest.mark.asyncio
async def test_session_inbox_is_installed_before_worker_start(tmp_path, monkeypatch):
"""A preloaded burst has one FIFO path before the worker can be scheduled."""
from nanobot.bus.events import InboundMessage, OutboundMessage
loop = _make_loop(tmp_path)
@@ -1534,7 +1504,6 @@ async def test_session_inbox_is_installed_before_worker_start(tmp_path, monkeypa
async def test_busy_session_burst_reaches_next_model_call_as_one_ordered_batch(
tmp_path, request, monkeypatch, log_content,
):
"""Messages accumulated during a model call share its next request snapshot."""
from loguru import logger
from nanobot.agent.loop import AgentLoop
@@ -1608,7 +1577,6 @@ async def test_busy_session_burst_reaches_next_model_call_as_one_ordered_batch(
finally:
release_first_request.set()
loop.stop()
# Shutdown drains accepted writes and closes the worker's WAL connection.
await asyncio.wait_for(run_task, timeout=30)
assert len(captured_messages) == 2
@@ -1636,7 +1604,6 @@ async def test_busy_session_burst_reaches_next_model_call_as_one_ordered_batch(
@pytest.mark.asyncio
async def test_session_worker_processes_leftovers_without_republishing(tmp_path):
"""Messages left by one turn stay on the same FIFO worker."""
from nanobot.bus.events import InboundMessage, OutboundMessage
loop = _make_loop(tmp_path)
@@ -1667,7 +1634,6 @@ async def test_session_worker_processes_leftovers_without_republishing(tmp_path)
@pytest.mark.asyncio
async def test_drain_injections_after_recoverable_tool_error():
"""A tool error and injected follow-up continue in the same runner conversation."""
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -1688,7 +1654,6 @@ async def test_drain_injections_after_recoverable_tool_error():
tool_calls=[ToolCallRequest(id="c1", name="exec", arguments={"cmd": "bad"})],
usage=None,
)
# Third call: respond normally to the injected follow-up.
return LLMResponse(content="reply to follow-up", tool_calls=[], usage=None)
provider.chat_stream_with_retry = chat_stream_with_retry
@@ -1716,7 +1681,6 @@ async def test_drain_injections_after_recoverable_tool_error():
assert result.had_injections is True
assert result.final_content == "reply to follow-up"
assert call_count["n"] == 3
# The injection should be in the messages history
injected = [
m for m in result.messages
if m.get("role") == "user" and m.get("content") == "follow-up after error"
@@ -1726,7 +1690,6 @@ async def test_drain_injections_after_recoverable_tool_error():
@pytest.mark.asyncio
async def test_drain_injections_on_llm_error():
"""A follow-up after an error stays raw and reaches the next model request."""
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -1752,7 +1715,6 @@ async def test_drain_injections_on_llm_error():
finish_reason="error",
usage=None,
)
# Second call: respond normally to the injected follow-up
return LLMResponse(content="recovered answer", tool_calls=[], usage=None)
provider.chat_stream_with_retry = chat_stream_with_retry
@@ -1800,7 +1762,6 @@ async def test_drain_injections_on_llm_error():
@pytest.mark.asyncio
async def test_drain_injections_on_empty_final_response():
"""Pending injections should be drained when the runner exits due to empty response."""
from nanobot.agent.runner import _MAX_EMPTY_RETRIES, AgentRunner
from nanobot.bus.events import InboundMessage
@@ -1818,7 +1779,6 @@ async def test_drain_injections_on_empty_final_response():
content="follow-up after empty",
))
return LLMResponse(content="", tool_calls=[], usage=None)
# After retries exhausted + injection drain, respond normally
return LLMResponse(content="answer after empty", tool_calls=[], usage=None)
provider.chat_stream_with_retry = chat_stream_with_retry
@@ -1854,7 +1814,6 @@ async def test_drain_injections_on_empty_final_response():
@pytest.mark.asyncio
@pytest.mark.parametrize("failed_stop_reason", ["error", "empty_final_response"])
async def test_late_injection_recovery_completes_and_delivers_reply(failed_stop_reason):
"""Input arriving after failure bookkeeping must not poison a recovered turn."""
from nanobot.agent.hook import AgentHook
from nanobot.agent.runner import _MAX_EMPTY_RETRIES, AgentRunner
from nanobot.agent.tools.registry import ToolRegistry
@@ -1881,8 +1840,6 @@ async def test_late_injection_recovery_completes_and_delivers_reply(failed_stop_
async def after_iteration(self, context) -> None:
if context.stop_reason == failed_stop_reason and context.error is not None:
observed_failures.append(context.error)
# The earlier final-response drain has already run. Inject at
# the late drain, after the runner has recorded the failure.
await injection_queue.put(InboundMessage(
channel="websocket",
sender_id="user",
@@ -1933,7 +1890,6 @@ async def test_late_injection_recovery_completes_and_delivers_reply(failed_stop_
@pytest.mark.asyncio
async def test_max_iterations_without_finalization_keeps_late_injection_queued():
"""Never consume a user message when no later model request can observe it."""
from nanobot.agent.hook import AgentHook
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -1987,7 +1943,6 @@ async def test_max_iterations_without_finalization_keeps_late_injection_queued()
@pytest.mark.asyncio
async def test_max_iterations_finalization_keeps_late_injection_queued():
"""New input waits for a turn with tools instead of entering no-tools finalization."""
from nanobot.agent.hook import AgentHook
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage
@@ -2057,7 +2012,6 @@ async def test_max_iterations_finalization_keeps_late_injection_queued():
@pytest.mark.asyncio
async def test_error_path_is_not_stopped_by_an_arbitrary_injection_cap():
"""Error recovery consumes every arrived snapshot until no follow-up remains."""
from nanobot.agent.runner import AgentRunner
from nanobot.bus.events import InboundMessage