#8507: fix: preserve accountId for multi-account agent-to-agent messaging
agents
stale
## Problem
When using `sessions_send` for agent-to-agent communication in a multi-account Telegram setup, announces from the target agent fail with:
```
Telegram bot token missing. Set TELEGRAM_BOT_TOKEN or channels.telegram.botToken.
```
This happens even when the account-level token is correctly configured at `channels.telegram.accounts.<id>.botToken`.
## Root Cause
When an agent runs via `sessions_send` (or other internal channel operations), `runAgentStep` calls the `agent` gateway method with `channel: INTERNAL_MESSAGE_CHANNEL` and `deliver: false`.
In `resolveAgentDeliveryPlan`, the `resolvedAccountId` was only being set from:
1. Explicit `params.accountId` (not passed by runAgentStep)
2. `baseDelivery.accountId` (only for implicit delivery mode)
Since internal channel operations don't use implicit delivery mode, the session's `lastAccountId` was lost, causing tools like `message` to not have the correct `agentAccountId` context.
## Fix
- Modified `resolveAgentDeliveryPlan` to fall back to `baseDelivery.lastAccountId` before checking delivery mode
- Updated Telegram token error messages to mention the account-level config path
## Testing
This fix enables multi-account announces where each agent has its own Telegram bot token:
```json5
{
channels: {
telegram: {
accounts: {
wren: { botToken: "wren-token" },
mogger: { botToken: "mogger-token" }
}
}
}
}
```
With this fix, when Mogger sends a `sessions_send` to Wren, Wren's announce will correctly use `wren-token` to post to Telegram.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes multi-account agent-to-agent messaging by preserving an account context when resolving an agent’s delivery plan, and it improves Telegram error messaging to include the account-scoped bot token config path.
The key behavioral change is in `resolveAgentDeliveryPlan` (`src/infra/outbound/agent-delivery.ts`), which now falls back to the session’s `lastAccountId` when no explicit `accountId` is provided, even for internal-channel operations where `deliver: false` is used. The Telegram tool updates (`src/agents/tools/telegram-actions.ts`) are purely user-facing error text changes to guide users toward `channels.telegram.accounts.<id>.botToken`.
<h3>Confidence Score: 3/5</h3>
- This PR is likely safe to merge, but the accountId fallback change may unintentionally affect explicit-target sends.
- The change is small and addresses a real missing-context bug, but `resolvedAccountId` now prefers `lastAccountId` even when `explicitTo` is provided, which appears to contradict an existing e2e test expectation and could change routing/account selection for explicit sends.
- src/infra/outbound/agent-delivery.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
#14359: fix: prefer named Telegram account over orphan 'default' in binding...
by itsGustav · 2026-02-12
81.7%
#20948: fix: propagate accountId from heartbeat delivery context to agent run
by odrobnik · 2026-02-19
81.4%
#8357: fix(gateway): preserve accountId across gateway restarts
by alfredo-feat-volky · 2026-02-03
81.2%
#8694: Fix Telegram routing when token override omits accountId
by codvik · 2026-02-04
80.8%
#8095: fix(sessions): include accountId in deliveryContext for outbound pe...
by codeslayer44 · 2026-02-03
80.4%
#13477: routing: normalize account ID matching for agent bindings
by davidahmann · 2026-02-10
79.1%
#9437: fix: normalize accountId in binding matching for consistent routing
by dbottme · 2026-02-05
79.1%
#14488: Fix Telegram default account listing and CLI cache-write usage mapping
by abdulbb · 2026-02-12
79.0%
#6007: fix(cron): auto-map agentId to accountId for Discord deliveries
by dwfinkelstein · 2026-02-01
78.8%
#6850: fix: support direct channel:account:peer format in session key extr...
by toboto · 2026-02-02
78.0%