Skip to content

The agent that grows with you

fix(gateway): verbose args holding ``` stay plain JSON instead of a code block

Teknium859d6e5e7fdf
diff --git a/agent/display.py b/agent/display.py
index 620e7b62cbe..b509bb49642 100644
--- a/agent/display.py
+++ b/agent/display.py
@@ -128,22 +128,21 @@ def get_tool_preview_max_len() -> int:
_JSON_ESCAPE_RE = re.compile(r"\\(.)")
-_FENCE_LINE_RE = re.compile(r"(?m)^[ \t]*```")
def verbose_tool_args_text(args: dict, max_len: int, code_block: bool) -> str:
"""Verbose tool-progress args: compact JSON (capped at ``max_len``; 0 = no cap). On markdown
platforms it goes in a code block so the formatter keeps it verbatim (``**x**``/``a_b`` are
- otherwise rendered as markup), with JSON's ``\\n``/``\\t`` shown as real line breaks unless a
- decoded line would start with ``` and close the block early."""
+ otherwise rendered as markup), with JSON's ``\\n``/``\\t`` shown as real line breaks. Args
+ holding ``` stay plain: Slack and Discord end a code block at any ``` inside it."""
import json
text = json.dumps(args, ensure_ascii=False, default=str)
if max_len > 0 and len(text) > max_len:
text = text[:max_len - 3] + "..."
- if not code_block:
+ if not code_block or "```" in text:
return text
decoded = _JSON_ESCAPE_RE.sub(lambda m: {"n": "\n", "t": "\t"}.get(m.group(1), m.group(0)), text)
- return f"```\n{text if _FENCE_LINE_RE.search(decoded) else decoded}\n```"
+ return f"```\n{decoded}\n```"
def set_friendly_tool_labels(enabled: bool) -> None:
diff --git a/tests/gateway/test_run_progress_verbose_args.py b/tests/gateway/test_run_progress_verbose_args.py
index a958a892f03..c229deed51a 100644
--- a/tests/gateway/test_run_progress_verbose_args.py
+++ b/tests/gateway/test_run_progress_verbose_args.py
@@ -49,11 +49,11 @@ async def test_verbose_args_render_in_a_code_block_on_markdown_platforms(monkeyp
assert '{"code": "x = a_b\\nprint(**kw)"}' in plain_text and "```" not in plain_text, plain_text
-def test_streamed_verbose_args_share_the_code_block_and_keep_nested_fences_inside():
- """format_tool_event (streamed tool chrome) renders the same block, and args holding a
- line-leading ``` stay JSON-escaped so they cannot close the block early."""
+def test_streamed_verbose_args_share_the_code_block_and_skip_it_for_embedded_fences():
+ """format_tool_event (streamed tool chrome) renders the same block; args holding ``` anywhere
+ stay plain JSON, since Slack/Discord would end the block there and render the rest as markup."""
adapter = object.__new__(_CodeBlockAdapter)
out = adapter.format_tool_event(ToolCallChunk("execute_code", args={"code": "a\nb"}), mode="verbose")
assert out.endswith('```\n{"code": "a\nb"}\n```'), out
- nested = adapter.format_tool_event(ToolCallChunk("execute_code", args={"code": "a\n```\nb"}), mode="verbose")
- assert nested.endswith('```\n{"code": "a\\n```\\nb"}\n```'), nested
+ nested = adapter.format_tool_event(ToolCallChunk("execute_code", args={"code": "see ```**k**``` x"}), mode="verbose")
+ assert nested.endswith('\n{"code": "see ```**k**``` x"}'), nested