mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-17 03:12:19 +03:00
c6436783d1
The fail-closed raw-text scan (unparsed snippets, and the whole-pass fallback when the AST
walk gives up) ended a single-quoted literal only at "\n". With bare-\r line endings an
apostrophe in a comment (`# it's here`) opened a "literal" that ran across lines, the
real literals fell out of step and the typed value stayed visible:
`fill_input('#p',\r # it's here\r 'pw'))\r` showed `'pw'`.
- _literal_end breaks at \r as well as \n (the parser's line terminators).
- _call_end skips `#` comments, so a bracket or quote in one never ends a typing-call
region early.
- _literal_regions masks quoted text in comments too (fail closed), lets a lone quote
in a comment end with it, and masks the rest of the region when an unterminated
literal outside a comment leaves the scan out of step.
Rows for the unparsed and walk-fallback shapes added to
test_browser_exec_typed_password_never_reaches_display (2 red on 8dd38f4f). A
24k-snippet fuzz (clean and malformed) shows 0 snippets leaking that head masked.
393 lines
14 KiB
Python
393 lines
14 KiB
Python
"""Tests for agent/display.py — build_tool_preview() and inline diff previews."""
|
|
|
|
import pytest
|
|
from unittest.mock import MagicMock
|
|
|
|
import agent.display as display_module
|
|
from agent.display import (
|
|
build_tool_preview,
|
|
capture_local_edit_snapshot,
|
|
extract_edit_diff,
|
|
get_cute_tool_message,
|
|
prepare_tool_preview,
|
|
redact_tool_args_for_display,
|
|
set_tool_preview_max_len,
|
|
_summarize_rendered_diff_sections,
|
|
render_edit_diff_with_delta,
|
|
)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def reset_tool_preview_max_len():
|
|
set_tool_preview_max_len(0)
|
|
yield
|
|
set_tool_preview_max_len(0)
|
|
|
|
|
|
def test_cute_tool_message_falls_back_when_renderer_raises(monkeypatch):
|
|
def _boom(*_args, **_kwargs):
|
|
raise RuntimeError("cosmetic failure")
|
|
|
|
monkeypatch.setattr(display_module, "_get_cute_tool_message", _boom)
|
|
|
|
result = get_cute_tool_message("web_extract", {"urls": []}, 0.25)
|
|
assert isinstance(result, str) and result
|
|
|
|
|
|
class TestBuildToolPreview:
|
|
"""Tests for build_tool_preview defensive handling and normal operation."""
|
|
|
|
def test_none_args_returns_none(self):
|
|
"""PR #453: None args should not crash, should return None."""
|
|
assert build_tool_preview("terminal", None) is None
|
|
|
|
def test_empty_dict_returns_none(self):
|
|
"""Empty dict has no keys to preview."""
|
|
assert build_tool_preview("terminal", {}) is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_memory_patch_preview_shows_the_span_being_edited(self):
|
|
args = {"action": "patch", "target": "memory", "old_text": "port 8080", "content": "port 9090"}
|
|
assert build_tool_preview("memory", args) == '~memory: "port 8080"'
|
|
assert '~memory: "port 8080"' in get_cute_tool_message("memory", args, 0.1)
|
|
|
|
def test_memory_operations_batch_previews_its_first_op_and_the_rest_count(self):
|
|
# operations[] is the memory tool's only advertised shape, so progress lines read it.
|
|
one = {"target": "user", "operations": [{"action": "add", "content": "Name: Alice"}]}
|
|
assert build_tool_preview("memory", one) == '+user: "Name: Alice"'
|
|
assert '+user: "Name: Alice"' in get_cute_tool_message("memory", one, 0.1)
|
|
three = {"target": "memory", "operations": [
|
|
{"action": "remove", "old_text": "port 8080"}, {"action": "add", "content": "a"},
|
|
{"action": "patch", "old_text": "b", "content": "c"}]}
|
|
assert build_tool_preview("memory", three) == '-memory: "port 8080" (2 more)'
|
|
assert '-memory: "port 8080" (2 more)' in get_cute_tool_message("memory", three, 0.1)
|
|
|
|
def test_browser_type_preview_redacts_api_key(self):
|
|
secret = "sk-proj-ABCD1234567890EFGH"
|
|
result = build_tool_preview("browser_type", {"ref": "@e3", "text": secret})
|
|
assert result is not None
|
|
assert secret not in result
|
|
assert "sk-pro" in result and "..." in result
|
|
|
|
def test_browser_type_preview_keeps_normal_text(self):
|
|
text = "hello world search query"
|
|
result = build_tool_preview("browser_type", {"ref": "@e3", "text": text})
|
|
assert result is not None
|
|
assert text in result
|
|
|
|
@pytest.mark.parametrize("code", [
|
|
"fill_input('input[type=password]', 'Hunter2Correct!')",
|
|
"js(\"document.querySelector('#pwd').value='Hunter2Correct!'\")",
|
|
"cdp('Input.insertText', text='Hunter2Correct!')",
|
|
"fill_input('#p', 'abc\\'Hunter2Correct!')",
|
|
"fill_input('#p', \"\"\"Hunter2Correct!\"\"\")",
|
|
"fill_input('#p', 'Hunter2Correct!",
|
|
"type_text('Hunter2Correct!')",
|
|
"fill_input('input[type=password]:not(.x)', 'Hunter2Correct!')",
|
|
"fill_input('#a, #pass', 'Hunter2Correct!')",
|
|
"fill_input('#p', f'Hunter2Correct!')",
|
|
"fill_input('#p', r'Hunter2Correct!')",
|
|
"pw = 'Hunter2Correct!'\nfill_input('#p', pw)",
|
|
"params = {'text': 'Hunter2Correct!'}\ncdp('Input.insertText', **params)",
|
|
"params = {\"text\": \"Hunter2Correct!\"}\ncdp('Input.insertText', params)",
|
|
"params = {'text': 'Hunter2Correct!'}\ncdp('Input.insertText', {**params})",
|
|
"params = {'text': 'Hunter2Correct!'}\nparams = {**params}\ncdp('Input.insertText', **params)",
|
|
"fill_input('#p', ('Hunter2Correct!'))",
|
|
"fill_input('#p', '' 'Hunter2Correct!')",
|
|
"fill_input(text='Hunter2Correct!', selector='#p')",
|
|
"fill_input('#p', f'{1}Hunter2Correct!')",
|
|
"pw = ('Hunter2Correct!')\nfill_input('#p', pw)",
|
|
"fill_input('#p', ('Hunter2Correct!'",
|
|
# the parser ends a line at \r and \r\n as well as \n: spans must count the same lines
|
|
"x = 1\rfill_input('#p', 'Hunter2Correct!')",
|
|
"x = 1\r\nfill_input('#p', 'Hunter2Correct!')",
|
|
"x = 1\rfill_input('#p', 'Hunter2Correct!')\nprint('hello world, a longer line here')",
|
|
"s = '''a\rb'''\r\nfill_input('#p', 'Hunter2Correct!')\rprint('kept')\n",
|
|
# names bound by unpacking, loops, splats and calls to a function the snippet defines
|
|
"sel, pw = '#p', 'Hunter2Correct!'\nfill_input(sel, pw)",
|
|
"fill_input(*('#p', 'Hunter2Correct!'))",
|
|
"fill_input(**{'selector': '#p', 'text': 'Hunter2Correct!'})",
|
|
"for pw in ['Hunter2Correct!']:\n fill_input('#p', pw)",
|
|
"def go(pw):\n fill_input('#p', pw)\ngo('Hunter2Correct!')",
|
|
# the raw-text fallback (unparsed: extra ')') ends a literal at \r and skips comments, so the
|
|
# quote in "# it's" cannot swallow the typed value
|
|
"fill_input('#p',\r # it's here\r 'Hunter2Correct!'))\r",
|
|
# a long concatenation makes the walk give up: the whole-pass fallback must hold too
|
|
pytest.param("# don't echo the password\rcss = " + " + ".join(f"'.c{i}{{}}'" for i in range(900))
|
|
+ "\rfill_input('#password', 'Hunter2Correct!')\r", id="cr-comment-quote-walk-fallback"),
|
|
])
|
|
def test_browser_exec_typed_password_never_reaches_display(self, code):
|
|
"""#72298: an ordinary password typed by a browser_exec snippet matches no secret shape,
|
|
yet every display surface (preview, verbose args, cute row) must mask it; the selector stays."""
|
|
args = {"code": code}
|
|
shown = (build_tool_preview("browser_exec", args, max_len=0),
|
|
str(redact_tool_args_for_display("browser_exec", args)),
|
|
get_cute_tool_message("browser_exec", args, 0.1))
|
|
assert all("Hunter2Correct!" not in s and "***" in s for s in shown)
|
|
assert args["code"] == code
|
|
|
|
def test_browser_exec_display_keeps_selectors_urls_and_untyped_strings(self):
|
|
"""Only typed values are masked: a selector, a URL, a text= kwarg outside insertText and a
|
|
.value comparison all read as written."""
|
|
code = ("new_tab(url='https://example.com/login', text='Search page')\n"
|
|
"fill_input('input[type=password]:not(.x)', 'Hunter2Correct!')\n"
|
|
"cdp('Input.insertText', text='Hunter2Correct!')\n"
|
|
"js(\"document.querySelector('#u').value == 'admin'\")")
|
|
shown = redact_tool_args_for_display("browser_exec", {"code": code})["code"]
|
|
assert "Hunter2Correct!" not in shown
|
|
for kept in ("https://example.com/login", "text='Search page'",
|
|
"input[type=password]:not(.x)", ".value == 'admin'"):
|
|
assert kept in shown
|
|
|
|
def test_browser_type_display_args_redact_api_key(self):
|
|
secret = "ghp_ABCDEFGHIJ1234567890"
|
|
safe_args = redact_tool_args_for_display(
|
|
"browser_type", {"ref": "@e3", "text": secret}
|
|
)
|
|
assert secret not in str(safe_args)
|
|
assert safe_args["ref"] == "@e3"
|
|
assert safe_args["text"].startswith("ghp_AB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_delegate_task_batch_preview_respects_max_len(self):
|
|
result = build_tool_preview(
|
|
"delegate_task",
|
|
{"tasks": [{"goal": "A" * 80}, {"goal": "B" * 80}]},
|
|
max_len=30,
|
|
)
|
|
assert result is not None and len(result) <= 30
|
|
|
|
def test_false_like_args_zero(self):
|
|
"""Non-dict falsy values should return None, not crash."""
|
|
assert build_tool_preview("terminal", 0) is None
|
|
assert build_tool_preview("terminal", "") is None
|
|
assert build_tool_preview("terminal", []) is None
|
|
|
|
@pytest.mark.parametrize("max_len", [1, 2, 3, 4])
|
|
def test_tiny_max_len_never_exceeded(self, max_len):
|
|
"""max_len is a hard cap on every preview path — dedicated builder (terminal), generic
|
|
fallback key (web_search), and the cute head-truncated path (#9439)."""
|
|
from agent.display import _cute_path, set_tool_preview_max_len
|
|
long = "abcdefghijklmnopqrstuvwxyz"
|
|
for tool, args in (("terminal", {"command": long}), ("web_search", {"query": long})):
|
|
preview = build_tool_preview(tool, args, max_len=max_len)
|
|
assert preview and len(preview) <= max_len, (tool, preview)
|
|
set_tool_preview_max_len(max_len)
|
|
try:
|
|
assert len(_cute_path("/" + long + "/file.py")) <= max_len
|
|
finally:
|
|
set_tool_preview_max_len(0)
|
|
|
|
|
|
class TestPrepareToolPreview:
|
|
def test_recovers_and_describes_truncated_url(self):
|
|
url = "https://example.com/a/very/long/path/to/a/page"
|
|
set_tool_preview_max_len(20)
|
|
|
|
preview = prepare_tool_preview(
|
|
"web_extract",
|
|
{"urls": [url]},
|
|
fallback=url[:17] + "...",
|
|
max_len=20,
|
|
)
|
|
|
|
assert preview.text == url[:17] + "..."
|
|
assert preview.truncated is True
|
|
assert preview.url == url
|
|
|
|
def test_untruncated_url_has_no_link_target(self):
|
|
url = "https://example.com/page"
|
|
preview = prepare_tool_preview(
|
|
"browser_navigate", None, fallback=url, max_len=40
|
|
)
|
|
|
|
assert preview.text == url
|
|
assert preview.truncated is False
|
|
assert preview.url is None
|
|
|
|
def test_truncated_non_url_has_no_link_target(self):
|
|
preview = prepare_tool_preview(
|
|
"web_search",
|
|
{"query": "how to parse a URL"},
|
|
fallback="how to parse a URL",
|
|
max_len=12,
|
|
)
|
|
|
|
assert preview.truncated is True
|
|
assert preview.url is None
|
|
|
|
|
|
class TestCuteToolMessagePreviewLength:
|
|
|
|
|
|
def test_search_files_preview_uses_positive_configured_limit_not_default(self):
|
|
set_tool_preview_max_len(80)
|
|
pattern = "function.formatToolCall.context.preview.compactPreview.maxLength.truncate"
|
|
|
|
line = get_cute_tool_message("search_files", {"pattern": pattern}, 0.1)
|
|
|
|
assert pattern in line
|
|
assert "..." not in line
|
|
|
|
|
|
|
|
|
|
|
|
def test_browser_type_cute_message_redacts_api_key(self):
|
|
secret = "sk-proj-ABCD1234567890EFGH"
|
|
line = get_cute_tool_message(
|
|
"browser_type",
|
|
{"ref": "@password", "text": secret},
|
|
0.1,
|
|
result='{"success": true, "typed": "sk-pro...EFGH"}',
|
|
)
|
|
|
|
assert secret not in line
|
|
assert "sk-pro" in line
|
|
|
|
def test_browser_type_cute_message_keeps_normal_text(self):
|
|
text = "hello world"
|
|
line = get_cute_tool_message(
|
|
"browser_type",
|
|
{"ref": "@search", "text": text},
|
|
0.1,
|
|
result='{"success": true, "typed": "hello world"}',
|
|
)
|
|
|
|
assert text in line
|
|
|
|
|
|
class TestEditDiffPreview:
|
|
|
|
|
|
|
|
def test_extract_edit_diff_uses_local_snapshot_for_write_file(self, tmp_path):
|
|
target = tmp_path / "note.txt"
|
|
target.write_text("old\n", encoding="utf-8")
|
|
|
|
snapshot = capture_local_edit_snapshot("write_file", {"path": str(target)})
|
|
|
|
target.write_text("new\n", encoding="utf-8")
|
|
|
|
diff = extract_edit_diff(
|
|
"write_file",
|
|
'{"bytes_written": 4}',
|
|
function_args={"path": str(target)},
|
|
snapshot=snapshot,
|
|
)
|
|
|
|
assert diff is not None
|
|
assert "--- a/" in diff
|
|
assert "+++ b/" in diff
|
|
assert "-old" in diff
|
|
assert "+new" in diff
|
|
|
|
|
|
|
|
def test_render_edit_diff_with_delta_handles_renderer_errors(self, monkeypatch):
|
|
printer = MagicMock()
|
|
|
|
monkeypatch.setattr("agent.display._summarize_rendered_diff_sections", MagicMock(side_effect=RuntimeError("boom")))
|
|
|
|
rendered = render_edit_diff_with_delta(
|
|
"patch",
|
|
'{"diff": "--- a/x\\n+++ b/x\\n"}',
|
|
print_fn=printer,
|
|
)
|
|
|
|
assert rendered is False
|
|
assert printer.call_count == 0
|
|
|
|
|
|
def test_summarize_rendered_diff_sections_limits_file_count(self):
|
|
diff = "".join(
|
|
f"--- a/file{i}.py\n+++ b/file{i}.py\n+line{i}\n"
|
|
for i in range(8)
|
|
)
|
|
|
|
rendered = _summarize_rendered_diff_sections(diff, max_files=3, max_lines=50)
|
|
|
|
assert any("a/file0.py" in line for line in rendered)
|
|
assert any("a/file1.py" in line for line in rendered)
|
|
assert any("a/file2.py" in line for line in rendered)
|
|
assert not any("a/file7.py" in line for line in rendered)
|
|
assert "additional file" in rendered[-1]
|
|
|
|
|
|
class TestBuildToolLabel:
|
|
"""Friendly human-phrased tool labels for built-in tools."""
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _enable_friendly(self):
|
|
from agent.display import set_friendly_tool_labels
|
|
set_friendly_tool_labels(True)
|
|
yield
|
|
set_friendly_tool_labels(True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_disabled_falls_back_to_preview(self):
|
|
from agent.display import (
|
|
build_tool_label,
|
|
build_tool_preview,
|
|
set_friendly_tool_labels,
|
|
)
|
|
set_friendly_tool_labels(False)
|
|
args = {"query": "weather in NYC"}
|
|
label = build_tool_label("web_search", args)
|
|
# With the feature off, must match the raw preview exactly
|
|
assert label == build_tool_preview("web_search", args)
|
|
assert "Searching the web" not in (label or "")
|
|
|
|
|
|
|
|
class TestBuildStatusPhrase:
|
|
"""build_status_phrase — live working-state text for Slack's status line."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_caps_length_for_slack_status_line(self):
|
|
from agent.display import build_status_phrase
|
|
phrase = build_status_phrase(
|
|
"terminal", {"command": "x" * 300}, max_len=49
|
|
)
|
|
assert phrase is not None and len(phrase) <= 49
|
|
assert phrase.endswith("…")
|
|
|
|
|
|
def test_respects_friendly_labels_toggle(self):
|
|
from agent.display import build_status_phrase, set_friendly_tool_labels
|
|
set_friendly_tool_labels(False)
|
|
try:
|
|
assert build_status_phrase("terminal", {"command": "ls"}) is None
|
|
finally:
|
|
set_friendly_tool_labels(True)
|