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(dispatcher): record a replayed output for silent-final promotion; the record is the last persisted text (#2807)

Call Whitedee80738943d
diff --git a/core/message_dispatcher.py b/core/message_dispatcher.py
index d022bcd2b..81e1d3dc7 100644
--- a/core/message_dispatcher.py
+++ b/core/message_dispatcher.py
@@ -74,6 +74,7 @@ from storage.background import (
TERMINAL_RUN_STATUSES,
normalize_run_status,
)
+from storage.messages_service import canonical_message_timestamp
from vibe.i18n import t as i18n_t
from vibe.message_types import TRANSCRIPT_COPY_KEY
@@ -426,7 +427,7 @@ class ConsolidatedMessageDispatcher:
# token. A Turn that ends on a silent reply makes this row its result
# (``promote_turn_text_to_result``). Every lifecycle-terminal emit takes
# the entry, so it never outlives its Turn.
- self._turn_visible_text: dict[str, tuple[str, str]] = {}
+ self._turn_visible_text: dict[str, tuple[str, str, Optional[tuple[str, str]]]] = {}
# Harness deliveries whose prompt has already been echoed to IM, kept as a
# bounded FIFO (see ``emit_harness_prompt``). Per-process only: a restart
# replay writes a new Delivery anyway, and a duplicated echo is cosmetic
@@ -2786,14 +2787,27 @@ class ConsolidatedMessageDispatcher:
def _remember_turn_text(self, context: MessageContext, row: Optional[dict]) -> None:
"""Record a persisted row as its Turn's latest visible text.
- Only a row whose Turn is identified: without a token, a later Turn's
- silent final could not tell this row apart from its own.
+ The record is the Turn's last *persisted* visible text: promotion edits
+ history, and a text whose row was never written is not in it. A row that
+ sits earlier in the transcript than the one held (a retried earlier
+ output) never displaces it; the order is the transcript's own
+ (``messages_service.transcript_order_value``: when the row entered the
+ transcript, then its id). Only a row whose Turn is identified: without a
+ token, a later Turn's silent final could not tell this row apart from its
+ own.
"""
row_id = (row or {}).get("id")
turn_token = (context.platform_specific or {}).get("turn_token")
- if row_id and turn_token:
- self._turn_visible_text[resolve_turn_sink_key(self.controller, context)] = (turn_token, row_id)
+ if not (row_id and turn_token):
+ return
+ key = resolve_turn_sink_key(self.controller, context)
+ entered = canonical_message_timestamp(row.get("delivered_at") or row.get("created_at"))
+ order = (entered, str(row_id)) if entered else None
+ held = self._turn_visible_text.get(key)
+ if held is not None and held[0] == turn_token and order and held[2] and held[2] > order:
+ return
+ self._turn_visible_text[key] = (turn_token, row_id, order)
def _take_turn_text(self, context: MessageContext) -> Optional[str]:
"""Remove and return the row recorded for this context's Turn, if any."""
@@ -3191,6 +3205,10 @@ class ConsolidatedMessageDispatcher:
break
if accepted_message:
logger.info("Skipping duplicate agent output %s", native_output_id)
+ # A dispatcher that never saw the first emit (a recovery replay) learns
+ # the row here; an older output's retry keeps the newer row recorded.
+ if remembers_turn_text:
+ self._remember_turn_text(context, accepted_message)
accepted_output_semantics = self._output_with_accepted_provenance(
output_semantics,
accepted_message,
diff --git a/tests/scenarios/message_delivery/catalog.yaml b/tests/scenarios/message_delivery/catalog.yaml
index b00942429..2794f9360 100644
--- a/tests/scenarios/message_delivery/catalog.yaml
+++ b/tests/scenarios/message_delivery/catalog.yaml
@@ -403,6 +403,7 @@ scenarios:
- tests/test_message_dispatcher_silent_final.py::SilentFinalTurnResultTests::test_a_silent_final_makes_the_last_visible_text_the_result
- tests/test_message_dispatcher_silent_final.py::SilentFinalTurnResultTests::test_concurrent_turns_in_one_channel_each_promote_their_own_text
- tests/test_message_dispatcher_silent_final.py::SilentFinalTurnResultTests::test_a_cancellation_during_the_promotion_still_commits_the_turn
+ - tests/test_message_dispatcher_silent_final.py::SilentFinalTurnResultTests::test_a_replayed_output_is_recorded_without_displacing_newer_text
- tests/test_message_dispatcher_silent_final.py::SilentFinalTurnResultTests::test_nothing_else_promotes_earlier_text
- tests/test_vibey_agent.py::test_a_silent_final_leaves_the_visible_reply_before_it_as_the_result
- id: MESSAGE-DELIVERY-326
diff --git a/tests/test_message_dispatcher_silent_final.py b/tests/test_message_dispatcher_silent_final.py
index 3020f41f9..0055d65e8 100644
--- a/tests/test_message_dispatcher_silent_final.py
+++ b/tests/test_message_dispatcher_silent_final.py
@@ -255,6 +255,49 @@ class SilentFinalTurnResultTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(self.agent_rows(), [("result", REPORT)])
self.assertEqual(len(controller.committed_turns), 1)
+ async def test_a_replayed_output_is_recorded_without_displacing_newer_text(self):
+ """Scenario: MESSAGE-DELIVERY-325.
+
+ An output replayed through the accepted-receipt short-circuit is recorded
+ by a dispatcher that never saw its first emit, and a retry of an older
+ output leaves the newer row recorded.
+ """
+
+ def phase(key: str) -> MessageOutput:
+ return MessageOutput(completes_turn=False, completes_run=False, idempotency_key=key)
+
+ with self.subTest("a fresh dispatcher replays the output"), self.state():
+ await _Controller("avibe").emit_agent_message(self.context(), "output", REPORT, output=phase("p1"))
+ recovered = _Controller("avibe")
+ await recovered.emit_agent_message(self.context(), "output", REPORT, output=phase("p1"))
+ await _Agent(recovered).emit_result_message(self.context(), "<silent>noted</silent>")
+
+ self.assertEqual(self.agent_rows(), [("result", REPORT)])
+
+ with self.subTest("a local-only output is replayed visibly after a newer one"), self.state():
+ # The replayed row keeps its creation time but enters the transcript
+ # now (``delivered_at``), after the newer output: it is the text shown last.
+ controller = _Controller("avibe")
+ background = self.context()
+ background.platform_specific["suppress_delivery"] = True
+ await controller.emit_agent_message(background, "output", REPORT, output=phase("p1"))
+ await controller.emit_agent_message(self.context(), "output", "Second report.", output=phase("p2"))
+ await controller.emit_agent_message(self.context(), "output", REPORT, output=phase("p1"))
+ await _Agent(controller).emit_result_message(self.context(), "<silent>noted</silent>")
+
+ self.assertEqual(
+ sorted(self.agent_rows()), [("output", "Second report."), ("result", REPORT)]
+ )
+
+ with self.subTest("an older output is retried after a newer one"), self.state():
+ controller = _Controller("avibe")
+ await controller.emit_agent_message(self.context(), "output", REPORT, output=phase("p1"))
+ await controller.emit_agent_message(self.context(), "output", "Second report.", output=phase("p2"))
+ await controller.emit_agent_message(self.context(), "output", REPORT, output=phase("p1"))
+ await _Agent(controller).emit_result_message(self.context(), "<silent>noted</silent>")
+
+ self.assertEqual(self.agent_rows(), [("output", REPORT), ("result", "Second report.")])
+
async def test_nothing_else_promotes_earlier_text(self):
"""Scenario: MESSAGE-DELIVERY-325."""
request = SimpleNamespace(output=None)