Skip to content

Commit e2a0e90

Browse files
perf(metadata-protocol): one form request reads the view list and each type's stored rows once (#38164)
Fixes #37709 Clause-②: yes (widening) — ObjectStackProtocolImplementation.readFormRequest Metadata-protocol: none ## 维护者速读 - 用户无感:匿名公开表单页和按名读取表单时,返回的内容与以前完全一样,只是同一次请求里不再把同一批元数据反复读好几遍。 - 属于"表单独立成为元数据类型"这一系列的收尾,解决上一轮评审留下的"同一请求重复读库"这一条遗留项。 - 若出错,公开表单可能打不开或判断错一个表单是否公开;改动不碰任何存储数据,可直接撤回。 - 无。 ## What changes The effective `form` read (ADR-0159 D5) resolves over both types: the generic `form` list, the projection of the `view` list, and the cross-type resolution (step 3) over the stored rows of both types. Each of those readings, and each of the five reads an anonymous form door makes, asked the store for the same rows again. - **`packages/metadata-protocol/src/protocol-form-reads.ts`.** `formRequestScope(host)`: one request's reading, over which each stored row set (per type and package binding) and each flattened list (per request, audience and layer) is read once. `readEffectiveFormList` reads through it, so the generic `form` list and step 3 share one reading of the `form` rows, and the `view` list and step 3 share one reading of the `view` rows. `readInFormRequest(read)` runs `read` as one request reading (an AsyncLocalStorage store): every protocol list read inside it over the same host shares one scope, and once `read` settles the scope memoizes nothing, so nothing outlives the request. The stored rows of step 3 stay read whole (a container row's name is not the name its expansion writes); they are read once per request. The `readEffectiveFormList` docblock now states that. - **`packages/metadata-protocol/src/protocol-meta-list-reads.ts`.** `readFlattenedMetaItems` takes an optional stored-row reader (the scope's); `getMetaItems` and `getMetaItemsPerPackage` read through the open request reading when there is one; the package-scoped `form` list face reads its envelope through the same scope. - **`packages/metadata-protocol/src/protocol.ts`.** The private `readFlattenedMetaItems` passes the row reader on. New server-only extension `readFormRequest(read)` (feature-detected by the doors, like `getMetaItemsPerPackage` and `getUnreadableFormBodies`): runs `read` as one request reading. - **`packages/rest/src/form-routes.ts`** (outside the claim's file surface; the measured producer of the repeated door reads, see Acceptance notes). `resolveFormBySlug` makes its five reads inside `readFormRequest` when the protocol serves it, and exactly as before otherwise. Both doors (`GET /forms/:slug`, `POST /forms/:slug/submit`) go through it. - **`packages/metadata-protocol/src/protocol-meta-item-reads.ts`.** Comment only at `readEffectiveFormItem`: on this tree it calls `readEffectiveForms` once (not up to three times), and that one call now reads each row set once. Every answer is unchanged: the same items, in the same order, from the same served-row order. The ADR-0159 D5 cross-type resolution is untouched; `names` and `packageId` handling is untouched (`names` was already pushed into both generic reads, and the reason `packageId` is not pushed down was already stated in the `readEffectiveFormList` docblock, both landed in #37422's rework; confirmed on this tree, not redone). ### Measured counts (real protocol, real `ObjectQL` + SQLite store, `OS_METADATA_OVERLAY_CACHE_TTL_MS=0`, one warm-up request) "Row-set read" = one `readActiveOverlayRows` call (one `sys_metadata` statement under the singular type, plus one under the plural when the singular answers no row). | Request | Form shape | `view` list (effective) before → after | `view` row sets before → after | `form` row sets before → after | |:---|:---|:---|:---|:---| | by-name `form` read | authored `form` item | 1 → 1 | 2 → 1 | 2 → 1 | | by-name `form` read | old-shape `formViews` entry | 1 → 1 | 1 → 1 | 1 → 1 | | form-door GET | authored `form` item | 2 → 1 | 5 → 1 | 3 → 1 | | form-door GET | old-shape `formViews` entry | 2 → 1 | 4 → 1 | 2 → 1 | Before = `origin/main` 93fa911 (with #37422 merged); after = this branch. The by-name read also issues one row read predicated on the name (the generic by-name read's own, `readItemActiveRows`), before and after. The door reads the `form` list, the per-package `form` layer and the per-package `view` layer once each, before and after. With the row cache on (default 30 s) the repeated reads were cache hits at the SQL level, but each still ran its parse and merge; with it off (or on a cold or invalidated cache) each was a statement. ## Tests - New `packages/rest/src/public-form-reads-once-37709.test.ts` (8 cases, both form shapes): the by-name read and the door GET read the `view` list once and each type's row sets once (exact counts, answers asserted); CONTROL 1, two requests read twice (the scope never spans requests); CONTROL 2, the five door calls outside one reading (a protocol without `readFormRequest`) read the `view` list twice and the `view` rows four times, so the counter sees a repeated read when there is one. - `packages/rest/src/rest-write-route-hook-refusal-sentence.ledger.test.ts`: its catch-all protocol double rejects every method it does not list, and the door now feature-detects `readFormRequest`, so the double's reads table gains one pass-through entry, as it gained `getMetaItemsPerPackage` and `getUnreadableFormBodies` when those door extensions landed. No assertion changed. Every other form-door and D5 test is unchanged and green. <details><summary>Suites on f97bc7d (merged with origin/main)</summary> - `@objectstack/metadata-protocol` `vitest run`: Test Files 372 passed, 9 skipped (381); Tests 23605 passed, 45 skipped. - `@objectstack/rest` `vitest run --project local`: Test Files 433 passed (433); Tests 8981 passed, 351 skipped. - `pnpm --filter @objectstack/metadata-protocol typecheck`, `pnpm --filter @objectstack/rest typecheck`: exit 0. - Final head 73983ef changes only `protocol.ts` text after that run (the `readFlattenedMetaItems` docblock reflowed and the new method's docblock made one line, so the facade stays at its 11705-line ceiling, `check:facade-delegation-only`). On it: rebuilt, both typechecks exit 0, and the pin plus 11 form-door files: Test Files 12 passed (12), Tests 898 passed (898). </details> <details><summary>Ablations on 6178fb1 (scripts/ablation-replace.mjs, each leg built and checked in dist with scripts/ablation-dist-preflight.mjs)</summary> - A1, the effective `form` read without its scope (drop `host = formRequestScope(host)` in `readEffectiveFormList`): 2 failed / 6 passed (the by-name read of an authored `form` item, and CONTROL 1). - A2, `readFormRequest` opens no reading (`readInFormRequest(read)` replaced by calling `read` directly): 4 failed / 4 passed (the door GET in both shapes, and CONTROL 1 in both). - A3, the scope's list reads read their rows outside it (no row reader passed): 6 failed / 2 passed. - Restore leg: every marker absent from dist, tree clean against HEAD, pin 8/8 green. </details> <details><summary>Local gates on 73983ef (the 75 families `dispatch-gates.mjs --commands` derives for this diff)</summary> - All 75 commands exit 0, each exit captured before any pipe. `pnpm check:dual-build-cjs-loads` first answered PREREQUISITE NOT MET (exit 3: it reads every package's `dist/`), and exit 0 when re-run after `check:type-check-debt`'s full build on the same head (126 require entry points across 74 packages load). - `check:facade-delegation-only` held the line: `protocol.ts` is at its 11705-line ceiling (it went red at 11719 on f97bc7d, fixed in 73983ef). - `dispatch-gates.mjs --ran`: 75 derived, 75 run, 0 NOT-MEASURED, 0 UNRUN. - Raw control-byte self-scan of the changed files: no match. </details> ## Acceptance notes - **Landing point outside the claim's file surface: `packages/rest/src/form-routes.ts`.** Measurement shows the door's repeated `view`-list and row reads come from its five separate protocol calls; no change inside `packages/metadata-protocol` alone can share a reading across them. A first cut answered the five reads through one new protocol call; it bypassed the doubles four existing door tests install by overriding `getMetaItems` / `getMetaItemsPerPackage` / `getUnreadableFormBodies` on a real protocol, so the door keeps its five calls and runs them inside one request reading instead. - **Premise check.** On 93fa911 `readEffectiveFormItem` calls `readEffectiveForms` once (the card's "up to three times" no longer holds); the repeats were inside that one call (the `form` and `view` row sets twice each). - **New seam worth a reviewer's eye:** `readFormRequest` is a new public method on `ObjectStackProtocolImplementation` (server-only, feature-detected), and `metadata-protocol` now uses `AsyncLocalStorage` (`node:async_hooks`; the package is already node-only). ## Verification First read, by risk: 1. `packages/metadata-protocol/src/protocol-form-reads.ts`, `formRequestScope` and `readInFormRequest`: a memoized list is one object handed to several callers in the same request (every caller reads it, none writes it), and the `live()` check that stops memoizing once the request settles. 2. `packages/rest/src/form-routes.ts`, `resolveFormBySlug`: the five reads inside `inOneReading`, in their old order. 3. `packages/metadata-protocol/src/protocol-meta-list-reads.ts`: `readRows` at the active-row read of `readFlattenedMetaItems`, and `inFormRequestScope` in `getMetaItems` / `getMetaItemsPerPackage`. Two-minute check: `pnpm --filter @objectstack/metadata-protocol build && pnpm --filter @objectstack/rest exec vitest run src/public-form-reads-once-37709.test.ts` prints `Tests 8 passed (8)`. --- _Generated by [Claude Code](https://claude.ai/code/session_017xRBcSAaBEfQpU2yJexSJ8)_ <!-- signed: repo:objectstack#8 · sunhaolin · session_017xRBcSAaBEfQpU2yJexSJ8 --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 216524a commit e2a0e90

8 files changed

Lines changed: 409 additions & 51 deletions
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@objectstack/metadata-protocol': patch
3+
'@objectstack/rest': patch
4+
---
5+
6+
perf(metadata-protocol): a by-name `form` read and an anonymous form-door request read the `view` list once and each type's stored rows once
7+
8+
Clause-②: yes (widening) — ObjectStackProtocolImplementation.readFormRequest
9+
Metadata-protocol: none
10+
11+
The effective `form` read resolves over both types (ADR-0159 D5): the generic `form` list, the projection of the `view` list, and the cross-type resolution over the stored rows of both types. Those readings asked for the same stored rows separately, and an anonymous form door made five protocol calls (the `view` list, the `form` list, the unreadable bodies and both per-package layers), each reading the rows again. With the overlay row cache off, a by-name `form` read read the `form` and `view` rows twice each, and a form-door GET read the `view` list twice, the `view` rows five times and the `form` rows three times.
12+
13+
- **`@objectstack/metadata-protocol`.** The effective `form` read now reads through one request scope: each stored row set (per type and package binding) and each flattened list is read once per request, and nothing is kept past the request. A new server-only extension, `readFormRequest(read)`, runs `read` as one request's reading: the `getMetaItems`, `getMetaItemsPerPackage` and `getUnreadableFormBodies` calls inside it share that one scope, each answering what it answers on its own.
14+
- **`@objectstack/rest`.** The form doors (`GET /forms/:slug`, `POST /forms/:slug/submit`) make their five reads inside `readFormRequest` when the protocol serves it, and as before otherwise.
15+
16+
Every answer is unchanged: the same items, in the same order, from the same served-row order. Nothing to migrate.

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

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* Pure over what the host reads: no write, no registry mutation.
4343
*/
4444

