Skip to content

Commit ea1c60c

Browse files
committed
fix(service-automation): the flow collision line asks the engine's resolver, and says no flow is armed when it refuses
Claude-Session: https://claude.ai/code/session_01ADHkmT3p3cREt88Gsd1rP3 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9f62acd commit ea1c60c

6 files changed

Lines changed: 198 additions & 41 deletions

File tree

‎.changeset/31754-flow-collision-two-packages-neither-armed.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
'@objectstack/service-automation': patch
33
---
44

5-
fix(service-automation): the boot log's flow name collision line says neither flow is armed when two packages hold the name
5+
fix(service-automation): the boot log's flow name collision line says no flow is armed when the engine refuses the name
66

77
Clause-②: no — log text only; what the engine arms, refuses or runs does not change.
88
Metadata-protocol: none
99

10-
When two packages ship a flow of the same name, the engine refuses the name at registration, naming both packages, and arms neither flow (ADR-0150 D3). The boot pull's collision warning still read "arming package 'FIRST' by package id, and shadowing 1 other definition(s). Only the armed definition dispatches." It now reads "arming neither: 2 packages hold the name, and the engine refuses a bare name more than one package holds instead of picking one (ADR-0150 D3). Neither definition dispatches; the 'failed to register flow' warning for 'NAME' names every holder. Rename one of them."
10+
When a second package ships a flow of the same name, the engine's package-scoped resolver refuses the bare name for a caller with no package, and `registerFlow` arms neither flow, naming both packages (ADR-0150 D3). The boot pull's collision warning still read "arming package 'FIRST' by package id", "arming package 'FIRST' because a managed package's flow is sealed" or "arming the declared override …", depending on which other definitions sat beside the two packages, and its log meta named an armed contender.
1111

12-
Every other collision line keeps its wording byte for byte: a package beside a stored row, the declared override beside a stored row, and stored rows of a name no package ships.
12+
The warning now asks the same resolver the engine asks. When it refuses the name, every such line reads "arming neither: the engine refuses this bare name for a caller with no package (ADR-0150 D3), so no definition dispatches; the 'failed to register flow' warning for 'NAME' names every holder. Rename one of them." ("arming none of them" for three or more definitions), and its meta carries `refused: true` and the contenders instead of `armed`/`shadowed`.
13+
14+
A name the resolver serves keeps its line byte for byte, and so does every line when no resolver is attached. `resolveFlowPrecedence` takes the reader as an optional fifth argument (`FlowNameRefusalReader`, exported as a type); the automation plugin passes it.

‎packages/services/service-automation/src/flow-declared-override-first-boot-pull.integration.test.ts‎

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ const shippingStack = () =>
9393
{ strict: false },
9494
) as Record<string, unknown>;
9595

96+
/** [#31754] A second package shipping FLOW's bare name — a clash sealed before ADR-0150 phase 1. */
97+
const OTHER = 'com.acme.other';
98+
const otherStack = () =>
99+
defineStack(
100+
{ manifest: { ...pluginManifest(OTHER), namespace: 'other', name: 'other' }, flows: [ticketFlow('OTHER')] } as never,
101+
{ strict: false },
102+
) as Record<string, unknown>;
103+
104+
/** Whether, and before or after the shipping package, OTHER is registered. */
105+
type Second = 'none' | 'before' | 'after';
106+
96107
type Row = 'override' | 'legacy';
97108

