Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(agent): fall back to truncation when an oversized result cannot be encoded

Haozhe Wua5bba6a4d52f
diff --git a/agent/src/agent/result_artifacts.py b/agent/src/agent/result_artifacts.py
index 2376ba31c..5e4f58cbe 100644
--- a/agent/src/agent/result_artifacts.py
+++ b/agent/src/agent/result_artifacts.py
@@ -92,11 +92,11 @@ def deliver_tool_result(result: str, run_dir: str | Path | None, registry) -> st
except (ValueError, TypeError):
payload = None
is_json = False
- root = Path(run_dir).resolve()
- digest = hashlib.sha256(result.encode()).hexdigest()
- folder = root / 'tool-results' / digest
- path = folder / ('result.json' if is_json else 'result.txt')
try:
+ root = Path(run_dir).resolve()
+ digest = hashlib.sha256(result.encode()).hexdigest()
+ folder = root / 'tool-results' / digest
+ path = folder / ('result.json' if is_json else 'result.txt')
_write_original(path, result, root)
parts = save_result_parts(result, folder / 'parts', root)
part_count = len(parts)
diff --git a/agent/tests/test_result_artifacts.py b/agent/tests/test_result_artifacts.py
index 3ff2f41ff..5eb5e59aa 100644
--- a/agent/tests/test_result_artifacts.py
+++ b/agent/tests/test_result_artifacts.py
@@ -89,6 +89,15 @@ def test_unavailable_artifact_is_not_claimed(tmp_path, monkeypatch, failure):
assert Path(receipt['_result_file']['path']).read_text() == 'changed by another writer'
+def test_unencodable_result_falls_back_to_truncation(tmp_path):
+ # An upstream payload that cut an emoji in half keeps a lone surrogate
+ # through json.dumps(ensure_ascii=False); hashing it must not escape.
+ raw = large_result()[:-1] + ', "title": "\ud83d"}'
+ result = deliver_tool_result(raw, tmp_path, registry_with_reader())
+ assert 'TRUNCATED:' in result
+ assert '_result_file' not in result
+
+
class _LargeResult(BaseTool):
name = 'large_result_probe'
description = 'Test large observation delivery.'