Skip to content

perf(listen): close idle Soniox sockets sooner - #22365

Merged
Git-on-my-level merged 4 commits into
mainfrom
lane/opus-soniox-idle
Oct 19, 2026
Merged

Git-on-my-level merged 4 commits into
mainfrom
lane/opus-soniox-idle

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Why

Soniox bills a real-time stream for every second it is connected, keepalive silence included. On 2026-10-18, 2,857 of 3,448 billed Soniox hours carried no audio ($171 of $238), per opus-soniox-attribution.md. One part of that is sockets kept open while the client sends no frames at all: an app in the background, a disconnected pendant, or a socket the client left open.

Today's idle close never fires for those sockets, because only VAD output drives it, and VAD only runs when frames arrive.

What

The PR was re-scoped after review. The VAD-silence policy is unchanged from prod. The PR adds a no-frames close and measurement.

Knob Prod before This PR (code default; env override kept)
SONIOX_IDLE_CLOSE_SECONDS (VAD silence) 45 (prod pin; code default off) 45 as the code default; the pin is no longer needed
SONIOX_IDLE_REARM_SECONDS 60 (hard floor 60) 60 (configurable, floor 5)
SONIOX_IDLE_MAX_CLOSES_PER_HOUR 12 12, now shared by both triggers
SONIOX_IDLE_NO_FRAMES_SECONDS (new) none 60 (floor 10; 0 disables only this trigger)
  • No-frames close. A per-socket watchdog wakes once per interval. If no client frame reached the Soniox leg for 60 s, through either send() or the VAD observer and in any VAD mode, it starts the existing planned close: finalize, drain final tokens for up to 2 s, then close.
    • It counts against the same re-arm floor and 12-per-hour cap as the VAD close, so total speaker-scope churn per session stays within today's bound.
    • The next frame reopens through the existing onset/rebase path, unchanged. Tests show no lost or duplicated bytes, in both gated and passthrough mode.
  • Hangover. If the last frame carried speech or its hangover tail, or any ungated audio, the no-frames close also waits out the 4 s VAD hangover. It never finalizes inside the tail that protects a word boundary. VAD_HANGOVER_SECONDS is pinned to VAD_GATE_HANGOVER_MS by a test.
  • Metrics.
    • omi_soniox_connected_seconds_total{demand_cohort}: wall time each Soniox websocket is actually open (the billed quantity), with idle close on or off.
    • omi_soniox_idle_closes_by_trigger_total{trigger="vad"|"no_frames"}: shows how the saving splits between the two triggers.
  • Config. Prod listen values, the prod overlay and the composed runtime_env.yaml no longer carry SONIOX_IDLE_CLOSE_SECONDS=45, because 45 is now the code default. Prod behaviour is identical. The stale entry in the pusher cohost listen-only allowlist is dropped. The flag registry and docs are updated.
  • Dev listen. Dev never had the pin, so it now gets the same policy as prod (on by default).

Expected capture

The attribution report (§5) estimates the no-client-audio share of the 10-18 silent hours as follows:

  • omi_listen_live_no_audio_sessions averaged 66 concurrent sockets, about 1,550 h/day across all providers.
  • At Soniox's ~62% share of open streams, that is ≈960 of the 2,857 no-audio hours (~34%), ≈$58/day (low confidence: the gauge has no provider label).

The no-frames close recovers most of that. Each stalled socket still bills up to 60 s (+4 s hangover) before closing. Sessions that already spent their 12 closes this hour wait for a slot. So the realistic range is ≈$40–58/day.

The remaining ~1,900 silent hours are short VAD-gated gaps and cap exhaustion. That part waits for the speaker-scope follow-up below.

Rollback

Each of the four knobs is an env var on backend-listen:

  • Restore exactly the pre-PR prod behaviour: SONIOX_IDLE_NO_FRAMES_SECONDS=0. The other three are already at prod values: SONIOX_IDLE_CLOSE_SECONDS=45, SONIOX_IDLE_REARM_SECONDS=60, SONIOX_IDLE_MAX_CLOSES_PER_HOUR=12. Set all four explicitly to pin that.
  • Full kill switch (always-connected adapter, no idle close of any kind): SONIOX_IDLE_CLOSE_SECONDS=0.

Follow-up (required before any lower VAD threshold)

Preserve speaker scope across idle reopen. Today IdleSonioxSocket._resume bumps _socket_epoch, and SpeakerProviderEpoch.stamp mints a new speaker_id_scope on each epoch (speaker_identity.py:60-66). So Soniox's restarted SPEAKER_00 becomes a new conversation speaker. The fix is to keep the conversation scope across a planned idle reopen and remap the new socket's SPEAKER_N onto the existing conversation speakers. Only after that lands should the VAD close move toward 20 s / 10 s re-arm / a higher cap. That is where the other ~$80–110/day lives.

