diff --git a/.changeset/32716-judges-package-less-rows-boolean.md b/.changeset/32716-judges-package-less-rows-boolean.md new file mode 100644 index 00000000000..37f4e942346 --- /dev/null +++ b/.changeset/32716-judges-package-less-rows-boolean.md @@ -0,0 +1,16 @@ +--- +'@objectstack/metadata-protocol': patch +--- + +fix(metadata-protocol)!: `judgesPackageLessRows` returns a plain `boolean`, not a type predicate + +Clause-②: yes (narrowing) — the exported `judgesPackageLessRows`'s return type moves from the predicate `type is PackageScopedType` to `boolean`, so a TypeScript caller that narrowed `type` through its `true` branch no longer compiles. Its runtime answer is unchanged. +Metadata-protocol: none + + + +**BREAKING** for a TypeScript caller that narrows `type` to `PackageScopedType` through `judgesPackageLessRows(registry, type)` (for example `if (judgesPackageLessRows(r, t)) useScoped(t)`): it no longer compiles. Call `isPackageScopedType(type)` from `@objectstack/core` first, as `packageLessRowOwners` now does. + +`judgesPackageLessRows(registry, type)` was declared `type is PackageScopedType`. Its `false` answer depends on the registry (`readsByPackage`), not on the type alone, so TypeScript narrowed a package-scoped type to `never` in a caller's `false` branch, although a package-scoped type the registry does not read by package (`hook`, for one) lands there. It now returns `boolean`. Its runtime answer is unchanged. To narrow a `string` to `PackageScopedType`, call `isPackageScopedType` from `@objectstack/core`, whose answer depends on the type alone. + +No read moves. A stored package-less row of a sealed type (`action`, `queue`, `tool`) of a name a package ships, read at a holder's exact address (`?package=`), gets the holder's own item before and after the boot load (`loadMetaFromDb`), around the list read's hydrator, and with a copy in the registry's bare slot. This holds with one holder and with two. The boot load is now pinned beside the other hydration states. diff --git a/packages/metadata-protocol/src/protocol-exact-qualifier.test.ts b/packages/metadata-protocol/src/protocol-exact-qualifier.test.ts index 1ca612a7b4c..2eefd0cbc55 100644 --- a/packages/metadata-protocol/src/protocol-exact-qualifier.test.ts +++ b/packages/metadata-protocol/src/protocol-exact-qualifier.test.ts @@ -9,7 +9,8 @@ * by the admitted holder's. */ -import { describe, it, expect } from 'vitest'; +import { describe, it, expect, expectTypeOf } from 'vitest'; +import type { PackageScopedType } from '@objectstack/core'; import { exactQualifierItem, judgesPackageLessRows, packageLessRowOwners, pairRowOnAddress } from './protocol-exact-qualifier.js'; const BASE = 'com.acme.alpha'; @@ -86,6 +87,17 @@ describe('[#31699] packageLessRowOwners', () => { expect(packageLessRowOwners({ packageScopedHoldersOf: () => ['a.x', 'b.y'] }, 'action', 'approve')).toEqual(['a.x', 'b.y']); expect(judgesPackageLessRows({ readsByPackage: () => false }, 'function')).toBe(false); }); + + // [#32716 N1] Checked by `tsc` (the package's `typecheck` compiles this + // file): under the type predicate `type is PackageScopedType`, the `false` + // branch below narrowed `type` to `never`, though a package-scoped type the + // registry does not read by package (`hook`) lands there. + it('[#32716 N1] judgesPackageLessRows is a plain boolean: a package-scoped type it does not judge keeps its type in the false branch (BASE: never)', () => { + const type = 'hook' as PackageScopedType; + if (judgesPackageLessRows({ readsByPackage: () => false }, type)) throw new Error('hook is not judged here'); + expectTypeOf(type).toEqualTypeOf(); + expect(type).toBe('hook'); + }); }); /** diff --git a/packages/metadata-protocol/src/protocol-exact-qualifier.ts b/packages/metadata-protocol/src/protocol-exact-qualifier.ts index 8f14e88f213..197b862a41a 100644 --- a/packages/metadata-protocol/src/protocol-exact-qualifier.ts +++ b/packages/metadata-protocol/src/protocol-exact-qualifier.ts @@ -54,7 +54,6 @@ import { type PackageScopedHolder, type PackageScopedRefusalReason, type PackageScopedResolvableType, - type PackageScopedType, } from '@objectstack/core'; import { ENVIRONMENT_ADDRESS_SEGMENT, readNamespacePositionSegment } from '@objectstack/spec/kernel'; import { installedManifestsOfEngine } from './installed-manifests.js'; @@ -612,8 +611,13 @@ export function resolveCallerPackageAddress( * narrowed type lets two namespaces hold one name whether or not its readers * are wired, so the refusal is in place from the change that narrows it * (`action` since #31697, read through `getItem`), never one reader card later. + * + * A plain `boolean`, never the type predicate `type is PackageScopedType`: + * the `false` answer depends on the registry (`readsByPackage`), not on the + * type alone, so a predicate would narrow a package-scoped type to `never` in + * a caller's `false` branch (#32716 N1). */ -export function judgesPackageLessRows(registry: { readsByPackage?(type: string): boolean }, type: string): type is PackageScopedType { +export function judgesPackageLessRows(registry: { readsByPackage?(type: string): boolean }, type: string): boolean { if (!isPackageScopedType(type)) return false; return registry.readsByPackage?.(type) === true || isNamespaceScopedBareNameType(type); } @@ -643,7 +647,9 @@ export function packageLessRowOwners( name: string, options?: { includeDisabledPackages?: boolean }, ): string[] { - if (!judgesPackageLessRows(registry, type)) return []; + // `isPackageScopedType` narrows `type` (its answer is the type's alone); + // `judgesPackageLessRows` is a plain boolean (its answer is the registry's too). + if (!isPackageScopedType(type) || !judgesPackageLessRows(registry, type)) return []; if (isPlatformRequestedBareName(type, name)) return []; // [#32183] The served holders: a read judges them, so a package-less row // of a name an enabled and a disabled package hold is the enabled one's. diff --git a/packages/objectql/src/package-scoped-exact-qualifier.test.ts b/packages/objectql/src/package-scoped-exact-qualifier.test.ts index ab20fad70ed..c3b86cab134 100644 --- a/packages/objectql/src/package-scoped-exact-qualifier.test.ts +++ b/packages/objectql/src/package-scoped-exact-qualifier.test.ts @@ -2226,10 +2226,13 @@ describe('[#32568] a package-less row at a holder\'s exact address: declined for }); describe('location 4: one answer in every hydration state (both registration orders)', () => { - const STATES = ['stored, not hydrated', 'after the read-side hydrator', "bare-slot copy stamped ''", "bare-slot copy stamped 'sys_metadata'"] as const; + // [#32716 N3] The boot load (`loadMetaFromDb`) is the other hydration the + // card names beside the list read: the same answers before and after it. + const STATES = ['stored, not hydrated', 'after the read-side hydrator', 'after the boot load', "bare-slot copy stamped ''", "bare-slot copy stamped 'sys_metadata'"] as const; const enter = async (type: string, state: (typeof STATES)[number]) => { storeRow(type, 'active', 'stored'); if (state === 'after the read-side hydrator') await protocol.getMetaItems({ type } as never); + if (state === 'after the boot load') expect(await protocol.loadMetaFromDb(), 'the boot load read the row').toMatchObject({ loaded: 1, errors: 0 }); if (state.startsWith('bare-slot')) { registry.registerItem(type, { ...sealedBody(type, 'stored'), _provenance: 'org', _packageId: state.endsWith("''") ? '' : 'sys_metadata' }, 'name' as never); } @@ -2258,11 +2261,12 @@ describe('[#32568] a package-less row at a holder\'s exact address: declined for } } - it(`${type}, one holder: the stored row is declined before and after the read-side hydrator, on the exact and the bare read (BASE: after it, both served the row)`, async () => { + it(`${type}, one holder: the stored row is declined before and after the boot load and the read-side hydrator, on the exact and the bare read (BASE: after the hydrator, both served the row)`, async () => { shipSealed(type, A); storeRow(type, 'active', 'stored'); - for (const when of ['before', 'after']) { - if (when === 'after') await protocol.getMetaItems({ type } as never); + for (const when of ['before', 'after the boot load', 'after the read-side hydrator']) { + if (when === 'after the boot load') await protocol.loadMetaFromDb(); + if (when === 'after the read-side hydrator') await protocol.getMetaItems({ type } as never); expect(labelOf(await read(type, 'approve', A)), `${when} exact`).toBe(`alpha ${type}`); expect(labelOf(await protocol.getMetaItem({ type, name: 'approve' } as never)), `${when} bare`).toBe(`alpha ${type}`); }