ChatbotX icon indicating copy to clipboard operation
ChatbotX copied to clipboard

Templates copy a rule without its conditions: the installed rule shows as Active and can never fire

Open rawdaymx opened this issue 2 months ago • 1 comments

Summary

The template system copies a Trigger's name, active flag, actions and folder, but not its conditions. Condition is its own table (packages/database/src/schema/trigger-condition.ts), keyed to the trigger by triggerId, and packages/business/src/template/adapters/triggers.ts never reads or writes it.

The result is worse than an empty rule, because the two halves combine:

  • A trigger row is created with active: true (schema/trigger.ts:19 defaults it, and the adapter passes the source's value through), so the installed rule appears Active in the list.
  • TriggerMatcher.evaluateTriggerConditions returns false for a trigger with no conditions (trigger-matcher.service.ts:76-78).

So installing a template that contains rules produces rules that look enabled and never fire, with nothing logged and no warning in the install report.

What isolates it. The adapter does warn about things it cannot resolve — unresolved references in actions go through ctx.warn, and so does a folder it cannot map. Conditions are not "resolved and warned about"; they are not consulted at all, so the install report is silent. And conditions are clearly first-class elsewhere in the same package: packages/business/src/trigger/service.ts writes them through conditionModel, and there is a test named trigger-service-update-with-conditions.test.ts covering exactly that.

Environment

Measured against upstream/main = e4913ef2a
How Read the repository. This is a static defect: no instance involved, no install was executed

Measurement

grep -rn conditionModel packages/business/src/template/ returns 0 results. That number is only meaningful with a control, so: the same query over packages/business/src/ returns trigger/service.ts and webhook/service.ts, which is how we know the query works and the zero is a real absence rather than a typo.

What the collector reads (adapters/triggers.ts:153-162) — five fields, none of them conditions:

sourceId, name, active, actions, folderId

What the installer writes (:85-92) — six columns, none of them conditions:

id, workspaceId, name, active, actions, folderId

Where it comes from, and why it looks like an oversight rather than a decision

Condition lives in its own table:

// packages/database/src/schema/trigger-condition.ts
export const conditionModel = pgTable("Condition", {
  ...sharedColumns,
  triggerId: bigintAsString().references(() => triggerModel.id, { onDelete: "cascade", ... }),
  webhookId: ...,
  type: text().notNull(),
  sourceId: text(),
  operator: varchar("operator", { length: 255 }),
  value: jsonb(),
})

The only two places in packages/business/src/template/ that mention conditions at all are comments, and both treat Condition.sourceId as something that travels inside the actions jsonb and is therefore already handled by the generic remapper:

  • adapters/triggers.ts:18-20 — "Condition.sourceId (discriminated by a sibling sourceType of trigger/webhook) lives inside these, handled by the generic remapper's DISCRIMINATED_REFERENCE_FIELDS rule."
  • adapters/flows.ts:44 — a similar note about a flow to trigger reference.

That is a different sourceId. DISCRIMINATED_REFERENCE_FIELDS (packages/flow-config/src/import-export/reference-fields.ts:88-98) keys on a sibling sourceType of trigger or webhook — a reference to a trigger from inside a step config. Condition.sourceId has no sourceType sibling and is not a trigger id; it is a tag id, a custom field id or a plain string, depending on Condition.type. So the comment that would have flagged the gap is the thing that hides it.

Suggested fix

We are not sending a PR for this one, and the reason is the first half: where conditions live in the template payload is a decision about your template format for a child table, and triggers.ts has no precedent for one. Guessing it and shipping it is worse than describing it. The second half, though, is measurable and we did measure it, so here it is in full.

Shape. The collector would read Condition rows where triggerId in ids and attach them to each entry (a nested conditions array is the obvious place, and is backwards compatible: an installer reading entry.conditions ?? [] keeps working with templates published before the change). The installer would insert them inside the existing transaction, after the trigger row, so the all-or-nothing property is preserved.

The remapping, which is the part that is easy to get wrong. Condition.sourceId means three different things, selected by Condition.type. Read off the evaluator (apps/worker/src/trigger/services/condition-evaluator.ts):

Condition.type What sourceId holds Where that is visible Remap against
tagApplied, tagRemoved a tag id :22-25 compares it to eventData.tagId idMaps.tag
customFieldValueChanged a custom field id :33-41 passes it to evaluateCustomFieldCondition(customFieldId, …) idMaps.customField
dateTimeBasedTrigger a custom field id :57-64 passes it to evaluateDateTimeCondition, whose first parameter is literally named customFieldId idMaps.customField
contactInfoUpdated not an id — an info type string such as phone :27-31 compares it to eventData.infoType nothing; copy verbatim
everything else unused / null the return true group nothing

Both kinds are already in TRIGGER_REMAP_KINDS (adapters/triggers.ts:25-40 lists tag and customField), and triggers install late enough that both id maps are populated — the adapter's own comment says so. An unresolved id should go through ctx.warn the same way an unresolved action reference does, so the install report stops being silent.

Getting this wrong fails quietly rather than loudly, which is the argument for your deciding it: a sourceId left pointing at the source workspace's tag id produces a condition that simply never matches — exactly the symptom this issue is about, only now with a row that looks correct.

Workaround for anyone hitting this

After installing a template that contains rules, open each rule and re-add its conditions by hand. To spot them: a rule that shows as Active with an empty condition list will never fire, and select "triggerId", count(*) from "Condition" group by 1 against a freshly installed workspace returns nothing for those triggers.

What we did not verify

  • We did not run a template install. The defect is established from the schema and the adapter's two queries, both read on the commit above; we did not collect a template from one workspace and install it into another to watch the conditions go missing. The reason to be explicit about this: it means we have not seen the empty-condition rule rendered as "Active" in the UI with our own eyes, only derived it from the default and the matcher.
  • We did not check whether the webhook side has the same gap. Condition carries a webhookId as well as a triggerId, and packages/business/src/webhook/service.ts also writes conditionModel, so it is worth looking at — we did not.
  • We did not audit the other adapters for child tables they might also be dropping; this is only about triggers.
  • We did not measure whether any template currently published actually contains rules with conditions. If none does, this is latent rather than live today.

Related issues - none of these describes it

Searched this repository's issues for template conditions, template and trigger conditions. Nothing open covers it. The search instrument was confirmed against a query that does return results.

rawdaymx avatar Oct 02 '26 20:10 rawdaymx

This is already fixed on main. #1669 (e505ac56e) added condition handling to packages/business/src/template/adapters/triggers.ts; the report was measured against e4913ef2a, which predates it.

  • Collect: Condition rows are read in one query and shipped as entry.conditions.
  • Install: rows are inserted in the same transaction, with sourceId remapped per condition type by remapTriggerConditions (trigger-conditions.ts): tag, custom field, sequence, product, pipeline and so on. contactInfoUpdated keeps its info-type string.
  • Install report: a condition that names something that did not come across is dropped and warned through ctx.warn. The trigger is then switched off instead of being left Active and unable to fire.
  • Older templates: a template saved before conditions shipped leaves existing rows alone.
  • Sync and backup: the same rows are covered by sync, backup and restore.

template-trigger-conditions.test.ts passes (16/16). I did not run an end-to-end install into a second workspace, and I did not check whether webhook conditions have the same gap.

realcodesiman avatar Nov 26 '26 01:11 realcodesiman