Repository navigation
fix(comment-automation): block private replies once the bot quota is reached - #1720
Merged
Merged
Conversation
…reached A `text` or `AIAgent` private reply goes straight out through the comment-anchored Send API, never through the chat worker, so the bot message quota gate never saw it: an over-quota workspace kept sending these DMs while every other bot send was blocked. The private reply gate now checks the owner's bot message quota (read at most once per comment, fail-open on a lookup error) and, when it is reached, records a `failed` event with "Private reply not sent: Bot message limit reached" instead of sending. The deferred TikTok private reply re-checks it at send time. The reply-once slot is released as for any other blocked private reply. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Problem
The bot message quota is enforced by the chat worker's gate (
apps/worker/src/chat/worker.ts). A comment automation'stextandAIAgentprivate replies never pass through it: they go straight to the comment-anchored Send API (executePrivateReply/processCommentAIReply). So once the owner's bot message quota was reached, every other bot send was blocked, but these DMs kept going out. They were still counted, so usage climbed past the limit.Follow-up to #1719, which makes sends dropped by the chat gate show as failed instead of
sent.Fix
private-reply-quota.ts:isPrivateReplyQuotaReached(workspaceId)wrapsisBotMessageQuotaReachedand fails open (logs{ err }, returnsfalse) if the lookup throws.resolvePrivateReplyBlockedReasontakesbotMessageQuotaReachedas a third blocked reason, after the channel's own rules (claimed DM, reply window). A blocked reply records afailedevent withPrivate reply not sent: Bot message limit reachedthrough the existingrecordBlockedPrivateReplypath. Nothing is sent, and no provider error is logged.processCommentAutomationreads the quota at most once per comment, and only when an automation has a private reply to send.Not changed: public replies and
flowDMs are already blocked by the chat gate.flowDMs are also checked here, so a blocked one does not spend the comment's single DM.Tests
apps/worker/__tests__/comment-private-reply-quota.test.ts(new): the helper, including the fail-open case, and the blocked-reason orderingapps/worker/__tests__/comment-automation.test.ts: an over-quota text DM is recordedfailedand not sent; the quota is read once per comment and not at all without a private replyapps/worker/__tests__/deferred-private-reply.test.ts: the deferred DM is blocked and recordedAll 12 worker test files touching comment automation pass (622 tests).
pnpm lint,pnpm check:invariantsandpnpm --filter worker check-typespass.🤖 Generated with Claude Code