|
| 1 | +/** |
| 2 | + * Trigger-event regression: a delivered event must reach the trigger node's |
| 3 | + * outputs on the `resolveNodeType` hydration path, not just the |
| 4 | + * `hydrateGraphNodeFlags` one. |
| 5 | + * |
| 6 | + * `actor.ts` gates the trigger entry point on `node.is_trigger`. Saved |
| 7 | + * workflow JSON never carries that flag, so it has to come from the registry |
| 8 | + * during hydration — and the two hydration paths are mutually exclusive |
| 9 | + * (`session.ts`: `resolveNodeType` takes `Graph.loadFromDict`, otherwise |
| 10 | + * `hydrateGraphNodeFlags`). `hydrateGraphNodeFlags` set the flag; the resolver |
| 11 | + * path dropped it in two places at once — `createGraphNodeTypeResolver` never |
| 12 | + * put `is_trigger` in its `descriptorDefaults`, and `loadFromDict` never read |
| 13 | + * it — so it silently defaulted to false. |
| 14 | + * |
| 15 | + * The failure was quiet, which is why it survived: the run completed, the |
| 16 | + * trigger node reported success, and the event was simply ignored. The node |
| 17 | + * fell through to `genProcess()`, which waits on a live adapter a |
| 18 | + * delivered-event run does not have, and emitted nothing. Every host passing |
| 19 | + * `resolveNodeType` was affected — the CLI and the websocket server both do. |
| 20 | + * |
| 21 | + * The assertion is the payload value, not merely that something was emitted: |
| 22 | + * the interval trigger's `genProcess()` path also emits a tick, so a test that |
| 23 | + * only checked for output would have passed against the bug. |
| 24 | + */ |
| 25 | +import { describe, it, expect } from "vitest"; |
| 26 | +import { |
| 27 | + BaseNode, |
| 28 | + NodeRegistry, |
| 29 | + createGraphNodeTypeResolver, |
| 30 | + prop |
| 31 | +} from "@nodetool-ai/node-sdk"; |
| 32 | +import type { StreamingOutputs, TriggerEvent } from "@nodetool-ai/node-sdk"; |
| 33 | +import { ExecutionSession } from "../src/index.js"; |
| 34 | + |
| 35 | +/** |
| 36 | + * Minimal stand-in for the shipped trigger nodes: `genProcess` emits a value |
| 37 | + * the delivered event never would, so the two paths are distinguishable. |
| 38 | + */ |
| 39 | +class ProbeTrigger extends BaseNode { |
| 40 | + static readonly nodeType = "test.triggers.Probe"; |
| 41 | + static readonly isTrigger = true; |
| 42 | + static readonly metadataOutputTypes = { body: "any" }; |
| 43 | + |
| 44 | + async process(): Promise<Record<string, unknown>> { |
| 45 | + return {}; |
| 46 | + } |
| 47 | + |
| 48 | + async *genProcess(): AsyncGenerator<Record<string, unknown>> { |
| 49 | + yield { body: "from-genProcess" }; |
| 50 | + } |
| 51 | + |
| 52 | + override async emitTriggerEvent( |
| 53 | + event: TriggerEvent, |
| 54 | + outputs: StreamingOutputs |
| 55 | + ): Promise<void> { |
| 56 | + const payload = (event.payload ?? {}) as { body?: unknown }; |
| 57 | + await outputs.emit("body", payload.body); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +class Sink extends BaseNode { |
| 62 | + static readonly nodeType = "test.Sink"; |
| 63 | + static readonly title = "Sink"; |
| 64 | + static readonly metadataOutputTypes = { output: "any" }; |
| 65 | + |
| 66 | + @prop({ type: "any", default: null }) |
| 67 | + declare value: unknown; |
| 68 | + |
| 69 | + async process(): Promise<Record<string, unknown>> { |
| 70 | + return { output: this.value }; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function registry(): NodeRegistry { |
| 75 | + const r = new NodeRegistry(); |
| 76 | + r.register(ProbeTrigger as never); |
| 77 | + r.register(Sink as never); |
| 78 | + return r; |
| 79 | +} |
| 80 | + |
| 81 | +const graph = { |
| 82 | + nodes: [ |
| 83 | + { id: "trig", type: "test.triggers.Probe", data: {} }, |
| 84 | + { id: "sink", type: "test.Sink", data: {} } |
| 85 | + ], |
| 86 | + edges: [ |
| 87 | + { |
| 88 | + id: "e1", |
| 89 | + source: "trig", |
| 90 | + sourceHandle: "body", |
| 91 | + target: "sink", |
| 92 | + targetHandle: "value" |
| 93 | + } |
| 94 | + ] |
| 95 | +}; |
| 96 | + |
| 97 | +async function runWith(useResolver: boolean) { |
| 98 | + const reg = registry(); |
| 99 | + const session = await ExecutionSession.create({ |
| 100 | + graph: graph as never, |
| 101 | + registry: reg, |
| 102 | + jobId: `trig-${useResolver ? "resolver" : "flags"}`, |
| 103 | + ...(useResolver |
| 104 | + ? { |
| 105 | + resolveNodeType: |
| 106 | + createGraphNodeTypeResolver(reg).resolveNodeType |
| 107 | + } |
| 108 | + : {}), |
| 109 | + triggerEvent: { |
| 110 | + node_id: "trig", |
| 111 | + payload: { body: "from-trigger-event" }, |
| 112 | + input_id: "i1" |
| 113 | + } |
| 114 | + } as never); |
| 115 | + return session.result; |
| 116 | +} |
| 117 | + |
| 118 | +describe("trigger event delivery across both hydration paths", () => { |
| 119 | + it("delivers the payload on the hydrateGraphNodeFlags path", async () => { |
| 120 | + const result = await runWith(false); |
| 121 | + expect(Object.values(result.outputs ?? {}).flat()).toEqual([ |
| 122 | + "from-trigger-event" |
| 123 | + ]); |
| 124 | + }); |
| 125 | + |
| 126 | + it("delivers the payload on the resolveNodeType path", async () => { |
| 127 | + // The regression: this used to yield "from-genProcess", because |
| 128 | + // is_trigger was false so the actor never took the trigger branch. |
| 129 | + const result = await runWith(true); |
| 130 | + expect(Object.values(result.outputs ?? {}).flat()).toEqual([ |
| 131 | + "from-trigger-event" |
| 132 | + ]); |
| 133 | + }); |
| 134 | +}); |
0 commit comments