#18694: fix(ui): reset session key to main after /new in webchat
app: web-ui
stale
size: S
## Summary
Fixes #18078
When a user sends `/new` in the webchat Control UI, the server creates a new session and the chat run completes. The UI refreshes the sessions sidebar via `loadSessions()`, but **never resets `host.sessionKey`** back to the main session. This means subsequent messages are still routed to whichever sidebar session was previously selected — not the newly created main session.
- After `/new` completes (state `"final"` + `refreshSessionsAfterChat` flag), reset `host.sessionKey` to the main session key from `hello.snapshot.sessionDefaults`
- Also call `setLastActiveSessionKey()` so the setting persists across page reloads
- Falls back to `"agent:main:main"` if no session defaults are available
## Test plan
- [x] Added unit test in `app-gateway.node.test.ts` verifying `sessionKey` resets to `mainSessionKey` after a `/new` chat run completes
- [x] All 4 existing + new gateway tests pass (`pnpm vitest run --config vitest.node.config.ts src/ui/app-gateway.node.test.ts`)
- [ ] Manual: Open Control UI → select a sidebar session → send `/new` → verify the session key resets and next message goes to the main session
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Fixes webchat Control UI bug where `sessionKey` wasn't reset after `/new` command, causing subsequent messages to route to previously-selected sidebar sessions instead of the main session.
**Key changes:**
- Added logic in `app-gateway.ts:227-237` to reset `host.sessionKey` and persist via `setLastActiveSessionKey()` when a `/new` chat completes (when `refreshSessionsAfterChat` flag is set)
- Falls back to `"agent:main:main"` if `sessionDefaults.mainSessionKey` is unavailable
- Added unit test verifying `sessionKey` resets to main after `/new` completes
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- Clean, focused bug fix that addresses a specific routing issue. The implementation properly integrates with existing session management patterns, includes a test verifying the core behavior, and has a sensible fallback value. The change is isolated to the `/new` command flow and doesn't affect other chat functionality.
- No files require special attention
<sub>Last reviewed commit: e45ff4f</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#4567: Webchat: canonicalize main session key for /new (fix #4446)
by selfboot · 2026-01-30
84.6%
#14309: fix(ui): resolve chat event session key mismatch
by justonlyforyou · 2026-02-11
82.2%
#17527: fix(gateway): allow WebChat to attach to main session regardless of...
by Glucksberg · 2026-02-15
81.7%
#19343: Refactor chat state management: reset chat messages and queue on se...
by saurav470 · 2026-02-17
81.7%
#3182: fix(gateway): use canonical session key in chat.send
by chrisherold · 2026-01-28
79.8%
#9218: Fix Control UI chat resync on gaps and terminal events
by figitaki · 2026-02-05
79.5%
#6853: fix: fire internal hooks on sessions.reset RPC (TUI/webchat /new)
by hamiltonchua · 2026-02-02
78.9%
#22798: feat(webchat): ChatGPT-style multi-chat threads with generated titles
by opnsec · 2026-02-21
78.8%
#10898: fix(mac): adopt canonical session key and add reset triggers
by Nachx639 · 2026-02-07
78.1%
#7537: fix(sessions): reset token counts on /new for channel sessions
by SoniAssist · 2026-02-02
77.9%