Skip to content

The agent that grows with you

refactor(compression): reuse _result_failure_suffix for the write_file refusal stub

kshitijk4poorf6c78be31ebb
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 06e59dfeccb..2aa5c8f2af5 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -1703,8 +1703,8 @@ def _sum_terminal(name, args, content, content_len, line_count):
def _sum_write_file(name, args, content, content_len, line_count):
# A refused write (stale-write guard, sensitive path, I/O error) changed nothing; summarized as
# "wrote to" it compresses into a success the post-compaction agent then reports.
- if error := _json_dict(content).get("error"):
- return f"[write_file] FAILED: {' '.join(str(error).split())[:80]} ({args.get('path', '?')})"
+ if failed := _result_failure_suffix(content):
+ return f"[write_file] {args.get('path', '?')}{failed}"
written_lines = _str_arg(args, "content").count("\n") + 1 if args.get("content") else "?"
return f"[write_file] wrote to {args.get('path', '?')} ({written_lines} lines)"
diff --git a/tests/agent/test_context_compressor.py b/tests/agent/test_context_compressor.py
index a50c17c579d..5d8a9de8c6c 100644
--- a/tests/agent/test_context_compressor.py
+++ b/tests/agent/test_context_compressor.py
@@ -26,17 +26,6 @@ from agent.auxiliary_client import CODEX_STREAM_STALL_MARKER
_REQ = httpx.Request("POST", "http://x")
[email protected]("result, expected", [
- ({"error": "Refusing to overwrite a.md: stale", "stale_write_blocked": True, "path": "a.md"},
- "[write_file] FAILED: Refusing to overwrite a.md: stale (a.md)"),
- ({"bytes_written": 13, "path": "a.md"}, "[write_file] wrote to a.md (2 lines)"),
-])
-def test_write_file_stub_keeps_the_refusal(result, expected):
- """A refused write changed nothing; its stub must not read as a successful write."""
- args = json.dumps({"path": "a.md", "content": "line 1\nline 2"})
- assert _summarize_tool_result("write_file", args, json.dumps(result)) == expected
-
-
class StubProviderError(Exception):
def __init__(self, message, *, status_code=None, response=None):
super().__init__(message)
@@ -265,6 +254,10 @@ class TestSummarizeToolResultOutcome:
("some_mcp_tool", {"a": 1}, {"error": "boom"}, "[some_mcp_tool] a=1 (17 chars result) FAILED: boom"),
("delegate_task", {"goal": "ship it"}, {"error": "Unknown action"},
"[delegate_task] 'ship it' (27 chars result) FAILED: Unknown action"),
+ # The stale-write guard refused: nothing was written, so the stub must not say "wrote to".
+ ("write_file", {"path": "a.md", "content": "line 1\nline 2"},
+ {"error": "Refusing to overwrite a.md: stale", "stale_write_blocked": True},
+ "[write_file] a.md FAILED: Refusing to overwrite a.md: stale"),
])
def test_failed_call_stub_is_marked_failed(self, tool_name, args, payload, expected):
assert self._stub(tool_name, args, payload) == expected
@@ -272,6 +265,8 @@ class TestSummarizeToolResultOutcome:
@pytest.mark.parametrize("tool_name, args, payload, expected", [
("web_search", {"query": "hermes"}, {"results": [{"title": "hit"}]},
"[web_search] query='hermes' (31 chars result)"),
+ ("write_file", {"path": "a.md", "content": "line 1\nline 2"}, {"bytes_written": 13},
+ "[write_file] wrote to a.md (2 lines)"),
("text_to_speech", {}, {"success": True, "path": "/tmp/out.wav"}, "[text_to_speech] generated audio (41 chars)"),
# ``job`` carries stored state from earlier runs; only this call's outcome may mark the stub.
("cronjob_manage", {"action": "poll"},