From 92095ff5d6ae69b6296b6684c3cb37788410b2d8 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Sat, 29 Aug 2026 14:01:40 +0500 Subject: [PATCH] fix: release reserved L2 agent when AMI redirect fails during escalation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Found during production smoke test: if the AMI Redirect call in create_escalation() raises (channel gone, AMI hiccup), the agent stays RESERVED forever with no owning call — orphaned out of the pool until someone fixes it by hand. Now releases the agent and marks the escalation failed before re-raising as a 502. --- services/asterisk_bridge_service/voice_ai.py | 28 +++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/services/asterisk_bridge_service/voice_ai.py b/services/asterisk_bridge_service/voice_ai.py index 615ff82..97f2958 100644 --- a/services/asterisk_bridge_service/voice_ai.py +++ b/services/asterisk_bridge_service/voice_ai.py @@ -721,15 +721,25 @@ def create_escalation(call_id: str, body: EscalationRequestIn, actor: dict) -> E session.commit() raise HTTPException(status_code=409, detail=f"No available {body.target_level} agent right now") - bridge._ami_action( - "Redirect", - { - "Channel": channel, - "Context": bridge._transfer_context(), - "Exten": reserved_extension, - "Priority": 1, - }, - ) + try: + bridge._ami_action( + "Redirect", + { + "Channel": channel, + "Context": bridge._transfer_context(), + "Exten": reserved_extension, + "Priority": 1, + }, + ) + except Exception as exc: + try: + bridge._release_routing_agent(call_id) + except Exception: + LOGGER.warning("bridge.escalation_release_agent_failed call_id=%s", call_id) + escalation.status = "failed" + escalation.completed_at = utc_now_iso() + session.commit() + raise HTTPException(status_code=502, detail=f"Failed to redirect call to agent: {exc}") from exc escalation.status = "ringing" escalation.assigned_agent_id = reserved_extension