Skip to content

Commit e905c02

Browse files
committed
test(metadata-protocol): pin the flow read and restore verdicts on a legacy D2 row
Claude-Session: https://claude.ai/code/session_01JQwxbaKY2r8oRQXNGhYHHg Co-authored-by: Claude <noreply@anthropic.com>
1 parent 0eb97fa commit e905c02

2 files changed

Lines changed: 178 additions & 35 deletions

File tree

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

Lines changed: 125 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,44 @@ describe('[#29007] a stored object carrying the retired titleFormat', () => {
975975
});
976976
});
977977

978+
// [#29557, #29696] The legacy-D2 flow fixtures the publish doors (#29557) and the
979+
// reads (#29696) are both pinned on.
980+
981+
/** start → get_record → end, the match map spelled under `filterKey`. */
982+
const fetchFlow = (name: string, filterKey: 'filters' | 'filter') => ({
983+
name,
984+
label: name,
985+
type: 'autolaunched',
986+
nodes: [
987+
{ id: 'start', type: 'start', label: 'Start' },
988+
{ id: 'fetch', type: 'get_record', label: 'Fetch', config: { objectName: 'deal', [filterKey]: { name: 'Acme' }, outputVariable: 'deal' } },
989+
{ id: 'end', type: 'end', label: 'End' },
990+
],
991+
edges: [
992+
{ id: 'e1', source: 'start', target: 'fetch' },
993+
{ id: 'e2', source: 'fetch', target: 'end' },
994+
],
995+
});
996+
997+
/** The stored active row a legacy draft is a re-save of. */
998+
async function seedActive(engine: any, body: { name: string }) {
999+
await engine.insert('sys_metadata', {
1000+
type: 'flow',
1001+
name: body.name,
1002+
organization_id: null,
1003+
package_id: PKG,
1004+
state: 'active',
1005+
metadata: JSON.stringify(body),
1006+
checksum: hashSpec(body, 'flow'),
1007+
version: 1,
1008+
});
1009+
}
1010+
1011+
const storedConfig = (rows: Map<string, Row>, name: string, state: 'active' | 'draft') => {
1012+
const row = [...rows.values()].find((r) => r.type === 'flow' && r.name === name && r.state === state);
1013+
return row ? JSON.parse(row.metadata).nodes[1].config : undefined;
1014+
};
1015+
9781016
/**
9791017
* [#29557] A flow draft stored in a legacy ADR-0087 D2 spelling the engine
9801018
* registers (`get_record` `config.filters`, `flow-node-crud-filter-alias`). The
@@ -998,41 +1036,6 @@ describe('[#29557] a stored flow draft in a legacy D2 spelling the engine regist
9981036
warn.mockRestore();
9991037
});
10001038

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-
10361039
async function stageLegacy(name: string, body = fetchFlow(name, 'filters')) {
10371040
const { engine, rows } = makeStubEngine();
10381041
const protocol = new ObjectStackProtocolImplementation(engine);
@@ -1116,3 +1119,90 @@ describe('[#29557] a stored flow draft in a legacy D2 spelling the engine regist
11161119
expect(issueTriples(batch.failed[0].issues)).toEqual(issueTriples(canonical.err.issues));
11171120
});
11181121
});
1122+
1123+
/**
1124+
* [#29696] A flow READ judges a stored legacy D2 row in the spelling the doors
1125+
* judge it in. A row at rest in a legacy ADR-0087 D2 spelling (`get_record`
1126+
* `config.filters`) is accepted by the save door, both publish doors (#29557)
1127+
* and the engine's canonicalizer; the read's `_diagnostics` schema half judges
1128+
* it through the same seam (`storedFlowVerdictBody`), and serves it as stored.
1129+
* Measured on `origin/main` `0eb97fa4` before the fix: every read below
1130+
* answered `valid: false` with `unrecognized_keys` on `config.filters`.
1131+
*/
1132+
describe('[#29696] a flow read judges a stored legacy D2 row in its D2-converted spelling', () => {
1133+
let warn: ReturnType<typeof vi.spyOn>;
1134+
beforeEach(() => {
1135+
warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
1136+
delete process.env.OS_ALLOW_UNLINTED_METADATA_WRITES;
1137+
});
1138+
afterEach(() => {
1139+
warn.mockRestore();
1140+
});
1141+
1142+
/** The legacy fixture with a config key no conversion rewrites and the parse refuses. */
1143+
const withUndeclaredKey = (name: string, filterKey: 'filters' | 'filter') => {
1144+
const flow = fetchFlow(name, filterKey);
1145+
flow.nodes[1] = { ...flow.nodes[1]!, config: { ...flow.nodes[1]!.config, not_a_config_key: true } } as typeof flow.nodes[number];
1146+
return flow;
1147+
};
1148+
1149+
async function seeded(body: { name: string }) {
1150+
const { engine, rows } = makeStubEngine();
1151+
const protocol = new ObjectStackProtocolImplementation(engine);
1152+
await seedActive(engine, body);
1153+
return { engine, rows, protocol };
1154+
}
1155+
1156+
/** The active item read's `_diagnostics` and the config it serves. */
1157+
async function activeRead(protocol: ObjectStackProtocolImplementation, name: string) {
1158+
const read: any = await protocol.getMetaItem({ type: 'flow', name, packageId: PKG } as any);
1159+
return { diagnostics: read.item._diagnostics, config: read.item.nodes[1].config };
1160+
}
1161+
1162+
it('the active item read reads valid: true and serves the stored bytes', async () => {
1163+
const { protocol } = await seeded(fetchFlow('legacy_fetch', 'filters'));
1164+
1165+
const { diagnostics, config } = await activeRead(protocol, 'legacy_fetch');
1166+
1167+
expect(diagnostics).toEqual({ valid: true });
1168+
expect(config).toEqual({ objectName: 'deal', filters: { name: 'Acme' }, outputVariable: 'deal' });
1169+
});
1170+
1171+
it('the list read reads valid: true and serves the stored bytes', async () => {
1172+
const { protocol } = await seeded(fetchFlow('legacy_fetch', 'filters'));
1173+
1174+
const list: any = await protocol.getMetaItems({ type: 'flow', packageId: PKG });
1175+
const item = (list.items as any[]).find((i) => i.name === 'legacy_fetch');
1176+
1177+
expect(item?._diagnostics).toEqual({ valid: true });
1178+
expect(item.nodes[1].config).toEqual({ objectName: 'deal', filters: { name: 'Acme' }, outputVariable: 'deal' });
1179+
});
1180+
1181+
it('the layered read reads valid: true', async () => {
1182+
const { protocol } = await seeded(fetchFlow('legacy_fetch', 'filters'));
1183+
1184+
const layered: any = await protocol.getMetaItemLayered({ type: 'flow', name: 'legacy_fetch', packageId: PKG } as any);
1185+
1186+
expect(layered._diagnostics).toEqual({ valid: true });
1187+
});
1188+
1189+
it('the draft read of a re-save of it reads valid: true', async () => {
1190+
const body = fetchFlow('legacy_fetch', 'filters');
1191+
const { rows, protocol } = await seeded(body);
1192+
expect(await stageDraft(protocol, 'flow', body)).toMatchObject({ success: true, state: 'draft' });
1193+
expect(storedConfig(rows, 'legacy_fetch', 'draft')).toEqual({ objectName: 'deal', filters: { name: 'Acme' }, outputVariable: 'deal' });
1194+
1195+
expect(await draftDiagnostics(protocol, 'flow', 'legacy_fetch')).toEqual({ valid: true });
1196+
});
1197+
1198+
it('a row still refused once converted reads valid: false, with the errors its canonical spelling reads', async () => {
1199+
const canonical = await activeRead((await seeded(withUndeclaredKey('refused_fetch', 'filter'))).protocol, 'refused_fetch');
1200+
const legacy = await activeRead((await seeded(withUndeclaredKey('refused_fetch', 'filters'))).protocol, 'refused_fetch');
1201+
1202+
expect(canonical.diagnostics.valid).toBe(false);
1203+
expect(JSON.stringify(canonical.diagnostics.errors)).toContain('not_a_config_key');
1204+
expect(legacy.diagnostics).toEqual(canonical.diagnostics);
1205+
// Served as stored: the legacy spelling is what the read hands back.
1206+
expect(legacy.config.filters).toEqual({ name: 'Acme' });
1207+
});
1208+
});

