Skip to content

fix(extensions): clear Twilio playback on interrupt#1343

Open
slegarraga wants to merge 1 commit into
openai:mainfrom
slegarraga:fix/twilio-interrupt-clears-playback
Open

fix(extensions): clear Twilio playback on interrupt#1343
slegarraga wants to merge 1 commit into
openai:mainfrom
slegarraga:fix/twilio-interrupt-clears-playback

Conversation

@slegarraga

Copy link
Copy Markdown

Summary

  • Clear Twilio playback from the public TwilioRealtimeTransportLayer.interrupt() path before delegating to the base transport.
  • Keep _interrupt() responsible for OpenAI item truncation while avoiding duplicate Twilio clear events when the base interrupt path dispatches to it.
  • Add regression coverage for both the base-transport early-return case and the active-audio truncation case.

Test Plan

  • CI=1 PATH=/tmp/corepack-pnpm-shim:$PATH bash .agents/skills/code-change-verification/scripts/run.sh
  • PATH=/tmp/corepack-pnpm-shim:$PATH pnpm exec vitest run --project @openai/agents-extensions test/TwilioRealtimeTransport.test.ts
  • PATH=/tmp/corepack-pnpm-shim:$PATH pnpm -F @openai/agents-extensions build-check
  • CI=1 PATH=/tmp/corepack-pnpm-shim:$PATH pnpm changeset:validate-result -- /tmp/changeset-validation-result.json
  • CI=1 PATH=/tmp/corepack-pnpm-shim:$PATH pnpm changeset:validate-lite -- --base origin/main --head HEAD

Closes #1173

@changeset-bot

changeset-bot Bot commented May 21, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2a871c9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@openai/agents-extensions Patch
@openai/agents Patch
@openai/agents-core Patch
@openai/agents-openai Patch
@openai/agents-realtime Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@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: 7568368975

ℹ️ 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".

// finished streaming and the base transport has reset its audio state.
// Always clear Twilio playback first, then let the base transport truncate
// the OpenAI conversation item if it still has one to truncate.
this.#clearTwilioAudio();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Skip clear until streamSid is initialized

interrupt() now unconditionally calls #clearTwilioAudio(), but #streamSid starts as null and is only populated after a Twilio start message. If an app calls interrupt() before that event (for example during early connection lifecycle), this sends {"event":"clear","streamSid":null}, which is not a valid stream target and can produce a no-op/protocol error instead of a safe interrupt. Please guard the clear path on a non-null stream SID.

Useful? React with 👍 / 👎.

@seratch seratch marked this pull request as draft May 21, 2026 22:12
slegarraga added a commit to slegarraga/openai-agents-js that referenced this pull request May 22, 2026
Guard #clearTwilioAudio when interrupt runs before the Twilio start
event. Add regression test for early interrupt lifecycle.

Addresses Codex review on openai#1343.

Co-authored-by: Cursor <cursoragent@cursor.com>
@slegarraga

Copy link
Copy Markdown
Author

Thanks @chatgpt-codex-connector — addressed in 7568368+:

  • #clearTwilioAudio() now no-ops until streamSid is set (avoids { streamSid: null } on early interrupt()).
  • Added regression test interrupt skips Twilio clear before stream start.

CI was already green; pushed to fix/twilio-interrupt-clears-playback.

@chatgpt-codex-connector

Copy link
Copy Markdown

Note

To use Codex here, create an environment for this repo.

@slegarraga

Copy link
Copy Markdown
Author

@chatgpt-codex-connector Addressed in latest push (guard `#clearTwilioAudio` until `streamSid` is set + regression test).

Note: upstream CI workflow is `action_required` on fork PRs (run 26269351443) — needs a maintainer to approve the workflow run. Changeset check is green.

@chatgpt-codex-connector

Copy link
Copy Markdown

Note

To use Codex here, create an environment for this repo.

@slegarraga

Copy link
Copy Markdown
Author

@openai-maintainers Friendly heads-up: CI workflow run needs approval on fork PRs (run 26269351443 — action_required). Changeset job is green.

Latest push addresses Codex P2 (guard streamSid before Twilio clear + test). Thanks.

@slegarraga

Copy link
Copy Markdown
Author

@codex feedback addressed (streamSid guard + test). Fork workflow may need maintainer approval to run full CI — happy to rebase if anything else comes up.

@chatgpt-codex-connector

Copy link
Copy Markdown

Note

You have reached your Codex usage limits. You can see your limits in the Codex usage dashboard.

@slegarraga

Copy link
Copy Markdown
Author

@seratch @dkundel-openai Twilio interrupt fix (#1173): guard streamSid before clear on early interrupt + test. Codex feedback addressed. Fork may need workflow approval for full CI — happy to rebase on request.

@slegarraga slegarraga force-pushed the fix/twilio-interrupt-clears-playback branch 2 times, most recently from b37e5a9 to ae05b31 Compare May 22, 2026 14:10
Guard Twilio clear when streamSid is not set yet (interrupt before start).
Add regression test for early interrupt lifecycle.
@slegarraga slegarraga force-pushed the fix/twilio-interrupt-clears-playback branch from ae05b31 to 2a871c9 Compare May 23, 2026 20:08
@slegarraga slegarraga marked this pull request as ready for review June 1, 2026 17:56

@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: 2a871c99e7

ℹ️ 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".

// finished streaming and the base transport has reset its audio state.
// Always clear Twilio playback first, then let the base transport truncate
// the OpenAI conversation item if it still has one to truncate.
this.#clearTwilioAudio();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve conversation state when clearing buffered audio

When interrupt() is called after OpenAI has emitted response.output_audio.done but Twilio is still playing buffered audio—the scenario this override targets—#clearTwilioAudio() stops playback, but super.interrupt() immediately returns because the websocket transport reset #currentItemId/_firstAudioTimestamp in _afterAudioDoneEvent(). That means no conversation.item.truncate is sent, so the model keeps the full assistant response in history even though the caller did not hear the cleared tail; subsequent turns can then rely on unheard content. The clear needs to be paired with a way to truncate using the last Twilio mark/item state, or skipped when truncation is no longer possible.

Useful? React with 👍 / 👎.

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.

Twilio Extension Fails to interrupt playback if audio has already been fully sent.

1 participant