fix(after-hours): LRU eviction for the per-chat cooldown map (v0.1.2) - #5
Merged
Conversation
The cooldown map evicted by first-seen order, so a continuously-active chat could be evicted ahead of idle ones once the 5000-entry cap was hit — resetting its cooldown and letting it bypass the throttle. Re-insert on each reply so eviction tracks recency.
rmyndharis
added a commit
that referenced
this pull request
Aug 12, 2026
…ly mapping (#93) * fix(group-translate): scan the skip filter token by token The filter that skips messages with nothing to translate was one regular expression whose emoji branch overlapped its URL branch: `\p{Emoji}` matches ASCII digits, `#` and `*`, because those are keycap-sequence components, and so it also matched the `\S+` inside `https?://\S+`. Every additional link-shaped token multiplied the backtracking space — a 145-character message took several seconds, and `maxLength` allows 2000. Regex execution cannot be interrupted, so that time is spent holding the worker's event loop, and any member of a group with translation on could send it. Scanning token by token is linear and needs no backtracking at all. Measured on the same input: 5.5 s before, 0.08 ms after, and flat at 3200 characters. Switching the emoji test to `\p{Extended_Pictographic}` fixes a second consequence of the same property: a message of `1` or `#5` was classed as emoji-only and silently left untranslated. * fix(chatwoot-adapter): stop the unscoped reply mapping from crossing tenants Chatwoot conversation ids are per-account autoincrement, so two accounts relayed by one gateway share one as a matter of course. Beside the tenant-scoped reverse mapping, an unscoped one was rewritten on every link and therefore held whichever session linked last. A delivery arriving without a session scope resolved through it — an agent reply for one customer sent to a different customer's number. Unlinking by conversation id deleted that shared mapping too, silently dropping the other tenant's replies until their next inbound message rebuilt it. The unscoped mapping is now claimed only while it is unclaimed, and marked unusable once a second session claims the same id: nothing in a scope-less delivery says which tenant it belongs to, so it is dropped and logged rather than routed to a guess. Unlink removes it only when it belongs to the session being unlinked. Mappings written before scoping keep resolving and single-tenant hosts are unaffected. One echo-loop test relied on the old guess and now supplies the scope it would carry in practice, which is what it meant to exercise; a new test pins the drop.
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
Hardens the per-chat cooldown in after-hours, released as v0.1.2.
The cooldown map capped at 5000 entries and evicted in first-insertion order. Re-setting an existing chat's timestamp didn't refresh its position, so a continuously-active chat kept its original (oldest) slot and was evicted first once the cap was reached. After eviction, its next message saw no record and was allowed immediately — bypassing the configured cooldown / amplifying replies.
allowReplynow deletes-then-sets the key on each touch, so iteration order tracks recency andkeys().next().valueis the genuinely least-recently-used entry. Only idle chats are dropped; an in-cooldown chat is never refreshed.Tests
tsc --noEmitclean, bundle packages cleanly.