Fix cross-platform reply race: per-command context via AsyncLocalStorage - #346
Merged
Merged
Conversation
…obals
currentPlatform/currentChannel/currentIsAdmin were module-level globals set
at the start of each command and read later (after awaits and in timers,
e.g. the 5-minute flush-vote timer). Overlapping Slack and Discord commands
could overwrite them, sending replies to the wrong platform and making
wrong admin decisions in help.
- lib/command-context.js: AsyncLocalStorage-backed {platform, channel,
isAdmin, userName} that follows a command through awaits and timers
- Router entry points (routeCommand/processInput/handleNaturalLanguage)
run inside the command's context; reaction handlers too
- _slackMessage, help, DMs, bestof and add-handlers read the context;
outside a command, sends default to Slack if configured, else Discord
- Tests for overlapping commands and timers keeping their own context
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HC2Ba1dYTJaUgH63ks2N1q
htilly
marked this pull request as ready for review
September 26, 2026 17:24
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.
Summary
Follow-up to #340.
index.jskept three module-level globals,currentPlatform,currentChannelandcurrentIsAdmin. Each command set them when it started, but they were read much later: after awaits, and in timers such as the 5-minute flush-vote timer. Slack commands run fire-and-forget, so overlapping Slack and Discord commands could overwrite each other's values. The result:helpcould make the wrong admin decisionChanges
lib/command-context.js(new): anAsyncLocalStorageholding{platform, channel, isAdmin, userName}. It follows a command through everyawait, promise chain andsetTimeoutthe command starts.lib/command-router.js: the public entry points (routeCommand,processInput,handleNaturalLanguage) now run inside the command's context. The optionalsetContextdependency is still accepted.index.js:setContextclosure._slackMessage,help, Discord DMs,bestofand add-handlers'getCurrentPlatformnow read the current command's context.Timers such as the flush vote, gong cleanup and TTS cleanup needed no changes, because they inherit the context of the command that created them.
Test plan
npm test: 867 passingtest/command-context.test.mjs: the context survives awaits and timers, overlapping runs are isolated, and there is no context outside a commandnode --checkon the changed filesflushvotefollowed by a command on the other platform🤖 Generated with Claude Code
https://claude.ai/code/session_01HC2Ba1dYTJaUgH63ks2N1q
Generated by Claude Code