Skip to content

Commit 0c245b1

Browse files
committed
fix(metadata-protocol): a tenant's edit follows its item across the packages of one App
When a later version of an App moves an item to another package of the same App (a suite split, or its rollback), the served-row resolution now serves the tenant's edit stored under the former owner: the App pair's override that declares the former owner, or a pre-close row bound to it. Same App is read from the installed manifests (appOf), and from the App-pair binding when the former owner is no longer installed; never from an id prefix. Claude-Session: https://claude.ai/code/session_01ADHkmR2hWY1vfpcDK2q19Q Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6924440 commit 0c245b1

5 files changed

Lines changed: 516 additions & 12 deletions

File tree

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import type { StoredOverlayRow } from './item-lock.js';
5050
import { overlayMemberWhere } from './migrations/overlay-index.js';
5151
import type { ObjectStackProtocolImplementation } from './protocol.js';
5252
import { rowMatchesOverlayWhere, servedOverlayRowCandidates } from './protocol.js';
53-
import { isServedOverrideRowOf } from './served-override.js';
53+
import { holdsServedOverlayCandidate } from './protocol-overlay-merge.js';
5454
import { installedManifestsOfEngine } from './installed-manifests.js';
5555

5656
/**
@@ -64,6 +64,7 @@ export interface EnvironmentRowsHost {
6464
readonly engine: ObjectStackProtocolImplementation['engine'];
6565
readonly findEnvironmentRow: ObjectStackProtocolImplementation['findEnvironmentRow'];
6666
readonly findEnvironmentRows: ObjectStackProtocolImplementation['findEnvironmentRows'];
67+
readonly shippedArtifactsOf: ObjectStackProtocolImplementation['shippedArtifactsOf'];
6768
readonly legacyOrganizationRowsOf: ObjectStackProtocolImplementation['legacyOrganizationRowsOf'];
6869
readonly rethrowUnlessMetadataStoreUnprovisioned: ObjectStackProtocolImplementation['rethrowUnlessMetadataStoreUnprovisioned'];
6970
readonly withoutLegacyOrganizationRows: ObjectStackProtocolImplementation['withoutLegacyOrganizationRows'];
@@ -97,7 +98,7 @@ export async function findEnvironmentRow(this: Pick<EnvironmentRowsHost, 'engine
9798

9899
/** The body of `ObjectStackProtocolImplementation#findServedOverlayRow`; the method's docblock there is its contract. */
99100
export async function findServedOverlayRow(
100-
this: Pick<EnvironmentRowsHost, 'engine' | 'findEnvironmentRow'>,
101+
this: Pick<EnvironmentRowsHost, 'engine' | 'findEnvironmentRow' | 'findEnvironmentRows' | 'shippedArtifactsOf'>,
101102
args: {
102103
type: string;
103104
name: string;
@@ -119,7 +120,14 @@ export async function findServedOverlayRow(
119120
// read); with no package context (legacy/runtime reader), any row.
120121
const other = PLURAL_TO_SINGULAR[args.type] ?? SINGULAR_TO_PLURAL[args.type];
121122
const installed = installedManifestsOfEngine(this.engine);
122-
for (const candidate of servedOverlayRowCandidates({ packageId: args.packageId, servedOverride: args.servedOverride, installed })) {
123+
// [#31593] The code packages that ship the item, read once, when a
124+
// moved edit of it can be a candidate (`suite-moved-overlay.ts`).
125+
let shippers: ReadonlySet<string> | undefined;
126+
const ships = (packageId: string): boolean => {
127+
shippers ??= shippingPackageIds(this.shippedArtifactsOf(args.type, args.name, undefined, args.member));
128+
return shippers.has(packageId);
129+
};
130+
for (const candidate of servedOverlayRowCandidates({ packageId: args.packageId, servedOverride: args.servedOverride, installed, ships })) {
123131
const type = candidate.spelling === 'canonical' ? args.type : other;
124132
if (type === undefined) continue;
125133
const where: Record<string, unknown> = {
@@ -130,6 +138,17 @@ export async function findServedOverlayRow(
130138
...overlayMemberWhere(type, args.member),
131139
};
132140
if (candidate.packageId !== undefined) where.package_id = candidate.packageId;
141+
if (candidate.moved === 'base') {
142+
// [#31593] A row bound to a former owner of the item, at any
143+
// binding: every row of the item is read and the predicate
144+
// picks; two former owners' rows are never guessed between.
145+
const rows = activeRows !== undefined && args.state === 'active'
146+
? activeRows.filter((stored) => rowMatchesOverlayWhere(stored, where))
147+
: await this.findEnvironmentRows('sys_metadata', { where, context: { isSystem: true } });
148+
const moved = (rows ?? []).filter((row) => holdsServedOverlayCandidate(candidate, row, installed, ships));
149+
if (moved.length === 1) return { row: moved[0], overrideOf: candidate.overrideOf };
150+
continue;
151+
}
133152
// [#21911, ADR-0096] The explicit system opt-in: a platform store
134153
// read, which no caller's grants scope — the door that asked
135154
// already authorized the caller, and the protocol scopes the row
@@ -143,8 +162,10 @@ export async function findServedOverlayRow(
143162
// candidate only when it declares the override of the address's
144163
// package ({@link isServedOverrideRowOf}, on the row's own binding and
145164
// body): an undeclared row of the pair is the pair's own item.
165+
// [#31593] …and the moved-override candidate only when its body
166+
// declares a former owner of the item ({@link isMovedOverrideRowOf}).
146167
if (candidate.overrideOf !== undefined) {
147-
if (isServedOverrideRowOf(row, candidate.overrideOf, installed)) return { row, overrideOf: candidate.overrideOf };
168+
if (holdsServedOverlayCandidate(candidate, row, installed, ships)) return { row, overrideOf: candidate.overrideOf };
148169
continue;
149170
}
150171
return { row };

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

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
type InstalledManifestLookup,
1313
} from '@objectstack/metadata-core';
1414
import { canonicalMetaUrlType } from '@objectstack/spec/shared';
15-
import { isServedOverrideRowOf } from './served-override.js';
15+
import { isServedOverrideRowOf, type ServedOverrideRow } from './served-override.js';
16+
import { isMovedBaseRowOf, isMovedOverrideRowOf, type ShipsName } from './suite-moved-overlay.js';
1617
import {
1718
listSlotNameOf,
1819
} from './protocol-object-extension-item.js';
@@ -39,6 +40,40 @@ interface ServedOverlayRowCandidate {
3940
* package's own item, never the base's.
4041
*/
4142
readonly overrideOf?: string;
43+
/**
44+
* [#31593] Set on the two candidates that hold a tenant's edit of the
45+
* item from before it moved to `overrideOf` from another package of the
46+
* same App (`suite-moved-overlay.ts`): `'override'`, a declared override
47+
* in the App's pair that names the former owner
48+
* ({@link isMovedOverrideRowOf}), at `packageId` the pair; `'base'`, a row
49+
* bound to the former owner itself ({@link isMovedBaseRowOf}), at any
50+
* package (`packageId` undefined). A row holds such a candidate only when
51+
* its predicate admits it, and it is then served as `overrideOf`'s item.
52+
*/
53+
readonly moved?: 'override' | 'base';
54+
}
55+
56+
/**
57+
* [#31593] Whether `row` holds `candidate`'s place beyond the `where` it
58+
* matched: the declared override when the candidate is one
59+
* ({@link isServedOverrideRowOf}), the moved edit when the candidate is one
60+
* ({@link isMovedOverrideRowOf}, {@link isMovedBaseRowOf}), and any row
61+
* otherwise. The one reading both readers of the candidates take
62+
* ({@link findServedOverlayRow}, {@link mergePackageAwareOverlay}).
63+
*/
64+
export function holdsServedOverlayCandidate(
65+
candidate: ServedOverlayRowCandidate,
66+
row: ServedOverrideRow,
67+
installed: InstalledManifestLookup | undefined,
68+
ships: ShipsName | undefined,
69+
): boolean {
70+
if (candidate.overrideOf === undefined) return true;
71+
if (installed === undefined) return false;
72+
if (candidate.moved === undefined) return isServedOverrideRowOf(row, candidate.overrideOf, installed);
73+
if (ships === undefined) return false;
74+
return candidate.moved === 'override'
75+
? isMovedOverrideRowOf(row, candidate.overrideOf, installed, ships)
76+
: isMovedBaseRowOf(row, candidate.overrideOf, installed, ships);
4277
}
4378

4479
/**
@@ -112,6 +147,14 @@ export function withoutServedRowName(item: unknown): Record<string, unknown> | u
112147
* the save never wrote (#23165's shape). A package-unbound row still wins
113148
* over the code package and over another package's row (ADR-0146 D8
114149
* item 1) until the migration moves it into the pair (D11).
150+
* - [#31593] …and, when the read also names which packages ship the name
151+
* (`ships`), a tenant's edit of the item from before it moved to the
152+
* package from another package of the same App (`suite-moved-overlay.ts`):
153+
* the App's pair's override that declares the former owner, right after
154+
* the package's own declared override; and a row bound to the former owner
155+
* itself, LAST, after the package-less row, which wins over another
156+
* package's row (D8 item 1). A read that names no `ships` (every write
157+
* address) is unchanged.
115158
*
116159
* Both readers of stored content take this order, so they cannot pick
117160
* differently: {@link ObjectStackProtocolImplementation.findServedOverlayRow}
@@ -133,13 +176,18 @@ export function servedOverlayRowCandidates(address: ServedOverlayAddress): Serve
133176
const pairs = address.servedOverride === true && typeof base === 'string' && base !== ''
134177
? pairsOf(base, address.installed)
135178
: [];
136-
const places: ReadonlyArray<Pick<ServedOverlayRowCandidate, 'packageId' | 'overrideOf'>> = base === null
179+
// [#31593] The moved edits are candidates only when the read names the
180+
// packages that ship the name (`ships`): the predicates need it.
181+
const moved = typeof base === 'string' && base !== '' && address.servedOverride === true && address.ships !== undefined;
182+
const places: ReadonlyArray<Pick<ServedOverlayRowCandidate, 'packageId' | 'overrideOf' | 'moved'>> = base === null
137183
? [{ packageId: null }]
138184
: base
139185
? [
140186
...pairs.map((pair) => ({ packageId: pair, overrideOf: base })),
187+
...(moved ? pairs.map((pair) => ({ packageId: pair, overrideOf: base, moved: 'override' as const })) : []),
141188
{ packageId: base },
142189
{ packageId: null },
190+
...(moved ? [{ packageId: undefined, overrideOf: base, moved: 'base' as const }] : []),
143191
]
144192
: [{ packageId: undefined }];
145193
return (['canonical', 'other'] as const).flatMap((spelling) =>
@@ -177,6 +225,12 @@ export type ServedOverlayAddress =
177225
readonly servedOverride: boolean | undefined;
178226
/** The installed manifests of the registry the read serves, read for the address's pair. */
179227
readonly installed: InstalledManifestLookup;
228+
/**
229+
* [#31593] Which code packages ship the item the read serves, in the
230+
* registry it serves. Named, the moved edits of the item are
231+
* candidates too (`suite-moved-overlay.ts`); omitted, they are not.
232+
*/
233+
readonly ships?: ShipsName;
180234
};
181235

182236
/**
@@ -359,20 +413,28 @@ export function mergePackageAwareOverlay(
359413
list: readonly Contribution[],
360414
real: string | undefined,
361415
): { item: any; overrideOf?: string } | undefined => {
416+
// [#31593] The code packages that ship the slot's item: its
417+
// lower-layer contributions, each under its own package.
418+
const shippers = new Set(list
419+
.filter((c) => c.stored === undefined && !c.standIn && c.pkg !== undefined)
420+
.map((c) => c.pkg as string));
421+
const ships = (packageId: string): boolean => shippers.has(packageId);
362422
for (const candidate of servedOverlayRowCandidates(options.installed !== undefined
363-
? { packageId: real, servedOverride: options.servedOverride, installed: options.installed }
423+
? { packageId: real, servedOverride: options.servedOverride, installed: options.installed, ships }
364424
: { packageId: real })) {
365-
const hit = list.find((c) => c.stored !== undefined
425+
const hits = list.filter((c) => c.stored !== undefined
366426
&& (c.stored.type === type ? 'canonical' : 'other') === candidate.spelling
367427
&& (candidate.packageId === undefined || (c.pkg ?? null) === candidate.packageId)
368428
// [#28409] The pair's row holds the override candidate only
369429
// when it declares the override: judged on the row's own
370430
// binding (the contribution's `pkg`, the row's `package_id`)
371431
// and its body, never on a stamp the body carries.
372-
&& (candidate.overrideOf === undefined || isServedOverrideRowOf(
373-
{ type: c.stored.type, package_id: c.pkg, metadata: c.item }, candidate.overrideOf,
374-
options.installed!,
375-
)));
432+
// [#31593] …and a moved candidate, when its predicate admits it.
433+
&& holdsServedOverlayCandidate(candidate,
434+
{ type: c.stored.type, package_id: c.pkg, metadata: c.item }, options.installed, ships));
435+
// [#31593] Two former owners' rows could follow the item: neither
436+
// is guessed (`suite-moved-overlay.ts`).
437+
const hit = candidate.moved === 'base' ? (hits.length === 1 ? hits[0] : undefined) : hits[0];
376438
if (hit) return candidate.overrideOf === undefined ? { item: hit.item } : { item: hit.item, overrideOf: candidate.overrideOf };
377439
}
378440
return undefined;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3814,6 +3814,8 @@ export class ObjectStackProtocolImplementation implements
38143814
const host: ThisParameterType<typeof findServedOverlayRow> = {
38153815
get engine() { return readEngine(); },
38163816
findEnvironmentRow: (...a) => this.findEnvironmentRow(...a),
3817+
findEnvironmentRows: (...a) => this.findEnvironmentRows(...a),
3818+
shippedArtifactsOf: (...a) => this.shippedArtifactsOf(...a),
38173819
};
38183820
return findServedOverlayRow.call(host, args, activeRows);
38193821
}

0 commit comments

Comments
 (0)