From 8704e9ca4ce0cee31a08c299103ababcfedd9ba0 Mon Sep 17 00:00:00 2001 From: Erosika Date: Tue, 8 Sep 2026 16:28:43 -0400 Subject: [PATCH] fix(honcho): put this agent's aiPeer in the a2a session key _a2a_session_key now names the session :a2a::-. Two profiles that share a workspace and a session key wrote one sender's DMs into one Honcho session. The recipient peer comes from the same aiPeer derivation the session builder uses, moved into session_peers.assistant_peer_id_for so the two cannot drift. --- plugins/memory/honcho/README.md | 2 +- plugins/memory/honcho/__init__.py | 7 +++++-- plugins/memory/honcho/session_peers.py | 15 ++++++++++++--- tests/honcho_plugin/test_a2a_sessions.py | 11 ++++++++++- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/plugins/memory/honcho/README.md b/plugins/memory/honcho/README.md index f0cb7911c8..547c961104 100644 --- a/plugins/memory/honcho/README.md +++ b/plugins/memory/honcho/README.md @@ -276,7 +276,7 @@ If `sessionPeerPrefix` is `true`, the peer name is prepended: `alice-hermes-agen #### Bot DMs (`a2aSessions`) -In bot mode another Hermes profile can DM this agent. The relay marks that turn with author `bot:`. A gateway platform marks a bot sender with its platform user id and a bot flag. Either way the turn never reaches the human's session. With `a2aSessions: true` (default) the turn is written into `:a2a:-<8-char digest>`: the sender's message under the sender's peer, the reply under this agent's `aiPeer`. A `bot:` sender is identified by its full id, `bot:` for a profile on this machine or `bot:/` for one relayed through a Desktop connection. Its peer is the `userPeerAliases` entry for that full id if one exists, else the id after `bot:` sanitized, with no `runtimePeerPrefix`. When sanitizing changed the id, or the result equals `peerName` or an alias target, a digest suffix is added the same way `runtimePeerPrefix` users get one, so a bot never lands on the operator's peer and two connections' `coder` stay apart. A platform bot resolves like any other runtime user: alias, then prefix. `pinUserPeer` never collapses a bot onto `peerName`. A bot whose peer would equal this agent's `aiPeer` is skipped, and so is every bot turn when `a2aSessions: false`. During a bot-authored turn `honcho_conclude` and `honcho_profile` refuse writes, because conclusions and cards describe the human. Recall still reads the human's session only. +In bot mode another Hermes profile can DM this agent. The relay marks that turn with author `bot:`. A gateway platform marks a bot sender with its platform user id and a bot flag. Either way the turn never reaches the human's session. With `a2aSessions: true` (default) the turn is written into `:a2a::-<8-char digest>`: the sender's message under the sender's peer, the reply under this agent's `aiPeer`. The `aiPeer` segment keeps two profiles that share a `workspace` and a session key from writing one sender's DMs into one session. A `bot:` sender is identified by its full id, `bot:` for a profile on this machine or `bot:/` for one relayed through a Desktop connection. Its peer is the `userPeerAliases` entry for that full id if one exists, else the id after `bot:` sanitized, with no `runtimePeerPrefix`. When sanitizing changed the id, or the result equals `peerName` or an alias target, a digest suffix is added the same way `runtimePeerPrefix` users get one, so a bot never lands on the operator's peer and two connections' `coder` stay apart. A platform bot resolves like any other runtime user: alias, then prefix. `pinUserPeer` never collapses a bot onto `peerName`. A bot whose peer would equal this agent's `aiPeer` is skipped, and so is every bot turn when `a2aSessions: false`. During a bot-authored turn `honcho_conclude` and `honcho_profile` refuse writes, because conclusions and cards describe the human. Recall still reads the human's session only. #### What each strategy produces diff --git a/plugins/memory/honcho/__init__.py b/plugins/memory/honcho/__init__.py index 2099789aaf..d1758f732d 100644 --- a/plugins/memory/honcho/__init__.py +++ b/plugins/memory/honcho/__init__.py @@ -21,6 +21,7 @@ from agent.memory_provider import MemoryProvider, is_trivial_prompt from agent.turn_author import a2a_key from plugins.memory.honcho.client import HonchoClientConfig, resolve_config_path, spawn_context_thread from plugins.memory.honcho.dialectic import DialecticMixin +from plugins.memory.honcho.session_peers import assistant_peer_id_for, sanitize_peer_id from plugins.memory.honcho.tool_schemas import ALL_TOOL_SCHEMAS from tools.registry import tool_error @@ -700,10 +701,12 @@ class HonchoMemoryProvider(DialecticMixin, MemoryProvider): def _a2a_session_key(self, author: Dict[str, Any]) -> str: """Honcho session for one sender bot's turns into this agent, named from core's ``a2a_key``. - The digest keeps two ids apart when sanitizing would make them equal.""" + This agent's ``aiPeer`` is in the key because two profiles can share a workspace and a session + key. The digest keeps two ids apart when sanitizing would make them equal.""" prefix, _, ident = (a2a_key(author) or "").partition(":") digest = hashlib.sha256(ident.encode("utf-8")).hexdigest()[:8] - key = f"{self._session_key}:{prefix}:{re.sub(r'[^a-zA-Z0-9_-]', '-', ident)}-{digest}" + recipient = assistant_peer_id_for(self._config) + key = f"{self._session_key}:{prefix}:{recipient}:{sanitize_peer_id(ident)}-{digest}" return HonchoClientConfig._enforce_session_id_limit(key, key) def _bot_turn_write_refusal(self) -> Optional[str]: diff --git a/plugins/memory/honcho/session_peers.py b/plugins/memory/honcho/session_peers.py index 983ebaa4f3..849782018d 100644 --- a/plugins/memory/honcho/session_peers.py +++ b/plugins/memory/honcho/session_peers.py @@ -17,12 +17,21 @@ _PEER_ID_HASH_ESCALATION_LENGTHS = (8, 12, 16, 24, 32, 64) BOT_AUTHOR_PREFIX = "bot:" +def sanitize_peer_id(id_str: str) -> str: + """Sanitize an ID to match Honcho's pattern: ^[a-zA-Z0-9_-]+""" + return re.sub(r'[^a-zA-Z0-9_-]', '-', id_str) + + +def assistant_peer_id_for(config: Any) -> str: + """The agent's own peer ID from ``aiPeer``, as the session builder derives it.""" + return sanitize_peer_id(getattr(config, "ai_peer", None) or "hermes-assistant") + + class SessionPeersMixin: """Resolve user/assistant/observer peer IDs. Reads ``self._config`` and runtime identities only.""" def _sanitize_id(self, id_str: str) -> str: - """Sanitize an ID to match Honcho's pattern: ^[a-zA-Z0-9_-]+""" - return re.sub(r'[^a-zA-Z0-9_-]', '-', id_str) + return sanitize_peer_id(id_str) def _cfg(self, name: str, default: Any = None) -> Any: return getattr(self._config, name, default) if self._config is not None else default @@ -121,7 +130,7 @@ class SessionPeersMixin: def assistant_peer_id(self) -> str: """This agent's own peer ID, as the session builder derives it from ``aiPeer``.""" - return self._sanitize_id(self._cfg("ai_peer") or "hermes-assistant") + return assistant_peer_id_for(self._config) def resolve_author_peer_id( self, key: str, author_id: str | None, author_name: str | None = None, *, is_bot: bool = False, diff --git a/tests/honcho_plugin/test_a2a_sessions.py b/tests/honcho_plugin/test_a2a_sessions.py index 0014322e6a..62c4cf67df 100644 --- a/tests/honcho_plugin/test_a2a_sessions.py +++ b/tests/honcho_plugin/test_a2a_sessions.py @@ -118,7 +118,16 @@ class TestA2aRouting: provider = _provider() keys = {provider._a2a_session_key({"id": bot, "is_bot": True}) for bot in ("bot:a.b", "bot:a-b", "bot:a_b", "bot:a:b")} assert len(keys) == 4 - assert all(key.startswith("Bot-Chat:a2a:bot-a") for key in keys) + assert all(key.startswith("Bot-Chat:a2a:hermes-assistant:bot-a") for key in keys) + + def test_two_recipients_sharing_a_session_key_get_different_sessions(self): + """Two profiles with one workspace and one session key must not merge a sender's DMs.""" + ivy, holly = _provider(), _provider() + ivy._config = HonchoClientConfig(workspace_id="shared", ai_peer="ivy") + holly._config = HonchoClientConfig(workspace_id="shared", ai_peer="holly") + + assert ivy._a2a_session_key(BOT_AUTHOR) != holly._a2a_session_key(BOT_AUTHOR) + assert ivy._a2a_session_key(BOT_AUTHOR).startswith("Bot-Chat:a2a:ivy:bot-coder-") def test_long_session_key_stays_within_the_honcho_limit(self): provider = _provider()