Skip to content

The agent that grows with you

test(gateway): identity-canonical ingress invariants (#88715 rows A/B) + docs

teknium1b40854451007
diff --git a/gateway/AGENTS.md b/gateway/AGENTS.md
index d41911e2d24..33433f342af 100644
--- a/gateway/AGENTS.md
+++ b/gateway/AGENTS.md
@@ -133,15 +133,22 @@ gateway under the backend, and do NOT "fix" update locks by widening the tree-ki
## Profile scope (adapters, turns, and everything between turns)
-- **One identity per inbound event.** `gateway/session_identity.py::resolve_identity` answers
- "which bot received it / who may admit it / where does it run" ONCE per event and pins a frozen
- `RoutingIdentity` on the source (wire-invisible, like `_transport_adapter_ref`);
- `_transport_owner`, `_authorization_home_for_source`, `_resolve_profile_home_for_source`,
- `_session_key_profile` and `_resolve_profile_for_key` read it when present and fall back to
- their old chain only for sources nothing resolved (restored rows, hand-built sources). Never
- derive a second answer next to the identity; extend the object. `transport_profile` ≠
- `runtime_profile` is normal (shared bot → routed satellite). A source copy goes through
- `session_identity.replace_source` so the identity travels with it.
+- **One identity per inbound event, canonicalized FIRST.** `gateway/session_identity.py::resolve_identity`
+ answers "which bot received it / who may admit it / where does it run" ONCE per event and pins a
+ frozen `RoutingIdentity` on the source (wire-invisible, like `_transport_adapter_ref`). Every
+ ingress path calls the canonicalize seam before it derives a key: the adapter side
+ (`platforms/base.py::_canonicalize` — `handle_message`, `_enqueue_text_event`, Telegram photo /
+ album routing, `_handle_message_while_active`, every `_source_session_key`) and the runner side
+ (`run_adapters.py::_canonicalize` — the per-profile / default message, busy and platform-event
+ handlers, the adapter auth-check callback, `run_inbound.py::_hm_admit_event`). No key derivation
+ before it; an unresolved identity under multiplexing (route to an unserved profile) is dropped
+ with one WARNING at the first seam it reaches, never keyed into `agent:main`. `_transport_owner`,
+ `_authorization_home_for_source`, `_resolve_profile_home_for_source`, `_session_key_profile` and
+ `_resolve_profile_for_key` read the identity when present and fall back to their old chain only
+ for sources nothing resolved (restored rows, hand-built sources). Never derive a second answer
+ next to the identity; extend the object. `transport_profile` ≠ `runtime_profile` is normal
+ (shared bot → routed satellite). A source copy goes through `session_identity.replace_source`
+ so the identity travels with it (`_apply_topic_recovery` does).
- **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 a16a05fde46..d061cf10d2c 100644
--- a/website/docs/developer-guide/multiplexing-gateway.md
+++ b/website/docs/developer-guide/multiplexing-gateway.md
@@ -179,12 +179,18 @@ weak `transport` ref to the receiving adapter. `"default"` is spelled out;
raises `IdentityUnresolved` and the event is dropped.
Adapters also carry `_owner_profile` (installed at adapter configuration time,
-before any inbound event) because adapter ingress runs before the runner pins
-the identity; `_session_key_profile` resolves identity → source stamp → owner
-profile → store resolver. Text/media batching, active-session tracking, and the
-busy-session guard are all keyed per lane, so two bots sharing a chat do not
-share a session lane. Copy a source with `session_identity.replace_source`, not
-`dataclasses.replace`, or the copy loses its transport and identity.
+before any inbound event). Every ingress path canonicalizes the identity FIRST
+— `BasePlatformAdapter._canonicalize` runs at `handle_message`, text/photo/album
+batching, the busy path and every adapter-derived session key; the runner's
+per-profile and default handlers, the auth-check callback and the shared
+`_handle_message` gate do the same — so no lane is keyed before the receiving
+bot is known. Text/media batching, active-session tracking, the busy-session
+guard, `/stop` `/new` `/reset` and clarify replies are all keyed per lane, so
+two bots sharing a chat do not share a session lane and a control command on one
+bot cannot reach the other's run. A route to an unserved profile is dropped with
+one WARNING at the first seam it reaches, never keyed into `agent:main`. Copy a
+source with `session_identity.replace_source`, not `dataclasses.replace`, or the
+copy loses its transport and identity.
## Control plane