ChatbotX icon indicating copy to clipboard operation
ChatbotX copied to clipboard

Comment and story automations have no per-account scope: with two Pages or two Instagram accounts connected, every automation is evaluated against both

Open rawdaymx opened this issue 2 months ago • 1 comments

Summary

A workspace can connect several accounts of the same channel type: Inbox is unique on (workspaceId, channel, sourceId), and #1107 made connecting several Pages or Instagram accounts in one flow a first-class feature. Comment automations and Instagram story automations carry no account at all. Neither CommentAutomation nor IgStoryAutomation has an inboxId (or any other account) column, and both findActiveAutomations select by workspaceId + isActive + type only. Every active automation of a channel type is therefore evaluated against every comment (or story reply) that arrives on any account of that type.

In practice:

  • An automation whose post targeting is all (the value the create form starts with, create-fb-comment-form.tsx:21) replies on every connected Page of that type, not only on the one the operator had in mind. matchPost returns true for every post type other than postIds.
  • An automation that targets specific posts does end up scoped, because a post id belongs to one Page (the post picker already lists each Page's posts, facebook-posts.ts). It is still evaluated against every other Page's comments, though, and each of those is recorded as a postNotMatched miss for it, so its miss analytics count comments from Pages it was never meant for.
  • Story automations have the same shape (all / storyIds) and no account either, so an all story automation answers story replies on every connected Instagram account.

The product already solved the equivalent problem for broadcasts: #1137 added per-page targeting through a BroadcastTarget table keyed by inboxId.

Environment

Type Static (code). Read from the source tree, not from a deployed instance
Commit upstream/main @ 3a1aaeec1 (2026-09-25)
How measured git grep / git show against that commit (commands below). No automation was run against a live workspace with two accounts

How to see it

This is not a runtime repro: the gap is visible in the schema, the two services and their callers.

  1. Inbox accepts several accounts of the same channel in one workspace:
    $ git grep -n "Inbox_workspaceId_channel_sourceId_key" -- packages/database/src/schema/inbox.ts
    packages/database/src/schema/inbox.ts:40:    uniqueIndex("Inbox_workspaceId_channel_sourceId_key").using(
    
  2. Neither automation table has an account column:
    $ git grep -nE "inboxId" -- packages/database/src/schema/comment-automation.ts packages/database/src/schema/ig-story-automation.ts
    (no output)
    
    Control: the same pattern over packages/database/src/schema/ matches 19 files, including broadcast-target.ts:24.
  3. Both services select by workspace + type only:
    // packages/business/src/comment-automation/service.ts:335-346
    findActiveAutomations(props: { workspaceId: string; channelType: CommentAutomationType }) {
      return db.query.commentAutomationModel.findMany({
        where: { workspaceId: props.workspaceId, isActive: true, type: props.channelType },
      })
    }
    
    packages/business/src/ig-story-automation/service.ts:50-61 has the same shape.
  4. The callers already know the account and never pass it. loadCommentAutomationContext (apps/worker/src/integration/handlers/comment-automation/index.ts:75-106) resolves integrationRow before calling findActiveAutomations at :92; the only use of integrationRow.inboxId in the file is :188, for tag info. processStoryReplyAutomation (story-reply-automation/index.ts:145-162) loads the contactInbox, which carries inboxId, and then calls findActiveAutomations with workspace + channel only.
  5. Consequence for post-targeted automations: matchPost fails for another Page's comment at comment-automation/index.ts:243, collectMiss(automation.id, "postNotMatched") runs at :251, and recordMisses writes it at :780.

Where it comes from

  • packages/business/src/comment-automation/service.ts:335-346 and packages/business/src/ig-story-automation/service.ts:50-61: where: { workspaceId, isActive, type }.
  • packages/database/src/schema/comment-automation.ts:29-143 and packages/database/src/schema/ig-story-automation.ts:28-71: no account column or index.
  • apps/builder/src/features/fb-comments/components/create-fb-comment-form.tsx:21: new automations start with post: { type: "all" }.
  • The fb / ig comment forms and the ig story form have no account selector (no match for inboxId|pageId|accountId), while 21 files under features/broadcasts reference inboxId.

Why we think this is a defect rather than the intended design

The one code comment about this scoping is at comment-automation/index.ts:201-205, added in #1171:

findActiveAutomations scopes by workspace + channel, not by post, so a single comment is shown to every active automation on the channel

It states the current behavior in order to justify batching the miss inserts. It reasons about posts, not accounts, and we read it as describing the behavior rather than deciding that one automation should span every account. Everything around it points the other way: the Inbox key includes sourceId, #1107 made multi-account connect first-class, and #1137 gave broadcasts per-page targeting. We found no test, doc line or comment saying an automation is meant to apply to all accounts. That is "not found", not "proven absent", so this section is a judgment call.

Suggested fix

No PR from us: how to model the scope is a data-model decision for you. Two options both look reasonable:

  1. A nullable inboxId on CommentAutomation / IgStoryAutomation, where NULL keeps today's "every account" behavior, so existing rows need no migration of meaning. findActiveAutomations would take the caller's inboxId (both call sites already have it) and filter inboxId IS NULL OR inboxId = :inboxId. The forms would get an account selector.
  2. A join table in the style of BroadcastTarget, for automations that should cover a subset of accounts. More general, with more migration surface.

Option 1 is the smaller change and keeps current behavior for every existing automation.

Workaround for anyone hitting this

Target specific posts instead of all: the post picker lists each Page's posts, and matchPost compares post ids, so the automation only fires on the Page those posts belong to. The cost is re-selecting posts as new ones are published, plus the postNotMatched misses described above. We established this by reading matchPost, not by running it against two live Pages. Alternatively, keep one account per channel type per workspace.

What we did not verify

  • No live reproduction with two connected Pages or two Instagram accounts. The finding comes from the schema, the services and the callers cited above.
  • Threads and TikTok go through the same findActiveAutomations; we did not check whether those channels allow several accounts per workspace today.
  • The effect on miss analytics was read from the code, not observed in the analytics UI.
  • A maintainer with more context may know of a decision we did not find in the tree.

Related issues (none of these describes it)

Searched issues and PRs (--state all) for IgStoryAutomation, findActiveAutomations, per-page automation, multiple pages, account scope: no issue describes this. The closest hits are #863 (closed; automations stored with the wrong type) and #1228 (flow-id validation), both unrelated to account scope. Control query CommentAutomation returns #1228 plus four PRs, so the search itself works.

rawdaymx avatar Sep 25 '26 19:09 rawdaymx

Thanks a lot for this report, @rawdaymx. It was unusually thorough and easy to act on. The repro commands, file and line references, the "why we think this is a defect" section and the list of what you did not verify saved us a lot of time.

This is fixed in #1500, which went with your option 1:

  • CommentAutomation and IgStoryAutomation now have a nullable inboxId.
    • NULL keeps the old behavior (every account of the channel), so existing automations didn't change.
    • findActiveAutomations now requires the incoming account and matches inboxId IS NULL OR inboxId = :inboxId.
    • Comments and story replies from another Page or Instagram account are no longer answered, and they no longer add postNotMatched misses.
  • The account is checked on every write.
    • We read the integration tables to do this, because Inbox.channel is instagram for both Instagram connection types.
    • The FK is ON DELETE CASCADE rather than SET NULL, so a Page-scoped automation can never quietly widen to every Page.
  • All five forms (Facebook, Instagram, Instagram stories, Threads, TikTok) have an account selector.
    • When two or more accounts of the channel are connected, choosing one is required.
    • Post and story pickers follow the selected account.
  • Agents get the same capability through the public API:
    • commentAutomations.listAccounts
    • inboxId on create, update, get and list
    • an inboxId filter on the post and story list endpoints

On your open questions: Threads and TikTok go through the same findActiveAutomations, so they are scoped the same way. The "process missed comments" replay now scans only the scoped account.

Thanks again!

sung17 avatar Oct 16 '26 15:10 sung17