Skip to content

fix(worker): forward commentAnchor to getUserData prompts - #1282

Merged
sung17 merged 1 commit into
mainfrom
fix/get-user-data-comment-anchor
Sep 22, 2026
Merged

sung17 merged 1 commit into
mainfrom
fix/get-user-data-comment-anchor

Conversation

@sung17

@sung17 sung17 commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1186.

The bug

A flow started by a comment automation with Private reply = Flow never delivered a getUserData question. The step was excluded from the anchor path on two independent counts, and neither edit works without the other:

  1. The generator never handed it the anchor. MESSAGE_PRODUCING_STEP_TYPES was defined as "whatever is mapped to sendFlowMessage", which silently excluded every step that sends from its own handler. getUserData got commentAnchor: undefined.
  2. The handler never forwarded it. The prompt was enqueued through enqueueFlowStepMessage with no anchor, and the field is optional on the job type so nothing flagged it.

Both consequences fail silently — the challenge row is written before the send and sendFlowStep's catch swallows the channel error, so the step still returns wait and the contact's answer is still captured. Only the question never arrives.

Flow shape Before After
getUserData first Nobody claimed the anchor, so the one comment_id-anchored DM Meta grants per comment went unused and the prompt was rejected as an out-of-window DM The prompt claims the anchor and reaches a brand-new commenter
sendText → getUserData The prompt reached the channel with no anchor, so isCommentPrivateRun was false and assertCommentPrivateReplyFollowUpDeliverable never ran The guard runs and records a readable sendError

Credit to @rawdaymx for the report: three runs of the same flow version on the same day with one variable (window open vs. closed), plus the observation that ErrorLog held zero rows matching %one private reply per comment% — exactly what you'd expect if the guard was never called.

Beyond the reported bug

A public-reply flow was broken too. A public anchor is never consumed, so after the fix a getUserData prompt posts as a comment reply — where the contact's next comment resolves back to the same conversation. Before, it went out as a plain DM on the comment-anchored conversation, which nothing can reach. Not mentioned in the issue; covered by a test here.

The set is no longer hand-written. STEP_PRODUCES_MESSAGE is an exhaustive Record<StepType, boolean> — a new step type does not compile until it is classified. true means the step both receives and may claim the anchor, so a step that sends but cannot carry one stays false. appointmentScheduling and questionnaires send from their own handlers through sendChatMessage, whose job type has no commentAnchor field, and are pinned false with a test until that plumbing exists.

The claim moved to after the handler runs, and is skipped on error. Claiming by step type up front burned the comment's single anchored DM on a step that sent nothing: a handler can fail before its send, and since getUserData declares only [success, skip] an error does not branch — so the run continued to the next step with a spent anchor and the guard refused a private reply that was never sent. This was a regression introduced while writing this fix, caught in review and pinned by a test.

Heavy-queue zod fix (pre-existing). heavyFlowContinuationSchema's commentAnchor omitted spent and automationId. Zod strips unknown keys and the trailing .transform(... as HeavyFlowContinuation) hid the mismatch, so a claimed anchor came back un-spent through an AI step and the next step re-claimed it — firing a second anchored DM that Meta rejects with "The comment is invalid for a private reply".

Channel impact

The fix is in shared worker code, so no integration needed changing:

  • Private reply: Messenger, Instagram and Instagram-via-Facebook all carried the identical bug. TikTok has no flow option for private reply (blocked in the form and again in the worker); Threads has no DM API.
  • Public reply: all five comment channels benefit, including Threads, which has no sendFlowStep handler at all — the comment path is the only one it has.

Known gaps (not closed here)

  • getUserData's date/datetime webview and WhatsApp location prompts still go through sendChatMessage. Instagram never takes that path (URL_QUICK_REPLY_CAPABLE_CHANNELS excludes it); Messenger is exposed. Closing it needs the field plus anchor handling in each Meta channel's outgoing sendMessage — a second feature, so it is left out deliberately. Same blocker as appointmentScheduling/questionnaires.
  • The resume path (runChallenge, the date-picker submit action) carries no anchor and has no field for one, so this fix covers the first prompt only. Pre-existing, unchanged.

Verification

  • pnpm lint clean · check-types clean for worker and worker-config
  • 2695 worker tests pass (242 files); Instagram 86, Messenger 303, sdk 33
  • Every new test was checked red→green by reverting the corresponding line — including the regression test, which goes red the moment the result.status !== "error" guard is removed
  • invariant-guard PASS

🤖 Generated with Claude Code

A flow started by a comment automation with Private reply = Flow never
delivered a `getUserData` question. The step was excluded from the anchor
path on two independent counts, and neither edit works without the other:

