← Back to PRs

#23158: discord: harden preflight/reply path against slow lookup latency

by danielstarman open 2026-02-22 02:40 View on GitHub →
channel: discord size: M
## Summary - Fix Discord preflight/reply stalls tied to slow channel and PluralKit lookups. - Add bounded timeout/abort handling and guarded retry behavior so monitor flow fails fast instead of hanging. - Harden thread-bound routing when Discord payload thread metadata is incomplete. - Keep scope strictly to Discord monitor/pluralkit paths and related tests. ## Linked Issue Closes #22378. ## What Changed - Preflight guild fast path: skip early channel REST lookup for guild traffic. - Channel lookup timeout support in preflight/thread-parent resolution. - PluralKit lookup timeout + abort support; bot-path retry only on timeout/abort-like failures. - Thread binding lookup now consistently uses `channel_id`. - Bound-thread session detection based on binding/session key presence. - Reply typing start is non-blocking (`fire-and-forget`) so reply dispatch is not delayed by typing REST latency. ## Scope Changed files: - `src/discord/monitor/message-handler.preflight.ts` - `src/discord/monitor/message-handler.process.ts` - `src/discord/monitor/message-utils.ts` - `src/discord/monitor/threading.ts` - `src/discord/pluralkit.ts` - Discord test updates only ## Validation - `pnpm build` passes. - `pnpm check` passes. - `pnpm test` passes. - Targeted Discord suites pass: - `src/discord/monitor/message-handler.process.test.ts` - `src/discord/monitor/message-utils.test.ts` - `src/discord/pluralkit.test.ts` - `src/discord/monitor.tool-result.sends-status-replies-responseprefix.test.ts` - `src/discord/monitor/message-handler.preflight.test.ts` ## Risk Notes - No new permissions, auth model changes, or config migrations. - Network behavior changed only by adding timeout/abort bounds on existing Discord/PluralKit calls. ## Transparency - AI-assisted: Yes (Codex). - Testing level: fully tested (`pnpm build`, `pnpm check`, `pnpm test`, plus targeted Discord suites). ## Context - This PR supersedes #22930, which was unintentionally closed after source-fork visibility/fork-linkage changes. - Commit content is the same fix series for #22378. <!-- greptile_comment --> <h3>Greptile Summary</h3> Added timeout/abort handling to Discord channel and PluralKit lookups to prevent monitor flow from stalling when external APIs are slow. Guild messages now skip early channel REST lookup (payload-first fast path), PluralKit lookups retry with extended timeout on abort-like errors for bot messages, and reply typing starts fire-and-forget to avoid blocking dispatch. Thread binding detection now consistently uses `channel_id` and checks binding presence rather than `threadChannel` existence. <h3>Confidence Score: 4/5</h3> - This PR is safe to merge with low risk - adds defensive timeout handling without changing core business logic. - The PR implements well-scoped timeout and retry mechanisms with comprehensive test coverage. All changes are defensive additions (timeouts, abort handling, fast paths) that fail safely. Tests validate timeout behavior, PluralKit retry logic, and non-blocking typing. However, one minor issue was found: the timeout wrapper in `message-utils.ts` doesn't call `unref()` conditionally, which could cause subtle issues in environments where it's not available. - Pay close attention to `src/discord/monitor/message-utils.ts` line 90 - the `unref()` call should be conditional. <sub>Last reviewed commit: 13e72ca</sub> <!-- greptile_other_comments_section --> <sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub> <!-- /greptile_comment -->

Most Similar PRs