Skip to content

Commit e6ddd60

Browse files
committed
refactor(metadata-protocol): move the flow save's retired-spelling replay verdict out of protocol.ts (#25467 cut 18)
Move the body of flowSaveReplaysRetired unedited into protocol-flow-save-replay.ts, behind a FlowSaveReplayHost that hands over getOverlayRepo as a closure. The class keeps the method, its docblock and declared signature; its body becomes a host literal plus a .call. FLOW_FALLBACK_VERDICT_NOT_REPLAYED gains export in place, and ALL_CONVERSIONS leaves protocol.ts' spec import with its last reader. Claude-Session: https://claude.ai/code/session_016SP1spj613tyGCgxoD1An6 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 0a1061a commit e6ddd60

2 files changed

Lines changed: 123 additions & 54 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#25467 cut 18] The flow save's retired-spelling replay verdict of
5+
* `ObjectStackProtocolImplementation` (`flowSaveReplaysRetired`, #23903
6+
* ruling B′): whether a flow save may keep the full conversion chain, because
7+
* every retired spelling its body carries is one a stored row already carries,
8+
* moved out of `protocol.ts` whole.
9+
*
10+
* A MOVE, not a redesign. The function below is the body of the
11+
* `ObjectStackProtocolImplementation` method of the same name as it stood,
12+
* re-indented one level and otherwise unedited: the same retired set, the same
13+
* conversion pass and site naming, and the same stored-row reads in the same
14+
* order.
15+
*
16+
* ## How the moved body reaches the class
17+
*
18+
* The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts`
19+
* header carries the full reasoning), as `protocol-batch-data.ts` applies it.
20+
* The class keeps the method — the published `dist/*.d.ts` lists its name,
21+
* docblock and declared signature — and its body is one literal of exactly
22+
* the {@link FlowSaveReplayHost} members it reads, plus a `.call`. The overlay
23+
* repository is handed over as a closure that calls the method on the protocol
24+
* at call time, so a test double or spy set on the instance is still the one
25+
* the body reaches. Every other reader of the method keeps calling the class
26+
* method.
27+
*
28+
* What the body reads of `protocol.ts`'s own module-level declarations it
29+
* imports from it; what it imports from elsewhere, it imports from where
30+
* `protocol.ts` imported it. Nothing re-exports this module, so the published
31+
* surface does not move.
32+
*/
33+
34+
import { ALL_CONVERSIONS, applyConversionsToFlow, type ConversionNotice } from '@objectstack/spec';
35+
import { itemDiscriminator } from '@objectstack/metadata-core';
36+
import type { MetadataWriteFace, ObjectStackProtocolImplementation } from './protocol.js';
37+
import { FLOW_FALLBACK_VERDICT_NOT_REPLAYED } from './protocol.js';
38+
39+
/**
40+
* The `ObjectStackProtocolImplementation` members the function below reads —
41+
* every one of them, and no other; the function's `this` is the `Pick` of the
42+
* members it reads. Each is typed AS the class member (an indexed access on
43+
* the class), so a change to a member's signature on the class is a change
44+
* here too, and naming a member the class does not have is a compile error.
45+
*/
46+
export interface FlowSaveReplayHost {
47+
getOverlayRepo: ObjectStackProtocolImplementation['getOverlayRepo'];
48+
}
49+
50+
/** The body of `ObjectStackProtocolImplementation#flowSaveReplaysRetired`; the method's docblock there is its contract. */
51+
export async function flowSaveReplaysRetired(
52+
this: Pick<FlowSaveReplayHost, 'getOverlayRepo'>,
53+
request: {
54+
name: string;
55+
item?: unknown;
56+
packageId?: string | null;
57+
source?: string;
58+
writeFace?: MetadataWriteFace;
59+
},
60+
mode: 'draft' | 'publish'
61+
): Promise<boolean> {
62+
if (request.source !== undefined) return true;
63+
if (request.writeFace === 'package-duplicate' || request.writeFace === 'package-unmanaged-import') return true;
64+
const retired = new Set(ALL_CONVERSIONS.filter((c) => c.retiredFromLoadPath).map((c) => c.id));
65+
// The site of a notice's path, ids standing in for the indexes of the
66+
// array elements that carry one, read on the converted body it names.
67+
const siteOf = (root: unknown, path: string): string => {
68+
let at: unknown = root;
69+
let site = '';
70+
for (const [, key, index] of path.matchAll(/([^.[\]]+)|\[(\d+)\]/g)) {
71+
if (key !== undefined) {
72+
site += site === '' ? key : `.${key}`;
73+
at = at !== null && typeof at === 'object' ? (at as Record<string, unknown>)[key] : undefined;
74+
continue;
75+
}
76+
const element: unknown = Array.isArray(at) ? at[Number(index)] : undefined;
77+
const id = element !== null && typeof element === 'object' ? (element as { id?: unknown }).id : undefined;
78+
site += typeof id === 'string' && id !== '' ? `[#${id}]` : '[' + index + ']';
79+
at = element;
80+
}
81+
return site;
82+
};
83+
const retiredHits = (body: unknown): Set<string> => {
84+
const fired: ConversionNotice[] = [];
85+
const converted = applyConversionsToFlow(body, {
86+
includeRetired: true,
87+
excludeConversionIds: FLOW_FALLBACK_VERDICT_NOT_REPLAYED,
88+
onNotice: (n) => { if (retired.has(n.conversionId)) fired.push(n); },
89+
});
90+
const root = { flows: [converted] };
91+
return new Set(fired.map((n) => `${n.conversionId}|${n.from}|${siteOf(root, n.path)}`));
92+
};
93+
// The request's retired hits; each one a stored row carries is struck off.
94+
const uncarried = retiredHits(request.item);
95+
const strikeCarried = (body: unknown): void => {
96+
if (uncarried.size === 0) return;
97+
for (const hit of retiredHits(body)) uncarried.delete(hit);
98+
};
99+
const repo = this.getOverlayRepo();
100+
const ref = { type: 'flow', name: request.name, org: 'env' } as Parameters<typeof repo.headAt>[0];
101+
const member = itemDiscriminator('flow', request.item);
102+
const state: 'active' | 'draft' = mode === 'draft' ? 'draft' : 'active';
103+
const target = await repo.headAt(ref, { state, packageId: request.packageId ?? null, member });
104+
if (target.head) {
105+
strikeCarried(target.head.body);
106+
if (uncarried.size === 0) return true;
107+
if (state !== 'draft') return false;
108+
} else if (state !== 'draft') {
109+
return false;
110+
}
111+
const active = (await repo.headAt(ref, { state: 'active', packageId: target.packageId, member })).head;
112+
if (!active) return false;
113+
strikeCarried(active.body);
114+
return uncarried.size === 0;
115+
}

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

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ import { PLURAL_TO_SINGULAR, SINGULAR_TO_PLURAL, canonicalMetaUrlType, metaUrlSp
165165
// live transport in through `attachMetadataMutationPubSub`.
166166
import type { IPubSub } from '@objectstack/spec/contracts';
167167
import { RLS_MEMBERSHIP_RESOLVER_SERVICE } from '@objectstack/spec/contracts';
168-
import { ALL_CONVERSIONS, applyConversionsToFlow, applyConversionsToStoredItem, type ConversionNotice, type ConversionTodoNotice } from '@objectstack/spec';
168+
import { applyConversionsToFlow, applyConversionsToStoredItem, type ConversionNotice, type ConversionTodoNotice } from '@objectstack/spec';
169169
import { type FormView, type I18nLabel, isAggregatedViewContainer, expandViewContainer } from '@objectstack/spec/ui';
170170
// [commit ece4dad31] Emitted-specifier pin. This module's inferred public declarations
171171
// structurally mention `FormFieldInput` (FormView `sections[].fields`), and
@@ -399,6 +399,8 @@ import { artifactLockLayerAt, lookupArtifactItem, shippedArtifactsOf, shippingPa
399399
import { namesWithOwnStoredRow, readActiveOverlayRows, readItemActiveRows, storedOverlayEntries } from './protocol-stored-overlay-reads.js';
400400
// [#25467 cut 17] The object-owner verdicts: which contributor kind a stored object row registers as, and handing an adopted object's name to its rebound row.
401401
import { classifyObjectContribution, releaseAdoptedObjectOwner } from './protocol-object-owner.js';
402+
// [#25467 cut 18] The flow save's retired-spelling replay verdict (#23903 ruling B′): does a save newly introduce a retired spelling no stored row carries.
403+
import { flowSaveReplaysRetired } from './protocol-flow-save-replay.js';
402404

403405
/**
404406
* Canonical Zod schema per metadata type lives in
@@ -3355,7 +3357,7 @@ function isoFromValidDate(value: unknown): unknown {
33553357
* predates the flip" is a fact; a save never knows that, exactly as the
33563358
* engine's canonicalizer does not.
33573359
*/
3358-
const FLOW_FALLBACK_VERDICT_NOT_REPLAYED: readonly string[] = [
3360+
export const FLOW_FALLBACK_VERDICT_NOT_REPLAYED: readonly string[] = [
33593361
'flow-decision-mode-inclusive-explicit',
33603362
];
33613363

@@ -6603,59 +6605,11 @@ export class ObjectStackProtocolImplementation implements
66036605
source?: string;
66046606
writeFace?: MetadataWriteFace;
66056607
}, mode: 'draft' | 'publish'): Promise<boolean> {
6606-
if (request.source !== undefined) return true;
6607-
if (request.writeFace === 'package-duplicate' || request.writeFace === 'package-unmanaged-import') return true;
6608-
const retired = new Set(ALL_CONVERSIONS.filter((c) => c.retiredFromLoadPath).map((c) => c.id));
6609-
// The site of a notice's path, ids standing in for the indexes of the
6610-
// array elements that carry one, read on the converted body it names.
6611-
const siteOf = (root: unknown, path: string): string => {
6612-
let at: unknown = root;
6613-
let site = '';
6614-
for (const [, key, index] of path.matchAll(/([^.[\]]+)|\[(\d+)\]/g)) {
6615-
if (key !== undefined) {
6616-
site += site === '' ? key : `.${key}`;
6617-
at = at !== null && typeof at === 'object' ? (at as Record<string, unknown>)[key] : undefined;
6618-
continue;
6619-
}
6620-
const element: unknown = Array.isArray(at) ? at[Number(index)] : undefined;
6621-
const id = element !== null && typeof element === 'object' ? (element as { id?: unknown }).id : undefined;
6622-
site += typeof id === 'string' && id !== '' ? `[#${id}]` : '[' + index + ']';
6623-
at = element;
6624-
}
6625-
return site;
6626-
};
6627-
const retiredHits = (body: unknown): Set<string> => {
6628-
const fired: ConversionNotice[] = [];
6629-
const converted = applyConversionsToFlow(body, {
6630-
includeRetired: true,
6631-
excludeConversionIds: FLOW_FALLBACK_VERDICT_NOT_REPLAYED,
6632-
onNotice: (n) => { if (retired.has(n.conversionId)) fired.push(n); },
6633-
});
6634-
const root = { flows: [converted] };
6635-
return new Set(fired.map((n) => `${n.conversionId}|${n.from}|${siteOf(root, n.path)}`));
6636-
};
6637-
// The request's retired hits; each one a stored row carries is struck off.
6638-
const uncarried = retiredHits(request.item);
6639-
const strikeCarried = (body: unknown): void => {
6640-
if (uncarried.size === 0) return;
6641-
for (const hit of retiredHits(body)) uncarried.delete(hit);
6608+
// [#25467 cut 18] The body moved to `protocol-flow-save-replay.ts` (see `FlowSaveReplayHost`).
6609+
const host: ThisParameterType<typeof flowSaveReplaysRetired> = {
6610+
getOverlayRepo: (...a) => this.getOverlayRepo(...a),
66426611
};
6643-
const repo = this.getOverlayRepo();
6644-
const ref = { type: 'flow', name: request.name, org: 'env' } as Parameters<typeof repo.headAt>[0];
6645-
const member = itemDiscriminator('flow', request.item);
6646-
const state: 'active' | 'draft' = mode === 'draft' ? 'draft' : 'active';
6647-
const target = await repo.headAt(ref, { state, packageId: request.packageId ?? null, member });
6648-
if (target.head) {
6649-
strikeCarried(target.head.body);
6650-
if (uncarried.size === 0) return true;
6651-
if (state !== 'draft') return false;
6652-
} else if (state !== 'draft') {
6653-
return false;
6654-
}
6655-
const active = (await repo.headAt(ref, { state: 'active', packageId: target.packageId, member })).head;
6656-
if (!active) return false;
6657-
strikeCarried(active.body);
6658-
return uncarried.size === 0;
6612+
return flowSaveReplaysRetired.call(host, request, mode);
66596613
}
66606614

66616615
/**

0 commit comments

Comments
 (0)