fix(gateway): retain conversation route anchors for Kanban callbacks
Carry canonical scope_id and parent_chat_id through session context and reply metadata. Stamp slash-created subscriptions with the routed source profile, not the notifier process profile. The gateway facade only wires the topical metadata helper and existing context binding. Two real gateway/DB invariants reproduced missing anchors and wrong owner before implementation. Related: #101196 and #101397. Co-authored-by: Brooklyn Nicholson <brooklyn.bb.nicholson@gmail.com>
This commit is contained in:
@@ -4007,6 +4007,8 @@ class GatewayRunner(
|
||||
metadata.setdefault("scope_id", str(team_id))
|
||||
if user_id:
|
||||
metadata.setdefault("user_id", str(user_id))
|
||||
from gateway.session_context import source_route_metadata
|
||||
metadata = source_route_metadata(source, metadata)
|
||||
# Routed profile for shared state.db namespaces: under profile_routes the transport adapter's
|
||||
# stamp is not the profile that wrote the binding (Telegram prune path needs it).
|
||||
# See #76423.
|
||||
@@ -4088,6 +4090,7 @@ class GatewayRunner(
|
||||
user_id_alt=str(context.source.user_id_alt) if context.source.user_id_alt else "",
|
||||
user_name=str(context.source.user_name) if context.source.user_name else "",
|
||||
scope_id=str(getattr(context.source, "scope_id", "") or ""),
|
||||
parent_chat_id=str(getattr(context.source, "parent_chat_id", "") or ""),
|
||||
session_key=context.session_key,
|
||||
message_id=str(context.source.message_id) if context.source.message_id else "",
|
||||
profile=getattr(context.source, "profile", "") or "",
|
||||
|
||||
@@ -36,7 +36,7 @@ _SESSION_VARS = (
|
||||
_SESSION_CHAT_NAME, _SESSION_THREAD_ID, _SESSION_USER_ID, _SESSION_USER_ID_ALT,
|
||||
_SESSION_USER_NAME, _SESSION_SCOPE_ID, _SESSION_KEY, _SESSION_ID,
|
||||
_SESSION_UI_SESSION_ID, _SESSION_MESSAGE_ID, _SESSION_PROFILE,
|
||||
_BROWSER_CONTROL_PRINCIPAL, _BROWSER_CONTROL_TRANSPORT_FAMILY, _CRON_SESSION,
|
||||
_BROWSER_CONTROL_PRINCIPAL, _BROWSER_CONTROL_TRANSPORT_FAMILY, _CRON_SESSION, _SESSION_PARENT_CHAT_ID,
|
||||
) = tuple(ContextVar(name, default=_UNSET) for name in (
|
||||
"HERMES_SESSION_PLATFORM", "HERMES_SESSION_SOURCE", "HERMES_SESSION_CHAT_ID",
|
||||
"HERMES_SESSION_CHAT_TYPE", "HERMES_SESSION_CHAT_NAME", "HERMES_SESSION_THREAD_ID",
|
||||
@@ -44,7 +44,7 @@ _SESSION_VARS = (
|
||||
"HERMES_SESSION_SCOPE_ID", "HERMES_SESSION_KEY", "HERMES_SESSION_ID",
|
||||
"HERMES_UI_SESSION_ID", "HERMES_SESSION_MESSAGE_ID", "HERMES_SESSION_PROFILE",
|
||||
"HERMES_BROWSER_CONTROL_PRINCIPAL", "HERMES_BROWSER_CONTROL_TRANSPORT_FAMILY",
|
||||
"HERMES_CRON_SESSION",
|
||||
"HERMES_CRON_SESSION", "HERMES_SESSION_PARENT_CHAT_ID",
|
||||
))
|
||||
|
||||
# Whether this channel can route an ASYNC completion back AFTER the turn ends (see
|
||||
@@ -101,13 +101,20 @@ def scoped_current_session_id(session_id: str | None = None) -> Iterator[None]:
|
||||
_SESSION_ID.set(previous)
|
||||
|
||||
|
||||
def source_route_metadata(source: Any, metadata: dict | None) -> dict | None:
|
||||
"""Keep inbound route anchors for durable deliveries after the source is gone."""
|
||||
anchors = {key: str(value) for key in ("scope_id", "parent_chat_id")
|
||||
if (value := getattr(source, key, None))}
|
||||
return {**(metadata or {}), **anchors} if anchors else metadata
|
||||
|
||||
|
||||
def set_session_vars(
|
||||
platform: str = "", source: str = "", chat_id: str = "", chat_type: str = "",
|
||||
chat_name: str = "", thread_id: str = "", user_id: str = "", user_id_alt: str = "",
|
||||
user_name: str = "", scope_id: str = "", session_key: str = "", session_id: str = "",
|
||||
message_id: str = "", profile: str = "", browser_control_principal: str = "",
|
||||
browser_control_transport_family: str = "", cwd: str = "", async_delivery: bool = True,
|
||||
ui_session_id: str = "", cron_session: Any = _UNSET,
|
||||
ui_session_id: str = "", cron_session: Any = _UNSET, parent_chat_id: str = "",
|
||||
) -> list:
|
||||
"""Set all session context variables and return reset tokens. Call
|
||||
``clear_session_vars(tokens)`` in a ``finally``; not nestable, clearing resets every var
|
||||
@@ -117,7 +124,7 @@ def set_session_vars(
|
||||
values = (
|
||||
platform, source, chat_id, chat_type, chat_name, thread_id, user_id, user_id_alt,
|
||||
user_name, scope_id, session_key, session_id, ui_session_id, message_id, profile,
|
||||
browser_control_principal, browser_control_transport_family, cron_session,
|
||||
browser_control_principal, browser_control_transport_family, cron_session, parent_chat_id,
|
||||
)
|
||||
tokens = [var.set(value) for var, value in zip(_SESSION_VARS, values)]
|
||||
tokens.append(_SESSION_ASYNC_DELIVERY.set(bool(async_delivery)))
|
||||
|
||||
@@ -398,7 +398,7 @@ class GatewaySlashCommandsMixin(
|
||||
# keys the participant on ``user_id_alt or user_id``, so a replayed wake rebuilds
|
||||
# the same session key only when the alt id survives the round-trip.
|
||||
user_id_alt=_field("user_id_alt"),
|
||||
notifier_profile=getattr(self, "_kanban_notifier_profile", None) or self._active_profile_name(),
|
||||
notifier_profile=_field("profile") or getattr(self, "_kanban_notifier_profile", None) or self._active_profile_name(),
|
||||
# Subscribing from chat: deliver the passive message and wake the destination agent.
|
||||
delivery_mode="notify+wake", delivery_metadata=delivery_metadata)
|
||||
finally:
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
"""Route anchors survive gateway background/subprocess producers without stale fallback."""
|
||||
from gateway.config import Platform
|
||||
from gateway.run import GatewayRunner
|
||||
from gateway.session import SessionContext, SessionSource
|
||||
from gateway.session_context import get_session_env
|
||||
|
||||
|
||||
def test_session_route_anchors_reach_metadata_and_context_then_clear(monkeypatch):
|
||||
runner = GatewayRunner.__new__(GatewayRunner)
|
||||
runner.adapters = {}
|
||||
source = SessionSource(platform=Platform.DISCORD, chat_id="post", chat_type="thread",
|
||||
thread_id="post", scope_id="scope", guild_id="guild",
|
||||
parent_chat_id="parent", profile="yuki")
|
||||
context = SessionContext(source=source, connected_platforms=[], home_channels={}, session_key="origin")
|
||||
expected = {key: getattr(source, key) for key in ("scope_id", "parent_chat_id")}
|
||||
metadata = runner._thread_metadata_for_source(source)
|
||||
assert {key: metadata.get(key) for key in expected} == expected
|
||||
keys = {key: "HERMES_SESSION_" + key.upper() for key in ("scope_id", "parent_chat_id")}
|
||||
for name in keys.values():
|
||||
monkeypatch.setenv(name, "stale")
|
||||
tokens = runner._set_session_env(context)
|
||||
try:
|
||||
assert {key: get_session_env(name) for key, name in keys.items()} == {key: expected[key] for key in keys}
|
||||
finally:
|
||||
runner._clear_session_env(tokens)
|
||||
assert all(get_session_env(name) == "" for name in keys.values())
|
||||
|
||||
|
||||
def test_slash_subscription_keeps_the_routed_source_owner(tmp_path, monkeypatch):
|
||||
import asyncio
|
||||
from gateway.platforms.event import MessageEvent
|
||||
from hermes_cli import kanban_db as kb, kanban_db_connect as kbc, kanban_db_notify as kbn
|
||||
|
||||
monkeypatch.setenv("HERMES_KANBAN_DB", str(tmp_path / "kanban.db"))
|
||||
runner = GatewayRunner.__new__(GatewayRunner)
|
||||
runner._kanban_notifier_profile = "default"
|
||||
source = SessionSource(platform=Platform.DISCORD, chat_id="post", chat_type="thread",
|
||||
thread_id="post", scope_id="guild", parent_chat_id="parent", profile="yuki")
|
||||
with kbc.connect() as conn:
|
||||
task = kb.create_task(conn, title="slash-created")
|
||||
assert asyncio.run(runner._kanban_auto_subscribe(MessageEvent(text="/kanban create", source=source), task, None))
|
||||
with kbc.connect() as conn:
|
||||
sub = kbn.list_notify_subs(conn, task)[0]
|
||||
assert sub["notifier_profile"] == source.profile
|
||||
assert all(sub["delivery_metadata"][key] == getattr(source, key)
|
||||
for key in ("scope_id", "parent_chat_id"))
|
||||
Reference in New Issue
Block a user