Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(bridges): don't strand a runtime indicator when a turn ends mid-move (CHOO-1104)

Louis Amaudruzed1cb3fd9369
diff --git a/core/switch_core/bridges/collaboration/adapter.py b/core/switch_core/bridges/collaboration/adapter.py
index 121c8cec3..e48d00345 100644
--- a/core/switch_core/bridges/collaboration/adapter.py
+++ b/core/switch_core/bridges/collaboration/adapter.py
@@ -263,6 +263,14 @@ class CollaborationAdapter(ABC):
)
return
+ if self._working_msg.get(key) is not live:
+ # The turn ended (or another move landed) while this post was in
+ # flight, so whoever cleared it has already removed the message this
+ # one replaces. Nothing will ever clear the replacement, so take it
+ # back down rather than strand it in the channel.
+ await self._remove_runtime_indicator(channel_id, ref)
+ return
+
self._working_msg[key] = replace(
live, message_ref=ref, thread_root_id=thread_root_id
)
diff --git a/core/switch_core/bridges/collaboration/mattermost/adapter.py b/core/switch_core/bridges/collaboration/mattermost/adapter.py
index cd4dc8467..90ee66022 100644
--- a/core/switch_core/bridges/collaboration/mattermost/adapter.py
+++ b/core/switch_core/bridges/collaboration/mattermost/adapter.py
@@ -535,6 +535,12 @@ class MattermostAdapter(CollaborationAdapter):
)
return
+ if self._working_msg.get(key) is not live:
+ # The turn ended while the delete was in flight. The old post is
+ # already gone and the entry with it, so there is nothing left to
+ # move — reposting now would strand an indicator no one will clear.
+ return
+
ref = await self.send_message(channel_id, agent_name, live.body, thread_root_id)
if ref is None:
# The old post is already gone, so there is nothing to fall back to.
@@ -546,6 +552,13 @@ class MattermostAdapter(CollaborationAdapter):
channel_id,
)
return
+
+ if self._working_msg.get(key) is not live:
+ # Same race, one step later: the replacement has no owner now, so
+ # take it back down rather than leave it in the channel.
+ await self._permanent_delete(ref)
+ return
+
self._working_msg[key] = replace(
live, message_ref=ref, thread_root_id=thread_root_id
)
diff --git a/core/tests/switch_core/bridges/collaboration/test_mattermost_runtime_state.py b/core/tests/switch_core/bridges/collaboration/test_mattermost_runtime_state.py
index 014bfd3dc..1ae4eb5aa 100644
--- a/core/tests/switch_core/bridges/collaboration/test_mattermost_runtime_state.py
+++ b/core/tests/switch_core/bridges/collaboration/test_mattermost_runtime_state.py
@@ -155,6 +155,49 @@ def test_failed_repost_after_delete_drops_the_stale_ref() -> None:
assert ("chan-1", "worker") not in adapter._working_msg
+def test_a_turn_ending_mid_delete_abandons_the_move() -> None:
+ # Mattermost deletes first, so a turn ending during that delete leaves
+ # nothing to move — reposting anyway would strand an indicator forever.
+ adapter = _adapter()
+ recorder = _Recorder(hard_delete_works=True)
+ recorder.install(adapter)
+ _seed_indicator(adapter)
+
+ real_delete = adapter._permanent_delete
+
+ async def delete_then_go_idle(post_id: str) -> bool:
+ ok = await real_delete(post_id)
+ adapter._working_msg.pop(("chan-1", "worker"), None)
+ return ok
+
+ adapter._permanent_delete = delete_then_go_idle # type: ignore[method-assign]
+ _run(adapter.reposition_runtime_state("chan-1", "worker", None))
+
+ assert recorder.sends == []
+ assert ("chan-1", "worker") not in adapter._working_msg
+
+
+def test_a_turn_ending_mid_repost_takes_the_replacement_back_down() -> None:
+ adapter = _adapter()
+ recorder = _Recorder(hard_delete_works=True)
+ recorder.install(adapter)
+ _seed_indicator(adapter)
+
+ real_send = adapter.send_message
+
+ async def send_then_go_idle(*args: Any, **kwargs: Any) -> str | None:
+ ref = await real_send(*args, **kwargs)
+ adapter._working_msg.pop(("chan-1", "worker"), None)
+ return ref
+
+ adapter.send_message = send_then_go_idle # type: ignore[method-assign]
+ _run(adapter.reposition_runtime_state("chan-1", "worker", None))
+
+ # post-1 was the move's own delete; post-2 is the orphan being cleaned up.
+ assert recorder.hard_deletes == ["post-1", "post-2"]
+ assert ("chan-1", "worker") not in adapter._working_msg
+
+
def test_idle_still_falls_back_to_a_terminal_marker() -> None:
# Turn end is unchanged: without a hard delete the post is edited rather
# than soft deleted, so no "(message deleted)" tombstone is left.
diff --git a/core/tests/switch_core/bridges/collaboration/test_slack_adapter.py b/core/tests/switch_core/bridges/collaboration/test_slack_adapter.py
index 78ff7782e..eb096f2c2 100644
--- a/core/tests/switch_core/bridges/collaboration/test_slack_adapter.py
+++ b/core/tests/switch_core/bridges/collaboration/test_slack_adapter.py
@@ -771,6 +771,38 @@ def test_reposition_is_a_noop_without_a_live_indicator() -> None:
assert fake.deletes == []
+def test_a_turn_ending_mid_move_does_not_strand_the_replacement() -> None:
+ # The turn can go idle while the replacement post is still in flight. The
+ # clear removes the message this move was replacing, so the replacement
+ # would be left with nothing to ever clear it — it must be taken back down.
+ adapter = _adapter()
+ fake = _FakeWebClient()
+ adapter._web_client = fake # type: ignore[assignment]
+
+ _run(
+ adapter.apply_runtime_state(
+ "C123", "agent-bot", "working", notify_user=None, thread_root_id=None
+ )
+ )
+
+ real_send = adapter.send_message
+
+ async def send_then_go_idle(*args: Any, **kwargs: Any) -> str | None:
+ ref = await real_send(*args, **kwargs)
+ await adapter.apply_runtime_state(
+ "C123", "agent-bot", "idle", notify_user=None, thread_root_id=None
+ )
+ return ref
+
+ adapter.send_message = send_then_go_idle # type: ignore[method-assign]
+ _run(adapter.reposition_runtime_state("C123", "agent-bot", None))
+
+ # Both the original and the orphaned replacement are gone, and nothing is
+ # left tracked for a turn that has ended.
+ assert {d["ts"] for d in fake.deletes} == {"999.9", "888.8"}
+ assert ("C123", "agent-bot") not in adapter._working_msg
+
+
def test_reposition_leaves_the_original_when_the_repost_fails() -> None:
# A move must never end with no indicator at all: if the replacement cannot
# be posted, the original stays where it is rather than being deleted.