mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-19 21:37:12 +03:00
ab842ab611
The agent tests seed and read the store through the storage fixtures.
802 lines
36 KiB
Python
802 lines
36 KiB
Python
"""Unit tests for the extracted turn prologue (``agent/turn_context.py``).
|
|
|
|
These exercise ``build_turn_context`` against a lightweight fake agent to
|
|
confirm the prologue produces the right ``TurnContext`` and applies the
|
|
``agent`` side effects the loop relies on — without spinning up a real
|
|
``AIAgent`` or hitting any provider.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import threading
|
|
import types
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from agent.message_metadata import TYPED_ASK
|
|
from agent.prompt_builder import steer_user_row
|
|
from agent.turn_context import (
|
|
PreflightCompressionTimedOut,
|
|
TurnContext,
|
|
build_turn_context,
|
|
)
|
|
|
|
|
|
class _FakeTodoStore:
|
|
def has_items(self):
|
|
return True
|
|
|
|
def _hydrate(self, *_a, **_k):
|
|
pass
|
|
|
|
|
|
class _FakeGuardrails:
|
|
def __init__(self):
|
|
self.reset_called = False
|
|
|
|
def reset_for_turn(self):
|
|
self.reset_called = True
|
|
|
|
|
|
class _FakeAgent:
|
|
"""Minimal stand-in covering only what the prologue touches."""
|
|
|
|
def __init__(self):
|
|
self.session_id = "sess-1"
|
|
self.model = "test/model"
|
|
self.provider = "openrouter"
|
|
self.requested_provider = "openrouter"
|
|
self.base_url = "https://openrouter.ai/api/v1"
|
|
self.api_key = "sk-x"
|
|
self.api_mode = "chat_completions"
|
|
self.platform = "cli"
|
|
self.quiet_mode = True
|
|
self.max_iterations = 90
|
|
self.tools = []
|
|
self.valid_tool_names = set()
|
|
self.enabled_toolsets = None
|
|
self.disabled_toolsets = None
|
|
self._skip_mcp_refresh = False
|
|
self.compression_enabled = False
|
|
self.context_compressor = types.SimpleNamespace(
|
|
protect_first_n=2, protect_last_n=2
|
|
)
|
|
# Make the fake compressor honour the ContextEngine contract that the
|
|
# real code now relies on (should_compress_info returns a (bool, reason)
|
|
# tuple). Without it build_turn_context raises AttributeError.
|
|
def _fake_should_compress(tokens=None):
|
|
return False
|
|
|
|
def _fake_should_compress_info(tokens=None):
|
|
return (False, None)
|
|
|
|
self.context_compressor.should_compress = _fake_should_compress
|
|
self.context_compressor.should_compress_info = _fake_should_compress_info
|
|
self._cached_system_prompt = "SYSTEM"
|
|
self._memory_store = None
|
|
self._memory_manager = None
|
|
self._memory_nudge_interval = 0
|
|
self._turns_since_memory = 0
|
|
self._user_turn_count = 0
|
|
self._todo_store = _FakeTodoStore()
|
|
self._tool_guardrails = _FakeGuardrails()
|
|
self._compression_warning = None
|
|
self._emit_warning = MagicMock()
|
|
self._last_ctx_overflow_warn = None
|
|
self._interrupt_requested = False
|
|
self._memory_write_origin = "assistant_tool"
|
|
self._stream_context_scrubber = None
|
|
self._stream_think_scrubber = None
|
|
# Attributes the prologue assigns; recorded for assertions.
|
|
self._invalid_tool_retries = -1
|
|
self._persist_calls = 0
|
|
self._session_messages = []
|
|
self._pending_cli_user_message = None
|
|
self._session_persist_lock = threading.RLock()
|
|
# Records _cached_system_prompt at the moment _ensure_db_session()
|
|
# is called (regression guard for #45499 turn-setup ordering).
|
|
self._ensure_db_prompt_at_call = "<unset>"
|
|
|
|
def _warn_context_overflow_blocked(self, reason, preflight_tokens, threshold_tokens):
|
|
# Mirror the real AIAgent helper so tests can assert the warning fired.
|
|
_warn_kind = (reason or "unknown").split(":", 1)[0]
|
|
_warn_key = ("ctx_overflow_blocked", _warn_kind)
|
|
if self._last_ctx_overflow_warn != _warn_key:
|
|
self._last_ctx_overflow_warn = _warn_key
|
|
self._emit_warning(
|
|
f"⚠ Context is over the compression threshold "
|
|
f"(~{preflight_tokens:,} tokens >= {threshold_tokens:,}) "
|
|
f"but compression is currently blocked ({reason})."
|
|
)
|
|
|
|
def _clear_context_overflow_warn(self):
|
|
self._last_ctx_overflow_warn = None
|
|
|
|
# --- methods the prologue calls ---
|
|
def _ensure_db_session(self):
|
|
self._ensure_db_prompt_at_call = self._cached_system_prompt
|
|
|
|
def _restore_primary_runtime(self):
|
|
pass
|
|
|
|
def _cleanup_dead_connections(self):
|
|
return False
|
|
|
|
def _emit_status(self, _msg):
|
|
pass
|
|
|
|
def _replay_compression_warning(self):
|
|
pass
|
|
|
|
def _hydrate_todo_store(self, *_a, **_k):
|
|
pass
|
|
|
|
def _safe_print(self, *_a, **_k):
|
|
pass
|
|
|
|
def _persist_session(self, *_a, **_k):
|
|
self._persist_calls += 1
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _stub_runtime_main():
|
|
"""``build_turn_context`` calls ``auxiliary_client.set_runtime_main`` as a
|
|
production side effect (telling aux tools the live main provider/model).
|
|
That writes a module-level global these unit tests don't care about and
|
|
which would otherwise leak into sibling tests (e.g. provider-parity
|
|
resolution) when the per-test process isolation plugin is disabled. Stub
|
|
it out so the prologue tests stay hermetic.
|
|
"""
|
|
with patch("agent.auxiliary_client.set_runtime_main", lambda *a, **k: None):
|
|
yield
|
|
|
|
|
|
def _build(agent, **overrides):
|
|
kwargs = dict(
|
|
agent=agent,
|
|
user_message="hello",
|
|
system_message=None,
|
|
conversation_history=None,
|
|
task_id=None,
|
|
stream_callback=None,
|
|
persist_user_message=None,
|
|
restore_or_build_system_prompt=lambda *a, **k: None,
|
|
install_safe_stdio=lambda: None,
|
|
sanitize_surrogates=lambda s: s,
|
|
summarize_user_message_for_log=lambda s: s,
|
|
set_session_context=lambda _sid: None,
|
|
set_current_write_origin=lambda _o: None,
|
|
ra=lambda: types.SimpleNamespace(_set_interrupt=lambda *a, **k: None),
|
|
)
|
|
kwargs.update(overrides)
|
|
return build_turn_context(**kwargs)
|
|
|
|
|
|
def test_returns_turn_context_with_user_message_appended():
|
|
agent = _FakeAgent()
|
|
ctx = _build(agent)
|
|
assert isinstance(ctx, TurnContext)
|
|
assert ctx.user_message == "hello"
|
|
# The user turn was appended and indexed.
|
|
assert ctx.messages[-1]["role"] == "user"
|
|
assert ctx.messages[-1]["content"] == "hello"
|
|
assert isinstance(ctx.messages[-1]["timestamp"], float)
|
|
assert ctx.current_turn_user_idx == len(ctx.messages) - 1
|
|
assert ctx.active_system_prompt == "SYSTEM"
|
|
|
|
|
|
def test_preflight_timeout_stops_turn_before_provider_boundary():
|
|
"""An unchanged payload above the model window must not escape turn construction (a request that
|
|
still fits its window is sent uncompressed instead — see test_preflight_compression_timeout_fail_closed)."""
|
|
agent = _FakeAgent()
|
|
agent.compression_enabled = True
|
|
agent.max_compression_attempts = 3
|
|
agent.context_compressor = types.SimpleNamespace(
|
|
protect_first_n=2,
|
|
protect_last_n=2,
|
|
threshold_tokens=1_000,
|
|
context_length=1_500,
|
|
summary_target_ratio=0.3,
|
|
last_prompt_tokens=0,
|
|
should_compress=lambda tokens=None: True,
|
|
should_compress_info=lambda tokens=None: (True, None),
|
|
get_active_compression_failure_cooldown=lambda: None,
|
|
)
|
|
|
|
def stalled_compression(messages, *_args, **_kwargs):
|
|
agent._last_compression_timed_out = True
|
|
return messages, "SYSTEM"
|
|
|
|
agent._compress_context = MagicMock(side_effect=stalled_compression)
|
|
oversized_history = [
|
|
{"role": "assistant", "content": "x" * 8_000},
|
|
]
|
|
provider_call = MagicMock()
|
|
|
|
def run_turn():
|
|
context = _build(agent, conversation_history=oversized_history)
|
|
provider_call(context.messages)
|
|
|
|
with pytest.raises(PreflightCompressionTimedOut, match="provider call was not sent"):
|
|
run_turn()
|
|
|
|
agent._compress_context.assert_called_once()
|
|
provider_call.assert_not_called()
|
|
|
|
|
|
def test_user_message_preserves_platform_event_timestamp():
|
|
agent = _FakeAgent()
|
|
|
|
ctx = _build(agent, persist_user_timestamp=123.5)
|
|
|
|
assert ctx.messages[-1]["timestamp"] == 123.5
|
|
|
|
|
|
# ── Trivial-prompt prefetch gate (PR #25350 salvage) ─────────────────────────
|
|
#
|
|
# The prologue is the ONLY place the per-turn synchronous
|
|
# memory_manager.prefetch_all() fires; a bare greeting must not block the
|
|
# turn on provider network round-trips, while a substantive question must
|
|
# still prefetch. These assert the gate at the call site (the classifier
|
|
# itself is covered in tests/agent/test_memory_provider.py).
|
|
|
|
|
|
def _agent_with_memory_manager():
|
|
agent = _FakeAgent()
|
|
mm = MagicMock()
|
|
mm.prefetch_all.return_value = "REMEMBERED CONTEXT"
|
|
agent._memory_manager = mm
|
|
return agent, mm
|
|
|
|
|
|
def test_prefetch_skipped_for_trivial_user_message():
|
|
agent, mm = _agent_with_memory_manager()
|
|
ctx = _build(agent, user_message="hi!")
|
|
mm.prefetch_all.assert_not_called()
|
|
assert ctx.ext_prefetch_cache == ""
|
|
|
|
|
|
def test_prefetch_runs_for_substantive_user_message():
|
|
agent, mm = _agent_with_memory_manager()
|
|
query = "what did we decide about the deploy pipeline?"
|
|
ctx = _build(agent, user_message=query)
|
|
mm.prefetch_all.assert_called_once_with(query, session_id=agent.session_id)
|
|
assert ctx.ext_prefetch_cache == "REMEMBERED CONTEXT"
|
|
|
|
|
|
# ── Per-turn author ──────────────────────────────────────────────────────────
|
|
|
|
|
|
def test_turn_author_is_normalized_then_reaches_on_turn_start_and_the_agent_stash():
|
|
agent, mm = _agent_with_memory_manager()
|
|
|
|
_build(agent, user_message="what did we decide about the deploy pipeline?",
|
|
turn_author={"id": " bot:al\x00pha ", "name": "Alpha", "is_bot": 1, "x": 1})
|
|
|
|
kwargs = mm.on_turn_start.call_args.kwargs
|
|
assert (kwargs["author_id"], kwargs["author_name"], kwargs["author_is_bot"]) == ("bot:alpha", "Alpha", True)
|
|
# The end-of-turn sync reads this back off the agent.
|
|
assert agent._turn_author == {"id": "bot:alpha", "name": "Alpha", "is_bot": True}
|
|
|
|
|
|
_PREVIOUS = "Add the ShapeIndex encoder."
|
|
_PRIOR_TURN = [{"role": "user", "content": _PREVIOUS}, {"role": "assistant", "content": "Done."}]
|
|
|
|
|
|
def _carrier(ask: str = "Round-trip the ShapeIndex.") -> str:
|
|
"""A compaction summary flipped into the user role, with the live ask merged after its end marker."""
|
|
from agent.context_compressor import HISTORICAL_TASK_HEADING, SUMMARY_PREFIX, _SUMMARY_END_MARKER
|
|
|
|
return f"{SUMMARY_PREFIX}\n{HISTORICAL_TASK_HEADING}\nold task\n\n{_SUMMARY_END_MARKER}" + (f"\n\n{ask}" if ask else "")
|
|
|
|
|
|
def _compaction_returning_history(history_for):
|
|
"""A turn-start compaction that hands back what ``conversation_history_after_compression`` does."""
|
|
from agent.turn_context_compaction import CompactionOutcome
|
|
|
|
def run(agent, *, messages, active_system_prompt, current_turn_user_idx, **_):
|
|
return CompactionOutcome(
|
|
messages=messages, active_system_prompt=active_system_prompt,
|
|
conversation_history=history_for(messages), current_turn_user_idx=current_turn_user_idx,
|
|
)
|
|
return run
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"history, compaction, expected",
|
|
[
|
|
(_PRIOR_TURN, None, _PREVIOUS),
|
|
# In place, the flush baseline already ends in this turn's own message.
|
|
(_PRIOR_TURN, _compaction_returning_history(list), _PREVIOUS),
|
|
# Rotation hands back no baseline at all.
|
|
(_PRIOR_TURN, _compaction_returning_history(lambda _messages: None), _PREVIOUS),
|
|
(None, None, ""),
|
|
([{"role": "user", "content": [{"type": "text", "text": _PREVIOUS},
|
|
{"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}]},
|
|
{"role": "assistant", "content": "Done."}], None, _PREVIOUS),
|
|
(_PRIOR_TURN + [{"role": "user", "content": "[System: Your previous response was truncated. Continue."}],
|
|
None, _PREVIOUS),
|
|
(_PRIOR_TURN + [{"role": "user", "content": "1. [ ] encode", "_todo_snapshot_synthetic": True}],
|
|
None, _PREVIOUS),
|
|
(_PRIOR_TURN + [{"role": "user", "content": _carrier()}, {"role": "assistant", "content": "Done."}],
|
|
None, "Round-trip the ShapeIndex."),
|
|
(_PRIOR_TURN + [{"role": "user", "content": _carrier("[Thu 2026-11-05 10:00:00 IST] Round-trip the ShapeIndex.")},
|
|
{"role": "assistant", "content": "Done."}], None, "Round-trip the ShapeIndex."),
|
|
(_PRIOR_TURN + [{"role": "user", "content": _carrier(ask="")}], None, _PREVIOUS),
|
|
(_PRIOR_TURN + [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}]},
|
|
{"role": "assistant", "content": "Done."}], None, ""),
|
|
# Gateway replay renders a timestamp prefix and drops the steer row's display_kind.
|
|
([{"role": "user", "content": f"[Thu 2026-11-05 10:00:00 IST] {_PREVIOUS}"}, {"role": "assistant", "content": "Done."}],
|
|
None, _PREVIOUS),
|
|
(_PRIOR_TURN + [{"role": "user", "content": steer_user_row("use zstd")["content"]}, {"role": "assistant", "content": "Ok."}],
|
|
None, _PREVIOUS),
|
|
(_PRIOR_TURN + [{"role": "user", "content": "[Thu 2026-11-05 10:01:00 IST] " + steer_user_row("use zstd")["content"]},
|
|
{"role": "assistant", "content": "Ok."}], None, _PREVIOUS),
|
|
(_PRIOR_TURN + [{"role": "user", "content": [{"type": "text", "text": "\n\n" + steer_user_row("use zstd")["content"]}]},
|
|
{"role": "assistant", "content": "Ok."}], None, _PREVIOUS),
|
|
# A multimodal turn's injected context is flattened into the stored row with the ask.
|
|
([{"role": "user", "content": f"{_PREVIOUS}\n[screenshot]\n\nPLUGIN NOTE", "display_metadata": {TYPED_ASK: _PREVIOUS}},
|
|
{"role": "assistant", "content": "Done."}], None, _PREVIOUS),
|
|
# With gateway timestamps on, the live text part (and so the stamp) carries the rendered prefix.
|
|
([{"role": "user", "content": "x", "display_metadata": {TYPED_ASK: f"[Thu 2026-11-05 10:00:00 IST] {_PREVIOUS}"}},
|
|
{"role": "assistant", "content": "Done."}], None, _PREVIOUS),
|
|
([{"role": "user", "content": f"{_PREVIOUS}\n[screenshot]\n<memory-context>\nlikes tea\n</memory-context>"},
|
|
{"role": "assistant", "content": "Done."}], None, f"{_PREVIOUS}\n[screenshot]"),
|
|
],
|
|
ids=["no-compaction", "in-place-compaction", "rotation", "first-turn", "multimodal", "skips-scaffolding",
|
|
"skips-flagged-scaffolding", "compaction-carrier-ask", "carrier-ask-timestamp", "skips-bare-summary", "image-only-stops-search",
|
|
"strips-gateway-timestamp", "skips-replayed-steer", "skips-timestamped-steer", "skips-steer-text-part",
|
|
"typed-ask-stamp", "timestamped-stamp", "unstamped-row-drops-memory-fence"],
|
|
)
|
|
def test_providers_get_what_the_person_last_asked(history, compaction, expected, monkeypatch):
|
|
"""A resumed process must tell providers what the last turn asked: a live process may have keyed
|
|
end-of-turn recall on it, and a provider may save no transcript of its own. Turn-start compaction
|
|
rebinds the history after this turn's message is appended, so the value has to be read before it."""
|
|
if compaction is not None:
|
|
monkeypatch.setattr("agent.turn_context_compaction.run_turn_start_compaction", compaction)
|
|
agent, mm = _agent_with_memory_manager()
|
|
|
|
_build(agent, user_message="Now decode it back.", conversation_history=history)
|
|
|
|
assert mm.on_turn_start.call_args.kwargs["previous_message"] == expected
|
|
|
|
|
|
@pytest.mark.parametrize("force_user_leading", [True, False])
|
|
def test_compaction_around_a_multimodal_turn_keeps_the_typed_ask(force_user_leading):
|
|
"""Compaction can wrap the turn before its context is appended (turn-start) or after (a later
|
|
pass); either way the stamp names the person's ask, never the summary or the injected notes."""
|
|
from agent.context_compressor import ContextCompressor
|
|
from agent.turn_context import (
|
|
_previous_user_message_text, _stamp_typed_ask_once, append_notes_to_multimodal_content,
|
|
)
|
|
|
|
compressor = ContextCompressor.__new__(ContextCompressor)
|
|
compressor._summary_has_user_turn = True
|
|
image = {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}
|
|
|
|
def turn():
|
|
return {"role": "user", "content": [{"type": "text", "text": "what is this"}, image]}
|
|
|
|
def inject(row):
|
|
_stamp_typed_ask_once(types.SimpleNamespace(), row)
|
|
append_notes_to_multimodal_content(row["content"], "PLUGIN NOTE")
|
|
|
|
def wrap(row):
|
|
compressor._merge_summary_into_tail_row(row, "[CONTEXT COMPACTION] old work", "user", force_user_leading)
|
|
|
|
before, after = turn(), turn()
|
|
wrap(before), inject(before)
|
|
inject(after), wrap(after)
|
|
for row in (before, after):
|
|
assert _previous_user_message_text([row, {"role": "assistant", "content": "x"}]) == "what is this"
|
|
|
|
|
|
def test_gateway_notes_on_a_multimodal_turn_stamp_the_persisted_ask():
|
|
"""Gateway notes land on the list before any memory context, and a list persist override (TUI
|
|
image turns) drops API-only parts the stamp must not keep either."""
|
|
from types import SimpleNamespace
|
|
from agent.turn_context import _merge_gateway_notes, _previous_user_message_text
|
|
|
|
image = {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}
|
|
for override, expected in ((None, "what is this"), ([{"type": "text", "text": "what is this"}, image], "what is this")):
|
|
row = {"role": "user", "content": [{"type": "text", "text": "what is this"},
|
|
{"type": "text", "text": "[barge-in note]"}, image] if override else
|
|
[{"type": "text", "text": "what is this"}, image]}
|
|
agent = SimpleNamespace(_gateway_turn_context_notes="GATEWAY NOTE", _persist_user_message_override=override)
|
|
_merge_gateway_notes(agent, [row], 0, "")
|
|
|
|
assert row["content"][-1] == {"type": "text", "text": "GATEWAY NOTE"}
|
|
assert _previous_user_message_text([row, {"role": "assistant", "content": "x"}]) == expected
|
|
|
|
|
|
def test_a_todo_snapshot_folded_onto_the_turn_stays_out_of_the_typed_ask():
|
|
"""Turn-start compaction folds the active task list onto the tail turn before context is appended;
|
|
a later compaction strips that block from the row, so a stamp holding it would outlive it."""
|
|
from tools.todo_tool import TODO_INJECTION_HEADER
|
|
from agent.turn_context import _stamp_typed_ask_once
|
|
|
|
image = {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}
|
|
row = {"role": "user", "content": [{"type": "text", "text": f"what is this\n\n{TODO_INJECTION_HEADER}\n- [ ] a"}, image]}
|
|
_stamp_typed_ask_once(types.SimpleNamespace(), row)
|
|
|
|
assert row["display_metadata"] == {TYPED_ASK: "what is this"}
|
|
|
|
|
|
@pytest.mark.parametrize("vetoed", [False, True], ids=["context-backfill", "veto"])
|
|
def test_a_row_rewrite_keeps_a_reaction_another_writer_added(tmp_path, vetoed):
|
|
"""A TUI submit row exists before the prologue, so a reaction can land on it while the agent builds;
|
|
the in-place rewrite (stamp backfill or veto) must merge its metadata, not replace the row's."""
|
|
import threading
|
|
from persistence_assembly import open_persistence, profile_scope
|
|
from agent.turn_context import _append_multimodal_context, _withhold_blocked_prompt
|
|
from tests.fixtures.storage_read import conversation
|
|
from tests.fixtures.storage_seed import seed_messages, seed_session
|
|
|
|
db = open_persistence(profile_scope(home=tmp_path))
|
|
seed_session(db, "s", "cli")
|
|
image = {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}
|
|
row = {"role": "user", "content": [{"type": "text", "text": "secret plan"}, image]}
|
|
[row["_row_id"]] = seed_messages(db, "s", {"role": "user", "content": "secret plan"})
|
|
db.messages.set_reaction("s", row["_row_id"], "👍", author="user")
|
|
agent = types.SimpleNamespace(_session_db=db, persistence=db, session_id="s", _session_persist_lock=threading.Lock(),
|
|
_persist_user_message_override=None, _last_compaction_in_place=False)
|
|
_append_multimodal_context(agent, row, "", "PLUGIN NOTE", preflight_compressed=False)
|
|
if vetoed:
|
|
_withhold_blocked_prompt(agent, row, row["content"])
|
|
|
|
stored = conversation(db, "s")[-1]["display_metadata"]
|
|
assert [r["emoji"] for r in stored["reactions"]] == ["👍"]
|
|
assert (TYPED_ASK in stored) is not vetoed # a veto removes the backfilled stamp from the row too
|
|
|
|
|
|
def test_gateway_notes_alone_on_a_stored_image_turn_backfill_the_row(tmp_path):
|
|
"""Only gateway notes landed on an already-stored image turn (no memory, no hook context): the row
|
|
still gets the notes and the stamp, or a restart would read the notes back as the ask."""
|
|
import threading
|
|
from persistence_assembly import open_persistence, profile_scope
|
|
from agent.turn_context import _append_multimodal_context, _merge_gateway_notes, _previous_user_message_text
|
|
from tests.fixtures.storage_read import conversation
|
|
from tests.fixtures.storage_seed import seed_messages, seed_session
|
|
|
|
db = open_persistence(profile_scope(home=tmp_path))
|
|
seed_session(db, "s", "cli")
|
|
image = {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}
|
|
row = {"role": "user", "content": [{"type": "text", "text": "look"}, image]}
|
|
[row["_row_id"]] = seed_messages(db, "s", {"role": "user", "content": "look"})
|
|
agent = types.SimpleNamespace(_session_db=db, persistence=db, session_id="s", _session_persist_lock=threading.Lock(),
|
|
_persist_user_message_override=None, _last_compaction_in_place=False,
|
|
_gateway_turn_context_notes="GATEWAY NOTE")
|
|
plugin_user_context = _merge_gateway_notes(agent, [row], 0, "")
|
|
_append_multimodal_context(agent, row, "", plugin_user_context, preflight_compressed=False)
|
|
|
|
reloaded = conversation(db, "s")
|
|
assert "GATEWAY NOTE" in reloaded[-1]["content"]
|
|
assert _previous_user_message_text([*reloaded, {"role": "assistant", "content": "x"}]) == "look"
|
|
|
|
|
|
def test_a_vetoed_turn_drops_its_typed_ask_stamp():
|
|
"""A gateway note can stamp a multimodal turn before a ``pre_llm_call`` hook vetoes it; the vetoed
|
|
text must not survive in the stamp, where ``previous_message`` would hand it to memory providers."""
|
|
from types import SimpleNamespace
|
|
from agent.turn_context import _previous_user_message_text, _withhold_blocked_prompt
|
|
|
|
row = {"role": "user", "content": [{"type": "text", "text": "secret plan"}],
|
|
"display_metadata": {TYPED_ASK: "secret plan"}}
|
|
_withhold_blocked_prompt(SimpleNamespace(_session_db=None), row, row["content"])
|
|
|
|
assert TYPED_ASK not in row["display_metadata"]
|
|
assert "secret plan" not in _previous_user_message_text([row, {"role": "assistant", "content": "x"}])
|
|
|
|
|
|
def test_turn_without_author_clears_previous_bot_author():
|
|
agent, mm = _agent_with_memory_manager()
|
|
_build(agent, user_message="first turn", turn_author={"id": "bot:alpha", "name": "Alpha", "is_bot": True})
|
|
assert agent._turn_author["id"] == "bot:alpha"
|
|
|
|
_build(agent, user_message="second turn")
|
|
|
|
assert agent._turn_author is None
|
|
kwargs = mm.on_turn_start.call_args.kwargs
|
|
assert kwargs["author_id"] is None
|
|
assert kwargs["author_is_bot"] is False
|
|
|
|
|
|
def test_turn_author_reaches_the_agent_through_the_real_facade(monkeypatch):
|
|
"""``AIAgent.run_conversation(turn_author=...)`` crosses the facade and the loop entry point, not only
|
|
``build_turn_context``; a kwarg dropped at either hop raised TypeError on every real turn."""
|
|
from types import SimpleNamespace
|
|
from run_agent import AIAgent
|
|
|
|
class _Completions:
|
|
def create(self, **_kw):
|
|
return SimpleNamespace(choices=[SimpleNamespace(
|
|
message=SimpleNamespace(content="ok", tool_calls=None, reasoning=None, reasoning_content=None),
|
|
finish_reason="stop")], usage=None, model="test-model")
|
|
|
|
monkeypatch.setattr("agent.process_bootstrap.OpenAI",
|
|
lambda **_kw: SimpleNamespace(chat=SimpleNamespace(completions=_Completions())))
|
|
monkeypatch.setattr("model_tools.get_tool_definitions", lambda *a, **k: [])
|
|
agent = AIAgent(model="test-model", api_key="k", base_url="http://localhost:1/v1", platform="cli",
|
|
max_iterations=2, quiet_mode=True, skip_memory=True)
|
|
agent._disable_streaming = True
|
|
|
|
agent.run_conversation("hi", turn_author={"id": "bot:alpha", "name": "Alpha", "is_bot": True})
|
|
assert agent._turn_author == {"id": "bot:alpha", "name": "Alpha", "is_bot": True}
|
|
agent.run_conversation("hi again")
|
|
assert agent._turn_author is None
|
|
|
|
|
|
# ── on_turn_start model kwarg ────────────────────────────────────────────────
|
|
# The prologue (the only on_turn_start caller) passes model=agent.model.
|
|
|
|
|
|
def test_on_turn_start_forwards_model_kwarg():
|
|
agent, mm = _agent_with_memory_manager()
|
|
_build(agent, user_message="what did we decide about the deploy pipeline?")
|
|
mm.on_turn_start.assert_called_once()
|
|
_args, kwargs = mm.on_turn_start.call_args
|
|
assert kwargs.get("model") == agent.model == "test/model"
|
|
|
|
|
|
def test_on_turn_start_model_kwarg_defaults_to_empty_string_when_absent():
|
|
"""Agents whose `model` attribute is falsy (e.g. unset during subagent
|
|
scaffolding) must not raise — the call site falls back to "" rather
|
|
than propagating None into the memory-provider contract."""
|
|
agent, mm = _agent_with_memory_manager()
|
|
agent.model = None
|
|
_build(agent, user_message="what did we decide about the deploy pipeline?")
|
|
mm.on_turn_start.assert_called_once()
|
|
_args, kwargs = mm.on_turn_start.call_args
|
|
assert kwargs.get("model") == ""
|
|
|
|
|
|
def test_turn_start_replaces_stale_parent_history_with_compression_child():
|
|
agent = _FakeAgent()
|
|
stale_history = [{"role": "user", "content": "stale parent"}]
|
|
compacted_history = [
|
|
{"role": "user", "content": "[CONTEXT COMPACTION] summary"},
|
|
{"role": "assistant", "content": "child tail"},
|
|
]
|
|
|
|
def _recover(_agent):
|
|
_agent.session_id = "compression-child"
|
|
return compacted_history
|
|
|
|
log_context = MagicMock()
|
|
with patch(
|
|
"agent.turn_context.recover_rotated_compression_session",
|
|
side_effect=_recover,
|
|
):
|
|
ctx = _build(
|
|
agent,
|
|
conversation_history=stale_history,
|
|
set_session_context=log_context,
|
|
)
|
|
|
|
assert agent.session_id == "compression-child"
|
|
assert agent._current_turn_id.startswith("compression-child:")
|
|
log_context.assert_called_once_with("compression-child")
|
|
assert ctx.conversation_history == compacted_history
|
|
assert ctx.messages[:-1] == compacted_history
|
|
assert ctx.messages[-1]["role"] == "user"
|
|
assert ctx.messages[-1]["content"] == "hello"
|
|
assert isinstance(ctx.messages[-1]["timestamp"], float)
|
|
assert all(message.get("content") != "stale parent" for message in ctx.messages)
|
|
|
|
|
|
def test_applies_agent_side_effects():
|
|
agent = _FakeAgent()
|
|
_build(agent)
|
|
# Retry counters reset, guardrails reset, turn counted.
|
|
assert agent._invalid_tool_retries == 0
|
|
assert agent._tool_guardrails.reset_called is True
|
|
assert agent._user_turn_count == 1
|
|
# Crash-resilience persistence fired once.
|
|
assert agent._persist_calls == 1
|
|
# task/turn ids assigned on the agent.
|
|
assert agent._current_task_id
|
|
assert agent._current_turn_id
|
|
|
|
|
|
def test_pending_cli_message_uses_clean_override_for_api_local_note():
|
|
"""A noted API message reuses the clean staged dict and its DB marker."""
|
|
agent = _FakeAgent()
|
|
staged = {"role": "user", "content": "clean prompt", "_db_persisted": True}
|
|
agent._pending_cli_user_message = staged
|
|
|
|
ctx = _build(
|
|
agent,
|
|
user_message="[MODEL NOTE]\n\nclean prompt",
|
|
persist_user_message="clean prompt",
|
|
)
|
|
|
|
assert ctx.messages[-1] is staged
|
|
assert ctx.messages[-1]["content"] == "[MODEL NOTE]\n\nclean prompt"
|
|
assert ctx.messages[-1]["_db_persisted"] is True
|
|
assert isinstance(ctx.messages[-1]["timestamp"], float)
|
|
assert agent._pending_cli_user_message is None
|
|
|
|
|
|
def test_recall_indicator_emitted_when_memory_injected():
|
|
"""When prefetch injects memory, the deterministic indicator is emitted."""
|
|
agent = _FakeAgent()
|
|
agent._emit_status = MagicMock()
|
|
mm = MagicMock()
|
|
mm.prefetch_all.return_value = "- recalled fact"
|
|
mm.describe_recall.return_value = "👁️ Hindsight — recalled 2 memories"
|
|
agent._memory_manager = mm
|
|
|
|
# A substantive query — a trivial prompt ("hi", "hello") skips prefetch_all
|
|
# entirely, so there'd be nothing to indicate. See is_trivial_prompt.
|
|
_build(agent, user_message="what did we decide about the deploy pipeline?")
|
|
|
|
agent._emit_status.assert_any_call("👁️ Hindsight — recalled 2 memories")
|
|
|
|
|
|
def test_recall_indicator_skipped_when_nothing_injected():
|
|
"""No memory injected → describe_recall isn't consulted, nothing emitted."""
|
|
agent = _FakeAgent()
|
|
agent._emit_status = MagicMock()
|
|
mm = MagicMock()
|
|
mm.prefetch_all.return_value = ""
|
|
agent._memory_manager = mm
|
|
|
|
# Substantive query so prefetch_all actually runs; it returns nothing, so the
|
|
# indicator path must stay silent (as opposed to being skipped as trivial).
|
|
_build(agent, user_message="what did we decide about the deploy pipeline?")
|
|
|
|
mm.describe_recall.assert_not_called()
|
|
for call in agent._emit_status.call_args_list:
|
|
assert "👁️" not in str(call)
|
|
|
|
|
|
def test_ensure_db_session_runs_after_system_prompt_restore():
|
|
"""Regression for #45499.
|
|
|
|
On a fresh API/gateway agent (``_cached_system_prompt is None``) the DB
|
|
session row must be created AFTER the system prompt is restored/built, so
|
|
the persisted snapshot is written non-NULL. If ``_ensure_db_session()``
|
|
ran first it would insert ``system_prompt=NULL`` and trip the misleading
|
|
"stored system prompt is null; rebuilding" warning plus a first-turn
|
|
prefix cache miss.
|
|
"""
|
|
agent = _FakeAgent()
|
|
agent._cached_system_prompt = None # fresh agent, no cached prompt yet
|
|
|
|
def _restore(_agent, _system_message, _history):
|
|
_agent._cached_system_prompt = "REBUILT-SYSTEM"
|
|
|
|
_build(agent, restore_or_build_system_prompt=_restore)
|
|
|
|
# The prompt was populated before the DB row was created.
|
|
assert agent._ensure_db_prompt_at_call == "REBUILT-SYSTEM"
|
|
assert agent._cached_system_prompt == "REBUILT-SYSTEM"
|
|
|
|
|
|
# ── Between-turns MCP refresh (cache-safe late-binding) ──────────────────────
|
|
#
|
|
# A slow MCP server that connects after the agent's build-time tool snapshot
|
|
# must become callable by the user's NEXT turn — without mutating an in-flight
|
|
# turn's cached request prefix. The prologue is exactly that boundary, so the
|
|
# refresh hook lives here. These assert the contract (R1/R2/R6 in the spec),
|
|
# not timing permutations.
|
|
|
|
|
|
def test_between_turns_refresh_adds_late_tool_when_servers_registered():
|
|
"""R1: a tool that registered since build lands in this turn's snapshot."""
|
|
agent = _FakeAgent()
|
|
|
|
new_def = {"type": "function", "function": {"name": "mcp_x_tool", "description": "", "parameters": {}}}
|
|
|
|
import model_tools
|
|
import tools.mcp_tool
|
|
with patch("tools.mcp_tool_discovery.has_registered_mcp_tools", return_value=True), \
|
|
patch.object(model_tools, "get_tool_definitions", return_value=[new_def]):
|
|
_build(agent)
|
|
|
|
assert "mcp_x_tool" in agent.valid_tool_names
|
|
assert any(t["function"]["name"] == "mcp_x_tool" for t in agent.tools)
|
|
|
|
|
|
class _TitlingAgent:
|
|
"""Only what ``_maybe_title_session_at_turn_start`` reads off an agent."""
|
|
|
|
def __init__(self, platform):
|
|
self.platform = platform
|
|
self.session_id = "sess-1"
|
|
self.model = "test/model"
|
|
self.provider = "openrouter"
|
|
self.base_url = "https://openrouter.ai/api/v1"
|
|
self.api_key = "sk-x"
|
|
self.api_mode = "chat_completions"
|
|
self._session_db = MagicMock()
|
|
self._session_db_created = True
|
|
|
|
|
|
def _title_turn(platform, message="Fix the login button"):
|
|
"""Run the prologue's titling step and return the maybe_auto_title mock."""
|
|
from agent import turn_context
|
|
|
|
with patch("agent.title_generator.maybe_auto_title") as titler:
|
|
turn_context._maybe_title_session_at_turn_start(
|
|
_TitlingAgent(platform),
|
|
[{"role": "user", "content": message}],
|
|
)
|
|
return titler
|
|
|
|
|
|
@pytest.mark.parametrize("platform", ["cli", "telegram", "desktop", "acp", None])
|
|
def test_prologue_titles_the_surfaces_a_person_reads(platform):
|
|
assert _title_turn(platform).called
|
|
|
|
|
|
@pytest.mark.parametrize("platform", ["cron", "CRON", "subagent"])
|
|
def test_prologue_does_not_title_machine_driven_runs(platform):
|
|
"""Cron names its own session after the job, and nobody opens a subagent's.
|
|
|
|
Both would otherwise pay a side-LLM call per run for a name that is either
|
|
overwritten or never read.
|
|
"""
|
|
assert not _title_turn(platform).called
|
|
|
|
|
|
def test_prologue_names_a_subagent_run_after_its_goal_without_a_model_call():
|
|
"""A delegate run gets ``Subagent: <goal>`` at derived authority so it reads as machinery
|
|
wherever ``sessions.show_subagents`` lists it, instead of staying untitled (#97202)."""
|
|
from agent import turn_context
|
|
|
|
agent = _TitlingAgent("subagent")
|
|
agent._session_db.set_auto_title.return_value = True
|
|
with patch("agent.title_generator.maybe_auto_title") as titler:
|
|
turn_context._maybe_title_session_at_turn_start(
|
|
agent, [{"role": "user", "content": "Audit the billing module\n\nContext: ..."}])
|
|
assert not titler.called
|
|
agent._session_db.set_auto_title.assert_called_once_with(
|
|
"sess-1", "Subagent: Audit the billing module", source="derived")
|
|
|
|
|
|
def test_prologue_leaves_cron_runs_untitled():
|
|
agent = _TitlingAgent("cron")
|
|
from agent import turn_context
|
|
|
|
turn_context._maybe_title_session_at_turn_start(agent, [{"role": "user", "content": "Run the job"}])
|
|
assert not agent._session_db.set_auto_title.called
|
|
|
|
|
|
def test_prologue_forwards_the_submit_title_preview_to_the_titler():
|
|
"""A paste-shrunk ``display_metadata.title_preview`` from prompt.submit is the text the
|
|
titler should read, not the full pasted body."""
|
|
from agent import turn_context
|
|
|
|
with patch("agent.title_generator.maybe_auto_title") as titler:
|
|
turn_context._maybe_title_session_at_turn_start(
|
|
_TitlingAgent("desktop"),
|
|
[{"role": "user", "content": "x" * 5000,
|
|
"display_metadata": {"title_preview": "Pasted 5000 chars"}}],
|
|
)
|
|
assert titler.call_args.kwargs["title_preview"] == "Pasted 5000 chars"
|
|
|
|
|
|
def test_prologue_hands_the_titler_the_agents_writer_handle():
|
|
from agent import turn_context
|
|
|
|
agent = _TitlingAgent("desktop")
|
|
agent.persistence = types.SimpleNamespace(read_only=False)
|
|
with patch("agent.title_generator.maybe_auto_title") as titler:
|
|
turn_context._maybe_title_session_at_turn_start(agent, [{"role": "user", "content": "Fix the login button"}])
|
|
assert titler.call_args.kwargs["persistence"] is agent.persistence
|
|
|
|
|
|
def test_prologue_names_a_subagent_run_through_the_writer_handle():
|
|
from agent import turn_context
|
|
from persistence.conversation.sessions import TitleSource
|
|
|
|
agent = _TitlingAgent("subagent")
|
|
agent.persistence = types.SimpleNamespace(read_only=False, sessions=MagicMock())
|
|
agent.persistence.sessions.set_auto_title.return_value = True
|
|
turn_context._maybe_title_session_at_turn_start(agent, [{"role": "user", "content": "Audit the billing module"}])
|
|
agent.persistence.sessions.set_auto_title.assert_called_once_with(
|
|
"sess-1", "Subagent: Audit the billing module", source=TitleSource.DERIVED)
|
|
assert not agent._session_db.set_auto_title.called
|