mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(read-lifecycle): only skip edits that were rejected before they ran
Keep earlier Reads fresh only when the failed Edit/Write tool_result is a <tool_use_error> validation error such as "String to replace not found". Any other error, for example a call interrupted while it was running that "may have partially or fully completed", still counts as an edit. An error tool_result whose tool_use_id is not a string is ignored instead of raising TypeError. Refs #4460 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
9b4aa6356d
commit
6aa14c9d3f
@@ -96,6 +96,16 @@ class ReadLifecycleResult:
|
||||
ccr_hashes: list[str] = field(default_factory=list)
|
||||
|
||||
|
||||
def _rejected_before_running(content: Any) -> bool:
|
||||
"""True when a tool_result's text is a ``<tool_use_error>`` validation error."""
|
||||
if isinstance(content, list):
|
||||
content = next(
|
||||
(b.get("text") for b in content if isinstance(b, dict) and b.get("type") == "text"),
|
||||
None,
|
||||
)
|
||||
return isinstance(content, str) and content.lstrip().startswith("<tool_use_error>")
|
||||
|
||||
|
||||
class ReadLifecycleManager:
|
||||
"""Event-driven Read lifecycle management.
|
||||
|
||||
@@ -267,19 +277,29 @@ class ReadLifecycleManager:
|
||||
|
||||
@staticmethod
|
||||
def _failed_tool_use_ids(messages: list[dict[str, Any]]) -> set[str]:
|
||||
"""Ids of Anthropic tool_use blocks whose tool_result has is_error set."""
|
||||
"""Ids of Anthropic tool_use blocks that were rejected before they ran.
|
||||
|
||||
Only an is_error tool_result whose text is a ``<tool_use_error>`` (the
|
||||
harness's pre-execution validation, e.g. "String to replace not found")
|
||||
proves the file was not touched. Any other error, such as a call that was
|
||||
interrupted and "may have partially or fully completed", still counts as
|
||||
an edit.
|
||||
"""
|
||||
failed: set[str] = set()
|
||||
for msg in messages:
|
||||
content = msg.get("content")
|
||||
if not isinstance(content, list):
|
||||
continue
|
||||
for block in content:
|
||||
if (
|
||||
if not (
|
||||
isinstance(block, dict)
|
||||
and block.get("type") == "tool_result"
|
||||
and block.get("is_error")
|
||||
):
|
||||
failed.add(block.get("tool_use_id", ""))
|
||||
continue
|
||||
tool_use_id = block.get("tool_use_id")
|
||||
if isinstance(tool_use_id, str) and _rejected_before_running(block.get("content")):
|
||||
failed.add(tool_use_id)
|
||||
return failed
|
||||
|
||||
def _find_tool_call_msg_index(
|
||||
|
||||
@@ -142,6 +142,7 @@ def make_anthropic_tool_result(tool_call_id: str, content: str) -> dict:
|
||||
|
||||
LARGE_CONTENT = "x" * 2000 # Well above min_size_bytes
|
||||
SMALL_CONTENT = "tiny" # Below min_size_bytes
|
||||
REJECTED_EDIT = "<tool_use_error>String to replace not found in file.</tool_use_error>"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@@ -440,7 +441,7 @@ class TestAnthropicFormat:
|
||||
"""A tool_result with is_error means the edit did not happen."""
|
||||
mgr = ReadLifecycleManager(ReadLifecycleConfig(enabled=True))
|
||||
|
||||
failed = make_anthropic_tool_result("e1", "old_string not found")
|
||||
failed = make_anthropic_tool_result("e1", REJECTED_EDIT)
|
||||
failed["content"][0]["is_error"] = True
|
||||
messages = [
|
||||
make_anthropic_read("r1", "/src/app.py"),
|
||||
@@ -457,7 +458,7 @@ class TestAnthropicFormat:
|
||||
def test_successful_edit_after_failed_edit_still_makes_read_stale(self):
|
||||
mgr = ReadLifecycleManager(ReadLifecycleConfig(enabled=True))
|
||||
|
||||
failed = make_anthropic_tool_result("e1", "old_string not found")
|
||||
failed = make_anthropic_tool_result("e1", REJECTED_EDIT)
|
||||
failed["content"][0]["is_error"] = True
|
||||
messages = [
|
||||
make_anthropic_read("r1", "/src/app.py"),
|
||||
@@ -471,6 +472,59 @@ class TestAnthropicFormat:
|
||||
result = mgr.apply(messages)
|
||||
assert result.reads_stale == 1
|
||||
|
||||
def test_interrupted_edit_still_makes_read_stale(self):
|
||||
"""An error that is not a pre-run rejection may have changed the file."""
|
||||
mgr = ReadLifecycleManager(ReadLifecycleConfig(enabled=True))
|
||||
|
||||
failed = make_anthropic_tool_result(
|
||||
"e1",
|
||||
"Interrupted: the response that made this tool call was stopped while "
|
||||
"the call was running; it may have partially or fully completed.",
|
||||
)
|
||||
failed["content"][0]["is_error"] = True
|
||||
messages = [
|
||||
make_anthropic_read("r1", "/src/app.py"),
|
||||
make_anthropic_tool_result("r1", LARGE_CONTENT),
|
||||
make_anthropic_edit("e1", "/src/app.py"),
|
||||
failed,
|
||||
]
|
||||
|
||||
result = mgr.apply(messages)
|
||||
assert result.reads_stale == 1
|
||||
|
||||
def test_rejected_edit_with_text_block_content_keeps_read_fresh(self):
|
||||
mgr = ReadLifecycleManager(ReadLifecycleConfig(enabled=True))
|
||||
|
||||
failed = make_anthropic_tool_result("e1", "")
|
||||
failed["content"][0]["content"] = [{"type": "text", "text": REJECTED_EDIT}]
|
||||
failed["content"][0]["is_error"] = True
|
||||
messages = [
|
||||
make_anthropic_read("r1", "/src/app.py"),
|
||||
make_anthropic_tool_result("r1", LARGE_CONTENT),
|
||||
make_anthropic_edit("e1", "/src/app.py"),
|
||||
failed,
|
||||
]
|
||||
|
||||
result = mgr.apply(messages)
|
||||
assert result.reads_stale == 0
|
||||
assert result.messages[1]["content"][0]["content"] == LARGE_CONTENT
|
||||
|
||||
def test_error_result_with_non_string_tool_use_id_is_ignored(self):
|
||||
mgr = ReadLifecycleManager(ReadLifecycleConfig(enabled=True))
|
||||
|
||||
bad = make_anthropic_tool_result("e1", REJECTED_EDIT)
|
||||
bad["content"][0]["tool_use_id"] = ["e1"]
|
||||
bad["content"][0]["is_error"] = True
|
||||
messages = [
|
||||
make_anthropic_read("r1", "/src/app.py"),
|
||||
make_anthropic_tool_result("r1", LARGE_CONTENT),
|
||||
bad,
|
||||
]
|
||||
|
||||
result = mgr.apply(messages)
|
||||
assert result.reads_fresh == 1
|
||||
assert result.messages[1]["content"][0]["content"] == LARGE_CONTENT
|
||||
|
||||
|
||||
class TestCCRStoreIntegration:
|
||||
"""Lifecycle manager stores originals in CCR."""
|
||||
|
||||
Reference in New Issue
Block a user