From 1026450af7a9e917ca199d681aa559819f2124e5 Mon Sep 17 00:00:00 2001 From: bala Date: Tue, 30 Dec 2025 17:18:38 +0000 Subject: [PATCH 1/2] escape string from anthropic-text-editor --- src/microbots/MicroBot.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/microbots/MicroBot.py b/src/microbots/MicroBot.py index 36e21e3..c4b7744 100644 --- a/src/microbots/MicroBot.py +++ b/src/microbots/MicroBot.py @@ -1,5 +1,4 @@ import json -from pprint import pformat import re import time from dataclasses import dataclass @@ -242,7 +241,7 @@ def run( try: output_json = json.loads(llm_command_output.stdout) if "content" in output_json: - output_text = pformat(output_json["content"]) + output_text = output_json["content"].encode("utf-8").decode("unicode_escape") except json.JSONDecodeError: pass else: From 2f6201f6858b4242c425efb95fce06effbf88d8b Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon, 5 Jan 2026 14:15:27 +0530 Subject: [PATCH 2/2] Add test coverage for unicode_escape decoding in anthropic-text-editor output handling (#95) * Initial plan * Add test case for unicode_escape handling in anthropic-text-editor output Co-authored-by: 0xba1a <2942888+0xba1a@users.noreply.github.com> * Address code review feedback: move json import to top and improve assertion order Co-authored-by: 0xba1a <2942888+0xba1a@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: 0xba1a <2942888+0xba1a@users.noreply.github.com> --- test/bot/test_microbot.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/test/bot/test_microbot.py b/test/bot/test_microbot.py index 5cbfbb0..403c52e 100644 --- a/test/bot/test_microbot.py +++ b/test/bot/test_microbot.py @@ -4,6 +4,7 @@ This test will create multiple custom bots - a reading bot, a writing bot using the base class. """ +import json import os from pathlib import Path import subprocess @@ -526,7 +527,6 @@ def test_pformat_produces_readable_output(self): command = """echo '{"input": {"command": "view", "path": "/tmp/test.txt"}}' | anthropic-text-editor""" # With json.dumps (old behavior) - escapes quotes making it harder to read - import json json_output = json.dumps(command) # With pformat (new behavior) - more readable @@ -544,6 +544,42 @@ def test_pformat_produces_readable_output(self): raw_output = command assert r'\"' not in raw_output, "Raw command should not have escaped quotes" assert '"input"' in raw_output, "Raw command should preserve double quotes" + + def test_anthropic_text_editor_unicode_escape_decoding(self): + """Test that unicode-escaped output is properly decoded. + + This unit test verifies that the unicode_escape decoding logic introduced + for anthropic-text-editor output works correctly. When the tool returns + JSON with a "content" field containing unicode-escaped strings (e.g., \\n, \\t), + the content should be decoded to actual newlines and tabs. + """ + # Simulate anthropic-text-editor returning JSON with unicode-escaped content + escaped_output = '{"content": "Line 1\\nLine 2\\nLine 3\\tTabbed"}' + + # Parse the JSON (simulating json.loads in the actual code) + output_json = json.loads(escaped_output) + + # Apply the unicode_escape decoding (the line being tested) + decoded_output = output_json["content"].encode("utf-8").decode("unicode_escape") + + # Verify the decoded output has actual newlines and tabs + expected_output = "Line 1\nLine 2\nLine 3\tTabbed" + assert decoded_output == expected_output, \ + "Output should contain decoded newlines and tabs" + + # Verify the escaped versions are NOT in the decoded output + assert "\\n" not in decoded_output, \ + "Decoded output should not contain escaped newlines" + assert "\\t" not in decoded_output, \ + "Decoded output should not contain escaped tabs" + + # Verify line count - should be 3 lines (split by actual newlines) + lines = decoded_output.split("\n") + assert len(lines) == 3, "Should have 3 lines after decoding" + assert lines[0] == "Line 1" + assert lines[1] == "Line 2" + assert lines[2] == "Line 3\tTabbed" # This line contains an actual tab + def test_tool_usage_instructions_appended_to_system_prompt(self): """Test that tool usage instructions are appended to the system prompt when creating LLM.""" from microbots.tools.tool import Tool