diff --git a/gateway/mirror.py b/gateway/mirror.py index 4f95746319b25..cee31302c40f7 100644 --- a/gateway/mirror.py +++ b/gateway/mirror.py @@ -45,9 +45,13 @@ def mirror_to_session( logger.debug("Mirror: no session found for %s:%s:%s", platform, chat_id, thread_id) return False + # Use "user" role for mirrored cron/delivery messages to maintain + # proper user/assistant alternation in the conversation history. + # Using "assistant" caused two consecutive assistant messages which + # confuses models and can cause API rejections on strict providers. mirror_msg = { - "role": "assistant", - "content": message_text, + "role": "user", + "content": f"[Cron delivery from {source_label}]\n{message_text}", "timestamp": datetime.now().isoformat(), "mirror": True, "mirror_source": source_label, diff --git a/tests/gateway/test_mirror.py b/tests/gateway/test_mirror.py index 427e720cd928e..19b16ff4f531c 100644 --- a/tests/gateway/test_mirror.py +++ b/tests/gateway/test_mirror.py @@ -161,8 +161,8 @@ def test_successful_mirror(self, tmp_path): transcript = sessions_dir / "sess_abc.jsonl" assert transcript.exists() msg = json.loads(transcript.read_text().strip()) - assert msg["content"] == "Hello!" - assert msg["role"] == "assistant" + assert msg["content"] == "[Cron delivery from cli]\nHello!" + assert msg["role"] == "user" assert msg["mirror"] is True assert msg["mirror_source"] == "cli"