Skip to content

Commit 2f8383f

Browse files
fix(metadata-protocol): a package duplicate withdraws a copied flow action whose target flow it did not write (#38738)
<!-- claude-projects-attribution: {"github_login":"os-steve"} --> _Requested by **Steve** · [project thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9RgRdwG1S43a4gfYWB4Ytyp)_ Fixes #38685 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:搭建者复制一个软件包时,若某个流程没能复制过去,复制出来的、要运行这个流程的操作、页面按钮、带这类按钮的对象以及提到它的翻译包也一并撤回并在失败清单里写明原因,不再留下一点就报"找不到流程"的坏条目。 - 为什么做:接着上一轮"复制软件包时流程互相调用要跟着改名"的修复,当时只撤回了调用失败流程的流程,漏了操作和其它引用流程的地方。 - 出错会怎样:最坏是复制时多撤回了某个本可保留的条目(例如整个翻译包,或整个对象而指向它的视图仍留着),它会出现在失败清单里,修好源头流程后再复制一次即可恢复,不影响源软件包。 - 要你做什么:请读下方 Acceptance notes 里的三条边界行为,确认它们符合预期。 ## What changes `duplicatePackage` (`packages/metadata-protocol/src/protocol-package-commits.ts`) already points every flow reference in a copied item at the copy's flow. Its post-loop close (from #38595) took back only copied FLOWS whose planned callee was not written. A copied flow action whose `target` names such a flow (case a), or one withdrawn transitively (case b: action, then parent, then child), stayed in `copied[]`, stored targeting a flow that exists nowhere, and answered 404 "flow not found" at run time. - **One predicate, shared with the rename engine** (`package-namespace-rename.ts`). The site resolution the rename's walk used inline (object sites, the derived index's `flow` sites, the `name` of an entry under a site, and the hand-written `target` beside a `'flow'` executor in `type` / `actionType`) moves to one module-level function, `referenceSiteAt`, which `deepRewrite` now calls (same precedence, same answers: the rename's behaviour is unchanged, and its tests are green). A new export, `flowReferencesOf(item)`, walks an item with that same resolver and answers every flow name it holds at a flow reference site, with the path. It is internal: the package's `index.ts` does not re-export it. - **The close covers every copied caller.** The copy loop records, for every copied item (not only flows), the planned flows it names: subflow/map callees as before (`plannedCalleesOf` stays), plus every `flowReferencesOf` hit that is a key of `planFlowSources`. A withdrawn item gets the same undo as before (the address read before the write, `undoBatchRowWrite`), leaves `copied[]`, and is reported in `failed[]` naming the flow and the site. Flows keep today's text ("calls flow 'X' through a subflow or map step"); a `target` site reads "runs flow 'X' through its flow action `target`" (with the path at depth); any other site reads "names flow 'X' at its flow reference `PATH`". The BatchRowHealError and undo-failed branches stay. - Only a withdrawn FLOW makes its own callers go (transitive close as before); a withdrawn action, page, object or translation does not enter `notWritten`, so a flow that happens to share its name is not affected. - A withdrawn item that is also a written object row is removed from the object settle's list, so the settle never judges or undoes it a second time. **NOT MEASURED:** no test pins this line. The package's duplicate tests run on a partial engine double, where `canJudgeBatchObjects` is false, so `writtenObjects` stays empty and the settle never runs; replacing the splice with a no-op leaves every test green (L1 ablation, comment 7275739965). Pinning it needs a judging-registry engine double and its ledger row, which is outside this card. - No refusal is added at any door (case d stays out of scope), and the import leg (`importUnmanagedPackage` + `targetNamespace`) is untouched. ## Tests All pins live in `packages/metadata-protocol/src/package-namespace-rename-flow-refs-38595.test.ts`, as one new top-level `describe` ("duplicatePackage withdraws a copied flow action whose target flow it did not write (#38685)") that reuses that file's harness and engine doubles (the real save door and copy door over a row-keeping stub engine). No new test file, and the engine-double ledger is untouched. - a. the action's target flow fails: the action is withdrawn, not in `copied[]`, in `failed[]` naming `crm2_child` and the `target` site, nothing stored at its address. - b. transitive (action, then parent, then child, child fails): the action goes with the parent, its error naming `crm2_parent` and "was withdrawn". - c. CONTROL: nothing fails, so the action and its flow are both copied and the target resolves. - CONTROL: two `modal` actions whose `target` spells the failed flow (source spelling and copy spelling) stay copied. - A page whose `action:button` block runs the failed flow (`actionType: 'flow'`, at `regions[0].components[0].properties.target`) is withdrawn; a `url` button page beside it stays (CONTROL). - (i) a translation bundle whose `flows` key names the failed flow is withdrawn whole (error names `flows.crm2_child`; no translation row is stored). - (ii) an object whose inline `actions[0]` runs the failed flow is withdrawn whole; (iii) its list view, which names the object, stays copied and stored with `object: 'crm2_lead'`. **Forced change to an existing pin.** In the same file, "a caller whose planned callee fails is withdrawn with it" now expects `failed[]` to be `['action/crm_run_child', 'flow/crm_child', 'flow/crm_parent']`. Its fixture (`callerBody`) carries the flow action `crm_run_child` targeting `crm_child`, the flow that fails, so the old two-flow list pinned the gap itself: it passed only because that action stayed copied, running a flow that exists nowhere. Under the ablation below it goes red with the new pins. Ablation (`scripts/ablation-replace.mjs`, anchor `for (const ref of flowReferencesOf(body)) {` replaced with an empty iterable): 6 red (the forced pin, a, b, the page case, (i), and (ii) with (iii)), 11 green (both CONTROLs and the pre-existing #38595 pins); restore proven (blob == HEAD, `git diff HEAD` empty). (iii) is a preservation pin: it holds with or without the fix, and is red under the ablation only through (ii)'s assertions in the same case. ## Acceptance notes Three edges of "every flow reference site", for the L1 reviewer and the maintainer to read: - **(i) A translation bundle goes whole.** A translation's `flows` keys are a flow reference site the rename rewrites, so a copied bundle that labels a planned flow the copy did not write is taken back whole, with every other label in it (objects, fields, views), not trimmed of the one key. It is reported in `failed[]` naming the flow; fixing that flow and duplicating again brings the bundle back. - **(ii) An object goes whole.** A copied object whose inline action has `type: 'flow'` and a `target` naming such a flow is taken back whole: its row is put back, it leaves `copied[]`, and it is dropped from the object settle (that splice is NOT MEASURED; see above). - **(iii) Referrers of a withdrawn object stay as they are.** Items that name that object (its views, other items pointing at `crm2_lead`) are not cascaded: they stay copied and stored, naming an object the copy no longer holds. This is the same behaviour as today when the object settle refuses a copied object; no new cascade is introduced here. - Out of scope, as triaged: the action save door does not judge a flow `target` (case d), so a direct publish of a flow action naming no flow is still stored. ## Verification Read first (by risk): 1. `packages/metadata-protocol/src/package-namespace-rename.ts` `referenceSiteAt` (around L187) and the `siteOf` call in `deepRewrite` (around L600 and L657): the rename's own site resolution, moved; it must answer exactly what the inline code did. 2. `packages/metadata-protocol/src/protocol-package-commits.ts` the post-loop close (around L770 to L825): the per-site reason text, the object-settle splice, and `notWritten` growing on flows only. 3. `plannedCallsOf` (around L524) and its use at L645: which copied items record a planned call. Two-minute check: `pnpm --filter @objectstack/metadata-protocol exec vitest run src/package-namespace-rename-flow-refs-38595.test.ts` shows 17 passed. Gap before the fix, re-measured on `80ddfb5050` with the seat's probe: 4 passed (the probe pins the gap), e.g. case a answered `copied` holding `action/crm2_run`, `failed` only `flow/crm_child`, the stored action targeting `crm2_child`, no such flow anywhere. All readings below are on head `a4bd4b7775` (origin/main `95ffacc2c5` merged in with a merge commit). <details><summary>Package tests and typecheck</summary> - `pnpm --filter @objectstack/metadata-protocol typecheck` (tsc --noEmit): exit 0. - `pnpm --filter @objectstack/metadata-protocol test`: VERDICT command-exit 0; Test Files 376 passed, 9 skipped (385); Tests 23807 passed, 45 skipped (23852). - `src/package-namespace-rename-flow-refs-38595.test.ts` alone: 17 passed. </details> <details><summary>Ablation</summary> - Mutation landed: anchor 1 to 0, blob 6ab27da to 91cb7c0b1fcf. - Run of the 38595 file: 6 failed, 11 passed (17). - Restored: blob == HEAD (6ab27da), `git diff HEAD` empty. </details> <details><summary>Derived gate families (dispatch-gates --commands, 69)</summary> - 67 exit 0, `pnpm check:engine-double-contract` among them. - `pnpm check:dual-build-cjs-loads`, `pnpm check:lean-entry-closure`: exit 3, PREREQUISITE NOT MET (no full build in this container), NOT MEASURED locally; CI runs them. - `dispatch-gates --ran`: 69 derived, 67 run, 2 NOT-MEASURED (recorded exit 3). - `node scripts/pm/overlap-check.mjs`: VERDICT clean, exit 0 (against origin/main 95ffacc, 0 PR heads). </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01ADHkmQJUGAAa5rz9Yi2DvW)_ <!-- signed: repo:objectstack#3 · os-steve · session_01ADHkmQJUGAAa5rz9Yi2DvW --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent f782a12 commit 2f8383f

4 files changed

Lines changed: 319 additions & 38 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@objectstack/metadata-protocol': patch
3+
---
4+
5+
fix(metadata-protocol): a package duplicate withdraws a copied flow action whose target flow it did not write
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
`duplicatePackage` points every flow reference in a copied item at the copy's flow (a `subflow` / `map` step's `flowName`, a flow action's `target`, an `action:button` block whose `actionType` is `'flow'`). When that flow was not written — it is in `failed[]`, or it was taken back because a flow it calls failed — the close after the copy loop took back only the copied flows that called it. A copied flow action, or a page whose button runs that flow, stayed in `copied[]`, stored running a flow that exists nowhere, and answered 404 "flow not found" at run time.
11+
12+
Now every copied item that names such a flow at a flow reference site is taken back the same way: its address is put back as it was, it leaves `copied[]`, and it is reported in `failed[]` naming the flow and the site (for an action, "runs flow '...' through its flow action `target`"). The sites are the rename engine's own (one internal predicate the rename and the close share), so the close withdraws exactly what the rename rewrote as a flow name. A non-flow action whose `target` happens to spell the flow's name is not a flow reference and stays copied. Two consequences of "every site": a copied translation bundle whose `flows` labels name such a flow is taken back whole, its other labels with it; and a copied object whose inline flow action runs such a flow is taken back whole, while items that name that object (a view of it) stay copied, as they do today when the registry refuses a copied object. The unmanaged import with `targetNamespace` is all-or-nothing and is unchanged.
13+
14+
The fix for a caller: fix the flow named in `failed[]`, then duplicate again.

‎packages/metadata-protocol/src/package-namespace-rename-flow-refs-38595.test.ts‎

Lines changed: 154 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,10 @@ describe('duplicatePackage of a parent-first package — the publish gate resolv
379379
sourcePackageId: 'com.example.crm-base', targetPackageId: 'com.example.crm2', targetNamespace: 'crm2',
380380
});
381381

