#3182: fix(gateway): use canonical session key in chat.send
app: web-ui
gateway
Fixes session key mismatch where `chat.send` used raw key (e.g. `"main"`) but `chat.history` used canonical key (e.g. `"agent:main:main"`), causing messages to be written to one session but read from another.
This affected:
- iOS app chat (messages not appearing in history)
- TUI chat output (no assistant responses shown)
The fix uses `canonicalKey` from `loadSessionEntry()` instead of the raw `p.sessionKey` when building the `MsgContext`.
Fixes #2493
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes a session key mismatch in `chat.send` by using the canonical session key returned by `loadSessionEntry()` when constructing the `MsgContext`. This aligns `chat.send` with `chat.history`/session storage behavior so messages written during send are appended to the same session that history reads from.
The change is localized to the gateway RPC handler (`src/gateway/server-methods/chat.ts`) and affects how inbound chat messages are attributed to a session when dispatched into the auto-reply pipeline.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge; the change is small and fixes a real session key mismatch, with a remaining risk of inconsistent use of raw vs canonical keys in adjacent control-plane logic.
- The core fix (using `canonicalKey` for `MsgContext.SessionKey`) matches the session store’s canonicalization behavior and should resolve the reported history mismatch. Remaining concern is that other parts of `chat.send` still use the raw session key (send policy evaluation, abort/broadcast bookkeeping), which can lead to edge-case inconsistencies if callers mix alias and canonical keys.
- src/gateway/server-methods/chat.ts
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#14309: fix(ui): resolve chat event session key mismatch
by justonlyforyou · 2026-02-11
84.2%
#4567: Webchat: canonicalize main session key for /new (fix #4446)
by selfboot · 2026-01-30
83.9%
#10898: fix(mac): adopt canonical session key and add reset triggers
by Nachx639 · 2026-02-07
80.0%
#18694: fix(ui): reset session key to main after /new in webchat
by Phineas1500 · 2026-02-17
79.8%
#6850: fix: support direct channel:account:peer format in session key extr...
by toboto · 2026-02-02
79.3%
#17527: fix(gateway): allow WebChat to attach to main session regardless of...
by Glucksberg · 2026-02-15
78.7%
#19343: Refactor chat state management: reset chat messages and queue on se...
by saurav470 · 2026-02-17
78.4%
#18232: fix: webchat rapid messages create orphan sessions
by MisterGuy420 · 2026-02-16
78.2%
#11794: fix(gateway): validate session key ownership against authenticated ...
by coygeek · 2026-02-08
78.2%
#15564: fix: webchat messages disappear during concurrent session activity
by Automatedcapitalist · 2026-02-13
78.0%