← Back to PRs

#6517: fix(matrix): pass accountId through outbound chain to resolveMatrixClient

by saxyguy81 open 2026-02-01 19:00 View on GitHub →
channel: matrix
## 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