fix(agent): log mid-turn injected messages (#5878)

* fix(agent): log messages consumed by mid-turn injection

* fix(agent): log injected batches after appending to transcript

* fix(agent): narrow injection metadata dictionary types

* refactor(agent): keep injection logging preview inline
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-23 14:58:26 +08:00
1 parent 3fe109bf01
commit e68afef670
2 files changed
+34 -3

No files matched your search

+11 -2
View File
@@ -41,6 +41,7 @@ from nanobot.providers.base import (
ProviderConversationState, ProviderConversationState,
) )
from nanobot.providers.conversation_state import ProviderConversationStateController from nanobot.providers.conversation_state import ProviderConversationStateController
from nanobot.session.history_visibility import is_hidden_history_message
from nanobot.session.summary import SessionSummaryCheckpoint from nanobot.session.summary import SessionSummaryCheckpoint
from nanobot.utils.helpers import ( from nanobot.utils.helpers import (
build_assistant_message, build_assistant_message,
@@ -204,9 +205,17 @@ class AgentRunner:
) )
self._append_injected_messages(messages, injections) self._append_injected_messages(messages, injections)
if real_injection: if real_injection:
preview = "[content hidden]"
if tool_log_content_allowed():
preview = "\n\n".join(
message["content"] for message in injections
if isinstance(message.get("content"), str)
and not is_hidden_history_message(message)
)
preview = preview[:80] + "..." if len(preview) > 80 else preview
logger.info( logger.info(
"Injected {} follow-up message(s) {} (snapshot {})", "Injected {} follow-up message(s) {} (snapshot {}): {}",
len(injections), phase, injection_cycles, len(injections), phase, injection_cycles, preview,
) )
else: else:
logger.info("Injected caller-requested continuation {}", phase) logger.info("Injected caller-requested continuation {}", phase)
+23 -1
View File
@@ -1514,11 +1514,21 @@ async def test_session_inbox_is_installed_before_worker_start(tmp_path):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_busy_session_burst_reaches_next_model_call_as_one_ordered_batch(tmp_path): @pytest.mark.parametrize("log_content", [True, False])
async def test_busy_session_burst_reaches_next_model_call_as_one_ordered_batch(
tmp_path, request, log_content,
):
"""Messages accumulated during a model call share its next request snapshot.""" """Messages accumulated during a model call share its next request snapshot."""
from loguru import logger
from nanobot.agent.loop import AgentLoop from nanobot.agent.loop import AgentLoop
from nanobot.bus.events import InboundMessage from nanobot.bus.events import InboundMessage
from nanobot.bus.queue import MessageBus from nanobot.bus.queue import MessageBus
from nanobot.session.manager import SessionPolicy
records = []
sink = logger.add(lambda message: records.append(message.record))
request.addfinalizer(lambda: logger.remove(sink))
provider = MagicMock() provider = MagicMock()
provider.get_default_model.return_value = "test-model" provider.get_default_model.return_value = "test-model"
@@ -1544,6 +1554,7 @@ async def test_busy_session_burst_reaches_next_model_call_as_one_ordered_batch(t
model="test-model", model="test-model",
) )
loop.tools.get_definitions = MagicMock(return_value=[]) loop.tools.get_definitions = MagicMock(return_value=[])
loop.sessions.get_or_create("cli:c").policy = SessionPolicy(log_content=log_content)
followups = [f"follow-up-{index:02}" for index in range(12)] followups = [f"follow-up-{index:02}" for index in range(12)]
run_task = asyncio.create_task(loop.run()) run_task = asyncio.create_task(loop.run())
@@ -1585,6 +1596,17 @@ async def test_busy_session_burst_reaches_next_model_call_as_one_ordered_batch(t
assert positions == sorted(positions) assert positions == sorted(positions)
assert loop.bus.inbound_size == 0 assert loop.bus.inbound_size == 0
injections = [r for r in records if r["message"].startswith("Injected ")]
assert len(injections) == 1
record = injections[0]
preview = "\n\n".join(followups)[:80] + "..." if log_content else "[content hidden]"
assert record["message"] == (
f"Injected {len(followups)} follow-up message(s) after final response (snapshot 1): {preview}"
)
assert record["level"].name == "INFO"
assert record["extra"]["session_key"] == "cli:c"
assert record["extra"]["turn_id"]
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_session_worker_processes_leftovers_without_republishing(tmp_path): async def test_session_worker_processes_leftovers_without_republishing(tmp_path):