Skip to content

fix(after-hours): LRU eviction for the per-chat cooldown map (v0.1.2) - #5

Merged
rmyndharis merged 1 commit into
mainfrom
after-hours-cooldown-lru
Jun 23, 2026
Merged

fix(after-hours): LRU eviction for the per-chat cooldown map (v0.1.2)#5
rmyndharis merged 1 commit into
mainfrom
after-hours-cooldown-lru

Conversation

@rmyndharis

Copy link
Copy Markdown
Owner

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.

allowReply now deletes-then-sets the key on each touch, so iteration order tracks recency and keys().next().value is the genuinely least-recently-used entry. Only idle chats are dropped; an in-cooldown chat is never refreshed.

Tests

  • New: re-touching a key protects it from eviction; the genuinely-oldest entry is dropped.
  • Existing cap/cooldown tests still pass. tsc --noEmit clean, bundle packages cleanly.

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
rmyndharis merged commit 23ee627 into main Jun 23, 2026
1 check passed
@rmyndharis
rmyndharis deleted the after-hours-cooldown-lru branch June 23, 2026 09:36
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.
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