382+
// [#38685] The fixture's flow action `crm_run_child` targets `crm_child`, so it is a caller too and
383+
// goes with it; a list without it pinned the gap (the action stored running a flow that exists nowhere).
382384
expect(duplicated.failed.map((f: { type: string; name: string }) => `${f.type}/${f.name}`).sort())
383-
.toEqual(['flow/crm_child', 'flow/crm_parent']);
385+
.toEqual(['action/crm_run_child', 'flow/crm_child', 'flow/crm_parent']);
384386
const parent = duplicated.failed.find((f: { name: string }) => f.name === 'crm_parent');
385387
expect(parent.error).toContain("'crm2_child'");
386388
expect(duplicated.copied.filter((c: { type: string }) => c.type === 'flow')).toEqual([]);
@@ -413,3 +415,154 @@ describe('duplicatePackage of a parent-first package — the publish gate resolv
413415
expect(danglingCalls(table, engineFlows)).toEqual([]);
414416
});
415417
});
418+
419+
// [#38685] A copied item that names a planned flow the duplicate did not write is withdrawn
420+
// with it — not only a flow calling it through a subflow / map step (above), but a flow
421+
// action's `target` and every other flow reference site the rename rewrites
422+
// (`flowReferencesOf`, `package-namespace-rename.ts`). Before the fix such an action stayed
423+
// in `copied[]`, stored targeting a flow that exists nowhere: 404 "flow not found" at run time.
424+
describe('duplicatePackage withdraws a copied flow action whose target flow it did not write (#38685)', () => {
425+
/** A flow action on the package's object. */
426+
const flowAction = (name: string, target: string, type = 'flow') => ({ name, label: name, objectName: 'crm_lead', type, target });
427+
428+
/** A page whose one region holds an `action:button` block running `target` through `actionType`. */
429+
const buttonPage = (name: string, target: string, actionType = 'flow') => ({
430+
name,
431+
label: name,
432+
type: 'app',
433+
regions: [{ name: 'main', components: [{ type: 'action:button', properties: { label: 'Run', actionType, target } }] }],
434+
});
435+
436+
/** Every active flow row anywhere, plus the engine's flows: the universe a target can resolve in. */
437+
const flowExistsAnywhere = (table: (name: string) => Row[], engineFlows: readonly string[], name: unknown) =>
438+
typeof name === 'string'
439+
&& (engineFlows.includes(name) || table('sys_metadata').some((r) => r.type === 'flow' && r.state === 'active' && r.name === name));
440+
441+
/**
442+
* Install `extra` as package `crm-base` while the engine holds `crm_ghost`,
443+
* then drop `crm_ghost` and duplicate it into namespace `crm2`: a copied flow
444+
* calling `crm_ghost` fails at the publish gate.
445+
*/
446+
async function duplicateWith(extra: Record<string, unknown>) {
447+
const flows = (extra.flows as Array<{ name: string }> | undefined) ?? [];
448+
const engineFlows = ['other_flow', 'crm_ghost', ...flows.map((f) => f.name)];
449+
const { protocol, table } = makeProtocol(engineFlows);
450+
const src = await protocol.importUnmanagedPackage({ manifest: crmBody(extra), targetPackageId: 'com.example.crm-base' });
451+
expect(src.failed ?? []).toEqual([]);
452+
engineFlows.splice(engineFlows.indexOf('crm_ghost'), 1);
453+
const duplicated = await protocol.duplicatePackage({
454+
sourcePackageId: 'com.example.crm-base', targetPackageId: 'com.example.crm2', targetNamespace: 'crm2',
455+
});
456+
return {
457+
duplicated,
458+
table,
459+
engineFlows,
460+
copy: itemsOf(table, 'com.example.crm2'),
461+
copied: duplicated.copied.map((c: { type: string; name: string }) => `${c.type}/${c.name}`).sort(),
462+
failedKeys: duplicated.failed.map((f: { type: string; name: string }) => `${f.type}/${f.name}`).sort(),
463+
errorOf: (key: string): string => duplicated.failed.find((f: { type: string; name: string }) => `${f.type}/${f.name}` === key)?.error,
464+
};
465+
}
466+
467+
it('a. the action\'s target flow fails: the action is withdrawn, reported naming the flow, and nothing is stored for it', async () => {
468+
const out = await duplicateWith({ flows: [flow('crm_child', 'crm_ghost')], actions: [flowAction('crm_run', 'crm_child')] });
469+
470+
expect(out.failedKeys).toEqual(['action/crm_run', 'flow/crm_child']);
471+
expect(out.copied).not.toContain('action/crm2_run');
472+
expect(out.errorOf('action/crm_run')).toContain("runs flow 'crm2_child' through its flow action `target`");
473+
expect(out.errorOf('action/crm_run')).toContain("the copy of flow 'crm_child' is in failed[]");
474+
// The address is put back: the copy holds no such action, and no stored action targets a flow that exists nowhere.
475+
expect(out.copy['action/crm2_run']).toBeUndefined();
476+
expect(Object.keys(out.copy).filter((k) => k.startsWith('action/') || k.startsWith('flow/'))).toEqual([]);
477+
// The rest of the copy is untouched.
478+
expect(out.copy['object/crm2_lead']).toBeDefined();
479+
});
480+
481+
it('b. transitive: action → parent → child, child fails: the parent goes, and the action with it', async () => {
482+
const out = await duplicateWith({
483+
flows: [flow('crm_parent', 'crm_child'), flow('crm_child', 'crm_ghost')],
484+
actions: [flowAction('crm_run', 'crm_parent')],
485+
});
486+
487+
expect(out.failedKeys).toEqual(['action/crm_run', 'flow/crm_child', 'flow/crm_parent']);
488+
expect(out.copied).not.toContain('action/crm2_run');
489+
expect(out.errorOf('action/crm_run')).toContain("runs flow 'crm2_parent' through its flow action `target`");
490+
expect(out.errorOf('action/crm_run')).toContain("the copy of flow 'crm_parent' was withdrawn");
491+
expect(out.copy['action/crm2_run']).toBeUndefined();
492+
expect(Object.keys(out.copy).filter((k) => k.startsWith('flow/'))).toEqual([]);
493+
});
494+
495+
it('c. CONTROL: nothing fails — the action and its flow are both copied, the action targeting the copy\'s flow', async () => {
496+
const out = await duplicateWith({ flows: [flow('crm_child')], actions: [flowAction('crm_run', 'crm_child')] });
497+
498+
expect(out.duplicated.failed).toEqual([]);
499+
expect(out.copied).toEqual(expect.arrayContaining(['action/crm2_run', 'flow/crm2_child']));
500+
expect(out.copy['action/crm2_run']).toMatchObject({ type: 'flow', target: 'crm2_child' });
501+
expect(out.copy['flow/crm2_child']).toBeDefined();
502+
expect(flowExistsAnywhere(out.table, out.engineFlows, out.copy['action/crm2_run'].target)).toBe(true);
503+
});
504+
505+
it('CONTROL: a non-flow action whose `target` equals the failed planned flow\'s name stays copied', async () => {
506+
const out = await duplicateWith({
507+
flows: [flow('crm_child', 'crm_ghost')],
508+
// The source spelling and the copy's spelling of the failed flow, on an executor whose target is no flow.
509+
actions: [flowAction('crm_open', 'crm_child', 'modal'), flowAction('crm_open_copy', 'crm2_child', 'modal')],
510+
});
511+
512+
expect(out.failedKeys).toEqual(['flow/crm_child']);
513+
expect(out.copied).toEqual(expect.arrayContaining(['action/crm2_open', 'action/crm2_open_copy']));
514+
expect(out.copy['action/crm2_open']).toMatchObject({ type: 'modal', target: 'crm_child' });
515+
expect(out.copy['action/crm2_open_copy']).toMatchObject({ type: 'modal', target: 'crm2_child' });
516+
});
517+
518+
it('a page whose `action:button` block runs the failed flow (`actionType: \'flow\'`, at depth) is withdrawn too; a `url` button page stays', async () => {
519+
const out = await duplicateWith({
520+
flows: [flow('crm_child', 'crm_ghost')],
521+
pages: [buttonPage('crm_home', 'crm_child'), buttonPage('crm_links', '/crm_child', 'url')],
522+
});
523+
524+
expect(out.failedKeys).toEqual(['flow/crm_child', 'page/crm_home']);
525+
expect(out.errorOf('page/crm_home'))
526+
.toContain("runs flow 'crm2_child' through its flow action `regions[0].components[0].properties.target`");
527+
expect(out.copy['page/crm2_home']).toBeUndefined();
528+
expect(out.copied).not.toContain('page/crm2_home');
529+
// CONTROL: the same string beside another executor names no flow.
530+
expect(out.copied).toContain('page/crm2_links');
531+
expect(out.copy['page/crm2_links'].regions[0].components[0].properties.target).toBe('/crm_child');
532+
});
533+
534+
it('(i) a copied translation bundle whose `flows` key names the failed flow is withdrawn whole', async () => {
535+
const out = await duplicateWith({
536+
flows: [flow('crm_child', 'crm_ghost')],
537+
translations: [{ 'zh-CN': { flows: { crm_child: { label: 'Child' } }, objects: { crm_lead: { label: 'Lead' } } } }],
538+
});
539+
540+
expect(out.failedKeys).toEqual(['flow/crm_child', 'translation/crm_zh_cn']);
541+
expect(out.errorOf('translation/crm_zh_cn')).toContain("names flow 'crm2_child' at its flow reference `flows.crm2_child`");
542+
// Whole: the bundle's other labels go with it, and nothing is stored at its address.
543+
expect(out.copied.filter((k: string) => k.startsWith('translation/'))).toEqual([]);
544+
expect(Object.keys(out.copy).filter((k) => k.startsWith('translation/'))).toEqual([]);
545+
});
546+
547+
it('(ii) a copied object whose inline flow action runs the failed flow is withdrawn whole; (iii) a view of it stays as it is', async () => {
548+
const out = await duplicateWith({
549+
flows: [flow('crm_child', 'crm_ghost')],
550+
objects: [{
551+
name: 'crm_lead',
552+
label: 'Lead',
553+
sharingModel: 'private',
554+
fields: { title: { type: 'text', label: 'Title' } },
555+
actions: [{ name: 'run_child', label: 'Run child', type: 'flow', target: 'crm_child' }],
556+
}],
557+
});
558+
559+
expect(out.failedKeys).toEqual(['flow/crm_child', 'object/crm_lead']);
560+
expect(out.errorOf('object/crm_lead')).toContain("runs flow 'crm2_child' through its flow action `actions[0].target`");
561+
expect(out.copied).not.toContain('object/crm2_lead');
562+
expect(out.copy['object/crm2_lead']).toBeUndefined();
563+
// (iii) The referrer is not cascaded, the same as when the object settle refuses an object today:
564+
// the copied view keeps naming the withdrawn object's copy name, and is reported copied.
565+
expect(out.copied).toContain('view/crm2_lead_open');
566+
expect(out.copy['view/crm2_lead_open']).toMatchObject({ object: 'crm2_lead' });
567+
});
568+
});

0 commit comments

Comments
 (0)