feat: no-answer retry, agent status machine, escalation events/timeline (ТЗ §13-15,22,25-27,37, AC-08,16-19)
Phase 2 of the L1->L2 routing engine (Phase 1: MR!4). - ami_loop() now also captures native AMI DialEnd/Hangup frames (not only UserEvent), needed to detect that an escalated agent did not answer. No dialplan change required - Redirect already routes the client channel into an existing Dial()-based transfer context, so Asterisk emits these events on its own; the listener just wasn't reading them before. - retry_escalation_no_answer(): on NOANSWER/BUSY/CANCEL/CHANUNAVAIL/ CONGESTION, releases the non-answering agent, excludes it, and reserves+redirects to the next available agent via the routing engine's existing exclude_agent_ids support. Exhausted pool marks the escalation failed and leaves the call with the AI instead of dropping the client (ТЗ §32). - Agent status now actually moves through RESERVED -> RINGING -> TALKING -> AFTER_CALL_WORK -> AVAILABLE instead of staying stuck on RESERVED for the whole call; a new acw_sweep_loop background thread (same pattern as the existing failed_retry_loop) times out AFTER_CALL_WORK back to AVAILABLE. - escalations gains attempt_count/real_agent_id/attempted_agent_ids_json (migration 0033); fixes a latent bug where assigned_agent_id stored the SIP extension instead of the real agent_id despite routing-service already returning it in RoutingAgentReserveOut. - Every transition now records an interaction timeline entry and publishes the ТЗ §25 event catalog (AgentReserved/AgentRinging/ AgentNoAnswer/AgentConnected/TransferCompleted/TransferFailed) through the existing emit_voice_event/EventOutboxRow idempotent path. Not in this MR (see plan): SLA config, Callback, L3 (needs real technical agents from the business), metrics.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
import json
|
||||
|
||||
from sqlalchemy import select
|
||||
@@ -134,3 +135,50 @@ def test_reserve_agent_excludes_disabled_and_excluded_ids():
|
||||
assert reserved.agent_id != excluded.agent_id
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
|
||||
def test_set_agent_status_transitions_ringing_to_talking():
|
||||
init_sql_schema()
|
||||
session = get_session()
|
||||
try:
|
||||
agent = _make_agent(session, level="L2")
|
||||
routing_engine.reserve_agent(session, call_id="call-status", level="L2", tenant_id=None, required_skills=[])
|
||||
|
||||
ringing = routing_engine.set_agent_status(session, agent_id=agent.agent_id, status="RINGING")
|
||||
assert ringing is not None
|
||||
assert ringing.status == "RINGING"
|
||||
|
||||
talking = routing_engine.set_agent_status(session, agent_id=agent.agent_id, status="TALKING")
|
||||
assert talking.status == "TALKING"
|
||||
|
||||
missing = routing_engine.set_agent_status(session, agent_id="unknown-agent", status="AVAILABLE")
|
||||
assert missing is None
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
|
||||
def test_sweep_after_call_work_releases_only_expired_agents():
|
||||
init_sql_schema()
|
||||
session = get_session()
|
||||
try:
|
||||
stale = _make_agent(session, level="L2", status="AFTER_CALL_WORK")
|
||||
fresh = _make_agent(session, level="L2", status="AFTER_CALL_WORK")
|
||||
|
||||
stale_ts = (datetime.now(timezone.utc) - timedelta(seconds=120)).replace(microsecond=0).isoformat()
|
||||
stale.updated_at = stale_ts
|
||||
stale.current_call_id = "call-stale"
|
||||
session.commit()
|
||||
|
||||
swept = routing_engine.sweep_after_call_work(session, older_than_seconds=30)
|
||||
swept_ids = {a.agent_id for a in swept}
|
||||
|
||||
assert stale.agent_id in swept_ids
|
||||
assert fresh.agent_id not in swept_ids
|
||||
|
||||
session.refresh(stale)
|
||||
session.refresh(fresh)
|
||||
assert stale.status == "AVAILABLE"
|
||||
assert stale.current_call_id is None
|
||||
assert fresh.status == "AFTER_CALL_WORK"
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
Reference in New Issue
Block a user