‎packages/metadata-protocol/src/protocol.restore-doors-save-refusal-family.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,3 +591,56 @@ pinBothDoors({
591591
},
592592
expectDetail: (error) => expect(error.message).toBe('refused in state active'),
593593
});
594+
595+
// ─── [#29696] A flow version stored in a legacy ADR-0087 D2 spelling ─────────
596+
//
597+
// The restore doors judge a flow version the way both publish doors judge a
598+
// stored flow draft (#29557): the runtime authoring rules read it in its
599+
// D2-converted spelling (`storedFlowVerdictBody`), and the version is restored
600+
// as stored. Measured on `origin/main` `0eb97fa4` before the fix: a version in
601+
// the legacy `get_record` `config.filters` spelling, which the save door, the
602+
// publish doors and the engine all accept, was refused at both restore doors
603+
// (422 `INVALID_METADATA`, `expression-invalid` at `config.filters`).
604+
605+
const LEGACY_FLOW = 'acme_fetch';
606+
/** start → get_record → end, the match map spelled under `filterKey`; `brokenEdge` adds a conditional edge whose CEL does not parse. */
607+
const fetchFlow = (label: string, filterKey: 'filters' | 'filter', brokenEdge = false) => ({
608+
name: LEGACY_FLOW,
609+
label,
610+
type: 'autolaunched',
611+
nodes: [
612+
{ id: 'start', type: 'start', label: 'Start' },
613+
{ id: 'fetch', type: 'get_record', label: 'Fetch', config: { objectName: 'acme_deal', [filterKey]: { name: 'Acme' }, outputVariable: 'deal' } },
614+
{ id: 'end', type: 'end', label: 'End' },
615+
],
616+
edges: [
617+
{ id: 'e1', source: 'start', target: 'fetch' },
618+
brokenEdge
619+
? { id: 'e2', source: 'fetch', target: 'end', type: 'conditional', condition: 'record.name ==' }
620+
: { id: 'e2', source: 'fetch', target: 'end' },
621+
],
622+
});
623+
624+
pinBothDoors({
625+
title: '[#29696] a flow version in a legacy D2 spelling is judged converted: restored as stored, and refused only where its converted spelling is',
626+
type: 'flow',
627+
name: LEGACY_FLOW,
628+
code: 'INVALID_METADATA',
629+
status: 422,
630+
async stage(h) {
631+
await h.protocol.saveMetaItem({ type: 'flow', name: LEGACY_FLOW, item: fetchFlow('v1', 'filter') });
632+
await h.protocol.saveMetaItem({ type: 'flow', name: LEGACY_FLOW, item: fetchFlow('v2', 'filter') });
633+
await h.protocol.saveMetaItem({ type: 'flow', name: LEGACY_FLOW, item: fetchFlow('v3', 'filter') });
634+
// Both versions as rows at rest in the legacy spelling; v2 also
635+
// carries an edge the rules refuse in any spelling.
636+
rewriteHistory(h, 'flow', LEGACY_FLOW, 1, () => fetchFlow('v1', 'filters'));
637+
rewriteHistory(h, 'flow', LEGACY_FLOW, 2, () => fetchFlow('v2', 'filters', true));
638+
return { refused: 2, accepted: 1 };
639+
},
640+
// Refused on the edge alone: nothing is reported at the legacy spelling.
641+
expectDetail: (error) => {
642+
const rules = (error.issues ?? []).map((i: { rule?: string }) => i.rule);
643+
expect(rules).toContain('expression-invalid');
644+
expect(JSON.stringify(error.issues)).not.toContain('filters');
645+
},
646+
});

0 commit comments

Comments
 (0)