Skip to content

The agent that grows with you

fix: /steer, /branch and compaction accounting keep reasoning intact

Tekniumdf3c21400d59
diff --git a/agent/anthropic_message_convert.py b/agent/anthropic_message_convert.py
index 8a0526ab096..6232f276b5b 100644
--- a/agent/anthropic_message_convert.py
+++ b/agent/anthropic_message_convert.py
@@ -571,14 +571,15 @@ def _keep_valid_thinking(content: list[Any], signature_dead: bool) -> list[Any]:
def _current_tool_loop_start(result: list[dict[str, Any]]) -> int:
- """Index after the last user message that is not purely tool results: every assistant turn from
- there on belongs to the in-flight tool loop, whose thinking Anthropic requires back."""
+ """Index after the last user message carrying no tool result: every assistant turn from there on belongs
+ to the in-flight tool loop, whose thinking Anthropic requires back. A steer merged into a tool_result
+ turn still continues the loop."""
for i in range(len(result) - 1, -1, -1):
m = result[i]
if m.get("role") != "user":
continue
content = m.get("content")
- if not (isinstance(content, list) and content and all(_block_type(b) == "tool_result" for b in content)):
+ if not (isinstance(content, list) and any(_block_type(b) == "tool_result" for b in content)):
return i + 1
return 0
diff --git a/agent/message_metadata.py b/agent/message_metadata.py
index ba7be2ed306..5a1669d594f 100644
--- a/agent/message_metadata.py
+++ b/agent/message_metadata.py
@@ -50,6 +50,12 @@ PERSISTENCE_ONLY_MESSAGE_FIELDS = frozenset(
) | REPAIR_BOOKKEEPING_FIELDS
+# Replay carriers the preflight estimate prices at 0, exactly as the compaction tail walk does (opaque signatures,
+# duplicate reasoning copies, ordered sidecars); a mismatch between the two estimates loops compaction.
+UNPRICED_REPLAY_FIELDS = frozenset({"_anthropic_content_blocks", "reasoning_details", "extra_content",
+ "reasoning_opaque", "reasoning_text", "bedrock_content_blocks"})
+
+
def without_persistence_fields(msg: Mapping[str, Any]) -> Mapping[str, Any]:
"""*msg* itself when it carries no persistence-only field, else a shallow copy without them."""
if PERSISTENCE_ONLY_MESSAGE_FIELDS.isdisjoint(msg):
diff --git a/agent/model_metadata.py b/agent/model_metadata.py
index ee240d805af..cfaf7ed0c85 100644
--- a/agent/model_metadata.py
+++ b/agent/model_metadata.py
@@ -22,7 +22,7 @@ from agent import model_metadata_http
from utils import atomic_json_write, atomic_yaml_write, base_url_host_matches, base_url_hostname
from hermes_constants import OPENROUTER_MODELS_URL, openrouter_variant_base
-from agent.message_metadata import PERSISTENCE_ONLY_MESSAGE_FIELDS, without_persistence_fields
+from agent.message_metadata import PERSISTENCE_ONLY_MESSAGE_FIELDS, UNPRICED_REPLAY_FIELDS, without_persistence_fields
logger = logging.getLogger(__name__)
@@ -2572,7 +2572,7 @@ def _wire_message_shadow(msg: dict[str, Any]) -> dict[str, Any]:
drop_reasoning_dup = isinstance(_rc, str) and bool(_rc.strip())
shadow: dict[str, Any] = {}
for k, v in msg.items():
- if k in ("_anthropic_content_blocks", "reasoning_details") or k in PERSISTENCE_ONLY_MESSAGE_FIELDS or (k == "reasoning" and drop_reasoning_dup):
+ if k in UNPRICED_REPLAY_FIELDS or k in PERSISTENCE_ONLY_MESSAGE_FIELDS or (k == "reasoning" and drop_reasoning_dup):
continue
if k == "api_content":
if sidecar_wins:
diff --git a/hermes_cli/cli_commands_mixin.py b/hermes_cli/cli_commands_mixin.py
index d90cd17b420..ac050556fdc 100644
--- a/hermes_cli/cli_commands_mixin.py
+++ b/hermes_cli/cli_commands_mixin.py
@@ -126,7 +126,7 @@ _WORKTREE_SUBCOMMANDS = {
**dict.fromkeys(("new", "add", "create"), "_worktree_new")}
# Message fields copied verbatim onto a /branch row (plus role / tool_name / api_content).
-_BRANCH_COPY_KEYS = ("content", "tool_calls", "tool_call_id", "reasoning", "reasoning_details",
+_BRANCH_COPY_KEYS = ("content", "tool_calls", "tool_call_id", "reasoning", "reasoning_content", "reasoning_details",
"codex_reasoning_items", "codex_message_items", "timestamp")
# /hatch progress event -> catalog key (``cli.commands.hatch.*``).
diff --git a/tests/agent/test_anthropic_signed_platform_replay.py b/tests/agent/test_anthropic_signed_platform_replay.py
index f00de76076b..85a9eb3beef 100644
--- a/tests/agent/test_anthropic_signed_platform_replay.py
+++ b/tests/agent/test_anthropic_signed_platform_replay.py
@@ -96,3 +96,13 @@ def test_signed_thinking_replays_exactly_where_the_route_can_verify_it(case):
if signed_route:
assert stale_thinking_reaches_wire("anthropic_messages", "custom", model, base_url) is ("sig_prior" in expected)
assert history == before
+
+
+def test_a_steer_merged_into_the_tool_result_turn_keeps_the_loop_thinking():
+ """/steer text lands in the tool_result user turn; it must not end the in-flight loop for
+ last-turn-only models (main kept the newest assistant's thinking there)."""
+ history = _history()
+ history.append({"role": "user", "content": "steer: also check b"})
+ _system, wire = AnthropicTransport().convert_messages(history, base_url=None, model="claude-sonnet-4-5")
+ sigs = [b.get("signature") for m in wire if m["role"] == "assistant" for b in m["content"] if b.get("type") == "thinking"]
+ assert sigs == LOOP