Skip to content

[AI-3805] Point new Twilio numbers at the signed call webhook - #2474

Merged
kanat merged 12 commits into
mainfrom
kanat/ai-3805-twilio-numbers-use-hook
Nov 25, 2026
Merged

kanat merged 12 commits into
mainfrom
kanat/ai-3805-twilio-numbers-use-hook

Conversation

@kanat

@kanat kanat commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Piece 2/4 of AI-3751 (AI-3805). Depends on #2433 (merged). Numbers attached before this PR move in AI-3806; the trunk IP allowlist is AI-3807.

Why

#2433 added the router's signed Twilio call webhook, which runs the pre-answer checks (suspension, money) and answers with the number's trunk in the response body. No number pointed at it yet. With this PR a newly attached Twilio number points at it.

How it works

POST /v1/phone/numbers/{n}/attach  ->  phone.Service.Attach
 ├─ no keyring (Twilio now asks_for_trunk, as Telnyx) ──────────────> refused (ErrNoSealer)
 ├─ attached already in this app ──────────────────────────────────> answered as before, not re-pointed
 ├─ provider.(CallHooked).CallHookReady(ROUTER_PUBLIC_URL)            (before anything is made)
 │    ├─ no TWILIO_WEBHOOK_AUTH_TOKEN ────────────────────────────> refused, not_configured
 │    ├─ no ROUTER_PUBLIC_URL ────────────────────────────────────> refused, not_configured
 │    └─ not a bare http(s) address (user, ?, #) ─────────────────> refused, not_configured
 ├─ Stream trunk + rule
 └─ finishAttach -> twilio.ConfigureInbound(Inbound{E164, Bridge, PublicURL})
      └─ POST IncomingPhoneNumbers/<sid>.json
           VoiceUrl    = ROUTER_PUBLIC_URL + /v1/phone/hooks/twilio   (no #rp=/#rt= overrides)
           VoiceMethod = POST

No TWILIO_ACCOUNT_SID: the Twilio provider cannot be opened, so attach is refused as before (tested).

before after
new Twilio number's voice URL inline TwiML, GET <ROUTER_PUBLIC_URL>/v1/phone/hooks/twilio, POST
what Twilio is told the trunk's dial instruction only the webhook URL
attach without token / public URL / keyring attached refused, nothing made at Stream or Twilio
Telnyx attach TeXML app unchanged (control: same requests as base 52435fe)

Callers of ConfigureInbound: Service.finishAttach (production), the telephony-latency bench (it now serves the same webhook itself on -hook-listen, signed with TWILIO_WEBHOOK_AUTH_TOKEN, and dials the bench), the Twilio integration test.

phone.TwilioCallHookPath is the one path the provider and the API server use. The phone CLI's attach has no keyring, so it now refuses Twilio numbers as it already refused Telnyx ones.

Deploy gate

Before the staging deploy: staging must set TWILIO_WEBHOOK_AUTH_TOKEN and ROUTER_PUBLIC_URL, else new Twilio attaches are refused.

Tests

  • internal/phone/twilio: voice URL is the hook on the public URL, POST, no fragment/query/user, no trunk value in anything sent; refused without the token (from options or TWILIO_WEBHOOK_AUTH_TOKEN) with no request to Twilio; a table of 14 public URL shapes.
  • internal/api TwilioHookSuite (real Twilio provider against a recording fake): attach, then a call signed over the voice URL Twilio was given is answered at it with the trunk; each missing setting (token, public URL, account SID, keyring) refuses with nothing at Twilio or Stream and the number unattached; an attached number re-attached without the settings still answers; Telnyx control.
  • evals/telephony-latency: the bench's webhook (signed call dials the bench, unsigned 401, bad form or too large 400, unknown number hangs up) and its server lifecycle.
  • Mutations: 28 mutants (every guard in the diff, the asks_for_trunk line, the hook path, the POST method, the check order and placement), all killed; the bench's run wiring is checked by build only (it needs real carriers).

🤖 Generated with Claude Code

kanat and others added 7 commits November 25, 2026 10:35
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[security R1] VERDICT: GO

Reviewed against the repo's security skill (go.md rules 5, 6, 7, 9) for the attach path, the voice URL given to Twilio, and the bench's call webhook.

  • Voice URL: only VoiceUrl (public URL + /v1/phone/hooks/twilio) and VoiceMethod=POST are sent; no trunk value, no user, query or fragment can reach it. Tests assert the exact form.
  • Attach refusal: every missing setting maps to the same fixed not_configured answer; setting names stay in the log.
  • Bench hook: body bounded, signature checked over a fixed URL before acting, refuses to start without a token, answers only the bench's own SIP URI.
  • Mutations: 5 of the new guards removed in turn (URL character check, fragment added, service pre-check, signed flag, bench signature): the PR's tests fail for each.

One Nit inline (S1.1).

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: NO-GO

Round 1 (main review) at b1e097c (it already contains main 48e5f96). The PR is a draft, so CI has not run: go and integration are skipped. They run at gh pr ready.

What I ran on the merged tree:

  • build, vet (unit and integration tags) and gofmt: clean.
  • Unit tests: internal/phone/..., evals/telephony-latency, twiliosig, metered, cmd/phonecheck. All pass.
  • Integration tests: internal/phone/..., plus internal/api with TwilioHook, TelnyxHook, Phone, CallStatus, TwilioEvents, TwilioHookSpec, SIPTrunks and CallHook. All pass: 247 PASS, 0 FAIL.
  • generated.sh: in step.

Checked and sound:

  • The voice URL given to Twilio is the URL the hook checks the signature over. Both are built from the same ROUTER_PUBLIC_URL and phone.TwilioCallHookPath. A query, fragment or user is refused, and a path prefix gives the same URL on both sides. The e2e test signs over the voice URL Twilio was given and gets the Dial back.
  • Telnyx attach is the same as on base. I ran the control test on base 48e5f96 and got the same requests. Only the Twilio provider reads Inbound.PublicURL or implements CallHooked.
  • A number attached before this PR is not re-pointed. Its repeat attach returns before the check and Twilio is asked nothing.
  • The bench webhook listens on all interfaces on purpose, because Twilio must reach it. It checks the signature, caps the body and answers only with the bench's own SIP URI.

Mutations: I checked 13 mutants and 1 survived (R1.4, the bench's wiring, which needs real carriers).

The verdict is NO-GO for R1.1: the phonecheck steps cannot give the local router the tunnel URL before a Twilio number is attached. R1.2 and R1.4 are Nits and R1.3 is a Question. Those three go to tickets and do not block the merge.

Comment thread .claude/skills/phonecheck/SKILL.md Resolved
Comment thread acceleration/internal/phone/service.go Resolved
kanat and others added 3 commits November 25, 2026 11:26
…pts in

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The readiness check runs before the call hook is pointed. The refusal table also covers a plain http public URL.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ilio attach

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: GO

Delta round 2 on 215abc0, merged onto main 0cd453c. Touched suites pass (unit phone/..., twiliosig, metered, evals/telephony-latency; integration phone/... and api TwilioHookSuite, PhoneSuite, TelnyxHook: 95 PASS, 0 FAIL). CI is a draft: Analyze, CodeQL and GitGuardian pass; go and integration are skipped.

Prior findings

  • S1.1 (https only) fixed. A router's Twilio voice URL must be https; Options.PlainHTTPHook is set only by evals/telephony-latency/main.go:328, and the router's one constructor (phone/vendors/vendors.go:25) passes Options{}, so no router path can turn it on. Mutants "accept http again" and "opt-in always on" are killed by the twilio unit tests and the api TwilioHookSuite row.
  • R1.2 fixed. The readiness check runs before the app's call hook is written when the number is not yet attached here. The refusal test now asserts the app's event hooks stay empty. Mutants "old order" and "check also for an attached number" are killed.
  • R1.1 fixed. Read in order: the tunnel URL is known in Do step 1, the router is restarted with ROUTER_PUBLIC_URL=<tunnel URL> before any Twilio attach, the new process id goes into the state file (cleanup kills it), and cleanup says a kept Twilio number points at the closed tunnel. The line about voice URLs now covers only numbers attached before the call webhook existed.
  • R1.3 (Redis) not done here: needs a new ServiceOptions field and wiring in three commands, so it goes to a ticket by the orchestrator's decision. The README row already says every call is hung up without Redis.

New findings (Nits)

  • acceleration/internal/phone/twilio/twilio_test.go:535: the comment still says "a bare http(s) address is taken"; only https is taken now.
  • acceleration/README.md:163: the TWILIO_WEBHOOK_AUTH_TOKEN row does not say the public URL must be https for a Twilio attach.

Unverified: that staging and prod ROUTER_PUBLIC_URL are https (deploy config is not in this repo). With an http URL the attach is refused loudly and nothing is written.

…he bare-address test comment

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
@kanat
kanat marked this pull request as ready for review November 25, 2026 16:42
@kanat
kanat merged commit ef17812 into main Nov 25, 2026
13 checks passed
@kanat
kanat deleted the kanat/ai-3805-twilio-numbers-use-hook branch November 25, 2026 18:36
kanat added a commit that referenced this pull request Nov 25, 2026
Piece 3/4 of [AI-3751](https://linear.app/stream/issue/AI-3751)
([AI-3806](https://linear.app/stream/issue/AI-3806)). Depends on #2474
(merged).

## Why

Since #2474 a newly attached Twilio number points at the router's signed
call webhook. Numbers attached earlier keep their old voice URL. This PR
adds the mechanism that moves earlier numbers to the signed call
webhook. **Nobody runs it in this PR**: running it on staging or prod is
a separate, gated step (dry run, one number, then all).

## Changes

- `store.PhoneNumbers.Attached`: read-only, paged by id, every
customer's attached numbers of one vendor.
- `phone.Service.MoveToCallHook` and `twilio.Provider.OnCallHook` (reads
the number's `voice_url` and `voice_method`).
- `router phone move-twilio`, built from the router's own config
(database, keyring, `ROUTER_PUBLIC_URL`, Twilio credentials).
- A runbook paragraph in `acceleration/README.md`.

## How it works

```
router phone move-twilio [--apply] [--one <e164>] [--app <stream app id>]
 └─ phone.Service.MoveToCallHook(vendor=twilio)
     ├─ no keyring ─────────────────────────────> refused   (nothing read at Twilio)
     ├─ no TWILIO_ACCOUNT_SID ──────────────────> refused
     ├─ vendor not CallHooked (telnyx, …) ──────> refused
     ├─ CallHookReady: token, https public URL ─> refused
     └─ store.PhoneNumbers.Attached (twilio, held, attached; by id, 100 a page)
         └─ each number
             ├─ no stored bridge ───────────────> "no stored bridge, needs a re-attach" (Twilio not asked)
             ├─ stored password does not open ──> run stops, names the number
             ├─ Twilio: VoiceUrl = hook, POST ──> "already moved"
             ├─ no --apply ─────────────────────> "would move"   (only a GET at Twilio)
             └─ ConfigureInbound (as attach) ───> "moved"        (POST VoiceUrl, VoiceMethod=POST)
```

Re-attach shortcut: `Attach` of a Twilio number already attached in this
app now runs the same per-number step, so an app can move one number
itself. It runs only when the router can answer the webhook; the move
runs under a 5 second deadline, and a failure or a timeout is logged and
the attach still answers, as the call hook repair beside it.

| | before | after |
|---|---|---|
| number attached before #2474 | old voice URL | moved by the command,
or by attaching it again |
| re-attach of an attached Twilio number | Twilio not asked | one GET;
one POST only when not on the webhook |
| output | - | one line a number: e164, Stream app, outcome; no
credentials |
| Telnyx numbers | - | never listed, never asked |

## Validation

- `go vet ./...` and `go vet -tags integration ./...` at the base
22120e7; unit `internal/phone/...`, `internal/store`,
`internal/metered`, `internal/twiliosig`, `cmd/router`; integration
`internal/phone/...`, `internal/api` Twilio/Telnyx/Phone suites,
`cmd/router` `PhoneMoveSuite` + `OptOutsCLISuite`: pass.
`internal/store` `TestEveryHotTableIsReadByIndex` (requests and
sandboxes plans) fails the same way on base 22120e7 with the same
database: not from this PR.

- `cmd/router` `PhoneMoveSuite` (Postgres, fake Stream, a fake Twilio
that keeps each number's voice URL and method): dry run writes nothing;
`--apply` writes each number once and a second run writes none; a number
on the hook by GET is moved; `--one`, `--app`; past one page (101
numbers), each reported once; each missing setting refuses before any
number is read; a run stops at the first number it could not move
(password that does not open, number not held, update refused) and moves
none after it; Telnyx never touched; bad flags refused; re-attach moves
the number, writes nothing the second time, and never fails over the
move; the command built from the router's config runs the same checks.
- `internal/store`: `Attached` filters (vendor, held, attached, app,
number) and pages.
- `internal/phone/twilio`: `OnCallHook` only for the hook URL by POST;
not held is an error; no token asks nothing.
- Mutations: 49 mutants over every added guard, filter, constant and
wiring line, in two batches: 43 killed. 6 left: 4 are error paths whose
next step fails with another message (a store read error, a Twilio list
error) or the same result (`released_at IS NULL`, since a release clears
`attached_at`; the page size), and 2 are lines moved unchanged from
`HoldsNumber`.

## Runbook (gated, not run here)

`acceleration/README.md` says how: no flag (list), then `--apply --one
<e164>` and call it, then `--apply`. Run it from the image the router
runs (same tag): it opens the database as the router does, which
migrates it unless a migrate DSN is set.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant