#8352: fix(gateway): include clientRunId in agent event payloads
gateway
stale
## Problem
Agent event payloads broadcast via WebSocket don't include `clientRunId` when it differs from the internal `runId`. This causes frontends to potentially drop or misroute events in scenarios where IDs diverge (e.g., isolated sub-agent sessions via `sessions_spawn`).
## Solution
Include `clientRunId` in the broadcast payload when present and different from `runId`. This is a no-op for typical webchat sessions where IDs match, but enables correct event routing for spawned sessions.
## Changes
- 8 lines in `src/gateway/server-chat.ts`
## Testing
Verified with sub-agent spawning — events now correctly correlate to the originating session.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Updates the gateway’s agent-event broadcast payload construction to include `clientRunId` (when it differs from `evt.runId`) alongside the existing `sessionKey`. This allows web frontends to correctly correlate events for spawned/isolated sub-agent sessions where the internal run ID and the client-facing run ID diverge, while remaining a no-op for typical sessions where IDs match.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- Change is small, localized to payload shaping, and guarded so `clientRunId` is only added when present and different from `runId`; existing behavior is unchanged for normal sessions. No type/logic hazards were identified in the modified code path.
- No files require special attention
<!-- greptile_other_comments_section -->
<sub>(4/5) You can add custom instructions or style guidelines for the agent [here](https://app.greptile.com/review/github)!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#14811: feat(gateway): route chat/agent events per-connection instead of glob…
by jiangjin11 · 2026-02-12
77.5%
#15792: fix: pass agentId to resolveSessionFilePath in additional call sites
by MisterGuy420 · 2026-02-13
76.9%
#7846: feat: add agentId param to webhook agent endpoint
by Bre77 · 2026-02-03
76.0%
#15982: fix: pass agentId to resolveSessionFilePath in reply flow (NX-003)
by automagik-genie · 2026-02-14
75.8%
#8598: fix: emit agent events for CLI agents targeting webchat
by dbottme · 2026-02-04
75.8%
#3517: fix: trigger agent response for webchat sessions after restart
by dovewars · 2026-01-28
75.7%
#16949: fix(gateway): deliver chat:final even when sessionKey is unresolved (…
by ekleziast · 2026-02-15
75.1%
#14309: fix(ui): resolve chat event session key mismatch
by justonlyforyou · 2026-02-11
74.8%
#10281: fix(infra): clear seqByRun entry when agent run context is cleared
by programming-pupil · 2026-02-06
74.8%
#10608: fix(agents): honor spawn model override in gateway and session spaw...
by 1kuna · 2026-02-06
74.7%