Repository navigation
refactor(discord): centralize voice resource lifecycle - #130415
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
Landing proof for Exact-head CI run 33015584772 completed successfully: 30 jobs passed, no failures. Native review artifacts are validated for this head. Latest ClawSweeper feedback is a review-started placeholder, with no findings or Rank-up moves yet; there is no completed actionable review to skip. The three reproduced failures are repaired at their resource owners: Node's transform lifecycle owns encoder acquisition/release; the shared streaming decoder owns both decode modes; each voice entry owns teardown and identity-fenced registry removal. Failure observers are installed before provider startup, and capture is not admitted after cancellation. Production is 90 lines smaller (+119/-209); tests are +230/-44. Before/after proof: the final focused regression set against both original production modules produced 3 failures / 13 passes (late encoder leak, false successful join after Discord destruction, startup teardown exception). The candidate passed 306 voice tests and 34 runtime E2E tests, including a real Discord player/Opus encoder draining all 6.8 seconds before advancing the reply queue. The later test-only Promise-executor lint correction passed its 2 focused tests and lint across all 5 changed files. Fresh Codex autoreview has no actionable findings. Commands used locally (with bounded workers and separate test caches): node scripts/run-vitest.mjs extensions/discord/src/voice
node scripts/run-vitest.mjs --config test/vitest/vitest.e2e.config.ts extensions/discord/src/voice/voice-runtime.e2e.test.ts extensions/discord/src/voice/voice-runtime.secret-owner.e2e.test.ts
node scripts/run-vitest.mjs extensions/discord/src/voice/audio.lifecycle.test.ts
node scripts/run-oxlint.mjs --tsconfig config/tsconfig/oxlint.extensions.json extensions/discord/src/voice/audio.lifecycle.test.ts extensions/discord/src/voice/audio.test.ts extensions/discord/src/voice/audio.ts extensions/discord/src/voice/voice-session.startup.test.ts extensions/discord/src/voice/voice-session.ts
node --import tsx scripts/check-database-first-legacy-stores.mts
node --import tsx scripts/check-media-download-helper-roundtrip.mts
node --import tsx scripts/check-runtime-sidecar-loaders.mts
node --import tsx scripts/check-import-cycles.ts
git diff --checkLocal changed-file checking passed its first 17 gates, including the doctor contract tests, formatting, dead-code and boundary guards. The broad local plugin typecheck hit missing unrelated IMAP dependencies in the shared installation; clean-install CI is the typecheck proof, not that local run. Initial CI found the test Promise-executor lint issue; it was fixed and repushed, with production unchanged. No candidate production deployment or authenticated Discord listening was performed: the available operator deployment is immutable, and replacing/restarting it is a separate operation outside this PR. Real player/codec integration is socket-free and is not described as live-channel proof. There are no public API, config/default, dependency or persistence changes. |
|
Merged via squash.
|
* refactor(discord): centralize voice resource lifecycle * test(discord): avoid returning from voice promise executor
Related: #130346
What Problem This Solves
Fixes an issue where users joining Discord voice could receive a successful join result after Discord had already destroyed the connection, or an internal startup error when provider output stopped playback before connection setup finished. Cancelling playback while the Opus encoder was still initializing could also leave the encoder allocated and process audio after cancellation.
This is the maintainer-requested lifecycle cleanup following the audio-cutoff repair in #130346. The affected ownership problems predate that PR.
Why This Change Was Made
Give each resource one lifecycle owner. The Node transform now acquires its encoder in
_construct, emits its final padded packet in_flush, and frees it in_destroy. This deletes the separate lazy-promise/free paths; Node serializes construction against cancellation. Buffered decoding now collects the canonical streaming decoder output, removing the duplicate decode loop and obsolete codec adapter.The voice entry now owns all session teardown, including identity-fenced registry removal. Its handlers are initialized and connection failure listeners installed before asynchronous provider startup, while capture is admitted only after successful startup. Setup failure and cancellation reuse that same teardown path. A delayed callback from an old session cannot remove its replacement.
Production LOC: +119/-209 (net -90); tests +230/-44 (net +186). No new dependency, public API, configuration/default, persistence, or provider-protocol changes. Increasing timeouts or adding late-encoder cleanup branches would retain the competing owners; this refactor removes them instead.
User Impact
Cancelled or failed voice joins release their resources and report failure instead of publishing a dead session. Cancelled playback releases even an encoder that finishes initializing late. Normal playback, final-frame padding, incoming voice decoding, and partial-audio error behavior remain covered through the existing voice paths.
Release-note context: Discord voice now cleans up failed startup and cancelled codec initialization reliably, with shared decoding and session teardown ownership.
Evidence
speakingHandlertemporal-dead-zone exception. The candidate passes all three.node scripts/run-vitest.mjs extensions/discord/src/voice: 27 files, 306 tests passed, including a real@discordjs/voiceplayer and reallibopus-wasmencoder draining every frame of a 6.8-second burst before advancing queued speech.node scripts/run-vitest.mjs --config test/vitest/vitest.e2e.config.ts extensions/discord/src/voice/voice-runtime.e2e.test.ts extensions/discord/src/voice/voice-runtime.secret-owner.e2e.test.ts: 2 files, 34 tests passed, after rebuilding local package artifacts. Covers join, audio ingress, agent dispatch, playback, leave, cancellation, participant access, and provider secret ownership.libopus-wasmdecoder output is an ownedInt16Arraycopy; decoder/encoder operations andfreeare synchronous. Node's documented_constructcontract delays writes, finalization, and destruction until initialization settles (Node stream documentation). Discord connection destruction emits its state transition synchronously and repeated destruction throws.imapflow,mailauth, andmailparser. Those IMAP sources and manifests are unchanged from the baseline. Clean-install exact-head CI must pass before landing; this is not reported as a green local typecheck.Live Discord listening against the candidate is not claimed: the available operator deployment is immutable, and testing this checkout there would require a separate production deployment/restart outside this PR's scope. The real player/codec integration runs without Discord network sockets; it is not an authenticated Discord-channel test.
Provenance: the lazy encoder ownership was introduced in
0c5f622f9a4c422720cf95f8bae33aca3ab235a3(2026-05-26, author/committer Peter Steinberger). The session startup ordering existed before the90beb639e7evoice-session split and was carried forward there; the exact original introduction is not established by the available history.AI-assisted implementation and review, with maintainer-directed scope.