Skip to content

The agent that grows with you

fix(honcho): read the turn author from sync_turn and treat the alt id as the session peer

Erosika20b117ad3b42
diff --git a/plugins/memory/honcho/__init__.py b/plugins/memory/honcho/__init__.py
index b863c6c491e..ed3add3fe05 100644
--- a/plugins/memory/honcho/__init__.py
+++ b/plugins/memory/honcho/__init__.py
@@ -601,9 +601,13 @@ class HonchoMemoryProvider(DialecticMixin, MemoryProvider):
return chunks
- def sync_turn(self, user_content: str, assistant_content: str, *, session_id: str = "") -> None:
+ def sync_turn(
+ self, user_content: str, assistant_content: str, *, session_id: str = "",
+ turn_author: Optional[Dict[str, Any]] = None, scope: Optional[str] = None,
+ ) -> None:
"""Record the conversation turn in Honcho (non-blocking), chunking messages that
- exceed the Honcho API limit. Honors saveMessages: false."""
+ exceed the Honcho API limit. Honors saveMessages: false. ``turn_author`` names who wrote
+ the user side; the ``on_turn_start`` stash is the fallback for callers that never pass it."""
if not self._writes_enabled():
return
if _is_internal_gateway_turn(user_content):
@@ -620,9 +624,9 @@ class HonchoMemoryProvider(DialecticMixin, MemoryProvider):
if not clean_user_content and not clean_assistant_content:
return
+ author = turn_author if isinstance(turn_author, dict) else self._turn_author
# Resolved before the thread starts so a following turn cannot retag a queued write.
- author_peer_id = self._manager.resolve_author_peer_id(
- self._session_key, self._turn_author.get("id"), self._turn_author.get("name"))
+ author_peer_id = self._manager.resolve_author_peer_id(self._session_key, author.get("id"), author.get("name"))
def _sync():
session = self._manager.get_or_create(self._session_key)
diff --git a/plugins/memory/honcho/session_peers.py b/plugins/memory/honcho/session_peers.py
index 40ba510d509..ddb26c52b1b 100644
--- a/plugins/memory/honcho/session_peers.py
+++ b/plugins/memory/honcho/session_peers.py
@@ -123,5 +123,8 @@ class SessionPeersMixin:
if self._config is not None and bool(getattr(self._config, "peer_name", None)) \
and getattr(self._config, "pin_peer_name", False) is True:
return None
+ # Either runtime id (Telegram UID or username) names the session's participant.
+ if runtime_id in self._runtime_user_ids():
+ return None
peer_id = self._peer_id_for_runtime_id(runtime_id)
return None if peer_id == self._resolve_user_peer_id(key) else peer_id
diff --git a/tests/honcho_plugin/test_turn_author_peers.py b/tests/honcho_plugin/test_turn_author_peers.py
index 57e0bad499f..0a233970465 100644
--- a/tests/honcho_plugin/test_turn_author_peers.py
+++ b/tests/honcho_plugin/test_turn_author_peers.py
@@ -28,11 +28,12 @@ def _config(**overrides) -> HonchoClientConfig:
return HonchoClientConfig(**base)
-def _manager(config: HonchoClientConfig, runtime_id: str | None = None) -> HonchoSessionManager:
+def _manager(config: HonchoClientConfig, runtime_id: str | None = None, runtime_id_alt: str | None = None) -> HonchoSessionManager:
mgr = HonchoSessionManager(
honcho=MagicMock(),
config=config,
runtime_user_peer_name=runtime_id,
+ runtime_user_peer_name_alt=runtime_id_alt,
)
mgr._get_or_create_peer = MagicMock(side_effect=lambda pid: MagicMock(name=f"peer:{pid}"))
mgr._get_or_create_honcho_session = MagicMock(return_value=(MagicMock(), []))
@@ -51,6 +52,11 @@ class TestResolveAuthorPeerId:
mgr = _manager(_config(), runtime_id="7654321")
assert mgr.resolve_author_peer_id("telegram:group1", "7654321") is None
+ def test_alt_runtime_id_is_the_session_peer(self):
+ """A transport may name the participant by either id (Telegram UID or username)."""
+ mgr = _manager(_config(), runtime_id="7654321", runtime_id_alt="eri_tg")
+ assert mgr.resolve_author_peer_id("telegram:group1", "eri_tg") is None
+
def test_other_participant_gets_its_own_peer(self):
mgr = _manager(_config(), runtime_id="7654321")
assert mgr.resolve_author_peer_id("telegram:group1", "111222") == "111222"
@@ -215,6 +221,33 @@ class TestProviderReadsTheAuthor:
assert user_calls, "the user turn was never written"
assert all(c[1]["author_peer_id"] == "alice" for c in user_calls)
+ def test_sync_turn_prefers_the_turn_author_keyword(self):
+ """The manager passes the author with the turn; the stash is only a fallback."""
+ provider = self._provider()
+ provider._session_initialized = True
+ provider._manager.get_or_create.return_value = MagicMock()
+ provider._manager.resolve_author_peer_id.return_value = "alice"
+
+ provider.on_turn_start(1, "hi", author_id="999")
+ provider.sync_turn("hi", "hello back", turn_author={"id": "111222", "name": "Alice", "is_bot": False})
+ if provider._sync_thread:
+ provider._sync_thread.join(timeout=5)
+
+ provider._manager.resolve_author_peer_id.assert_called_once_with("telegram:group1", "111222", "Alice")
+
+ def test_sync_turn_falls_back_to_the_stash(self):
+ provider = self._provider()
+ provider._session_initialized = True
+ provider._manager.get_or_create.return_value = MagicMock()
+ provider._manager.resolve_author_peer_id.return_value = None
+
+ provider.on_turn_start(1, "hi", author_id="999", author_name="Nine")
+ provider.sync_turn("hi", "hello back")
+ if provider._sync_thread:
+ provider._sync_thread.join(timeout=5)
+
+ provider._manager.resolve_author_peer_id.assert_called_once_with("telegram:group1", "999", "Nine")
+
def test_sync_turn_resolves_before_the_write_thread_starts(self):
"""A following turn must not retag a write that is already queued."""
provider = self._provider()