fix: reuse background context instead of AgentContext.first()

linuztx committed Mar 18, 2026 at 00:08 UTC f840b8489d5cc8ca2d4644c140222fa710de4621
1 file changed +8 -2
plugins/_email_integration/helpers/handler.py
+8 -2
@@ -143,8 +143,14 @@ async def _fetch_exchange(
143 async def _dispatch_all(handler_cfg: dict, messages: list[InboundMessage]):
144 own_address = (handler_cfg.get("username") or "").lower()
145
146 - # Need an agent for dispatcher AI calls — use first available or create temp
147 - ctx = AgentContext.first()
146 + # Need an agent for dispatcher AI calls
147 + # find existing dispatcher or create new background context
148 + ctx = None
149 + for c in AgentContext._contexts.values():
150 + if isinstance(c, AgentContext) and c.name == "Email Dispatcher":
151 + ctx = c
152 + break
153 +
154 if not ctx:
155 agent_config = initialize_agent()
156 ctx = AgentContext(agent_config, name="Email Dispatcher",