Skip to content

Commit 7ddcc64

Browse files
committed
fix(rest): ask the pair's catalog only for an object whose tab titles diverge from the packaged base
The object reads asked getPairTranslationBundle for every listed object, so an MCP list_objects paid one more translation read than the ADR-0090 D10 budget in mcp-list-objects-statements.pin.test.ts allows. A tab title equal to the base's reads the deployment catalog, which already holds the pair's share, so the pair is now asked only when the served object carries a title off the base. A new pin counts the asks. Claude-Session: https://claude.ai/code/session_01L9qibw44Q7BwdAY1RmfR73 Co-authored-by: Claude <noreply@anthropic.com>
1 parent fe0f53e commit 7ddcc64

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

‎packages/rest/src/meta-item-read-gate.ts‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3677,18 +3677,26 @@ export async function patchTranslationBundlesOf(
36773677
* `objects.<name>._relatedLists` subtree is handed on, per locale: every other
36783678
* string of the object keeps the ranking it has without a pair (D10), and an
36793679
* empty answer is `undefined`, never a catalog that answers nothing.
3680+
*
3681+
* The pair is asked only when `served` carries a tab title that diverges from
3682+
* the packaged base ({@link hasRelatedListTitleOffBase}), the one case its
3683+
* catalog decides: a title equal to the base's reads the deployment catalog,
3684+
* which already holds the pair's share. So an object list whose tab titles no
3685+
* arrangement changed pays no translation read (ADR-0090 D10's statement
3686+
* budget, `mcp-list-objects-statements.pin.test.ts` in `@objectstack/runtime`).
36803687
*/
36813688
async function objectPairRelatedListsBundleOf(
36823689
protocol: unknown,
36833690
type: string,
3684-
name: unknown,
3691+
served: unknown,
36853692
packagedBase: unknown,
36863693
memo?: Map<string, Promise<unknown>>,
36873694
): Promise<unknown> {
3695+
const name = (served as Record<string, unknown> | null | undefined)?.name;
36883696
if (type !== 'object' || typeof name !== 'string' || name === '') return undefined;
3689-
const base = packagedBase && typeof packagedBase === 'object'
3690-
? (packagedBase as Record<string, unknown>)._packageId
3691-
: undefined;
3697+
if (!packagedBase || typeof packagedBase !== 'object') return undefined;
3698+
if (!hasRelatedListTitleOffBase(served, packagedBase as Record<string, unknown>)) return undefined;
3699+
const base = (packagedBase as Record<string, unknown>)._packageId;
36923700
const pair = await pairTranslationBundleOfBase(protocol, base, memo);
36933701
if (!pair || typeof pair !== 'object') return undefined;
36943702
const slice: Record<string, unknown> = {};
@@ -3700,6 +3708,29 @@ async function objectPairRelatedListsBundleOf(
37003708
return Object.keys(slice).length > 0 ? slice : undefined;
37013709
}
37023710

3711+
/**
3712+
* Whether `served` carries an object-form `relatedLists` entry whose non-empty
3713+
* `label` the packaged base's entry for the same `list` does not carry — the
3714+
* divergence `translateObjectRelatedLists` (`@objectstack/spec/system`) judges
3715+
* a title by, where an entry the base does not name counts as diverged.
3716+
*/
3717+
function hasRelatedListTitleOffBase(served: unknown, packagedBase: Record<string, unknown>): boolean {
3718+
const lists = (served as Record<string, unknown> | null | undefined)?.relatedLists;
3719+
if (!Array.isArray(lists)) return false;
3720+
const baseLabels = new Map<string, unknown>();
3721+
for (const raw of Array.isArray(packagedBase.relatedLists) ? packagedBase.relatedLists : []) {
3722+
if (raw && typeof raw === 'object' && typeof (raw as any).list === 'string' && !baseLabels.has((raw as any).list)) {
3723+
baseLabels.set((raw as any).list, (raw as any).label);
3724+
}
3725+
}
3726+
return lists.some((raw) => {
3727+
if (!raw || typeof raw !== 'object') return false;
3728+
const { list, label } = raw as Record<string, unknown>;
3729+
return typeof list === 'string' && list !== '' && typeof label === 'string' && label !== ''
3730+
&& baseLabels.get(list) !== label;
3731+
});
3732+
}
3733+
37033734
/** A non-empty string, or `undefined`. */
37043735
function nonEmptyText(value: unknown): string | undefined {
37053736
return typeof value === 'string' && value.length > 0 ? value : undefined;
@@ -3883,7 +3914,7 @@ export async function translateMetaList(
38833914
const pairBundle = p === undefined
38843915
? undefined
38853916
: (await pairTranslationBundleOf(p, metaType, item, pairBundles))
3886-
?? (await objectPairRelatedListsBundleOf(p, metaType, item?.name, packagedBase, pairBundles));
3917+
?? (await objectPairRelatedListsBundleOf(p, metaType, item, packagedBase, pairBundles));
38873918
const patchBundles = p === undefined ? undefined : await patchTranslationBundlesOf(p, metaType, item?.name, packageBundles);
38883919
const out = translateMetadataDocument(metaType, item, bundle, {
38893920
...metaTranslateOptions(i18n, locale),
@@ -3963,7 +3994,7 @@ export async function translateMetaDocument(
39633994
const pairBundle = p === undefined
39643995
? undefined
39653996
: (await pairTranslationBundleOf(p, metaType, document))
3966-
?? (await objectPairRelatedListsBundleOf(p, metaType, (document as any)?.name, packagedBase));
3997+
?? (await objectPairRelatedListsBundleOf(p, metaType, document, packagedBase));
39673998
// [#33952] Each patching package's own translations: a patched field slot's second rank.
39683999
const patchBundles = p === undefined ? undefined : await patchTranslationBundlesOf(p, metaType, (document as any)?.name);
39694000
const translated = translateMetadataDocument(metaType, document as any, bundle, {

‎packages/rest/src/meta-object-related-list-pair-i18n.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ function boot(opts: Boot = {}) {
127127
},
128128
};
129129
const protocol = new ObjectStackProtocolImplementation(engine as never, undefined, 'env_test');
130+
// Every question the reads put to the pair's catalog, for the statement-budget pin.
131+
let pairAsks = 0;
132+
const askPair = protocol.getPairTranslationBundle.bind(protocol);
133+
(protocol as unknown as { getPairTranslationBundle: typeof askPair }).getPairTranslationBundle = (base) => { pairAsks++; return askPair(base); };
130134

131135
const catalog = (locale: string) => overlay(BASE_CATALOG[locale], opts.pairObjects?.[locale] && { objects: opts.pairObjects[locale] });
132136
const i18nService = {
@@ -165,12 +169,13 @@ function boot(opts: Boot = {}) {
165169
.map((entry: any) => (typeof entry === 'string' ? entry : entry?.label ?? entry?.list));
166170

167171
/** `crm_account` on the by-name read and on the list read: its label and its tab titles. */
168-
return async (locale: string) => {
172+
const readAt = async (locale: string) => {
169173
const byName = (await run('/api/v1/meta/:type/:name', { type: 'object', name: 'crm_account' }, locale))?.item;
170174
const listBody = await run('/api/v1/meta/:type', { type: 'object' }, locale);
171175
const listed = (Array.isArray(listBody) ? listBody : listBody?.items ?? []).find((o: any) => o?.name === 'crm_account');
172176
return [byName, listed].map((doc) => ({ label: doc?.label, tabs: titles(doc) }));
173177
};
178+
return Object.assign(readAt, { pairAsks: () => pairAsks });
174179
}
175180

176181
/** The arrangement: relabels the owner's labelled tab and titles its bare one. */
@@ -223,4 +228,15 @@ describe('#37622 — controls', () => {
223228
});
224229
for (const served of await pairFirst('zh-CN')) expect(served.tabs).toEqual(['人员', 'crm_case']);
225230
});
231+
232+
it('asks the pair\'s catalog only for an object whose tab titles an arrangement changed (ADR-0090 D10 budget)', async () => {
233+
for (const extension of [undefined, { relatedLists: [{ list: 'crm_contact', label: 'Contacts' }, 'crm_case'] }]) {
234+
const read = boot({ extension, pairObjects: { 'zh-CN': { crm_account: { _relatedLists: { crm_contact: { label: '人员' } } } } } });
235+
await read('zh-CN');
236+
expect(read.pairAsks()).toBe(0);
237+
}
238+
const changed = boot({ extension: ARRANGEMENT });
239+
await changed('zh-CN');
240+
expect(changed.pairAsks()).toBeGreaterThan(0);
241+
});
226242
});

0 commit comments

Comments
 (0)