Repository navigation
fix(voip): pass Media Streams ticket via <Parameter>, not query string (#1073) - #1075
Merged
Merged
Conversation
#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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
<Stream url>query string, which Twilio does not forward on the Media Streams WebSocket. The handler sawticket=None, closed beforeaccept(), and the handshake 403'd.<Parameter>(Twilio surfaces these in thestartevent'scustomParameters), and reorder the handler toaccept()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_idstays in the path (no query string). Bothurlandvalueremainquoteattr-escaped.src/backend/adapters/transports/twilio_media_stream.py—handle_media_stream:accept()first; resolve the ticket from the query string (fallback) orstart.customParameters.ticket; scope-check (voip:{call_id});GETDELthe 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; thestart-framestreamSidis 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) — driveshandle_media_streamwith a Twilio-stylestartevent: happy path, missing-ticket, wrong-scope, no-start-frame timeout, query-string fallback, plus the pure_parse_start_framehelper.tests/registry.json,docs/memory/architecture.md— synced.Test Plan
pytest tests/unit/test_voip_service.py tests/unit/test_1073_voip_media_stream_ticket.py -v→ 26 passedpasslib/josechain that affectstest_1069/test_voice_authcollection on the bare host, unrelated to this changeVOIP_ENABLEDinstance behind a tunnel → call connects and stays up past answer (no Twilio 31920)Fixes #1073
🤖 Generated with Claude Code