Skip to content

The agent that grows with you

fix(gateway): also persist user_id and session_key in child-session creates

Jason4f0531c972e5
diff --git a/gateway/slash_commands.py b/gateway/slash_commands.py
index 8328de0e6e5..312e1a3365b 100644
--- a/gateway/slash_commands.py
+++ b/gateway/slash_commands.py
@@ -4786,19 +4786,21 @@ class GatewaySlashCommandsMixin:
model=(self.config.get("model", {}) or {}).get("default") if isinstance(self.config, dict) else None,
model_config={"_branched_from": parent_session_id},
parent_session_id=parent_session_id,
- # Gateway routing columns (#NNNNN): forward the caller's own
- # chat_id/chat_type/thread_id at CREATE time, same fix as
- # the compression-rotation bug in
- # agent/conversation_compression.py. Without this, the
- # branched child row has NULL routing columns until
- # switch_session() below calls _record_gateway_session_peer()
- # — a crash/kill anywhere between here and there (most
- # plausibly mid-history-copy, since each append_message call
- # a few lines down is independently best-effort) leaves the
- # branch permanently unroutable: unreachable by chat/thread
- # lookup, and unreachable via /resume's IDOR guard too
- # (which requires the row's chat_id/thread_id to match the
- # caller's).
+ # Gateway routing columns — forward ALL of them at CREATE time,
+ # same fix as the compression-rotation bug in
+ # agent/conversation_compression.py. Without these, the branched
+ # child row has NULL routing columns until switch_session() below
+ # calls _record_gateway_session_peer() — a crash/kill anywhere
+ # between here and there (most plausibly mid-history-copy, since
+ # each append_message call a few lines down is independently
+ # best-effort) leaves the branch permanently unroutable:
+ # unreachable by chat/thread lookup, and unreachable via /resume's
+ # IDOR guard too (which requires the row's chat_id/thread_id to
+ # match the caller's). user_id is critical for the fallback lookup
+ # path (hermes_state.py:1994-2009) that searches by the complete
+ # peer tuple when session_key doesn't match.
+ user_id=source.user_id,
+ session_key=session_key,
chat_id=source.chat_id,
chat_type=source.chat_type,
thread_id=source.thread_id,
diff --git a/tests/gateway/test_branch_routing_columns.py b/tests/gateway/test_branch_routing_columns.py
index d1422477226..5a47e238c61 100644
--- a/tests/gateway/test_branch_routing_columns.py
+++ b/tests/gateway/test_branch_routing_columns.py
@@ -128,6 +128,15 @@ class TestBranchRoutingColumns:
)
assert row["chat_type"] == "dm"
assert row["thread_id"] == "544520"
+ # user_id and session_key are also required for the fallback lookup
+ # path (hermes_state.py:1994-2009) when session_key-based lookup fails
+ assert row["user_id"] == "170829464", (
+ "branched session lost user_id — fallback peer-tuple lookup "
+ "in find_latest_gateway_session_for_peer can never match"
+ )
+ assert row["session_key"] is not None, (
+ "branched session lost session_key — primary lookup path fails"
+ )
_ = real_switch_session # silence unused