Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(claude): retire an injected turn's assistant text when its detached result lands (#2348)

Call White7d2a789d75d8
diff --git a/modules/agents/claude_agent.py b/modules/agents/claude_agent.py
index b3e5e1e42..043e38b98 100644
--- a/modules/agents/claude_agent.py
+++ b/modules/agents/claude_agent.py
@@ -2008,6 +2008,11 @@ class ClaudeAgent(BaseAgent):
joined_running_client = getattr(client, "_vibe_receiver_attached", False)
setattr(client, "_vibe_receiver_attached", True)
turn_origin: str | None = "" if joined_running_client else None
+ # Each Result ends one native response phase. The pending human-path
+ # Assistant text belongs to a phase only if this receiver set it there.
+ response_phase = object()
+ ending_phase = None
+ pending_assistant_phase = None
while True:
settling_ambiguous_primary = False
settling_ambiguous_assistant_text = None
@@ -2110,6 +2115,7 @@ class ClaudeAgent(BaseAgent):
terminal_steering_generation = None
if message_type == "result":
turn_origin = None
+ ending_phase, response_phase = response_phase, object()
elif message_type == "assistant" and turn_origin is None:
turn_origin = ""
if message_type == "user":
@@ -2316,6 +2322,7 @@ class ClaudeAgent(BaseAgent):
continue
if assistant_text:
self._last_assistant_text[composite_key] = assistant_text
+ pending_assistant_phase = response_phase
pending_requests = self._pending_requests.get(composite_key) or []
pending_request = pending_requests[0] if pending_requests else None
@@ -2345,6 +2352,7 @@ class ClaudeAgent(BaseAgent):
if text_parts:
formatted_assistant = formatter.format_assistant_message(text_parts)
self._pending_assistant_message[composite_key] = formatted_assistant
+ pending_assistant_phase = response_phase
# AskUserQuestion handling disabled - SDK cannot respond programmatically
# See: https://github.com/anthropics/claude-code/issues/10168
@@ -2444,6 +2452,10 @@ class ClaudeAgent(BaseAgent):
continue
if message_type == "result":
+ replay_baseline = (
+ self._pending_assistant_message.get(composite_key),
+ self._last_assistant_text.get(composite_key),
+ )
if await self._flush_buffered_assistant_messages(
composite_key,
context,
@@ -2458,6 +2470,19 @@ class ClaudeAgent(BaseAgent):
reason="assistant_auth_failure",
)
return
+ # Replayed frames belong to the phase this Result ends.
+ # Each write stores a fresh object, so identity shows one.
+ if any(
+ current is not None and current is not before
+ for current, before in zip(
+ (
+ self._pending_assistant_message.get(composite_key),
+ self._last_assistant_text.get(composite_key),
+ ),
+ replay_baseline,
+ )
+ ):
+ pending_assistant_phase = ending_phase
raw_result_text = getattr(message, "result", None)
result_text = raw_result_text
if output_mode in {"activity", "detached"}:
@@ -2468,10 +2493,23 @@ class ClaudeAgent(BaseAgent):
)
if record is None:
raise RuntimeError("Claude terminal phase has no output owner")
+ phase_text = None
+ if pending_assistant_phase is ending_phase:
+ # This phase streamed Assistant text on the human
+ # path before its Result proved another owner. The
+ # record takes it over, so it never replays as the
+ # next turn's narration or terminal text.
+ self._pending_assistant_message.pop(composite_key, None)
+ phase_text = self._last_assistant_text.pop(composite_key, None)
self._classify_output_record(
record, context,
text=self._select_detached_result_text(
- composite_key, message, raw_result_text, record.text,
+ composite_key,
+ message,
+ raw_result_text,
+ # The phase text stands in only for an empty body.
+ record.text
+ or (None if str(raw_result_text or "").strip() else phase_text),
),
message=message,
)
diff --git a/tests/scenarios/harness_failure_recovery/catalog.yaml b/tests/scenarios/harness_failure_recovery/catalog.yaml
index 19acad5e6..0cb96fd01 100644
--- a/tests/scenarios/harness_failure_recovery/catalog.yaml
+++ b/tests/scenarios/harness_failure_recovery/catalog.yaml
@@ -4399,6 +4399,17 @@ scenarios:
test: tests/test_background_command_caller_context.py::test_hfr_486_watch_child_vault_callback_uses_only_current_binding
# Real argv/shell children with clean/contaminated parents, bound/unbound
# targets, isolated Vault expiry, retargeting, and remote snapshot transport.
+ - id: HFR-488
+ name: an injected Claude turn's reply is never replayed as the next turn's narration
+ status: covered
+ kind: recovery
+ layer: contract
+ backend: claude
+ # A peer or wakeup turn without an Activity streams its Assistant text on the
+ # human path (inline or buffered replay), then its Result proves a detached
+ # owner. The detached record takes that phase's text, even when the Result body
+ # is empty, so it never replays later; text another phase set is untouched.
+ test: tests/test_claude_result_provenance.py::test_injected_turn_result_retires_its_unbuffered_assistant_text
manual_evidence:
- local Incus backend process death after prompt acceptance
- local Incus controller restart with persisted queued successor
diff --git a/tests/test_claude_result_provenance.py b/tests/test_claude_result_provenance.py
index 86f29f762..b3fe8c1cc 100644
--- a/tests/test_claude_result_provenance.py
+++ b/tests/test_claude_result_provenance.py
@@ -2271,6 +2271,43 @@ class ClaudeResultProvenanceTests(unittest.IsolatedAsyncioTestCase):
self.assertTrue(next_token)
service.release_runtime_turn(next_context)
+ async def test_injected_turn_result_retires_its_unbuffered_assistant_text(self):
+ """HFR-488: a peer turn's reply is delivered once, never replayed as narration."""
+
+ reply = "Reply to the peer.\n\n---\n[Check] | [Skip]"
+ # An empty Result body still delivers the phase's Assistant text.
+ for result_body in (reply, ""):
+ with self.subTest(result_body=result_body):
+ key = "session-injected-reply:/tmp/work"
+ agent, service = _build_agent()
+ agent._get_formatter = lambda _context: _ToolcallFormatter()
+ context = _context(key)
+ agent.emit_result_message = _dispatcher_owned_emit(service)
+
+ await agent._receive_messages(
+ _client(
+ [
+ UserMessage("peer message", origin={"kind": "peer"}),
+ AssistantMessage(_block(TextBlock, text=reply)),
+ ResultMessage(result_body, origin={"kind": "peer"}),
+ UserMessage("second peer message", origin={"kind": "peer"}),
+ AssistantMessage(_block(TextBlock, text="second reply")),
+ ResultMessage("second reply", origin={"kind": "peer"}),
+ ]
+ ),
+ "sess-injected-reply",
+ "/tmp/work",
+ context,
+ composite_key=key,
+ )
+
+ self.assertEqual(
+ [call.args[1] for call in agent.emit_result_message.await_args_list],
+ [reply, "second reply"],
+ )
+ self.assertNotIn(("assistant", reply), _visible_emits(agent))
+ self.assertNotIn(key, agent._last_assistant_text)
+
async def test_detached_synthetic_delivery_retries_with_same_output_identity(self):
key = "session-synthetic-detached-retry:/tmp/work"
agent, service = _build_agent()