Skip to content

Commit 8d76dec

Browse files
committed
fix(metadata-protocol): both publish doors judge a stored flow draft in its D2-converted spelling
Claude-Session: https://claude.ai/code/session_01JQwxbYUL4P4uPHYTjwZQz2 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 744211f commit 8d76dec

8 files changed

Lines changed: 230 additions & 14 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@objectstack/metadata-protocol': patch
3+
'@objectstack/lint': patch
4+
---
5+
6+
fix(metadata-protocol): both publish doors judge a stored flow draft in its D2-converted spelling, as the save door does
7+
8+
Clause-②: yes (widening)
9+
Metadata-protocol: none
10+
11+
A flow draft stored in a legacy ADR-0087 D2 spelling that the automation engine registers (a `get_record` node's `config.filters`, for one) saved as a draft, because the save door judges a flow in its converted spelling and stores the body as written, but the per-item publish (`POST /meta/flow/:name/publish`) and the package publish (`publish-drafts`) then refused it with `422 INVALID_METADATA` (`expression-invalid`, `flow-region-shape-invalid`), because both judged the raw stored body. Both doors, and the draft read's publish verdict, now judge the stored draft through the save door's conversion seam, with the full conversion chain the engine replays when it registers a stored row, and promote it as stored: the active row is the draft's bytes. A draft the flow rules still refuse once converted is refused with the issues its current spelling gets.
12+
13+
The `flow-region-shape-invalid` explanation (`os explain`) now also names the server as a reader of the rule and the body it judges.

‎packages/lint/src/rule-explanations.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5479,7 +5479,9 @@ const FLOW_REGION_SHAPE_INVALID_EXPLANATION: RuleExplanation = {
54795479
'same region check the engine runs when it registers the flow, and the message is the engine\'s ' +
54805480
'refusal behind the flow\'s name. Like the engine it reports a flow\'s first malformed region, so a ' +
54815481
'flow with two may need two fixes. A flow the flow schema refuses cannot be judged and is reported ' +
5482-
'as such, with the schema\'s issues.',
5482+
'as such, with the schema\'s issues. The server asks it too, when a flow is saved active or a draft ' +
5483+
'is published, of the flow with its legacy spellings converted to the current ones: the body the ' +
5484+
'engine registers.',
54835485
'Not judged: a cycle in the flow\'s top-level graph, outside any region. The engine refuses that too, ' +
54845486
'with a check this rule cannot reach.',
54855487
'To fix a multi-exit region, add one node every exit of the region leads to; to fix a cycle, remove ' +

‎packages/lint/src/validate-flow-region-shape.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
/**
44
* `flow-region-shape-invalid` — a flow the automation engine refuses to
55
* register for its ADR-0031 region shape, refused at every authoring command.
6+
* The runtime authoring gate reads it too, at the save of an active flow and
7+
* at both publish doors (the per-item publish and the package publish), and
8+
* judges the body the engine registers: the flow in its ADR-0087 D2-converted
9+
* spelling (#21982, #29557).
610
*
711
* ## What it reports
812
*

‎packages/metadata-protocol/src/protocol-authoring-gate.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import type { ObjectStackProtocolImplementation } from './protocol.js';
5050
import { permissionSetOwnerBindingRefusal, runtimeHookWithoutBodyRefusal } from './protocol.js';
5151
import { objectBodyExtensionFieldSuffixRefusal } from './protocol-extension-field-suffix.js';
5252
import { datasourceCredentialsRefSaveRefusal } from './protocol-datasource-credentials-ref.js';
53+
import { storedFlowVerdictBody } from './protocol-flow-save-replay.js';
5354
import { evaluateRuntimeAuthoringGate } from './runtime-authoring-gate.js';
5455
import type { PackagedRuleCopyRefusal, RuntimeAuthoringIssue, RuntimeAuthoringVerdict, RuntimePendingDeclarations } from './runtime-authoring-gate.js';
5556
import { savedItemNameRefusal } from '@objectstack/metadata/view-container-name';
@@ -544,7 +545,9 @@ export async function draftPublishVerdict(
544545
type: singular,
545546
name: draft.name,
546547
state: 'active',
547-
body: draft.body,
548+
// [#29557] The body the promotion judges: a flow draft in its
549+
// D2-converted spelling, every other draft as stored.
550+
body: storedFlowVerdictBody(singular, draft.body),
548551
packageId: draft.packageId,
549552
...(pending !== undefined ? { pending } : {}),
550553
...(channel

‎packages/metadata-protocol/src/protocol-flow-save-replay.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
* imports from it; what it imports from elsewhere, it imports from where
3030
* `protocol.ts` imported it. Nothing re-exports this module, so the published
3131
* surface does not move.
32+
*
33+
* ## [#29557] The flow verdict body, one seam for every door that judges a flow
34+
*
35+
* {@link flowVerdictBody} below is the conversion the save door's fallback
36+
* verdict (#21982) applies, and {@link storedFlowVerdictBody} is that same
37+
* conversion asked of a STORED flow draft: the body the per-item publish, the
38+
* package publish (both through `promoteDraftForPublish`) and the draft read's
39+
* publish verdict (`draftPublishVerdict`) judge. One judge, one spelling: a
40+
* legacy D2 spelling the save door accepted and stored raw is accepted at
41+
* publish too, and what goes live is still the stored bytes.
3242
*/
3343

3444
import { ALL_CONVERSIONS, applyConversionsToFlow, type ConversionNotice } from '@objectstack/spec';
@@ -113,3 +123,39 @@ export async function flowSaveReplaysRetired(
113123
strikeCarried(active.body);
114124
return uncarried.size === 0;
115125
}
126+
127+
/**
128+
* [#21982] The body a flow is JUDGED in when the save door's canonicalizer did
129+
* not resolve or threw: the request body with the spec's ADR-0087 D2
130+
* conversions applied (`applyConversionsToFlow`), the full chain minus
131+
* {@link FLOW_FALLBACK_VERDICT_NOT_REPLAYED}, with the retired entries when
132+
* `includeRetired` says so. For the VERDICT only: the caller stores the body
133+
* it was handed, never this one. A key no conversion rewrites is judged
134+
* exactly as written.
135+
*/
136+
export function flowVerdictBody(body: unknown, options: { includeRetired: boolean }): unknown {
137+
return applyConversionsToFlow(body, {
138+
includeRetired: options.includeRetired,
139+
excludeConversionIds: FLOW_FALLBACK_VERDICT_NOT_REPLAYED,
140+
});
141+
}
142+
143+
/**
144+
* [#29557] The body a publish judges a STORED draft in: for a `flow`, the
145+
* {@link flowVerdictBody} of the draft row with the retired entries included;
146+
* every other type, the draft as stored. The two publish doors and the draft
147+
* read's publish verdict judge through here and promote, store and serve the
148+
* draft as stored.
149+
*
150+
* A stored draft is a row at rest, so the chain is the full one: the one the
151+
* automation engine replays when it registers the published row (its stored-
152+
* flow canonicalizer, which every caller but the save door calls with no
153+
* options) and the one `applyConversionsToStoredItem` replays on read. The
154+
* retired-entry predicate of the save door ({@link flowSaveReplaysRetired},
155+
* #23903 ruling B') is not asked again: it judges authored input, and the
156+
* draft's own save asked it, in draft mode as in publish mode. `type` is the
157+
* singular type name.
158+
*/
159+
export function storedFlowVerdictBody(type: string, body: unknown): unknown {
160+
return type === 'flow' ? flowVerdictBody(body, { includeRetired: true }) : body;
161+
}

‎packages/metadata-protocol/src/protocol-meta-item-reads.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ export async function readMetaItem(host: Pick<ProtocolHost, 'artifactLockLayerAt
117117
// [#26707] A served DRAFT is decorated as above and then told what its
118118
// publish would refuse: the runtime gate's verdict on the stored draft
119119
// body ({@link draftPublishVerdict}), folded into `_diagnostics`
120-
// ({@link withPublishVerdict}). Judged on the row as stored — the body
121-
// the promotion judges — never on the governed, converted or redacted
122-
// copy this read serves. The execution face serves no `_diagnostics`.
120+
// ({@link withPublishVerdict}). Handed the row as stored — the body the
121+
// promotion reads, and [#29557] converts for its verdict exactly as the
122+
// promotion does — never the governed, converted or redacted copy this
123+
// read serves. The execution face serves no `_diagnostics`.
123124
const serveDraft = async <T>(resolved: T, row: { metadata?: unknown; package_id?: string | null }): Promise<T> => {
124125
const served = await serveFormLayout(serveItem(resolved));
125126
if (execution || !served || typeof served !== 'object') return served;

‎packages/metadata-protocol/src/protocol.draft-publish-verdict.test.ts‎

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030

3131
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
32-
import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate } from '@objectstack/metadata-core';
32+
import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate, hashSpec } from '@objectstack/metadata-core';
3333
import { ObjectStackProtocolImplementation } from './protocol.js';
3434

3535
/** The LIVE object universe: a deal with a formula field, and its account. */
@@ -974,3 +974,145 @@ describe('[#29007] a stored object carrying the retired titleFormat', () => {
974974
expect(saved.success).toBe(true);
975975
});
976976
});
977+
978+
/**
979+
* [#29557] A flow draft stored in a legacy ADR-0087 D2 spelling the engine
980+
* registers (`get_record` `config.filters`, `flow-node-crud-filter-alias`). The
981+
* save door judges a flow on the canonicalizer's fallback in its D2-converted
982+
* spelling and stores the raw body (#21982), so the draft saves; both publish
983+
* doors and the draft read judge the stored draft through the same seam, so it
984+
* publishes, and what goes live is the stored bytes.
985+
*
986+
* The draft is the realistic one: a stored active row carries the spelling (a
987+
* row at rest), and the author's draft re-save of it carries the same hit, so
988+
* the save replays the full chain (#23903 ruling B'). This harness reaches no
989+
* automation service, so every save takes the fallback.
990+
*/
991+
describe('[#29557] a stored flow draft in a legacy D2 spelling the engine registers', () => {
992+
let warn: ReturnType<typeof vi.spyOn>;
993+
beforeEach(() => {
994+
warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
995+
delete process.env.OS_ALLOW_UNLINTED_METADATA_WRITES;
996+
});
997+
afterEach(() => {
998+
warn.mockRestore();
999+
});
1000+
1001+
/** start → get_record → end, the match map spelled under `filterKey`. */
1002+
const fetchFlow = (name: string, filterKey: 'filters' | 'filter') => ({
1003+
name,
1004+
label: name,
1005+
type: 'autolaunched',
1006+
nodes: [
1007+
{ id: 'start', type: 'start', label: 'Start' },
1008+
{ id: 'fetch', type: 'get_record', label: 'Fetch', config: { objectName: 'deal', [filterKey]: { name: 'Acme' }, outputVariable: 'deal' } },
1009+
{ id: 'end', type: 'end', label: 'End' },
1010+
],
1011+
edges: [
1012+
{ id: 'e1', source: 'start', target: 'fetch' },
1013+
{ id: 'e2', source: 'fetch', target: 'end' },
1014+
],
1015+
});
1016+
1017+
/** The stored active row a legacy draft is a re-save of. */
1018+
async function seedActive(engine: any, body: { name: string }) {
1019+
await engine.insert('sys_metadata', {
1020+
type: 'flow',
1021+
name: body.name,
1022+
organization_id: null,
1023+
package_id: PKG,
1024+
state: 'active',
1025+
metadata: JSON.stringify(body),
1026+
checksum: hashSpec(body, 'flow'),
1027+
version: 1,
1028+
});
1029+
}
1030+
1031+
const storedConfig = (rows: Map<string, Row>, name: string, state: 'active' | 'draft') => {
1032+
const row = [...rows.values()].find((r) => r.type === 'flow' && r.name === name && r.state === state);
1033+
return row ? JSON.parse(row.metadata).nodes[1].config : undefined;
1034+
};
1035+
1036+
async function stageLegacy(name: string, body = fetchFlow(name, 'filters')) {
1037+
const { engine, rows } = makeStubEngine();
1038+
const protocol = new ObjectStackProtocolImplementation(engine);
1039+
await seedActive(engine, body);
1040+
const saved = await stageDraft(protocol, 'flow', body);
1041+
expect(saved).toMatchObject({ success: true, state: 'draft' });
1042+
// Stored as written: the conversion is the verdict's, never the row's.
1043+
expect(storedConfig(rows, name, 'draft')).toEqual({ objectName: 'deal', filters: { name: 'Acme' }, outputVariable: 'deal' });
1044+
return { engine, rows, protocol };
1045+
}
1046+
1047+
it('publishes through the per-item door, and the active row is the stored bytes', async () => {
1048+
const { rows, protocol } = await stageLegacy('legacy_fetch');
1049+
1050+
const published: any = await protocol.publishMetaItem({ type: 'flow', name: 'legacy_fetch', packageId: PKG } as any);
1051+
1052+
expect(published.success).toBe(true);
1053+
expect(storedConfig(rows, 'legacy_fetch', 'active')).toEqual({ objectName: 'deal', filters: { name: 'Acme' }, outputVariable: 'deal' });
1054+
expect(storedConfig(rows, 'legacy_fetch', 'draft')).toBeUndefined();
1055+
});
1056+
1057+
it('publishes through the package door, and the active row is the stored bytes', async () => {
1058+
const { rows, protocol } = await stageLegacy('legacy_fetch');
1059+
1060+
const published: any = await protocol.publishPackageDrafts({ packageId: PKG });
1061+
1062+
expect(published).toMatchObject({ success: true, publishedCount: 1, failedCount: 0 });
1063+
expect(storedConfig(rows, 'legacy_fetch', 'active')).toEqual({ objectName: 'deal', filters: { name: 'Acme' }, outputVariable: 'deal' });
1064+
});
1065+
1066+
it('its draft read reports no publish refusal for it', async () => {
1067+
const { rows, protocol } = await stageLegacy('legacy_fetch');
1068+
const stored = JSON.parse([...rows.values()].find((r) => r.name === 'legacy_fetch' && r.state === 'draft')!.metadata);
1069+
1070+
// The draft read's publish verdict, on the stored draft: nothing.
1071+
expect(await (protocol as any).draftPublishVerdict({ type: 'flow', name: 'legacy_fetch', body: stored, packageId: PKG }))
1072+
.toEqual([]);
1073+
// Served, it carries none of the rules the publish refused it on before.
1074+
// (The read's own schema half still judges the served body as stored;
1075+
// that seam is the read's, not the publish verdict's.)
1076+
const codes = ((await draftDiagnostics(protocol, 'flow', 'legacy_fetch')).errors ?? []).map((e) => e.code);
1077+
expect(codes).not.toContain('expression-invalid');
1078+
expect(codes).not.toContain('flow-region-shape-invalid');
1079+
});
1080+
1081+
/** The same flow with a conditional edge whose CEL does not parse. */
1082+
const brokenEdge = (name: string, filterKey: 'filters' | 'filter') => {
1083+
const flow = fetchFlow(name, filterKey);
1084+
flow.edges[1] = { ...flow.edges[1]!, type: 'conditional', condition: 'record.name ==' } as typeof flow.edges[number];
1085+
return flow;
1086+
};
1087+
1088+
/** The per-item door's 422 for a draft of `body`, staged as `stageLegacy` stages it. */
1089+
async function perItemRefusal(body: ReturnType<typeof fetchFlow>) {
1090+
const { engine } = makeStubEngine();
1091+
const protocol = new ObjectStackProtocolImplementation(engine);
1092+
await seedActive(engine, body);
1093+
await stageDraft(protocol, 'flow', body);
1094+
const err: any = await protocol.publishMetaItem({ type: 'flow', name: body.name, packageId: PKG } as any)
1095+
.then(() => null, (e: unknown) => e);
1096+
return { protocol, err };
1097+
}
1098+
1099+
const issueTriples = (issues: Array<{ rule?: string; path?: string; message: string }>) =>
1100+
issues.map((i) => [i.rule, i.path, i.message]).sort();
1101+
1102+
it('a draft still refused once converted is refused at both doors with the issues its canonical spelling gets', async () => {
1103+
const canonical = await perItemRefusal(brokenEdge('broken_fetch', 'filter'));
1104+
const legacy = await perItemRefusal(brokenEdge('broken_fetch', 'filters'));
1105+
1106+
expect({ code: canonical.err?.code, status: canonical.err?.status }).toEqual({ code: 'INVALID_METADATA', status: 422 });
1107+
expect(canonical.err.issues.map((i: any) => i.rule)).toContain('expression-invalid');
1108+
expect({ code: legacy.err?.code, status: legacy.err?.status }).toEqual({ code: 'INVALID_METADATA', status: 422 });
1109+
expect(issueTriples(legacy.err.issues)).toEqual(issueTriples(canonical.err.issues));
1110+
1111+
// The package door refuses the same draft, on the same issues, and
1112+
// publishes nothing.
1113+
const batch: any = await legacy.protocol.publishPackageDrafts({ packageId: PKG });
1114+
expect(batch).toMatchObject({ success: false, publishedCount: 0 });
1115+
expect(batch.failed.map((f: any) => f.name)).toEqual(['broken_fetch']);
1116+
expect(issueTriples(batch.failed[0].issues)).toEqual(issueTriples(canonical.err.issues));
1117+
});
1118+
});

0 commit comments

Comments
 (0)