perf(agent): skip transcript snapshots without run hooks

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-15 10:48:59 +08:00
1 parent 2ad0da786d
commit c58bfa71bc
5 files changed
+151 -8

No files matched your search

+15
View File
@@ -73,6 +73,13 @@ class AgentHook:
def wants_streaming(self) -> bool:
return False
def has_run_hooks(self) -> bool:
"""Whether run-level callbacks consume a detached context snapshot."""
return any(
getattr(getattr(self, name), "__func__", None) is not getattr(AgentHook, name)
for name in ("before_run", "after_run", "on_error", "on_finally")
)
async def before_run(self, context: AgentRunHookContext) -> None:
pass
@@ -172,6 +179,14 @@ class CompositeHook(AgentHook):
def wants_streaming(self) -> bool:
return any(h.wants_streaming() for h in self._hooks)
def has_run_hooks(self) -> bool:
if any(
getattr(getattr(self, name), "__func__", None) is not getattr(CompositeHook, name)
for name in ("before_run", "after_run", "on_error", "on_finally")
):
return True
return any(h.has_run_hooks() for h in self._hooks)
async def _for_each_hook_safe(self, method_name: str, *args: Any, **kwargs: Any) -> None:
for h in self._hooks:
if getattr(h, "_reraise", False):
+6 -5
View File
@@ -291,8 +291,9 @@ class AgentRunner:
async def run(self, spec: AgentRunSpec) -> AgentRunResult:
hook = spec.hook or AgentHook()
has_run_hooks = hook.has_run_hooks()
messages, compaction = self._initial_transcript_and_compaction(spec)
context = AgentRunHookContext(messages=deepcopy(messages))
context = AgentRunHookContext(messages=deepcopy(messages) if has_run_hooks else [])
llm_usage_source_token = bind_llm_usage_source(
spec.llm_usage_source or source_from_session_key(spec.session_key)
)
@@ -301,20 +302,20 @@ class AgentRunner:
await hook.before_run(context)
result = await self._run_core(spec, hook, messages, compaction)
except asyncio.CancelledError as exc:
context.messages = deepcopy(messages)
context.messages = deepcopy(messages) if has_run_hooks else []
context.stop_reason = "cancelled"
context.error = None
context.exception = exc
raise
except Exception as exc:
context.messages = deepcopy(messages)
context.messages = deepcopy(messages) if has_run_hooks else []
context.stop_reason = "error"
context.error = f"Error: {type(exc).__name__}: {exc}"
context.exception = exc
await hook.on_error(context)
raise
else:
context.messages = deepcopy(result.messages)
context.messages = deepcopy(result.messages) if has_run_hooks else []
context.final_content = result.final_content
context.tools_used = list(result.tools_used)
context.usage = result.usage
@@ -329,7 +330,7 @@ class AgentRunner:
return result
finally:
try:
context.messages = deepcopy(messages)
context.messages = deepcopy(messages) if has_run_hooks else []
if context.exception is None:
await hook.on_finally(context)
else:
+44
View File
@@ -452,6 +452,50 @@ class TestEphemeralDirect:
assert response.content == "done"
loop.provider.chat_stream_with_retry.assert_awaited()
async def test_dream_progress_only_turn_skips_run_transcript_snapshots(self, tmp_path):
from copy import deepcopy
from unittest.mock import AsyncMock, MagicMock, patch
from nanobot.agent.loop import AgentLoop
from nanobot.bus.queue import MessageBus
from nanobot.providers.base import GenerationSettings
store = MemoryStore(tmp_path)
store.write_memory("# Memory")
store.append_history("Dream input")
dream_prompt = store.build_dream_prompt()
assert dream_prompt is not None
prompt, _ = dream_prompt
provider = MagicMock()
provider.get_default_model.return_value = "test-model"
provider.supports_tools = True
provider.generation = GenerationSettings()
provider.estimate_prompt_tokens.return_value = (100, "test")
provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="done"))
loop = AgentLoop(
bus=MessageBus(), provider=provider, workspace=tmp_path,
context_window_tokens=32_000,
)
async def _silent(*_args, **_kwargs):
pass
with patch("nanobot.agent.runner.deepcopy", wraps=deepcopy) as copy_state:
response = await loop.process_direct(
prompt, session_key=MemoryStore.dream_session_key(), ephemeral=True,
tools=store.build_dream_tools(), on_progress=_silent,
)
assert response is not None
assert response.content == "done"
assert MemoryStore.dream_run_completed(response) is True
provider.chat_stream_with_retry.assert_awaited_once()
transcript_copies = [
call.args[0] for call in copy_state.call_args_list
if call.args[0] and "role" in call.args[0][0]
]
assert len(transcript_copies) == 0
async def test_ephemeral_sets_ctx_flag(self, tmp_path, _make_loop):
"""Verify that ephemeral=True is forwarded to TurnContext."""
from unittest.mock import patch
+18
View File
@@ -13,8 +13,12 @@ from nanobot.agent.hook import (
AgentRunHookContext,
AgentTurnHookContext,
CompositeHook,
SDKCaptureHook,
)
from nanobot.agent.hooks.file_edit_activity import FileEditActivityHook
from nanobot.agent.progress_hook import AgentProgressHook
from nanobot.agent.tools.context import RequestContext
from nanobot.events import NO_EVENTS
from nanobot.utils.progress_events import output_events
@@ -351,6 +355,19 @@ def test_composite_wants_streaming_empty():
assert hook.wants_streaming() is False
def test_builtin_hooks_detect_run_snapshot_consumers():
assert AgentHook().has_run_hooks() is False
assert AgentProgressHook().has_run_hooks() is False
assert SDKCaptureHook().has_run_hooks() is True
assert FileEditActivityHook(events=NO_EVENTS, workspace=None).has_run_hooks() is True
def test_composite_detects_nested_run_snapshot_consumers():
progress = AgentProgressHook()
assert CompositeHook([AgentHook(), CompositeHook([progress])]).has_run_hooks() is False
assert CompositeHook([progress, CompositeHook([SDKCaptureHook()])]).has_run_hooks() is True
# ---------------------------------------------------------------------------
# Empty hooks list: behaves like no-op AgentHook
# ---------------------------------------------------------------------------
@@ -359,6 +376,7 @@ def test_composite_wants_streaming_empty():
@pytest.mark.asyncio
async def test_composite_empty_hooks_no_ops():
hook = CompositeHook([])
assert hook.has_run_hooks() is False
ctx = _ctx()
run_ctx = _run_ctx()
await hook.before_run(run_ctx)
+68 -3
View File
@@ -3,6 +3,7 @@ cached-token propagation, and hook context."""
from __future__ import annotations
from copy import deepcopy
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -387,8 +388,9 @@ async def test_runner_calls_run_level_hooks_on_success():
@pytest.mark.asyncio
async def test_runner_run_level_context_is_detached_snapshot():
from nanobot.agent.hook import AgentHook, AgentRunHookContext
@pytest.mark.parametrize("composite", [False, True])
async def test_runner_run_level_context_is_detached_snapshot(composite):
from nanobot.agent.hook import AgentHook, AgentRunHookContext, CompositeHook
from nanobot.agent.runner import AgentRunner
provider = MagicMock(spec=LLMProvider)
@@ -422,6 +424,7 @@ async def test_runner_run_level_context_is_detached_snapshot():
async def on_finally(self, context: AgentRunHookContext) -> None:
context.messages[0]["content"] = "mutated-finally"
hook = MutatingRunHook()
runner = AgentRunner()
result = await runner.run(make_run_spec(provider,
initial_messages=[{"role": "user", "content": "hi"}],
@@ -429,7 +432,7 @@ async def test_runner_run_level_context_is_detached_snapshot():
model="test-model",
max_iterations=2,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
hook=MutatingRunHook(),
hook=CompositeHook([hook]) if composite else hook,
))
assert request_messages[0][0]["content"] == "hi"
@@ -440,6 +443,68 @@ async def test_runner_run_level_context_is_detached_snapshot():
]
@pytest.mark.asyncio
async def test_runner_snapshots_for_composite_subclass_run_callback():
from nanobot.agent.hook import AgentRunHookContext, CompositeHook
from nanobot.agent.progress_hook import AgentProgressHook
from nanobot.agent.runner import AgentRunner
provider = MagicMock(spec=LLMProvider)
provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="done"))
tools = MagicMock()
tools.get_definitions.return_value = []
snapshots: list[list[dict]] = []
class CaptureCompositeHook(CompositeHook):
async def after_run(self, context: AgentRunHookContext) -> None:
snapshots.append([dict(message) for message in context.messages])
context.messages[0]["content"] = "hook-only"
hook = CaptureCompositeHook([AgentProgressHook()])
result = await AgentRunner().run(make_run_spec(provider,
initial_messages=[{"role": "user", "content": "hi"}],
tools=tools,
model="test-model",
max_iterations=1,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
hook=hook,
))
assert hook.has_run_hooks() is True
assert snapshots == [result.messages]
assert result.messages[0]["content"] == "hi"
assert result.final_content == "done"
@pytest.mark.asyncio
@pytest.mark.parametrize("outcome", ["error", "cancelled"])
async def test_runner_skips_run_snapshots_without_consumers_on_exception(outcome):
import asyncio
from nanobot.agent.progress_hook import AgentProgressHook
from nanobot.agent.runner import AgentRunner
error = RuntimeError("provider exploded") if outcome == "error" else asyncio.CancelledError()
provider = MagicMock(spec=LLMProvider)
provider.chat_stream_with_retry = AsyncMock(side_effect=error)
tools = MagicMock()
tools.get_definitions.return_value = []
with patch("nanobot.agent.runner.deepcopy", wraps=deepcopy) as copy_state:
with pytest.raises(type(error)):
await AgentRunner().run(make_run_spec(provider,
initial_messages=[{"role": "user", "content": "hi"}],
tools=tools,
model="test-model",
max_iterations=1,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
hook=AgentProgressHook(),
))
provider.chat_stream_with_retry.assert_awaited_once()
assert copy_state.call_count == 0
@pytest.mark.asyncio
async def test_runner_calls_on_error_for_model_error_result():
from nanobot.agent.hook import AgentHook, AgentRunHookContext