1. `MESSAGE_PRODUCING_STEP_TYPES` was defined as "whatever is mapped to
   `sendFlowMessage`", which silently excluded every step that sends from
   its own handler. `getUserData` was handed `commentAnchor: undefined`.
2. Its prompt was enqueued through `enqueueFlowStepMessage` without the
   anchor, and the field is optional on the job type so nothing flagged it.

Two failures, both silent — the challenge row is written before the send
and `sendFlowStep`'s catch swallows the channel error, so the step still
returns `wait` and the contact's answer is still captured; only the
question never arrives:

- A question-first flow claimed nothing, so the one comment_id-anchored DM
  Meta grants per comment went unused and the prompt was rejected as an
  out-of-window DM.
- A question after a `sendText` reached the channel with no anchor, so
  `isCommentPrivateRun` was false and
  `assertCommentPrivateReplyFollowUpDeliverable` never ran.

Replaces the hand-written set with `STEP_PRODUCES_MESSAGE`, an exhaustive
`Record<StepType, boolean>` — a new step type no longer compiles until it
is classified. `true` means the step both receives and may claim the
anchor, so a step that sends but cannot carry one stays `false`:
`appointmentScheduling` and `questionnaires` send through
`sendChatMessage`, whose job type has no `commentAnchor` field, and are
pinned `false` until that plumbing exists.

Also moves the claim to after the handler returns, skipping it on an
`error` result. Claiming by step type up front burned the comment's single
anchored DM on a step that sent nothing: a handler can fail before its
send, and since `getUserData` declares only `[success, skip]` an error does
not branch, so the run moved on to the next step with a spent anchor and
the guard refused a private reply that was never sent.

Fixes the heavy queue's `commentAnchor` zod sub-schema, which omitted
`spent` and `automationId`. Zod strips unknown keys and the trailing
`.transform(... as HeavyFlowContinuation)` hid the mismatch, so a claimed
anchor came back un-spent and the next step re-claimed it, firing a second
anchored DM that Meta rejects.

Closes #1186

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working as expected label Sep 22, 2026
@sung17
sung17 changed the base branch from develop to main September 22, 2026 16:42
@sung17
sung17 merged commit 7e35593 into main Sep 22, 2026
2 checks passed
hello-jackytruong referenced this pull request in hello-jackytruong/ChatWorkAI Nov 22, 2026
A flow started by a comment automation with Private reply = Flow never
delivered a `getUserData` question. The step was excluded from the anchor
path on two independent counts, and neither edit works without the other:

1. `MESSAGE_PRODUCING_STEP_TYPES` was defined as "whatever is mapped to
   `sendFlowMessage`", which silently excluded every step that sends from
   its own handler. `getUserData` was handed `commentAnchor: undefined`.
2. Its prompt was enqueued through `enqueueFlowStepMessage` without the
   anchor, and the field is optional on the job type so nothing flagged it.

Two failures, both silent — the challenge row is written before the send
and `sendFlowStep`'s catch swallows the channel error, so the step still
returns `wait` and the contact's answer is still captured; only the
question never arrives:

- A question-first flow claimed nothing, so the one comment_id-anchored DM
  Meta grants per comment went unused and the prompt was rejected as an
  out-of-window DM.
- A question after a `sendText` reached the channel with no anchor, so
  `isCommentPrivateRun` was false and
  `assertCommentPrivateReplyFollowUpDeliverable` never ran.

Replaces the hand-written set with `STEP_PRODUCES_MESSAGE`, an exhaustive
`Record<StepType, boolean>` — a new step type no longer compiles until it
is classified. `true` means the step both receives and may claim the
anchor, so a step that sends but cannot carry one stays `false`:
`appointmentScheduling` and `questionnaires` send through
`sendChatMessage`, whose job type has no `commentAnchor` field, and are
pinned `false` until that plumbing exists.

Also moves the claim to after the handler returns, skipping it on an
`error` result. Claiming by step type up front burned the comment's single
anchored DM on a step that sent nothing: a handler can fail before its
send, and since `getUserData` declares only `[success, skip]` an error does
not branch, so the run moved on to the next step with a spent anchor and
the guard refused a private reply that was never sent.

Fixes the heavy queue's `commentAnchor` zod sub-schema, which omitted
`spent` and `automationId`. Zod strips unknown keys and the trailing
`.transform(... as HeavyFlowContinuation)` hid the mismatch, so a claimed
anchor came back un-spent and the next step re-claimed it, firing a second
anchored DM that Meta rejects.

Closes #1186

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

Labels

bug Something isn't working as expected

Projects

None yet

1 participant