Skip to content

refactor: remove dead code and duplicate constants; fix a data race in snark picking - #48

Merged
jordan-simonovski merged 1 commit into
mainfrom
refactor/dead-code-sweep
Nov 14, 2026
Merged

jordan-simonovski merged 1 commit into
mainfrom
refactor/dead-code-sweep

Conversation

@jordan-simonovski

Copy link
Copy Markdown
Owner

Implements docs/backlog/15-dead-code-sweep.md (deleted in this PR, with its index row).

A real bug, found on the way

RandomSnarkPicker created one *rand.Rand from math/rand, and cmd/server shares that single picker across every request goroutine. math/rand's Rand isn't safe for concurrent use, so concurrent rejections raced on its state. A new test, TestRandomSnarkPickerIsSafeForConcurrentUse, calls the picker and the other two snark helpers from 32 goroutines. Run with -race it reports DATA RACE on current main, and passes here. snark.go now uses math/rand/v2's top-level rand.IntN, which is goroutine-safe and needs no seeding. The per-call reseeding in the other two helpers, and the comment explaining it, are gone.

Removed (each confirmed unused outside tests first)

Where What
internal/domain ErrUnsupportedReason, EvaluateKarmaAction (the server calls EvaluateKarmaActionWithLimits)
internal/parser ParseMentionAction, ParseMentionActions, MentionAction (the server uses ParseKarmaSegments)
internal/slack ChannelSettingsService.GetReplyMode; never-set payload fields SlashCommandPayload.{Token, ChannelName, EnterpriseID, EnterpriseName}, EventEnvelope.Token, SlackEvent.ChannelID, MessageResponse.ThreadTS; Blocks is now any
internal/persistence DynamoSettingsRepository.GetReplyMode; its own copies of the snark level constants, replaced by domain's (the same 5, 1 and 10)

Kept: the local abs in reaction_settings.go, as the brief says. Not in the brief's table, so left for a later pass: settings_blocks.go still uses map[string]interface{}.

Tests

No coverage was lost. TestEvaluateKarmaAction now calls EvaluateKarmaActionWithLimits with the same limits. The parser cases moved to TestParseUserMentions using test helpers over ParseKarmaSegments, with the same cases and semantics. The settings-service test checks unknown modes still default to thread, via GetChannelSettings. make check passes, including go vet -tags=integration. No changeset.

Review

/ce-code-review ran as a lite correctness and testing review. It found no defects. It confirmed:

  • no removed symbol or field had a non-test reader
  • the removed payload fields were never set by the parsers
  • the settings defaults are numerically identical
  • the parser tests keep the old semantics
  • rand.IntN covers the same ranges

…n snark picking

- remove ErrUnsupportedReason, EvaluateKarmaAction, ParseMentionAction(s) and
  MentionAction, and GetReplyMode on the settings service and repository;
  their tests now exercise the functions the server uses
- drop never-used fields from the Slack payload types; Blocks is any
- the settings repository uses domain's snark level constants instead of
  its own copies
- snark.go uses math/rand/v2. RandomSnarkPicker shared one *rand.Rand
  across all request goroutines, which is a data race; a new test catches it
  under -race
@jordan-simonovski
jordan-simonovski merged commit ebc482e into main Nov 14, 2026
4 checks passed
@jordan-simonovski
jordan-simonovski deleted the refactor/dead-code-sweep branch November 14, 2026 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant