|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #4792 — an embedded host that never seals the vocabulary must be TOLD, not |
| 5 | + * left silent. |
| 6 | + * |
| 7 | + * #4771 moved the ADR-0018 §M1 node-type check out of `registerFlow` and into |
| 8 | + * `sealNodeTypeVocabulary()`. `AutomationServicePlugin` calls it at |
| 9 | + * `kernel:bootstrapped`, so plugin-hosted deployments are covered — but a host |
| 10 | + * that builds `new AutomationEngine()` itself and never calls seal lost the |
| 11 | + * check entirely, with no signal. Those hosts control their own ordering, so |
| 12 | + * the pre-#4771 verdict was *accurate* for them: the fix swapped an unreliable |
| 13 | + * warning for no warning at all, recoverable only by reading a changeset. |
| 14 | + * |
| 15 | + * Two halves, and the second is as load-bearing as the first: |
| 16 | + * |
| 17 | + * 1. the omission is now loud, once per engine instance; |
| 18 | + * 2. the paths that did nothing wrong — the plugin boot, and a host that |
| 19 | + * calls seal itself — gain no log line at all, which is the only thing |
| 20 | + * keeping this warning from becoming the noise #4771 deleted. |
| 21 | + * |
| 22 | + * Reverse-verified while writing: temporarily inverting the guard in |
| 23 | + * `warnIfNodeTypeVocabularyNeverSealed()` (warn when the vocabulary IS sealed) |
| 24 | + * turns the two sentinels below red and the embedded-host cases green-for-the- |
| 25 | + * wrong-reason, so the sentinels really do guard the seal, not just the string. |
| 26 | + */ |
| 27 | + |
| 28 | +import { describe, it, expect, vi } from 'vitest'; |
| 29 | +import { LiteKernel } from '@objectstack/core'; |
| 30 | +import type { Plugin, PluginContext } from '@objectstack/core'; |
| 31 | +import { AutomationEngine } from './engine.js'; |
| 32 | +import { AutomationServicePlugin } from './plugin.js'; |
| 33 | + |
| 34 | +/** Substring that identifies the #4792 line, wherever logs are captured. */ |
| 35 | +const OMISSION_MARKER = 'node-type vocabulary was never sealed'; |
| 36 | + |
| 37 | +/** A flow that needs no plugin-contributed executor: structural nodes only. */ |
| 38 | +const trivialFlow = (name: string) => ({ |
| 39 | + name, |
| 40 | + label: name, |
| 41 | + type: 'autolaunched', |
| 42 | + nodes: [ |
| 43 | + { id: 'start', type: 'start', label: 'Start' }, |
| 44 | + { id: 'end', type: 'end', label: 'End' }, |
| 45 | + ], |
| 46 | + edges: [{ id: 'e1', source: 'start', target: 'end' }], |
| 47 | +}); |
| 48 | + |
| 49 | +/** A flow whose only real node type nothing registers (the #4771 subject). */ |
| 50 | +const approvalFlow = (name: string) => ({ |
| 51 | + name, |
| 52 | + label: name, |
| 53 | + type: 'autolaunched', |
| 54 | + nodes: [ |
| 55 | + { id: 'start', type: 'start', label: 'Start' }, |
| 56 | + { id: 'signoff', type: 'approval', label: 'Sign-off', config: { approvers: [{ type: 'user', value: 'u1' }] } }, |
| 57 | + { id: 'end', type: 'end', label: 'End' }, |
| 58 | + ], |
| 59 | + edges: [ |
| 60 | + { id: 'e1', source: 'start', target: 'signoff' }, |
| 61 | + { id: 'e2', source: 'signoff', target: 'end' }, |
| 62 | + ], |
| 63 | +}); |
| 64 | + |
| 65 | +function loggerCapturing(lines: string[]) { |
| 66 | + const l: any = { |
| 67 | + info: (m: string) => lines.push(`info ${m}`), |
| 68 | + debug: () => {}, |
| 69 | + error: (m: string) => lines.push(`error ${m}`), |
| 70 | + warn: (m: string) => lines.push(`warn ${m}`), |
| 71 | + child: () => l, |
| 72 | + }; |
| 73 | + return l; |
| 74 | +} |
| 75 | + |
| 76 | +const omissionLines = (lines: string[]) => lines.filter((l) => l.includes(OMISSION_MARKER)); |
| 77 | + |
| 78 | +/** |
| 79 | + * Minimal `objectql` stand-in exposing the one seam the boot flow-pull reads, |
| 80 | + * mirroring `flow-node-type-audit.test.ts` — this is how a flow gets registered |
| 81 | + * during `AutomationServicePlugin.start()`, i.e. on the real plugin path. |
| 82 | + */ |
| 83 | +function fakeObjectqlPlugin(flows: unknown[]): Plugin { |
| 84 | + return { |
| 85 | + name: 'fake-objectql', |
| 86 | + version: '1.0.0', |
| 87 | + async init(ctx: PluginContext) { |
| 88 | + (ctx as unknown as { registerService(n: string, s: unknown): void }).registerService('objectql', { |
| 89 | + registry: { |
| 90 | + listItems: (type: string) => (type === 'flow' ? flows : []), |
| 91 | + getObject: () => undefined, |
| 92 | + }, |
| 93 | + }); |
| 94 | + }, |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +const sealedState = (engine: AutomationEngine) => |
| 99 | + (engine as unknown as { nodeTypeVocabularySealed: boolean }).nodeTypeVocabularySealed; |
| 100 | + |
| 101 | +describe('#4792 — a never-sealed node-type vocabulary announces itself at the first execution', () => { |
| 102 | + it('warns on the first execute() of an embedded host that never sealed', async () => { |
| 103 | + const lines: string[] = []; |
| 104 | + const engine = new AutomationEngine(loggerCapturing(lines)); |
| 105 | + // The embedded shape: register the executors, register the flows, run — |
| 106 | + // and never call sealNodeTypeVocabulary(), because no plugin does it here. |
| 107 | + engine.registerFlow('embedded_flow', trivialFlow('embedded_flow')); |
| 108 | + expect(omissionLines(lines), 'registration alone must stay quiet').toEqual([]); |
| 109 | + |
| 110 | + const result = await engine.execute('embedded_flow'); |
| 111 | + expect(result.success).toBe(true); |
| 112 | + |
| 113 | + const warned = omissionLines(lines); |
| 114 | + expect(warned).toHaveLength(1); |
| 115 | + // The line owes both halves AGENTS.md asks a degradation for: what was |
| 116 | + // lost, and the call that restores it. |
| 117 | + expect(warned[0]).toMatch(/^warn /); |
| 118 | + expect(warned[0]).toContain('sealNodeTypeVocabulary()'); |
| 119 | + expect(warned[0]).toContain('NO_EXECUTOR'); |
| 120 | + expect(warned[0]).toContain('kernel:bootstrapped'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('says it ONCE per engine — and every engine instance speaks for itself', async () => { |
| 124 | + const lines: string[] = []; |
| 125 | + const engine = new AutomationEngine(loggerCapturing(lines)); |
| 126 | + engine.registerFlow('a', trivialFlow('a')); |
| 127 | + engine.registerFlow('b', trivialFlow('b')); |
| 128 | + |
| 129 | + await engine.execute('a'); |
| 130 | + await engine.execute('a'); |
| 131 | + await engine.execute('b'); |
| 132 | + expect(omissionLines(lines), 'one line per engine, not one per run').toHaveLength(1); |
| 133 | + |
| 134 | + // Dedupe is per instance, not per process/module: a host that builds one |
| 135 | + // engine per tenant forgot the call on each of them, and a module-level |
| 136 | + // flag would report only whichever engine happened to run first. |
| 137 | + const otherLines: string[] = []; |
| 138 | + const other = new AutomationEngine(loggerCapturing(otherLines)); |
| 139 | + other.registerFlow('a', trivialFlow('a')); |
| 140 | + await other.execute('a'); |
| 141 | + expect(omissionLines(otherLines)).toHaveLength(1); |
| 142 | + }); |
| 143 | + |
| 144 | + it('does not fire for an unknown or disabled flow name — the trigger is a real run', async () => { |
| 145 | + const lines: string[] = []; |
| 146 | + const engine = new AutomationEngine(loggerCapturing(lines)); |
| 147 | + engine.registerFlow('off', trivialFlow('off')); |
| 148 | + await engine.toggleFlow('off', false); |
| 149 | + |
| 150 | + expect((await engine.execute('never_registered')).success).toBe(false); |
| 151 | + expect((await engine.execute('off')).success).toBe(false); |
| 152 | + // Both already answer loudly on their own; the omission is reported when |
| 153 | + // an execution actually starts, which is the moment the issue argues is |
| 154 | + // both safe and certain to be reached. |
| 155 | + expect(omissionLines(lines)).toEqual([]); |
| 156 | + }); |
| 157 | + |
| 158 | + it('warns but does NOT seal — the host keeps authority over closing the vocabulary', async () => { |
| 159 | + const lines: string[] = []; |
| 160 | + const engine = new AutomationEngine(loggerCapturing(lines)); |
| 161 | + engine.registerFlow('embedded_flow', trivialFlow('embedded_flow')); |
| 162 | + await engine.execute('embedded_flow'); |
| 163 | + expect(omissionLines(lines)).toHaveLength(1); |
| 164 | + |
| 165 | + // Auto-sealing here would put two answers behind "who decides the |
| 166 | + // vocabulary is closed", and the second one would not be harmless: after |
| 167 | + // a seal `registerFlow` validates inline, so a host that registers a |
| 168 | + // plugin's executors AFTER its first run — legal, ADR-0018 keeps the |
| 169 | + // vocabulary open — would start getting the false "will fail at |
| 170 | + // execution time" assertions #4771 exists to delete. |
| 171 | + expect(sealedState(engine)).toBe(false); |
| 172 | + engine.registerFlow('later', approvalFlow('later')); |
| 173 | + expect(lines.filter((l) => l.includes('no registered executor or descriptor'))).toEqual([]); |
| 174 | + |
| 175 | + // …and the host's own seal still works, and still reports the finding. |
| 176 | + const audit = engine.sealNodeTypeVocabulary(); |
| 177 | + expect(audit.map((e) => e.flowName)).toEqual(['later']); |
| 178 | + expect(lines.filter((l) => l.includes('no registered executor or descriptor'))).toHaveLength(1); |
| 179 | + }); |
| 180 | + |
| 181 | + it('SENTINEL — a host that called seal itself gets no extra line', async () => { |
| 182 | + const lines: string[] = []; |
| 183 | + const engine = new AutomationEngine(loggerCapturing(lines)); |
| 184 | + engine.registerFlow('embedded_flow', trivialFlow('embedded_flow')); |
| 185 | + expect(engine.sealNodeTypeVocabulary()).toEqual([]); |
| 186 | + |
| 187 | + const before = [...lines]; |
| 188 | + expect((await engine.execute('embedded_flow')).success).toBe(true); |
| 189 | + |
| 190 | + expect(omissionLines(lines)).toEqual([]); |
| 191 | + // Nothing at warn/error was added by the run at all — the new code is |
| 192 | + // reachable here (execute ran) and stayed silent because the host did |
| 193 | + // its part, not because the path was skipped. |
| 194 | + expect(sealedState(engine)).toBe(true); |
| 195 | + const added = lines.slice(before.length).filter((l) => l.startsWith('warn ') || l.startsWith('error ')); |
| 196 | + expect(added).toEqual([]); |
| 197 | + }); |
| 198 | + |
| 199 | + it('SENTINEL — the AutomationServicePlugin path is untouched: sealed at boot, no new log', async () => { |
| 200 | + // `kernel:bootstrapped` fires strictly after every plugin's start() and |
| 201 | + // every kernel:ready handler, so any execute() is necessarily after the |
| 202 | + // seal and this warning can never reach the normal deployment. |
| 203 | + const stdout: string[] = []; |
| 204 | + const spy = vi.spyOn(process.stdout, 'write').mockImplementation(((chunk: unknown) => { |
| 205 | + stdout.push(String(chunk)); |
| 206 | + return true; |
| 207 | + }) as never); |
| 208 | + const kernel = new LiteKernel(); |
| 209 | + kernel.use(fakeObjectqlPlugin([trivialFlow('plugin_flow')])); |
| 210 | + kernel.use(new AutomationServicePlugin()); |
| 211 | + try { |
| 212 | + await kernel.bootstrap(); |
| 213 | + const engine = kernel.getService<AutomationEngine>('automation'); |
| 214 | + expect(sealedState(engine), 'the plugin seals at kernel:bootstrapped').toBe(true); |
| 215 | + |
| 216 | + const bootLines = stdout.length; |
| 217 | + expect((await engine.execute('plugin_flow')).success).toBe(true); |
| 218 | + |
| 219 | + expect(omissionLines(stdout)).toEqual([]); |
| 220 | + expect(stdout.slice(bootLines).join('')).not.toContain('sealNodeTypeVocabulary'); |
| 221 | + } finally { |
| 222 | + spy.mockRestore(); |
| 223 | + await kernel.shutdown(); |
| 224 | + } |
| 225 | + }); |
| 226 | +}); |
0 commit comments