Deploy note

Deploy this listen change separately from the live static-routing stage changes (#22231), so their effects on Soniox connected hours, reopen latency and routing don't confound each other. Both PRs edit prod_omi_backend_listen_values.yaml. After deploy, watch:

  • increase(omi_soniox_connected_seconds_total[1d])/3600, against the baseline of about 3,450 h/day;
  • omi_soniox_idle_closes_by_trigger_total by trigger;
  • omi_soniox_idle_reopens_total, omi_soniox_idle_reopen_seconds and omi_soniox_idle_reopen_failures_total.

Verification

  • test_soniox_idle_close.py, test_listen_stt_rollout.py and test_soniox_idle_reopen_capture.py: 128 passed. New and updated tests cover:
    • defaults equal to the prod policy;
    • no-frames close and reopen with exact bytes, in gated and passthrough mode;
    • the watchdog running unattended;
    • no-frames sharing the hourly cap and the re-arm floor;
    • the no-frames close waiting out the hangover after speech;
    • no-frames disabled;
    • frames without speech not triggering it;
    • the 60 s re-arm default;
    • closes counted by trigger;
    • the connected-seconds meter;
    • a dead transport left to failover.
  • Related backend unit test files (Soniox, demand cohort, runtime env, flag registry) were each run in their own process, and so was the flag registry check. Merged with origin/main (merge commit, no rebase).
  • Not measured: live reopen latency or the realized $ split. Those need a deploy and closes_by_trigger.

Product invariants affected

  • INV-MEM-4: not affected. It is cited only because the path trigger requires it. This change only alters Soniox transport lifetime and metrics, with no memory promotion or canonical write.

Failure class (fixes)

No fix: commits (perf: and chore:), so no declaration is required.

🤖 Generated with Claude Code

Git-on-my-level and others added 2 commits October 19, 2026 10:56
Soniox bills every connected second, keepalive silence included. On
2026-10-18, 2,857 of 3,448 billed Soniox hours carried no audio.

- Idle close is default-on: SONIOX_IDLE_CLOSE_SECONDS defaults to 20 s
  (0/off is the kill switch). Prod's 45 s pin is removed.
- Re-arm floor is configurable and lower: SONIOX_IDLE_REARM_SECONDS
  default 10 s (floor 5 s), was hard-coded 60 s.
- Hourly close cap raised: SONIOX_IDLE_MAX_CLOSES_PER_HOUR default 120,
  was 12.
- New wall-clock close when the client sends no frames at all for
  SONIOX_IDLE_NO_FRAMES_SECONDS (default 20 s, 0 disables), independent
  of VAD. It honors the re-arm floor; the next frame reopens through the
  existing onset/rebase path.
- New omi_soniox_connected_seconds_total{demand_cohort} measures billed
  websocket-open time directly, plus
  omi_soniox_idle_no_frames_closes_total.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lowlist

The prod pin is gone; the code default governs both charts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T05:47:26.611740Z dd0ce9b New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8b4981744e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

delay = self.check_no_frames()
if delay is None:
return
await _sleep(delay)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Replace the watchdog sleep with an interruptible wait

When a listen WebSocket disconnects while this coroutine is sleeping, the per-connection shutdown event cannot wake it, and this task was created without the supervisor's task set, leaving cleanup dependent on later wrapper-specific cancellation. Use wait_for_event(event, delay) with the connection shutdown event so disconnects stop the watchdog immediately rather than allowing it to remain alive and potentially start an idle close during teardown. The backend WebSocket concurrency contract explicitly prohibits bare asyncio.sleep() in polling loops for this reason.

AGENTS.md reference: backend/AGENTS.md:L293-L296

Useful? React with 👍 / 👎.

…ames close

Review found every idle reopen mints a new speaker scope, so a faster VAD
cadence churns live speaker labels. Re-scope:

- VAD close defaults return to the prod values (45 s, re-arm 60 s,
  12 closes/hour); 45 s is now the code default.
- No-frames close defaults to 60 s, spends the shared hourly cap and
  re-arm floor, and waits the 4 s VAD hangover after speech/ungated audio.
- omi_soniox_idle_closes_by_trigger_total{trigger=vad|no_frames}
  replaces the no-frames-only counter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Git-on-my-level
Git-on-my-level merged commit c88d8b5 into main Oct 19, 2026
60 checks passed
@Git-on-my-level
Git-on-my-level deleted the lane/opus-soniox-idle branch October 19, 2026 06:17
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