45+
import { AsyncLocalStorage } from 'node:async_hooks';
4546
import { pairsOf, type InstalledManifestLookup } from '@objectstack/metadata-core';
4647
import { PROJECTED_FORM_SOURCE_KEY, isAggregatedViewContainer, projectViewForms, type UnreadableFormBody } from '@objectstack/spec/ui';
4748
import { installedManifestsOfEngine } from './installed-manifests.js';
@@ -71,6 +72,109 @@ export type FormReadHost = Pick<ProtocolHost,
7172

7273
const keyOf = (pkg: unknown, name: unknown): string => `${typeof pkg === 'string' ? pkg : ''}\u0000${String(name)}`;
7374

75+
/** The hosts {@link formRequestScope} built, so a scope handed on is never scoped twice. */
76+
const REQUEST_SCOPES = new WeakSet<object>();
77+
78+
/**
79+
* One request's reading ({@link readInFormRequest}): the scope it built over
80+
* each host, and whether the request is still reading. Once the request has
81+
* answered, a scope it built memoizes nothing: a continuation that outlives
82+
* the request reads the store as any read does.
83+
*/
84+
interface FormRequestReading {
85+
open: boolean;
86+
readonly scopes: WeakMap<object, object>;
87+
}
88+
89+
const FORM_REQUEST = new AsyncLocalStorage<FormRequestReading>();
90+
91+
/** The two host members a {@link formRequestScope} replaces. */
92+
type ScopableHost = Pick<FormReadHost, 'readActiveOverlayRows' | 'readFlattenedMetaItems'>;
93+
94+
/**
95+
* [ADR-0159 D5, D6] Run `read` as ONE request's reading of the two types:
96+
* every list read and stored-row read inside it, through any of the protocol
97+
* reads below, shares one {@link formRequestScope} per host. The anonymous
98+
* form doors read the `view` list, the `form` list, the unreadable bodies and
99+
* both per-package layers as five protocol calls; inside one reading they read
100+
* the `view` list once and each type's stored rows once, and each call answers
101+
* what it answers on its own. The reading ends when `read` settles.
102+
*/
103+
export async function readInFormRequest<T>(read: () => Promise<T>): Promise<T> {
104+
const reading: FormRequestReading = { open: true, scopes: new WeakMap() };
105+
try {
106+
return await FORM_REQUEST.run(reading, read);
107+
} finally {
108+
reading.open = false;
109+
}
110+
}
111+
112+
/**
113+
* [ADR-0159 D5] ONE request's reading of the two types: the host the `form`
114+
* read reads through, over which each stored row set (`sys_metadata`, per type
115+
* and package binding) and each flattened list (per request, audience and
116+
* layer) is read once, however many of the readings below ask for it.
117+
*
118+
* The effective `form` read asks for the same rows more than once by
119+
* construction: the generic `form` list and step 3 both read the stored `form`
120+
* rows, and the `view` list and step 3 both read the stored `view` rows. A
121+
* form door asks the `view` list, the `form` list (which reads the `view` list
122+
* again), both per-package layers and the unreadable bodies, each over the
123+
* same stored rows. Read through one scope, the request reads each row set and
124+
* each list once (measured and pinned in `public-form-reads-once-37709.test.ts`,
125+
* `@objectstack/rest`). A list read in the scope reads its stored rows through
126+
* the scope too (`readFlattenedMetaItems`' row reader), so the generic lists
127+
* and step 3 share one reading of each type's rows.
128+
*
129+
* Inside {@link readInFormRequest} the scope is the request's own, shared by
130+
* every read of that request over the same host; outside one, it is the scope
131+
* of the one call that built it. ⛔ Never kept across requests (the
132+
* cross-request row cache is `meta-overlay-cache.ts`, under its own
133+
* invalidation). Every member the scope does not replace is the host's own,
134+
* read at call time.
135+
*/
136+
export function formRequestScope<H extends ScopableHost>(host: H): H {
137+
if (REQUEST_SCOPES.has(host)) return host;
138+
const reading = FORM_REQUEST.getStore();
139+
const shared = reading?.scopes.get(host) as H | undefined;
140+
if (shared !== undefined) return shared;
141+
const live = (): boolean => reading === undefined || reading.open;
142+
const rows = new Map<string, Promise<any[]>>();
143+
const lists = new Map<string, ReturnType<FormReadHost['readFlattenedMetaItems']>>();
144+
const readActiveOverlayRows: FormReadHost['readActiveOverlayRows'] = (request) => {
145+
if (!live()) return host.readActiveOverlayRows(request);
146+
const key = `${request.type}\u0000${request.packageId ?? ''}`;
147+
let read = rows.get(key);
148+
if (read === undefined) {
149+
read = host.readActiveOverlayRows(request);
150+
rows.set(key, read);
151+
}
152+
return read;
153+
};
154+
const readFlattenedMetaItems: FormReadHost['readFlattenedMetaItems'] = (request, audience, layer = 'effective') => {
155+
if (!live()) return host.readFlattenedMetaItems(request, audience, layer);
156+
const key = JSON.stringify([request.type, request.packageId ?? null, request.names ?? null, request.previewDrafts === true, audience, layer]);
157+
let read = lists.get(key);
158+
if (read === undefined) {
159+
read = host.readFlattenedMetaItems(request, audience, layer, readActiveOverlayRows);
160+
lists.set(key, read);
161+
}
162+
return read;
163+
};
164+
const scope = Object.create(host, {
165+
readActiveOverlayRows: { value: readActiveOverlayRows },
166+
readFlattenedMetaItems: { value: readFlattenedMetaItems },
167+
}) as H;
168+
REQUEST_SCOPES.add(scope);
169+
reading?.scopes.set(host, scope);
170+
return scope;
171+
}
172+
173+
/** The scope of the request reading in progress ({@link readInFormRequest}) over `host`, or `host` outside one. */
174+
export function inFormRequestScope<H extends ScopableHost>(host: H): H {
175+
return FORM_REQUEST.getStore()?.open === true ? formRequestScope(host) : host;
176+
}
177+
74178
/** A listed item's package, as its envelope states it. */
75179
function packageOf(item: unknown): string | undefined {
76180
const pkg = (item as { _packageId?: unknown } | null)?._packageId;
@@ -226,7 +330,10 @@ export async function readEffectiveForms(
226330
* is read from, so the named items resolve exactly as the whole list would.
227331
* The stored rows of step 3 are read whole: a container row's `name` is not
228332
* the name its expansion writes, so no row predicate can select them by the
229-
* form's name.
333+
* form's name. They are read ONCE per request ({@link formRequestScope}):
334+
* step 3 reads the same stored rows of each type the two generic reads read,
335+
* so a by-name read reads each type's stored rows once, as a form door does
336+
* (measured on the pin, `public-form-reads-once-37709.test.ts`).
230337
* - **`packageId` is NOT pushed down**, and is applied to the resolved list:
231338
* the effective `form` read is resolved per address over both types, and a
232339
* list scoped to a package reads another address than its own. Step 1
@@ -242,6 +349,8 @@ export async function readEffectiveFormList(
242349
request: { type: string; packageId?: string; names?: readonly string[]; previewDrafts?: boolean },
243350
audience: 'served' | 'execution',
244351
): Promise<{ type: string; items: unknown[] } & Record<string, unknown>> {
352+
// Every reading below reads through one request scope: each stored row set once.
353+
host = formRequestScope(host);
245354
const { packageId: scope, names, ...rest } = request;
246355
const unscoped = { ...rest, type: 'form', ...(names !== undefined ? { names } : {}) };
247356
const genericList = await host.readFlattenedMetaItems(unscoped, audience) as { items: unknown[] } & Record<string, unknown>;
@@ -401,7 +510,7 @@ export async function readUnreadableFormBodies(
401510
report: (line: string) => void = (line) => console.error(line),
402511
): Promise<UnreadableFormBody[]> {
403512
const out: UnreadableFormBody[] = [];
404-
for (const entry of await storedEntries(host as FormReadHost, 'view')) {
513+
for (const entry of await storedEntries(inFormRequestScope(host as FormReadHost), 'view')) {
405514
const projection = projectViewForms(entry.data);
406515
if (projection.unreadable === undefined) continue;
407516
const body: UnreadableFormBody = {

‎packages/metadata-protocol/src/protocol-meta-item-reads.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,10 @@ async function readEffectiveFormItem(
695695
const generic: any = await host.readMetaItemFace(request, 'served');
696696
// ONE effective read of the name, unscoped: the list scoped to a package
697697
// is the same resolution filtered to that address (`readEffectiveFormList`),
698-
// so the address's answer and every address's are both read off it.
698+
// so the address's answer and every address's are both read off it. It
699+
// reads the `view` list once and each type's stored rows once (its request
700+
// scope, `formRequestScope`); the generic read above reads only the rows
701+
// of the name.
699702
const everywhere = await readEffectiveForms(host, { type: 'form', names: [request.name] }, 'served');
700703
const pkgOf = (item: unknown): unknown => (item as { _packageId?: unknown } | null)?._packageId;
701704
const listed = request.packageId === undefined

‎packages/metadata-protocol/src/protocol-meta-list-reads.ts‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
} from './protocol-authoring-refusals.js';
3535
import { shippingPackageIds } from './protocol-environment-rows.js';
3636
import type { ProtocolHost } from './protocol-host.js';
37-
import { readEffectiveFormList, type FormReadHost } from './protocol-form-reads.js';
37+
import { formRequestScope, inFormRequestScope, readEffectiveFormList, type FormReadHost } from './protocol-form-reads.js';
3838
import { canonicalizeMetaRequestType } from './protocol-meta-type-schema.js';
3939
import { mergePackageAwareOverlay, servedViewExpansion, withoutServedRowName } from './protocol-overlay-merge.js';
4040
import {
@@ -71,9 +71,11 @@ async function effectiveFormList(
7171
request: { type: string; packageId?: string; names?: readonly string[]; previewDrafts?: boolean },
7272
audience: 'served' | 'execution',
7373
) {
74-
const resolved = await readEffectiveFormList(host, request, audience);
74+
// One request scope: the scoped list's envelope reads the stored rows the resolution read.
75+
const scoped = formRequestScope(host);
76+
const resolved = await readEffectiveFormList(scoped, request, audience);
7577
if (request.packageId === undefined) return resolved;
76-
return { ...(await host.readFlattenedMetaItems(request, audience)), items: resolved.items };
78+
return { ...(await scoped.readFlattenedMetaItems(request, audience)), items: resolved.items };
7779
}
7880

7981
/** The body of `ObjectStackProtocolImplementation#getMetaItems`; the method's docblock there is its contract. */
@@ -92,7 +94,8 @@ export async function getMetaItems(host: FormReadHost, request: { type: string;
9294
// [ADR-0159 D5] The `form` list is resolved over both types, in one
9395
// generic read where the list names no package (`readEffectiveFormList`).
9496
if (canonical.type === 'form') return effectiveFormList(host, canonical, 'served');
95-
return host.readFlattenedMetaItems(canonical, 'served');
97+
// Inside a form request's reading (`readInFormRequest`), through its scope.
98+
return inFormRequestScope(host).readFlattenedMetaItems(canonical, 'served');
9699
}
97100

98101
/** The body of `ObjectStackProtocolImplementation#getMetaItemsForExecution`; the method's docblock there is its contract. */
@@ -104,9 +107,10 @@ export async function getMetaItemsForExecution(host: FormReadHost, request: { ty
104107
}
105108

106109
/** The body of `ObjectStackProtocolImplementation#getMetaItemsPerPackage`; the method's docblock there is its contract. */
107-
export async function getMetaItemsPerPackage(host: Pick<ProtocolHost, 'readFlattenedMetaItems'>, request: { type: string; packageId?: string; previewDrafts?: boolean }) {
110+
export async function getMetaItemsPerPackage(host: Pick<ProtocolHost, 'readActiveOverlayRows' | 'readFlattenedMetaItems'>, request: { type: string; packageId?: string; previewDrafts?: boolean }) {
108111
// The same #4432 fold {@link getMetaItems} applies, for the same reason.
109-
return host.readFlattenedMetaItems(canonicalizeMetaRequestType(request), 'served', 'per-package');
112+
// Inside a form request's reading (`readInFormRequest`), through its scope.
113+
return inFormRequestScope(host).readFlattenedMetaItems(canonicalizeMetaRequestType(request), 'served', 'per-package');
110114
}
111115

112116
/**
@@ -188,8 +192,11 @@ function disabledHolderListing(
188192
/** The body of `ObjectStackProtocolImplementation#readFlattenedMetaItems`; the method's docblock there is its contract. */
189193
export async function readFlattenedMetaItems(host: Pick<ProtocolHost, 'artifactLockLayerAt' | 'baseServesBundleMember' | 'convertStoredItem' | 'declinesStoredRow' | 'disabledActionsOf' | 'engine' | 'environmentId' | 'expandStoredViewContainers' | 'findEnvironmentRows' | 'flowRegistrationsOf' | 'foldObjectExtendersFromRegistry' | 'getServicesRegistry' | 'governServedObject' | 'hydrateOverlayIntoRegistry' | 'isStoredEntryOfDeclinedName' | 'isUnservedSealedOverlay' | 'isWritablePackage' | 'lookupArtifactItem' | 'namesWithOwnStoredRow' | 'readActiveOverlayRows' | 'readItemActiveRows' | 'rethrowUnlessMetadataStoreUnprovisioned' | 'shippedArtifactsOf' | 'shippingPackagesOf' | 'shipsBundleName' | 'storedOverlayEntries'>, request: { type: string; packageId?: string; names?: readonly string[]; previewDrafts?: boolean },
190194
audience: 'served' | 'execution',
191-
layer: 'effective' | 'per-package' = 'effective',) {
195+
layer: 'effective' | 'per-package' = 'effective',
196+
/** [ADR-0159 D5] One request's stored-row reader (`formRequestScope`); omitted, the host's. */
197+
rows?: (request: { type: string; packageId?: string }) => Promise<any[]>,) {
192198
const { packageId } = request;
199+
const readRows = rows ?? ((r: { type: string; packageId?: string }) => host.readActiveOverlayRows(r));
193200
const servedOverride = layer === 'effective';
194201
// The registry's installed manifests: a package's declared override is
195202
// its App's pair's (`pairOf`, `isServedOverrideRowOf`).
@@ -299,8 +306,8 @@ export async function readFlattenedMetaItems(host: Pick<ProtocolHost, 'artifactL
299306
// the view containers it expands through the same two calls, so
300307
// the two doors cannot disagree about which containers are in
301308
// scope for one caller.
302-
const records = await host.readActiveOverlayRows(request);
303-
lockRows = packageId ? await host.readActiveOverlayRows({ type: request.type }) : records;
309+
const records = await readRows(request);
310+
lockRows = packageId ? await readRows({ type: request.type }) : records;
304311
const standInRows = packageId ? lockRows.filter((row) => (row?.package_id ?? null) === null) : [];
305312
// [#28409, ADR-0146 D8 item 2] …and a list scoped to a package
306313
// serves, in each slot the package seats, the package's declared

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
22

33
import { formTypeWriteRefusal } from './protocol-form-writes.js';
4-
import { readUnreadableFormBodies } from './protocol-form-reads.js';
4+
import { readInFormRequest, readUnreadableFormBodies } from './protocol-form-reads.js';
55
import type {
66
DataProtocol,
77
FlowRegistration,
@@ -3540,6 +3540,11 @@ export class ObjectStackProtocolImplementation implements
35403540
return readUnreadableFormBodies(protocolHost(this));
35413541
}
35423542

3543+
/** Contract: {@link readInFormRequest} [ADR-0159 D5, D6]; the doors' five reads as one request reading, feature-detected likewise. */
3544+
async readFormRequest<T>(read: () => Promise<T>): Promise<T> {
3545+
return readInFormRequest(read);
3546+
}
3547+
35433548
/**
35443549
* [#28411, ADR-0146 D8 item 4] The served overrides of a base package's
35453550
* items: each ACTIVE row of its paired `<base>.custom` package that
@@ -3628,30 +3633,25 @@ export class ObjectStackProtocolImplementation implements
36283633
}
36293634

36303635
/**
3631-
* The one body behind {@link getMetaItems},
3632-
* {@link getMetaItemsForExecution} and {@link getMetaItemsPerPackage}.
3633-
* `audience` decides ONLY whether the serving decorations are applied at
3634-
* the very end; every source, merge and filter above that line is shared,
3635-
* so the list the engine executes and the list a caller is served cannot
3636+
* The one body behind {@link getMetaItems}, {@link getMetaItemsForExecution} and {@link getMetaItemsPerPackage}.
3637+
* `audience` decides ONLY whether the serving decorations are applied at the very end; every source, merge and
3638+
* filter above that line is shared, so the list the engine executes and the list a caller is served cannot
36363639
* disagree about which items exist.
36373640
*
3638-
* [#28409] `layer` decides ONLY whether the two steps of the one
3639-
* effective read are taken (ADR-0146 D8): a package's declared override
3640-
* served over its slot ({@link servedOverlayRowCandidates}'
3641-
* `servedOverride`), and, naming no package, a name exactly one code
3642-
* package ships listed once, under that package
3643-
* ({@link soleShippingPackageOf}). `'effective'` takes both; the
3644-
* per-package layer takes neither.
3645-
*
3646-
* `request` arrives already folded by the calling face (#4432).
3641+
* [#28409] `layer` decides ONLY whether the two steps of the one effective read are taken (ADR-0146 D8): a
3642+
* package's declared override served over its slot ({@link servedOverlayRowCandidates}' `servedOverride`), and,
3643+
* naming no package, a name exactly one code package ships listed once, under that package
3644+
* ({@link soleShippingPackageOf}). `'effective'` takes both; the per-package layer takes neither.
3645+
* `request` arrives already folded by the calling face (#4432). [ADR-0159 D5] `rows`, when given, is one
3646+
* request's stored-row reader (`formRequestScope`); omitted, {@link readActiveOverlayRows}.
36473647
*/
36483648
private async readFlattenedMetaItems(
36493649
request: { type: string; packageId?: string; names?: readonly string[]; previewDrafts?: boolean },
36503650
audience: 'served' | 'execution',
3651-
layer: 'effective' | 'per-package' = 'effective',
3651+
layer: 'effective' | 'per-package' = 'effective', rows?: (request: { type: string; packageId?: string }) => Promise<any[]>,
36523652
) {
36533653
// [#25467 E1] The body moved to `protocol-meta-list-reads.ts`; it reaches this class through `protocol-host.ts`.
3654-
return readFlattenedMetaItems(protocolHost(this), request, audience, layer);
3654+
return readFlattenedMetaItems(protocolHost(this), request, audience, layer, rows);
36553655
}
36563656

36573657
/**

0 commit comments

Comments
 (0)