fix(cartesia): ignore stale frames from previous contexts on pooled websockets - #2401
Merged
Conversation
🦋 Changeset detectedLatest commit: 6a8ab5e The changes in this PR will be included in the next version bump. This PR includes changesets to release 39 packages
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 |
chenghao-mou
approved these changes
Sep 2, 2026
Merged
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.
Ports livekit/agents#7081.
A reused Cartesia WebSocket can retain unread audio and completion messages from an interrupted synthesis context. Filter messages by the active request context before processing them so stale audio is not emitted and stale completion cannot truncate the current synthesis.
Adds the source-equivalent local WebSocket regression test and a patch changeset for
@livekit/agents-plugin-cartesia.Source diff coverage
Source diff coverage
livekit-plugins/livekit-plugins-cartesia/livekit/plugins/cartesia/tts.py->plugins/cartesia/src/tts.ts. The Pythoncontext_idguard is applied before all TypeScript message handling, using the activerequestId; target Cartesia message schemas requirecontext_id, so the source optional-presence check reduces to direct mismatch filtering.tests/test_plugin_cartesia_tts.py->plugins/cartesia/src/tts.test.ts. The source fake pooled socket scenario is expressed with the target existing local WebSocket server harness: stale audio and done are sent before current-context audio and done, and only current audio is asserted.Verification
pnpm test plugins/cartesia(11 passed, 2 credential-gated skipped)pnpm buildpnpm exec prettier --check "plugins/cartesia/src/tts.ts" "plugins/cartesia/src/tts.test.ts" ".changeset/fresh-cartesia-contexts.md"pnpm exec eslint -f unix "plugins/cartesia/src/tts.ts" "plugins/cartesia/src/tts.test.ts"pnpm lintattempted; blocked by an unchanged Prettier error inplugins/baseten/src/stt.test.tspnpm --filter @livekit/agents-plugin-cartesia lintattempted; blocked by an unchanged Prettier error inplugins/cartesia/src/models.tsA provider-path
cue-clirun was not possible becauseCARTESIA_API_KEYis not configured in the environment. The deterministic local WebSocket test exercises the affected runtime receive path without provider credentials.Ported from livekit/agents#7081
Original PR description
When the ConnectionPool returns a previously-used websocket, the socket buffer may still contain unread audio frames / done from the abandoned context (occurring when a SynthesizeStream was interrupted before the frames were fully consumed). The
_recv_taskdid not validate thecontext_idon received messages, so stale audio was pushed into the new output and a stale "done" could prematurely truncate the new synthesis.Changes
_recv_task): skip any message whosecontext_idis present but does not match the currentcartesia_context_id. Messages without acontext_id(e.g. server errors) are not affected.Reproduction
Without the fix, sending a stale-context audio frame + done then current-context audio + done:
With the fix:
Related
Root cause analysis: In the production session, 27 consecutive
say()calls hadttfb≈0.002s(impossible over a fresh connection) andduration≈0.01s, confirming the socket was replaying stale frames instead of performing fresh synthesis. The"..."SPEAK text that triggered the visible symptom was just the most obvious case — any text sent through a pooled socket after an interrupted stream would exhibit the same corruption.