fix(automations): include nodeId in SEND_DM payloads and keep single execution + ordered dispatch for multi-node turns

Open
#49 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
postgresql, typescript

Research direction

Read apps/api/src/instagram/services/instagram-automation-runner.service.ts alongside apps/workers/src/workers/message-worker.ts, focusing on resumeWaitingExecution, triggerMatchingAutomations, and processSendMessageJob. Trace how multi-node turns create execution rows and schedule jobs, then check the existing worker handling of nodeId and isTerminalStep. Done means one execution per trigger, correct node IDs, ordered sends, and unchanged single-node and AI agent flows.

Written by the indexing model from the issue text.

Description

backend bug good first issue workers
Problem Description

In apps/api/src/instagram/services/instagram-automation-runner.service.ts:

  1. Missing nodeId in SEND_DM job payloads:
    Neither resumeWaitingExecution (lines 486-498) nor triggerMatchingAutomations (lines 867-879) passes nodeId: actionNode.nodeId in backgroundJob.payload.
    As a result, processSendMessageJob (apps/workers/src/workers/message-worker.ts, lines 293 & 313) logs ExecutionStep.nodeId as the generic fallback 'ACTION_SEND_DM' instead of the actual canvas node ID, and falls back to currentExec.currentNodeId for downstream node checks.

  2. Duplicate / overwritten execution state & out-of-order risk when waitForReplyAfterSend === false:
    When DM 1 has waitForReplyAfterSend: false and chains into DM 2, resolveExecutableActionNodes returns [DM 1, DM 2] in the same turn:

    • triggerMatchingAutomations (lines 835-884) creates a separate AutomationExecution row for each node in the loop. Because DM 1 has nextNodeIds, message-worker.ts (line 315) sees hasNextNodes === true and leaves Execution #1 stuck in PENDING instead of COMPLETED.
    • resumeWaitingExecution (lines 467-475) overwrites currentNodeId on the same waitingExecution.id in a loop before the worker runs.
    • Both jobs are queued with runAfter = new Date() when delays are 0, so across multiple worker replicas DM 2 can be picked up before DM 1.

(Note: Normal single-node-per-reply flows with waitForReplyAfterSend: true are unaffected.)


Expected Behavior
  1. Every SEND_DM BackgroundJob payload includes nodeId: actionNode.nodeId so ExecutionStep records the exact node ID.
  2. Each automation trigger creates one AutomationExecution (not one per node in a non-waiting chain), and WAITING / COMPLETED state is driven cleanly by the final node in that turn (isTerminalStep).
  3. Sequential nodes queued in the same turn (waitForReplyAfterSend: false) preserve send order (DM 1 before DM 2) via cumulative runAfter timestamps.
  4. Single-node (waitForReplyAfterSend: true) and ACTION_AI_AGENT flows continue to behave identically with zero regressions.

Where to Look
  • apps/api/src/instagram/services/instagram-automation-runner.service.ts (resumeWaitingExecution L404-508, triggerMatchingAutomations L770-889)
  • apps/workers/src/workers/message-worker.ts (processSendMessageJob L288-334 — already reads job.data.nodeId and job.data.isTerminalStep)

Solution / Implementation Guide (Safe, Minimal Changes)
  1. Pass nodeId in both payloads:

    • Add nodeId: actionNode.nodeId to backgroundJob.payload in both resumeWaitingExecution and triggerMatchingAutomations.
  2. Keep one AutomationExecution per trigger & preserve DM 1 -> DM 2 order:

    • In triggerMatchingAutomations, create one AutomationExecution per trigger (instead of creating a new row on every iteration of actionNodesToExecute), updating currentNodeId and status (WAITING vs PENDING) based on the final node in the turn.
    • Pass isTerminalStep: isLastNodeInTurn && !shouldWait in backgroundJob.payload (which message-worker.ts lines 316-319 already support) so DM 1 doesn't prematurely complete a waiting execution and single-node turns behave identically.
    • Track a cumulative nextRunAfterMs across nodes queued in the same turn (e.g., Math.max(Date.now() + delaySec * 1000, prevRunAfterMs + 1000) for subsequent nodes) so DM 1 is always eligible before DM 2.
Dominant language
TypeScript
Stars
2
Forks
1
Avg merge
6m
Merged PRs (30d)
27

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sumamakhan761/tryinstance

All issues in sumamakhan761/tryinstance

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.