refactor(agent): tighten comments, extract constant, strengthen edge case test

- Extract synthetic user message string to module-level constant
- Tighten comments in _snip_history recovery branch
- Strengthen no-user edge case test to verify safety net interaction
This commit is contained in:
chengyongru
2026-04-17 16:20:53 +08:00
committed by Xubin Ren
parent 44b526c4ee
commit 8c0c4e5b31
4 changed files with 20 additions and 7 deletions
+9
View File
@@ -2909,3 +2909,12 @@ def test_snip_history_no_user_at_all_falls_back_gracefully(monkeypatch):
assert isinstance(trimmed, list)
# Must have at least system.
assert any(m.get("role") == "system" for m in trimmed)
# The _enforce_role_alternation safety net must be able to fix whatever
# _snip_history returns here — verify it produces a valid sequence.
from nanobot.providers.base import LLMProvider
fixed = LLMProvider._enforce_role_alternation(trimmed)
non_system = [m for m in fixed if m["role"] != "system"]
if non_system:
assert non_system[0]["role"] in ("user", "tool"), (
f"Safety net should ensure first non-system is user/tool, got {non_system[0]['role']}"
)