#6517: fix(matrix): pass accountId through outbound chain to resolveMatrixClient
channel: matrix
Cluster:
Multi-Account Matrix Support
## Bug Description
When using the `message` tool without specifying `accountId`, messages were sent from the wrong Matrix account. Instead of using the accountId from the session context, it would default to the last-started Matrix account (which ends up being alphabetically last).
## How to Reproduce
1. Configure multiple Matrix accounts (e.g., `sam` and `yessica`)
2. Start the gateway (accounts start in alphabetical order)
3. Send a message via the Matrix channel as agent `sam` without specifying accountId
4. Message is sent from `yessica` instead of `sam`
## Root Cause
The `accountId` parameter was not being passed through the chain:
- `outbound.ts` → `send.ts` → `send/client.ts` → `resolveMatrixClient()`
When `resolveMatrixClient()` doesn't receive an `accountId`, it falls back to `getActiveMatrixClient()` which returns whichever client was set last (typically the last account to start, alphabetically).
## The Fix
Three files updated to pass `accountId` through the chain:
1. **`extensions/matrix/src/outbound.ts`** — Extract `accountId` from outbound context and pass to `sendMessageMatrix`
2. **`extensions/matrix/src/matrix/send.ts`** — Pass `opts.accountId` to `resolveMatrixClient` calls
3. **`extensions/matrix/src/matrix/send/client.ts`** — Accept `accountId` parameter and pass to `resolveSharedMatrixClient` and `createMatrixClient`
## Testing
- TypeScript compilation passes (`npx tsc --noEmit`)
- Changes are minimal and follow the existing pattern used by other channels (Telegram, Discord, etc.)
- The `accountId` field already existed in `MatrixSendOpts` and `ChannelOutboundContext` types, it just wasn't being threaded through
## Related
This matches how other multi-account channels handle routing. The session context already contains the correct `accountId` from `resolveAgentRoute()`, it just wasn't being used during outbound message delivery.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR threads `accountId` through Matrix outbound message sending so `resolveMatrixClient()` can select the right Matrix client instead of implicitly using whichever client was last started.
Changes are localized to the Matrix extension: the outbound adapter now forwards `accountId` into `sendMessageMatrix`, and `sendMessageMatrix`/`sendPollMatrix` forward it into `resolveMatrixClient`, which in turn passes it to shared/per-send client creation.
<h3>Confidence Score: 3/5</h3>
- Mergeable, but there are a couple of correctness gaps that can still cause wrong-account sends in multi-account setups.
- The intent is correct and changes are small, but `resolveMatrixClient()` still returns `getActiveMatrixClient()` before considering `accountId`, and `sendPoll` doesn’t forward `accountId`, so the reported bug may persist in some paths.
- extensions/matrix/src/matrix/send/client.ts, extensions/matrix/src/outbound.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</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
#23333: fix(matrix): add accountId routing for multi-account message sending 🤖
by BadTurki · 2026-02-22
88.9%
#21105: feat(matrix): multi-account support
by omnibot1985 · 2026-02-19
81.9%
#13013: feat(matrix): multi account support
by ti · 2026-02-10
81.2%
#11869: feat: Multi-account Matrix plugin with accountId routing
by jacoblyles · 2026-02-08
80.6%
#8507: fix: preserve accountId for multi-account agent-to-agent messaging
by djh58 · 2026-02-04
77.6%
#13057: feat(matrix): add sessionScope=room to route sessions by roomId
by spengrah · 2026-02-10
75.2%
#8095: fix(sessions): include accountId in deliveryContext for outbound pe...
by codeslayer44 · 2026-02-03
74.4%
#14227: fix(matrix): pass asVoice to audioAsVoice for native voice messages
by tfm-neo-ai · 2026-02-11
74.0%
#8357: fix(gateway): preserve accountId across gateway restarts
by alfredo-feat-volky · 2026-02-03
74.0%
#16940: fix(matrix): send voice messages as native voice bubbles
by zerone0x · 2026-02-15
73.4%