|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Regression guard for the 2026-07-06 incident: a `record-after-update` flow |
| 5 | + * whose action writes back to its OWN trigger record re-fires itself. Normally |
| 6 | + * the start `condition` suppresses the second fire, but a broken guard makes it |
| 7 | + * INFINITE — HotCRM's `case_escalation` guards on `record.is_escalated != true`, |
| 8 | + * yet a `boolean` field persists as integer `1` on SQLite/libsql and CEL |
| 9 | + * `1 != true` is `true`, so it never trips. During first-boot seed (which awaits |
| 10 | + * automation to settle) that infinite cascade wedged the whole per-env kernel |
| 11 | + * build, leaving the environment unopenable. |
| 12 | + * |
| 13 | + * The engine now breaks the SAME flow re-entering for the SAME record while an |
| 14 | + * execution is still on the stack (see `activeRecordFlows`). This test drives |
| 15 | + * the exact shape: a node executor that re-invokes `execute()` for the same |
| 16 | + * flow+record, simulating the update→afterUpdate→dispatch→execute cascade. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 20 | +import { AutomationEngine } from './engine.js'; |
| 21 | + |
| 22 | +function createTestLogger() { |
| 23 | + return { debug() {}, info() {}, warn() {}, error() {} } as any; |
| 24 | +} |
| 25 | + |
| 26 | +describe('AutomationEngine — record-flow re-entrancy loop guard', () => { |
| 27 | + let engine: AutomationEngine; |
| 28 | + beforeEach(() => { |
| 29 | + engine = new AutomationEngine(createTestLogger()); |
| 30 | + }); |
| 31 | + |
| 32 | + it('breaks a self-triggering flow that re-fires for the same record', async () => { |
| 33 | + let executeCalls = 0; |
| 34 | + let skippedInner = false; |
| 35 | + |
| 36 | + // A node that mimics `case_escalation`'s action: it writes back to the |
| 37 | + // trigger record, which (in the real runtime) re-dispatches the same flow |
| 38 | + // for the same record. Here we invoke execute() directly to model that |
| 39 | + // synchronous cascade. |
| 40 | + engine.registerNodeExecutor({ |
| 41 | + type: 'self_retrigger', |
| 42 | + async execute(_node, _vars, _ctx?: any) { |
| 43 | + executeCalls += 1; |
| 44 | + if (executeCalls > 50) throw new Error('INFINITE LOOP — guard failed to break re-entry'); |
| 45 | + // Re-fire the SAME flow for the SAME record (the loop shape). |
| 46 | + const r = await engine.execute('looping_flow', { |
| 47 | + record: { id: 'case-1', is_escalated: 1 }, // int 1, like SQLite |
| 48 | + object: 'crm_case', |
| 49 | + event: 'record-after-update', |
| 50 | + } as any); |
| 51 | + if ((r.output as any)?.reason === 'reentrancy_loop_guard') skippedInner = true; |
| 52 | + return { success: true }; |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + engine.registerFlow('looping_flow', { |
| 57 | + name: 'looping_flow', |
| 58 | + label: 'Looping', |
| 59 | + type: 'autolaunched', |
| 60 | + nodes: [ |
| 61 | + { id: 'start', type: 'start', label: 'Start' }, |
| 62 | + { id: 'act', type: 'self_retrigger', label: 'Re-fire' }, |
| 63 | + { id: 'end', type: 'end', label: 'End' }, |
| 64 | + ], |
| 65 | + edges: [ |
| 66 | + { id: 'e1', source: 'start', target: 'act' }, |
| 67 | + { id: 'e2', source: 'act', target: 'end' }, |
| 68 | + ], |
| 69 | + }); |
| 70 | + |
| 71 | + const result = await engine.execute('looping_flow', { |
| 72 | + record: { id: 'case-1', is_escalated: 1 }, |
| 73 | + object: 'crm_case', |
| 74 | + event: 'record-after-update', |
| 75 | + } as any); |
| 76 | + |
| 77 | + // The OUTER run completes; the INNER re-entry is broken by the guard |
| 78 | + // (not an infinite loop). executeCalls stays at 1 (the guard short-circuits |
| 79 | + // before the inner run reaches the node again). |
| 80 | + expect(result.success).toBe(true); |
| 81 | + expect(skippedInner).toBe(true); |
| 82 | + expect(executeCalls).toBe(1); |
| 83 | + }); |
| 84 | + |
| 85 | + it('does NOT block a different record (legitimate cross-record fan-out)', async () => { |
| 86 | + const seen: string[] = []; |
| 87 | + engine.registerNodeExecutor({ |
| 88 | + type: 'touch', |
| 89 | + async execute() { return { success: true }; }, |
| 90 | + }); |
| 91 | + engine.registerFlow('per_record', { |
| 92 | + name: 'per_record', label: 'Per record', type: 'autolaunched', |
| 93 | + nodes: [ |
| 94 | + { id: 'start', type: 'start', label: 'Start' }, |
| 95 | + { id: 't', type: 'touch', label: 'Touch' }, |
| 96 | + { id: 'end', type: 'end', label: 'End' }, |
| 97 | + ], |
| 98 | + edges: [ |
| 99 | + { id: 'e1', source: 'start', target: 't' }, |
| 100 | + { id: 'e2', source: 't', target: 'end' }, |
| 101 | + ], |
| 102 | + }); |
| 103 | + for (const id of ['a', 'b', 'c']) { |
| 104 | + const r = await engine.execute('per_record', { record: { id }, object: 'o', event: 'record-after-insert' } as any); |
| 105 | + if (r.success && !(r.output as any)?.reason) seen.push(id); |
| 106 | + } |
| 107 | + // All three distinct records run fully — the guard only trips on re-entry |
| 108 | + // for the SAME record while active. |
| 109 | + expect(seen).toEqual(['a', 'b', 'c']); |
| 110 | + }); |
| 111 | + |
| 112 | + it('does NOT block a different flow on the same record (distinct-flow chain)', async () => { |
| 113 | + engine.registerNodeExecutor({ type: 'noop', async execute() { return { success: true }; } }); |
| 114 | + for (const name of ['flow_x', 'flow_y']) { |
| 115 | + engine.registerFlow(name, { |
| 116 | + name, label: name, type: 'autolaunched', |
| 117 | + nodes: [ |
| 118 | + { id: 'start', type: 'start', label: 'Start' }, |
| 119 | + { id: 'n', type: 'noop', label: 'noop' }, |
| 120 | + { id: 'end', type: 'end', label: 'End' }, |
| 121 | + ], |
| 122 | + edges: [ |
| 123 | + { id: 'e1', source: 'start', target: 'n' }, |
| 124 | + { id: 'e2', source: 'n', target: 'end' }, |
| 125 | + ], |
| 126 | + }); |
| 127 | + } |
| 128 | + const rx = await engine.execute('flow_x', { record: { id: 'rec-1' }, object: 'o', event: 'record-after-update' } as any); |
| 129 | + const ry = await engine.execute('flow_y', { record: { id: 'rec-1' }, object: 'o', event: 'record-after-update' } as any); |
| 130 | + expect(rx.success).toBe(true); |
| 131 | + expect((rx.output as any)?.reason).toBeUndefined(); |
| 132 | + expect(ry.success).toBe(true); |
| 133 | + expect((ry.output as any)?.reason).toBeUndefined(); |
| 134 | + }); |
| 135 | +}); |
0 commit comments