#8879: fix(sessions-spawn): resolve target agent's bound accountId for subagent gateway calls
agents
stale
Cluster:
Session Management Enhancements
## Summary
Builds on top of a13efbe2b (`fix: pass threadId/to/accountId from parent to subagent gateway call`) which correctly forwards requester context (threadId, to, accountId) so sub-agents reply to the correct thread/topic.
This PR adds **binding-aware accountId resolution** so that when a sub-agent has its own bot identity configured via `bindings`, it uses that identity instead of inheriting the parent's.
**Example scenario:**
- Aria (main agent, bound to `aria-bot`) spawns Rex (content agent, bound to `rex-bot`) via `sessions_spawn`
- Rex needs to send a Telegram message back to the user
- **Before (a13efbe2b only):** Rex replies through Aria's bot (correct thread, wrong identity)
- **After (this PR):** Rex replies through Rex's own bot (correct thread, correct identity)
The two fixes are complementary:
| Fix | Solves |
|-----|--------|
| a13efbe2b (upstream) | Sub-agent replies land in the correct forum topic/thread |
| This PR | Sub-agent replies come from the correct bot identity |
## Changes
1. **Added `resolveAgentBoundAccountId()`** in `src/routing/bindings.ts`
- Looks up an agent's bound accountId for a given channel from config bindings
- Follows the same pattern as existing `resolveDefaultAgentBoundAccountId()`
2. **Updated `sessions-spawn-tool.ts`**
- Resolves the target agent's bound accountId before the gateway call
- Uses `targetAccountId ?? requesterOrigin?.accountId` — binding wins when configured, parent context is the fallback
- Preserves upstream's `to` and `threadId` passthrough
3. **Added tests** in `sessions-spawn-bound-accountid.test.ts`
- Target agent's bound accountId takes priority over requester's
- Falls back to requester's accountId when no binding exists
- Skips resolution for cron/hook spawns (no channel context)
## Test plan
- [x] `pnpm build` passes
- [x] `pnpm check` passes
- [x] 3 new unit tests pass (binding priority, fallback, cron/hook)
- [ ] Manual testing with multi-agent setup (spawning agent with separate Telegram binding)
Most Similar PRs
#16249: fix(sessions): allow cross-agent session paths in multi-agent bindings
by 0xbrak · 2026-02-14
73.6%
#22205: fix: expose resolvedTo as currentChannelId so sub-agents inherit th...
by Galygious · 2026-02-20
73.4%
#15727: fix(routing): resolve channel default account instead of hardcoded ...
by FuseWeb · 2026-02-13
72.3%
#15792: fix: pass agentId to resolveSessionFilePath in additional call sites
by MisterGuy420 · 2026-02-13
71.6%
#8357: fix(gateway): preserve accountId across gateway restarts
by alfredo-feat-volky · 2026-02-03
71.3%
#13477: routing: normalize account ID matching for agent bindings
by davidahmann · 2026-02-10
70.9%
#22982: fix: prevent stale threadId from routing subagent announces to wron...
by unboxed-ai · 2026-02-21
70.8%
#8507: fix: preserve accountId for multi-account agent-to-agent messaging
by djh58 · 2026-02-04
70.4%
#23656: fix(routing): trust binding agentId even when not in agents.list
by SleuthCo · 2026-02-22
70.1%
#9437: fix: normalize accountId in binding matching for consistent routing
by dbottme · 2026-02-05
70.0%