Skip to content

refactor(discord): centralize voice resource lifecycle - #130415

Merged
steipete merged 2 commits into
mainfrom
codex/discord-voice-lifecycle-cleanup
Aug 26, 2026
Merged

steipete merged 2 commits into
mainfrom
codex/discord-voice-lifecycle-cleanup

Conversation

@steipete

@steipete steipete commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Before/after reproduction: restoring both original production modules while retaining the final regression tests produced 3 failed / 13 passed. Failures were the unreleased late encoder, false successful join after connection destruction, and startup cleanup's speakingHandler temporal-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/voice player and real libopus-wasm encoder 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.
  • Buffered and streaming decode tests both round-trip real Opus audio and preserve already-decoded PCM when the source fails. The final padded packet is decoded and checked for the complete frame length. Startup tests also exercise ordinary provider rejection and manager shutdown during connect.
  • Dependency contracts inspected directly: installed libopus-wasm decoder output is an owned Int16Array copy; decoder/encoder operations and free are synchronous. Node's documented _construct contract delays writes, finalization, and destruction until initialization settles (Node stream documentation). Discord connection destruction emits its state transition synchronously and repeated destruction throws.
  • Fresh Codex autoreview completed with no actionable findings at its default reporting threshold; the owner/caller/sibling source and regression evidence were also manually reviewed.
  • Local changed-file checking passed formatting, dependency/ownership/export guards, dead-code scans, and doctor contract tests, then stopped at the broad plugin typecheck because the shared local installation lacks the unrelated IMAP dependencies imapflow, mailauth, and mailparser. 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.
  • The remaining database-first, media helper, runtime loader, and import-cycle guards passed when run separately. Initial clean-install CI passed production/test types but caught an implicit return in a new test Promise executor; corrected with a block-bodied callback, without changing production behavior.

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 the 90beb639e7e voice-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.

@clawsweeper

clawsweeper Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added channel: discord Channel integration: discord size: L maintainer Maintainer-authored PR labels Aug 26, 2026
@steipete
steipete marked this pull request as ready for review August 26, 2026 21:15
@steipete

Copy link
Copy Markdown
Contributor Author

Landing proof for 4ada3a64147821da1e5d2ec2425395244653b853.

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 --check

Local 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.

@steipete
steipete merged commit 54db7d4 into main Aug 26, 2026
93 checks passed
@steipete
steipete deleted the codex/discord-voice-lifecycle-cleanup branch August 26, 2026 21:42
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 27, 2026
* refactor(discord): centralize voice resource lifecycle

* test(discord): avoid returning from voice promise executor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: discord Channel integration: discord maintainer Maintainer-authored PR size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant