Skip to content

fix(voip): pass Media Streams ticket via <Parameter>, not query string (#1073) - #1075

Merged
vybe merged 2 commits into
devfrom
feature/1073-voip-ticket-parameter
Jun 4, 2026
Merged

vybe merged 2 commits into
devfrom
feature/1073-voip-ticket-parameter

Conversation

@vybe

@vybe vybe commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Summary

  • VoIP outbound calls placed and rang after VoIP outbound call always 422: POST /api/agents/{agent_name}/voip/call path param is 'name' not 'agent_name' #1069, but dropped ~1s after answer with Twilio error 31920 (WebSocket handshake error). Root cause: the Media Streams auth ticket was passed in the <Stream url> query string, which Twilio does not forward on the Media Streams WebSocket. The handler saw ticket=None, closed before accept(), and the handshake 403'd.
  • Fix: pass the ticket as a Media Streams <Parameter> (Twilio surfaces these in the start event's customParameters), and reorder the handler to accept() first, then authenticate from the start frame. A query-string ?ticket= is retained as a fallback for non-Twilio/diagnostic clients.

Changes

  • src/backend/services/voip_service.py — build_stream_twiml: ticket → <Stream><Parameter name="ticket" value="…"/></Stream>; call_id stays in the path (no query string). Both url and value remain quoteattr-escaped.
  • src/backend/adapters/transports/twilio_media_stream.py — handle_media_stream: accept() first; resolve the ticket from the query string (fallback) or start.customParameters.ticket; scope-check (voip:{call_id}); GETDEL the staged intent; create the Gemini session. New _parse_start_frame + bounded _read_until_start (10s / 20 frames) so a silent client can't pin a worker; the start-frame streamSid is carried into the bridge.
  • tests/unit/test_voip_service.py — TwiML assertions rewritten for the <Parameter> form + a regression guard that no query-string ticket is ever emitted.
  • tests/unit/test_1073_voip_media_stream_ticket.py (new) — drives handle_media_stream with a Twilio-style start event: happy path, missing-ticket, wrong-scope, no-start-frame timeout, query-string fallback, plus the pure _parse_start_frame helper.
  • tests/registry.json, docs/memory/architecture.md — synced.

Test Plan

  • New + updated unit tests pass locally: pytest tests/unit/test_voip_service.py tests/unit/test_1073_voip_media_stream_ticket.py -v → 26 passed
  • Full in-container suite (CI) — couldn't run locally (Docker down); the host-only gap is the passlib/jose chain that affects test_1069/test_voice_auth collection on the bare host, unrelated to this change
  • Manual: place an outbound VoIP call on a VOIP_ENABLED instance behind a tunnel → call connects and stays up past answer (no Twilio 31920)

Fixes #1073

🤖 Generated with Claude Code

Eugene Vyborov and others added 2 commits June 4, 2026 16:14
#1073)

Twilio Media Streams does not forward the <Stream url> query string, so the
auth ticket arrived as None at the WS handler, which closed before accept() and
403'd the handshake (Twilio error 31920) — every outbound call dropped ~1s after
answer.

- build_stream_twiml: move the ticket out of the query string into a
  <Parameter name="ticket"> child of <Stream> (Twilio surfaces these in the
  `start` event's customParameters); keep call_id in the path for scope checks.
- handle_media_stream: accept() FIRST, then read start.customParameters.ticket,
  scope-check, GETDEL the staged intent, and create the Gemini session. A
  query-string ?ticket= is still honored as a fallback for non-Twilio/diagnostic
  clients. The start-frame read is bounded (10s / 20 frames) so a silent client
  can't pin a worker; the streamSid from that frame is carried into the bridge.
- tests: rewrite the TwiML assertions for the <Parameter> form (+ a regression
  guard that no query-string ticket is ever emitted) and add
  test_1073_voip_media_stream_ticket.py exercising the handler with a
  Twilio-style start event (happy path, missing/wrong-scope ticket, no-start
  timeout, query-string fallback) plus the pure _parse_start_frame helper.
- docs: sync architecture.md VoIP transport description + endpoint table.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Both files exec a backend module at import time with heavy leaf deps stubbed
into sys.modules and restored synchronously in the loader's finally block.
That uses bare sys.modules.update/pop/[]= — which tests/lint_sys_modules.py
(#762) bans, so `lint (sys.modules pollution check)` went red (and the dev
push run already caught #1069's copy, the #802 post-merge scenario).

Adopt the sanctioned exemption (precedent: test_telegram_webhook_backfill.py):
a top-level `_STUBBED_MODULE_NAMES` list + an autouse `_restore_sys_modules`
fixture that snapshot/restores those keys around each test. The fixture pair
exempts the whole file from the lint while adding a real belt-and-suspenders
restore guard; the existing import-time stub/restore logic is unchanged.

- lint_sys_modules.py: no new violations (was +3 each for #1069 and #1073).
- test_1073 + test_voip_service: 26 passed locally.
- test_1069 collects/passes in the CI container (host-only passlib gap is
  pre-existing and unrelated to this change).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@vybe
vybe merged commit cfee275 into dev Jun 4, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant