Skip to content

The agent that grows with you

docs(gateway): identity survives restore, relay, callbacks and thread hops

teknium104a151c3e084
diff --git a/gateway/AGENTS.md b/gateway/AGENTS.md
index 0bb5baa37a8..075c6ae2159 100644
--- a/gateway/AGENTS.md
+++ b/gateway/AGENTS.md
@@ -164,6 +164,20 @@ gateway under the backend, and do NOT "fix" update locks by widening the tree-ki
| shared bot → profile that owns its own bot | routed | receiving adapter | receiving adapter (conversation continuity; #70625's "routed bot" reading was not adopted) |
| secondary-owned bot → `default` (`bot_profile`) | default (`agent:main`) | receiving adapter | receiving adapter |
| restored / hand-built, no live provenance | stored `source.profile` | **`None`** | unique owner of `(platform, runtime)`; a disconnected secondary → `None`, never the default bot |
+- **Identity survives the process.** `SessionEntry.transport_profile` (routing index +
+ `sessions.transport_profile`, nullable, reconciled by `SCHEMA_SQL`) persists the receiving bot
+ next to the key namespace; the namespace says where a lane RUNS, the column says which bot may
+ DELIVER to it. Anything reviving a session from durable state (auto-resume, heartbeat restore,
+ plugin injection, background-process events) reads `entry.origin` through
+ `authz_mixin.py::_restored_source`, which re-pins a `RoutingIdentity(transport=None)` via
+ `session_identity.restore_identity`; `_delivery_adapter_for` then delivers through that bot's
+ adapter or nothing (never the default bot by heuristic). Rows without the column (pre-PR-5)
+ keep the `_is_shared_bot_satellite` fallback. Deferred callbacks capture the identity/home at
+ command time (`/model` picker); `_run_in_executor_with_context` carries the scope over thread
+ hops. Relay: `_with_scope` echoes the routed `profile` on every outbound frame and `follow_up`
+ derives it from the key namespace so the connector stamps it on the next `passthrough_forward`.
+ Ambient `get_active_profile_name()` reads in `gateway/` are boot-only and marked
+ `# launch profile, pre-identity`; a path with an identity reads `identity.runtime_profile`.
- **Token locks.** An adapter that connects with a unique credential (bot token, API key) calls
`acquire_scoped_lock()` from `gateway.status` in `connect()`/`start()` and `release_scoped_lock()`
in `disconnect()`/`stop()`, so two profiles cannot share one credential. Canonical:
diff --git a/website/docs/developer-guide/multiplexing-gateway.md b/website/docs/developer-guide/multiplexing-gateway.md
index dea5122e7db..7f72f2f7e94 100644
--- a/website/docs/developer-guide/multiplexing-gateway.md
+++ b/website/docs/developer-guide/multiplexing-gateway.md
@@ -222,6 +222,22 @@ conflating (`gateway/authz_mixin.py`):
Outside multiplexing there is one adapter per platform, so both seams return
it. `tests/gateway/test_multiplex_transport_matrix.py` asserts every row.
+### Restore, relay, callbacks and thread hops
+
+The routing entry persists `transport_profile` next to the key (and the
+`sessions.transport_profile` column in `state.db`), so after a restart a
+revived lane still knows which bot received it: `_restored_source(entry)`
+re-pins a `RoutingIdentity` with no live adapter and `_adapter_for_source`
+delivers through that bot's adapter or fails closed — a satellite routed
+through the default bot keeps answering from the default bot, a lane owned by
+a secondary never falls back to the default bot's credential. Entries written
+before the column existed carry `null` and keep the shared-bot heuristics.
+Over the relay, every outbound frame's `metadata.profile` (and `follow_up`'s
+key namespace) tells the connector which profile to stamp on the next
+`passthrough_forward`, so a button press after a routed slash command stays in
+the same profile. Deferred callbacks (`/model` picker) capture the routed home
+at command time and the gateway's executor hops copy the ContextVar scope.
+
## Control plane
Desktop plugins reach the gateway only through the ws JSON-RPC door, so
diff --git a/website/docs/developer-guide/session-storage.md b/website/docs/developer-guide/session-storage.md
index 56430819040..db42da3f27a 100644
--- a/website/docs/developer-guide/session-storage.md
+++ b/website/docs/developer-guide/session-storage.md
@@ -151,7 +151,8 @@ Abridged — see `SCHEMA_SQL` in `hermes_state_common.py` (applied by `hermes_st
(which also includes gateway routing metadata such as `session_key`, `chat_id`,
`chat_type`, `thread_id`, `display_name`, `origin_json`, `expiry_finalized`,
workspace fields `cwd` / `git_branch` / `git_repo_root`, handoff and
-compression-failure fields, `profile_name`, `rewind_count`, `archived`, and
+compression-failure fields, `profile_name`, `transport_profile` (the multiplex
+bot that received the lane, nullable), `rewind_count`, `archived`, and
`pinned`):
```sql