Skip to content

feat(comment-automation): harden reply delivery and gate media steps per channel - #1121

Merged
sung17 merged 1 commit into
mainfrom
feat/comment-reply-delivery-and-media-step-rules
Sep 9, 2026
Merged

sung17 merged 1 commit into
mainfrom
feat/comment-reply-delivery-and-media-step-rules

Conversation

@sung17

@sung17 sung17 commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Comment automation could silently never reach the customer: a partial dispatch failure re-sent the public reply on the next comment, a bailed-out AI job blocked the contact on that post forever, and every message after the first in a private-reply flow was rejected by Meta with the error swallowed. Each of those now either delivers or shows a reason.
  • Meta's hard limits are enforced before the call instead of after the rejection: one comment_id-anchored DM per comment (shared across every automation matching it) and the 7-day comment window.
  • Media steps a channel cannot deliver are described in flow-config and surfaced in the builder, and Instagram's text-only comment replies now fail visibly instead of vanishing.

Changes

Reply delivery

  • apps/worker/.../comment-automation/: dedup written when any branch dispatched; new dedup.ts holds the rollback helper; privateReplyClaimed shared across the loop; executors return whether they dispatched; warn when the incoming comment's message row is missing
  • packages/business/.../fb-comment-automation/service.ts: deleteDedup
  • packages/worker-config: commentDedup rides the commentAIReply job
  • packages/sdk: CommentAnchor.spent, isWithinMetaResponseWindow, assertCommentPrivateReplyFollowUpDeliverable
  • apps/worker/.../flow.ts: a claimed private anchor continues as spent: true instead of being dropped
  • integrations/{messenger,instagram,instagram-facebook}: guard every non-anchored send of a comment-triggered private run on the contact's 24-hour window

Media steps

  • packages/flow-config: media-step-rules + media-step-validators
  • apps/builder: media-step-notice on the send-* step editors, keys in all 20 locales
  • integrations/instagram{,-facebook}: sendComment throws PAYLOAD_INVALID for attachments; IG-FB collects a step's messages so quick replies land on the last one

Test plan

  • pnpm lint
  • pnpm --filter worker check-types (+ sdk, business, worker-config, flow-config, builder, all three integrations)
  • apps/worker: 2091 tests / 212 files
  • integrations: messenger 264, instagram 79, instagram-facebook 53
  • packages/flow-config: 476 tests
  • Manual: comment on a post with a private-reply flow of 2+ messages from an account that has never DMed the Page — first message arrives, second shows the one-reply-per-comment error in the inbox
  • Manual: same flow from a contact who DMed within 24h — the whole flow arrives
  • Manual: an AI-agent reply with a deleted agent — the contact's next comment is still answered (dedup rolled back)

…per channel

Two threads over the same surface: comment replies that silently never
reached the customer, and media steps that a channel cannot deliver.

Reply delivery (worker + sdk + channels):

- Write the dedup row when *any* branch dispatched, not only when both did.
  A public reply that went out while the private one threw used to leave no
  row, so the contact's next comment posted the public reply a second time.
- Roll the dedup row back when an AI reply job bails out (agent missing, no
  text, workspace outside active hours). It used to count as "replied", and
  `replyOncePerUserPerPost` then blocked that contact on that post forever.
  A thrown failure still keeps the row — BullMQ retries it.
- Skip a private reply once another automation on the same comment has spent
  it: Meta allows exactly one comment_id-anchored DM per comment, so the
  second call could only fail. Public replies of that automation still run.
- Skip a private reply for a comment older than Meta's 7-day window, delay
  included, instead of letting the Send API reject it.
- Keep a claimed private anchor travelling as `spent: true` rather than
  dropping it, so each channel's `sendFlowStep` can tell a comment-triggered
  follow-up from a plain flow message. Message two onward now sends as a
  normal DM inside the contact's 24-hour window and otherwise throws
  `comment_private_reply_already_used`, which lands on the message row as a
  `sendError` the inbox shows. Before, Meta rejected it and the error was
  swallowed, so a two-message flow looked half-delivered with no reason.
- Log when the incoming comment's message row is missing: like, hide and
  parent threading were being skipped with no trace.

Media steps (flow-config + builder + Instagram):

- `media-step-rules` / `media-step-validators` describe which media a channel
  accepts, surfaced in the builder as a notice on the send-* step editors.
- Instagram comment replies are text-only (`POST /{ig-comment-id}/replies`
  has no `attachment_url`), so both variants' `sendComment` now throw
  `PAYLOAD_INVALID` instead of returning an empty result behind a warn.
- Instagram-via-Facebook collects a step's messages before sending so quick
  replies land on the last one, and upload failures propagate to the inbox.

Co-authored-by: Claude Opus 5 <[email protected]>
@github-actions github-actions Bot added the feature New feature or request label Sep 9, 2026
@sung17
sung17 merged commit 674c90f into main Sep 9, 2026
4 of 5 checks passed
hello-jackytruong referenced this pull request in hello-jackytruong/ChatWorkAI Nov 22, 2026
…per channel (#1121)

Two threads over the same surface: comment replies that silently never
reached the customer, and media steps that a channel cannot deliver.

Reply delivery (worker + sdk + channels):

- Write the dedup row when *any* branch dispatched, not only when both did.
  A public reply that went out while the private one threw used to leave no
  row, so the contact's next comment posted the public reply a second time.
- Roll the dedup row back when an AI reply job bails out (agent missing, no
  text, workspace outside active hours). It used to count as "replied", and
  `replyOncePerUserPerPost` then blocked that contact on that post forever.
  A thrown failure still keeps the row — BullMQ retries it.
- Skip a private reply once another automation on the same comment has spent
  it: Meta allows exactly one comment_id-anchored DM per comment, so the
  second call could only fail. Public replies of that automation still run.
- Skip a private reply for a comment older than Meta's 7-day window, delay
  included, instead of letting the Send API reject it.
- Keep a claimed private anchor travelling as `spent: true` rather than
  dropping it, so each channel's `sendFlowStep` can tell a comment-triggered
  follow-up from a plain flow message. Message two onward now sends as a
  normal DM inside the contact's 24-hour window and otherwise throws
  `comment_private_reply_already_used`, which lands on the message row as a
  `sendError` the inbox shows. Before, Meta rejected it and the error was
  swallowed, so a two-message flow looked half-delivered with no reason.
- Log when the incoming comment's message row is missing: like, hide and
  parent threading were being skipped with no trace.

Media steps (flow-config + builder + Instagram):

- `media-step-rules` / `media-step-validators` describe which media a channel
  accepts, surfaced in the builder as a notice on the send-* step editors.
- Instagram comment replies are text-only (`POST /{ig-comment-id}/replies`
  has no `attachment_url`), so both variants' `sendComment` now throw
  `PAYLOAD_INVALID` instead of returning an empty result behind a warn.
- Instagram-via-Facebook collects a step's messages before sending so quick
  replies land on the last one, and upload failures propagate to the inbox.

Co-authored-by: Deathgiver <[email protected]>
Co-authored-by: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant