Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/38595-rename-flow-references.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@objectstack/metadata-protocol': patch
---

fix(metadata-protocol): a package copied under a new namespace calls its own flows

Clause-②: no
Metadata-protocol: none

A package copy under a new namespace (an `unmanaged` install with `targetNamespace`, or `duplicatePackage`) renames the package's flows, `crm_child` to `crm2_child`. The names that call those flows now follow them: a `subflow` step's `config.flowName`, a translation's `flows` keys, and an action's `target` when its executor is `flow` (`type: 'flow'` on an action or an API endpoint, `actionType: 'flow'` on an `action:button` or `action:icon` page block). Before, the copy kept the source's name: its step failed on every run with "Flow not found", or, with the source package still installed, ran the source's flow.

A value is rewritten only when it is exactly the name of a flow the copy renames. A flow the package does not hold keeps its name, and so does a `target` beside any other executor. A renamed name that meets a name the item already holds there (a translation's `flows` keyed by both `crm_child` and `crm2_child`) refuses that item, as the same meeting does for an object name. A `duplicatePackage` whose stored rows hold a calling flow before the flow it calls now copies both: the publish gate is told the flows the copy plans to write, as it already was on the `unmanaged` install, where before it refused the caller with `flow-subflow-unknown` and only the called flow was copied. When a planned flow is not copied after all (it lands in `failed[]`), every copied flow that calls it is withdrawn with it, transitively, into `failed[]` with an error naming that flow, so no copied flow calls a flow that exists nowhere. Nothing an author writes changes.

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions packages/metadata-protocol/src/package-namespace-rename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,102 @@ describe('planNamespaceRename — a copied capability, and every grant and requi
});
});


describe('planNamespaceRename — a flow name follows the flow, at every site that calls it (#38595)', () => {
const subflow = (name: string, calls: string) => ({
name, label: name, type: 'autolaunched',
nodes: [
{ id: 'start', type: 'start', label: 'Start' },
{ id: 'call', type: 'subflow', label: 'Call', config: { flowName: calls } },
],
edges: [{ id: 'e1', source: 'start', target: 'call' }],
});
const flowItems: NamespaceRenameItem[] = [
...items,
{ type: 'flow', name: 'crm_parent', body: subflow('crm_parent', 'crm_child') },
{ type: 'flow', name: 'crm_child', body: { name: 'crm_child', label: 'Child', type: 'autolaunched', nodes: [], edges: [] } },
// The prefix swap keeps a flow without the source prefix, so nothing names it anew.
{ type: 'flow', name: 'bare_child', body: { name: 'bare_child', label: 'Bare', type: 'autolaunched', nodes: [], edges: [] } },
];
const plan = (targetNamespace = 'crm2', list = flowItems) => planNamespaceRename({
sourcePackageId: 'com.example.crm', sourceNamespace: 'crm', targetNamespace, items: list, retryVerb: 'install',
});
const itemOf = (out: unknown) => (out as { item: any }).item;

it('a subflow step\'s `config.flowName` naming a package flow takes that flow\'s copy name', () => {
const rename = plan();
expect(rename.copyName('flow', 'crm_child')).toBe('crm2_child');
const parent = itemOf(rename.rewrite('flow', 'crm_parent', flowItems[3]!.body));
expect(parent.name).toBe('crm2_parent');
expect(parent.nodes[1].config.flowName).toBe('crm2_child');
// The edge's `target` is a node id, not a flow: it is never a flow site.
expect(parent.edges[0]).toEqual({ id: 'e1', source: 'start', target: 'call' });
});

it('CONTROL: a flow the package does not hold keeps its name, and so does one the prefix swap keeps', () => {
const rename = plan();
expect(itemOf(rename.rewrite('flow', 'crm_parent', subflow('crm_parent', 'other_flow'))).nodes[1].config.flowName)
.toBe('other_flow');
expect(itemOf(rename.rewrite('flow', 'crm_parent', subflow('crm_parent', 'bare_child'))).nodes[1].config.flowName)
.toBe('bare_child');
// Never a substring: a name that merely contains a renamed flow's is another name.
expect(itemOf(rename.rewrite('flow', 'crm_parent', subflow('crm_parent', 'crm_child_v2'))).nodes[1].config.flowName)
.toBe('crm_child_v2');
});

it('a flow action\'s `target` follows the flow; a `target` beside any other executor does not (CONTROL)', () => {
const rename = plan();
const flowAction = itemOf(rename.rewrite('action', 'crm_run_child', {
name: 'crm_run_child', objectName: 'crm_lead', type: 'flow', target: 'crm_child',
}));
expect(flowAction).toMatchObject({ name: 'crm2_run_child', objectName: 'crm2_lead', type: 'flow', target: 'crm2_child' });
for (const type of ['modal', 'script', 'api']) {
const other = itemOf(rename.rewrite('action', 'crm_open_child', {
name: 'crm_open_child', objectName: 'crm_lead', type, target: 'crm_child',
}));
expect(other.target).toBe('crm_child');
}
});

it('the same `target` rule holds wherever an action sits: an object\'s actions, an API endpoint, a page action block', () => {
const rename = plan();
const object = itemOf(rename.rewrite('object', 'crm_lead', {
name: 'crm_lead', fields: {}, actions: [{ name: 'run', type: 'flow', target: 'crm_child' }, { name: 'open', type: 'modal', target: 'crm_child' }],
}));
expect(object.actions.map((a: any) => a.target)).toEqual(['crm2_child', 'crm_child']);
const api = itemOf(rename.rewrite('api', 'crm_run', { name: 'crm_run', path: '/api/v1/apps/crm/run', method: 'POST', type: 'flow', target: 'crm_child' }));
expect(api.target).toBe('crm2_child');
// On a page block `type` is the component, so the executor is `actionType`.
const page = itemOf(rename.rewrite('page', 'crm_home', {
name: 'crm_home',
regions: [{ components: [
{ type: 'action:button', properties: { label: 'Run', actionType: 'flow', target: 'crm_child' } },
{ type: 'action:button', properties: { label: 'Go', actionType: 'url', target: '/crm_child' } },
] }],
}));
expect(page.regions[0].components.map((c: any) => c.properties.target)).toEqual(['crm2_child', '/crm_child']);
});

it('a translation\'s `flows` keys follow the flows — the index\'s second flow site', () => {
const rename = plan();
const bundle = itemOf(rename.rewrite('translation', 'crm_en', {
name: 'crm_en', locale: 'en', flows: { crm_child: { label: 'Child' }, other_flow: { label: 'Other' } },
}));
expect(bundle.flows).toEqual({ crm2_child: { label: 'Child' }, other_flow: { label: 'Other' } });
});

it('a renamed flow name meeting one the item already holds there is refused, never merged — as for an object', () => {
const out = plan().rewrite('translation', 'crm_en', {
name: 'crm_en', locale: 'en', flows: { crm_child: { label: 'Child' }, crm2_child: { label: 'Copy' } },
});
expect((out as { refusal: string }).refusal).toMatch(
/^Translation 'crm_en' is not copied: its `flows` key 'crm_child' follows the copied flow to 'crm2_child', a key it already holds there/,
);
});

it('CONTROL: a copy that keeps the source namespace renames no flow and rewrites no call', () => {
const rename = plan('crm');
expect(rename.copyName('flow', 'crm_child')).toBe('crm_child');
expect(itemOf(rename.rewrite('flow', 'crm_parent', flowItems[3]!.body)).nodes[1].config.flowName).toBe('crm_child');
});
});
86 changes: 75 additions & 11 deletions packages/metadata-protocol/src/package-namespace-rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export interface NamespaceRename {
*/
copyNameRefusal(type: string, name: string): string | undefined;
/**
* The body as the copy writes it: every name the copy renames rewritten,
* The body as the copy writes it: every name the copy renames rewritten
* (an object's, and a flow's at the sites that call a flow, #38595),
* `name` set to {@link copyName}, a permission set's field-rule keys
* following the renamed objects — or the collision that refuses it.
* `item` is the body converted to today's protocol; `name` is its SOURCE
Expand Down Expand Up @@ -138,6 +139,21 @@ function isCapabilityType(type: string): boolean {
return (PLURAL_TO_SINGULAR[String(type)] ?? String(type)) === 'capability';
}

/** A `flow` row, in either spelling of the type. */
function isFlowType(type: string): boolean {
return (PLURAL_TO_SINGULAR[String(type)] ?? String(type)) === 'flow';
}

/**
* [#38595] The keys that name an action's EXECUTOR beside its `target`: `type`
* on an action (`ActionSchema`, an inline action, an `action:group` member, an
* API endpoint), `actionType` on an `action:button` / `action:icon` page block,
* whose `type` is the component itself. When the executor is `'flow'`, the
* `target` is a flow name ("the flow name to invoke", `ActionSchema`'s
* docblock), so it is a flow reference site. See `flowTargetSite` below.
*/
const FLOW_EXECUTOR_KEYS = ['type', 'actionType'] as const;

/**
* [#35157] The keys a body names capabilities under: a permission set GRANTS
* them (`systemPermissions`) and a resource REQUIRES them
Expand Down Expand Up @@ -463,10 +479,58 @@ export function planNamespaceRename(input: NamespaceRenameInput): NamespaceRenam
const objectReferenceProperties = new Set(
(REFERENCE_SITES.byTarget.get('object') ?? []).map((site) => site.property),
);
type ReferenceCollision = { at: string; shape: 'key' | 'entry'; moved: string; met: string };
const deepRewrite = (v: any, collisions: ReferenceCollision[], atReference = false, at = ''): any => {

// [#38595] ⭐ A FLOW NAME FOLLOWS THE FLOW, AS AN OBJECT NAME FOLLOWS THE
// OBJECT. A package flow is copied under the prefix swap (`crm_child` →
// `crm2_child`), and a flow is called by name: a `subflow` step's
// `config.flowName`, a flow action's `target`. Left as written, the copy's
// step called `crm_child` — a flow no copy holds, so every run failed, or,
// with the source package still installed, the SOURCE's flow. So every
// value that names a flow this copy renames is rewritten to the copy's
// name, whole and only whole, at a flow reference site: the derived index's
// `flow` sites (`REFERENCE_SITES`, target `flow`: `flowName`, a
// translation's `flows` keys), read by property wherever it occurs, as the
// object sites are, since a node config is declared by a node descriptor
// the index reaches only through that property. A flow the package does
// not hold, or one the prefix swap keeps (no source prefix), is not
// renamed, so every name of it stays as written. A name that is not a
// renamed flow's falls through to the rules every string takes, so
// nothing that read the same before reads differently now.
//
// ⛔ REFUSED, NEVER MERGED, exactly as at an object site: a renamed flow
// name meeting one the item already holds there (a translation's `flows`
// keyed by both `crm_child` and `crm2_child`) is a collision.
const flowRenames = new Map<string, string>();
for (const item of items) {
if (!isFlowType(item?.type) || typeof item.name !== 'string') continue;
const nn = renameName(item.name);
if (nn !== item.name) flowRenames.set(item.name, nn);
}
const flowReferenceProperties = new Set(
(REFERENCE_SITES.byTarget.get('flow') ?? []).map((site) => site.property),
);
type ReferenceSiteRename = { readonly noun: 'object' | 'flow'; readonly renames: ReadonlyMap<string, string> };
const OBJECT_SITE: ReferenceSiteRename = { noun: 'object', renames: renameMap };
const FLOW_SITE: ReferenceSiteRename = { noun: 'flow', renames: flowRenames };
// ⚠️ THE ONE HAND-WRITTEN FLOW SITE: an action's `target` when its
// executor is `'flow'` ({@link FLOW_EXECUTOR_KEYS}). The derived index
// cannot carry it — `target` names a URL, a script, a page, an endpoint
// or a form on the other executors, so a property-wide site would rewrite
// those too, and its "Used by" answer (`findReferencesToMeta`) would list
// them as flow references. The site is keyed on the declared shape, the
// `target` beside a `'flow'` executor in one object, and nothing else.
const flowTargetSite = (node: Record<string, unknown>, key: string): ReferenceSiteRename | undefined =>
key === 'target' && typeof node.target === 'string'
&& FLOW_EXECUTOR_KEYS.some((executor) => node[executor] === 'flow')
? FLOW_SITE
: undefined;
const referenceSiteOf = (key: string): ReferenceSiteRename | undefined =>
objectReferenceProperties.has(key) ? OBJECT_SITE : flowReferenceProperties.has(key) ? FLOW_SITE : undefined;

type ReferenceCollision = { at: string; shape: 'key' | 'entry'; noun: 'object' | 'flow'; moved: string; met: string };
const deepRewrite = (v: any, collisions: ReferenceCollision[], site?: ReferenceSiteRename, at = ''): any => {
if (typeof v === 'string') {
const whole = atReference ? renameMap.get(v) : undefined;
const whole = site ? site.renames.get(v) : undefined;
if (whole !== undefined) return whole;
const viewItem = viewItemRenames.get(v);
if (viewItem !== undefined) return viewItem;
Expand All @@ -484,16 +548,16 @@ export function planNamespaceRename(input: NamespaceRenameInput): NamespaceRenam
return last === 0 ? objectPass(v) : out + objectPass(v.slice(last));
}
if (Array.isArray(v)) {
const out = v.map((element, i) => deepRewrite(element, collisions, atReference, `${at}[${i}]`));
if (atReference) {
const out = v.map((element, i) => deepRewrite(element, collisions, site, `${at}[${i}]`));
if (site) {
const firstAt = new Map<string, number>();
out.forEach((element, i) => {
if (typeof element !== 'string') return;
const j = firstAt.get(element);
if (j === undefined) {
firstAt.set(element, i);
} else if (v[j] !== v[i]) {
collisions.push({ at, shape: 'entry', moved: v[i] !== element ? v[i] : v[j], met: element });
collisions.push({ at, shape: 'entry', noun: site.noun, moved: v[i] !== element ? v[i] : v[j], met: element });
}
});
}
Expand All @@ -503,10 +567,10 @@ export function planNamespaceRename(input: NamespaceRenameInput): NamespaceRenam
const o: any = {};
const from = new Map<string, string>();
for (const [k, val] of Object.entries(v)) {
const key = atReference ? (renameMap.get(k) ?? k) : k;
const key = site ? (site.renames.get(k) ?? k) : k;
const other = from.get(key);
if (other !== undefined) {
collisions.push({ at, shape: 'key', moved: key === k ? other : k, met: key });
collisions.push({ at, shape: 'key', noun: site?.noun ?? 'object', moved: key === k ? other : k, met: key });
}
from.set(key, k);
if (CAPABILITY_REFERENCE_KEYS.has(k)) {
Expand All @@ -515,7 +579,7 @@ export function planNamespaceRename(input: NamespaceRenameInput): NamespaceRenam
}
const entry = deepRewrite(
val, collisions,
objectReferenceProperties.has(k) || (atReference && k === 'name'),
referenceSiteOf(k) ?? (site && k === 'name' ? site : flowTargetSite(v, k)),
at ? `${at}.${k}` : k,
);
// ⭐ A RECORD ENTRY'S `name` FOLLOWS ITS KEY. An entry that
Expand All @@ -540,7 +604,7 @@ export function planNamespaceRename(input: NamespaceRenameInput): NamespaceRenam
return v;
};
const describeCollision = (c: ReferenceCollision): string =>
`its \`${c.at}\` ${c.shape} '${c.moved}' follows the copied object to '${c.met}', `
`its \`${c.at}\` ${c.shape} '${c.moved}' follows the copied ${c.noun} to '${c.met}', `
+ `${c.shape === 'key' ? 'a key' : 'an entry'} it already holds there, which this copy does not rename. `
+ (c.shape === 'key'
? 'A record keeps one entry per key, so the copy would drop one of the two. '
Expand Down
19 changes: 12 additions & 7 deletions packages/metadata-protocol/src/protocol-door-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ type SaveMetaItemRequest = {
*/
inheritedGrantObjects?: readonly string[],
/**
* [#35143] The names of the flows the same package copy writes, the copy
* door's plan (`importUnmanagedPackage`, whose items are the request's
* body; `duplicatePackage` copies stored rows, which the gate's own read
* of the stored flows already holds). A `flow`
* write's `subflow` / `map` call naming one of them is a call to a flow the
* copy holds once its loop ends, so the publish gate resolves it, as it
* resolves a flow of a `publishPackageDrafts` batch (`pending.flows`).
* [#35143, #38595] The names of the flows the same package copy plans to
* write, under the names it writes them as: the copy door's plan. Both
* copy doors state it on each flow write: `importUnmanagedPackage`, whose
* items are the request's body, and `duplicatePackage`, whose items are
* stored rows renamed into the target namespace (a copied step calls the
* COPY's flow, which no stored row holds until the loop writes it). A
* `flow` write's `subflow` / `map` call naming one of them is resolved by
* the publish gate, as it resolves a flow of a `publishPackageDrafts`
* batch (`pending.flows`). The plan holds once the loop ends: the import
* is all-or-nothing, and on the best-effort duplicate a copied caller
* whose planned callee was not written is withdrawn with it, into
* `failed[]`.
* Additive only: a name can let a call through, never refuse one. Set by
* server code only; no HTTP door forwards it.
*/
Expand Down
Loading
Loading