98109
const storedRow = (row: Row) =>
@@ -153,7 +164,7 @@ afterEach(async () => {
153164
* Boot the composition on `file`. `start` runs in a plugin that starts after
154165
* the automation plugin: after the boot pull, before `kernel:ready`.
155166
*/
156-
async function boot(file: string, start: (ql: ObjectQL, automation: AutomationEngine) => Promise<void>) {
167+
async function boot(file: string, start: (ql: ObjectQL, automation: AutomationEngine) => Promise<void>, second: Second = 'none') {
157168
const kernel = new ObjectKernel({ logger: { level: 'fatal' }, gracefulShutdown: false } as never);
158169
kernels.push(kernel);
159170
const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: file }, useNullAsDefault: true });
@@ -174,9 +185,11 @@ async function boot(file: string, start: (ql: ObjectQL, automation: AutomationEn
174185
version: '1.0.0',
175186
dependencies: ['com.objectstack.engine.objectql', 'com.objectstack.service-automation'],
176187
async init(ctx: PluginContext) {
177-
const stack = shippingStack();
178-
await ctx.getService<{ register(m: unknown): Promise<void> }>('manifest')
179-
.register({ ...(stack.manifest as object), ...stack });
188+
const manifests = ctx.getService<{ register(m: unknown): Promise<void> }>('manifest');
189+
const registerStack = (stack: Record<string, unknown>) => manifests.register({ ...(stack.manifest as object), ...stack });
190+
if (second === 'before') await registerStack(otherStack());
191+
await registerStack(shippingStack());
192+
if (second === 'after') await registerStack(otherStack());
180193
const ql = () => ctx.getService<ObjectQL>('objectql');
181194
const automation = () => ctx.getService<AutomationEngine>('automation');
182195
automation().registerTrigger(objectqlRecordTrigger(ql, automation, fired));
@@ -192,7 +205,11 @@ async function boot(file: string, start: (ql: ObjectQL, automation: AutomationEn
192205
}
193206

194207
/** The first boot stores `rows`, in this order; the second, cold, is measured. */
195-
async function measuredBoot(rows: readonly Row[], start: (ql: ObjectQL, automation: AutomationEngine) => Promise<void>) {
208+
async function measuredBoot(
209+
rows: readonly Row[],
210+
start: (ql: ObjectQL, automation: AutomationEngine) => Promise<void>,
211+
second: Second = 'none',
212+
) {
196213
const dir = mkdtempSync(join(tmpdir(), 'qa-28717-'));
197214
dirs.push(dir);
198215
const file = join(dir, 'store.db');
@@ -202,11 +219,16 @@ async function measuredBoot(rows: readonly Row[], start: (ql: ObjectQL, automati
202219
await first.kernel.shutdown();
203220
kernels.splice(kernels.indexOf(first.kernel), 1);
204221
const warn = vi.spyOn(ObjectLogger.prototype, 'warn');
205-
const measured = await boot(file, start);
206-
const collisions = warn.mock.calls
207-
.map(([message]) => String(message))
208-
.filter((message) => message.includes('Flow name collision') && message.includes(`'${FLOW}'`));
209-
return { ...measured, collisions };
222+
const measured = await boot(file, start, second);
223+
const collisionCalls = warn.mock.calls
224+
.filter(([message]) => String(message).includes('Flow name collision') && String(message).includes(`'${FLOW}'`));
225+
const collisions = collisionCalls.map(([message]) => String(message));
226+
// [#31754] The engine's own answer for FLOW: each refusal it logged while arming.
227+
const refusals = warn.mock.calls
228+
.filter(([message, meta]) =>
229+
String(message).endsWith('failed to register flow') && (meta as { flow?: unknown } | undefined)?.flow === FLOW)
230+
.map(([, meta]) => String((meta as { error?: unknown }).error));
231+
return { ...measured, collisions, collisionMeta: collisionCalls.map(([, meta]) => meta), refusals };
210232
}
211233

212234
const insertTicket = (ql: ObjectQL, title: string) => ql.insert(TICKET, { title }, { context: SYS } as never);
@@ -273,3 +295,69 @@ describe('[#28717] a declared .custom flow override is armed from the first boot
273295
expect(collisions).toEqual([]);
274296
});
275297
});
298+
299+
/**
300+
* [#31754, ADR-0150 D3] The collision line agrees with the engine on a real
301+
* cold boot. A second package shipping FLOW's bare name (a clash sealed before
302+
* phase 1) makes the engine's resolver refuse the name for a caller with no
303+
* package, so `registerFlow` arms nothing — in each of the three shapes the
304+
* pull ranks differently (the two packages alone; the declared override beside
305+
* them; a package-less stored row beside them). Every collision line for the
306+
* name then reads "arming neither"/"arming none of them", its meta names no
307+
* armed contender, and the engine's refusal names both packages. The line asks
308+
* the resolver the engine asks (`refusalOf`), never a guess from the shapes.
309+
*/
310+
describe('[#31754] a name a second package also ships: the collision line says no definition is armed, as the engine does', () => {
311+
const SHAPES: ReadonlyArray<readonly [string, readonly Row[]]> = [
312+
['two packages', []],
313+
['the declared override beside a second package', ['override']],
314+
['two packages and a package-less stored row', ['legacy']],
315+
];
316+
for (const [shape, rows] of SHAPES) {
317+
for (const second of ['before', 'after'] as const) {
318+
it(`${shape}, the second package registered ${second} the shipping one`, async () => {
319+
let armedAtPull: unknown = 'unread';
320+
const { automation, collisions, collisionMeta, refusals } = await measuredBoot(rows, async (_ql, engine) => {
321+
armedAtPull = await engine.getFlow(FLOW);
322+
}, second);
323+
324+
// The engine: refused at every arming step, naming both packages; nothing armed.
325+
expect(refusals.length, 'the engine refused the name').toBeGreaterThan(0);
326+
for (const refusal of refusals) {
327+
expect(refusal).toContain('held by 2 packages in this environment');
328+
expect(refusal).toContain(`package "${BASE}"`);
329+
expect(refusal).toContain(`package "${OTHER}"`);
330+
}
331+
expect(armedAtPull).toBeNull();
332+
expect(await automation.getFlow(FLOW)).toBeNull();
333+
expect(automation.getShadowedFlows()).toEqual([]);
334+
335+
// The line: every collision warning for the name agrees.
336+
expect(collisions.length, 'the pull warned about the collision').toBeGreaterThan(0);
337+
for (const line of collisions) {
338+
expect(line).toMatch(/; arming (neither|none of them): the engine refuses this bare name for a caller with no package \(ADR-0150 D3\), so no definition dispatches; /);
339+
expect(line).toContain(`the 'failed to register flow' warning for '${FLOW}' names every holder. Rename one of them.`);
340+
expect(line).not.toMatch(/arming (package|the declared override|a runtime-authored)/);
341+
expect(line).not.toContain('Only the armed definition dispatches');
342+
}
343+
for (const meta of collisionMeta) {
344+
expect(meta).toMatchObject({ flow: FLOW, refused: true });
345+
expect(meta).not.toHaveProperty('armed');
346+
}
347+
});
348+
}
349+
}
350+
351+
it('CONTROL: a name the resolver serves (one package and a package-less row) keeps its line byte for byte', async () => {
352+
const { automation, collisions, collisionMeta, refusals } = await measuredBoot(['legacy'], async () => {});
353+
expect(refusals).toEqual([]);
354+
expect(((await automation.getFlow(FLOW)) as { label?: string } | null)?.label).toBe('BASE');
355+
expect(collisions).toEqual([
356+
`[Automation] Flow name collision: '${FLOW}' is claimed by 2 definitions (package '${BASE}', a runtime-authored ` +
357+
`row (sys_metadata)); arming package '${BASE}' because a managed package's flow is sealed (ADR-0126 §2), and ` +
358+
'shadowing 1 other definition(s). Only the armed definition dispatches. To change what runs, declare it as ' +
359+
`an override in '${PAIR}' (overrides: { package: '${BASE}', basedOn }), or save it under a new name.`,
360+
]);
361+
expect(collisionMeta).toEqual([{ flow: FLOW, armed: { source: 'package', packageId: BASE }, shadowed: [{ source: 'runtime' }] }]);
362+
});
363+
});

‎packages/services/service-automation/src/flow-package-scoped-resolution.test.ts‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,23 +369,30 @@ describe('FlowActivationProjection', () => {
369369
});
370370

371371
/**
372-
* [#31754, ADR-0150 D3] The boot pull's flow name collision line agrees with
373-
* the engine. For a name two packages hold, precedence still hands the
374-
* lexicographically first body on, and `registerFlow` refuses it (red line 2
375-
* above), so the line says NEITHER is armed and points at that refusal —
376-
* never "arming <first> by package id". Every other collision class keeps its
377-
* wording byte for byte (the CONTROL cases below).
372+
* [#31754, ADR-0150 D3] The boot pull's flow name collision line asks the
373+
* resolver the engine asks (`refusalOf`, built here as the plugin builds it,
374+
* from `flowNameResolver`). For a name two packages hold, precedence still
375+
* hands the lexicographically first body on and `registerFlow` refuses it
376+
* (red line 2 above), so the line says NEITHER is armed and points at that
377+
* refusal. The three shapes on a real cold boot are pinned in
378+
* `flow-declared-override-first-boot-pull.integration.test.ts`. CONTROL: with
379+
* no resolver handed in, every arm's line is byte for byte main's.
378380
*/
379381
describe('[#31754] the collision line for a name two packages hold says neither is armed', () => {
380382
const NOTHING_INSTALLED = installedManifestsOf(undefined);
381383
/** The boot pull's two steps over the registry: precedence (its one warning), then registration. */
382384
function pull(): { line: string; refusal: unknown } {
383385
const warn = vi.fn();
386+
const resolver = flowNameResolver({ getService: () => ({ registry }) } as never);
384387
const [winner] = resolveFlowPrecedence(
385388
registry.listItems('flow') as unknown[],
386389
{ warn },
387390
(name) => engine.packagedFlowOwner(name),
388391
NOTHING_INSTALLED,
392+
(name) => {
393+
const resolution = resolver(name, null);
394+
return resolution?.status === 'refused' ? resolution.message : undefined;
395+
},
389396
);
390397
expect(warn).toHaveBeenCalledTimes(1);
391398
let refusal: unknown;
@@ -412,8 +419,12 @@ describe('[#31754] the collision line for a name two packages hold says neither
412419
loaderSet.notify = A;
413420

414421
const { line, refusal } = pull();
415-
expect(line).toContain("is claimed by 2 definitions (package 'com.acme.alpha', package 'com.acme.stranger'); arming neither:");
416-
expect(line).toContain("Neither definition dispatches; the 'failed to register flow' warning for 'notify' names every holder.");
422+
expect(line).toBe(
423+
"[Automation] Flow name collision: 'notify' is claimed by 2 definitions (package 'com.acme.alpha', " +
424+
"package 'com.acme.stranger'); arming neither: the engine refuses this bare name for a caller with " +
425+
"no package (ADR-0150 D3), so no definition dispatches; the 'failed to register flow' warning for " +
426+
"'notify' names every holder. Rename one of them.",
427+
);
417428
expect(line).not.toContain('arming package');
418429
expect(line).not.toContain('by package id');
419430
expect(line).not.toContain('Only the armed definition dispatches');
@@ -427,6 +438,18 @@ describe('[#31754] the collision line for a name two packages hold says neither
427438
});
428439
}
429440

441+
it('CONTROL: with no resolver, two packages keep main\'s line byte for byte (by package id)', () => {
442+
const line = lineOf(
443+
[{ ...leaf('notify'), _packageId: Q, _provenance: 'package' }, { ...leaf('notify'), _packageId: A, _provenance: 'package' }],
444+
{ notify: A },
445+
);
446+
expect(line).toBe(
447+
"[Automation] Flow name collision: 'notify' is claimed by 2 definitions (package 'com.acme.alpha', " +
448+
"package 'com.acme.stranger'); arming package 'com.acme.alpha' by package id, and shadowing 1 other " +
449+
'definition(s). Only the armed definition dispatches. Rename one of them.',
450+
);
451+
});
452+
430453
it('CONTROL: a package beside an environment row keeps its line byte for byte (ADR-0126 §2)', () => {
431454
const line = lineOf(
432455
[{ ...leaf('notify'), _packageId: A, _provenance: 'package' }, { ...leaf('notify'), _provenance: 'org' }],

0 commit comments

Comments
 (0)