fix(gateway): record the receiving bot on routes saved without one

Review should-fix 1: only route creation wrote transport_profile, and /reset,
/new and switches copy it, so every routing entry saved before the column
existed stayed NULL forever and a bot_profile chat (tg bot -> work runtime)
still lost its background results across a restart. The next inbound turn on
such a route now records the bot its validated ingress names; an inferred
identity (no transport evidence) never fills it with the primary-bot guess.
This commit is contained in:
Teknium
2026-11-01 17:38:25 -08:00
parent 14641f9169
commit 8f5a76f4f4
3 changed files with 35 additions and 1 deletions

View File

@@ -15,7 +15,7 @@ from typing import Dict, List, Optional, Any
from .config import Platform, GatewayConfig, HomeChannel
from .whatsapp_identity import canonical_whatsapp_identifier
from gateway.session_detached import detached_route_of
from gateway.session_identity import transport_profile_of
from gateway.session_identity import known_transport_profile, transport_profile_of
from gateway.session_persistence import SessionPersistenceMixin, _DB_UNPINNED
from gateway.session_prompt_pin import SessionPromptPinMixin, sanitize_prompt_pin
from gateway.session_recovery import SessionRecoveryMixin
@@ -1053,6 +1053,11 @@ class SessionStore(
decision.entry = entry
decision.needs_save = touch_activity or healed
decision.metadata_only_save = touch_activity and not healed
# Only creation wrote the receiving bot, and /reset, /new and switches copy it, so a
# route saved before the column existed stays NULL and its completions stay pending
# after a restart. Its next inbound turn records the bot that verifiably received it.
if touch_activity and entry.transport_profile is None:
entry.transport_profile = known_transport_profile(source)
return decision
def _route_recover(

View File

@@ -113,6 +113,13 @@ def transport_profile_of(source: Any) -> Optional[str]:
return identity.transport_profile if identity is not None and identity.multiplexed else None
def known_transport_profile(source: Any) -> Optional[str]:
""":func:`transport_profile_of` minus inferred identities: the bot a live adapter or a persisted
record named, never the primary-profile guess a source with no transport evidence falls back to."""
identity = identity_of(source)
return transport_profile_of(source) if identity is not None and not identity.transport_inferred else None
def replace_source(source: SessionSource, **changes: Any) -> SessionSource:
""":func:`dataclasses.replace` that keeps the wire-invisible provenance (transport ref,
authorization home, identity). A plain ``replace`` silently produces a source the runner

View File

@@ -217,3 +217,25 @@ async def test_secondary_bot_route_runs_in_the_routed_runtime_scope(mux):
await handler(MessageEvent(text="hi", source=mux.team_b.build_source(
chat_id=chat, chat_type="dm", user_id=chat)))
assert scopes == [mux.home, mux.home / "profiles" / "team_b"]
def test_inbound_turn_records_the_receiving_bot_on_a_route_saved_without_one(mux, tmp_path):
"""A route saved before ``transport_profile`` existed (or carried over by /reset, /new) is NULL, so
after a restart its completions have no bot to answer through. The next inbound turn records the
bot that received it; an inferred identity (no transport evidence) never fills it with a guess."""
from gateway.session import SessionStore
def store():
return SessionStore(sessions_dir=tmp_path / "sessions", config=GatewayConfig(multiplex_profiles=True))
live, _ = _live(mux, mux.team_b, "999", transport_profile="team_b")
legacy = store()
legacy.get_or_create_session(live).transport_profile = None
legacy._save_entries()
inferred = SessionSource(platform=Platform.TELEGRAM, chat_id="999", chat_type="dm", user_id="999")
assert resolve_identity(inferred, runner=mux.runner).transport_inferred
after_restart = store()
assert after_restart.get_or_create_session(inferred).transport_profile is None
assert after_restart.get_or_create_session(live).transport_profile == "team_b"
assert store().get_or_create_session(live, touch_activity=False).transport_profile == "team_b"