fix: update facade tests for contextvar hooks + fix ephemeral guard
The test_run_populates_tools_used_across_iterations, test_run_populates_final_messages, and test_run_user_hooks_still_fire_alongside_capture tests were reading bot._loop._extra_hooks directly in their fake_process_direct mocks, but the contextvar change moved per-call hooks to _per_call_hooks. Also fixes the ephemeral guard: per_call hooks should not be used in ephemeral mode (the original code incorrectly applied them regardless).
This commit is contained in:
@@ -728,7 +728,9 @@ class AgentLoop:
|
|||||||
)
|
)
|
||||||
hook: AgentHook = loop_hook
|
hook: AgentHook = loop_hook
|
||||||
per_call = _per_call_hooks.get()
|
per_call = _per_call_hooks.get()
|
||||||
extra_hooks = per_call if per_call is not None else (self._extra_hooks if not ephemeral else None)
|
extra_hooks = None
|
||||||
|
if not ephemeral:
|
||||||
|
extra_hooks = per_call if per_call is not None else self._extra_hooks
|
||||||
if extra_hooks:
|
if extra_hooks:
|
||||||
hook = CompositeHook([loop_hook] + extra_hooks)
|
hook = CompositeHook([loop_hook] + extra_hooks)
|
||||||
|
|
||||||
|
|||||||
@@ -184,8 +184,10 @@ async def test_run_populates_tools_used_across_iterations(tmp_path):
|
|||||||
bot = Nanobot.from_config(config_path, workspace=tmp_path)
|
bot = Nanobot.from_config(config_path, workspace=tmp_path)
|
||||||
|
|
||||||
async def fake_process_direct(message, *, session_key):
|
async def fake_process_direct(message, *, session_key):
|
||||||
# Whatever hooks the SDK installed are now on the loop.
|
# Whatever hooks the SDK installed are now in the contextvar.
|
||||||
extras = bot._loop._extra_hooks
|
from nanobot.agent.loop import _per_call_hooks
|
||||||
|
|
||||||
|
extras = _per_call_hooks.get() or []
|
||||||
messages = [{"role": "user", "content": message}]
|
messages = [{"role": "user", "content": message}]
|
||||||
ctx1 = AgentHookContext(iteration=0, messages=messages)
|
ctx1 = AgentHookContext(iteration=0, messages=messages)
|
||||||
ctx1.tool_calls = [
|
ctx1.tool_calls = [
|
||||||
@@ -217,7 +219,9 @@ async def test_run_populates_final_messages(tmp_path):
|
|||||||
bot = Nanobot.from_config(config_path, workspace=tmp_path)
|
bot = Nanobot.from_config(config_path, workspace=tmp_path)
|
||||||
|
|
||||||
async def fake_process_direct(message, *, session_key):
|
async def fake_process_direct(message, *, session_key):
|
||||||
extras = bot._loop._extra_hooks
|
from nanobot.agent.loop import _per_call_hooks
|
||||||
|
|
||||||
|
extras = _per_call_hooks.get() or []
|
||||||
messages = [
|
messages = [
|
||||||
{"role": "user", "content": message},
|
{"role": "user", "content": message},
|
||||||
{"role": "assistant", "content": "hi there"},
|
{"role": "assistant", "content": "hi there"},
|
||||||
@@ -266,7 +270,9 @@ async def test_run_user_hooks_still_fire_alongside_capture(tmp_path):
|
|||||||
seen_iterations.append(context.iteration)
|
seen_iterations.append(context.iteration)
|
||||||
|
|
||||||
async def fake_process_direct(message, *, session_key):
|
async def fake_process_direct(message, *, session_key):
|
||||||
extras = bot._loop._extra_hooks
|
from nanobot.agent.loop import _per_call_hooks
|
||||||
|
|
||||||
|
extras = _per_call_hooks.get() or []
|
||||||
assert len(extras) == 2, f"expected capture + user hook, got {len(extras)}"
|
assert len(extras) == 2, f"expected capture + user hook, got {len(extras)}"
|
||||||
ctx = AgentHookContext(iteration=7, messages=[])
|
ctx = AgentHookContext(iteration=7, messages=[])
|
||||||
for h in extras:
|
for h in extras:
|
||||||
|
|||||||
Reference in New Issue
Block a user