From 4001a5c55926528701e401cb94b210c5fd56f22e Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:55:54 +0000 Subject: [PATCH 01/12] feat(scripts): codemod-split-registry, the registry.ts split as a re-runnable command The first pass of codemod-split-engine.mjs, copied with its host parameterised (registry.ts, SchemaRegistry, registry-*, #33949) and an exports-equality proof added beside its byte-identity proof. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ --- scripts/codemod-split-registry.mjs | 3067 ++++++++++++++++++++++++++++ 1 file changed, 3067 insertions(+) create mode 100644 scripts/codemod-split-registry.mjs diff --git a/scripts/codemod-split-registry.mjs b/scripts/codemod-split-registry.mjs new file mode 100644 index 00000000000..038976dec46 --- /dev/null +++ b/scripts/codemod-split-registry.mjs @@ -0,0 +1,3067 @@ +#!/usr/bin/env node +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * codemod-split-registry — move whole sections of `SchemaRegistry`'s method + * bodies out of `packages/objectql/src/registry.ts` into section modules, move + * the top-level declarations ahead of the class into one leaf module, + * deterministically, and prove the move: byte-identity, and the module's + * exports unchanged. + * + * node scripts/codemod-split-registry.mjs # plan: print the section table, prove it, write nothing + * node scripts/codemod-split-registry.mjs --write # apply the split, then verify it + * node scripts/codemod-split-registry.mjs --verify --base + * # re-prove a split on disk against registry.ts at + * node scripts/codemod-split-registry.mjs --self-test + * node scripts/codemod-split-registry.mjs --help + * + * ## Why a script, and whose + * + * The hot-file programme splits the platform's largest files one at a time + * (ruling 6897467376 on #33949, item 1: `registry.ts` first). Each split is + * generated by script against fresh `main` right before it is queued, the + * original file stays as the forwarding entry so the public exports are + * unchanged, and a split waits for every open PR touching its file to merge. + * So the split is a COMMAND, not a diff: re-run on the latest `main`, never + * rebased, never hand-edited. + * + * This script is the FIRST PASS of `scripts/codemod-split-engine.mjs` + * (#29986, the objectql `engine.ts` split; the programme's pilot is + * `scripts/codemod-split-sql-driver.mjs` and its other precedent + * `scripts/codemod-split-protocol.mjs`), copied with its five later passes + * left out and its host parameterised: the file `registry.ts`, the class + * `SchemaRegistry`, the module prefix `registry-`, the card `#33949`. The cut + * pattern, the stay rules, the leaf, the facet hosts, the byte-identity + * proof and that pass's six self-test batteries are copied as they stand; + * the copy renamed `engine.ts` / `ObjectQL` / "the engine" to `registry.ts` / + * `SchemaRegistry` / "the registry" throughout, comments included, so a card + * number in a comment below (#30020, #30051, …) is the engine card that + * introduced the rule, not a registry one. Copied, never imported (the + * engine script's constants are module-level), so a later fix to one is a + * decision about the other too. + * + * In short, as that pass does it: + * + * - A method stays on the class with its docblock, modifiers and signature + * byte-for-byte, so the published `.d.ts` keeps its names and an instance + * spy or a subclass override is still the one callers reach. Its body + * moves, unedited, into a static method of a stateless facet class in + * `registry-
.ts`, whose `this` is the facet's host: the + * `SchemaRegistry` members the body reads, typed AS the class members. + * - The stub is one line, `Facet.x.call(xHost.of(this), …)`; the host is ONE + * object per facet and registry (`HOSTS_MODULE`), built on first use by a + * factory the class's static block installs. + * - The top-level declarations ahead of the class (`const` / `function` / + * `interface` / `type`) move whole, with their comments, into + * `LEAF_MODULE`, which never imports `registry.ts` or a `registry-*` + * module; `registry.ts` re-exports each exported one by name. + * - A method or declaration a mechanical rule keeps (`STAY_REASONS`, + * `TOP_STAY_REASONS`) stays whole; the plan prints every one and why. + * + * ## What this script adds: exports-equality (Proof 2) + * + * The ruling's "public exports are unchanged" is checked, not assumed: + * `verifyExports` compiles the base `registry.ts` alone and the split one + * with every generated module in one in-memory TypeScript project and + * requires the two export tables to be equal, name for name, each a value or + * a type as it was. `--write` and `--verify` both run it; a difference is + * exit 1 and nothing is written. + * + * ## Re-running it (the ruling's "regenerated against fresh main") + * + * On a branch freshly cut from the latest `origin/main`, ONE command: + * + * node scripts/codemod-split-registry.mjs --write + * + * then the facade's `ceilings` row in `scripts/facade-delegation-registry.json` + * is re-measured on the written file. `--verify --base origin/main` re-proves + * the result on disk. + * + * ts-morph is a root devDependency, resolved from the root manifest. + * + * Exit codes: 0 done · 1 a proof failed (stop: the generated split is never + * hand-edited to pass) · 2 the script could not run (unknown anchor, missing + * prerequisite, bad arguments) · 3 an input does not parse (`ts-parse.mjs`'s + * refusal, which names the parse errors). + */ + +import { createHash } from 'node:crypto'; +import { execFileSync } from 'node:child_process'; +import { existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'; +import { createRequire } from 'node:module'; +import { dirname, join, relative, resolve } from 'node:path'; +import { fileURLToPath, pathToFileURL } from 'node:url'; + +import { isEntrypoint } from './invoked-as.mjs'; +import { parseSourceFile } from './ts-parse.mjs'; + +const ROOT = join(fileURLToPath(new URL('.', import.meta.url)), '..'); +const SRC_REL = 'packages/objectql/src'; +const SRC_DIR = join(ROOT, SRC_REL); +const HOST_BASENAME = 'registry.ts'; +const HOST_PATH = join(SRC_DIR, HOST_BASENAME); +const CLASS_NAME = 'SchemaRegistry'; +const CARD = '#33949'; +const SCRIPT = 'scripts/codemod-split-registry.mjs'; + +/** The marker every generated file and stub carries; `--verify` keys on it. */ +const MARK = `[${CARD} codemod]`; + +/** A generated or hand-series section module of `registry.ts`, by its import specifier. */ +const SEAM_SPECIFIER = /^\.\/registry-[a-z0-9-]+\.js$/; + +/** + * The sections of `SchemaRegistry`, top to bottom, each named by its first member + * (a field or a method), mapped to the module its movable bodies go to. + * `null` keeps the section on the class. No module shares a file with a + * hand-written one: the script refuses to write over a file it did not + * generate, and the hand series' 25 module names are not reused. + */ +const SECTION_ANCHORS = Object.freeze([ + ['_logLevel', 'Logging control, object storage and generic metadata storage', 'registry-storage.ts'], + ['restorePoint', 'Restore on refusal', 'registry-restore.ts'], + ['registerNamespace', 'Namespaces, default record pages and security catalog names', 'registry-namespaces-and-claims.ts'], + ['bareNameClaims', 'Bare names, one name one package holder', 'registry-bare-names.ts'], + ['objectRegistrationRefusal', 'Object registration (ownership model)', 'registry-object-registration.ts'], + ['setDeploymentPlatformGlobalObjects', 'Platform-global declaration and uninstall checks', 'registry-platform-global.ts'], + ['registerItem', 'Generic metadata items and package bodies', 'registry-items.ts'], + ['overlaySlotBinding', 'Package-scoped addressing', 'registry-package-addressing.ts'], + ['installPackage', 'Package management', 'registry-packages.ts'], + ['registerApp', 'Apps, navigation contributions and their diagnostics', 'registry-apps.ts'], + ['registerPlugin', 'Plugins, kinds, shared picklists and reset', 'registry-picklists.ts'], +]); + +/** + * The leaf the top-level declarations ahead of the class move to, whole + * (lever (b) of ruling `6591753301`). A leaf: it never imports `registry.ts` or + * any `registry-*` module, so it is evaluated before the class and forms no + * cycle with it. + */ +const LEAF_MODULE = 'registry-top-level.ts'; + +/** + * The module that holds each facet's host, one per registry (#30051): a + * `FacetHost` per facet module, built on first use by the factory the class's + * static block installs and kept for the registry's life. It imports types only, + * so it is evaluated before the class whichever module is loaded first, and + * the class's static block can install into it. + */ +const HOSTS_MODULE = 'registry-facet-hosts.ts'; +const HOSTS_CLASS = 'FacetHost'; + +/** + * Names whose text a reader takes out of `registry.ts` BY PATH. Moving it would + * leave that reader reading a stub, or nothing. A class method or a top-level + * declaration of the name stays. Each row names the reader. A row naming + * nothing the file declares stops the script (exit 2), so the table cannot rot + * into names nobody reads. + */ +const PATH_READER_PINS = Object.freeze({ + // Empty since #30055: the six rows the #4918 query-options erasure guard + // held here (its per-file baseline names `registry.ts`, so a moved `as any` + // site would have red `pnpm lint`) retired once their sites were typed, and + // the fourth pass (`--members`) moved the six bodies. +}); + +/** + * Literal text a reader requires to find in `registry.ts` BY PATH. A class + * method or a top-level declaration whose text carries one stays, so the + * reader still finds it where it looks. Each row names the reader. A needle + * the file no longer carries stops the script (exit 2). + */ +const PATH_READER_NEEDLES = Object.freeze({ +}); + +/** + * Literal text a reader requires to find in `registry.ts` OR the leaf, but reads + * in no section module. A method whose text carries one stays on the class; a + * top-level declaration carrying one moves as any other, because the reader + * reads the leaf it moves to. The same stale-needle refusal applies. + */ +const CLASS_PATH_READER_NEEDLES = Object.freeze({ +}); + +/** + * The `SchemaRegistry` methods a host hands its body as a BOUND reference + * (`this.m.bind(this)`) rather than as a closure that calls it. The pilot + * needed one for a gate that refuses an unbound builder call; no gate reads + * `registry.ts` that way, so the set is empty and every method goes as a + * closure. + */ +const BOUND_HOST_METHODS = new Set(); + +/** Every reason a method stays, with the one-line why the plan prints. */ +const STAY_REASONS = Object.freeze({ + 'no-section': 'declared above the first section anchor', + 'section-kept': 'its section maps to no module', + 'static-this': 'a static method whose body reads `this` (the class; a facet static has its own)', + 'static-name-shared': 'a static method whose name an instance member also takes (one facet cannot hold both)', + 'no-body': 'an overload signature or abstract member', + 'generator': 'a generator', + 'overloaded': 'an overloaded method', + 'decorated': 'a decorated method', + 'not-identifier-name': 'a computed, string or #private name', + 'inferred-return-type': 'a non-private method whose return type is inferred (the .d.ts would infer it through the module)', + 'parameter-shape': 'a destructured or `this` parameter (the stub cannot forward it unchanged)', + 'comment-in-parameters': 'a comment inside its parameter list (one-line parameters would drop it)', + 'path-reader': 'a reader takes its text out of registry.ts by path (PATH_READER_PINS / PATH_READER_NEEDLES / CLASS_PATH_READER_NEEDLES)', + 'arguments': 'its body reads `arguments`', + 'super': 'its body reads `super`', + 'new-target': 'its body reads `new.target`', + 'this-type': 'it names the polymorphic `this` type', + 'bare-this': 'it uses `this` other than as `this.member`', + 'unknown-member': 'it reads a `this.member` the class does not declare', + 'this-write': 'it assigns a `this.member` that is not a field, or deletes one (the host carries a setter for a field only)', + 'method-as-value': 'it reads a method as a value instead of calling it', + 'field-call': 'it calls a field or accessor as a function (the host would change its receiver)', + 'overloaded-host-method': 'it calls an overloaded method (the host closure cannot be typed)', + 'static-write': 'its static reads go through SchemaRegistryStatics and it assigns a static (the forwarder is read-only)', + 'static-as-value': 'its static reads go through SchemaRegistryStatics and it reads a static method as a value (a forwarder is another function)', + 'static-field-call': 'its static reads go through SchemaRegistryStatics and it calls a static field or accessor (the receiver would change)', + 'overloaded-static': 'its static reads go through SchemaRegistryStatics and it calls an overloaded static (one forwarder cannot carry the overloads)', + 'static-route-conflict': 'its facet module reads the statics through SchemaRegistryStatics, imported AS `SchemaRegistry`, and it names `SchemaRegistry` other than as `SchemaRegistry.`', + 'module-write': 'it assigns a module-scope binding of registry.ts (an import is read-only)', + 'already-moved': 'its body is already a hand-series seam stub', + 'parameter-shadows': 'a parameter shadows a stub local (`host`, `readX`, `writeX`)', + 'foreign-private': 'it reads a private or protected instance member off a receiver other than `this` (a module function cannot)', + 'private-static': 'it reads a non-public static of its class (the statics route is `SchemaRegistry`\'s alone; the scoped classes declare none)', + 'facade-taken': 'it hands the registry on, and the class declares a member named `facade` (the identity port would shadow it)', + 'ctor-facet-name': 'a class that declares a member of the name a moved constructor body takes in its facet', + 'definite-assignment': 'a constructor that writes a field only a constructor may assign (TS2564) other than as R8 carries it: a leading `this.F = EXPR;` whose EXPR reads no `this` and no top-level name of registry.ts', +}); + +/** + * The fourth pass's identity port (#30020 R1): the host entry a moved body + * reads the registry itself through, where the method read a bare `this` + * (`ql: this`, `new ScopedContext(ctx, this)`, `(this as any)._flag`). The + * host factory hands it as the registry (`facade: registry`), so at each such site + * the body sees the same object the method saw. + */ +const FACADE_MEMBER = 'facade'; +const FACADE_DOC = ' /** The registry itself: a moved body reads `this.facade` where the method read a bare `this` (#30020). */'; +const facadeIfaceLine = (classType) => `${FACADE_DOC}\n readonly ${FACADE_MEMBER}: ${classType};`; +const FACADE_INSTALL_LINE = ` ${FACADE_MEMBER}: registry,`; +/** The same port on a scoped class's instance host (#30020 S1): `facade: instance`. */ +const SCOPED_FACADE_DOC = ' /** The instance itself: a moved body reads `this.facade` where the method read a bare `this` (#30020). */'; +const SCOPED_FACADE_INSTALL_LINE = ` ${FACADE_MEMBER}: instance,`; + +/** Every reason a top-level declaration ahead of the class stays in registry.ts. */ +const TOP_STAY_REASONS = Object.freeze({ + 'top-kind': 'not a const / function / interface / type declaration of plain names', + 'merged-declaration': 'its name is declared by more than one statement (an overload set or a merged declaration)', + 'path-reader': 'a reader takes its text out of registry.ts by path (PATH_READER_PINS / PATH_READER_NEEDLES)', + 'class-reference': 'it names a class registry.ts declares (the leaf would import registry.ts back)', + 'seam-import': 'it uses an import from a registry-* module (the leaf would reach registry.ts back through it)', + 'needs-stayer': 'it names a declaration that stays (the leaf would import registry.ts back)', +}); + +// ── Loading ts-morph ───────────────────────────────────────────────────────── + +let tsMorph = null; + +/** ts-morph from the root manifest's devDependencies; a missing install is exit 2, never a stack trace. */ +async function loadTsMorph() { + if (tsMorph) return tsMorph; + const manifest = join(ROOT, 'package.json'); + try { + const resolved = createRequire(manifest).resolve('ts-morph'); + const mod = await import(pathToFileURL(resolved).href); + tsMorph = mod.default ?? mod; + return tsMorph; + } catch (error) { + console.error( + `✗ codemod-split-registry: PREREQUISITE NOT MET — ts-morph does not resolve from ${manifest} ` + + `(${error && error.code ? error.code : 'error'}). Run \`pnpm install\` in this worktree.`, + ); + process.exit(2); + } +} + +/** + * One in-memory project per parse: no file system, no lib, no build output. + * The text goes through `ts-parse.mjs`'s checked parse first, which refuses a + * source with parse errors (its exit 3) instead of handing back a recovered + * partial tree; ts-morph then reads the same text, already vouched for. + */ +function parse(tm, fileName, text) { + parseSourceFile(fileName, text); + const project = new tm.Project({ + useInMemoryFileSystem: true, + compilerOptions: { target: tm.ts.ScriptTarget.ES2020, strict: true, noLib: true, allowJs: false }, + }); + project.getFileSystem().writeFileSync(fileName, text); + return project.addSourceFileAtPath(fileName); +} + +/** A reason the script cannot run at all (exit 2) — never a verdict on the split. */ +class CodemodRefusal extends Error {} + +function refuse(message) { + throw new CodemodRefusal(message); +} + +// ── Reading the sections ───────────────────────────────────────────────────── + +/** + * Each member's section: the last anchor at or above it. Refuses an anchor the + * class does not declare, a duplicate anchor, or anchors out of class order. + * Returns a function from a member node to its `[anchor, title, module]` row, + * or null above the first anchor. + */ +function readSections(cls, anchors) { + const firstStart = new Map(); + for (const m of cls.getMembers()) { + const name = typeof m.getName === 'function' ? m.getName() : null; + if (name && !firstStart.has(name)) firstStart.set(name, m.getStart()); + } + const rows = []; + const seen = new Set(); + for (const row of anchors) { + const [anchor] = row; + if (seen.has(anchor)) refuse(`SECTION_ANCHORS names ${anchor} twice`); + seen.add(anchor); + if (!firstStart.has(anchor)) refuse(`SECTION_ANCHORS names ${anchor}, which ${CLASS_NAME} does not declare`); + rows.push({ row, at: firstStart.get(anchor) }); + } + for (let i = 1; i < rows.length; i += 1) { + if (rows[i].at <= rows[i - 1].at) refuse(`SECTION_ANCHORS out of class order: ${rows[i].row[0]} is declared above ${rows[i - 1].row[0]}`); + } + return (member) => { + let hit = null; + for (const r of rows) if (r.at <= member.getStart()) hit = r.row; + return hit; + }; +} +// ── Analysis ───────────────────────────────────────────────────────────────── + +/** The names registry.ts binds at module scope, and which of them it exports. */ +function moduleScope(tm, sf) { + const { Node } = tm; + const bindings = new Map(); // name -> { kind: 'import' | 'local', specifier?, form?, imported?, typeOnly? } + const exported = new Set(); + const importOrder = new Map(); // specifier -> first position among the import declarations + for (const st of sf.getStatements()) { + if (Node.isImportDeclaration(st)) { + const specifier = st.getModuleSpecifierValue(); + const order = importOrder.has(specifier) ? importOrder.get(specifier) : importOrder.size; + importOrder.set(specifier, order); + const declTypeOnly = st.isTypeOnly(); + const def = st.getDefaultImport(); + if (def) bindings.set(def.getText(), { kind: 'import', specifier, form: 'default', typeOnly: declTypeOnly }); + const ns = st.getNamespaceImport(); + if (ns) bindings.set(ns.getText(), { kind: 'import', specifier, form: 'namespace', typeOnly: declTypeOnly }); + for (const spec of st.getNamedImports()) { + bindings.set((spec.getAliasNode() ?? spec.getNameNode()).getText(), { + kind: 'import', specifier, form: 'named', imported: spec.getNameNode().getText(), + typeOnly: declTypeOnly || spec.isTypeOnly(), + }); + } + continue; + } + if (Node.isExportDeclaration(st)) { + if (!st.getModuleSpecifier()) for (const spec of st.getNamedExports()) exported.add(spec.getNameNode().getText()); + continue; + } + const isExported = Node.isExportable(st) && st.isExported(); + const names = []; + if (Node.isVariableStatement(st)) { + for (const decl of st.getDeclarations()) { + for (const id of [decl.getNameNode(), ...decl.getNameNode().getDescendantsOfKind(tm.SyntaxKind.Identifier)]) { + if (Node.isIdentifier(id)) names.push(id.getText()); + } + } + } else if (typeof st.getName === 'function' && st.getName()) { + names.push(st.getName()); + } + for (const name of names) { + bindings.set(name, { kind: 'local' }); + if (isExported) exported.add(name); + } + } + return { bindings, exported, importOrder }; +} + +/** Is `node` inside a type node, below `stop`? */ +function inTypePosition(tm, node, stop) { + for (let n = node.getParent(); n && n !== stop; n = n.getParent()) { + if (tm.ts.isTypeNode(n.compilerNode)) return true; + } + return false; +} + +/** The node that binds the `this` a `this` keyword at `node` reads. */ +function thisContainer(tm, node) { + const { Node } = tm; + for (let n = node.getParent(); n; n = n.getParent()) { + if ( + Node.isFunctionDeclaration(n) || Node.isFunctionExpression(n) || Node.isMethodDeclaration(n) || + Node.isConstructorDeclaration(n) || Node.isGetAccessorDeclaration(n) || Node.isSetAccessorDeclaration(n) || + Node.isClassDeclaration(n) || Node.isClassExpression(n) || Node.isClassStaticBlockDeclaration(n) + ) return n; + } + return null; +} + +/** Is the property access `pa` written (assigned, updated, deleted, destructured into)? */ +function isWritten(tm, pa) { + const { Node, SyntaxKind } = tm; + let target = pa; + for (let p = target.getParent(); p; target = p, p = p.getParent()) { + if ( + Node.isParenthesizedExpression(p) || Node.isArrayLiteralExpression(p) || Node.isObjectLiteralExpression(p) || + Node.isPropertyAssignment(p) || Node.isShorthandPropertyAssignment(p) || Node.isSpreadElement(p) || + Node.isSpreadAssignment(p) + ) continue; + break; + } + const parent = target.getParent(); + if (!parent) return false; + if (Node.isBinaryExpression(parent) && parent.getLeft() === target) { + const op = parent.getOperatorToken().getKind(); + return op >= SyntaxKind.FirstAssignment && op <= SyntaxKind.LastAssignment; + } + if (target === pa && (Node.isPrefixUnaryExpression(parent) || Node.isPostfixUnaryExpression(parent))) { + const op = parent.getOperatorToken(); + return op === SyntaxKind.PlusPlusToken || op === SyntaxKind.MinusMinusToken; + } + if (target === pa && Node.isDeleteExpression(parent)) return true; + if ((Node.isForOfStatement(parent) || Node.isForInStatement(parent)) && parent.getInitializer() === target) return true; + return false; +} + +/** The class's instance members by name: `{ kind: 'method' | 'field' | 'getter' | 'setter', overloaded }`. */ +function instanceMembers(tm, cls) { + const { Node } = tm; + const members = new Map(); + const note = (name, kind, extra = {}) => { + const prev = members.get(name); + if (prev && prev.kind === 'setter' && kind === 'getter') { members.set(name, { kind: 'getter', ...extra }); return; } + if (prev && prev.kind === 'getter' && kind === 'setter') return; + members.set(name, { kind, ...extra }); + }; + for (const m of cls.getMembers()) { + if (Node.isConstructorDeclaration(m)) { + for (const p of m.getParameters()) if (p.isParameterProperty()) note(p.getName(), 'field'); + continue; + } + if (typeof m.isStatic === 'function' && m.isStatic()) continue; + const nameNode = typeof m.getNameNode === 'function' ? m.getNameNode() : null; + if (!nameNode || !Node.isIdentifier(nameNode)) continue; + const name = nameNode.getText(); + if (Node.isMethodDeclaration(m)) note(name, 'method', { overloaded: m.getOverloads().length > 0 }); + else if (Node.isPropertyDeclaration(m)) note(name, 'field'); + else if (Node.isGetAccessorDeclaration(m)) note(name, 'getter'); + else if (Node.isSetAccessorDeclaration(m)) note(name, 'setter'); + } + return members; +} + +/** + * Read one method: why it must stay (empty = it moves), what its body reads + * through the host, and which imports its module needs. + */ +function readMethod(tm, ctx, m, { skip = [] } = {}) { + const { Node, SyntaxKind } = tm; + // The constructor (#30020 R8) is read as a method is, with the statements + // R8 keeps on the class (`skip`, source ranges) left out of the reading. + const isCtor = Node.isConstructorDeclaration(m); + const skipped = (node) => skip.some(([s, e]) => node.getStart() >= s && node.getEnd() <= e); + const reasons = []; + const add = (code, detail) => { + const tag = detail ? `${code}: ${detail}` : code; + if (!reasons.includes(tag)) reasons.push(tag); + }; + const host = new Map(); // member -> 'method' | 'getter' | 'facade' + const writes = new Set(); // fields the body assigns: the host carries a setter for each + const imports = new Map(); // local -> { value } + const moduleRefs = new Map(); // registry.ts top-level name -> { value } + // The identity port (#30020 R1, the fourth pass only): the start of every + // bare `this` in the body, each of which the facet member reads as + // `this.facade`, the host entry that hands the registry itself. + const identity = []; + // The private-static-through-host rule: the statics the body reads as + // `SchemaRegistry.x`, whether one is non-public (that takes the route), what the + // route cannot carry, and whether the body names `SchemaRegistry` any other way. + const classStatics = new Map(); // static -> 'method' | 'getter' + const routeBlockers = []; + let readsNonPublicStatic = false; + let classOther = false; + // The parameters the body reads (a constructor's facet takes only those: R8). + const paramsRead = new Set(); + const result = () => ({ reasons, host, writes, imports, moduleRefs, classStatics, routeBlockers, readsNonPublicStatic, classOther, identity, paramsRead }); + + if (!isCtor && m.isStatic() && ctx.members.has(m.getName())) add('static-name-shared'); + if (!m.getBody()) add('no-body'); + if (m.isAbstract && m.isAbstract()) add('no-body'); + if (!isCtor && m.isGenerator()) add('generator'); + if (m.getOverloads().length > 0) add('overloaded'); + if (!isCtor && m.getDecorators().length > 0) add('decorated'); + const nameNode = isCtor ? null : m.getNameNode(); + // R9 (#30020, the second pass's members mode only): a method keyed by a + // symbol binding (`[SEED_USER_GRANTS]`) moves under that binding's facet name. + const symbolKey = !isCtor && ctx.symbolKeys ? symbolKeyOf(tm, ctx, m) : null; + if (!isCtor && !Node.isIdentifier(nameNode) && !symbolKey) { add('not-identifier-name'); return result(); } + const name = isCtor ? 'constructor' : nameNode.getText(); + if (!isCtor && !m.getReturnTypeNode() && m.getScope() !== 'private') add('inferred-return-type'); + for (const p of m.getParameters()) { + // A defaulted identifier forwards as it is: the stub keeps the default and + // hands the body the value it produced. + if (!Node.isIdentifier(p.getNameNode()) || p.getName() === 'this') add('parameter-shape', p.getName()); + } + const body = m.getBody(); + if (!body) return result(); + + // A comment between the parentheses that no parameter's own text carries. + const params = m.getParameters(); + const open = m.getFirstChildByKindOrThrow(SyntaxKind.OpenParenToken); + const close = m.getFirstChildByKindOrThrow(SyntaxKind.CloseParenToken); + let between = ctx.text.slice(open.getEnd(), close.getStart()); + for (const p of [...params].reverse()) { + between = between.slice(0, p.getStart() - open.getEnd()) + between.slice(p.getEnd() - open.getEnd()); + } + if (/\/\/|\/\*/.test(between)) add('comment-in-parameters'); + + const methodText = m.getText(); + for (const needle of Object.keys(ctx.needles)) if (methodText.includes(needle)) add('path-reader', JSON.stringify(needle)); + for (const needle of Object.keys(ctx.classNeedles)) if (methodText.includes(needle)) add('path-reader', JSON.stringify(needle)); + if (Object.prototype.hasOwnProperty.call(ctx.pins, name)) add('path-reader', name); + + const regions = [...m.getTypeParameters(), ...params, m.getReturnTypeNode(), body].filter(Boolean); + const checker = ctx.sf.getProject().getTypeChecker().compilerObject; + for (const region of regions) { + const nodes = [region, ...region.getDescendants()]; + for (const node of nodes) { + if (skip.length > 0 && skipped(node)) continue; + const kind = node.getKind(); + if (kind === SyntaxKind.SuperKeyword) add('super'); + if (kind === SyntaxKind.ThisType) add('this-type'); + if (Node.isMetaProperty(node) && node.getKeywordToken() === SyntaxKind.NewKeyword) add('new-target'); + if (Node.isPropertyAccessExpression(node) && node.getExpression().getKind() !== SyntaxKind.ThisKeyword) { + // `other.x` where `x` is a private or protected INSTANCE member of the + // class: legal in the class body, refused in a module function. (A + // static is the statics route's question, not this one.) + const foreign = foreignNonPublicMember(tm, ctx, checker, node); + if (foreign) add('foreign-private', foreign); + } + if (kind === SyntaxKind.ThisKeyword) { + if (thisContainer(tm, node) !== m) continue; + if (!isCtor && m.isStatic()) { add('static-this'); continue; } + const parent = node.getParent(); + const inBody = region === body && !inTypePosition(tm, node, m); + if (ctx.identityPort && inBody && Node.isElementAccessExpression(parent) && parent.getExpression() === node) { + // R3 (#30020): `this[k]`, `k` typed as string literals each naming a + // field, reads (and writes) those fields through host getters (and + // setters); the body's text is unchanged. + const keys = literalFieldKeys(ctx, checker, parent.getArgumentExpression()); + if (!keys) { add('bare-this'); continue; } + const outer = parent.getParent(); + if (Node.isCallExpression(outer) && outer.getExpression() === parent) { add('field-call', keys.join('|')); continue; } + const written = isWritten(tm, parent); + if (written && Node.isDeleteExpression(outer)) { add('this-write', keys.join('|')); continue; } + for (const k of keys) { + host.set(k, 'getter'); + if (written) writes.add(k); + } + continue; + } + if (!Node.isPropertyAccessExpression(parent) || parent.getExpression() !== node) { + // R1 (#30020): a `this` that hands the registry on reads the host's + // `facade`, the registry itself. + if (ctx.identityPort && inBody && !Node.isElementAccessExpression(parent)) { + if (ctx.members.has(FACADE_MEMBER)) { add('facade-taken'); continue; } + identity.push(node.getStart()); + host.set(FACADE_MEMBER, 'facade'); + continue; + } + add('bare-this'); + continue; + } + const memberNameNode = parent.getNameNode(); + if (!Node.isIdentifier(memberNameNode)) { add('bare-this'); continue; } + const member = memberNameNode.getText(); + const info = ctx.members.get(member); + if (!info) { add('unknown-member', member); continue; } + if (isWritten(tm, parent)) { + // A field is written through a host setter; a method, an accessor or + // a `delete` cannot be. + if (info.kind === 'field' && !Node.isDeleteExpression(parent.getParent())) writes.add(member); + else add('this-write', member); + } + const outer = parent.getParent(); + const called = Node.isCallExpression(outer) && outer.getExpression() === parent; + if (info.kind === 'method') { + if (!called) add('method-as-value', member); + if (info.overloaded) add('overloaded-host-method', member); + host.set(member, 'method'); + } else if (info.kind === 'setter') { + add('this-write', member); + } else { + if (called) add('field-call', member); + host.set(member, 'getter'); + } + continue; + } + if (kind !== SyntaxKind.Identifier) continue; + // `typeof this` (its `this` is an identifier in a type query): with the + // identity port the value `this` beside it would read `this.facade` and + // the type would name the host, so the body stays as the polymorphic + // `this` type does. + if (ctx.identityPort && node.getText() === 'this' && Node.isTypeQuery(node.getParent())) add('this-type'); + if (node.getText() === 'arguments') { + const parent = node.getParent(); + const isName = Node.isPropertyAccessExpression(parent) && parent.getNameNode() === node; + if (!isName) add('arguments'); + } + const parent = node.getParent(); + let symbol; + if (Node.isShorthandPropertyAssignment(parent) && parent.getNameNode() === node) { + symbol = checker.getShorthandAssignmentValueSymbol(parent.compilerNode); + } else { + symbol = checker.getSymbolAtLocation(node.compilerNode); + } + if (!symbol || !symbol.declarations) continue; + for (const decl of symbol.declarations) { + if (decl.getSourceFile() !== ctx.sf.compilerNode) continue; + const ts = tm.ts; + const typePos = inTypePosition(tm, node, m); + if (ts.isParameter(decl) && decl.parent === m.compilerNode && region === body) paramsRead.add(node.getText()); + if (ts.isImportSpecifier(decl) || ts.isImportClause(decl) || ts.isNamespaceImport(decl)) { + const local = node.getText(); + const prev = imports.get(local); + imports.set(local, { value: Boolean(prev && prev.value) || !typePos }); + continue; + } + if (!isModuleScopeDecl(ts, decl)) continue; + // Lever (a) of ruling 6591753301: a module-scope name is imported — + // from the leaf when it moved there, from `registry.ts` (exported + // for it) when it stayed. Two things a module function cannot do: + // reach a non-public static of the class, and assign an import. + const declName = decl.name && ts.isIdentifier(decl.name) ? decl.name.text : node.getText(); + if (ts.isClassDeclaration(decl) && declName === (ctx.className ?? CLASS_NAME)) { + const access = node.getParent(); + const info = !typePos && Node.isPropertyAccessExpression(access) && access.getExpression() === node + ? ctx.statics.get(access.getName()) : null; + if (info) { + const member = access.getName(); + if (info.scope !== 'public') readsNonPublicStatic = true; + const outer = access.getParent(); + const called = Node.isCallExpression(outer) && outer.getExpression() === access; + const block = (tag) => { if (!routeBlockers.includes(tag)) routeBlockers.push(tag); }; + if (isWritten(tm, access) || info.kind === 'setter') block(`static-write: ${member}`); + if (info.kind === 'method' && !called) block(`static-as-value: ${member}`); + if (info.kind === 'method' && info.overloaded) block(`overloaded-static: ${member}`); + if (info.kind !== 'method' && called) block(`static-field-call: ${member}`); + classStatics.set(member, info.kind === 'method' ? 'method' : 'getter'); + } else { + classOther = true; + } + } + if (!typePos && isWritten(tm, node)) add('module-write', declName); + const prev = moduleRefs.get(declName); + moduleRefs.set(declName, { value: Boolean(prev && prev.value) || !typePos }); + } + } + } + + const binding = ctx.scope.bindings.get(name); + // A hand-series seam stub calls the `registry-*` import of its own name; a + // method that only shares the name (`ObjectRepository#count` beside the + // hand-series `count`) and never reads that import is not one. + if (binding && binding.kind === 'import' && SEAM_SPECIFIER.test(binding.specifier) && imports.has(name)) add('already-moved', binding.specifier); + for (const p of params) { + const pn = p.getName(); + if (pn === 'host' || /^(read|write)[A-Z]/.test(pn)) add('parameter-shadows', pn); + } + // A body that reads a non-public static takes the route; what the route + // cannot carry keeps it on the class. (A module another mover routes + // applies the same to every mover in it: `plan`.) + if (readsNonPublicStatic) { + for (const tag of routeBlockers) add(tag); + if (classOther) add('static-route-conflict'); + } + return result(); +} + +/** + * The fields of the class a strict compiler requires the constructor to + * assign (TS2564 when no constructor statement does): an instance property + * with a declared type, no initializer, no `?` and no `!`, whose type node is + * not `any`, `unknown` or a union naming `undefined`. Judged on the type + * node's text alone, so a type that admits `undefined` through an alias counts + * as required, which only ever keeps more on the class. + */ +function requiredFieldsOf(tm, cls) { + const { SyntaxKind } = tm; + const out = new Set(); + for (const p of cls.getProperties()) { + if (p.isStatic() || p.hasInitializer() || p.hasQuestionToken() || p.hasExclamationToken()) continue; + const t = p.getTypeNode(); + if (!t) continue; + const k = t.getKind(); + if (k === SyntaxKind.AnyKeyword || k === SyntaxKind.UnknownKeyword) continue; + if (k === SyntaxKind.UnionType && t.getTypeNodes().some((u) => u.getKind() === SyntaxKind.UndefinedKeyword)) continue; + out.add(p.getName()); + } + return out; +} + +/** The marker R8 leaves in the constructor's facet body where it kept a statement on the class. */ +const r8MarkerText = (field, fn) => `// ${MARK} R8: \`this.${field}\` is assigned on the class (\`${fn}\`).`; + +/** The facet member that returns the value R8 keeps a constructor assignment of `field` reading. */ +const r8FnName = (field) => `${CTOR_FACET_MEMBER}${field[0].toUpperCase()}${field.slice(1)}`; + +/** The facet member a moved constructor body becomes. */ +const CTOR_FACET_MEMBER = 'construct'; + +/** Is `name` one a moved constructor's facet member takes (`construct`, or R8's `constructF`)? */ +const isCtorFacetName = (name) => name === CTOR_FACET_MEMBER || new RegExp(`^${CTOR_FACET_MEMBER}[A-Z_$]`).test(name); + +/** + * R8 (#30020), definite-assignment wiring. A strict compiler accepts a field + * the constructor must assign (`requiredFieldsOf`) only when a constructor + * statement assigns it, so moving that statement into a facet is a TS2564 + * (and a TS2565 at every read after it). R8 keeps the assignment on the class + * and moves its right side: each LEADING statement of the body that is + * exactly `this.F = EXPR;`, F required, EXPR reading no `this` and no + * top-level name of `registry.ts` (its parameters, imports and globals only), + * stays as `this.F = Facet.constructF(params);`, and `constructF` returns + * EXPR. The facet body holds a marker line in its place, which the undo turns + * back into the statement. Any other write to a required field (after another + * statement, nested, or with a right side R8 cannot carry) keeps the + * constructor whole: `definite-assignment`. + * + * Returns `{ r8: [{ field, fn, start, end, exprText, imports, params }], reasons }`. + */ +function definiteAssignmentsOf(tm, ctx, ctor) { + const { Node, SyntaxKind } = tm; + const required = requiredFieldsOf(tm, ctx.cls); + const r8 = []; + const reasons = []; + const body = ctor.getBody(); + if (!body || required.size === 0) return { r8, reasons }; + const checker = ctx.sf.getProject().getTypeChecker().compilerObject; + const ts = tm.ts; + const carried = (stmt) => { + if (!Node.isExpressionStatement(stmt)) return null; + const e = stmt.getExpression(); + if (!Node.isBinaryExpression(e) || e.getOperatorToken().getKind() !== SyntaxKind.EqualsToken) return null; + const left = e.getLeft(); + if (!Node.isPropertyAccessExpression(left) || left.getExpression().getKind() !== SyntaxKind.ThisKeyword) return null; + const field = left.getName(); + if (!required.has(field)) return null; + const expr = e.getRight(); + if (stmt.getText() !== `this.${field} = ${expr.getText()};`) return null; + const imports = new Map(); + const params = new Set(); + for (const n of [expr, ...expr.getDescendants()]) { + const k = n.getKind(); + if (k === SyntaxKind.ThisKeyword || k === SyntaxKind.SuperKeyword) return null; + if (k !== SyntaxKind.Identifier) continue; + if (n.getText() === 'arguments') return null; + const symbol = checker.getSymbolAtLocation(n.compilerNode); + for (const decl of symbol?.declarations ?? []) { + if (decl.getSourceFile() !== ctx.sf.compilerNode) continue; + if (ts.isParameter(decl) && decl.parent === ctor.compilerNode) { params.add(n.getText()); continue; } + if (ts.isImportSpecifier(decl) || ts.isImportClause(decl) || ts.isNamespaceImport(decl)) { + const prev = imports.get(n.getText()); + imports.set(n.getText(), { value: Boolean(prev && prev.value) || !inTypePosition(tm, n, ctor) }); + continue; + } + if (isModuleScopeDecl(ts, decl)) return null; + } + } + if (ctx.members.has(r8FnName(field))) return null; + return { field, fn: r8FnName(field), start: stmt.getStart(), end: stmt.getEnd(), exprText: expr.getText(), imports, params }; + }; + const statements = body.getStatements(); + let i = 0; + for (; i < statements.length; i += 1) { + const hit = carried(statements[i]); + if (!hit) break; + r8.push(hit); + } + for (const st of statements.slice(i)) { + for (const pa of [st, ...st.getDescendants()].filter((n) => Node.isPropertyAccessExpression(n))) { + if (pa.getExpression().getKind() !== SyntaxKind.ThisKeyword || thisContainer(tm, pa.getExpression()) !== ctor) continue; + if (required.has(pa.getName()) && isWritten(tm, pa)) { + const tag = `definite-assignment: ${pa.getName()}`; + if (!reasons.includes(tag)) reasons.push(tag); + } + } + } + return { r8, reasons }; +} + +/** + * R3 (#30020): the fields a computed key `this[k]` can name, when `k`'s type + * is a string literal or a union of them and each one names a field of the + * class; null otherwise, and the body stays. + */ +function literalFieldKeys(ctx, checker, key) { + if (!key) return null; + const type = checker.getTypeAtLocation(key.compilerNode); + const parts = type.isUnion() ? type.types : [type]; + const keys = []; + for (const t of parts) { + if (!t.isStringLiteral()) return null; + const info = ctx.members.get(t.value); + if (!info || info.kind !== 'field') return null; + keys.push(t.value); + } + return keys.length > 0 ? [...new Set(keys)].sort() : null; +} + +/** The name of the class's non-public instance member `pa` reads, or null. */ +function foreignNonPublicMember(tm, ctx, checker, pa) { + const ts = tm.ts; + const nameNode = pa.getNameNode(); + if (!tm.Node.isIdentifier(nameNode)) return null; + const symbol = checker.getSymbolAtLocation(nameNode.compilerNode); + for (const decl of symbol?.declarations ?? []) { + const owner = ts.isParameter(decl) && decl.parent && ts.isConstructorDeclaration(decl.parent) ? decl.parent.parent : decl.parent; + if (owner !== ctx.cls.compilerNode) continue; + const flags = ts.getCombinedModifierFlags(decl); + if (flags & ts.ModifierFlags.Static) continue; + if (flags & (ts.ModifierFlags.Private | ts.ModifierFlags.Protected)) return nameNode.getText(); + } + return null; +} + +/** + * R9 (#30020): a method keyed by a symbol binding, `[NAME]` where NAME is a + * binding of `registry.ts`'s module scope spelled SCREAMING_SNAKE, moves into the + * facet member named NAME in camelCase (`[SEED_USER_GRANTS]` -> + * `seedUserGrants`, the name the hand move of `[APP_AUTHORED_HOOK_API]` took). + * Its stub keeps the key; only the facet member is renamed. Null for any other + * name. + */ +function symbolKeyOf(tm, ctx, m) { + const { Node } = tm; + const nameNode = m.getNameNode(); + if (!Node.isComputedPropertyName(nameNode)) return null; + const expr = nameNode.getExpression(); + if (!Node.isIdentifier(expr)) return null; + const key = expr.getText(); + if (!ctx.scope.bindings.has(key) || !/^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*$/.test(key)) return null; + return { key, facetName: key.toLowerCase().replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase()) }; +} + +function isModuleScopeDecl(ts, decl) { + if (ts.isVariableDeclaration(decl)) { + const statement = decl.parent && decl.parent.parent; + return Boolean(statement && ts.isVariableStatement(statement) && ts.isSourceFile(statement.parent)); + } + if (ts.isBindingElement(decl)) { + let n = decl; + while (n && !ts.isVariableDeclaration(n)) n = n.parent; + return Boolean(n && isModuleScopeDecl(ts, n)); + } + return Boolean(decl.parent && ts.isSourceFile(decl.parent)) && + (ts.isFunctionDeclaration(decl) || ts.isClassDeclaration(decl) || ts.isInterfaceDeclaration(decl) || + ts.isTypeAliasDeclaration(decl) || ts.isEnumDeclaration(decl) || ts.isModuleDeclaration(decl)); +} + +// ── The plan ───────────────────────────────────────────────────────────────── + +/** `registry-transactions-section.ts` -> `TransactionsSectionHost`. */ +function hostNameOf(moduleFile) { + const slug = moduleFile.replace(/^registry-/, '').replace(/\.ts$/, ''); + return `${slug.split('-').map((w) => w[0].toUpperCase() + w.slice(1)).join('')}Host`; +} + +/** `registry-transactions-section.ts` -> `TransactionsSectionFacet`: the stateless class the bodies move into. */ +function facetNameOf(moduleFile) { + return hostNameOf(moduleFile).replace(/Host$/, 'Facet'); +} + +/** `registry-transactions-section.ts` -> `transactionsSectionHost`: the module's `FacetHost` in `HOSTS_MODULE`. */ +function hostVarOf(moduleFile) { + const name = hostNameOf(moduleFile); + return `${name[0].toLowerCase()}${name.slice(1)}`; +} + +/** The object the class fills with forwarders to the statics the facets read, and its type. */ +const STATICS_NAME = `${CLASS_NAME}Statics`; +const STATICS_HOST_NAME = `${CLASS_NAME}StaticsHost`; +/** How a routed facet module names the class as a type: `SchemaRegistry` there is `SchemaRegistryStatics`. */ +const CLASS_TYPE_ALIAS = `${CLASS_NAME}Class`; + +/** The placeholder above a moved public or protected body: its docblock stays on the class. */ +const placeholderDoc = (name, className = CLASS_NAME) => `/** The body of \`${className}#${name}\`; the method's docblock there is its contract. */`; + +/** + * The statics block `registry.ts` carries for the routed facets: the type and + * the object ahead of the class, and the static block at the end of the class + * that fills the object. `kinds` maps each routed static to `method` (a + * forwarder that calls it on the class) or `getter` (read when read). Pure: + * `verify` rebuilds both texts from the base class and compares them. + */ +function staticsTexts(kinds) { + const members = [...kinds.keys()].sort(); + const ahead = [ + `// ${MARK} The statics of \`${CLASS_NAME}\` its facet modules read, private ones included, as`, + `// the class declares them (an indexed access on the class). The static block at the end of`, + `// the class fills \`${STATICS_NAME}\` with forwarders: a method is called on the class at`, + '// call time, a field or accessor is read when the body reads it. A facet module imports', + `// \`${STATICS_NAME}\` AS \`${CLASS_NAME}\`, so each moved body reads \`${CLASS_NAME}.x\` unedited;`, + '// `index.ts` re-exports neither, so neither reaches the package.', + `export interface ${STATICS_HOST_NAME} {`, + ...members.map((n) => ` readonly ${n}: (typeof ${CLASS_NAME})['${n}'];`), + '}', + `export const ${STATICS_NAME} = {} as ${STATICS_HOST_NAME};`, + ].join('\n'); + const block = [ + ` // ${MARK} Fills \`${STATICS_NAME}\` (declared above the class) for the facet modules.`, + ' static {', + ` Object.defineProperties(${STATICS_NAME}, {`, + ...members.map((n) => (kinds.get(n) === 'method' + ? ` ${n}: { value: (...args: Parameters) => ${CLASS_NAME}.${n}(...args) },` + : ` ${n}: { get: () => ${CLASS_NAME}.${n} },`)), + ' });', + ' }', + ].join('\n'); + return { ahead, block }; +} + +/** + * The facet hosts (#30051): `HOSTS_MODULE`'s text, and the static block at the + * end of the class that installs one host factory per facet into it. `facets` + * lists each facet module whose bodies read the class, with `members` (member + * -> `method` | `getter`, the union over its bodies) and `written` (the fields + * a body assigns). The host is the module's whole `
Host`, so every + * stub of the facet hands its body the same object, which carries every + * member any body of the facet reads (a `Pick` of it is what each body's + * `this` names). Each member forwards as the per-call literal did: a method + * as a closure that calls it on the registry at call time, a field or accessor + * as a getter read when the body reads it, a written field with a setter that + * assigns it on the registry. So a spy or subclass override installed after the + * host was built is still the one reached, and the only thing a body can tell + * apart is that two calls see the same host object, which no moved body can + * observe: a body that reads `this` bare stays on the class (`bare-this`). + * Pure: `verify` rebuilds both texts from the head's host interfaces and the + * base class, and compares them. + */ +function hostsTexts(facets) { + const sorted = [...facets].sort((a, b) => (a.moduleFile < b.moduleFile ? -1 : a.moduleFile > b.moduleFile ? 1 : 0)); + const module = [ + '// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.', + '', + '/**', + ` * ${MARK} The host of each facet of \`${CLASS_NAME}\`, built ONCE per registry (#30051):`, + ` * what every stub of a facet hands that facet's bodies as \`this\`, in place of`, + ' * a host literal built on every call. Each `FacetHost` below is filled by the', + ` * factory \`${CLASS_NAME}\`'s static block installs, the one place that can read`, + ' * the class\'s private members; the host is built on the first call that needs', + ' * it and kept for the registry\'s life. A method goes as a closure that calls it', + ' * on the registry at call time, a field or accessor as a getter (and a setter', + ' * when a body assigns it), so a body reads and writes the registry exactly as', + ' * the per-call literal let it.', + ' *', + ` * Written by \`${SCRIPT}\`; it imports types only, so it forms no`, + ' * evaluation cycle and is evaluated before the class whichever module loads', + ' * first. `index.ts` re-exports nothing from this module.', + ' */', + '', + `import type { ${CLASS_NAME} } from './registry.js';`, + ...sorted.map((f) => `import type { ${hostNameOf(f.moduleFile)} } from './${f.moduleFile.replace(/\.ts$/, '.js')}';`), + '', + `/** One facet's host for each registry: built on first use, kept for the registry's life. */`, + `export class ${HOSTS_CLASS} {`, + ` private readonly hosts = new WeakMap<${CLASS_NAME}, H>();`, + ` private build: ((registry: ${CLASS_NAME}) => H) | undefined;`, + '', + ' constructor(private readonly facet: string) {}', + '', + ` /** Called once, from \`${CLASS_NAME}\`'s static block. */`, + ` install(build: (registry: ${CLASS_NAME}) => H): void {`, + ' this.build = build;', + ' }', + '', + ' /** The host of `registry`, built on first use. */', + ` of(registry: ${CLASS_NAME}): H {`, + ' let host = this.hosts.get(registry);', + ' if (host === undefined) {', + ' if (this.build === undefined) {', + ` throw new Error(\`\${this.facet}: ${CLASS_NAME} has not installed its host factory\`);`, + ' }', + ' host = this.build(registry);', + ' this.hosts.set(registry, host);', + ' }', + ' return host;', + ' }', + '}', + '', + ...sorted.map((f) => `export const ${hostVarOf(f.moduleFile)} = new ${HOSTS_CLASS}<${hostNameOf(f.moduleFile)}>('${facetNameOf(f.moduleFile)}');`), + '', + ].join('\n'); + const block = [ + ` // ${MARK} Installs each facet's host factory (\`${HOSTS_MODULE}\`): a static block adds`, + ' // no member to the class, so the published `.d.ts` does not move.', + ' static {', + ...sorted.flatMap((f) => [ + ` ${hostVarOf(f.moduleFile)}.install((registry) => ({`, + ...hostInstallLines(f.members, f.written), + ' }));', + ]), + ' }', + ].join('\n'); + return { module, block }; +} + +/** The member lines of one facet's host factory, in its install call: sorted, a getter's setter after it. */ +function hostInstallLines(members, written) { + return [...members.keys()].sort().flatMap((member) => { + if (members.get(member) === 'facade') return [FACADE_INSTALL_LINE]; + if (members.get(member) === 'getter') { + return [` get ${member}() { return registry.${member}; },`, + ...(written.has(member) ? [` set ${member}(v) { registry.${member} = v; },`] : [])]; + } + if (BOUND_HOST_METHODS.has(member)) return [` get ${member}() { return registry.${member}.bind(registry); },`]; + return [` ${member}: (...a) => registry.${member}(...a),`]; + }); +} + +/** Is `node` inside a JSDoc comment? A `{@link X}` there is prose, not a use. */ +function inJsDoc(tm, node) { + for (let n = node.compilerNode; n; n = n.parent) { + if (n.kind >= tm.SyntaxKind.FirstJSDocNode && n.kind <= tm.SyntaxKind.LastJSDocNode) return true; + } + return false; +} + +/** The statement of `sf` (a direct child of the source file) a declaration belongs to. */ +function topStatementOf(ts, decl) { + let n = decl; + while (n.parent && !ts.isSourceFile(n.parent)) n = n.parent; + return n.parent ? n : null; +} + +/** The names a top-level statement declares, or null when one is not a plain identifier. */ +function declaredNames(tm, st) { + const { Node } = tm; + if (Node.isVariableStatement(st)) { + const names = []; + for (const d of st.getDeclarations()) { + if (!Node.isIdentifier(d.getNameNode())) return null; + names.push(d.getName()); + } + return names; + } + if (Node.isFunctionDeclaration(st) || Node.isInterfaceDeclaration(st) || Node.isTypeAliasDeclaration(st)) { + return st.getName() ? [st.getName()] : null; + } + return null; +} + +/** + * Lever (b): read every top-level statement between the last import and the + * class, and decide which move whole into the leaf. A mover never names the + * class, an `registry-*` module's import, or a statement that stays — so the + * leaf it lands in never imports `registry.ts` back. Pure. + */ +function planTopLevel(tm, ctx) { + const { Node, SyntaxKind } = tm; + const ts = tm.ts; + const stmts = ctx.sf.getStatements(); + const clsIndex = stmts.indexOf(ctx.cls); + let lastImport = -1; + stmts.forEach((st, i) => { if (Node.isImportDeclaration(st)) lastImport = i; }); + const checker = ctx.sf.getProject().getTypeChecker().compilerObject; + const nameCount = new Map(); + for (const st of stmts) { + for (const n of declaredNames(tm, st) ?? []) nameCount.set(n, (nameCount.get(n) ?? 0) + 1); + } + const records = []; + const byStatement = new Map(); + for (let i = lastImport + 1; i < clsIndex; i += 1) { + const st = stmts[i]; + const names = declaredNames(tm, st); + const record = { + node: st, names: names ?? [], label: names ? names.join(', ') : `(${st.getKindName()})`, + exported: Node.isExportable(st) && st.isExported(), reasons: [], + deps: new Set(), imports: new Map(), + startLine: st.getStartLineNumber(true), lines: st.getEndLineNumber() - st.getStartLineNumber(true) + 1, + typeOnly: Node.isInterfaceDeclaration(st) || Node.isTypeAliasDeclaration(st), + }; + const add = (code, detail) => { + const tag = detail ? `${code}: ${detail}` : code; + if (!record.reasons.includes(tag)) record.reasons.push(tag); + }; + if (!names || (Node.isVariableStatement(st) && st.hasDeclareKeyword()) || + (Node.isFunctionDeclaration(st) && (!st.getBody() || st.hasDeclareKeyword()))) add('top-kind'); + for (const n of record.names) if ((nameCount.get(n) ?? 0) > 1) add('merged-declaration', n); + for (const n of record.names) if (Object.prototype.hasOwnProperty.call(ctx.pins, n)) add('path-reader', n); + const fullText = st.getFullText(); + for (const needle of Object.keys(ctx.needles)) if (fullText.includes(needle)) add('path-reader', JSON.stringify(needle)); + for (const node of st.getDescendantsOfKind(SyntaxKind.Identifier)) { + if (inJsDoc(tm, node)) continue; + const parent = node.getParent(); + const symbol = Node.isShorthandPropertyAssignment(parent) && parent.getNameNode() === node + ? checker.getShorthandAssignmentValueSymbol(parent.compilerNode) + : checker.getSymbolAtLocation(node.compilerNode); + if (!symbol || !symbol.declarations) continue; + for (const decl of symbol.declarations) { + if (decl.getSourceFile() !== ctx.sf.compilerNode) continue; + const typePos = inTypePosition(tm, node, st); + if (ts.isImportSpecifier(decl) || ts.isImportClause(decl) || ts.isNamespaceImport(decl)) { + const local = node.getText(); + const binding = ctx.scope.bindings.get(local); + if (binding && (SEAM_SPECIFIER.test(binding.specifier) || binding.specifier === './registry.js')) add('seam-import', local); + const prev = record.imports.get(local); + record.imports.set(local, { value: Boolean(prev && prev.value) || !typePos }); + continue; + } + if (!isModuleScopeDecl(ts, decl)) continue; + const owner = topStatementOf(ts, decl); + if (!owner || owner === st.compilerNode) continue; + if (owner === ctx.cls.compilerNode) { add('class-reference'); continue; } + record.deps.add(owner); + } + } + records.push(record); + byStatement.set(st.compilerNode, record); + } + // A mover may not name a statement that stays: close over the stayers until stable. + for (let changed = true; changed;) { + changed = false; + for (const r of records) { + if (r.reasons.length > 0) continue; + for (const owner of r.deps) { + const dep = byStatement.get(owner); + if (!dep || dep.reasons.length > 0) { + const depName = dep ? dep.label : (declaredNames(tm, ctx.sf.getStatements().find((s) => s.compilerNode === owner)) ?? ['?']).join(', '); + r.reasons.push(`needs-stayer: ${depName}`); + changed = true; + break; + } + } + } + } + const movers = records.filter((r) => r.reasons.length === 0); + const moverNames = new Map(); + for (const r of movers) for (const n of r.names) moverNames.set(n, r); + return { records, movers, moverNames }; +} + +/** + * Which moved top-level names the text that STAYS in `registry.ts` still + * reads, value or type: everything outside the moved statements and the moved + * method bodies, JSDoc excepted. Those are what `registry.ts` imports back + * from the leaf. + */ +function hostUsesOfMovers(tm, ctx, top, methodRecords) { + const { SyntaxKind } = tm; + const ts = tm.ts; + const checker = ctx.sf.getProject().getTypeChecker().compilerObject; + const skip = []; + for (const r of top.movers) skip.push([r.node.getFullStart(), r.node.getEnd()]); + for (const r of methodRecords) if (r.reasons.length === 0) skip.push([r.node.getBody().getStart(), r.node.getBody().getEnd()]); + const uses = new Map(); + for (const node of ctx.sf.getDescendantsOfKind(SyntaxKind.Identifier)) { + const at = node.getStart(); + if (skip.some(([a, b]) => at >= a && at < b)) continue; + if (inJsDoc(tm, node)) continue; + const name = node.getText(); + if (!top.moverNames.has(name)) continue; + const symbol = checker.getSymbolAtLocation(node.compilerNode); + if (!symbol || !symbol.declarations) continue; + const mover = top.moverNames.get(name); + if (!symbol.declarations.some((d) => topStatementOf(ts, d) === mover.node.compilerNode)) continue; + const typePos = inTypePosition(tm, node, ctx.sf); + const prev = uses.get(name); + uses.set(name, { value: Boolean(prev && prev.value) || !typePos }); + } + return uses; +} + +/** + * The class's statics: scope, and what a forwarder in `SchemaRegistryStatics` would + * be (a method forwards as a function; a field or accessor as a getter). + */ +function classStaticsOf(tm, cls) { + const { Node } = tm; + const statics = new Map(); + for (const m of cls.getMembers()) { + if (typeof m.isStatic === 'function' && m.isStatic() && typeof m.getName === 'function') { + const prev = statics.get(m.getName()); + const kind = Node.isMethodDeclaration(m) ? 'method' : Node.isSetAccessorDeclaration(m) ? (prev ? prev.kind : 'setter') + : Node.isGetAccessorDeclaration(m) ? 'getter' : 'field'; + statics.set(m.getName(), { + scope: typeof m.getScope === 'function' ? m.getScope() : 'public', kind, + overloaded: Node.isMethodDeclaration(m) && m.getOverloads().length > 0, + }); + } + } + return statics; +} + +/** + * Read `text` (an `registry.ts`) and decide, per method, where its body goes, + * and per top-level declaration ahead of the class, whether it moves to the + * leaf. Pure: the same text gives the same plan. + */ +function plan(tm, text, { + anchors = SECTION_ANCHORS, pins = PATH_READER_PINS, needles = PATH_READER_NEEDLES, classNeedles = CLASS_PATH_READER_NEEDLES, +} = {}) { + const sf = parse(tm, HOST_PATH, text); + const cls = sf.getClass(CLASS_NAME); + if (!cls) refuse(`no class ${CLASS_NAME} in ${HOST_BASENAME}`); + const sectionOf = readSections(cls, anchors); + const ctx = { text, sf, cls, members: instanceMembers(tm, cls), scope: moduleScope(tm, sf), statics: classStaticsOf(tm, cls), pins, needles, classNeedles }; + for (const n of [STATICS_NAME, STATICS_HOST_NAME, ...SECTION_ANCHORS.filter((row) => row[2]).map((row) => facetNameOf(row[2]))]) { + if (ctx.scope.bindings.has(n)) refuse(`${HOST_BASENAME} already binds ${n}, a name this script generates`); + } + const methodNames = new Set(cls.getMethods().map((m) => m.getName())); + const stalePins = Object.keys(pins).filter((n) => !methodNames.has(n) && !ctx.scope.bindings.has(n)); + if (stalePins.length > 0) refuse(`PATH_READER_PINS names what ${HOST_BASENAME} no longer declares: ${stalePins.join(', ')}`); + const staleNeedles = Object.keys(needles).filter((n) => !text.includes(n)); + if (staleNeedles.length > 0) refuse(`PATH_READER_NEEDLES names text ${HOST_BASENAME} no longer carries: ${staleNeedles.map((n) => JSON.stringify(n)).join(', ')}`); + const staleClassNeedles = Object.keys(classNeedles).filter((n) => !text.includes(n)); + if (staleClassNeedles.length > 0) refuse(`CLASS_PATH_READER_NEEDLES names text ${HOST_BASENAME} no longer carries: ${staleClassNeedles.map((n) => JSON.stringify(n)).join(', ')}`); + + const top = planTopLevel(tm, ctx); + + const records = []; + for (const m of cls.getMethods()) { + const startLine = m.getStartLineNumber(true); + const section = sectionOf(m); + const read = readMethod(tm, ctx, m); + const name = m.getName(); + if (!section) read.reasons.unshift('no-section'); + else if (section[2] === null) read.reasons.unshift('section-kept'); + const lineCount = m.getEndLineNumber() - startLine + 1; + const body = m.getBody(); + const bodyLines = body ? body.getEndLineNumber() - body.getStartLineNumber() + 1 : 0; + // Docblock ownership (ruling 6624345766 item 1): a PRIVATE method's + // docblock moves with its body; a public or protected one stays on the + // facade, where the published surface prints it. + const docStart = m.getStart(true); + const doc = m.getScope() === 'private' && docStart < m.getStart() ? text.slice(docStart, m.getStart()) : null; + const record = { + name, node: m, section: section ? section[1] : null, module: section ? section[2] : null, + startLine, lineCount, bodyLines, doc, docLines: doc ? doc.split('\n').length - 1 : 0, ...read, + }; + if (section && section[2]) { + for (const p of m.getParameters()) if (p.getName() === facetNameOf(section[2])) read.reasons.push(`parameter-shadows: ${p.getName()}`); + } + if (record.reasons.length === 0) record.stubLines = stubBody(record, record.module, ' ').split('\n').length; + records.push(record); + } + + // The private-static-through-host rule, per facet module: once one mover + // reads the statics through `SchemaRegistryStatics` (imported AS `SchemaRegistry`), + // every mover in that module does, so what the route cannot carry keeps a + // mover on the class. A router itself never carries a blocker (readMethod + // already kept it), so the set of routed modules is stable. + for (let changed = true; changed;) { + changed = false; + for (const movers of groupMovers(records).values()) { + if (!movers.some((r) => r.readsNonPublicStatic)) continue; + for (const r of movers) { + const extra = [...r.routeBlockers, ...(r.classOther ? ['static-route-conflict'] : [])].filter((t) => !r.reasons.includes(t)); + if (extra.length > 0) { r.reasons.push(...extra); changed = true; } + } + } + } + const modules = groupMovers(records); + const routedModules = new Set(); + const routedStatics = new Map(); + for (const [moduleFile, movers] of modules) { + if (!movers.some((r) => r.readsNonPublicStatic)) continue; + routedModules.add(moduleFile); + for (const r of movers) for (const [member, kind] of r.classStatics) routedStatics.set(member, kind); + } + for (const [moduleFile, movers] of modules) { + const facet = facetNameOf(moduleFile); + for (const r of movers) { + for (const local of [...r.imports.keys(), ...r.moduleRefs.keys()]) { + if (local === facet || local === hostNameOf(moduleFile) || local === CLASS_TYPE_ALIAS) { + refuse(`${moduleFile}: ${r.name} reads ${local}, a name this script generates in that module`); + } + } + } + } + const hostUses = hostUsesOfMovers(tm, ctx, top, records); + + // What each side imports across the cut: the section modules read moved + // names from the leaf and staying names from registry.ts (lever (a)); + // registry.ts reads moved names from the leaf. + const sectionLeafNames = new Set(); + const hostExportsForSections = new Map(); // staying name -> { value } + for (const movers of modules.values()) { + for (const r of movers) { + for (const [n, use] of r.moduleRefs) { + if (top.moverNames.has(n)) { sectionLeafNames.add(n); continue; } + if (n === CLASS_NAME || ctx.scope.exported.has(n)) continue; + const prev = hostExportsForSections.get(n); + hostExportsForSections.set(n, { value: Boolean(prev && prev.value) || use.value }); + } + } + } + const leafExports = []; + for (const r of top.movers) { + if (r.exported) continue; + for (const n of r.names) if (hostUses.has(n) || sectionLeafNames.has(n)) leafExports.push(n); + } + return { sf, cls, ctx, records, modules, top, hostUses, leafExports, hostExportsForSections, routedModules, routedStatics }; +} + +/** Movers grouped by module, modules in the order their first member appears. */ +function groupMovers(records) { + const byModule = new Map(); + for (const r of records) { + if (r.reasons.length > 0 || !r.module) continue; + if (!byModule.has(r.module)) byModule.set(r.module, []); + byModule.get(r.module).push(r); + } + return byModule; +} + +// ── Generation ─────────────────────────────────────────────────────────────── + + +const byName = ([a], [b]) => (a < b ? -1 : a > b ? 1 : 0); +const leafSpecifier = () => `./${LEAF_MODULE.replace(/\.ts$/, '.js')}`; + +/** + * One `import` / `export` statement over `names`: on one line when it fits in + * 120 columns, else one name per wrapped run, two-space indented. + */ +function namedStatement(head, names, tail) { + const one = `${head} { ${names.join(', ')} }${tail}`; + if (one.length <= 120) return one; + const out = [`${head} {`]; + let line = ' '; + for (const n of names) { + if (line.length + n.length + 2 > 118 && line !== ' ') { out.push(line); line = ' '; } + line += ` ${n},`; + } + out.push(line); + out.push(`}${tail}`); + return out.join('\n'); +} + +/** + * `import { … }` and `import type { … }` from one specifier, values first. + * `styles` keeps a statement a hand edit left on one line (`'line'`) on one + * line, however long; the first pass's own form (`'wrap'`, the default) is + * `namedStatement`'s. + */ +function importPair(specifier, uses, styles = {}) { + const out = []; + const entries = [...uses.entries()].sort(byName); + const values = entries.filter(([, u]) => u.value).map(([n]) => n); + const types = entries.filter(([, u]) => !u.value).map(([n]) => n); + const stmt = (head, names, style) => (style === 'line' + ? `${head} { ${names.join(', ')} } from '${specifier}';` : namedStatement(head, names, ` from '${specifier}';`)); + if (values.length > 0) out.push(stmt('import', values, styles.value)); + if (types.length > 0) out.push(stmt('import type', types, styles.type)); + return out; +} + +/** + * The form each statement of a leaf or `./registry.js` import run is in: + * `'wrap'` when it is `namedStatement`'s, `'line'` when a hand edit grew it on + * one line past the wrap width (#30227 left one so), each with its names + * sorted; null when a statement is in neither form. A move renders the run + * back in the forms it read, and so does the undo, so neither rewraps a line + * the hand wrote. + */ +function runStyles(text, decls, specifier) { + const styles = {}; + if (specifier !== leafSpecifier() && specifier !== './registry.js') return styles; + for (const d of decls) { + const stmt = text.slice(d.start, d.end); + const type = stmt.startsWith('import type '); + const head = type ? 'import type' : 'import'; + const names = [...d.bindings.keys()].sort(); + const tail = ` from '${specifier}';`; + let style; + if (stmt === namedStatement(head, names, tail)) style = 'wrap'; + else if (stmt === `${head} { ${names.join(', ')} }${tail}`) style = 'line'; + else return null; + const key = type ? 'type' : 'value'; + if (styles[key]) return null; + styles[key] = style; + } + return styles; +} + +/** + * The import lines a module needs: registry.ts's own imports it uses, in + * registry.ts's import order; then the moved top-level names, from the leaf; + * then what stayed in registry.ts (lever (a)), including the class. + */ +function importLines(scope, users, { needsHost = false, moduleRefs = new Map(), leafNames = new Map(), routed = false, className = CLASS_NAME } = {}) { + const bySpecifier = new Map(); + for (const r of users) { + for (const [local, use] of r.imports) { + const b = scope.bindings.get(local); + if (!b || b.kind !== 'import') refuse(`import binding ${local} not found`); + if (!bySpecifier.has(b.specifier)) bySpecifier.set(b.specifier, new Map()); + const slot = bySpecifier.get(b.specifier); + const prev = slot.get(local); + slot.set(local, { ...b, value: Boolean(prev && prev.value) || (use.value && !b.typeOnly) }); + } + } + const out = []; + const specifiers = [...bySpecifier.keys()].sort((a, b) => scope.importOrder.get(a) - scope.importOrder.get(b)); + for (const specifier of specifiers) out.push(...specifierImportLines(specifier, bySpecifier.get(specifier))); + const fromLeaf = new Map(); + const fromHost = new Map(); + for (const [n, use] of moduleRefs) (leafNames.has(n) ? fromLeaf : fromHost).set(n, use); + if (routed) { + // The private-static-through-host rule: `SchemaRegistry.x` in a moved body reads + // the forwarder object; the host interface names the class by an alias. + fromHost.delete(CLASS_NAME); + fromHost.set(`${STATICS_NAME} as ${CLASS_NAME}`, { value: true }); + if (needsHost) fromHost.set(`${CLASS_NAME} as ${CLASS_TYPE_ALIAS}`, { value: false }); + } else if (needsHost && !fromHost.has(className)) fromHost.set(className, { value: false }); + out.push(...importPair(leafSpecifier(), fromLeaf)); + out.push(...importPair('./registry.js', fromHost)); + return out; +} + +/** + * The import lines of one package specifier in a facet module, from its + * bindings (local -> { form, imported, value }): the value statement, then a + * type-only default, a namespace, then the type-only names, each list sorted. + */ +function specifierImportLines(specifier, bindings) { + const out = []; + const entries = [...bindings.entries()].sort(byName); + const named = (value) => entries + .filter(([, e]) => e.form === 'named' && e.value === value) + .map(([local, e]) => (e.imported === local ? local : `${e.imported} as ${local}`)); + const def = entries.find(([, e]) => e.form === 'default'); + const ns = entries.find(([, e]) => e.form === 'namespace'); + const valueNamed = named(true); + const defValue = def && def[1].value ? def[0] : null; + if (defValue || valueNamed.length > 0) { + const parts = [defValue, valueNamed.length > 0 ? `{ ${valueNamed.join(', ')} }` : null].filter(Boolean); + out.push(`import ${parts.join(', ')} from '${specifier}';`); + } + if (def && !def[1].value) out.push(`import type ${def[0]} from '${specifier}';`); + if (ns) out.push(`import ${ns[1].value ? '' : 'type '}* as ${ns[0]} from '${specifier}';`); + const typeNamed = named(false); + if (typeNamed.length > 0) out.push(`import type { ${typeNamed.join(', ')} } from '${specifier}';`); + return out; +} + +/** `text` wrapped into ` * ` comment lines no wider than `width`. */ +function wrapComment(words, width = 80) { + const lines = []; + let line = ' *'; + for (const w of words) { + if (line.length + 1 + w.length > width && line !== ' *') { lines.push(line); line = ' *'; } + line += ` ${w}`; + } + if (line !== ' *') lines.push(line); + return lines; +} + +function paramsText(m) { + return m.getParameters().map((p) => p.getText()); +} + +function forwardList(m) { + return m.getParameters().map((p) => (p.isRestParameter() ? `...${p.getName()}` : p.getName())); +} + +/** A facet module header's import paragraph: values imported from `registry.ts` (lever (a)), or types only. */ +const VALUES_CYCLE_PARA = [ + ' * Values that stayed in `registry.ts` are imported from it (lever (a) of', + ' * ruling 6591753301); every use is inside a function body, so the import', + ' * cycle is never read at evaluation time. `index.ts` re-exports nothing from', + ' * this module.', +].join('\n'); +const TYPES_CYCLE_PARA = [ + ' * The module imports `registry.ts` for types only, so the two modules form no', + ' * evaluation cycle. `index.ts` re-exports nothing from this module.', +].join('\n'); + +/** + * A routed facet module's header paragraph on the statics route: what + * `SchemaRegistry` names there. The first pass writes it for a module it routes, and + * the fourth pass's R10 (#30020) for one it reroutes. + */ +const STATICS_PARA = [ + ` * \`${CLASS_NAME}\` here is \`${STATICS_NAME}\` (imported AS \`${CLASS_NAME}\`): forwarders to the`, + ' * statics the bodies read, private ones included, which the class fills in a', + ` * static block; the class as a type is \`${CLASS_TYPE_ALIAS}\`.`, +].join('\n'); + +/** Does a facet module import a VALUE from `./registry.js`? */ +const importsEngineValues = (text) => /^import \{[^}]*\} from '\.\/registry\.js';$/m.test(text); + +/** + * A facet module's header paragraph made to say what its `./registry.js` lines + * import (R4 of #30020: a move that brings the first value import switches + * it, and the undo switches it back). Refuses a header carrying neither form. + */ +function withCyclePara(text, file) { + const values = importsEngineValues(text); + const [from, to] = values ? [TYPES_CYCLE_PARA, VALUES_CYCLE_PARA] : [VALUES_CYCLE_PARA, TYPES_CYCLE_PARA]; + if (text.includes(to)) return text; + const at = text.indexOf(from); + if (at < 0 || text.indexOf(from, at + 1) >= 0) refuse(`${file}: its header carries no single import paragraph as the first pass writes one`); + return text.slice(0, at) + to + text.slice(at + from.length); +} + +function moduleText(moduleFile, title, movers, scope, leafNames, routed = false) { + const hostName = hostNameOf(moduleFile); + const facet = facetNameOf(moduleFile); + const classType = routed ? CLASS_TYPE_ALIAS : CLASS_NAME; + const hostMembers = new Map(); + const written = new Set(); + for (const r of movers) for (const [member, kind] of r.host) hostMembers.set(member, kind); + for (const r of movers) for (const member of r.writes) written.add(member); + const needsHost = hostMembers.size > 0; + const moduleRefs = new Map(); + for (const r of movers) { + for (const [n, use] of r.moduleRefs) { + const prev = moduleRefs.get(n); + moduleRefs.set(n, { value: Boolean(prev && prev.value) || use.value }); + } + } + const readsHostValues = [...moduleRefs].some(([n, use]) => use.value && !leafNames.has(n)); + const out = []; + out.push('// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.'); + out.push(''); + out.push('/**'); + out.push(...moduleHeaderLines(title, movers.map((r) => r.name))); + out.push(' *'); + out.push(` * A MOVE, not a redesign. {@link ${facet}} is a stateless facet of \`SchemaRegistry\``); + out.push(' * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry`'); + out.push(' * method of the same name exactly as it stood: not re-indented, not'); + out.push(' * formatted, not edited. The method stays on the class as a one-call stub with'); + out.push(' * its signature, so the published surface, an instance spy and a subclass'); + out.push(' * override are what they were. A public or protected method keeps its'); + out.push(' * docblock on the class, the contract; a private method\'s docblock moved here'); + out.push(' * with its body. The state stays on `SchemaRegistry`.'); + out.push(' *'); + if (needsHost) { + out.push(...wrapComment((`The bodies reach the class through {@link ${hostName}}: exactly the \`SchemaRegistry\` ` + + 'members they read, private ones included. Every stub hands its body, by a `.call`, the same host: ' + + `\`${hostVarOf(moduleFile)}\` in \`${HOSTS_MODULE}\`, built once per registry by the class's static block. ` + + 'Methods go as closures that call them on the registry at call time, so a subclass override or an ' + + 'instance spy is still the one reached; fields and accessors go as GETTERS, read when the body reads ' + + 'them, as they were on the class, and a field a body assigns goes with a SETTER that assigns it on ' + + 'the registry.').split(' '))); + } else { + out.push(' * No body reads an `SchemaRegistry` member, so each stub calls its body directly.'); + } + if (routed) { + out.push(' *'); + out.push(STATICS_PARA); + } + out.push(' *'); + out.push(readsHostValues ? VALUES_CYCLE_PARA : TYPES_CYCLE_PARA); + out.push(' */'); + out.push(''); + out.push(...importLines(scope, movers, { needsHost, moduleRefs, leafNames, routed })); + if (needsHost) { + out.push(''); + out.push('/**'); + out.push(` * The \`SchemaRegistry\` members the bodies of {@link ${facet}} read — every one of`); + out.push(' * them, and no other. Each is typed AS the class member (an indexed access on'); + out.push(' * the class), so a change to a member\'s signature on the class is a change'); + out.push(' * here too. `readonly` members are the getters; the others are fields a body'); + out.push(' * assigns.'); + out.push(' */'); + out.push(`export interface ${hostName} {`); + for (const member of [...hostMembers.keys()].sort()) { + // R1's identity port: only a fourth-pass mover reads it (R11 starts its facet with this text). + if (hostMembers.get(member) === 'facade') { out.push(facadeIfaceLine(classType)); continue; } + const ro = hostMembers.get(member) === 'getter' && !written.has(member); + out.push(` ${ro ? 'readonly ' : ''}${member}: ${classType}['${member}'];`); + } + out.push('}'); + } + out.push(''); + out.push(`/** The bodies of the "${title}" section of \`SchemaRegistry\`: a stateless facet, called by the class's stubs. */`); + out.push(`export class ${facet} {`); + movers.forEach((r, i) => { + if (i > 0) out.push(''); + out.push(facetMethodText(r, hostName)); + }); + out.push('}'); + out.push(''); + return out.join('\n'); +} + +/** The opening lines of a facet module's header docblock: its section's title and the names of its bodies. */ +/** + * A facet module's header sentence, wrapped. `tail` is the words a hand edit + * wrote after that sentence in the same paragraph (the fourth pass reads them + * back with `headerNamesOf` and carries them over unedited); the first pass + * writes none. + */ +function moduleHeaderLines(title, names, tail = []) { + const listed = names.map((n, i) => `\`${n}\`${i === names.length - 1 ? '' : ','}`); + return wrapComment([MARK, 'The', `"${title}"`, 'section', 'of', '`SchemaRegistry`:', 'the', 'bodies', 'of', + ...listed, ...HEADER_SENTENCE_END.trim().split(' '), ...tail]); +} + +/** The words that close a facet module's header sentence, after its list of names. */ +const HEADER_SENTENCE_END = ` moved out of \`registry.ts\` whole by \`${SCRIPT}\`.`; + +/** + * One facet member, as its module holds it: the method's docblock (a private + * one) or the placeholder, `static`, and the method's own parameters, return + * type and body text, with `this` a `Pick` of the host when the body reads it. + */ +function facetMethodText(r, hostName, className = CLASS_NAME) { + const m = r.node; + const picks = [...r.host.keys()].sort().map((k) => `'${k}'`).join(' | '); + const params = [...(r.host.size > 0 ? [`this: Pick<${hostName}, ${picks}>`] : []), ...paramsText(m)]; + const ret = m.getReturnTypeNode() ? `: ${m.getReturnTypeNode().getText()}` : ''; + const typeParams = m.getTypeParameters().length > 0 ? `<${m.getTypeParameters().map((t) => t.getText()).join(', ')}>` : ''; + return ` ${r.doc ?? `${placeholderDoc(r.name, className)}\n `}static ${m.isAsync() ? 'async ' : ''}${r.facetName ?? r.name}${typeParams}(${params.join(', ')})${ret} ${identityBodyText(m.getBody(), r.identity)}`; +} + +/** + * A method's body text with R1 applied (#30020): each bare `this` at the + * starts `identity` lists reads `this.facade`. No other byte changes, so the + * undo (`identityUndoText`) is the one inverse rewrite. + */ +function identityBodyText(body, identity = []) { + const start = body.getStart(); + let text = body.getText(); + for (const at of [...identity].sort((a, b) => b - a)) { + if (text.slice(at - start, at - start + 4) !== 'this') refuse(`identity port: no \`this\` at offset ${at}`); + text = `${text.slice(0, at - start)}this.${FACADE_MEMBER}${text.slice(at - start + 4)}`; + } + return text; +} + +/** + * R1's inverse, read off a facet member at head: its body text with each + * `this.facade` its own `this` reads back to a bare `this`. + */ +function identityUndoText(tm, fm) { + const { Node, SyntaxKind } = tm; + const body = fm.getBody(); + const start = body.getStart(); + let text = body.getText(); + const sites = body.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression).filter((pa) => + pa.getExpression().getKind() === SyntaxKind.ThisKeyword && Node.isIdentifier(pa.getNameNode()) && + pa.getName() === FACADE_MEMBER && thisContainer(tm, pa.getExpression()) === fm); + for (const pa of sites.sort((a, b) => b.getStart() - a.getStart())) { + text = `${text.slice(0, pa.getStart() - start)}this${text.slice(pa.getEnd() - start)}`; + } + return text; +} + +/** + * The leaf: the moved top-level statements, each its base text exactly (its + * leading comments and blank lines included), in base order, after the + * imports they use; then one `export { … }` of the moved names base did not + * export and something outside the leaf now imports. + */ +function leafText(scope, top, leafExports) { + // The header is `//` lines, not a `/** */` docblock: the bundled `.d.ts` + // keeps a JSDoc block that leads a module's first statement, and a leaf + // bundled first would publish text the base did not carry (the pilot's + // measurement: 14 added lines in dist/index.d.ts). + const out = []; + out.push('// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.'); + out.push(''); + out.push(...wrapComment([MARK, 'The', 'top-level', 'declarations', 'ahead', 'of', '`SchemaRegistry`', 'in', '`registry.ts`', + 'that', 'moved', 'out', 'whole', 'by', `\`${SCRIPT}\``, '(lever', '(b)', 'of', 'ruling', + '6591753301).']).map((l) => l.replace(/^ \*/, '//'))); + out.push('//'); + out.push('// A MOVE, not a redesign: each declaration below is its `registry.ts` text'); + out.push('// exactly, with the comments above it, in the order it stood there. A LEAF:'); + out.push('// it imports neither `registry.ts` nor any `registry-*` module, so it is'); + out.push('// evaluated before the class and forms no cycle with it. `registry.ts`'); + out.push('// re-exports, by name, every declaration here it exported before, so every'); + out.push('// importer of `./registry.js` reads what it read.'); + out.push(''); + out.push(...importLines(scope, top.movers)); + let text = out.join('\n'); + for (const r of top.movers) text += r.node.getFullText(); + if (leafExports.length > 0) { + text += `\n\n// ${MARK} Declared above without \`export\`, and read by \`registry.ts\` or a section module.\n`; + text += `${namedStatement('export', [...leafExports].sort(), ';')}`; + } + return `${text}\n`; +} + +const DOC_MOVED = 'its docblock with it'; + +/** + * A stub's body: the marker line, then ONE call of the facet member of the + * same name. A body that reads the class is called with its facet's host, the + * one object per registry `HOSTS_MODULE` keeps (#30051), not a literal built per + * call. `node` is the method (base or head: the stub keeps its signature). + * Pure: `verify` rebuilds every stub with it and compares. + */ +function stubBody({ node, name, usesHost, docMoved }, moduleFile, closeIndent, mark = MARK) { + const ind = `${closeIndent} `; + const hostName = hostNameOf(moduleFile); + // A generic method calls its body INSTANTIATED with its own type + // parameters (`Function#call` alone would erase them). + const typeArgs = node.getTypeParameters().map((t) => t.getName()); + const fn = typeArgs.length > 0 ? `(${facetNameOf(moduleFile)}.${name}<${typeArgs.join(', ')}>)` : `${facetNameOf(moduleFile)}.${name}`; + const out = ['{']; + const args = forwardList(node).join(', '); + const doc = docMoved ? `, ${DOC_MOVED}` : ''; + if (!usesHost) { + out.push(`${ind}// ${mark} The body moved to \`${moduleFile}\`${doc}.`); + out.push(`${ind}return ${fn}(${args});`); + } else { + out.push(`${ind}// ${mark} The body moved to \`${moduleFile}\`${doc} (see \`${hostName}\`).`); + out.push(`${ind}return ${fn}.call(${hostVarOf(moduleFile)}.of(this)${args ? `, ${args}` : ''});`); + } + out.push(`${closeIndent}}`); + return out.join('\n'); +} + +/** The constructor's forwarded arguments: the parameters a moved part reads, in declaration order. */ +const ctorArgs = (node, read) => forwardList(node).filter((_, i) => read.has(node.getParameters()[i].getName())); + +/** + * A moved constructor's body (#30020 R8): the marker line, each statement R8 + * keeps (`this.F = Facet.constructF(…)`), then ONE call of the facet's + * `construct`. Gate wiring only: `this.x = ` and a + * forwarding call. Pure: `verify` rebuilds it and compares. + */ +function ctorStubBody({ node, usesHost, r8, paramsRead }, moduleFile, closeIndent, mark = MARK) { + const ind = `${closeIndent} `; + const facet = facetNameOf(moduleFile); + const out = ['{']; + out.push(usesHost + ? `${ind}// ${mark} The body moved to \`${moduleFile}\` (see \`${hostNameOf(moduleFile)}\`).` + : `${ind}// ${mark} The body moved to \`${moduleFile}\`.`); + for (const a of r8) out.push(`${ind}this.${a.field} = ${facet}.${a.fn}(${ctorArgs(node, a.params).join(', ')});`); + const args = ctorArgs(node, paramsRead).join(', '); + out.push(usesHost + ? `${ind}${facet}.${CTOR_FACET_MEMBER}.call(${hostVarOf(moduleFile)}.of(this)${args ? `, ${args}` : ''});` + : `${ind}${facet}.${CTOR_FACET_MEMBER}(${args});`); + out.push(`${closeIndent}}`); + return out.join('\n'); +} + +/** R8's facet member: the value the constructor statement R8 keeps on the class assigns. */ +function r8FnText(node, a, classType) { + const params = node.getParameters().filter((p) => a.params.has(p.getName())).map((p) => p.getText()); + return ` /** R8 (#30020): the value \`${CLASS_NAME}\`'s constructor assigns to \`${a.field}\`, a field only a constructor may assign. */\n` + + ` static ${a.fn}(${params.join(', ')}): ${classType}['${a.field}'] {\n return ${a.exprText};\n }`; +} + +/** + * The constructor's facet member: its parameters the body reads, `this` a + * `Pick` of the host when it reads the class, and its body with R1 applied and + * each statement R8 kept on the class replaced by a marker line. + */ +function ctorFacetText(r, hostName) { + const m = r.node; + const picks = [...r.host.keys()].sort().map((k) => `'${k}'`).join(' | '); + const params = [...(r.host.size > 0 ? [`this: Pick<${hostName}, ${picks}>`] : []), + ...m.getParameters().filter((p) => r.paramsRead.has(p.getName())).map((p) => p.getText())]; + const body = m.getBody(); + const start = body.getStart(); + const text = body.getText(); + const edits = []; + for (const at of r.identity) { + if (text.slice(at - start, at - start + 4) !== 'this') refuse(`identity port: no \`this\` at offset ${at}`); + edits.push({ start: at - start, end: at - start + 4, text: `this.${FACADE_MEMBER}` }); + } + for (const a of r.r8) edits.push({ start: a.start - start, end: a.end - start, text: r8MarkerText(a.field, a.fn) }); + return ` /** The body of \`${CLASS_NAME}\`'s constructor; the constructor there keeps its signature and docblock. */\n` + + ` static ${CTOR_FACET_MEMBER}(${params.join(', ')}): void ${applyOrderedEdits(text, edits)}`; +} + +/** The facet member texts a moved constructor becomes, in module order: R8's, then `construct`. */ +function ctorFacetTexts(r, hostName, classType) { + return [...r.r8.map((a) => r8FnText(r.node, a, classType)), ctorFacetText(r, hostName)]; +} + +/** The facets whose bodies read the class, as `hostsTexts` takes them, from the movers of each module. */ +function facetsOf(modules) { + const facets = []; + for (const [moduleFile, movers] of modules) { + const members = new Map(); + const written = new Set(); + for (const r of movers) for (const [member, kind] of r.host) members.set(member, kind); + for (const r of movers) for (const member of r.writes) written.add(member); + if (members.size > 0) facets.push({ moduleFile, members, written }); + } + return facets; +} + +/** + * Apply a plan to `text`. Returns `{ host, modules, pruned }`: the new + * `registry.ts`, a Map of module basename -> text (the leaf among them), and the + * import bindings the prune dropped. Pure. + */ +function generate(tm, text, planned) { + const { sf, ctx, modules, top, hostUses, leafExports, hostExportsForSections, routedModules, routedStatics } = planned; + const edits = []; + const importBlock = []; + if (top.movers.length > 0) { + const leafUses = new Map([...hostUses].filter(([n]) => top.moverNames.has(n))); + const lines = importPair(leafSpecifier(), leafUses); + if (lines.length > 0) { + importBlock.push(`\n// ${MARK} The top-level declarations that moved whole to \`${LEAF_MODULE}\`, as this file still reads them.`); + for (const l of lines) importBlock.push(`\n${l}`); + } + const reexports = top.movers.filter((r) => r.exported) + .flatMap((r) => r.names.map((n) => [n, r.typeOnly ? `type ${n}` : n])) + .sort(byName).map(([, spelled]) => spelled); + if (reexports.length > 0) { + importBlock.push(`\n// ${MARK} Re-exported by name, so every importer of \`./registry.js\` reads what it read.`); + importBlock.push(`\n${namedStatement('export', reexports, ` from '${leafSpecifier()}';`)}`); + } + for (const r of top.movers) edits.push({ start: r.node.getFullStart(), end: r.node.getEnd(), text: '' }); + } + for (const [moduleFile, movers] of modules) { + const title = movers[0].section; + const hostName = hostNameOf(moduleFile); + const anyHost = movers.some((r) => r.host.size > 0); + importBlock.push(`\n// ${MARK} ${title}: the methods of the same names below hand the bodies` + + `${anyHost ? ` what they read (\`${hostName}\`)` : ' their arguments'}.`); + importBlock.push(`\nimport { ${facetNameOf(moduleFile)} } from './${moduleFile.replace(/\.ts$/, '.js')}';`); + for (const r of movers) { + const body = r.node.getBody(); + const lineStart = text.lastIndexOf('\n', body.getEnd() - 1) + 1; + const closeIndent = text.slice(lineStart, body.getEnd() - 1); + if (!/^ *$/.test(closeIndent)) refuse(`${r.name}: the body's closing brace does not open its own line`); + if (r.host.size > 0 && r.node.getParameters().some((p) => p.getName() === hostVarOf(moduleFile))) { + refuse(`${r.name}: a parameter is named ${hostVarOf(moduleFile)}, which its stub reads as the facet's host`); + } + const stub = stubBody({ node: r.node, name: r.name, usesHost: r.host.size > 0, docMoved: Boolean(r.doc) }, moduleFile, closeIndent); + edits.push({ start: body.getStart(), end: body.getEnd(), text: stub }); + const asyncKw = r.node.getModifiers().find((x) => x.getKind() === tm.SyntaxKind.AsyncKeyword); + if (asyncKw) edits.push({ start: asyncKw.getStart(), end: r.node.getNameNode().getStart(), text: '' }); + if (r.doc) edits.push({ start: r.node.getStart(true), end: r.node.getStart(), text: '' }); + } + } + // After the class's last member: the static block that fills the statics + // forwarders, then the one that installs the facet hosts (one edit, so the + // two keep this order). + const tail = []; + if (routedStatics.size > 0) { + // The private-static-through-host rule: the forwarder object ahead of the + // class (ahead of its leading comments), the static block that fills it + // after the class's last member. + const { ahead, block } = staticsTexts(routedStatics); + edits.push({ start: ctx.cls.getFullStart(), end: ctx.cls.getFullStart(), text: `\n\n${ahead}` }); + tail.push(`\n\n${block}`); + } + const facets = facetsOf(modules); + const hosts = facets.length > 0 ? hostsTexts(facets) : null; + if (hosts) { + for (const f of facets) { + if (ctx.scope.bindings.has(hostVarOf(f.moduleFile))) refuse(`registry.ts already binds ${hostVarOf(f.moduleFile)}, the name of ${f.moduleFile}'s host`); + } + tail.push(`\n\n${hosts.block}`); + importBlock.push(`\n// ${MARK} The facets' hosts, one per registry, which the static block at the end of the class builds.`); + importBlock.push(`\n${namedStatement('import', facets.map((f) => hostVarOf(f.moduleFile)).sort(), ` from './${HOSTS_MODULE.replace(/\.ts$/, '.js')}';`)}`); + } + if (tail.length > 0) { + const last = ctx.cls.getMembers().at(-1); + edits.push({ start: last.getEnd(), end: last.getEnd(), text: tail.join('') }); + } + if (hostExportsForSections.size > 0) { + const spelled = [...hostExportsForSections.keys()].sort().map((n) => { + const st = ctx.sf.getStatements().find((s) => (declaredNames(tm, s) ?? []).includes(n)); + return st && (tm.Node.isInterfaceDeclaration(st) || tm.Node.isTypeAliasDeclaration(st)) ? `type ${n}` : n; + }); + importBlock.push(`\n// ${MARK} Exported for the section modules above, which import them (lever (a)); \`index.ts\` re-exports by name, so none reaches the package.`); + importBlock.push(`\n${namedStatement('export', spelled, ';')}`); + } + const lastImport = sf.getImportDeclarations().at(-1); + if (!lastImport) refuse('registry.ts has no import declaration to place the generated imports after'); + if (importBlock.length > 0) edits.push({ start: lastImport.getEnd(), end: lastImport.getEnd(), text: importBlock.join('') }); + edits.sort((a, b) => b.start - a.start || b.end - a.end); + let out = text; + let floor = Infinity; + for (const e of edits) { + if (e.end > floor) refuse('overlapping edits'); + out = out.slice(0, e.start) + e.text + out.slice(e.end); + floor = e.start; + } + // The imports only moved text read are dropped (the package compiles with + // `noUnusedLocals`); `verify` recomputes the same prune from the head. + const pruned = pruneImports(tm, out, usedImportNames(tm, out)); + const moduleTexts = new Map(); + const leafNames = top.moverNames; + if (top.movers.length > 0) moduleTexts.set(LEAF_MODULE, leafText(ctx.scope, top, leafExports)); + for (const [moduleFile, movers] of modules) { + moduleTexts.set(moduleFile, moduleText(moduleFile, movers[0].section, movers, ctx.scope, leafNames, routedModules.has(moduleFile))); + } + if (hosts) moduleTexts.set(HOSTS_MODULE, hosts.module); + return { host: pruned.text, modules: moduleTexts, pruned: pruned.removed }; +} + +// ── The import prune ───────────────────────────────────────────────────────── + +/** Is `node` (a compiler node) inside a JSDoc comment? */ +function inJsDocNode(ts, node) { + for (let n = node; n; n = n.parent) { + if (n.kind >= ts.SyntaxKind.FirstJSDocNode && n.kind <= ts.SyntaxKind.LastJSDocNode) return true; + } + return false; +} + +/** Is the identifier a property NAME (`a.NAME`, `{ NAME: … }`, a member name), not a reference? */ +function isPropertyNameIdentifier(ts, node) { + const p = node.parent; + if (!p) return false; + if (ts.isPropertyAccessExpression(p) && p.name === node) return true; + if ((ts.isPropertyAssignment(p) || ts.isPropertyDeclaration(p) || ts.isPropertySignature(p) || + ts.isMethodDeclaration(p) || ts.isMethodSignature(p) || ts.isGetAccessorDeclaration(p) || + ts.isSetAccessorDeclaration(p) || ts.isEnumMember(p)) && p.name === node) return true; + if (ts.isQualifiedName(p) && p.right === node) return true; + return false; +} + +/** + * The import bindings of an `registry.ts` that something outside its import + * declarations reads: a reference in code or a type, a local `export { … }`, + * or a JSDoc `{@link X}` / `@see X` name. Those JSDoc names count because the + * compiler's `noUnusedLocals` counts them; other JSDoc text and `//` comments + * do not. + */ +function usedImportNames(tm, text) { + const ts = tm.ts; + const sfm = parse(tm, HOST_PATH, text); + const sf = sfm.compilerNode; + const checker = sfm.getProject().getTypeChecker().compilerObject; + const used = new Set(); + const isImportDecl = (d) => d.getSourceFile() === sf && (ts.isImportSpecifier(d) || ts.isImportClause(d) || ts.isNamespaceImport(d)); + const leftmost = (name) => { + let n = name; + while (n && (ts.isQualifiedName(n) || ts.isPropertyAccessExpression(n) || n.kind === ts.SyntaxKind.JSDocMemberName)) { + n = ts.isQualifiedName(n) || n.kind === ts.SyntaxKind.JSDocMemberName ? n.left : n.expression; + } + return n && ts.isIdentifier(n) ? n : undefined; + }; + const visitDoc = (node) => { + const linkish = node.kind === ts.SyntaxKind.JSDocLink || node.kind === ts.SyntaxKind.JSDocLinkCode || + node.kind === ts.SyntaxKind.JSDocLinkPlain || node.kind === ts.SyntaxKind.JSDocNameReference; + if (linkish && node.name) { + const id = leftmost(node.name); + const symbol = id && checker.getSymbolAtLocation(id); + if (id && (symbol?.declarations ?? []).some(isImportDecl)) used.add(id.text); + } + if (Array.isArray(node.comment)) node.comment.forEach(visitDoc); + ts.forEachChild(node, visitDoc); + }; + const visit = (node) => { + if (ts.isImportDeclaration(node)) return; + for (const doc of node.jsDoc ?? []) visitDoc(doc); + if (ts.isExportDeclaration(node) && node.moduleSpecifier) return; + if (ts.isIdentifier(node) && !inJsDocNode(ts, node)) { + const parent = node.parent; + let symbol; + if (ts.isExportSpecifier(parent)) symbol = checker.getExportSpecifierLocalTargetSymbol(parent); + else if (ts.isShorthandPropertyAssignment(parent) && parent.name === node) symbol = checker.getShorthandAssignmentValueSymbol(parent); + else if (!isPropertyNameIdentifier(ts, node)) symbol = checker.getSymbolAtLocation(node); + if ((symbol?.declarations ?? []).some(isImportDecl)) used.add(node.text); + } + ts.forEachChild(node, visit); + }; + visit(sf); + return used; +} + +/** + * Drop every import binding `used` lacks. A declaration left with no binding + * goes whole (its own text and the line break after it; the comments above it + * stay, unless `dropAttachedComments`: then the `//` lines directly above it, + * with no blank line between, go with it); one left with some keeps them, a + * one-line `{ … }` on one line, a multi-line one as one binding per line. + * Pure. Returns `{ text, removed }`. + */ +function pruneImports(tm, text, used, { dropAttachedComments = false } = {}) { + const ts = tm.ts; + const sf = parseSourceFile(HOST_PATH, text); + const edits = []; + const removed = []; + for (const st of sf.statements) { + if (!ts.isImportDeclaration(st) || !st.importClause) continue; + const clause = st.importClause; + const nb = clause.namedBindings; + const named = nb && ts.isNamedImports(nb) ? [...nb.elements] : []; + const dropDefault = Boolean(clause.name && !used.has(clause.name.text)); + const dropNs = Boolean(nb && ts.isNamespaceImport(nb) && !used.has(nb.name.text)); + const dropNamed = named.filter((e) => !used.has(e.name.text)); + if (!dropDefault && !dropNs && dropNamed.length === 0) continue; + if (dropDefault) removed.push(clause.name.text); + if (dropNs) removed.push(nb.name.text); + for (const e of dropNamed) removed.push(e.name.text); + const keepDefault = Boolean(clause.name && !dropDefault); + const keepNs = Boolean(nb && ts.isNamespaceImport(nb) && !dropNs); + const keepNamed = named.filter((e) => used.has(e.name.text)); + if (!keepDefault && !keepNs && keepNamed.length === 0) { + const end = text[st.getEnd()] === '\n' ? st.getEnd() + 1 : st.getEnd(); + let start = st.getStart(sf); + if (dropAttachedComments) { + // Walk up the `//` lines that touch the declaration (no blank line between). + for (;;) { + const prevEnd = start - 1; + if (prevEnd <= 0 || text[prevEnd] !== '\n') break; + const lineStart = text.lastIndexOf('\n', prevEnd - 1) + 1; + if (!/^\s*\/\/.*$/.test(text.slice(lineStart, prevEnd))) break; + start = lineStart; + } + } + edits.push({ start, end, text: '' }); + continue; + } + if (nb && ts.isNamedImports(nb) && dropNamed.length > 0) { + if (keepNamed.length === 0) { + edits.push({ start: clause.name.getEnd(), end: nb.getEnd(), text: '' }); + } else { + const one = !nb.getText(sf).includes('\n'); + const parts = keepNamed.map((e) => e.getText(sf)); + edits.push({ start: nb.getStart(sf), end: nb.getEnd(), text: one ? `{ ${parts.join(', ')} }` : `{\n${parts.map((x) => ` ${x},`).join('\n')}\n}` }); + } + } + if (dropNs) edits.push({ start: clause.name.getEnd(), end: nb.getEnd(), text: '' }); + if (dropDefault) edits.push({ start: clause.name.getStart(sf), end: nb.getStart(sf), text: '' }); + } + edits.sort((a, b) => b.start - a.start); + let out = text; + for (const e of edits) out = out.slice(0, e.start) + e.text + out.slice(e.end); + return { text: out, removed: removed.sort() }; +} + +// ── Proof 1: byte-identity ─────────────────────────────────────────────────── + +const sha256 = (text) => createHash('sha256').update(text).digest('hex'); + +/** + * Prove a split against its base, from the texts alone (it shares no state with + * `generate`): every generated function is its base method's text, every leaf + * declaration is its base statement's text, and undoing the split rebuilds the + * base `registry.ts` byte-for-byte, once the base's import bindings the head no + * longer reads are pruned from it. + * + * Undoing is: put each body back on its stub (and `async` back), drop the + * generated import / export statements, then splice the leaf's statements back + * between the statements that stayed — the interleaving read off the base by + * NAME only, so every byte of the result comes from the head or the leaf. + * + * The one edit the undo does not reverse by reading the head is the import + * prune: it is recomputed from the head (which import bindings does the head + * still read?) and applied to the base, and every binding it drops must occur + * in a moved body or a moved declaration, so a binding the head lost for any + * other reason fails. + * + * Returns `{ failures, moved, movedTop, pruned, linesBefore, linesAfter }`; no failure = proved. + */ +function verify(tm, baseText, headText, moduleTexts, { readSibling = null } = {}) { + const { Node } = tm; + const failures = []; + const moved = []; + const movedTop = []; + const baseSf = parse(tm, HOST_PATH, baseText); + const baseCls = baseSf.getClass(CLASS_NAME); + const headSf = parse(tm, HOST_PATH, headText); + const headCls = headSf.getClass(CLASS_NAME); + const none = { failures: [`no class ${CLASS_NAME} at base or head`], moved, movedTop, pruned: [], linesBefore: 0, linesAfter: 0 }; + if (!baseCls || !headCls) return none; + const prune = pruneImports(tm, baseText, usedImportNames(tm, headText)); + const target = prune.text; + + const modules = new Map(); + let leaf = null; + let hostsOnDisk = null; + for (const [file, text] of moduleTexts) { + if (!text.includes(MARK)) continue; + if (file === HOSTS_MODULE) { hostsOnDisk = text; continue; } + const sf = parse(tm, join(SRC_DIR, file), text); + if (file === LEAF_MODULE) { + const statements = []; + const stmts = sf.getStatements(); + // The generated header is leading trivia of the first statement when no + // import precedes it; a moved statement's text starts after it. + const headerEnd = text.indexOf('\n\n', text.indexOf(MARK)) + 1; + stmts.forEach((st, i) => { + if (Node.isImportDeclaration(st)) { + if (st.getModuleSpecifierValue() === './registry.js' || SEAM_SPECIFIER.test(st.getModuleSpecifierValue())) { + failures.push(`${file}: imports ${st.getModuleSpecifierValue()} — a leaf never imports registry.ts or a registry-* module`); + } + return; + } + if (i === stmts.length - 1 && Node.isExportDeclaration(st) && !st.getModuleSpecifier() && st.getFullText().includes(MARK)) return; + const names = declaredNames(tm, st); + if (!names) { failures.push(`${file}: a statement that is not a moved declaration: ${st.getText().slice(0, 60)}`); return; } + statements.push({ names, text: text.slice(Math.max(st.getFullStart(), headerEnd), st.getEnd()), node: st }); + }); + leaf = { file, sf, statements }; + if (readSibling) { + // The leaf's relative VALUE imports, followed on disk: none may reach + // registry.ts or a registry-* module. A type-only import is erased, so it + // forms no evaluation cycle and is not followed. + const seen = new Set(); + const queue = valueSpecifiers(text).map((spec) => [file, spec]); + while (queue.length > 0) { + const [from, spec] = queue.shift(); + let rel = relative(SRC_DIR, resolve(dirname(join(SRC_DIR, from)), spec.replace(/\.js$/, '.ts'))); + if (seen.has(rel)) continue; + seen.add(rel); + if (rel === HOST_BASENAME || /^registry-[a-z0-9-]+\.ts$/.test(rel)) { + failures.push(`${file}: reaches ${rel} through its value imports (from ${from}) — not a leaf`); + continue; + } + let sibling = readSibling(rel); + if (sibling === null) { + const index = rel.replace(/\.ts$/, '/index.ts'); + sibling = readSibling(index); + if (sibling !== null) rel = index; + } + if (sibling === null) continue; + for (const next of valueSpecifiers(sibling)) queue.push([rel, next]); + } + } + continue; + } + // A facet module: imports, the host interface, and ONE class, the facet, + // whose every member is a static method that is a moved body. + const fns = new Map(); + const facet = facetNameOf(file); + for (const st of sf.getStatements()) { + if (Node.isImportDeclaration(st)) continue; + if (Node.isInterfaceDeclaration(st) && st.getName() === hostNameOf(file)) continue; + if (Node.isClassDeclaration(st) && st.getName() === facet && !fns.size) { + for (const member of st.getMembers()) { + if (Node.isMethodDeclaration(member) && member.isStatic() && member.getBody() && !fns.has(member.getName())) fns.set(member.getName(), member); + else failures.push(`${file}: ${facet} has a member that is not a moved body: ${member.getText().slice(0, 60)}`); + } + continue; + } + failures.push(`${file}: a top-level statement that is neither an import, the host interface nor the facet class ${facet}: ${st.getText().slice(0, 60)}`); + } + modules.set(file, { sf, text, fns, claimed: new Set() }); + } + + const marker = new RegExp(`^\\{\\s*// ${MARK.replace(/[[\]]/g, '\\$&')} The body moved to \`(registry-[a-z0-9-]+\\.ts)\`(, ${DOC_MOVED})?`); + const movedDocs = []; + const edits = []; + for (const m of headCls.getMethods()) { + const body = m.getBody(); + if (!body) continue; + const hit = marker.exec(body.getText()); + if (!hit) continue; + const name = m.getName(); + const mod = modules.get(hit[1]); + const fn = mod && mod.fns.get(name); + if (!fn) { failures.push(`${name}: its stub names ${hit[1]}, which has no function ${name}`); continue; } + mod.claimed.add(name); + // The stub itself is exactly what `stubBody` writes for this method: one + // call of the facet member of the same name, with the facet's own host + // when the body reads the class (#30051). + { + const lineStart = headText.lastIndexOf('\n', body.getEnd() - 1) + 1; + const closeIndent = headText.slice(lineStart, body.getEnd() - 1); + const usesHost = fn.getParameters().some((p) => p.getName() === 'this'); + const want = stubBody({ node: m, name, usesHost, docMoved: Boolean(hit[2]) }, hit[1], closeIndent); + if (body.getText() !== want) failures.push(`${name}: its stub is not the generated one-call stub (want ${JSON.stringify(want.split('\n').at(-2).trim())})`); + } + const base = baseCls.getMethods().find((x) => x.getName() === name && x.isStatic() === m.isStatic()); + if (!base || !base.getBody()) { failures.push(`${name}: no method of that name with a body at base`); continue; } + const fnParams = fn.getParameters().filter((p) => p.getName() !== 'this').map((p) => p.getText()); + const baseParams = base.getParameters().map((p) => p.getText()); + const same = { + body: fn.getBody().getText() === base.getBody().getText(), + params: JSON.stringify(fnParams) === JSON.stringify(baseParams), + returnType: (fn.getReturnTypeNode()?.getText() ?? null) === (base.getReturnTypeNode()?.getText() ?? null), + async: fn.isAsync() === base.isAsync(), + typeParameters: JSON.stringify(fn.getTypeParameters().map((t) => t.getText())) === JSON.stringify(base.getTypeParameters().map((t) => t.getText())), + static: m.isStatic() === base.isStatic(), + }; + // Docblock ownership: the facet member's leading text is the base + // method's docblock (the stub says so, and only a private method's), else + // exactly the placeholder; the undo puts a moved docblock back. + const lead = mod.text.slice(fn.getStart(true), fn.getStart()); + const docMoved = Boolean(hit[2]); + if (docMoved) { + if (m.getStart(true) !== m.getStart()) failures.push(`${name}: its stub says the docblock moved, but the class still carries one`); + if (m.getScope() !== 'private') failures.push(`${name}: a ${m.getScope()} method's docblock moved off the class (only a private one may)`); + edits.push({ start: m.getStart(), end: m.getStart(), text: lead }); + movedDocs.push(lead); + } else if (lead !== `${placeholderDoc(name)}\n `) { + failures.push(`${name}: the facet member's leading text is neither its moved docblock nor the placeholder`); + } + for (const [what, ok] of Object.entries(same)) if (!ok) failures.push(`${name}: the generated function's ${what} is not the base method's`); + moved.push({ + name, module: hit[1], bodySha: sha256(fn.getBody().getText()), baseBodySha: sha256(base.getBody().getText()), + lines: base.getBody().getEndLineNumber() - base.getBody().getStartLineNumber() + 1, text: fn.getText(), docMoved, ...same, + }); + edits.push({ start: body.getStart(), end: body.getEnd(), text: fn.getBody().getText() }); + if (fn.isAsync()) edits.push({ start: m.getNameNode().getStart(), end: m.getNameNode().getStart(), text: 'async ' }); + } + + // The statics route: the forwarder type and object ahead of the class and + // the static block that fills them must be exactly what `staticsTexts` + // writes for the statics the type names, each forwarded as the BASE class + // declares it (a method called, anything else read). Then the undo drops + // them. + const staticsHost = headSf.getInterface(STATICS_HOST_NAME); + const staticsVar = headSf.getVariableStatement(STATICS_NAME); + const markedBlocks = headCls.getMembers().filter((x) => Node.isClassStaticBlockDeclaration(x) && x.getFullText().includes(MARK)); + const hostsBlocks = markedBlocks.filter((x) => x.getFullText().includes(`// ${MARK} Installs`)); + const staticBlocks = markedBlocks.filter((x) => !hostsBlocks.includes(x)); + if (staticsHost || staticsVar || staticBlocks.length > 0) { + const kinds = new Map(); + for (const member of staticsHost ? staticsHost.getProperties() : []) { + const n = member.getName(); + const declared = baseCls.getMembers().filter((x) => typeof x.isStatic === 'function' && x.isStatic() && typeof x.getName === 'function' && x.getName() === n); + if (declared.length === 0) failures.push(`${STATICS_HOST_NAME}.${n}: the base class declares no static of that name`); + kinds.set(n, declared.some((x) => Node.isMethodDeclaration(x)) ? 'method' : 'getter'); + } + const { ahead, block } = staticsTexts(kinds); + const aheadAt = staticsHost ? headText.lastIndexOf(`// ${MARK} The statics of`, staticsHost.getStart()) : -1; + const aheadEnd = staticsVar ? staticsVar.getEnd() : -1; + if (!staticsHost || !staticsVar || aheadAt < 0 || headText.slice(aheadAt, aheadEnd) !== ahead) { + failures.push(`${STATICS_HOST_NAME} / ${STATICS_NAME} are not the generated forwarder declarations for the statics they name`); + } else { + edits.push({ start: headText.lastIndexOf('\n\n', aheadAt), end: aheadEnd, text: '' }); + } + const blockAt = staticBlocks.length === 1 ? headText.lastIndexOf(` // ${MARK} Fills`, staticBlocks[0].getStart()) : -1; + if (staticBlocks.length !== 1 || blockAt < 0 || headText.slice(blockAt, staticBlocks[0].getEnd()) !== block) { + failures.push(`the class's static block is not the generated filler of ${STATICS_NAME} for the statics it names`); + } else { + edits.push({ start: headText.lastIndexOf('\n\n', blockAt), end: staticBlocks[0].getEnd(), text: '' }); + } + } + for (const [file, mod] of modules) { + for (const name of mod.fns.keys()) if (!mod.claimed.has(name)) failures.push(`${file}: function ${name} has no stub on the class`); + } + + // The facet hosts (#30051): `HOSTS_MODULE` and the static block that + // installs the factories must be exactly what `hostsTexts` writes for the + // facets on disk, each member forwarded as the BASE class declares it (a + // method as a closure, anything else as a getter, a member the interface + // does not mark `readonly` with a setter too). Then the undo drops both. + const baseMembers = instanceMembers(tm, baseCls); + const facets = []; + for (const [file, mod] of modules) { + const iface = mod.sf.getInterface(hostNameOf(file)); + if (!iface) continue; + const members = new Map(); + const written = new Set(); + for (const p of iface.getProperties()) { + const n = p.getName(); + const info = baseMembers.get(n); + if (!info) { failures.push(`${file}: ${hostNameOf(file)}.${n} names no instance member of the base class`); continue; } + members.set(n, info.kind === 'method' ? 'method' : 'getter'); + if (info.kind !== 'method' && !p.isReadonly()) written.add(n); + } + facets.push({ moduleFile: file, members, written }); + } + if (facets.length > 0 || hostsOnDisk !== null || hostsBlocks.length > 0) { + const want = hostsTexts(facets); + if (hostsOnDisk !== want.module) failures.push(`${HOSTS_MODULE} is not the generated host module for the facets on disk`); + const blockAt = hostsBlocks.length === 1 ? headText.lastIndexOf(` // ${MARK} Installs`, hostsBlocks[0].getStart()) : -1; + if (hostsBlocks.length !== 1 || blockAt < 0 || headText.slice(blockAt, hostsBlocks[0].getEnd()) !== want.block) { + failures.push('the class\'s host static block is not the generated installer of the facet hosts'); + } else { + edits.push({ start: headText.lastIndexOf('\n\n', blockAt), end: hostsBlocks[0].getEnd(), text: '' }); + } + } + const generatedSpecifier = (spec) => { + const hit = /^\.\/(registry-[a-z0-9-]+)\.js$/.exec(spec ?? ''); + return Boolean(hit && (modules.has(`${hit[1]}.ts`) || `${hit[1]}.ts` === HOSTS_MODULE || (leaf && `${hit[1]}.ts` === leaf.file))); + }; + for (const st of headSf.getStatements()) { + const drop = (Node.isImportDeclaration(st) && generatedSpecifier(st.getModuleSpecifierValue())) || + (Node.isExportDeclaration(st) && (st.getModuleSpecifier() + ? generatedSpecifier(st.getModuleSpecifierValue()) + : st.getFullText().includes(MARK))); + if (drop) edits.push({ start: st.getFullStart(), end: st.getEnd(), text: '' }); + } + edits.sort((a, b) => b.start - a.start || b.end - a.end); + let rebuilt = headText; + for (const e of edits) rebuilt = rebuilt.slice(0, e.start) + e.text + rebuilt.slice(e.end); + + if (leaf) { + // Splice the leaf back, in the order the base interleaves it by NAME. + const restSf = parse(tm, HOST_PATH, rebuilt); + const rest = restSf.getStatements(); + const queue = [...leaf.statements]; + const baseStmts = parse(tm, HOST_PATH, target).getStatements(); + let j = 0; + let assembled = rest.length > 0 ? rebuilt.slice(0, rest[0].getFullStart()) : ''; + for (const b of baseStmts) { + const names = declaredNames(tm, b); + const key = names ? names.join(',') : null; + if (queue.length > 0 && key !== null && queue[0].names.join(',') === key) { + const l = queue.shift(); + assembled += l.text; + const same = l.text === b.getFullText(); + if (!same) failures.push(`${leaf.file}: ${key} is not its base text (leading comments included)`); + movedTop.push({ + name: key, lines: b.getEndLineNumber() - b.getStartLineNumber(true) + 1, + sha: sha256(l.text), baseSha: sha256(b.getFullText()), same, exported: Node.isExportable(b) && b.isExported(), + }); + continue; + } + if (j >= rest.length) { failures.push(`undoing the split runs out of head statements at base ${key ?? b.getKindName()}`); break; } + assembled += rest[j].getFullText(); + j += 1; + } + if (queue.length > 0) failures.push(`${leaf.file}: ${queue.map((q) => q.names.join(',')).join('; ')} not placed — out of base order or not in the base`); + if (rest.length > 0) assembled += rebuilt.slice(rest[rest.length - 1].getEnd()); + rebuilt = assembled; + } + + const movedText = [...moved.map((m) => m.text), ...movedDocs, ...(leaf ? leaf.statements.map((s) => s.text) : [])].join('\n'); + for (const n of prune.removed) { + if (!new RegExp(`(^|[^\\w$])${n.replace(/\$/g, '\\$')}($|[^\\w$])`).test(movedText)) { + failures.push(`the head dropped the import \`${n}\`, which no moved body or declaration reads`); + } + } + if (rebuilt !== target) { + const a = rebuilt.split('\n'); + const b = target.split('\n'); + let i = 0; + while (i < Math.max(a.length, b.length) && a[i] === b[i]) i += 1; + failures.push(`undoing the split does not rebuild the base registry.ts (its unread imports pruned): first difference at line ${i + 1}` + + `\n rebuilt: ${JSON.stringify(a[i] ?? '')}\n base: ${JSON.stringify(b[i] ?? '')}`); + } + if (moved.length === 0 && movedTop.length === 0) failures.push('no moved method or declaration found: a proof over nothing is not a proof'); + return { failures, moved, movedTop, pruned: prune.removed, linesBefore: lineCount(baseText), linesAfter: lineCount(headText), + routedStatics: staticsHost ? staticsHost.getProperties().length : 0 }; +} + +/** Lines as `wc -l` counts them: newline characters. */ +function lineCount(text) { + return text.split('\n').length - 1; +} + +// ── Reports ────────────────────────────────────────────────────────────────── + +function planReport(planned) { + const out = []; + const bySection = new Map(); + for (const r of planned.records) { + const key = r.section ?? '(above the first anchor)'; + if (!bySection.has(key)) bySection.set(key, []); + bySection.get(key).push(r); + } + let movedMethods = 0; + let movedLines = 0; + let movedSaved = 0; + const reasonCounts = new Map(); + for (const [section, rs] of bySection) { + const movers = rs.filter((r) => r.reasons.length === 0); + const lines = movers.reduce((n, r) => n + r.bodyLines, 0); + const saved = movers.reduce((n, r) => n + r.bodyLines - r.stubLines, 0); + movedMethods += movers.length; + movedLines += lines; + movedSaved += saved; + out.push(`§ ${section} -> ${rs[0].module ?? '(stays)'}: ${movers.length}/${rs.length} methods move ` + + `(${lines} body lines out, ${lines - saved} stub lines in)`); + for (const r of rs) { + if (r.reasons.length === 0) out.push(` move ${r.name} (body ${r.bodyLines} -> stub ${r.stubLines})`); + else out.push(` stay ${r.name} (${r.lineCount}) — ${r.reasons.join('; ')}`); + for (const reason of r.reasons) { + const code = reason.split(':')[0]; + reasonCounts.set(code, (reasonCounts.get(code) ?? 0) + 1); + } + } + } + out.push(''); + out.push(`total: ${movedMethods}/${planned.records.length} methods move into ${planned.modules.size} module(s): ` + + `${movedLines} body lines out, ${movedLines - movedSaved} stub lines in, plus ${2 * planned.modules.size} import lines`); + out.push(`stay reasons (a method may carry several): ${[...reasonCounts].sort((a, b) => b[1] - a[1]).map(([c, n]) => `${c} ${n}`).join(', ')}`); + const top = planned.top; + out.push(''); + out.push(`§ top-level declarations ahead of the class -> ${LEAF_MODULE}: ${top.movers.length}/${top.records.length} move`); + const topReasons = new Map(); + for (const r of top.records) { + if (r.reasons.length === 0) out.push(` move ${r.label} (${r.lines})${r.exported ? ' [exported, re-exported]' : ''}`); + else out.push(` stay ${r.label} (${r.lines}) — ${r.reasons.join('; ')}`); + for (const reason of r.reasons) { + const code = reason.split(':')[0]; + topReasons.set(code, (topReasons.get(code) ?? 0) + 1); + } + } + const topLines = top.movers.reduce((n, r) => n + r.lines, 0); + out.push(`top-level total: ${top.movers.length}/${top.records.length} declarations move (${topLines} lines with their docblocks); ` + + `stay reasons: ${[...topReasons].sort((a, b) => b[1] - a[1]).map(([c, n]) => `${c} ${n}`).join(', ') || 'none'}`); + out.push(`lever (a): ${planned.hostExportsForSections.size} staying name(s) exported for the section modules` + + `${planned.hostExportsForSections.size ? ` (${[...planned.hostExportsForSections.keys()].sort().join(', ')})` : ''}; ` + + `${planned.leafExports.length} leaf name(s) exported beside the base exports`); + return out.join('\n'); +} + +function treeHash(files) { + const h = createHash('sha256'); + for (const [name, text] of [...files].sort(([a], [b]) => (a < b ? -1 : 1))) h.update(`${name}\0${sha256(text)}\n`); + return h.digest('hex'); +} + +function printVerify(v) { + for (const f of v.failures) console.error(` ✗ ${f}`); + const identical = v.moved.filter((m) => m.body && m.params && m.returnType && m.async).length; + const identicalTop = v.movedTop.filter((m) => m.same).length; + console.log(`byte-identity: ${identical}/${v.moved.length} moved bodies identical to base (body, parameters, return type, async); ` + + `${identicalTop}/${v.movedTop.length} moved top-level declarations identical to base (leading comments included); ` + + `registry.ts ${v.linesBefore} -> ${v.linesAfter} lines; undoing the split ${v.failures.some((f) => f.startsWith('undoing')) ? 'does NOT' : 'does'} rebuild the base byte-for-byte ` + + `once the ${v.pruned.length} import binding(s) only moved text reads are pruned from the base (${v.pruned.join(', ') || 'none'})`); +} + +function gitShow(ref, path) { + return execFileSync('git', ['show', `${ref}:${path}`], { cwd: ROOT, encoding: 'utf8', maxBuffer: 1 << 28 }); +} + +/** A file under `registry.ts`'s directory by its relative path, or null when there is none. */ +function readSibling(rel) { + const p = join(SRC_DIR, rel); + return existsSync(p) ? readFileSync(p, 'utf8') : null; +} + +function generatedModulesOnDisk() { + const out = new Map(); + for (const file of [LEAF_MODULE, HOSTS_MODULE, ...SECTION_ANCHORS.map((row) => row[2])]) { + if (!file) continue; + const p = join(SRC_DIR, file); + if (existsSync(p)) out.set(file, readFileSync(p, 'utf8')); + } + return out; +} + +// ── Proof 2: the module's exports are unchanged ────────────────────────────── + +/** + * The names `registry.ts` exports, value and type alike, each with whether it + * carries a value, as the TypeScript checker resolves them: a name the leaf + * now declares and `registry.ts` re-exports by name counts as the same export. + * The files are compiled in one in-memory project (no lib, no `node_modules`), + * so a package import resolves to nothing and only the module graph under + * `SRC_REL` is read. + * @param {Map} files basename -> text, `registry.ts` among them + * @returns {Map} + */ +function exportedNames(tm, files) { + const project = new tm.Project({ + useInMemoryFileSystem: true, + compilerOptions: { + target: tm.ts.ScriptTarget.ES2020, module: tm.ts.ModuleKind.ES2020, + moduleResolution: tm.ts.ModuleResolutionKind.Node10, strict: true, noLib: true, allowJs: false, + }, + }); + const fs = project.getFileSystem(); + for (const [file, text] of files) fs.writeFileSync(join(SRC_DIR, file), text); + for (const [file] of files) project.addSourceFileAtPath(join(SRC_DIR, file)); + const sf = project.getSourceFileOrThrow(HOST_PATH); + const checker = project.getTypeChecker().compilerObject; + const moduleSymbol = checker.getSymbolAtLocation(sf.compilerNode); + const out = new Map(); + if (!moduleSymbol) return out; + for (const sym of checker.getExportsOfModule(moduleSymbol)) { + const target = sym.flags & tm.ts.SymbolFlags.Alias ? checker.getAliasedSymbol(sym) : sym; + // A package import resolves to nothing here (no `node_modules`): the + // checker's unknown symbol, kind `unresolved` on both sides alike. + // A name exported `type`-only carries no value whatever it aliases. + const typeOnly = (sym.declarations ?? []).some((d) => + tm.ts.isExportSpecifier(d) && (d.isTypeOnly || d.parent.parent.isTypeOnly)); + const kind = typeOnly ? 'type' + : target.flags & tm.ts.SymbolFlags.Value ? 'value' : target.flags & tm.ts.SymbolFlags.Type ? 'type' : 'unresolved'; + out.set(sym.getName(), kind); + } + return out; +} + +/** The names the head's generated lever-(a) statement exports for the section modules (`Exported for the section modules`). */ +function leverExportsOf(tm, headText) { + const names = new Set(); + for (const st of parse(tm, HOST_PATH, headText).getStatements()) { + if (!tm.Node.isExportDeclaration(st) || st.getModuleSpecifier() || !st.getFullText().includes(`${MARK} Exported for the section modules`)) continue; + for (const spec of st.getNamedExports()) names.add(spec.getNameNode().getText()); + } + return names; +} + +/** + * Every module under `SRC_DIR` that re-exports `registry.ts` with `export *`: + * through one, a name `registry.ts` newly exports would reach the package. The + * package entries (`index.ts`, `core.ts`) name what they take from it today. + */ +function starReexportersOnDisk() { + const out = []; + const walk = (dir) => { + for (const entry of readdirSync(dir, { withFileTypes: true })) { + const p = join(dir, entry.name); + if (entry.isDirectory()) { if (entry.name !== 'node_modules' && entry.name !== 'dist') walk(p); continue; } + if (!/\.ts$/.test(entry.name) || /\.(test|spec)\.ts$/.test(entry.name)) continue; + for (const m of readFileSync(p, 'utf8').matchAll(/^\s*export\s+\*\s+(?:as\s+\w+\s+)?from\s+'(\.{1,2}\/[^']+)'/gm)) { + if (resolve(dirname(p), m[1].replace(/\.js$/, '.ts')) === HOST_PATH) out.push(relative(SRC_DIR, p)); + } + } + }; + walk(SRC_DIR); + return out; +} + +/** + * Exports-equality (Proof 2): the split `registry.ts` (with every generated + * module) exports every name the base exports, each a value or a type as it + * was; the only names it adds are the staying declarations lever (a) exports + * for the section modules, and no module re-exports `registry.ts` with + * `export *`, so none of those reaches the package. Returns the failures; + * none = proved. + */ +function verifyExports(tm, baseText, headText, moduleTexts, { starReexporters = [] } = {}) { + const before = exportedNames(tm, new Map([[HOST_BASENAME, baseText]])); + const after = exportedNames(tm, new Map([[HOST_BASENAME, headText], ...moduleTexts])); + // The generated additions: lever (a)'s names, and the statics route's + // forwarder type and object when a moved body reads a non-public static. + const lever = new Set([...leverExportsOf(tm, headText), ...(headText.includes(`// ${MARK} The statics of`) ? [STATICS_NAME, STATICS_HOST_NAME] : [])]); + const failures = []; + if (before.size === 0) failures.push(`the base ${HOST_BASENAME} exports nothing the checker can read: an equality over nothing is not a proof`); + for (const [name, kind] of before) { + if (!after.has(name)) failures.push(`exports: ${name} (${kind}) is exported at base and not by the split ${HOST_BASENAME}`); + else if (after.get(name) !== kind) failures.push(`exports: ${name} is a ${kind} at base and a ${after.get(name)} after the split`); + } + const added = [...after.keys()].filter((name) => !before.has(name)); + for (const name of added) { + if (!lever.has(name)) failures.push(`exports: ${name} (${after.get(name)}) is exported by the split ${HOST_BASENAME} and not at base, and is not a generated export (lever (a) or the statics route)`); + } + for (const file of starReexporters) { + failures.push(`exports: ${file} re-exports ${HOST_BASENAME} with \`export *\`, so its generated names would reach the package; name what it takes instead`); + } + return { failures, count: before.size, values: [...before.values()].filter((k) => k === 'value').length, added }; +} + +function printExports(e) { + for (const f of e.failures) console.error(` ✗ ${f}`); + console.log(`exports-equality: ${e.failures.length === 0 ? 'equal' : 'NOT equal'} — ${HOST_BASENAME} exports ${e.count} name(s) at base ` + + `(${e.values} value(s), ${e.count - e.values} type(s)); the split exports ${e.failures.length === 0 ? 'every one, each the same kind' : 'a different set'}, ` + + `plus ${e.added.length} generated name(s) (lever (a), the statics route) (${e.added.join(', ') || 'none'}), which no module re-exports with \`export *\``); +} + +// ── Entry ──────────────────────────────────────────────────────────────────── + +const USAGE = [ + 'usage: node scripts/codemod-split-registry.mjs [--write] plan (or apply) the split of an UNSPLIT registry.ts', + ' node scripts/codemod-split-registry.mjs --verify --base REF re-prove a split on disk against registry.ts at REF', + ' node scripts/codemod-split-registry.mjs --self-test', + ' node scripts/codemod-split-registry.mjs --help', +].join('\n'); + +async function main(argv) { + // Answered before any guard: `--help` reads no tree, so the "already split" + // refusal below must never be what a reader asking for usage gets. + if (argv.includes('--help') || argv.includes('-h')) { + console.log(USAGE); + return 0; + } + if (argv.includes('--self-test')) { + await selfTest(); + if (process.exitCode === 1) return 1; + if (!selfTestReachedVerdict) { + console.error('\n✗ codemod-split-registry self-test: selfTest() returned without reaching its verdict,\n' + + 'so no success line was printed. Exiting 0 here would report a self-test that never\n' + + 'finished as a self-test that passed.'); + return 1; + } + return 0; + } + const known = new Set(['--write', '--verify', '--base']); + for (let i = 0; i < argv.length; i += 1) { + if (argv[i] === '--base') { i += 1; continue; } + if (!known.has(argv[i])) refuse(`unknown argument ${JSON.stringify(argv[i])}\n${USAGE}`); + } + const tm = await loadTsMorph(); + if (argv.includes('--verify')) { + const i = argv.indexOf('--base'); + const ref = i >= 0 ? argv[i + 1] : null; + if (!ref) refuse('--verify needs --base '); + const baseText = gitShow(ref, `${SRC_REL}/${HOST_BASENAME}`); + const headText = readFileSync(HOST_PATH, 'utf8'); + const modules = generatedModulesOnDisk(); + const v = verify(tm, baseText, headText, modules, { readSibling }); + printVerify(v); + const e = verifyExports(tm, baseText, headText, modules, { starReexporters: starReexportersOnDisk() }); + printExports(e); + return v.failures.length === 0 && e.failures.length === 0 ? 0 : 1; + } + const input = readFileSync(HOST_PATH, 'utf8'); + if (input.includes(MARK)) refuse(`${HOST_BASENAME} already carries ${MARK} stubs: run the script on an unsplit file (re-run on the latest main, never on its own output)`); + const planned = plan(tm, input); + console.log(planReport(planned)); + const out = generate(tm, input, planned); + const files = new Map([[HOST_BASENAME, out.host], ...out.modules]); + console.log(`output tree sha256: ${treeHash(files)} (input sha256 ${sha256(input)})`); + const v = verify(tm, input, out.host, out.modules, { readSibling }); + printVerify(v); + const e = verifyExports(tm, input, out.host, out.modules, { starReexporters: starReexportersOnDisk() }); + printExports(e); + if (v.failures.length > 0 || e.failures.length > 0) { + console.error('✗ the generated split fails its proof: nothing written. Report the fork; never hand-edit the split.'); + return 1; + } + if (!argv.includes('--write')) { + console.log('plan only: nothing written (pass --write to apply).'); + return 0; + } + for (const [file] of out.modules) { + const p = join(SRC_DIR, file); + if (existsSync(p) && !readFileSync(p, 'utf8').includes(MARK)) refuse(`${file} exists and was not written by this script`); + } + writeFileSync(HOST_PATH, out.host); + for (const [file, text] of out.modules) writeFileSync(join(SRC_DIR, file), text); + console.log(`✓ wrote ${HOST_BASENAME} and ${out.modules.size} module(s) under ${SRC_REL}`); + return 0; +} + +function valueSpecifiers(src) { + const out = []; + for (const m of src.matchAll(/^[ \t]*(?:import|export)\s+(type\s+)?([^;'"]*?)\s*from\s*'(\.{1,2}\/[^']+)'/gm)) { + if (m[1]) continue; + const inner = /^\{([^}]*)\}$/.exec(m[2].trim()); + if (inner && inner[1].split(',').map((s) => s.trim()).filter(Boolean).every((s) => s.startsWith('type '))) continue; + out.push(m[3]); + } + for (const m of src.matchAll(/^[ \t]*import\s*'(\.{1,2}\/[^']+)'/gm)) out.push(m[1]); + return out; +} + +// ── Self-test ──────────────────────────────────────────────────────────────── +// +// The proofs above are this script's whole value, and a proof that cannot fail +// proves nothing, so the self-test feeds each one an input it must REFUSE. The +// roster pins battery NAMES with a floor each, the roster's own size is pinned, +// and the verdict sets a flag the dispatch reads (AGENTS.md, "Writing a +// `--self-test`"), so a battery that stops running, or a `return` above the +// verdict, cannot read as a pass. + +let selfTestReachedVerdict = false; + +const SELF_TEST_BATTERIES = Object.freeze({ + 'stay rules': 20, + 'facets, statics and docblocks': 17, + 'top-level moves': 11, + 'byte-identity proof': 10, + 'import prune': 4, + 'refusals': 6, + 'exports-equality (Proof 2)': 6, +}) +const SELF_TEST_BATTERY_FLOOR = 7; + +const FIXTURE_ANCHORS = Object.freeze([ + ['count', 'Lifecycle', 'registry-lifecycle.ts'], + ['keptSection', 'Kept', null], +]); +const FIXTURE_NEEDLES = Object.freeze({ 'PINNED_TEXT': 'a fixture reader', 'needleInMethod(': 'a fixture reader' }); +const FIXTURE_CLASS_NEEDLES = Object.freeze({ 'classReaderText(': 'a fixture reader that also reads the leaf' }); +const FIXTURE_PINS = Object.freeze({ pinnedMethod: 'a fixture reader' }); +const fixturePlan = (tm, text) => plan(tm, text, + { anchors: FIXTURE_ANCHORS, pins: FIXTURE_PINS, needles: FIXTURE_NEEDLES, classNeedles: FIXTURE_CLASS_NEEDLES }); + +function fixtureSource(extraMethods = '') { + const pad = Array.from({ length: 12 }, (_, i) => ` const p${i} = ${i};`).join('\n'); + return `import type { Knex } from 'knex'; +import { helperImported } from './other.js'; +import { onlyMovedReads } from './moved-only.js'; +import { seamHelper } from './registry-seam.js'; + +/** A value the leaf takes, read by a moved body. */ +const LOCAL_VALUE = 1; +type LocalType = { n: number }; +export interface ExportedShape { a: number } + +// A reader takes this one out of the file by path. +const PINNED = 'PINNED_TEXT'; +function dependsOnPinned(): string { + return PINNED; +} +function namesClass(d: SchemaRegistry): SchemaRegistry { + return d; +} +const VIA_SEAM = seamHelper(); +export const DERIVED = LOCAL_VALUE + 1; +function classReaderText(n: number): number { + return n; +} + +export class SchemaRegistry { + private count = 0; + private knex: any = null; + protected logger: { warn(m: string): void } = console; + private readonly fn = (x: string): string => x; + private static readonly SECRET = 1; + private static bump = 0; + + /** A private docblock: it moves with the body. */ + private async movable(object: string, trx?: Knex.Transaction): Promise { +${pad} + const a = helperImported(object) as ExportedShape | string; + this.logger.warn(String(a) + onlyMovedReads()); + return this.other(String(a)) + String(trx ? this.count : 0); + } + + /** A protected docblock: it stays on the facade. */ + protected other(x: string): string { + return x; + } + + protected writes(): void { +${pad} + this.count = 1; + } + + protected asValue(): string[] { +${pad} + return ['a'].map(this.other); + } + + protected readsLocal(): number { +${pad} + return LOCAL_VALUE + dependsOnPinned().length; + } + + protected namesLocalType(): LocalType | null { +${pad} + return null; + } + + public inferred() { +${pad} + return 1; + } + + protected scoped(object: string): string { +${pad} + return this.getBuilder(object); + } + + protected getBuilder(object: string): string { + return object; + } + + protected defaulted(x: string = 'a'): string { +${pad} + return x; + } + + protected callsField(): string { +${pad} + return this.fn('a'); + } + + protected readsStatic(): number { +${pad} + return SchemaRegistry.SECRET; + } + + protected bumpsStatic(): number { +${pad} + SchemaRegistry.bump += 1; + return SchemaRegistry.SECRET; + } + + protected namesClassType(): SchemaRegistry | null { +${pad} + return null; + } + + protected delete(): number { +${pad} + return 4; + } + + protected deletesField(): void { +${pad} + delete this.knex; + } + + protected destructured({ a }: { a: number }): number { +${pad} + return a; + } + + protected generic(x: T): T { +${pad} + return this.other(x) as T; + } + + static staticHelper(n: number): number { +${pad} + return n + SchemaRegistry.SECRET; + } + + static usesThis(): number { +${pad} + return this.SECRET; + } + + protected pinnedMethod(): number { +${pad} + return 3; + } + + protected carriesNeedle(): number { +${pad} + return needleInMethod(1); + } + + protected carriesClassNeedle(): number { +${pad} + return classReaderText(1); + } +${extraMethods} + protected keptSection(): number { +${pad} + return 2 + VIA_SEAM + namesClass(this).count; + } +} + +function needleInMethod(n: number): number { + return n; +} +`; +} + +async function selfTest() { + const tm = await loadTsMorph(); + const batterySeen = new Map(); + let battery = null; + let failed = 0; + let cases = 0; + const open = (name) => { battery = name; }; + const check = (label, ok, detail = '') => { + batterySeen.set(battery, (batterySeen.get(battery) ?? 0) + 1); + cases += 1; + if (ok) console.log(` ✓ [${battery}] ${label}`); + else { failed += 1; console.error(` ✗ [${battery}] ${label}${detail ? ` — ${detail}` : ''}`); } + }; + const refused = (fn) => { try { fn(); return false; } catch (e) { return e instanceof CodemodRefusal; } }; + const refusalOf = (fn) => { try { fn(); return ''; } catch (e) { if (e instanceof CodemodRefusal) return e.message; throw e; } }; + + open('stay rules'); + { + const planned = fixturePlan(tm, fixtureSource()); + const why = (name) => (planned.records.find((r) => r.name === name)?.reasons ?? ['']).join('; '); + const stays = (name, code) => check(`${name} stays (${code})`, why(name).split('; ').some((r) => r.startsWith(code)), why(name)); + const moves = (name, because) => check(`${name} moves (${because})`, why(name) === '', why(name)); + moves('movable', 'the canonical body, private members read through the host'); + moves('readsLocal', 'lever (a)/(b): it imports the leaf value and the staying function'); + moves('namesLocalType', 'a non-exported type it names moves to the leaf'); + moves('scoped', 'a method it calls goes as a closure'); + moves('other', 'a body no longer than its stub moves too: the facade gate counts bodies, not lines'); + moves('writes', 'a field it assigns goes through a host setter'); + stays('deletesField', 'this-write'); + moves('delete', 'a reserved word is a valid facet member name'); + stays('asValue', 'method-as-value'); + stays('inferred', 'inferred-return-type'); + moves('defaulted', 'the stub keeps the default and forwards the value'); + stays('destructured', 'parameter-shape'); + moves('generic', 'the stub calls the body instantiated with its type parameters'); + moves('staticHelper', 'a static whose body reads no `this`'); + stays('usesThis', 'static-this'); + moves('readsStatic', 'a private static it reads goes through SchemaRegistryStatics'); + stays('bumpsStatic', 'static-write'); + stays('namesClassType', 'static-route-conflict'); + check('a pinned method and a method carrying a needle stay (path-reader)', + why('pinnedMethod').startsWith('path-reader') && why('carriesNeedle').startsWith('path-reader'), + `${why('pinnedMethod')} | ${why('carriesNeedle')}`); + check('a method in a section that maps to no module stays (section-kept)', why('keptSection').startsWith('section-kept'), why('keptSection')); + } + + open('top-level moves'); + { + const input = fixtureSource(); + const planned = fixturePlan(tm, input); + const top = (label) => planned.top.records.find((r) => r.label === label); + const why = (label) => (top(label)?.reasons ?? ['']).join('; '); + check('a value, a local type and an exported interface move', ['LOCAL_VALUE', 'LocalType', 'ExportedShape', 'DERIVED'] + .every((n) => why(n) === ''), ['LOCAL_VALUE', 'LocalType', 'ExportedShape', 'DERIVED'].map(why).join(' | ')); + check('a declaration carrying a reader\'s needle stays (path-reader)', why('PINNED').startsWith('path-reader'), why('PINNED')); + check('a declaration carrying a class-scoped needle moves: its reader reads the leaf', why('classReaderText') === '', why('classReaderText')); + check('a declaration naming a stayer stays (needs-stayer)', why('dependsOnPinned').startsWith('needs-stayer'), why('dependsOnPinned')); + check('a declaration naming the class stays (class-reference)', why('namesClass').startsWith('class-reference'), why('namesClass')); + check('a declaration using a registry-* import stays (seam-import)', why('VIA_SEAM').startsWith('seam-import'), why('VIA_SEAM')); + check('a declaration after the class is not in the region', !top('needleInMethod')); + const out = generate(tm, input, planned); + const leaf = out.modules.get(LEAF_MODULE) ?? ''; + check('the leaf header is line comments, never a docblock the bundled .d.ts would publish', + leaf.includes(MARK) && !leaf.slice(0, leaf.indexOf('\n\n', leaf.indexOf(MARK))).includes('/*')); + check('the leaf imports neither registry.ts nor a registry-* module', leaf.length > 0 && !/from '\.\/registry[.-]/.test(leaf), leaf.slice(0, 200)); + check('registry.ts re-exports the exported movers by name, the interface as a type', + /export \{ DERIVED, type ExportedShape \} from '\.\/registry-top-level\.js';/.test(out.host)); + check('lever (a): the staying function a moved body reads is exported for its module, and the module imports it', + /export \{ dependsOnPinned \};/.test(out.host) && + /import \{ SchemaRegistryStatics as SchemaRegistry, dependsOnPinned \} from '\.\/registry\.js';/.test(out.modules.get('registry-lifecycle.ts') ?? '')); + } + + open('byte-identity proof'); + { + const input = fixtureSource(); + const planned = fixturePlan(tm, input); + const out = generate(tm, input, planned); + const again = generate(tm, input, fixturePlan(tm, input)); + const v = verify(tm, input, out.host, out.modules); + check('the generated split proves against its base', v.failures.length === 0 && v.moved.length === 12 && v.movedTop.length === 5, + [...v.failures, `moved: ${v.moved.map((m) => m.name).join()}`, `top: ${v.movedTop.map((m) => m.name).join()}`].join(' | ')); + check('two runs on one input write byte-identical trees', + treeHash(new Map([['h', out.host], ...out.modules])) === treeHash(new Map([['h', again.host], ...again.modules]))); + const mod = out.modules.get('registry-lifecycle.ts'); + const edited = new Map([...out.modules, ['registry-lifecycle.ts', mod.replace('const p3 = 3;', 'const p3 = 4;')]]); + check('one edited character in a moved body fails', verify(tm, input, out.host, edited).failures.length > 0); + check('an edited stub signature fails', + verify(tm, input, out.host.replace('trx?: Knex.Transaction): Promise', 'trx?: Knex.Transaction): Promise'), out.modules).failures.length > 0); + check('an extra top-level statement in a module fails', + verify(tm, input, out.host, new Map([...out.modules, ['registry-lifecycle.ts', `${mod}\nexport const extra = 1;\n`]])).failures.length > 0); + check('a proof over no moved method fails', verify(tm, input, input, new Map()).failures.length > 0); + const leaf = out.modules.get(LEAF_MODULE); + const leafWith = (text) => new Map([...out.modules, [LEAF_MODULE, text]]); + check('one edited character in a moved declaration fails', + verify(tm, input, out.host, leafWith(leaf.replace('const LOCAL_VALUE = 1;', 'const LOCAL_VALUE = 2;'))).failures.length > 0); + check('an edited comment above a moved declaration fails', + verify(tm, input, out.host, leafWith(leaf.replace('/** A value the leaf takes', '/** A value the leaf holds'))).failures.length > 0); + check('a leaf that imports registry.ts back fails', + verify(tm, input, out.host, leafWith(leaf.replace('\nconst LOCAL_VALUE', "\nimport { SchemaRegistry } from './registry.js';\nconst LOCAL_VALUE"))).failures.length > 0); + const swapped = leaf.replace('type LocalType = { n: number };\nexport interface ExportedShape { a: number }', + 'export interface ExportedShape { a: number }\ntype LocalType = { n: number };'); + check('two moved declarations out of base order fail', swapped !== leaf && verify(tm, input, out.host, leafWith(swapped)).failures.length > 0); + } + + open('facets, statics and docblocks'); + { + const input = fixtureSource(); + const out = generate(tm, input, fixturePlan(tm, input)); + const mod = out.modules.get('registry-lifecycle.ts') ?? ''; + check('the bodies move into one stateless facet class of static methods', /\nexport class LifecycleFacet \{\n/.test(mod) && + /\n static async movable\(this: Pick SchemaRegistry.SECRET },\n });\n }\n\n // ${MARK} Installs`) && !out.host.includes('bump:')); + check('a written field gets a host setter that assigns it on the registry', + out.host.includes(' get count() { return registry.count; },\n set count(v) { registry.count = v; },')); + check('a generic method calls its body instantiated with its own type parameters', + out.host.includes('return (LifecycleFacet.generic).call(lifecycleHost.of(this), x);') && mod.includes(' static generic(this: Pick= 3 && + out.host.includes("import { lifecycleHost } from './registry-facet-hosts.js';")); + check('the host module keeps one FacetHost per facet and imports types only', + hostsMod.includes("export const lifecycleHost = new FacetHost('LifecycleFacet');") && + !/^import (?!type )/m.test(hostsMod) && hostsMod.includes("import type { LifecycleHost } from './registry-lifecycle.js';")); + check('a method goes into the host as a closure called on the registry at call time', + /\n [a-z]\w*: \(\.\.\.a\) => registry\.[a-z]\w*\(\.\.\.a\),\n/.test(out.host), out.host.slice(out.host.indexOf('Installs'))); + check('a stub handing another facet\'s host fails', verify(tm, input, + out.host.replace('.call(lifecycleHost.of(this), x)', '.call(otherHost.of(this), x)'), out.modules).failures.length > 0); + check('an edited host module fails', verify(tm, input, out.host, + new Map([...out.modules, [HOSTS_MODULE, hostsMod.replace('this.hosts.set(registry, host);', '')]])).failures.length > 0); + check('a written field\'s setter left out of the host fails', verify(tm, input, + out.host.replace(' set count(v) { registry.count = v; },\n', ''), out.modules).failures.length > 0); + check('a static method keeps its static stub and calls its facet body directly', + out.host.includes(' static staticHelper(n: number): number {\n // [#33949 codemod] The body moved to `registry-lifecycle.ts`.\n return LifecycleFacet.staticHelper(n);\n }')); + const withMod = (text) => new Map([...out.modules, ['registry-lifecycle.ts', text]]); + check('an edited moved docblock fails', verify(tm, input, out.host, + withMod(mod.replace('it moves with the body', 'it moved with the body'))).failures.length > 0); + check('an edited placeholder fails', verify(tm, input, out.host, + withMod(mod.replace(placeholderDoc('other'), placeholderDoc('other').replace('contract', 'promise')))).failures.length > 0); + check('a static forwarded as the wrong kind fails', verify(tm, input, + out.host.replace('SECRET: { get: () => SchemaRegistry.SECRET },', 'SECRET: { value: SchemaRegistry.SECRET },'), out.modules).failures.length > 0); + } + + open('import prune'); + { + const input = fixtureSource(); + const out = generate(tm, input, fixturePlan(tm, input)); + check('an import only a moved body read is pruned from registry.ts', out.pruned.includes('onlyMovedReads') && + !out.host.includes('onlyMovedReads'), out.pruned.join()); + check('an import the staying text still reads is kept', out.host.includes("import { seamHelper } from './registry-seam.js';")); + const v = verify(tm, input, out.host, out.modules); + check('the proof recomputes the prune from the head and names it', v.failures.length === 0 && v.pruned.includes('onlyMovedReads'), v.failures.join(' | ')); + const lost = out.host.replace("import { seamHelper } from './registry-seam.js';\n", ''); + check('an import the head lost that no moved text reads fails', verify(tm, input, lost, out.modules).failures.length > 0); + } + + open('refusals'); + { + check('an anchor the class does not declare refuses', + refused(() => plan(tm, fixtureSource(), { anchors: [['gone', 'x', 'registry-x.ts']], pins: {}, needles: {}, classNeedles: {} }))); + check('anchors out of class order refuse', + refused(() => plan(tm, fixtureSource(), { anchors: [['keptSection', 'x', null], ['count', 'y', 'registry-y.ts']], pins: {}, needles: {}, classNeedles: {} }))); + check('a pin naming what the file lacks refuses', refused(() => plan(tm, fixtureSource(), { anchors: FIXTURE_ANCHORS, pins: { gone: 'x' }, needles: {}, classNeedles: {} }))); + check('a class-scoped needle the file no longer carries refuses', + refused(() => plan(tm, fixtureSource(), { anchors: FIXTURE_ANCHORS, pins: {}, needles: {}, classNeedles: { 'gone text': 'x' } }))); + check('a needle the file no longer carries refuses', refused(() => plan(tm, fixtureSource(), { anchors: FIXTURE_ANCHORS, pins: {}, needles: { 'gone text': 'x' }, classNeedles: {} }))); + // `--help` reads no tree: it answers before the "already split" guard, so + // it exits 0 whether registry.ts on disk is split (as on a branch) or not. + check('--help is answered, never refused, whatever registry.ts holds', (await main(['--help']).catch(() => 2)) === 0); + } + + open('exports-equality (Proof 2)'); + { + const input = fixtureSource(); + const out = generate(tm, input, fixturePlan(tm, input)); + const e = verifyExports(tm, input, out.host, out.modules); + check('the generated split exports every base name, each the same kind, plus only its generated names (lever (a), the statics route)', + e.failures.length === 0 && e.count > 0 && e.added.includes('dependsOnPinned') && e.added.includes('SchemaRegistryStatics'), + [...e.failures, `added: ${e.added.join()}`].join(' | ')); + check('a base export the split no longer exports fails', + verifyExports(tm, input, out.host.replace(/export \{ DERIVED, type ExportedShape \} from '\.\/registry-top-level\.js';/, "export { type ExportedShape } from './registry-top-level.js';"), out.modules) + .failures.some((f) => /DERIVED \(value\) is exported at base and not by the split/.test(f))); + check('an export that changed kind (a value re-exported as a type only) fails', + verifyExports(tm, input, out.host.replace('export { DERIVED, type ExportedShape }', 'export { type DERIVED, type ExportedShape }'), out.modules) + .failures.some((f) => /DERIVED is a value at base and a type after the split/.test(f))); + check('a new export outside the lever-(a) statement fails', + verifyExports(tm, input, `${out.host}\nexport const SNUCK_IN = 1;\n`, out.modules) + .failures.some((f) => /SNUCK_IN \(value\) is exported by the split registry\.ts and not at base, and is not a generated export/.test(f))); + check('a module re-exporting registry.ts with export * fails', + verifyExports(tm, input, out.host, out.modules, { starReexporters: ['index.ts'] }) + .failures.some((f) => /index\.ts re-exports registry\.ts with `export \*`/.test(f))); + check('a base that exports nothing the checker reads fails: an equality over nothing is not a proof', + verifyExports(tm, 'export {};\n', 'export {};\n', new Map()).failures.some((f) => /an equality over nothing/.test(f))); + } + + const declared = Object.keys(SELF_TEST_BATTERIES); + if (declared.length < SELF_TEST_BATTERY_FLOOR) { + failed += 1; + console.error(`✗ self-test floor: ${declared.length} batteries declared, below the pinned ${SELF_TEST_BATTERY_FLOOR}`); + } + for (const [name, count] of batterySeen) { + if (!declared.includes(name)) { failed += 1; console.error(`✗ self-test floor: battery "${name}" ran ${count} case(s) but is not declared`); } + } + for (const name of declared) { + const count = batterySeen.get(name) ?? 0; + if (count < SELF_TEST_BATTERIES[name]) { + failed += 1; + console.error(`✗ self-test floor: battery "${name}" ran ${count} case(s), below its pinned ${SELF_TEST_BATTERIES[name]}`); + } + } + if (failed > 0) { + console.error(`\n✗ codemod-split-registry self-test: ${failed} failure(s) (cases and floor).`); + process.exitCode = 1; + selfTestReachedVerdict = true; + return; + } + console.log(`\n✓ codemod-split-registry self-test: ${cases} cases in ${declared.length} batteries pass.`); + selfTestReachedVerdict = true; +} + +if (isEntrypoint(import.meta.url)) { + main(process.argv.slice(2)).then( + (code) => process.exit(code), + (error) => { + if (error instanceof CodemodRefusal) { + console.error(`✗ codemod-split-registry: ${error.message}`); + process.exit(2); + } + console.error(error); + process.exit(2); + }, + ); +} From 6a0c994c8525240e7e025ccd289a8ca369dc4f47 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:56:11 +0000 Subject: [PATCH 02/12] refactor(objectql): split registry.ts into section modules by codemod (move-only) Generated by node scripts/codemod-split-registry.mjs --write on a branch cut from origin/main; never hand-edited. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ --- packages/objectql/src/registry-apps.ts | 216 + packages/objectql/src/registry-bare-names.ts | 376 + packages/objectql/src/registry-facet-hosts.ts | 68 + packages/objectql/src/registry-items.ts | 351 + .../src/registry-namespaces-and-claims.ts | 490 + .../src/registry-object-registration.ts | 1638 ++++ .../src/registry-package-addressing.ts | 783 ++ packages/objectql/src/registry-packages.ts | 708 ++ packages/objectql/src/registry-picklists.ts | 505 + .../objectql/src/registry-platform-global.ts | 753 ++ packages/objectql/src/registry-restore.ts | 362 + packages/objectql/src/registry-storage.ts | 187 + packages/objectql/src/registry-top-level.ts | 2748 ++++++ packages/objectql/src/registry.ts | 8243 ++--------------- 14 files changed, 9967 insertions(+), 7461 deletions(-) create mode 100644 packages/objectql/src/registry-apps.ts create mode 100644 packages/objectql/src/registry-bare-names.ts create mode 100644 packages/objectql/src/registry-facet-hosts.ts create mode 100644 packages/objectql/src/registry-items.ts create mode 100644 packages/objectql/src/registry-namespaces-and-claims.ts create mode 100644 packages/objectql/src/registry-object-registration.ts create mode 100644 packages/objectql/src/registry-package-addressing.ts create mode 100644 packages/objectql/src/registry-packages.ts create mode 100644 packages/objectql/src/registry-picklists.ts create mode 100644 packages/objectql/src/registry-platform-global.ts create mode 100644 packages/objectql/src/registry-restore.ts create mode 100644 packages/objectql/src/registry-storage.ts create mode 100644 packages/objectql/src/registry-top-level.ts diff --git a/packages/objectql/src/registry-apps.ts b/packages/objectql/src/registry-apps.ts new file mode 100644 index 00000000000..16b443a6f22 --- /dev/null +++ b/packages/objectql/src/registry-apps.ts @@ -0,0 +1,216 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33949 codemod] The "Apps, navigation contributions and their diagnostics" + * section of `SchemaRegistry`: the bodies of `getApp`, `getAllApps`, + * `registerAppNavContribution`, `setPackageNavContributions`, + * `getAppNavContributions`, `applyNavContributions`, `findNavGroup`, + * `recordNavGroupMiss`, `getAppNavDiagnostics` moved out of `registry.ts` whole + * by `scripts/codemod-split-registry.mjs`. + * + * A MOVE, not a redesign. {@link AppsFacet} is a stateless facet of `SchemaRegistry` + * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` + * method of the same name exactly as it stood: not re-indented, not + * formatted, not edited. The method stays on the class as a one-call stub with + * its signature, so the published surface, an instance spy and a subclass + * override are what they were. A public or protected method keeps its + * docblock on the class, the contract; a private method's docblock moved here + * with its body. The state stays on `SchemaRegistry`. + * + * The bodies reach the class through {@link AppsHost}: exactly the + * `SchemaRegistry` members they read, private ones included. Every stub hands + * its body, by a `.call`, the same host: `appsHost` in + * `registry-facet-hosts.ts`, built once per registry by the class's static + * block. Methods go as closures that call them on the registry at call time, so + * a subclass override or an instance spy is still the one reached; fields and + * accessors go as GETTERS, read when the body reads them, as they were on the + * class, and a field a body assigns goes with a SETTER that assigns it on the + * registry. + * + * The module imports `registry.ts` for types only, so the two modules form no + * evaluation cycle. `index.ts` re-exports nothing from this module. + */ + +import { formatNavContributionGroupDiagnostic, navContributionGroupDiagnostic, findNavGroup as resolveNavGroup } from './nav-contribution-diagnostics.js'; +import type { NavContributionGroupDiagnostic } from './nav-contribution-diagnostics.js'; +import type { SchemaRegistry } from './registry.js'; + +/** + * The `SchemaRegistry` members the bodies of {@link AppsFacet} read — every one of + * them, and no other. Each is typed AS the class member (an indexed access on + * the class), so a change to a member's signature on the class is a change + * here too. `readonly` members are the getters; the others are fields a body + * assigns. + */ +export interface AppsHost { + readonly appNavContributions: SchemaRegistry['appNavContributions']; + applyNavContributions: SchemaRegistry['applyNavContributions']; + findNavGroup: SchemaRegistry['findNavGroup']; + isPackageDisabled: SchemaRegistry['isPackageDisabled']; + listItems: SchemaRegistry['listItems']; + log: SchemaRegistry['log']; + readonly navGroupDiagnostics: SchemaRegistry['navGroupDiagnostics']; + noteAppNavContributions: SchemaRegistry['noteAppNavContributions']; + recordNavGroupMiss: SchemaRegistry['recordNavGroupMiss']; + resolveItem: SchemaRegistry['resolveItem']; + warn: SchemaRegistry['warn']; +} + +/** The bodies of the "Apps, navigation contributions and their diagnostics" section of `SchemaRegistry`: a stateless facet, called by the class's stubs. */ +export class AppsFacet { + /** The body of `SchemaRegistry#getApp`; the method's docblock there is its contract. */ + static getApp(this: Pick, name: string, currentPackageId?: string): any { + const app = this.resolveItem('app', name, currentPackageId); + if (!app) return app; + return this.applyNavContributions(app); + } + + /** The body of `SchemaRegistry#getAllApps`; the method's docblock there is its contract. */ + static getAllApps(this: Pick): any[] { + return this.listItems('app').map((app: any) => this.applyNavContributions(app)); + } + + /** The body of `SchemaRegistry#registerAppNavContribution`; the method's docblock there is its contract. */ + static registerAppNavContribution(this: Pick, contribution: { app: string; group?: string; priority?: number; items?: any[] }, packageId?: string): void { + if (!contribution || !contribution.app) return; + this.noteAppNavContributions(contribution.app); + const list = this.appNavContributions.get(contribution.app) ?? []; + list.push({ + packageId, + group: contribution.group, + priority: contribution.priority ?? 200, + items: Array.isArray(contribution.items) ? contribution.items : [], + }); + this.appNavContributions.set(contribution.app, list); + this.log( + `[Registry] Navigation contribution: ${packageId ?? '(unknown)'} -> ${contribution.app}` + + (contribution.group ? `/${contribution.group}` : '') + + ` (${list[list.length - 1].items.length} items)`, + ); + } + + /** The body of `SchemaRegistry#setPackageNavContributions`; the method's docblock there is its contract. */ + static setPackageNavContributions(this: Pick, packageId: string, contributions: ReadonlyArray<{ app: string; group?: string; priority?: number; items?: any[] }>): void { + const incoming = new Map>(); + for (const contribution of contributions) { + if (!contribution || !contribution.app) continue; + const list = incoming.get(contribution.app) ?? []; + list.push({ + packageId, + group: contribution.group, + priority: contribution.priority ?? 200, + items: Array.isArray(contribution.items) ? contribution.items : [], + }); + incoming.set(contribution.app, list); + } + for (const [app, list] of this.appNavContributions) { + if (list.some((c) => c.packageId === packageId)) this.noteAppNavContributions(app); + } + for (const app of incoming.keys()) this.noteAppNavContributions(app); + for (const [app, list] of [...this.appNavContributions]) { + const at = list.findIndex((c) => c.packageId === packageId); + if (at === -1) continue; + const kept = list.filter((c) => c.packageId !== packageId); + kept.splice(at, 0, ...(incoming.get(app) ?? [])); + incoming.delete(app); + if (kept.length > 0) this.appNavContributions.set(app, kept); + else this.appNavContributions.delete(app); + } + for (const [app, list] of incoming) { + this.appNavContributions.set(app, [...(this.appNavContributions.get(app) ?? []), ...list]); + } + for (const c of contributions) { + if (!c || !c.app) continue; + this.log( + `[Registry] Navigation contribution: ${packageId} -> ${c.app}` + + (c.group ? `/${c.group}` : '') + + ` (${Array.isArray(c.items) ? c.items.length : 0} items)`, + ); + } + } + + /** The body of `SchemaRegistry#getAppNavContributions`; the method's docblock there is its contract. */ + static getAppNavContributions(this: Pick, appName: string): Array<{ packageId?: string; group?: string; priority: number; items: any[] }> { + return this.appNavContributions.get(appName) ?? []; + } + + /** The body of `SchemaRegistry#applyNavContributions`; the method's docblock there is its contract. */ + static applyNavContributions(this: Pick, app: any): any { + const contributions = this.appNavContributions.get(app?.name); + if (!contributions || contributions.length === 0) return app; + + const cloned = structuredClone(app); + const nav: any[] = Array.isArray(cloned.navigation) ? cloned.navigation : (cloned.navigation = []); + + // Lower priority applied first — mirrors object extender ordering. + const sorted = [...contributions].sort((a, b) => a.priority - b.priority); + for (const c of sorted) { + if (!c.items.length) continue; + // [#28117] A disabled package's contributions leave the app, as its + // objects leave the data plane: "disabled" has one meaning (hidden + + // refused + data kept). Asked per read, never cached, so an enable + // restores them with nothing to invalidate; skipped BEFORE the group + // lookup, so a disabled contribution is not relocated or diagnosed either. + if (this.isPackageDisabled(c.packageId)) continue; + if (c.group) { + const group = this.findNavGroup(nav, c.group); + if (group) { + if (!Array.isArray(group.children)) group.children = []; + group.children.push(...c.items); + } else { + // [#14553] The relocation itself is UNCHANGED — the fold stays + // order-independent and contributions into optional groups keep + // working (option A, a refusal plus a registration-order rule, was + // weighed and not taken). What changed is that it is no longer + // silent: this used to be one `this.log(...)` gated at `info`, so a + // deployment at `OS_REGISTRY_LOG=warn` watched its information + // architecture change with nothing in the log at all. + this.recordNavGroupMiss(String(app.name), c); + nav.push(...c.items); + } + } else { + nav.push(...c.items); + } + } + return cloned; + } + + /** + * Depth-first search for a `type: 'group'` nav item by id. + * + * [#14553] Delegates rather than implements: `os build` asks the identical + * question at compile time over a composed artifact, and two copies of this + * walk is exactly the drift that would let the build call an authoring error + * fine while this fold relocates it. + */ + static findNavGroup(items: any[], groupId: string): any | undefined { + return resolveNavGroup(items, groupId); + } + + /** + * Record — and, the first time, announce — one relocated contribution. + * + * The entry is kept so a caller that is not reading the log + * ({@link getAppNavDiagnostics}: `os doctor`, a boot report, a test) can ask + * the app what happened to it. + */ + static recordNavGroupMiss(this: Pick, appName: string, c: { packageId?: string; group?: string; items: any[] }): void { + const diagnostic = navContributionGroupDiagnostic({ + app: appName, + group: String(c.group), + ...(c.packageId === undefined ? {} : { packageId: c.packageId }), + items: c.items, + }); + const perApp = this.navGroupDiagnostics.get(appName) ?? new Map(); + const key = `${c.packageId ?? ''}|${c.group}`; + if (perApp.has(key)) return; + perApp.set(key, diagnostic); + this.navGroupDiagnostics.set(appName, perApp); + this.warn(formatNavContributionGroupDiagnostic(diagnostic)); + } + + /** The body of `SchemaRegistry#getAppNavDiagnostics`; the method's docblock there is its contract. */ + static getAppNavDiagnostics(this: Pick, appName: string): NavContributionGroupDiagnostic[] { + return [...(this.navGroupDiagnostics.get(appName)?.values() ?? [])]; + } +} diff --git a/packages/objectql/src/registry-bare-names.ts b/packages/objectql/src/registry-bare-names.ts new file mode 100644 index 00000000000..7ec1f2f4aac --- /dev/null +++ b/packages/objectql/src/registry-bare-names.ts @@ -0,0 +1,376 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33949 codemod] The "Bare names, one name one package holder" section of + * `SchemaRegistry`: the bodies of `warnAddressLiteralNamespaceOnce`, + * `bareNamePackageHolders`, `bareNameRenameFor`, `bareNameHolderNamespace`, + * `bareNameHoldersOtherThan`, `bareNameBundleAdditionMeets`, + * `refuseBareNameConflicts`, `recordBareNameClaims`, `forgetBareNameClaims`, + * `claimBareNameFunctions`, `releaseBareNameFunctions`, `sealBareNameHolders` + * moved out of `registry.ts` whole by `scripts/codemod-split-registry.mjs`. + * + * A MOVE, not a redesign. {@link BareNamesFacet} is a stateless facet of `SchemaRegistry` + * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` + * method of the same name exactly as it stood: not re-indented, not + * formatted, not edited. The method stays on the class as a one-call stub with + * its signature, so the published surface, an instance spy and a subclass + * override are what they were. A public or protected method keeps its + * docblock on the class, the contract; a private method's docblock moved here + * with its body. The state stays on `SchemaRegistry`. + * + * The bodies reach the class through {@link BareNamesHost}: exactly the + * `SchemaRegistry` members they read, private ones included. Every stub hands + * its body, by a `.call`, the same host: `bareNamesHost` in + * `registry-facet-hosts.ts`, built once per registry by the class's static + * block. Methods go as closures that call them on the registry at call time, so + * a subclass override or an instance spy is still the one reached; fields and + * accessors go as GETTERS, read when the body reads them, as they were on the + * class, and a field a body assigns goes with a SETTER that assigns it on the + * registry. + * + * Values that stayed in `registry.ts` are imported from it (lever (a) of + * ruling 6591753301); every use is inside a function body, so the import + * cycle is never read at evaluation time. `index.ts` re-exports nothing from + * this module. + */ + +import { ITEM_KEY_DISCRIMINATORS } from '@objectstack/metadata-core'; +import type { ObjectStackManifest } from '@objectstack/spec/kernel'; +import { BARE_NAME_ONE_HOLDER_TYPES, BUILT_IN_BARE_NAMES, bareNameHolderKey, bareNameHoldersMeet, declaredBareNames, declaredBuiltInBoundActions, declaredBundleMembers, handlerOwnerPackageId, holdsBareName, isBareNameRegistryType, isBundleAdditionPair, isOverridableBaseline, suggestedBareNameRename } from './bare-name-one-holder.js'; +import type { BareNameHolder, BareNameOneHolderType, BundleHolding } from './bare-name-one-holder.js'; +import { bundleBaseKey, isShareableNamespace } from './registry-top-level.js'; +import type { BareNameConflict, BundleArrival } from './registry-top-level.js'; +import { BareNameConflictError } from './registry.js'; +import type { SchemaRegistry } from './registry.js'; + +/** + * The `SchemaRegistry` members the bodies of {@link BareNamesFacet} read — every one of + * them, and no other. Each is typed AS the class member (an indexed access on + * the class), so a change to a member's signature on the class is a change + * here too. `readonly` members are the getters; the others are fields a body + * assigns. + */ +export interface BareNamesHost { + readonly addressLiteralNamespaceWarned: SchemaRegistry['addressLiteralNamespaceWarned']; + bareNameBundleAdditionMeets: SchemaRegistry['bareNameBundleAdditionMeets']; + readonly bareNameClaims: SchemaRegistry['bareNameClaims']; + bareNameHolderNamespace: SchemaRegistry['bareNameHolderNamespace']; + bareNameHoldersOtherThan: SchemaRegistry['bareNameHoldersOtherThan']; + bareNameHoldersSealed: SchemaRegistry['bareNameHoldersSealed']; + bareNamePackageHolders: SchemaRegistry['bareNamePackageHolders']; + bareNameRenameFor: SchemaRegistry['bareNameRenameFor']; + declaredBundleHolding: SchemaRegistry['declaredBundleHolding']; + getAllPackages: SchemaRegistry['getAllPackages']; + getPackage: SchemaRegistry['getPackage']; + readonly metadata: SchemaRegistry['metadata']; + noteBareNameClaim: SchemaRegistry['noteBareNameClaim']; + registeredBundleHolding: SchemaRegistry['registeredBundleHolding']; +} + +/** The bodies of the "Bare names, one name one package holder" section of `SchemaRegistry`: a stateless facet, called by the class's stubs. */ +export class BareNamesFacet { + /** + * [ADR-0150 D5, child G1] Warn, once per package, that an installed + * package's namespace spells an address-grammar literal: it keeps booting, + * its items are read with `?package=`, and the remedy is a rename. + */ + static warnAddressLiteralNamespaceOnce(this: Pick, packageId: string, refusal: string): void { + if (this.addressLiteralNamespaceWarned.has(packageId)) return; + this.addressLiteralNamespaceWarned.add(packageId); + console.warn( + `[Registry] Package '${packageId}' is installed under a namespace the address grammar reads as a route. ` + + `${refusal} It keeps booting: its items are read with \`?package=${packageId}\`, and a new install ` + + `of this namespace is refused. Renaming the namespace gives the package its qualified address.`, + ); + } + + /** + * The packages holding `(type, name)`, in registration order — the item + * store's order (whose first is the holder a caller with no package is + * served by {@link getItem}), then any holder by claim alone. A package holds + * a name through an item registered under it (its composite + * `:` slot, when the item is one the rule covers — + * `holdsBareName`: an action bound to an object holds nothing) or its claim: + * recorded at install, or, for a function, at the hook binder's bind + * ({@link claimBareNameFunctions}). ⛔ Never the bare slot: that is the + * environment catalog (the ADR-0005 overlay), whatever `_packageId` its + * hydrated row wears. ⛔ Never a type's overridable baseline + * ({@link isOverridableBaseline}): the platform's copies, which the + * consumer serves another package's item over by contract. A bundled type's + * key carries its member (`pkg:name@locale`, `bundleBaseKey`): the NAME is + * what is held, so every member of one package's bundle is that one holder. + */ + static bareNamePackageHolders(this: Pick, type: BareNameOneHolderType, name: string): string[] { + const holders = new Set(); + if (isBareNameRegistryType(type)) { + const suffix = `:${name}`; + for (const [key, item] of this.metadata.get(type) ?? []) { + const base = bundleBaseKey(key); + if (base === name || !base.endsWith(suffix) || !holdsBareName(type, item)) continue; + const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; + holders.add(typeof stamped === 'string' && stamped !== '' ? stamped : base.slice(0, -suffix.length)); + } + } + for (const claimant of this.bareNameClaims.get(type)?.get(name) ?? []) holders.add(claimant); + return [...holders].filter((packageId) => !isOverridableBaseline(type, packageId)); + } + + /** + * The rename a refusal carries for `name` in `packageId` — its namespace + * prefixed ({@link suggestedBareNameRename}) — read from the manifest the + * door holds, else from the installed record. None for a shareable platform + * namespace (`base` / `system` / `sys`): it names no one package. + */ + static bareNameRenameFor(this: Pick, packageId: string, name: string, namespace?: string): string | undefined { + const ns = namespace ?? this.getPackage(packageId)?.manifest?.namespace; + if (!ns || isShareableNamespace(ns)) return undefined; + return suggestedBareNameRename(ns, name); + } + + /** + * [ADR-0150 D2/D9, child B] The namespace a package holder declares — its + * installed record's `manifest.namespace` — or `undefined`: no record, or + * none declared. What {@link bareNameHoldersMeet} compares for a type whose + * rule has narrowed to the namespace. + */ + static bareNameHolderNamespace(this: Pick, packageId: string): string | undefined { + const namespace = this.getPackage(packageId)?.manifest?.namespace; + return typeof namespace === 'string' && namespace !== '' ? namespace : undefined; + } + + /** + * Every holder `incomingPackageId` would join by holding `(type, name)` — + * the ONE predicate both doors ask ({@link refuseBareNameConflicts} at the + * package door, the item seam in {@link registerItem}). Empty before the + * seal, and empty when `incomingPackageId` already holds the name: a reload, + * a re-install or an upgrade of a package that came through the boot beside + * another holder is not a new holder (module doc, "The cold boot"). A + * built-in name is read only when `builtIns` (the package door), as the + * security catalog reads its own. A package holder counts only when it + * meets the incoming package under {@link bareNameHoldersMeet}: for a type + * narrowed to the namespace, a holder of another namespace does not + * (`incomingNamespace`, the namespace the incoming package declares) — + * unless one holding is an addition to the other's bundle + * ({@link bareNameBundleAdditionMeets}; `incomingHolding`, read only then). + * `incomingArrival` is what the registration brings (the members it adds): + * a holder's members are read as if it were registered, so the door judges + * the state the cold boot's seal would judge (#32408, delta review N1). + */ + static bareNameHoldersOtherThan(this: Pick, type: BareNameOneHolderType, name: string, incomingPackageId: string, opts: { + builtIns: boolean; + incomingNamespace: string | undefined; + incomingHolding?: () => BundleHolding | undefined; + incomingArrival?: { readonly members: ReadonlySet }; + }): BareNameHolder[] { + if (!this.bareNameHoldersSealed) return []; + const packages = this.bareNamePackageHolders(type, name); + if (packages.includes(incomingPackageId)) return []; + const found = new Map(); + if (opts.builtIns && BUILT_IN_BARE_NAMES[type].has(name)) found.set(bareNameHolderKey({ kind: 'built-in' }), { kind: 'built-in' }); + for (const packageId of packages) { + if ( + !bareNameHoldersMeet(type, opts.incomingNamespace, this.bareNameHolderNamespace(packageId), name) + && !this.bareNameBundleAdditionMeets(type, name, opts.incomingHolding, packageId, + opts.incomingArrival && { packageId: incomingPackageId, ...opts.incomingArrival }) + ) continue; + const holder: BareNameHolder = { kind: 'package', packageId }; + found.set(bareNameHolderKey(holder), holder); + } + return [...found.values()]; + } + + /** + * [ADR-0150 D9, #31699] Do two package holders of a bundled name meet + * because one holding is an ADDITION to the other's bundle + * ({@link isBundleAdditionPair}) — whatever their namespaces, so a type + * narrowed to the namespace keeps refusing a dependent's added member as + * it did? `false` for a type that is no bundle, or when the incoming + * holding cannot be read. + */ + static bareNameBundleAdditionMeets(this: Pick, type: BareNameOneHolderType, name: string, incomingHolding: (() => BundleHolding | undefined) | undefined, holderPackageId: string, arrival?: BundleArrival): boolean { + if (!ITEM_KEY_DISCRIMINATORS[type] || !incomingHolding) return false; + const incoming = incomingHolding(); + const holder = this.registeredBundleHolding(type, name, holderPackageId, arrival); + return incoming !== undefined && holder !== undefined && isBundleAdditionPair(incoming, holder); + } + + /** + * The package door's half of the rule: once sealed, refuse a package whose + * declared items of a covered registry type bear a name another holder holds, + * ahead of every mutation {@link installPackage} makes — the security + * catalog's disposition, for its reasons: every conflict collected into one + * refusal, no co-owner exemption, no `collisionPolicy` downgrade, and a + * package with no identity not judged. + * + * @throws {BareNameConflictError} ADR-0112 envelope, naming both holders of + * each conflicting name and the rename. + */ + static refuseBareNameConflicts(this: Pick, manifest: ObjectStackManifest, selfId: string | undefined): void { + if (selfId === undefined || !this.bareNameHoldersSealed) return; + const conflicts: BareNameConflict[] = []; + const seen = new Set(); + const incomingNamespace = typeof manifest.namespace === 'string' && manifest.namespace !== '' ? manifest.namespace : undefined; + for (const { type, name } of declaredBareNames(manifest)) { + // One package declaring one name twice is one holder. + const key = `${type}|${name}`; + if (seen.has(key)) continue; + seen.add(key); + for (const existingHolder of this.bareNameHoldersOtherThan(type, name, selfId, { + builtIns: true, + incomingNamespace, + incomingHolding: () => this.declaredBundleHolding(manifest, selfId, type, name), + incomingArrival: { members: declaredBundleMembers(manifest, type, name) }, + })) { + conflicts.push({ + type, + name, + incomingPackageId: selfId, + existingHolder, + rename: this.bareNameRenameFor(selfId, name, manifest.namespace), + }); + } + } + // [#31697, contract review G6] An action bound to an object under a key the + // platform holds (`BUILT_IN_BOUND_ACTION_KEYS`): the platform's own UI posts + // to that route by its fixed name. + for (const { name, objectName } of declaredBuiltInBoundActions(manifest)) { + if (this.getPackage(selfId) !== undefined && declaredBuiltInBoundActions(this.getPackage(selfId)?.manifest) + .some((held) => held.name === name && held.objectName === objectName)) continue; + conflicts.push({ + type: 'action', name, objectName, incomingPackageId: selfId, existingHolder: { kind: 'built-in' }, + rename: this.bareNameRenameFor(selfId, name, manifest.namespace), + }); + } + if (conflicts.length > 0) throw new BareNameConflictError(conflicts); + } + + /** + * Record `packageId`'s bare-name claims — additive, like + * {@link recordSecurityCatalogClaims} and for its reasons; released by + * {@link uninstallPackage}. Recorded before the seal too: the cold boot reads + * them. + */ + static recordBareNameClaims(this: Pick, packageId: string, manifest: ObjectStackManifest): void { + for (const { type, name } of declaredBareNames(manifest)) { + let byName = this.bareNameClaims.get(type); + if (!byName) this.bareNameClaims.set(type, byName = new Map()); + this.noteBareNameClaim(type, name); + let claimants = byName.get(name); + if (!claimants) byName.set(name, claimants = new Set()); + claimants.add(packageId); + } + } + + /** Drop every bare-name claim `packageId` holds. */ + static forgetBareNameClaims(this: Pick, packageId: string): void { + for (const byName of this.bareNameClaims.values()) { + for (const [name, claimants] of byName) { + claimants.delete(packageId); + if (claimants.size === 0) byName.delete(name); + } + } + } + + /** The body of `SchemaRegistry#claimBareNameFunctions`; the method's docblock there is its contract. */ + static claimBareNameFunctions(this: Pick, owner: string | undefined, names: readonly string[]): void { + if (!owner || names.length === 0) return; + const packageId = handlerOwnerPackageId(owner); + const unique = [...new Set(names.filter((n) => typeof n === 'string' && n !== ''))]; + if (this.bareNameHoldersSealed) { + const conflicts: BareNameConflict[] = []; + for (const name of unique) { + for (const existingHolder of this.bareNameHoldersOtherThan('function', name, packageId, { + builtIns: true, + incomingNamespace: this.bareNameHolderNamespace(packageId), + })) { + conflicts.push({ + type: 'function', name, incomingPackageId: packageId, existingHolder, + rename: this.bareNameRenameFor(packageId, name), + }); + } + } + if (conflicts.length > 0) throw new BareNameConflictError(conflicts); + } + let byName = this.bareNameClaims.get('function'); + if (!byName) this.bareNameClaims.set('function', byName = new Map()); + for (const name of unique) { + let claimants = byName.get(name); + if (!claimants) byName.set(name, claimants = new Set()); + claimants.add(packageId); + } + } + + /** The body of `SchemaRegistry#releaseBareNameFunctions`; the method's docblock there is its contract. */ + static releaseBareNameFunctions(this: Pick, owner: string | undefined, stillHolds: (name: string) => boolean): void { + if (!owner) return; + const byName = this.bareNameClaims.get('function'); + if (!byName) return; + const packageId = handlerOwnerPackageId(owner); + for (const [name, claimants] of [...byName]) { + if (!claimants.has(packageId) || stillHolds(name)) continue; + claimants.delete(packageId); + if (claimants.size === 0) byName.delete(name); + } + } + + /** The body of `SchemaRegistry#sealBareNameHolders`; the method's docblock there is its contract. */ + static sealBareNameHolders(this: Pick): BareNameConflict[] { + this.bareNameHoldersSealed = true; + const conflicts: BareNameConflict[] = []; + for (const type of BARE_NAME_ONE_HOLDER_TYPES) { + const candidates = new Set(); + if (isBareNameRegistryType(type)) { + for (const key of this.metadata.get(type)?.keys() ?? []) { + // A bundled type's key ends `@`: the NAME is the candidate. + const base = bundleBaseKey(key); + const sep = base.lastIndexOf(':'); + if (sep > 0) candidates.add(base.slice(sep + 1)); + } + } + for (const name of this.bareNameClaims.get(type)?.keys() ?? []) candidates.add(name); + for (const name of [...candidates].sort()) { + if (name === '') continue; + const packages = this.bareNamePackageHolders(type, name); + if (BUILT_IN_BARE_NAMES[type].has(name)) { + for (const packageId of packages) { + conflicts.push({ + type, name, incomingPackageId: packageId, existingHolder: { kind: 'built-in' }, + rename: this.bareNameRenameFor(packageId, name), + }); + } + continue; + } + // Each later holder against the first earlier one it meets + // (`bareNameHoldersMeet`): for a type that has not narrowed, always the + // first-registered; for one narrowed to the namespace, the first of its + // own namespace, and none when every earlier holder declares another. + packages.forEach((packageId, index) => { + const namespace = this.bareNameHolderNamespace(packageId); + const first = packages + .slice(0, index) + .find((earlier) => + bareNameHoldersMeet(type, namespace, this.bareNameHolderNamespace(earlier), name) + || this.bareNameBundleAdditionMeets(type, name, () => this.registeredBundleHolding(type, name, packageId), earlier)); + if (first === undefined) return; + conflicts.push({ + type, name, incomingPackageId: packageId, existingHolder: { kind: 'package', packageId: first }, + rename: this.bareNameRenameFor(packageId, name), + }); + }); + } + } + // [#31697, contract review G6] A package the boot registered that declares + // an action bound to a key the platform holds (`BUILT_IN_BOUND_ACTION_KEYS`). + for (const pkg of this.getAllPackages()) { + const packageId = pkg.manifest?.id; + if (typeof packageId !== 'string') continue; + for (const { name, objectName } of declaredBuiltInBoundActions(pkg.manifest)) { + conflicts.push({ + type: 'action', name, objectName, incomingPackageId: packageId, existingHolder: { kind: 'built-in' }, + rename: this.bareNameRenameFor(packageId, name), + }); + } + } + return conflicts; + } +} diff --git a/packages/objectql/src/registry-facet-hosts.ts b/packages/objectql/src/registry-facet-hosts.ts new file mode 100644 index 00000000000..9b780d10604 --- /dev/null +++ b/packages/objectql/src/registry-facet-hosts.ts @@ -0,0 +1,68 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33949 codemod] The host of each facet of `SchemaRegistry`, built ONCE per registry (#30051): + * what every stub of a facet hands that facet's bodies as `this`, in place of + * a host literal built on every call. Each `FacetHost` below is filled by the + * factory `SchemaRegistry`'s static block installs, the one place that can read + * the class's private members; the host is built on the first call that needs + * it and kept for the registry's life. A method goes as a closure that calls it + * on the registry at call time, a field or accessor as a getter (and a setter + * when a body assigns it), so a body reads and writes the registry exactly as + * the per-call literal let it. + * + * Written by `scripts/codemod-split-registry.mjs`; it imports types only, so it forms no + * evaluation cycle and is evaluated before the class whichever module loads + * first. `index.ts` re-exports nothing from this module. + */ + +import type { SchemaRegistry } from './registry.js'; +import type { AppsHost } from './registry-apps.js'; +import type { BareNamesHost } from './registry-bare-names.js'; +import type { ItemsHost } from './registry-items.js'; +import type { NamespacesAndClaimsHost } from './registry-namespaces-and-claims.js'; +import type { ObjectRegistrationHost } from './registry-object-registration.js'; +import type { PackageAddressingHost } from './registry-package-addressing.js'; +import type { PackagesHost } from './registry-packages.js'; +import type { PicklistsHost } from './registry-picklists.js'; +import type { PlatformGlobalHost } from './registry-platform-global.js'; +import type { RestoreHost } from './registry-restore.js'; +import type { StorageHost } from './registry-storage.js'; + +/** One facet's host for each registry: built on first use, kept for the registry's life. */ +export class FacetHost { + private readonly hosts = new WeakMap(); + private build: ((registry: SchemaRegistry) => H) | undefined; + + constructor(private readonly facet: string) {} + + /** Called once, from `SchemaRegistry`'s static block. */ + install(build: (registry: SchemaRegistry) => H): void { + this.build = build; + } + + /** The host of `registry`, built on first use. */ + of(registry: SchemaRegistry): H { + let host = this.hosts.get(registry); + if (host === undefined) { + if (this.build === undefined) { + throw new Error(`${this.facet}: SchemaRegistry has not installed its host factory`); + } + host = this.build(registry); + this.hosts.set(registry, host); + } + return host; + } +} + +export const appsHost = new FacetHost('AppsFacet'); +export const bareNamesHost = new FacetHost('BareNamesFacet'); +export const itemsHost = new FacetHost('ItemsFacet'); +export const namespacesAndClaimsHost = new FacetHost('NamespacesAndClaimsFacet'); +export const objectRegistrationHost = new FacetHost('ObjectRegistrationFacet'); +export const packageAddressingHost = new FacetHost('PackageAddressingFacet'); +export const packagesHost = new FacetHost('PackagesFacet'); +export const picklistsHost = new FacetHost('PicklistsFacet'); +export const platformGlobalHost = new FacetHost('PlatformGlobalFacet'); +export const restoreHost = new FacetHost('RestoreFacet'); +export const storageHost = new FacetHost('StorageFacet'); diff --git a/packages/objectql/src/registry-items.ts b/packages/objectql/src/registry-items.ts new file mode 100644 index 00000000000..1847151020c --- /dev/null +++ b/packages/objectql/src/registry-items.ts @@ -0,0 +1,351 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33949 codemod] The "Generic metadata items and package bodies" section of + * `SchemaRegistry`: the bodies of `keepPackagelessActionOwnerKeys`, + * `packagelessActionEntryToRetire`, `retirePackagelessActionSlot`, `validate`, + * `unregisterItemsByPackage`, `replacingPackageBody`, + * `withdrawPackageBodyItemsNotShipped`, `withdrawPackageClaimsNotDeclared`, + * `getItem` moved out of `registry.ts` whole by + * `scripts/codemod-split-registry.mjs`. + * + * A MOVE, not a redesign. {@link ItemsFacet} is a stateless facet of `SchemaRegistry` + * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` + * method of the same name exactly as it stood: not re-indented, not + * formatted, not edited. The method stays on the class as a one-call stub with + * its signature, so the published surface, an instance spy and a subclass + * override are what they were. A public or protected method keeps its + * docblock on the class, the contract; a private method's docblock moved here + * with its body. The state stays on `SchemaRegistry`. + * + * The bodies reach the class through {@link ItemsHost}: exactly the + * `SchemaRegistry` members they read, private ones included. Every stub hands + * its body, by a `.call`, the same host: `itemsHost` in + * `registry-facet-hosts.ts`, built once per registry by the class's static + * block. Methods go as closures that call them on the registry at call time, so + * a subclass override or an instance spy is still the one reached; fields and + * accessors go as GETTERS, read when the body reads them, as they were on the + * class, and a field a body assigns goes with a SETTER that assigns it on the + * registry. + * + * The module imports `registry.ts` for types only, so the two modules form no + * evaluation cycle. `index.ts` re-exports nothing from this module. + */ + +import { ObjectSchema } from '@objectstack/spec/data'; +import { ITEM_KEY_DISCRIMINATORS } from '@objectstack/metadata-core'; +import { InstalledPackageSchema, ManifestSchema } from '@objectstack/spec/kernel'; +import type { ObjectStackManifest } from '@objectstack/spec/kernel'; +import { AppSchema } from '@objectstack/spec/ui'; +import { declaredSecurityCatalogNames } from './security-catalog-namespace.js'; +import { declaredBareNames, isBareNameRegistryType } from './bare-name-one-holder.js'; +import { + collectBundle, entriesOfMember, isStoredActionRow, overlaySlotEntriesOf, packageBodySlot, packagelessActionOwnerKey, + packagelessActionSlotEntriesOf, packagelessActionSlotKey, +} from './registry-top-level.js'; +import type { SchemaRegistry } from './registry.js'; + +/** + * The `SchemaRegistry` members the bodies of {@link ItemsFacet} read — every one of + * them, and no other. Each is typed AS the class member (an indexed access on + * the class), so a change to a member's signature on the class is a change + * here too. `readonly` members are the getters; the others are fields a body + * assigns. + */ +export interface ItemsHost { + readonly bareNameClaims: SchemaRegistry['bareNameClaims']; + getObject: SchemaRegistry['getObject']; + invalidateAll: SchemaRegistry['invalidateAll']; + log: SchemaRegistry['log']; + readonly metadata: SchemaRegistry['metadata']; + noteBareNameClaim: SchemaRegistry['noteBareNameClaim']; + noteItem: SchemaRegistry['noteItem']; + noteItemOrder: SchemaRegistry['noteItemOrder']; + noteSecurityCatalogClaim: SchemaRegistry['noteSecurityCatalogClaim']; + openPackageBody: SchemaRegistry['openPackageBody']; + orderedOverlayTier: SchemaRegistry['orderedOverlayTier']; + readonly packageBodyItems: SchemaRegistry['packageBodyItems']; + readonly restorePoint: SchemaRegistry['restorePoint']; + readonly securityCatalogClaims: SchemaRegistry['securityCatalogClaims']; + unregisterPicklistExtensionsByPackage: SchemaRegistry['unregisterPicklistExtensionsByPackage']; + withdrawPackageBodyItemsNotShipped: SchemaRegistry['withdrawPackageBodyItemsNotShipped']; + withdrawPackageClaimsNotDeclared: SchemaRegistry['withdrawPackageClaimsNotDeclared']; +} + +/** The bodies of the "Generic metadata items and package bodies" section of `SchemaRegistry`: a stateless facet, called by the class's stubs. */ +export class ItemsFacet { + /** + * [ADR-0150 D4] Before a package-less action `incoming` takes the bare slot + * of `name`, the name's package-less actions it does not replace keep their + * own owner keys: the bare slot's present item, when its owner key is + * another, moves to that key's own slot ({@link packagelessActionSlotKey}). + * So the platform's action bound to `lead` and the environment's object-less + * action of the name are both kept, in either registration order, and each + * is read on its own key. + * + * What `incoming` replaces: the item of its own owner key (one key, one + * item; the later registration stays, as in the bare slot), and, when it is + * a stored row ({@link isStoredActionRow}), the name's stored row on any key + * — the environment's store holds one row of a name, so a row saved again + * bound to another object is that row, never a second item. + */ + static keepPackagelessActionOwnerKeys(this: Pick, collection: Map, name: string, incoming: unknown): void { + const ownerKey = packagelessActionOwnerKey(incoming); + const replaces = (item: unknown): boolean => + packagelessActionOwnerKey(item) === ownerKey || (isStoredActionRow(incoming) && isStoredActionRow(item)); + for (const { key, item } of packagelessActionSlotEntriesOf(collection, name)) { + if (!replaces(item)) continue; + this.noteItem('action', key); + collection.delete(key); + } + const occupant = collection.get(name); + if (occupant === undefined || replaces(occupant)) return; + const aside = packagelessActionSlotKey(packagelessActionOwnerKey(occupant), name); + this.noteItem('action', aside); + collection.set(aside, occupant); + this.log(`[Registry] Kept package-less action ${name} on ${packagelessActionOwnerKey(occupant)} beside the one on ${ownerKey}`); + } + + /** + * [ADR-0150 D4] The slot a removal of the package-less action `name`'s own + * entry retires ({@link removeRuntimeShadow}, {@link removeOverlayEntry}): + * the bare slot, unless another owner key's slot holds the name's one stored + * row ({@link isStoredActionRow}, the row the removal heals) while the bare + * slot holds code's. + */ + static packagelessActionEntryToRetire(collection: Map, name: string): string { + const aside = packagelessActionSlotEntriesOf(collection, name); + if (aside.length === 0 || isStoredActionRow(collection.get(name))) return name; + const rows = aside.filter(({ item }) => isStoredActionRow(item)); + return rows.length === 1 ? rows[0].key : name; + } + + /** + * [ADR-0150 D4] Retire one package-less action slot of `name`. When it is + * the bare slot, the first other owner key's item takes it, so the bare + * slot holds a package-less action of the name whenever one is kept. + */ + static retirePackagelessActionSlot(this: Pick, collection: Map, name: string, key: string): void { + collection.delete(key); + if (key !== name) return; + const [next] = packagelessActionSlotEntriesOf(collection, name); + if (next === undefined) return; + this.noteItem('action', next.key); + collection.delete(next.key); + this.noteItem('action', name); + collection.set(name, next.item); + } + + /** The body of `SchemaRegistry#validate`; the method's docblock there is its contract. */ + static validate(type: string, item: any): unknown { + if (type === 'object') { + return ObjectSchema.parse(item); + } + if (type === 'app') { + return AppSchema.parse(item); + } + if (type === 'package') { + return InstalledPackageSchema.parse(item); + } + if (type === 'plugin') { + return ManifestSchema.parse(item); + } + return true; + } + + /** The body of `SchemaRegistry#unregisterItemsByPackage`; the method's docblock there is its contract. */ + static unregisterItemsByPackage(this: Pick, packageId: string): { removed: string[]; orphanedOverlays: string[] } { + const prefix = `${packageId}:`; + const removed: string[] = []; + const orphanedOverlays: string[] = []; + + for (const [type, collection] of this.metadata.entries()) { + // Collect before deleting — mutating a Map while iterating its own keys + // is legal but reads as a trap, and the bare-slot probe below wants the + // collection in its post-removal state to be truthful. + const owned = [...collection.keys()].filter(key => key.startsWith(prefix)); + for (const key of owned) { + collection.delete(key); + removed.push(`${type}/${key}`); + this.log(`[Registry] Unregistered ${type}: ${key} (package ${packageId} uninstalled)`); + } + for (const key of owned) { + // The bare (overlay) slot for this item is its key without the package + // prefix — the same `bareKey` the artifact-vs-DB collision warning in + // `registerItem` compares against, discriminator included. + const bareSlot = key.slice(prefix.length); + const label = `${type}/${bareSlot}`; + if (collection.has(bareSlot) && !orphanedOverlays.includes(label)) { + orphanedOverlays.push(label); + } + } + } + + if (orphanedOverlays.length > 0) { + console.warn( + `[Registry] Package "${packageId}" was uninstalled, but ${orphanedOverlays.length} ` + + `runtime/DB overlay row(s) layered over its items were KEPT — they are tenant-authored ` + + `(ADR-0005) and an uninstall does not delete them: ${orphanedOverlays.join(', ')}. ` + + `Each now overlays nothing — re-install the package that owns it, or delete the ` + + `sys_metadata row.`, + ); + } + if (removed.length > 0) { + this.log(`[Registry] Unregistered ${removed.length} item(s) from package: ${packageId}`); + } + // The package's `picklistExtensions` leave with it, and every resolved + // object is re-derived without its lists and its added values. + const droppedExtensions = this.unregisterPicklistExtensionsByPackage(packageId); + // [#24400] …and so is every object its bound actions were attached to. + // [#28877] …and every object whose actions a flow of this package withheld. + if (droppedExtensions || removed.some((r) => r.startsWith('picklist/') || r.startsWith('action/') || r.startsWith('flow/'))) this.invalidateAll(); + return { removed, orphanedOverlays }; + } + + /** The body of `SchemaRegistry#replacingPackageBody`; the method's docblock there is its contract. */ + static replacingPackageBody(this: Pick, packageId: string | undefined, manifest: ObjectStackManifest, register: () => T): T { + if (!packageId) return register(); + const open = this.openPackageBody; + const body = { packageId, written: new Set() }; + this.openPackageBody = body; + let result: T; + try { + result = register(); + } finally { + this.openPackageBody = open; + } + const previous = this.packageBodyItems.get(packageId); + if (previous !== undefined) { + this.withdrawPackageBodyItemsNotShipped(packageId, previous, body.written); + this.withdrawPackageClaimsNotDeclared(packageId, manifest); + } + this.restorePoint?.note(`packageBody\u0000${packageId}`, () => { + const had = this.packageBodyItems.has(packageId); + const saved = this.packageBodyItems.get(packageId); + return () => { + if (had && saved !== undefined) this.packageBodyItems.set(packageId, saved); + else this.packageBodyItems.delete(packageId); + }; + }); + this.packageBodyItems.set(packageId, body.written); + // A body of the same package registered inside another one's run (none + // does today) ships its slots as part of that one. + if (open?.packageId === packageId) for (const slot of body.written) open.written.add(slot); + return result; + } + + /** The slots `previous` names that `written` does not, withdrawn (see {@link replacingPackageBody}). */ + static withdrawPackageBodyItemsNotShipped(this: Pick, packageId: string, previous: ReadonlySet, written: ReadonlySet): void { + const removed: string[] = []; + for (const slot of previous) { + if (written.has(slot)) continue; + const separator = slot.indexOf('\u0000'); + const type = slot.slice(0, separator); + const key = slot.slice(separator + 1); + const collection = this.metadata.get(type); + if (!collection?.has(key)) continue; + this.noteItemOrder(type); + this.noteItem(type, key); + collection.delete(key); + removed.push(`${type}/${key}`); + } + if (removed.length === 0) return; + this.log( + `[Registry] Withdrew ${removed.length} item(s) package ${packageId}'s new body no longer ships: ${removed.join(', ')}`, + ); + // The read caches {@link unregisterItemsByPackage} drops for the same types. + if (removed.some((r) => r.startsWith('picklist/') || r.startsWith('action/') || r.startsWith('flow/'))) this.invalidateAll(); + } + + /** + * The bare-name and security catalog claims `packageId` holds that + * `manifest` no longer declares, released (see {@link replacingPackageBody}). + * ⛔ Never a `function` claim: those are the hook binder's + * ({@link claimBareNameFunctions}), recorded and released by its own bind. + */ + static withdrawPackageClaimsNotDeclared(this: Pick, packageId: string, manifest: ObjectStackManifest): void { + const bare = new Set(declaredBareNames(manifest).map(({ type, name }) => packageBodySlot(type, name))); + for (const [type, byName] of this.bareNameClaims) { + if (!isBareNameRegistryType(type)) continue; + for (const [name, claimants] of byName) { + if (!claimants.has(packageId) || bare.has(packageBodySlot(type, name))) continue; + this.noteBareNameClaim(type, name); + claimants.delete(packageId); + if (claimants.size === 0) byName.delete(name); + } + } + const catalog = new Set(declaredSecurityCatalogNames(manifest).map(({ type, name }) => packageBodySlot(type, name))); + for (const [type, byName] of this.securityCatalogClaims) { + for (const [name, claimants] of byName) { + if (!claimants.has(packageId) || catalog.has(packageBodySlot(type, name))) continue; + this.noteSecurityCatalogClaim(type, name); + claimants.delete(packageId); + if (claimants.size === 0) byName.delete(name); + } + } + } + + /** The body of `SchemaRegistry#getItem`; the method's docblock there is its contract. */ + static getItem(this: Pick, type: string, name: string, currentPackageId?: string, member?: string): T | undefined { + // Special handling for 'object' and 'objects' types - use objectContributors + if (type === 'object' || type === 'objects') { + return this.getObject(name) as unknown as T | undefined; + } + + const collection = this.metadata.get(type); + if (!collection) return undefined; + + // [#7730] A discriminated type's name addresses a BUNDLE, so every tier + // below is a group rather than a single key. The tiers themselves are + // unchanged (overlay → prefer-local → first composite). + // + // [#22877] A bare-name read answers the CANONICAL member — the one the + // `/meta` by-name doors address when no member is named — wherever it + // sits in that tier order. It used to take the first tier that held ANY + // member and pick canonical-first only inside it, so an overlay of a + // second locale alone (a template customized in `zh-CN` only) answered a + // read that named no member. Only a bundle with no canonical member at + // all still answers its first member, as it always did. A named `member` + // answers that member, in the same tier order, or nothing. + const disc = ITEM_KEY_DISCRIMINATORS[type]; + // [#33127] The package overlay slots join the overlay tier, after the bare slot. + if (disc) { + const { bare, local, other } = collectBundle(collection, name, disc, currentPackageId); + const overlays = this.orderedOverlayTier(type, collection, name, overlaySlotEntriesOf(collection, name, disc), currentPackageId); + const tiers = [...bare, ...overlays, ...local, ...other]; + if (member !== undefined) return (entriesOfMember(tiers, disc, member)[0]?.item as T) ?? undefined; + return ((entriesOfMember(tiers, disc, disc.canonical)[0] ?? tiers[0])?.item as T) ?? undefined; + } + + // A bare-key entry (a runtime/DB overlay rehydrated by restoreMetadataFromDb) + // intentionally shadows the packaged composite item — ADR-0005 overlay + // precedence (a customization wins over its package default). This is + // checked before prefer-local so that precedence holds; note an env-wide + // (package-less) overlay of a name that collides across packages is + // inherently ambiguous by schema (sys_metadata is unique on type+name+org, + // not package) and resolves to the single overlay row. + const direct = collection.get(name); + if (direct) return direct as T; + // [#33127, ADR-0150 D4] …and with no package-less row, a stored row bound + // to a package (its package overlay slot): the caller's package's first, + // else in the holders' order ({@link orderedOverlayTier}), never in the + // order the rows were registered. One stored row reads as it did in the + // bare slot. + const overlays = overlaySlotEntriesOf(collection, name); + if (overlays.length > 0) return this.orderedOverlayTier(type, collection, name, overlays, currentPackageId)[0].item as T; + + // Prefer-local: resolve within the caller's package first. + if (currentPackageId) { + const local = collection.get(`${currentPackageId}:${name}`); + if (local) return local as T; + } + + // Fallback: first composite key matching the bare name (legacy behaviour). + for (const [key, item] of collection) { + if (key.endsWith(`:${name}`)) { + return item as T; + } + } + return undefined; + } +} diff --git a/packages/objectql/src/registry-namespaces-and-claims.ts b/packages/objectql/src/registry-namespaces-and-claims.ts new file mode 100644 index 00000000000..f7f832b9a13 --- /dev/null +++ b/packages/objectql/src/registry-namespaces-and-claims.ts @@ -0,0 +1,490 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33949 codemod] The + * "Namespaces, default record pages and security catalog names" section of + * `SchemaRegistry`: the bodies of `registerNamespace`, `unregisterNamespace`, + * `getNamespaceOwner`, `getNamespaceOwners`, `defaultRecordPageSlots`, + * `defaultRecordPageHoldersOf`, `otherDefaultRecordPageHolders`, + * `refuseDefaultRecordPageConflicts`, `recordDefaultRecordPageClaims`, + * `forgetDefaultRecordPageClaims`, `securityCatalogHoldersOtherThan`, + * `refuseSecurityCatalogNameConflicts`, `recordSecurityCatalogClaims`, + * `forgetSecurityCatalogClaims`, `securityCatalogNamesHeldBy`, + * `environmentHeldSecurityCatalogConflicts`, `packageHeldSecurityCatalogNames`, + * `securityCatalogPackageHolders` moved out of `registry.ts` whole by + * `scripts/codemod-split-registry.mjs`. + * + * A MOVE, not a redesign. {@link NamespacesAndClaimsFacet} is a stateless facet of `SchemaRegistry` + * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` + * method of the same name exactly as it stood: not re-indented, not + * formatted, not edited. The method stays on the class as a one-call stub with + * its signature, so the published surface, an instance spy and a subclass + * override are what they were. A public or protected method keeps its + * docblock on the class, the contract; a private method's docblock moved here + * with its body. The state stays on `SchemaRegistry`. + * + * The bodies reach the class through {@link NamespacesAndClaimsHost}: exactly + * the `SchemaRegistry` members they read, private ones included. Every stub + * hands its body, by a `.call`, the same host: `namespacesAndClaimsHost` in + * `registry-facet-hosts.ts`, built once per registry by the class's static + * block. Methods go as closures that call them on the registry at call time, so + * a subclass override or an instance spy is still the one reached; fields and + * accessors go as GETTERS, read when the body reads them, as they were on the + * class, and a field a body assigns goes with a SETTER that assigns it on the + * registry. + * + * Values that stayed in `registry.ts` are imported from it (lever (a) of + * ruling 6591753301); every use is inside a function body, so the import + * cycle is never read at evaluation time. `index.ts` re-exports nothing from + * this module. + */ + +import { isTenantAuthored } from '@objectstack/metadata-core'; +import type { ObjectStackManifest } from '@objectstack/spec/kernel'; +import { permissionSetHoldersMeet } from '@objectstack/core'; +import type { SecurityCatalogType } from '@objectstack/core'; +import { BUILT_IN_SECURITY_CATALOG_NAMES, ENVIRONMENT_HELD_SECURITY_CATALOG_TYPES, declaredSecurityCatalogNames, securityCatalogHolderKey } from './security-catalog-namespace.js'; +import type { SecurityCatalogHolder } from './security-catalog-namespace.js'; +import { declaredDefaultRecordPages, defaultRecordPageHolderKey, holdsRecordPageSlot, misplacedDefaultPages, recordPageSlotKey } from './default-record-page.js'; +import type { DefaultRecordPageHolder, RecordPageSlot } from './default-record-page.js'; +import type { DefaultRecordPageConflict, SecurityCatalogNameConflict } from './registry-top-level.js'; +import { + DefaultRecordPageConflictError, MisplacedDefaultPageError, SecurityCatalogNameConflictError, +} from './registry.js'; +import type { SchemaRegistry } from './registry.js'; + +/** + * The `SchemaRegistry` members the bodies of {@link NamespacesAndClaimsFacet} read — every one of + * them, and no other. Each is typed AS the class member (an indexed access on + * the class), so a change to a member's signature on the class is a change + * here too. `readonly` members are the getters; the others are fields a body + * assigns. + */ +export interface NamespacesAndClaimsHost { + bareNameHolderNamespace: SchemaRegistry['bareNameHolderNamespace']; + readonly defaultRecordPageClaims: SchemaRegistry['defaultRecordPageClaims']; + defaultRecordPageSlots: SchemaRegistry['defaultRecordPageSlots']; + forgetDefaultRecordPageClaims: SchemaRegistry['forgetDefaultRecordPageClaims']; + log: SchemaRegistry['log']; + readonly metadata: SchemaRegistry['metadata']; + readonly namespaceRegistry: SchemaRegistry['namespaceRegistry']; + noteDefaultRecordPageClaims: SchemaRegistry['noteDefaultRecordPageClaims']; + noteNamespace: SchemaRegistry['noteNamespace']; + noteSecurityCatalogClaim: SchemaRegistry['noteSecurityCatalogClaim']; + otherDefaultRecordPageHolders: SchemaRegistry['otherDefaultRecordPageHolders']; + packageHeldSecurityCatalogNames: SchemaRegistry['packageHeldSecurityCatalogNames']; + readonly securityCatalogClaims: SchemaRegistry['securityCatalogClaims']; + securityCatalogHoldersOtherThan: SchemaRegistry['securityCatalogHoldersOtherThan']; + securityCatalogPackageHolders: SchemaRegistry['securityCatalogPackageHolders']; +} + +/** The bodies of the "Namespaces, default record pages and security catalog names" section of `SchemaRegistry`: a stateless facet, called by the class's stubs. */ +export class NamespacesAndClaimsFacet { + /** The body of `SchemaRegistry#registerNamespace`; the method's docblock there is its contract. */ + static registerNamespace(this: Pick, namespace: string, packageId: string): void { + if (!namespace) return; + this.noteNamespace(namespace); + + let owners = this.namespaceRegistry.get(namespace); + if (!owners) { + owners = new Set(); + this.namespaceRegistry.set(namespace, owners); + } + owners.add(packageId); + this.log(`[Registry] Registered namespace: ${namespace} → ${packageId}`); + } + + /** The body of `SchemaRegistry#unregisterNamespace`; the method's docblock there is its contract. */ + static unregisterNamespace(this: Pick, namespace: string, packageId: string): void { + const owners = this.namespaceRegistry.get(namespace); + if (owners) { + this.noteNamespace(namespace); + owners.delete(packageId); + if (owners.size === 0) { + this.namespaceRegistry.delete(namespace); + } + this.log(`[Registry] Unregistered namespace: ${namespace} ← ${packageId}`); + } + } + + /** The body of `SchemaRegistry#getNamespaceOwner`; the method's docblock there is its contract. */ + static getNamespaceOwner(this: Pick, namespace: string): string | undefined { + const owners = this.namespaceRegistry.get(namespace); + if (!owners || owners.size === 0) return undefined; + // Return the first registered package for backwards compatibility + return owners.values().next().value; + } + + /** The body of `SchemaRegistry#getNamespaceOwners`; the method's docblock there is its contract. */ + static getNamespaceOwners(this: Pick, namespace: string): string[] { + const owners = this.namespaceRegistry.get(namespace); + return owners ? Array.from(owners) : []; + } + + /** + * Every page holding the default record page of `object`, each tagged with + * whether it holds through the registry's bare slot (an environment item, + * which stands in for every slot of its name — module doc, "What is not a + * second default"). + * + * Read, per page name: the bare slot when there is one — its own body + * decides, and the composite slots and claims of that name are shadowed by + * it; otherwise every composite `PACKAGE:NAME` slot, and every install claim. + * A bare-slot item carrying a real package stamp is that package's page as + * overlaid in this environment (the protocol grafts the package's envelope + * onto an overlay of a packaged page), and is named as that package's page. + * A disabled package is still installed, and its pages still hold. + */ + static defaultRecordPageSlots(this: Pick, slot: RecordPageSlot): Array<{ holder: DefaultRecordPageHolder; bare: boolean }> { + const found = new Map(); + const add = (holder: DefaultRecordPageHolder, bare: boolean) => { + found.set(defaultRecordPageHolderKey(holder), { holder, bare }); + }; + const bareNames = new Set(); + const composites: Array<{ packageId: string; page: string; item: unknown }> = []; + for (const [key, item] of this.metadata.get('page') ?? []) { + const sep = key.lastIndexOf(':'); + const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; + if (sep < 0) { + bareNames.add(key); + if (!holdsRecordPageSlot(item, slot)) continue; + const packaged = + typeof stamped === 'string' && stamped !== '' && stamped !== 'sys_metadata' && !isTenantAuthored(item); + add(packaged ? { kind: 'package', packageId: stamped, page: key } : { kind: 'environment', page: key }, true); + } else { + composites.push({ + packageId: typeof stamped === 'string' && stamped !== '' ? stamped : key.slice(0, sep), + page: key.slice(sep + 1), + item, + }); + } + } + for (const { packageId, page, item } of composites) { + if (bareNames.has(page) || !holdsRecordPageSlot(item, slot)) continue; + add({ kind: 'package', packageId, page }, false); + } + for (const [packageId, page] of this.defaultRecordPageClaims.get(recordPageSlotKey(slot)) ?? []) { + if (bareNames.has(page)) continue; + add({ kind: 'package', packageId, page }, false); + } + return [...found.values()]; + } + + /** The body of `SchemaRegistry#defaultRecordPageHoldersOf`; the method's docblock there is its contract. */ + static defaultRecordPageHoldersOf(this: Pick, object: string, app?: string): DefaultRecordPageHolder[] { + return this.defaultRecordPageSlots(app === undefined ? { object } : { object, app }).map(({ holder }) => holder); + } + + /** + * The holders of `object`'s default record page that a package registering + * `page` as that default would meet as a SECOND default — every holder but + * the package's own pages and the bare-slot item of the same name (the + * environment's overlay of the very page being registered). + */ + static otherDefaultRecordPageHolders(this: Pick, slot: RecordPageSlot, page: string, packageId: string): DefaultRecordPageHolder[] { + return this.defaultRecordPageSlots(slot) + .filter(({ holder, bare }) => { + if (holder.kind === 'package' && holder.packageId === packageId) return false; + return !(bare && holder.page === page); + }) + .map(({ holder }) => holder); + } + + /** + * The package door's half of the rule: refuse a package whose declared + * default record pages meet another holder — a second page of the same + * manifest for the same object, or a page another package or the environment + * holds — ahead of every mutation {@link installPackage} makes, so a refused + * package leaves no record, no namespace ownership and no claim behind. + * + * Every conflict is collected, so one refusal reports all of them. A package + * with no identity (`artifactPackageId` answers nothing) registers under no + * package at all and has nothing to hold; it is not judged here. + * + * @throws {DefaultRecordPageConflictError} ADR-0112 envelope, naming both + * pages of each conflict. + */ + static refuseDefaultRecordPageConflicts(this: Pick, manifest: ObjectStackManifest, selfId: string | undefined): void { + // `isDefault: true` on a page that is not a record page bound to an object + // is refused on its own account first (maintainer ruling A on + // objectstack#23610) — whatever the package's identity, since the page + // itself, not a holder, is what is wrong. + const misplaced = misplacedDefaultPages(manifest); + if (misplaced.length > 0) throw new MisplacedDefaultPageError(selfId, misplaced); + if (selfId === undefined) return; + const conflicts: DefaultRecordPageConflict[] = []; + // slot → the first page this manifest declares for it. + const firstDeclared = new Map(); + for (const { page, ...slot } of declaredDefaultRecordPages(manifest)) { + const incoming: DefaultRecordPageHolder = { kind: 'package', packageId: selfId, page }; + const key = recordPageSlotKey(slot); + const first = firstDeclared.get(key); + if (first === undefined) { + firstDeclared.set(key, page); + for (const existing of this.otherDefaultRecordPageHolders(slot, page, selfId)) { + conflicts.push({ ...slot, incoming, existing }); + } + } else if (first !== page) { + // Two pages of one manifest for one slot. One page declared twice + // under one name is one page (the stack contract refuses the repeat). + conflicts.push({ ...slot, incoming, existing: { kind: 'package', packageId: selfId, page: first } }); + } + } + if (conflicts.length > 0) throw new DefaultRecordPageConflictError(conflicts); + } + + /** + * Replace `packageId`'s claims with what `manifest` declares (see + * {@link defaultRecordPageClaims} for why a re-install replaces). + */ + static recordDefaultRecordPageClaims(this: Pick, packageId: string, manifest: ObjectStackManifest): void { + this.noteDefaultRecordPageClaims(packageId); + this.forgetDefaultRecordPageClaims(packageId); + for (const { page, ...slot } of declaredDefaultRecordPages(manifest)) { + const key = recordPageSlotKey(slot); + let byPackage = this.defaultRecordPageClaims.get(key); + if (!byPackage) { + byPackage = new Map(); + this.defaultRecordPageClaims.set(key, byPackage); + } + if (!byPackage.has(packageId)) byPackage.set(packageId, page); + } + } + + /** Drop every default record page claim `packageId` holds. */ + static forgetDefaultRecordPageClaims(this: Pick, packageId: string): void { + for (const [object, byPackage] of this.defaultRecordPageClaims) { + byPackage.delete(packageId); + if (byPackage.size === 0) this.defaultRecordPageClaims.delete(object); + } + } + + /** + * Every holder of `(type, name)` other than `exceptPackageId` — the ONE + * predicate both refusals ({@link refuseSecurityCatalogNameConflicts} at the + * package door, the item seam in {@link registerItem}) ask. + * + * Read, in this order: a built-in (only when `builtIns` — see + * {@link BUILT_IN_SECURITY_CATALOG_NAMES} for why the item seam does not ask); + * the bare slot (an override a package bound to itself, else an + * environment-authored item — the latter only when `environment`); every + * composite `:` slot; the package claims. A package that + * holds the name more than one way is one holder. A disabled package is still + * installed, and still holds its names. + * + * [ADR-0150 D2/D9, #31698] For a permission set, a package holder counts only + * when it meets the incoming package under {@link permissionSetHoldersMeet} + * (`incomingNamespace`, the namespace the incoming package declares): two + * packages of two namespaces each hold their own set of the name. Positions + * and capabilities keep one holder per name. + */ + static securityCatalogHoldersOtherThan(this: Pick, type: SecurityCatalogType, name: string, exceptPackageId: string | undefined, opts: { builtIns: boolean; environment: boolean; incomingNamespace: string | undefined }): SecurityCatalogHolder[] { + const found = new Map(); + const add = (holder: SecurityCatalogHolder) => { + if (holder.kind === 'package' && holder.packageId === exceptPackageId) return; + if (holder.kind === 'package' && type === 'permission' + && !permissionSetHoldersMeet(opts.incomingNamespace, this.bareNameHolderNamespace(holder.packageId), name)) return; + found.set(securityCatalogHolderKey(holder), holder); + }; + if (opts.builtIns && BUILT_IN_SECURITY_CATALOG_NAMES[type].has(name)) add({ kind: 'built-in' }); + const suffix = `:${name}`; + for (const [key, item] of this.metadata.get(type) ?? []) { + const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; + if (key === name) { + if (typeof stamped === 'string' && stamped !== '') add({ kind: 'package', packageId: stamped }); + else if (opts.environment) add({ kind: 'environment' }); + } else if (key.endsWith(suffix)) { + add({ + kind: 'package', + packageId: typeof stamped === 'string' && stamped !== '' ? stamped : key.slice(0, -suffix.length), + }); + } + } + for (const claimant of this.securityCatalogClaims.get(type)?.get(name) ?? []) add({ kind: 'package', packageId: claimant }); + return [...found.values()]; + } + + /** + * The package door's half of the rule: refuse a package whose declared + * positions, permission sets or capabilities name something another holder + * already holds — built-ins included — ahead of every mutation + * {@link installPackage} makes, so a refused package leaves no record, no + * namespace ownership and no claim behind (the namespace gate's disposition). + * + * Every conflict is collected, so one refusal reports all of them. A package + * with no identity (`artifactPackageId` answers nothing) registers under no + * package at all and has nothing to hold; it is not judged here. + * + * @throws {SecurityCatalogNameConflictError} ADR-0112 envelope, naming both + * holders of each conflicting name. + */ + static refuseSecurityCatalogNameConflicts(this: Pick, manifest: ObjectStackManifest, selfId: string | undefined): void { + if (selfId === undefined) return; + const conflicts: SecurityCatalogNameConflict[] = []; + const seen = new Set(); + const incomingNamespace = typeof manifest.namespace === 'string' && manifest.namespace !== '' ? manifest.namespace : undefined; + for (const { type, name } of declaredSecurityCatalogNames(manifest)) { + // One package declaring one name twice is one holder (an in-package + // repeat is the package's own business, and not this rule's). + const key = `${type}|${name}`; + if (seen.has(key)) continue; + seen.add(key); + for (const existingHolder of this.securityCatalogHoldersOtherThan(type, name, selfId, { builtIns: true, environment: true, incomingNamespace })) { + conflicts.push({ catalogType: type, name, incomingPackageId: selfId, existingHolder }); + } + } + if (conflicts.length > 0) throw new SecurityCatalogNameConflictError(conflicts); + } + + /** + * Record `packageId`'s claims. Additive, like the item store beside it: a + * re-install does not unregister the items an earlier install of the same + * package registered, so it does not release their names either — and a + * re-install whose manifest carries no collections at all (`POST + * /api/v1/packages` stores the manifest alone) must not strip the names the + * package's code still declares. {@link uninstallPackage} is what releases + * them. + */ + static recordSecurityCatalogClaims(this: Pick, packageId: string, manifest: ObjectStackManifest): void { + for (const { type, name } of declaredSecurityCatalogNames(manifest)) { + let byName = this.securityCatalogClaims.get(type); + if (!byName) { + byName = new Map(); + this.securityCatalogClaims.set(type, byName); + } + this.noteSecurityCatalogClaim(type, name); + let claimants = byName.get(name); + if (!claimants) byName.set(name, claimants = new Set()); + claimants.add(packageId); + } + } + + /** Drop every claim `packageId` holds. */ + static forgetSecurityCatalogClaims(this: Pick, packageId: string): void { + for (const byName of this.securityCatalogClaims.values()) { + for (const [name, claimants] of byName) { + claimants.delete(packageId); + if (claimants.size === 0) byName.delete(name); + } + } + } + + /** The body of `SchemaRegistry#securityCatalogNamesHeldBy`; the method's docblock there is its contract. */ + static securityCatalogNamesHeldBy(this: Pick, packageId: string, type: SecurityCatalogType): string[] { + const names = new Set(); + for (const [name, claimants] of this.securityCatalogClaims.get(type) ?? []) { + if (claimants.has(packageId)) names.add(name); + } + const prefix = `${packageId}:`; + for (const [key, item] of this.metadata.get(type) ?? []) { + if (key.startsWith(prefix)) names.add(key.slice(prefix.length)); + else if ((item as { _packageId?: unknown } | null | undefined)?._packageId === packageId) names.add(key); + } + return [...names].sort(); + } + + /** + * The cold-boot half of the rule (`security-catalog-namespace.ts`, "The cold + * boot"; maintainer ruling letter A on #22307, record 6063176077): every + * package-held position and permission-set name the environment catalog also + * holds, as the conflicts {@link SecurityCatalogNameConflictError} reports — + * the package that declares the name as the incoming side, the environment + * catalog as the holder. Read-only: it records nothing and refuses nothing; + * the engine plugin asks it once `sys_metadata` has hydrated, and refuses the + * boot on any answer. + * + * - **The environment's names** are the bare-slot items of the two types the + * environment can author ({@link ENVIRONMENT_HELD_SECURITY_CATALOG_TYPES}). + * Every bare-slot item is the environment's whatever `_packageId` it wears: + * only a registration with no package writes the bare slot, and at a cold + * boot hydration grafts the package's envelope onto the stored row (the + * protocol's artifact-protection merge), so the stamp names the very + * package the row collides with. + * - **A package holds a name** through an item registered under it (its + * composite slot) or its install claim — never through the bare slot. + * - **Built-in names are skipped**, as the item seam skips the environment + * holder for them: the platform declares its built-in positions itself + * (after this check, in its own `start()`), and an environment item under a + * built-in name is a stored definition that shadows the declaration at + * read (ADR-0005) — a legitimate shadow, not a second holder. + * + * Sorted by type, then name, so two boots of one database report alike. + * + * Computed as {@link packageHeldSecurityCatalogNames} met with the bare slot, + * so the package half is ONE reading: the cold boot asks it here, and + * `os migrate security-catalog-overlays` asks it through the exported + * {@link findPackageHeldSecurityCatalogNames} and meets it with the stored rows + * (maintainer ruling letter B on #22371, record 6074838935). + * + * Private, like the rest of this rule's registry half; the engine plugin, + * which owns the boot sequence it is asked in, reaches it through the + * module-level {@link findEnvironmentHeldSecurityCatalogNames}, which the + * package entries do not re-export. + */ + static environmentHeldSecurityCatalogConflicts(this: Pick): SecurityCatalogNameConflict[] { + const conflicts: SecurityCatalogNameConflict[] = []; + for (const { catalogType, name, packageIds } of this.packageHeldSecurityCatalogNames()) { + // The environment's item: the bare slot, whatever `_packageId` it wears. + // A held name never contains `:` and is never a built-in, so this is the + // bare-slot reading the method header states. + if (!this.metadata.get(catalogType)?.has(name)) continue; + for (const packageId of packageIds) { + conflicts.push({ catalogType, name, incomingPackageId: packageId, existingHolder: { kind: 'environment' } }); + } + } + return conflicts; + } + + /** + * Every position and permission-set name a package holds, with its holders — + * the package half of {@link environmentHeldSecurityCatalogConflicts}, and the + * reading {@link findPackageHeldSecurityCatalogNames} exports. + * + * The candidate names are every composite slot's name and every install + * claim of the two types the environment catalog can hold + * ({@link ENVIRONMENT_HELD_SECURITY_CATALOG_TYPES}); each one's holders are + * {@link securityCatalogPackageHolders}'s answer, the one predicate. Built-in + * names are skipped, as the cold boot skips them (see the method above). + * Sorted by type in that list's order, then name. + * + * Also read BY NAME from `@objectstack/metadata-protocol` + * (`protocol-save-door-refusals.ts`, `registryPackageHoldersOf`): renaming + * this member means updating that caller and + * `protocol-permission-set-name-registry-reading.test.ts`. + */ + static packageHeldSecurityCatalogNames(this: Pick): Array<{ catalogType: SecurityCatalogType; name: string; packageIds: string[] }> { + const held: Array<{ catalogType: SecurityCatalogType; name: string; packageIds: string[] }> = []; + for (const type of ENVIRONMENT_HELD_SECURITY_CATALOG_TYPES) { + const candidates = new Set(); + for (const key of this.metadata.get(type)?.keys() ?? []) { + const sep = key.lastIndexOf(':'); + if (sep > 0) candidates.add(key.slice(sep + 1)); + } + for (const name of this.securityCatalogClaims.get(type)?.keys() ?? []) candidates.add(name); + for (const name of [...candidates].sort()) { + if (name === '' || BUILT_IN_SECURITY_CATALOG_NAMES[type].has(name)) continue; + const packageIds = this.securityCatalogPackageHolders(type, name); + if (packageIds.length > 0) held.push({ catalogType: type, name, packageIds }); + } + } + return held; + } + + /** + * The packages holding `(type, name)` through an item registered under them + * (a composite `:` slot) or an install claim — the package + * half of {@link securityCatalogHoldersOtherThan}'s reading, without the bare + * slot, whose stamp is not a claim (see {@link environmentHeldSecurityCatalogConflicts}). + */ + static securityCatalogPackageHolders(this: Pick, type: SecurityCatalogType, name: string): string[] { + const holders = new Set(); + const suffix = `:${name}`; + for (const [key, item] of this.metadata.get(type) ?? []) { + if (key === name || !key.endsWith(suffix)) continue; + const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; + holders.add(typeof stamped === 'string' && stamped !== '' ? stamped : key.slice(0, -suffix.length)); + } + for (const claimant of this.securityCatalogClaims.get(type)?.get(name) ?? []) holders.add(claimant); + return [...holders]; + } +} diff --git a/packages/objectql/src/registry-object-registration.ts b/packages/objectql/src/registry-object-registration.ts new file mode 100644 index 00000000000..690c43145d4 --- /dev/null +++ b/packages/objectql/src/registry-object-registration.ts @@ -0,0 +1,1638 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33949 codemod] The "Object registration (ownership model)" section of + * `SchemaRegistry`: the bodies of `objectRegistrationRefusal`, + * `judgeRegistrationDoors`, `recordCurrencyFollowRefusalOf`, + * `servedFoldWithLayer`, `followDoorSlotPriority`, `registerObject`, + * `resolveObject`, `getStorageObject`, `getAllStorageObjects`, + * `resolveStorageObject`, `withholdDisabledExtensions`, + * `resolveObjectLeavingExtensionsOut`, `foldExtenders`, + * `foldExtendersOntoDefinition`, `reportRedeclaredFields`, + * `reportFieldPatchMisses`, `withoutExtensionQueueField`, `finishServedObject`, + * `attachBoundActions`, `boundServedActions`, `boundCodeActions`, + * `findUnboundActionTargets`, `tenantAuthoredScalars`, + * `tenantPatchedDisplayKeys`, `tenantRuleSettings`, `withPackagedOwnerRules`, + * `packagedRules`, `tenantRuleCopyRefusal`, `reportChangedRuleCopies`, + * `foldObjectExtendersOnto`, `materializeBaseLayer`, + * `materializeServedObjectOnto`, `declinedBaseLayerStamps`, + * `provisionSearchCompanionOnto`, `stripProvisionedSearchCompanionFrom`, + * `stripMaterializedStampsFrom` moved out of `registry.ts` whole by + * `scripts/codemod-split-registry.mjs`. + * + * A MOVE, not a redesign. {@link ObjectRegistrationFacet} is a stateless facet of `SchemaRegistry` + * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` + * method of the same name exactly as it stood: not re-indented, not + * formatted, not edited. The method stays on the class as a one-call stub with + * its signature, so the published surface, an instance spy and a subclass + * override are what they were. A public or protected method keeps its + * docblock on the class, the contract; a private method's docblock moved here + * with its body. The state stays on `SchemaRegistry`. + * + * The bodies reach the class through {@link ObjectRegistrationHost}: exactly + * the `SchemaRegistry` members they read, private ones included. Every stub + * hands its body, by a `.call`, the same host: `objectRegistrationHost` in + * `registry-facet-hosts.ts`, built once per registry by the class's static + * block. Methods go as closures that call them on the registry at call time, so + * a subclass override or an instance spy is still the one reached; fields and + * accessors go as GETTERS, read when the body reads them, as they were on the + * class, and a field a body assigns goes with a SETTER that assigns it on the + * registry. + * + * Values that stayed in `registry.ts` are imported from it (lever (a) of + * ruling 6591753301); every use is inside a function body, so the import + * cycle is never read at evaluation time. `index.ts` re-exports nothing from + * this module. + */ + +import { BASE_FIELD_DISPLAY_WHITELIST, isInjectedColumnDefinition, materializeTreeSelfReferences, provisionPrimary, resolveInjectedSystemColumns } from '@objectstack/spec/data'; +import type { ObjectOwnership, ServiceObject } from '@objectstack/spec/data'; +import { QUEUE_FIELD_DEF, applyAuditFieldGovernance, fieldPatchTargetMisses, isCodeArtifactBody, isTenantAuthored } from '@objectstack/metadata-core'; +import type { FieldPatchTargetMiss } from '@objectstack/metadata-core'; +import { scalarOverridesPackagedBase } from '@objectstack/spec/system'; +import { SEARCH_COMPANION_FIELD, provisionSearchCompanion } from './search-companion.js'; +import { applyProtection } from '@objectstack/spec/shared'; +import { isObjectLessActionKey, standaloneActionOwnerKey } from './action-governance.js'; +import { recordCurrencyFollowRefusal, recordCurrencySummaryRefusal } from './record-currency-aggregate-door.js'; +import type { RecordCurrencyFollowRefusedError } from './record-currency-aggregate-door.js'; +import { + DEFAULT_EXTENDER_PRIORITY, DEFAULT_OVERLAY_PRIORITY, DEFAULT_OWNER_PRIORITY, NO_PACKAGE_REFERRER, + OBJECT_FOLD_SCALAR_KEYS, QUEUE_FIELD, admittedFieldPatches, applySystemFields, changedPackagedRuleKeys, computeFQN, + displayKeyOverridesPackagedBase, fieldNamesOf, fieldPatchYieldKey, indexKeysOf, isLeftOutExtension, + isPackagelessActionSlotKey, isPlainRecord, leavesExtensionsOut, mergeObjectDefinitions, mergeValidationsByName, + optionByValue, ownFieldNamesOf, ownFieldsTarget, parseOverlaySlotKey, parsePackageObjectActions, picklistReferrerOf, + provisionTenantScopeIndex, reconcileManagedApiMethods, recordFieldOf, ruleNamesOf, sortActionsByOrder, + stableStringify, storedRowPackageOf, tenantRuleSettingsOf, validationRuleName, warnFunctionalCompleteness, + warnStrippedLegacyApiMethods, withTenantRuleSettings, withoutWithheldMembers, +} from './registry-top-level.js'; +import type { + BundleEntry, FoldedExtensionFields, FollowDoorLayer, ObjectContributor, ObjectExtensionBody, + ObjectExtensionFoldOptions, ObjectFoldScalarKey, PicklistReferrer, ServedObjectLayer, TenantRuleSettings, + UnboundActionTarget, WithheldExtensions, +} from './registry-top-level.js'; +import { + ObjectOwnershipConflictError, ValidationRuleCopyRefusedError, extendContributionRefusal, objectFieldTypeRefusal, + objectQueueColumnRefusal, +} from './registry.js'; +import type { SchemaRegistry } from './registry.js'; + +/** + * The `SchemaRegistry` members the bodies of {@link ObjectRegistrationFacet} read — every one of + * them, and no other. Each is typed AS the class member (an indexed access on + * the class), so a change to a member's signature on the class is a change + * here too. `readonly` members are the getters; the others are fields a body + * assigns. + */ +export interface ObjectRegistrationHost { + _objectRevision: SchemaRegistry['_objectRevision']; + applyDeploymentTenancy: SchemaRegistry['applyDeploymentTenancy']; + attachBoundActions: SchemaRegistry['attachBoundActions']; + readonly bareNameHoldersAreSealed: SchemaRegistry['bareNameHoldersAreSealed']; + boundCodeActions: SchemaRegistry['boundCodeActions']; + boundServedActions: SchemaRegistry['boundServedActions']; + debug: SchemaRegistry['debug']; + declinedBaseLayerStamps: SchemaRegistry['declinedBaseLayerStamps']; + readonly deploymentPlatformGlobalObjects: SchemaRegistry['deploymentPlatformGlobalObjects']; + extensionTargetKey: SchemaRegistry['extensionTargetKey']; + readonly fieldPatchMissesWarned: SchemaRegistry['fieldPatchMissesWarned']; + finishServedObject: SchemaRegistry['finishServedObject']; + foldExtenders: SchemaRegistry['foldExtenders']; + foldExtendersOntoDefinition: SchemaRegistry['foldExtendersOntoDefinition']; + followDoorSlotPriority: SchemaRegistry['followDoorSlotPriority']; + getAllObjects: SchemaRegistry['getAllObjects']; + getObject: SchemaRegistry['getObject']; + getObjectContributors: SchemaRegistry['getObjectContributors']; + getObjectOwner: SchemaRegistry['getObjectOwner']; + hasDisabledPackage: SchemaRegistry['hasDisabledPackage']; + isActionWithheldByDisabledPackage: SchemaRegistry['isActionWithheldByDisabledPackage']; + isPackageDisabled: SchemaRegistry['isPackageDisabled']; + judgeRegistrationDoors: SchemaRegistry['judgeRegistrationDoors']; + log: SchemaRegistry['log']; + materializeBaseLayer: SchemaRegistry['materializeBaseLayer']; + readonly mergedObjectCache: SchemaRegistry['mergedObjectCache']; + readonly metadata: SchemaRegistry['metadata']; + readonly multiTenant: SchemaRegistry['multiTenant']; + noteObjectEntry: SchemaRegistry['noteObjectEntry']; + readonly objectContributors: SchemaRegistry['objectContributors']; + openObjectEntry: SchemaRegistry['openObjectEntry']; + packagedRules: SchemaRegistry['packagedRules']; + rankedForPackage: SchemaRegistry['rankedForPackage']; + recordCurrencyFollowRefusalOf: SchemaRegistry['recordCurrencyFollowRefusalOf']; + readonly redeclaredFieldsWarned: SchemaRegistry['redeclaredFieldsWarned']; + refusePicklistItemArrival: SchemaRegistry['refusePicklistItemArrival']; + registerNamespace: SchemaRegistry['registerNamespace']; + reportChangedRuleCopies: SchemaRegistry['reportChangedRuleCopies']; + reportFieldPatchMisses: SchemaRegistry['reportFieldPatchMisses']; + reportRedeclaredFields: SchemaRegistry['reportRedeclaredFields']; + readonly reportedRuleCopyChanges: SchemaRegistry['reportedRuleCopyChanges']; + resolveObject: SchemaRegistry['resolveObject']; + resolveObjectKey: SchemaRegistry['resolveObjectKey']; + resolvePicklistFields: SchemaRegistry['resolvePicklistFields']; + resolveStorageObject: SchemaRegistry['resolveStorageObject']; + readonly restorePoint: SchemaRegistry['restorePoint']; + readonly searchCompanion: SchemaRegistry['searchCompanion']; + servedFoldWithLayer: SchemaRegistry['servedFoldWithLayer']; + readonly shadowedExtensionQueueWarned: SchemaRegistry['shadowedExtensionQueueWarned']; + stripDeploymentTenancyFrom: SchemaRegistry['stripDeploymentTenancyFrom']; + stripProvisionedPrimaryFrom: SchemaRegistry['stripProvisionedPrimaryFrom']; + stripProvisionedSearchCompanionFrom: SchemaRegistry['stripProvisionedSearchCompanionFrom']; + stripProvisionedTenantIndexFrom: SchemaRegistry['stripProvisionedTenantIndexFrom']; + subtractExtenderContributions: SchemaRegistry['subtractExtenderContributions']; + tenantAuthoredScalars: SchemaRegistry['tenantAuthoredScalars']; + tenantPatchedDisplayKeys: SchemaRegistry['tenantPatchedDisplayKeys']; + tenantRuleSettings: SchemaRegistry['tenantRuleSettings']; + withPackagedOwnerRules: SchemaRegistry['withPackagedOwnerRules']; + withholdDisabledExtensions: SchemaRegistry['withholdDisabledExtensions']; + withoutExtensionQueueField: SchemaRegistry['withoutExtensionQueueField']; +} + +/** The bodies of the "Object registration (ownership model)" section of `SchemaRegistry`: a stateless facet, called by the class's stubs. */ +export class ObjectRegistrationFacet { + /** The body of `SchemaRegistry#objectRegistrationRefusal`; the method's docblock there is its contract. */ + static objectRegistrationRefusal(this: Pick, schema: ServiceObject, packageId?: string, ownership: ObjectOwnership = 'own', options: { + /** + * [ADR-0144 D2 and D6, amended] The caller asks about a body whose unit + * is whole, so nothing it registers with can still change the other end + * of a pair, and it relays the refusal to its author. Two callers are: + * a SINGLE save or publish that makes the body live in the same breath + * (the producers are enumerated in `@objectstack/metadata-protocol`'s + * `registry-question.ts`), and a batch writer — a package duplicate, an + * unmanaged import, a package publish, the stored-metadata migration + * pass — asking of each row once all its rows are registered + * (`batch-object-registration.ts`, which answers a refused row in the + * batch's own result). The doors that read the registry's other objects + * are then asked too — D6's roll-up half and its follow door — once the + * cold boot has sealed the registry. A refused save is answered with + * the door's own envelope and stores nothing, instead of being stored + * and refused, logged, at the write-through. ⛔ Not for a row asked + * before its batch has registered: a later sibling can still complete + * the pair (#29673). + * + * The follow door has two askers, and {@link registerObject} is neither + * (seat ruling on the contract review of PR #32368): this option, and + * the end of a registration run on a sealed registry + * ({@link restoringOnRefusal}) — a running install or reload, which + * registers a package's `objectExtensions` after its `objects` and the + * packages of an artifact one after another, and hears the refusal as + * a thrown error with the run put back. The cold boot, the stored-row + * hydration and every other bare `registerObject` register the follow + * pair exactly as before. + */ + againstRegistry?: boolean; + } = {}): Error | null { + const againstRegistry = options.againstRegistry === true && this.bareNameHoldersAreSealed; + const door = this.judgeRegistrationDoors(schema, packageId, ownership, { bodyOnly: !againstRegistry, againstRegistry }); + return 'refusal' in door ? door.refusal : null; + } + + /** + * [#29673] The registration doors of {@link registerObject}, moved here + * whole so {@link objectRegistrationRefusal} asks exactly what the + * registration throws: the refusal, or the body to register (an `extend` + * body re-keyed to the object it extends). `bodyOnly` skips the doors + * that read the registry's other objects, and `againstRegistry` (a sealed + * registry asked about a body whose unit is whole) is the only way these + * doors ask the follow door (see {@link objectRegistrationRefusal}); a + * registration run asks it of every body it registered, at its end + * ({@link restoringOnRefusal}). + */ + static judgeRegistrationDoors(this: Pick, schema: ServiceObject, packageId: string | undefined, ownership: ObjectOwnership, opts: { bodyOnly?: boolean; againstRegistry?: boolean } = {}): { refusal: Error } | { schema: ServiceObject } { + // [#16319] ⭐ THE FIELD-`type` DOOR — first statement in the method, and + // its position is the whole point. + // + // MAINTAINER RULING 2026-09-10 (director seat batch #111 item 2): 「一个没写 + // type(或拼错)的字段 应该禁止加载」. This registry is the ONE choke point every + // metadata door passes — declared stacks, plugin/package manifests, + // `saveMetaItem`, the `sys_metadata` boot rehydration and raw + // `registerObject` calls — so one check here closes all of them at once, + // which is why the ruling put it here rather than at the four downstream + // guesses it also retires. + // + // ⛔ BEFORE `applySystemFields` and every other materialization below, so + // the object and field the refusal names are the AUTHOR's own and never a + // platform injection; and ⛔ before the contributor list is opened, so a + // refused declaration leaves no half-registered entry behind. + // + // ⛔ The whole object is refused; the offending field is never dropped on + // its own. See {@link ObjectFieldTypeRefusedError}. + const fieldTypeRefusal = objectFieldTypeRefusal(schema); + if (fieldTypeRefusal) return { refusal: fieldTypeRefusal }; + + // [ADR-0142 D3] Beside it, for its reason: an eligible object's own + // `queue` field is refused before the injection could be skipped for it — + // see {@link ObjectQueueColumnRefusedError}. A base layer only: an `extend` + // body is never planned on its own (#24314, below), so its `queue` is + // governed in the fold instead ({@link withoutExtensionQueueField}), + // whichever order the layers register in. + if (ownership !== 'extend') { + const queueRefusal = objectQueueColumnRefusal(schema); + if (queueRefusal) return { refusal: queueRefusal }; + } + + // [ADR-0095 D1 note; ruling E′ item 4] An `extend` contribution never sets + // the object's default row scope — see + // {@link ExtensionDefaultRowScopeRefusedError}. Here, beside the field-`type` + // door and for the same reason: before any materialization and before the + // contributor list is opened, so a refused contribution leaves nothing behind. + // [#25234 ruling A, refusal 10] Nor does it declare a membership key — see + // {@link ExtensionMembershipKeysRefusedError}. The same door, for the same + // reason, so a refused contribution leaves nothing behind. [#28574] Both + // are asked through {@link extendContributionRefusal}, the one copy the + // metadata save and publish doors also ask of an `object_extension` item. + if (ownership === 'extend') { + const extendRefusal = extendContributionRefusal(schema, packageId); + if (extendRefusal) return { refusal: extendRefusal }; + // [#28858] …filed under the key the object it extends is registered + // under — see {@link extensionTargetKey}. Every entry that registers an + // extension reaches this line: the manifest `objectExtensions` loop and + // a stored `object_extension` row ({@link registerObjectExtension}). + const target = this.extensionTargetKey(String(schema.name ?? ''), packageId); + if (target !== schema.name) schema = { ...schema, name: target }; + } + // [#29673] The pre-flight question stops here: the door below reads the + // registry's other objects, not the body alone. + if (opts.bodyOnly) return { schema }; + // [ADR-0144 D6] A `summary` roll-up that reads a `record`-mode amount — + // `sum` / `avg` / `min` / `max` of a child field whose rows each carry + // their own currency — is refused here, the door every metadata path + // passes, before any materialization: the pair is judged at whichever + // registration completes it. See `record-currency-aggregate-door.ts`. + const recordCurrencySummary = recordCurrencySummaryRefusal( + schema, + (name) => this.getObject(name), + () => this.getAllObjects(), + ); + if (recordCurrencySummary) return { refusal: recordCurrencySummary }; + // [ADR-0144 D2 and D6, amended] Beside it, for the same reason: a + // following amount whose parent holds no code field under that name, and a + // `record` roll-up over a child amount that follows nothing — the halves + // `defineStack` judges where a stack holds both objects, for the + // single-object save that holds one. Asked here only of a caller whose + // unit is whole and who relays the refusal (`againstRegistry`, which + // {@link objectRegistrationRefusal} grants only once the cold boot has + // sealed the registry): before the seal an `extend` contribution or the + // other end can still register and contradict the verdict (AGENTS.md, + // startup registry reads), and a multi-registration unit after it — + // `registerApp`'s objects before its `objectExtensions`, the packages of + // one artifact, a batch writer's rows in its own order — has the same + // hazard at each of its registrations. So `registerObject` never asks + // it. The unit does, once it is whole: a batch writer through this + // option, a registration run at its end + // ({@link recordCurrencyFollowRefusalOfRun}). + if (opts.againstRegistry === true && this.bareNameHoldersAreSealed) { + const recordCurrencyFollow = this.recordCurrencyFollowRefusalOf(schema, ownership, { packageId, before: null }); + if (recordCurrencyFollow) return { refusal: recordCurrencyFollow }; + } + return { schema }; + } + + /** + * [ADR-0144 D2 and D6, amended] The follow door's question of one layer, as + * `ownership`, against the registry as it stands: the one copy a single + * save ({@link judgeRegistrationDoors}) and a registration run + * ({@link recordCurrencyFollowRefusalOfRun}) ask. `all` answers the + * registered objects; a caller asking of several bodies reads them once. + * `lookup` answers one of them by name, and is the served object unless the + * caller reads an object another way ({@link followRefusalAsServed}). + * + * [#33593 ruling A, amended] The door judges the layer's object AS SERVED + * with the layer in its slot ({@link servedFoldWithLayer}) — never the bare + * body, so an `extend` contribution's following amount is judged as an own + * body's is (R1), and an owner judges the amounts the extensions already + * registered under it add (R3). An object no owner serves is no verdict + * (R2). `layer.before` is what the slot held before: a pair already refused + * with it in place is not this layer's to answer for (R7). + */ + static recordCurrencyFollowRefusalOf(this: Pick, schema: ServiceObject, ownership: ObjectOwnership, layer: FollowDoorLayer, all: () => readonly ServiceObject[] = () => this.getAllObjects(), lookup: (name: string) => ServiceObject | undefined = (name) => this.getObject(name)): RecordCurrencyFollowRefusedError | undefined { + const fqn = String(schema.name ?? ''); + const served = this.servedFoldWithLayer(fqn, ownership, layer.packageId, { + packageId: layer.packageId, + namespace: '', + ownership, + priority: layer.priority ?? this.followDoorSlotPriority(fqn, ownership, layer.packageId), + definition: { ...schema, name: fqn }, + }); + if (served === undefined) return undefined; + const before = this.servedFoldWithLayer(fqn, ownership, layer.packageId, layer.before ?? undefined); + return recordCurrencyFollowRefusal(served, { + lookup, + all, + extensionPackageOf: (name, field) => { + const key = this.resolveObjectKey(name); + if (key === undefined) return undefined; + // As the served fold reads the object ({@link resolveObject}): a + // disabled package's extension is withheld; the last to add wins. + const adders = this.getObjectContributors(key).filter((c) => c.ownership === 'extend' + && !this.isPackageDisabled(c.packageId) + && Object.prototype.hasOwnProperty.call((c.definition as { fields?: object }).fields ?? {}, field)); + return adders.length === 0 ? undefined : adders[adders.length - 1].packageId; + }, + }, before); + } + + /** + * [#33593 ruling R1] The object `fqn` as {@link resolveObject} would serve + * it with `layer` filling the slot `ownership` and `packageId` name (one + * owner, one overlay, one extension per package — the replacement rules + * {@link registerObject} applies), or with that slot empty when `layer` is + * `undefined`. `undefined` when no owner would serve it. Uncached: it is a + * question about a registry the caller has not written. + */ + static servedFoldWithLayer(this: Pick, fqn: string, ownership: ObjectOwnership, packageId: string | undefined, layer: ObjectContributor | undefined): ServiceObject | undefined { + const others = (this.objectContributors.get(fqn) ?? []).filter((c) => c.ownership !== ownership + || (ownership === 'extend' && c.packageId !== packageId)); + const contributors = layer === undefined ? others : [...others, layer].sort((a, b) => a.priority - b.priority); + const owner = contributors.find((c) => c.ownership === 'own'); + if (!owner) return undefined; + const base = contributors.find((c) => c.ownership === 'overlay') ?? owner; + const withheld = this.withholdDisabledExtensions(contributors); + return withheld + ? this.foldExtendersOntoDefinition(withheld.kept, withoutWithheldMembers(base.definition, withheld)) + : this.foldExtenders(contributors, base); + } + + /** The priority a layer about to register in the slot would take: the sitting one's, else the kind's default. */ + static followDoorSlotPriority(this: Pick, fqn: string, ownership: ObjectOwnership, packageId: string | undefined): number { + const sitting = (this.objectContributors.get(fqn) ?? []).find((c) => c.ownership === ownership + && (ownership !== 'extend' || c.packageId === packageId)); + if (sitting) return sitting.priority; + return ownership === 'own' ? DEFAULT_OWNER_PRIORITY : ownership === 'overlay' ? DEFAULT_OVERLAY_PRIORITY : DEFAULT_EXTENDER_PRIORITY; + } + + /** The body of `SchemaRegistry#registerObject`; the method's docblock there is its contract. */ + static registerObject(this: Pick, schema: ServiceObject, packageId?: string, namespace?: string, ownership: ObjectOwnership = 'own', priority: number = ownership === 'own' + ? DEFAULT_OWNER_PRIORITY + : ownership === 'overlay' + ? DEFAULT_OVERLAY_PRIORITY + : DEFAULT_EXTENDER_PRIORITY): string { + // [#29673] The registration DOORS — every refusal this method answers + // before any materialization and before the contributor list is opened — + // live in {@link judgeRegistrationDoors}, so the metadata doors that make + // a body live can ask the same question BEFORE they promote it + // ({@link objectRegistrationRefusal}) instead of meeting the throw after + // the row is already active. Still the first statement here, and the + // position is still the whole point (the comments inside say why). + const door = this.judgeRegistrationDoors(schema, packageId, ownership); + if ('refusal' in door) throw door.refusal; + schema = door.schema; + // The body as the doors judged it, before anything below materializes it: + // what an open registration run is asked about at its end (below, where + // the layer is stored). + const judged = schema; + + // [ADR-0150 D3, #31700; epic seat ruling R2-A, 6812862622] After the + // boot sealed the picklist vocabulary, a packaged body registered on its + // own (outside an install judged as a whole: a plugin or the metadata + // service registering through the registry directly, a reloaded object) + // meets the one re-judge before its layer is stored: a field it writes + // must not leave another package's field of the same name writing to a + // different list ({@link refusePicklistItemArrival}). ⛔ Never an overlay + // or a tenant-authored body: it fills no package's slot and writes no + // package's reference, resolving as the environment's (`picklistReferrerOf`; + // swept for seat ruling R3-A). + if (packageId && (ownership === 'own' || ownership === 'extend') && !isTenantAuthored(schema)) { + this.refusePicklistItemArrival( + packageId, + ownership === 'own' + ? { objects: [schema] } + : { objectExtensions: [{ extend: schema.name, fields: (schema as { fields?: unknown }).fields }] }, + `object '${schema.name}' of package '${packageId}'`, + ); + } + + // [#25744] A code package's object keeps its own actions as their + // `ActionSchema` parse — `requiresFeature` / `requiresMembershipReach` + // lowered into `visible` — or is refused here, beside the doors above and + // for their reason: before any materialization and before the contributor + // list is opened, so a refused object leaves nothing behind and a refused + // re-registration leaves the registered one in place + // ({@link parsePackageObjectActions}). The test is `registerItem`'s action + // parse's, asked of the body as `applyProtection` below will stamp it (a + // package id it does not already carry, `'package'` provenance): ⛔ a + // registration with no `packageId`, the `sys_metadata` sentinel, or a + // tenant-authored body (`_provenance: 'org'` — the save door's write-through + // and the boot rehydration) is never judged here. + if (packageId && isCodeArtifactBody({ + _packageId: (schema as { _packageId?: unknown })._packageId ?? packageId, + _provenance: (schema as { _provenance?: unknown })._provenance ?? 'package', + })) { + schema = parsePackageObjectActions(schema, packageId); + } + + // [#23065] A `tree` field that names no `reference` references the object + // that declares it, and this is where that is written down for every door + // the parse does not cover: a metadata save stores the authored body + // verbatim and the `sys_metadata` rehydration and a raw `registerObject` + // never parse at all. `ObjectSchema`'s parse writes the same thing through + // the same spec helper, so the two doors cannot disagree, and every reader + // of the registered schema — the referential delete walk, the write-time + // reference check, `expand`, nested relation conditions — sees an ordinary + // self-reference. ⛔ No reader resolves it on its own. An extension + // registers under the object it extends (`name: ext.extend`), which is the + // object its trees reference. + const treeFields = materializeTreeSelfReferences(schema.name, schema.fields); + if (treeFields !== schema.fields) schema = { ...schema, fields: treeFields }; + + // Apply system-field injection (multi-tenant org_id, future owner/audit) + // BEFORE FQN computation and contributor storage so every consumer of + // the registered schema (driver syncSchema, REST projector, hooks) + // sees the same canonical shape. Author-declared fields win — see + // applySystemFields(). + // + // [#24314] A BASE layer only (`own` / `overlay`). An `extend` body is a + // bag of fields for an object someone else declares, so it is never + // planned on its own: it carries none of the declarations the plan reads + // (`managedBy`, `systemFields`, `tenancy`, `ownership`), and planning it + // as an ordinary object injected the full column set onto targets whose + // own declaration withholds it. Measured on a walled boot: an extension of + // `sys_organization` (`managedBy: 'better-auth'`) added `organization_id`, + // NULL on every row, and the wall then hid the organisation from its own + // members; the same on `sys_user`. The fold also let the injected + // definition REPLACE a column the owner declares itself: plugin-auth's + // own `sys_sso_provider` extension swapped the authored `organization_id` + // for the platform's lookup. So the extended object's system columns are + // exactly its base layer's plan, whichever order the layers register in. + if (ownership !== 'extend') { + schema = applySystemFields(schema, { + multiTenant: this.multiTenant, + platformGlobalObjects: this.deploymentPlatformGlobalObjects, + }); + } + + // [ADR-0092 / #1591] Reconcile generic-write `apiMethods` against the CRUD + // affordances a better-auth-managed object actually grants — strip verbs + // the identity write guard would reject anyway, so the HTTP exposure gate + // answers a clean 405 and the metadata can't contradict itself. No-op for + // every non-`better-auth` object. + schema = reconcileManagedApiMethods(schema); + + // [#3543] One-shot per-object diagnostic for retired LEGACY apiMethods + // values (the derived 8) — ignored by the derivation resolver; this adds + // the object-name context the parse-time strip warning lacks. Runs after + // reconcile so we diagnose what actually ships. + warnStrippedLegacyApiMethods(schema); + + // [ADR-0078 Phase 4] One-shot per-object functional-completeness + // diagnostic — the registry is the choke point every metadata door goes + // through, including the ones that skip Zod and lint (#3896, raw + // registerObject). Same shared predicate and rule ids as `os lint`; + // warn-never-throw (an inert FIELD must not kill the boot). + warnFunctionalCompleteness(schema); + + // [ADR-0079] Object-materialization seam — DESIGNATE-ONLY primary-title + // provisioning. Runs AFTER `applySystemFields` (so any designated field + // co-exists with the injected system columns) and ONLY for owned objects + // (extensions must not redesignate the owner's title). `synthesize: false` + // means: when a title-eligible field already EXISTS, set `nameField` to it + // (so `nameField` is reliably populated for normal/user/AI-built objects, + // which always carry a text label); when NOTHING is title-eligible, the + // object is left exactly as-is. We deliberately do NOT synthesize a `name` + // column here — that would materialize a new DB column on title-less + // system/append-only tables (sys_record_share, sys_member, …) via the + // driver's `syncSchema`, a schema-migration-bearing change. Those keep + // resolving their title on read via `resolveDisplayField` / `titleFormat`. + // + // [ADR-0029 D9, blast-radius row 2] The gate asks "is this a BASE LAYER", + // not "is this the owner". An `overlay` REPLACES the base at resolution + // time (D9.2), so the overlay body IS the resolved object — gating title + // provisioning on `own` alone would silently change `nameField` on every + // overlaid object, which is the same designation the packaged twin gets. + // Extensions still never redesignate: they merge into a base that has + // already been provisioned. + if (ownership === 'own' || ownership === 'overlay') { + schema = this.materializeBaseLayer(schema); + } + + const shortName = schema.name; + const fqn = computeFQN(namespace, shortName); + + // Ensure namespace is registered. [#12623] `packageId` is now optional + // on this method, so a namespace passed without one has no owner to + // record — skip rather than registering `undefined` as an owner (which + // would also mean widening `registerNamespace`'s own required-string + // contract, out of scope here). + // [#28697] Recorded before the first write below (the namespace claim), + // so a refusal raised after it — the ownership conflict — puts this + // object's layers back exactly ({@link restoringOnRefusal}). + this.noteObjectEntry(fqn); + + if (namespace && packageId) { + this.registerNamespace(namespace, packageId); + } + + // Get or create contributor list (#10945: the one creation choke point — + // it keeps `objectKeysByShortName` in step with `objectContributors`) + const contributors = this.openObjectEntry(fqn); + + // [#33593 ruling R7] What this layer's slot held, for the open run's end. + let replaced: ObjectContributor | null = null; + // Validate ownership rules + if (ownership === 'own') { + const existingOwner = contributors.find(c => c.ownership === 'own'); + if (existingOwner && isTenantAuthored(existingOwner.definition) && !isTenantAuthored(schema)) { + // ── LATE INSTALL (ADR-0029 D9 §6.1) ── + // + // A code package registers an object a TENANT row already holds. That + // happens whenever the hydration seams run before the package does — + // a `sys_metadata` row written on a kernel where nothing shipped the + // name, then a package that later ships it (install, upgrade, or a + // boot whose order puts `loadMetaFromDb` first). Before D9 the tenant + // row had taken the `own` slot by default, so this threw "already + // owned by" under a foreign id, or SPLICED THE TENANT'S BODY AWAY + // under the same id. + // + // D9's recommended default, and the only outcome that loses nothing: + // the CODE layer becomes the owner and the tenant contribution is + // re-classified as its overlay layer — exactly the two layers that + // would exist had the package registered first. The resolved object + // does not move (the tenant body is the base either way, D9.2); what + // changes is that the packaged definition now survives underneath it, + // so `isArtifactBacked` is honest from this moment on. + // + // Deliberately narrow: it fires ONLY when the sitting owner is + // tenant-authored (`_provenance: 'org'`) and the incoming body is not. + // Two code packages claiming one name is D3's cross-package refusal + // and still throws below, whatever the ids. + const staleOverlay = contributors.findIndex(c => c.ownership === 'overlay'); + if (staleOverlay !== -1) contributors.splice(staleOverlay, 1); + existingOwner.ownership = 'overlay'; + existingOwner.priority = DEFAULT_OVERLAY_PRIORITY; + this.log( + `[Registry] Late install of "${fqn}": package "${packageId}" takes ownership; ` + + `the tenant contribution (${existingOwner.packageId}) becomes its overlay layer.` + ); + } else if (existingOwner && existingOwner.packageId !== packageId) { + // [ADR-0029 D3] Two packages claiming one name — the cross-package + // refusal, carried as an ADR-0112 envelope (`code` + `status: 422`) + // with the message text unchanged. See {@link ObjectOwnershipConflictError}. + throw new ObjectOwnershipConflictError(fqn, existingOwner.packageId, packageId); + } else if (existingOwner) { + // Remove existing owner contribution from same package (re-registration). + // Normal path (metadata rebuild / HMR / multi-project seed replays the + // same owned object), not an error — keep it at debug so a stock boot + // stays warning-free (#3420). + contributors.splice(contributors.indexOf(existingOwner), 1); + replaced = existingOwner; + this.debug(`[Registry] Re-registering owned object: ${fqn} from ${packageId}`); + } + } else if (ownership === 'overlay') { + // [ADR-0029 D9.9] AT MOST ONE overlay layer per object name, replaced on + // re-write — and keyed by KIND, never by package. The row's `package_id` + // is provenance ON the layer, not a second identity for it: the + // overlay-uniqueness index in `sys_metadata` keys on + // `(type, name, organization_id, COALESCE(package_id, ''))` and can + // legitimately hold two rows for one `(type, name)` bound to two + // packages, which this registry could never represent because + // `computeFQN` is identity. Keying the replacement by package would let + // that unrepresentable shape in through the side door; the mis-bound row + // is refused at the PRODUCER instead (`saveMetaItem`), and counted in + // `loadMetaFromDb`'s per-record `errors` at boot. + const idx = contributors.findIndex(c => c.ownership === 'overlay'); + if (idx !== -1) { + replaced = contributors.splice(idx, 1)[0]; + this.debug(`[Registry] Replacing overlay layer of object: ${fqn} from ${packageId}`); + } + } else { + // extend mode: remove existing extension from same package + const idx = contributors.findIndex(c => c.packageId === packageId && c.ownership === 'extend'); + if (idx !== -1) { + replaced = contributors.splice(idx, 1)[0]; + } + } + + // ADR-0010 §3.7 — translate the author-facing `protection` block + // into the private `_lock` envelope and stamp package provenance + // on the schema before it lands in the contributor list. Mirrors + // registerItem() so object schemas surface lock fields on GET — including + // its note on the `_packageVersion` a manifest-holding caller stamps first. + applyProtection(schema as any, { packageId }); + + // Add new contributor + const contributor: ObjectContributor = { + packageId, + namespace: namespace || '', + ownership, + priority, + definition: { ...schema, name: fqn }, // Store with FQN as name + }; + contributors.push(contributor); + // [ADR-0144 D2 and D6, amended] …and told to the open registration run, + // under the slot the layer just filled (the replacement rules above: one + // owner, one overlay, one extension per package), so the run's end asks + // the record-currency follow door of it with the whole run registered + // ({@link restoringOnRefusal}). Outside a run nothing is recorded. + this.restorePoint?.noteRegisteredObject( + `${fqn}\u0000${ownership}${ownership === 'extend' ? `\u0000${packageId ?? ''}` : ''}`, + { fqn, schema: judged, contributor, replaced }, + ); + + // Sort by priority (ascending: lower priority applied first) + contributors.sort((a, b) => a.priority - b.priority); + + // Invalidate merge cache + this.mergedObjectCache.delete(fqn); + // …and tell registry-derived caches (the engine's roll-up summary index) + // that the object set moved, whichever path got here. + this._objectRevision += 1; + + this.log(`[Registry] Registered object: ${fqn} (${ownership}, priority=${priority}) from ${packageId}`); + return fqn; + } + + /** The body of `SchemaRegistry#resolveObject`; the method's docblock there is its contract. */ + static resolveObject(this: Pick, fqn: string): ServiceObject | undefined { + // Check cache first + const cached = this.mergedObjectCache.get(fqn); + if (cached) return cached; + + const contributors = this.objectContributors.get(fqn); + if (!contributors || contributors.length === 0) { + return undefined; + } + + // Find owner (must exist for a valid object) + const ownerContrib = contributors.find(c => c.ownership === 'own'); + if (!ownerContrib) { + // [ADR-0029 D9.5] An ORPHAN OVERLAY lands here too, and says so: the + // tenant can still see the row in `sys_metadata`, so "extenders" would + // be the wrong noun to hand whoever reads this line. + console.warn( + contributors.some(c => c.ownership === 'overlay') + ? `[Registry] Object "${fqn}" has an overlay layer but no owner. Skipping.` + : `[Registry] Object "${fqn}" has extenders but no owner. Skipping.` + ); + return undefined; + } + + // [ADR-0029 D9.2] The BASE layer is the tenant overlay when one is + // registered, and the owner otherwise — `overlay ?? own`. Selection asks + // the KIND (D9.3), never the priority. + // + // This is deliberately BIT-FOR-BIT what the pre-D9 splice produced: the + // overlay used to BE the owner, so the fold already ran over the overlay + // body. The resolved schema — `_provenance: 'org'` included, which every + // registry-direct consumer reads — does not move. Only what the registry + // REMEMBERS changes: the packaged owner is still here, underneath. + const baseContrib = contributors.find(c => c.ownership === 'overlay') ?? ownerContrib; + // A disabled package's `extend` contributions are withheld from the served + // fold ({@link withholdDisabledExtensions}); the cache is dropped by every + // enable and disable ({@link invalidateAll}). + const withheld = this.withholdDisabledExtensions(contributors); + const merged = withheld + ? this.foldExtendersOntoDefinition(withheld.kept, withoutWithheldMembers(baseContrib.definition, withheld)) + : this.foldExtenders(contributors, baseContrib); + + // Cache the result + this.mergedObjectCache.set(fqn, merged); + return merged; + } + + /** The body of `SchemaRegistry#getStorageObject`; the method's docblock there is its contract. */ + static getStorageObject(this: Pick, name: string): ServiceObject | undefined { + const fqn = this.resolveObjectKey(name); + return fqn === undefined ? undefined : this.resolveStorageObject(fqn); + } + + /** The body of `SchemaRegistry#getAllStorageObjects`; the method's docblock there is its contract. */ + static getAllStorageObjects(this: Pick): ServiceObject[] { + // Nothing disabled: the two layers are one answer, read through the one walk. + if (!this.hasDisabledPackage()) return this.getAllObjects(); + const results: ServiceObject[] = []; + for (const fqn of this.objectContributors.keys()) { + const merged = this.resolveStorageObject(fqn); + if (!merged) continue; + (merged as any)._packageId = this.getObjectOwner(fqn)?.packageId; + results.push(merged); + } + return results; + } + + /** See {@link getStorageObject}. Uncached when it differs from the served fold, as {@link resolveOwnerLayer} is. */ + static resolveStorageObject(this: Pick, fqn: string): ServiceObject | undefined { + const contributors = this.objectContributors.get(fqn); + if (!contributors || !contributors.some((c) => c.ownership === 'extend' && this.isPackageDisabled(c.packageId))) { + return this.resolveObject(fqn); + } + const ownerContrib = contributors.find(c => c.ownership === 'own'); + if (!ownerContrib) return this.resolveObject(fqn); + return this.foldExtenders(contributors, contributors.find(c => c.ownership === 'overlay') ?? ownerContrib); + } + + /** + * The `extend` contributions of a DISABLED package are withheld from every + * served fold of the object they extend, as the package's own objects, + * flows, navigation and actions are: "disabled" means hidden, refused on the + * data plane, data kept. The served object loses the fields, rules and + * indexes such an extension adds and the patches it makes, so the data + * plane's field map (its default projection and its unknown-field refusal) + * loses them too; the storage layer keeps them ({@link getStorageObject}). + * + * `withheldFields` / `withheldRules` / `withheldIndexes` are the members a + * withheld extension adds that neither the owner's own declaration nor a + * kept extension also declares (indexes compared structurally, as the fold's + * own index subtraction compares them). A base body that already carries them (a tenant overlay saved + * from a served read, a metadata-service copy seeded from a resolved body) + * has them taken off ({@link withoutWithheldMembers}), so where the base + * came from does not decide what a disable hides. + * + * `undefined` when no extension of the object is withheld, so the ordinary + * fold runs unchanged. + * + * [#30756] `asEnabled` names a package whose contributions are kept although + * it is disabled: the fold the enable of that package would leave + * ({@link ObjectExtensionFoldOptions}). + */ + static withholdDisabledExtensions(this: Pick, contributors: readonly ObjectContributor[], asEnabled?: string): WithheldExtensions | undefined { + const withheld = contributors.filter((c) => c.ownership === 'extend' + && this.isPackageDisabled(c.packageId) + && (asEnabled === undefined || c.packageId !== asEnabled)); + if (withheld.length === 0) return undefined; + const kept = contributors.filter((c) => !withheld.includes(c)); + const owner = contributors.find((c) => c.ownership === 'own')?.definition; + const keptFields = new Set(ownFieldNamesOf(owner) ?? []); + const keptRules = new Set(ruleNamesOf(owner)); + const keptIndexes = new Set(indexKeysOf(owner)); + for (const c of kept) { + if (c.ownership !== 'extend') continue; + for (const name of fieldNamesOf(c.definition)) keptFields.add(name); + for (const name of ruleNamesOf(c.definition)) keptRules.add(name); + for (const key of indexKeysOf(c.definition)) keptIndexes.add(key); + } + const withheldFields = new Set(); + const withheldRules = new Set(); + const withheldIndexes = new Set(); + for (const c of withheld) { + for (const name of fieldNamesOf(c.definition)) if (!keptFields.has(name)) withheldFields.add(name); + for (const name of ruleNamesOf(c.definition)) if (!keptRules.has(name)) withheldRules.add(name); + for (const key of indexKeysOf(c.definition)) if (!keptIndexes.has(key)) withheldIndexes.add(key); + } + return { kept, withheldFields, withheldRules, withheldIndexes }; + } + + /** + * [#29903, #29872, ADR-0146 D8 item 3] An object as {@link resolveObject} + * resolves it, but with the `extend` contributions `options` names left out + * of the fold ({@link ObjectExtensionFoldOptions}), as if those packages had + * never extended it: the same base layer (`overlay ?? own`), every other + * extender folded in the same order, the same tail. With no such + * contribution it IS {@link resolveObject}'s answer (the cached body). + * Uncached otherwise, as {@link resolveOwnerLayer} is. + * + * The reader is the protocol's per-package layer, which serves a package's + * objects to the package's own export as the package declares them: never + * with an extension another package contributes (its `.custom` pair's, or + * any other installed package's). That extension travels in its own + * package's export, as its `objectExtensions` entry + * ({@link getObjectExtensionsOf}); folded in here too, its field and field + * patches travel twice, the second time as if the owner shipped them. + */ + static resolveObjectLeavingExtensionsOut(this: Pick, fqn: string, options: ObjectExtensionFoldOptions | undefined): ServiceObject | undefined { + // [#30756] A disabled package folded in as if enabled; one that is not + // disabled changes nothing, so it does not leave the cached fold. + const asEnabled = options?.asEnabled !== undefined && this.isPackageDisabled(options.asEnabled) + ? options.asEnabled + : undefined; + const contributors = leavesExtensionsOut(options) || asEnabled !== undefined ? this.objectContributors.get(fqn) : undefined; + if (!contributors || !contributors.some((c) => isLeftOutExtension(c, options!) + || (asEnabled !== undefined && c.ownership === 'extend' && c.packageId === asEnabled))) return this.resolveObject(fqn); + const kept = contributors.filter((c) => !isLeftOutExtension(c, options!)); + const ownerContrib = kept.find(c => c.ownership === 'own'); + if (!ownerContrib) return undefined; + const base = kept.find(c => c.ownership === 'overlay') ?? ownerContrib; + // …and a disabled package's, as on every served fold ({@link withholdDisabledExtensions}), + // except the one `asEnabled` names. + const withheld = this.withholdDisabledExtensions(kept, asEnabled); + return withheld + ? this.foldExtendersOntoDefinition(withheld.kept, withoutWithheldMembers(base.definition, withheld)) + : this.foldExtenders(kept, base); + } + + /** + * Fold every `extend` contribution over a chosen base layer, in the list's + * (priority-sorted) order. Extracted from {@link resolveObject} so the + * code-layer resolution ({@link resolveOwnerLayer}) folds extenders exactly + * the same way rather than growing a second, drifting copy. + */ + static foldExtenders(this: Pick, contributors: ObjectContributor[], base: ObjectContributor, layer: ServedObjectLayer = 'served'): ServiceObject { + return this.foldExtendersOntoDefinition(contributors, base.definition, layer); + } + + /** + * The fold itself, over a base DEFINITION rather than a base contributor, so + * {@link foldObjectExtendersOnto} can apply it to a body that never came + * from this registry without growing a second copy of the merge. + */ + static foldExtendersOntoDefinition(this: Pick, contributors: ObjectContributor[], baseDefinition: ServiceObject, layer: ServedObjectLayer = 'served'): ServiceObject { + // [ADR-0029 D9.2a] Computed ONCE, over the base the fold starts from — never + // re-derived from the running `merged`, which would make an extender's own + // scalar look "authored" to the next extender and silently invert + // extender-vs-extender precedence (D9.3: declared numbers order peers). + const tenantAuthored = this.tenantAuthoredScalars(contributors, baseDefinition); + // [#24314] An `extend` body is not planned at registration, so an audit + // field it declares is governed HERE, by the base layer's plan: where the + // base carries the audit family, a `created_at` an extender declares cannot + // replace the platform's read-only definition with a writable one (#4447). + const governAudit = resolveInjectedSystemColumns(baseDefinition).audit; + // [ADR-0142 D3] The ownership-queue column, for the same reason: the + // registration door's refusal ({@link objectQueueColumnRefusal}) never sees + // an `extend` body, so where the base plans `queue` an extender's own `queue` + // field is dropped here and the platform's column stands. Letting it replace + // the injected definition would read as `author` provenance and switch the + // access path, the claim and the assignment seam off on this object, while + // the gate kept judging the extender's business column. + const governQueue = resolveInjectedSystemColumns(baseDefinition).queue; + // [ADR-0029 D9.2a, 2026-10-30 note] A tenant base's rules are copies: each + // one a code layer ships by name runs as the package declares it, with the + // tenant's `message` and `active`. Computed once, over the base, as the + // scalars above are. + const tenantRules = this.tenantRuleSettings(contributors, baseDefinition); + // [ADR-0146 D10, second rule; ADR-0029 D9.2a] The display keys the tenant + // authored on the fields the extenders patch — computed once, over the + // base, for the scalars' reason above. + const tenantPatched = this.tenantPatchedDisplayKeys(contributors, baseDefinition); + // [ADR-0146 D10, second rule] The fields a patch reaches: the owner's own. + const owner = contributors.find((c) => c.ownership === 'own')?.definition; + const ownFields = ownFieldNamesOf(owner); + let merged = tenantRules + ? this.withPackagedOwnerRules(contributors, baseDefinition, tenantRules) + : { ...baseDefinition }; + // [#28835] Each extension's `fields` as the merge took them, in fold order. + const folded: FoldedExtensionFields[] = []; + for (const contrib of contributors) { + if (contrib.ownership === 'extend') { + let extension = governAudit ? applyAuditFieldGovernance(contrib.definition) : contrib.definition; + if (governQueue) extension = this.withoutExtensionQueueField(extension, contrib.packageId); + // [#28809] The judge, asked of the owner's fields as the OWNER declares + // them (what the install door and `os validate` read), never as an + // extension's `fields` redeclared them; the merge applies only what it + // admits. A field counts as carried as the merge below will carry it: + // in the running body's record spelling, or redeclared by this body. + const patches = (extension as ObjectExtensionBody).fieldPatches; + const running = merged; + const misses = patches && owner && ownFields + ? fieldPatchTargetMisses( + String(baseDefinition.name ?? extension.name ?? ''), + patches, + ownFieldsTarget(owner, ownFields, (name) => !!(recordFieldOf(running, name) ?? recordFieldOf(extension, name))), + ) + : []; + const admitted = misses.length > 0 + ? { ...extension, fieldPatches: admittedFieldPatches(patches!, misses) } as ObjectExtensionBody + : extension; + merged = mergeObjectDefinitions(merged, admitted, tenantAuthored, tenantRules, tenantPatched, ownFields); + // [#28809] …and the same judge once more, over the admitted patches + // against the owner's fields as MERGED, which only names: an admitted + // option an earlier layer left out (an extension's `fields` + // redeclaration, a tenant overlay base) had nothing to relabel, so the + // fold names it as it names any patch it skipped. Never drops, never + // refuses; admission above stays the owner's. + const admittedPatches = (admitted as ObjectExtensionBody).fieldPatches; + const residual = admittedPatches && owner && ownFields + ? fieldPatchTargetMisses( + String(baseDefinition.name ?? extension.name ?? ''), + admittedPatches, + ownFieldsTarget(merged, ownFields, (name) => !!recordFieldOf(merged, name)), + ) + : []; + this.reportFieldPatchMisses([...misses, ...residual], contrib.packageId); + folded.push({ packageId: contrib.packageId, fields: extension.fields }); + } + } + this.reportRedeclaredFields(String(merged.name ?? baseDefinition.name ?? ''), contributors, ownFields, folded); + if (tenantRules) this.reportChangedRuleCopies(contributors, baseDefinition); + // A picklist-bound field is served with its list's options resolved onto + // it — here, in the fold every object read and the write door share, so + // the set a client is offered and the set a write is judged by are one. + return this.finishServedObject(merged, layer, picklistReferrerOf(contributors)); + } + + /** + * [#28835] Name, once, each field name of the object that more than one + * contributor declares in `fields`: two extensions both adding it, or an + * extension redeclaring a field the owner declares (one of `ownFields`). The + * merge spreads `fields`, so the definition served is the last one the fold + * applied: contributions fold in ascending `priority`, and at equal priority + * in the order they registered, so the one registered last. The line names + * the object, the field, every declaring package in that order, and the one + * whose definition is served. + * + * Judged from the contributors' own declarations (`folded`, as the merge + * took them), never from the running body, so a base that already carries + * the extensions' fields (a folded body handed to + * {@link foldObjectExtendersOnto}, a stored overlay row) is not read as a + * redeclaration. `fieldPatches` is not a declaration and is not counted + * ({@link reportFieldPatchMisses} names a patch that misses). A `.custom` + * pair's `__c` field and a dependent's plain one never share a name. + * + * ⛔ Never a refusal, and the served definition is unchanged: the same + * reasons as {@link reportFieldPatchMisses} (the fold runs on every read + * while a boot is still registering packages). At `warn`: one package's + * definition of the field is not served. + */ + static reportRedeclaredFields(this: Pick, object: string, contributors: readonly ObjectContributor[], ownFields: ReadonlySet | undefined, folded: readonly FoldedExtensionFields[]): void { + if (folded.length === 0) return; + const owner = contributors.find((c) => c.ownership === 'own'); + const declarers = new Map>(); + for (const { packageId, fields } of folded) { + if (!isPlainRecord(fields)) continue; + for (const name of Object.keys(fields)) { + let list = declarers.get(name); + if (!list) { + list = owner && ownFields?.has(name) ? [{ packageId: owner.packageId, owner: true }] : []; + declarers.set(name, list); + } + list.push({ packageId, owner: false }); + } + } + const named = (packageId: string | undefined) => (packageId ? `"${packageId}"` : '(no package)'); + for (const [field, list] of declarers) { + if (list.length < 2) continue; + const key = `${object}|${field}|${list.map((d) => d.packageId ?? '').join(',')}`; + if (this.redeclaredFieldsWarned.has(key)) continue; + this.redeclaredFieldsWarned.add(key); + const extenders = list.filter((d) => !d.owner).map((d) => named(d.packageId)); + const declaredBy = list[0]!.owner + ? `the owner ${named(list[0]!.packageId)} and by extensions from ${extenders.join(', ')}` + : `extensions from ${extenders.join(', ')}`; + console.warn( + `[Registry] Field "${field}" of object "${object}" is declared in \`fields\` by ${declaredBy}; ` + + `the definition served is the one from ${named(list[list.length - 1]!.packageId)}, the last the fold applies ` + + '(ascending `priority`; at equal priority, the one registered last). Rename the field in one extension, ' + + 'or patch an owner field\'s display properties instead of redeclaring it.', + ); + } + } + + /** + * [ADR-0146 D10, second rule] Name, once, each patch the fold did not apply + * because the judge missed it: a field that is not one of the owner's own + * (absent, or added only by an extension), an option value the owner's field + * does not declare (one an extension adds by redeclaring the field through + * `fields` included, #28809), or an option of a picklist-bound field. The + * verdict (`misses`) is the install door's and `os validate`'s own judge + * (`fieldPatchTargetMisses`), asked by the fold of the owner's declaration + * ({@link ownFieldsTarget}) as those doors ask it, so the boot names exactly + * what they refuse — plus, asked again of the merged fields, an admitted + * option an earlier layer left out, which had nothing to relabel. What the + * fold names is what it skipped. With no owner registered there is nothing + * to judge against, and nothing is named. + * + * ⛔ Never a refusal, and worded as the state it observes: the fold runs on + * every read while a boot is still registering packages, and an owner that + * registers again declaring the field (an upgrade) makes the patch apply on + * the next read, with nothing recorded to undo (AGENTS.md *Startup registry + * reads*). At `warn`: the object is visibly unpatched, and nothing durable + * is lost. + */ + static reportFieldPatchMisses(this: Pick, misses: readonly FieldPatchTargetMiss[], packageId: string | undefined): void { + for (const miss of misses) { + const object = miss.object; + const key = `${object}|${packageId ?? ''}|${miss.field}|${miss.option ?? ''}`; + if (this.fieldPatchMissesWarned.has(key)) continue; + this.fieldPatchMissesWarned.add(key); + console.warn( + `[Registry] An extension of object "${object}"${packageId ? ` from package "${packageId}"` : ''} ` + + `carries a field patch that is not applied while the object has no such target: ${miss.message}`, + ); + } + } + + /** + * [ADR-0142 D3] An extender's body without its own `queue` field, for a base + * that plans the column; the body itself when it declares none or carries + * the platform's own definition (a served document coming back). The first + * drop per object and package is named once, with the remedy the + * registration door's refusal prescribes. + */ + static withoutExtensionQueueField(this: Pick, extension: ServiceObject, packageId: string | undefined): ServiceObject { + const fields = extension.fields as Record | undefined; + const declared = fields?.[QUEUE_FIELD]; + if (declared === undefined || isInjectedColumnDefinition(declared, QUEUE_FIELD_DEF)) return extension; + const { [QUEUE_FIELD]: _dropped, ...rest } = fields!; + const key = `${extension.name}|${packageId ?? ''}`; + if (!this.shadowedExtensionQueueWarned.has(key)) { + this.shadowedExtensionQueueWarned.add(key); + console.warn( + `[Registry] An extension of object "${extension.name}"${packageId ? ` from package "${packageId}"` : ''} ` + + 'declares a field named `queue`, which the platform reserves on an object whose records have a user ' + + 'owner: it holds the ownership queue a record without an owner sits in. The extension\'s `queue` is ' + + 'ignored and the platform\'s column stands. Rename the business field (for example `support_queue`).', + ); + } + return { ...extension, fields: rest } as ServiceObject; + } + + /** + * The tail every object read ends in — {@link resolveObject} (through + * {@link foldExtendersOntoDefinition}), the code layer, and + * {@link foldObjectExtendersOnto}, which the protocol's stored-row and + * MetadataService layers run. A step added here reaches every one of them. + * + * 1. picklist-bound fields get their list's options resolved onto them; + * 2. [#24400] the bound top-level actions are attached — the code + * packages' and, [#28356], the runtime-authored ones + * ({@link attachBoundActions}). The CODE layer + * ({@link resolveOwnerLayer}, `layer: 'code'`) attaches the code + * packages' alone: a tenant's action is not something a code package + * ships, and `getArtifactItem('object')` answers exactly that question. + */ + static finishServedObject(this: Pick, body: T, layer: ServedObjectLayer = 'served', referrerOf: PicklistReferrer = NO_PACKAGE_REFERRER): T { + return this.attachBoundActions(this.resolvePicklistFields(body, referrerOf), layer); + } + + /** + * [#24400] Attach to an object the top-level actions that code packages + * bind to it by `objectName` — whichever package registered them, and in + * either registration order. + * + * `defineStack()` merges a bound action into its target only when the target + * is in the SAME stack (`mergeActionsIntoObjects`). An action bound to an + * object another package owns — the route `ObjectExtensionSchema`'s + * `guidance.actions` names, since an extension cannot carry `actions` — stays + * at the stack's top level and registers here as a standalone `action` item. + * The router already dispatched it (`resolveRouteActionDeclaration`, rung 2), + * but no served object listed it, so no client rendered it. Attaching it + * HERE, at read time, is what makes it order-independent: the object and + * the action may register in either order, and the cached fold is dropped + * when an action registers ({@link registerItem}) or leaves. + * + * What attaches: a standalone `action` item this registry serves + * ({@link boundServedActions}) — one a code package shipped, or [#28356] one + * a runtime author saved through the metadata door, which the save's + * write-through and the boot's `sys_metadata` hydration register here — + * whose owner key ({@link standaloneActionOwnerKey}, the router's own test) + * is this object's name, and whose package is not disabled. Attached BY + * REFERENCE, so the served entry is the very declaration rung 2 resolves: + * its `requiredPermissions`, `requiresMembershipReach` and execution + * identity travel with it unchanged, and nothing about the object's own + * write policy moves. + * + * Deduplicated by NAME, which is also what makes the step idempotent over a + * body that already carries the action — the same-stack merge's copy, a + * MetadataService copy bridged from {@link getAllObjects}, or a stored row + * written back from a served read. An action the object already declares + * keeps the object's declaration, the precedence the router applies (rung 1 + * before rung 2); when the two declarations differ, the ObjectQL plugin + * reports the pair at boot ({@link FIND_BOUND_ACTION_COLLISIONS}). A target no + * package registers is refused at boot, not here — see + * {@link findUnboundActionTargets}. + * + * The object's own `actions` keep their order exactly — re-sorting them by + * `order` here would reorder an object no package bound anything to the + * moment one did. The attached actions follow, stable-sorted among + * themselves by `order`. + * + * Returns `body` by reference when nothing attaches. + */ + static attachBoundActions(this: Pick, body: T, layer: ServedObjectLayer = 'served'): T { + if (body === null || typeof body !== 'object') return body; + const objectName = (body as { name?: unknown }).name; + if (typeof objectName !== 'string' || objectName === '') return body; + const declared = (body as { actions?: unknown }).actions; + const base: any[] = Array.isArray(declared) ? declared : []; + // [#28877] On the SERVED layer the object's own actions that launch into a + // disabled package are withheld too ({@link isActionWithheldByDisabledPackage}), + // as an attached one is: the same-stack merge puts a bound action here, not + // in the collection below. The code layer is the artifact as shipped and + // keeps them. A withheld name still blocks an attached twin of that name, + // so nothing stands in for it. + const kept = layer === 'served' ? base.filter((action) => !this.isActionWithheldByDisabledPackage(action)) : base; + const present = new Set(base.map((action) => action?.name)); + const fresh: any[] = []; + const collection = this.metadata.get('action'); + if (collection && collection.size > 0) { + for (const action of layer === 'code' ? this.boundCodeActions() : this.boundServedActions()) { + if (standaloneActionOwnerKey(action) !== objectName || present.has(action.name)) continue; + present.add(action.name); + fresh.push(action); + } + } + if (fresh.length === 0 && kept.length === base.length) return body; + // The object's own actions keep their served order, untouched; the + // attached ones follow, ordered among themselves by `order`. + return { ...body, actions: [...kept, ...sortActionsByOrder(fresh)] }; + } + + /** + * [#28356] The standalone `action` items bound to an object that this + * registry SERVES — the population {@link attachBoundActions} attaches. + * + * Every bound item in the collection, whoever authored it: a code package's + * (under its `:` key) and a runtime author's, which the + * save door's write-through and the boot's `sys_metadata` hydration register + * (under the bare name, or a bound package's key with `_provenance: 'org'`). + * Leaving the runtime rows out made a bound action saved through the + * metadata door dispatchable (rung 2 reads {@link getItem}) yet listed by no + * served object, so no client drew it. + * + * One item per name where {@link getItem} would pick one: a bare-key entry + * (an ADR-0005 overlay) shadows the composite entries of its name, so a + * customised bound action is attached as customised — the declaration rung 2 + * resolves — and never as the packaged default under it. + * + * The registry holds no org-scoped row (the protocol's hydration declines + * one), so nothing here crosses an organization. A disabled package's items + * are left out, as the rest of its items are withheld. + * + * ⛔ Not the boot audits' population: a runtime row naming an object no + * package registers must not fail a boot, so {@link findUnboundActionTargets} + * and {@link FIND_BOUND_ACTION_COLLISIONS} keep reading + * {@link boundCodeActions}. + */ + static boundServedActions(this: Pick): any[] { + const collection = this.metadata.get('action'); + if (!collection) return []; + // [#33127, ADR-0150 D4] A stored row in a package overlay slot shadows the + // composite entry of the package it customizes (its envelope's), as the + // bare slot shadows every composite of its name; of two rows customizing + // one package, the one its read serves ({@link rankedForPackage}). + const overlaysOf = new Map(); + for (const [key, item] of collection) { + if (parseOverlaySlotKey(key) === undefined) continue; + const holder = storedRowPackageOf({ key: '', item }) ?? parseOverlaySlotKey(key)!.packageId; + const group = `${holder}:${String((item as any)?.name)}`; + overlaysOf.set(group, [...(overlaysOf.get(group) ?? []), { key, item }]); + } + const servedOverlays = new Set(); + for (const [group, entries] of overlaysOf) { + servedOverlays.add(this.rankedForPackage(entries, group.slice(0, group.lastIndexOf(':')))[0].key); + } + const out: any[] = []; + for (const [key, action] of collection) { + if (!action || typeof action !== 'object' || typeof (action as any).name !== 'string') continue; + const name: string = (action as any).name; + // [ADR-0150 D4] A package-less action of another owner key than the + // bare slot's is its own item, never shadowed by it. + if (key !== name && !isPackagelessActionSlotKey(key) && collection.has(name)) continue; + if (parseOverlaySlotKey(key) !== undefined ? !servedOverlays.has(key) : overlaysOf.has(key)) continue; + if (isObjectLessActionKey(standaloneActionOwnerKey(action))) continue; + // [#28877] …and so is an action that launches into a disabled package. + if (this.isActionWithheldByDisabledPackage(action)) continue; + out.push(action); + } + return out; + } + + /** + * [#24400] The standalone `action` items code packages ship that are bound + * to an object — the population {@link findUnboundActionTargets} and + * {@link FIND_BOUND_ACTION_COLLISIONS} audit. A disabled package's actions + * are left out, as the rest of its items are withheld. + */ + static boundCodeActions(this: Pick): any[] { + const out: any[] = []; + for (const action of this.metadata.get('action')?.values() ?? []) { + if (!action || typeof action !== 'object' || typeof (action as any).name !== 'string') continue; + if (!isCodeArtifactBody(action)) continue; + if (isObjectLessActionKey(standaloneActionOwnerKey(action))) continue; + if (this.isPackageDisabled((action as any)._packageId)) continue; + out.push(action); + } + return out; + } + + /** The body of `SchemaRegistry#findUnboundActionTargets`; the method's docblock there is its contract. */ + static findUnboundActionTargets(this: Pick): UnboundActionTarget[] { + const out: UnboundActionTarget[] = []; + for (const action of this.boundCodeActions()) { + const objectName = standaloneActionOwnerKey(action); + if (this.resolveObjectKey(objectName) !== undefined) continue; + out.push({ action: action.name, objectName, packageId: action._packageId }); + } + return out; + } + + /** + * [ADR-0029 D9.2a] Which of the three fold scalars the BASE layer carries a + * TENANT-AUTHORED value for — i.e. one that no longer equals the packaged + * owner's. + * + * Maintainer ruling, 2026-08-13 (option A, "tenant wins"): an extender's + * scalar applies only while the fold's base still carries the packaged + * owner's value; a diverged base has been authored by the tenant and the + * extender yields. This **amends ADR-0029 D9.2**, whose fold was + * unconditionally last-writer-wins on the scalars, and it is deliberately the + * SAME comparison-based mechanism the #8284 fix applies one layer up — the + * predicate is literally {@link scalarOverridesPackagedBase}, imported from + * `@objectstack/spec`, not a second copy of the shape. One sentence at both + * layers: *an explicit override beats a packaged default.* + * + * ⛔ No provenance flag is stamped on the document and no migration is + * implied: the question is answered from two values, at fold time. + * + * The accepted cost is the point, not a regression — a package can no longer + * relabel an object a tenant has deliberately renamed. There is no escape + * hatch for it, by ruling. + * + * Conservative in the same three ways the predicate is (absent base, non-string + * or empty value, exact equality), plus two of this layer's own: + * + * - **no `own` contributor → no opinion.** An orphan overlay (D9.5) keeps + * today's fold exactly. + * - **base IS the packaged owner → no opinion, by reference.** That is + * {@link resolveObject} with no overlay registered and every + * {@link resolveOwnerLayer} call (D9.6), so the common shape pays one + * identity comparison and cannot change answer. It is an OPTIMISATION, not + * a correctness edge: comparing that base against itself answers "not + * diverged" for every key anyway. ⛔ So it must never be relaxed into + * something weaker than identity — a value-equality test here would start + * answering for bodies that merely LOOK like the owner. + * + * ⛔ The owner is read as the `own` CONTRIBUTOR, deliberately NOT through + * {@link getPackagedObjectOwner} — whose extra `isCodeArtifactBody` test + * (D9.8) would make this decline to protect a RUNTIME-authored object, i.e. + * exactly the object whose owner row the tenant wrote by hand. The two agree + * wherever a packaged owner exists, which is every shape the ADR-0029 D9.2a amendment records; they + * differ only on a tenant-authored owner, and there the ruled sentence still + * reads the same way — the tenant's own row is the explicit override and a + * package's `objectExtensions` entry is the packaged default. The rejected + * alternative is the trap commit 427344c26 named one layer up, in its own form: + * comparing against a body that already has extenders folded onto it + * ({@link resolveOwnerLayer}) would report every extender's scalar as + * "unchanged" and yield nothing, ever. + * + * IDEMPOTENCE is preserved, which matters because + * {@link foldObjectExtendersOnto} is documented as idempotent (#8027) and two + * shipped call sites really do hand it an already-folded base. A base that + * already carries an extender's scalar reads as "diverged", so every extender + * yields and the value stays exactly what the first fold produced — the same + * answer, reached by yielding instead of by re-applying. + */ + static tenantAuthoredScalars(contributors: ObjectContributor[], baseDefinition: ServiceObject): ReadonlySet | undefined { + const owner = contributors.find((c) => c.ownership === 'own')?.definition; + if (!owner || owner === baseDefinition) return undefined; + let authored: Set | undefined; + for (const key of OBJECT_FOLD_SCALAR_KEYS) { + if (scalarOverridesPackagedBase(owner, key, baseDefinition[key])) { + (authored ??= new Set()).add(key); + } + } + return authored; + } + + /** + * [ADR-0146 D10, second rule; ADR-0029 D9.2a] The display keys a fold's base + * has authored away from the packaged owner's value, on the fields and + * options the `extend` contributors patch — each a {@link fieldPatchYieldKey}. + * A patch yields on those keys, as an extender's scalar yields on + * {@link tenantAuthoredScalars}, and by the same comparison + * ({@link displayKeyOverridesPackagedBase}). + * + * The same two "no opinion" edges as {@link tenantAuthoredScalars}, for its + * reasons: no `own` contributor, or a base that IS the owner's body (by + * reference). Idempotent over a folded base for its reason too: a base that + * already carries a patch's text reads as authored, so the patch yields and + * the value stays what the first fold produced. + */ + static tenantPatchedDisplayKeys(contributors: ObjectContributor[], baseDefinition: ServiceObject): ReadonlySet | undefined { + const owner = contributors.find((c) => c.ownership === 'own')?.definition; + if (!owner || owner === baseDefinition) return undefined; + let authored: Set | undefined; + for (const contrib of contributors) { + if (contrib.ownership !== 'extend') continue; + const patches = (contrib.definition as ObjectExtensionBody).fieldPatches; + if (!patches) continue; + for (const [name, patch] of Object.entries(patches)) { + const packagedField = recordFieldOf(owner, name); + const baseField = recordFieldOf(baseDefinition, name); + if (!patch || !packagedField || !baseField) continue; + for (const key of BASE_FIELD_DISPLAY_WHITELIST.field) { + if (displayKeyOverridesPackagedBase(packagedField, key, baseField[key])) { + (authored ??= new Set()).add(fieldPatchYieldKey(name, key)); + } + } + for (const value of Object.keys(patch.options ?? {})) { + const packagedOption = optionByValue(packagedField, value); + const baseOption = optionByValue(baseField, value); + if (!packagedOption || !baseOption) continue; + for (const key of BASE_FIELD_DISPLAY_WHITELIST.option) { + if (displayKeyOverridesPackagedBase(packagedOption, key, baseOption[key])) { + (authored ??= new Set()).add(fieldPatchYieldKey(name, key, value)); + } + } + } + } + } + return authored; + } + + /** + * [ADR-0029 D9.2a, 2026-10-30 note] The `message` / `active` a TENANT base + * sets on its rules, by name — `undefined` when the fold's base is a code + * layer, where a later layer's same-named rule replaces the earlier one + * whole, as a field does. + * + * The base is the tenant's when it is not the `own` contributor's body (the + * overlay layer, a stored row, a served copy — the identity test + * {@link tenantAuthoredScalars} makes), or when the owner itself is + * tenant-authored (a runtime-authored object). Idempotent over a folded body: + * its copies already carry what the first fold produced. + */ + static tenantRuleSettings(contributors: ObjectContributor[], baseDefinition: ServiceObject): ReadonlyMap | undefined { + const owner = contributors.find((c) => c.ownership === 'own')?.definition; + if (!owner) return undefined; + if (owner === baseDefinition && !isTenantAuthored(baseDefinition)) return undefined; + return tenantRuleSettingsOf(baseDefinition.validations); + } + + /** + * [ADR-0029 D9.2a, 2026-10-30 note] A tenant base whose rules include copies + * of the packaged OWNER's rules, with each copy replaced by the owner's rule + * carrying the copy's `message` and `active` — so a package fix to the rule + * reaches the tenant, and the copy's own condition never runs. + * + * Only names the base carries: a rule the overlay leaves out stays out, as + * D9.2's replace has always meant. A second copy of one name (a row stored + * while the fold still appended) is dropped. The base is returned as a fresh + * spread either way, as the fold has always started from one. + */ + static withPackagedOwnerRules(contributors: ObjectContributor[], baseDefinition: ServiceObject, tenantRules: ReadonlyMap): ServiceObject { + const merged = { ...baseDefinition }; + const owner = contributors.find((c) => c.ownership === 'own')?.definition; + if (!owner || owner === baseDefinition || !isCodeArtifactBody(owner)) return merged; + const ownerRules = new Map(); + for (const rule of owner.validations ?? []) { + const name = validationRuleName(rule); + if (name !== undefined && !ownerRules.has(name)) ownerRules.set(name, rule); + } + const baseRules = baseDefinition.validations; + if (ownerRules.size === 0 || !Array.isArray(baseRules) || baseRules.length === 0) return merged; + const seen = new Set(); + const rules: unknown[] = []; + for (const rule of baseRules) { + const name = validationRuleName(rule); + const packaged = name === undefined ? undefined : ownerRules.get(name); + if (packaged === undefined) { + rules.push(rule); + } else if (!seen.has(name!)) { + seen.add(name!); + rules.push(withTenantRuleSettings(packaged, tenantRules.get(name!))); + } + } + merged.validations = rules as ServiceObject['validations']; + return merged; + } + + /** + * [ADR-0029 D9.2a, 2026-10-30 note] Every rule a CODE layer of this object + * ships, by name, in fold order — the packaged owner's (when the owner is a + * code artifact), then each code `extend` contributor's, a later layer's + * rule replacing an earlier one of its name. What a tenant's copy of a rule + * is compared with ({@link tenantRuleCopyRefusal}, + * {@link reportChangedRuleCopies}) and what {@link getPackagedObjectBase} + * serves as the packaged rule set. + */ + static packagedRules(contributors: readonly ObjectContributor[]): unknown[] | undefined { + let rules: unknown[] | undefined; + const owner = contributors.find((c) => c.ownership === 'own'); + if (owner && isCodeArtifactBody(owner.definition) && Array.isArray(owner.definition.validations)) { + rules = [...owner.definition.validations]; + } + for (const contrib of contributors) { + if (contrib.ownership !== 'extend' || !isCodeArtifactBody(contrib.definition)) continue; + const list = contrib.definition.validations; + if (!Array.isArray(list) || list.length === 0) continue; + rules = mergeValidationsByName(rules, list, undefined); + } + return rules; + } + + /** The body of `SchemaRegistry#tenantRuleCopyRefusal`; the method's docblock there is its contract. */ + static tenantRuleCopyRefusal(this: Pick, name: string, body: unknown): Error | null { + const fqn = this.resolveObjectKey(name); + const contributors = fqn === undefined ? undefined : this.objectContributors.get(fqn); + if (!contributors) return null; + const packaged = new Map(); + for (const rule of this.packagedRules(contributors) ?? []) { + const ruleName = validationRuleName(rule); + if (ruleName !== undefined) packaged.set(ruleName, rule); + } + if (packaged.size === 0) return null; + const rules = (body as { validations?: unknown } | null | undefined)?.validations; + if (!Array.isArray(rules)) return null; + for (const rule of rules) { + const ruleName = validationRuleName(rule); + if (ruleName === undefined || !packaged.has(ruleName)) continue; + const changed = changedPackagedRuleKeys(rule, packaged.get(ruleName)); + if (changed.length > 0) return new ValidationRuleCopyRefusedError(fqn!, ruleName, changed); + } + return null; + } + + /** + * [ADR-0029 D9.2a, 2026-10-30 note] Say, once, that a tenant base carries a + * copy of a packaged rule that changes what the package owns. The fold has + * already enforced the package's rule; the stored copy's condition is not + * what runs, while the object serves normally, so this is an `error` with + * its consequence and its fix. Reported, never refused, here: a stored copy + * also differs after a package upgrade changes the rule, and refusing it + * would drop the tenant's whole layer at boot. + */ + static reportChangedRuleCopies(this: Pick, contributors: ObjectContributor[], baseDefinition: ServiceObject): void { + const rules = baseDefinition.validations; + if (!Array.isArray(rules) || rules.length === 0) return; + const packaged = new Map(); + for (const rule of this.packagedRules(contributors) ?? []) { + const ruleName = validationRuleName(rule); + if (ruleName !== undefined) packaged.set(ruleName, rule); + } + if (packaged.size === 0) return; + const reported = new Set(); + for (const rule of rules) { + const ruleName = validationRuleName(rule); + if (ruleName === undefined || reported.has(ruleName) || !packaged.has(ruleName)) continue; + reported.add(ruleName); + const changed = changedPackagedRuleKeys(rule, packaged.get(ruleName)); + if (changed.length === 0) continue; + const key = `${baseDefinition.name}|${ruleName}|${stableStringify(rule)}|${stableStringify(packaged.get(ruleName))}`; + if (this.reportedRuleCopyChanges.has(key)) continue; + this.reportedRuleCopyChanges.add(key); + const keys = changed.map((k) => `\`${k}\``).join(', '); + console.error( + `[Registry] [validation_rule_copy_changed] Object "${baseDefinition.name}": the tenant's copy of ` + + `validation rule "${ruleName}" changes ${keys} from the packaged rule. Only a packaged rule's ` + + '`message` and `active` follow the tenant, so the package\'s rule is enforced with the tenant\'s ' + + `message and switch, and the copy's ${keys} does not run, while the object serves normally. ` + + 'To change the condition, add a rule under a new name, or change it in the package. To keep the ' + + 'packaged rule, save the object again from its served definition.', + ); + } + } + + /** The body of `SchemaRegistry#foldObjectExtendersOnto`; the method's docblock there is its contract. */ + static foldObjectExtendersOnto(this: Pick, name: string, base: T, options?: ObjectExtensionFoldOptions): T { + if (base === null || typeof base !== 'object') return base; + const fqn = this.resolveObjectKey(name); + // With nothing to fold, the body still gets its picklist-bound fields + // resolved — the same step the fold below ends with — so a body this + // registry never saw is served in the one shape every other read uses. + // By reference when no field names a picklist. + if (fqn === undefined) return this.finishServedObject(base); + // [#29903, #29872] The contributions `options` names are left out, as on + // {@link getAllObjects} ({@link ObjectExtensionFoldOptions}). + const registered = this.objectContributors.get(fqn); + const listed = leavesExtensionsOut(options) ? registered?.filter((c) => !isLeftOutExtension(c, options)) : registered; + // A disabled package's `extend` contributions are withheld, and taken off + // a base that already carries them, as on {@link resolveObject}. + const withheld = listed ? this.withholdDisabledExtensions(listed) : undefined; + const contributors = withheld?.kept ?? listed; + const served = withheld ? withoutWithheldMembers(base as unknown as ServiceObject, withheld) : base as unknown as ServiceObject; + if (!contributors || !contributors.some((c) => c.ownership === 'extend')) { + return this.finishServedObject(served as unknown as T, 'served', picklistReferrerOf(contributors)); + } + return this.foldExtendersOntoDefinition( + contributors, + this.subtractExtenderContributions(contributors, served), + ) as unknown as T; + } + + /** + * [#8268] The ADR-0079 object-materialization block itself, as ONE function. + * + * Every stamp this registry puts on a base layer at registration time lives + * HERE, and {@link registerObject} calls it rather than spelling the sequence + * out — so {@link materializeServedObjectOnto}, which replays it onto a body + * that never came through `registerObject`, is the SAME code and not a second + * copy free to drift from it. + * + * That property is the whole point, and it is the lesson three cards taught + * one at a time. Each of #6562 (injected system columns), #8038 (`__search`) + * and #8268 (`nameField`) is the same shape: a stamp the registry applies at + * this seam, absent from the copy a `/meta` read serves, found and filed + * SEPARATELY because nothing tied the stamps together. A fourth stamp added + * to this method is converged at the read exits the day it is added, with no + * fourth card and no fourth patch — which is what "fix the class" means here. + * + * ⛔ Order is contractual, not incidental: `provisionPrimary` runs BEFORE + * `provisionSearchCompanion` because the companion's source is resolved over + * the post-designation document. Keep additions in the order the seam applies + * them. + * + * @param opts.designatePrimary - `false` withholds ONLY the ADR-0079 + * designation step. See {@link materializeServedObjectOnto} for the one + * caller that passes it and why the choice is not this method's to make. + * @param opts.provisionCompanion - `false` withholds ONLY the `__search` + * step, for the same reason and from the same caller (#8376). + */ + static materializeBaseLayer(this: Pick, schema: ServiceObject, opts?: { designatePrimary?: boolean; provisionCompanion?: boolean }): ServiceObject { + let out = schema; + + // [ADR-0131 D7] The deployment's platform-global declaration, recorded on + // the base layer. FIRST, because every later stamp asks its question of the + // object as this deployment provisions it — the tenant index below must + // see a declared object as carrying no tenant column. See + // {@link applyDeploymentTenancy}; its write-side inverse is the LAST strip + // in {@link stripMaterializedStampsFrom}. + out = this.applyDeploymentTenancy(out); + + // [ADR-0079] DESIGNATE-ONLY primary-title provisioning — `synthesize: + // false` never adds a column, so this is safe against title-less + // system/junction tables (see the call in `registerObject` for the full + // reasoning on why synthesis is refused at this seam). + if (opts?.designatePrimary !== false) { + out = provisionPrimary(out, { synthesize: false }); + } + + // [#2486] Search-normalization companion column (`__search`). Runs AFTER + // `provisionPrimary` so the just-designated `nameField` is visible, and + // ONLY when the deployment enables pinyin search — the column is a real + // additive migration (ADR-0045) that the driver's `syncSchema` + // materializes, populated by plugin-pinyin-search's before-save hooks and + // OR-ed into `$search` by the engine's `expandSearchToFilter`. + if (this.searchCompanion && opts?.provisionCompanion !== false) { + out = provisionSearchCompanion(out); + } + + // [#8375] The tenant-scope index — the fourth stamp of this seam, and the + // first one added AFTER #8268 generalised it. It needed no new method here, + // no new method in the protocol and no new convergence at the read exits: + // being a step of this block IS the convergence, which is the property + // #8268 exists to provide. Runs LAST because it is the only stamp gated on + // a DEPLOYMENT flag rather than on the document, so nothing it appends can + // change the answer the two steps above reached over the fields map. + out = provisionTenantScopeIndex(out, { multiTenant: this.multiTenant }); + + return out; + } + + /** The body of `SchemaRegistry#materializeServedObjectOnto`; the method's docblock there is its contract. */ + static materializeServedObjectOnto(this: Pick, base: T): T { + if (base === null || typeof base !== 'object') return base; + const declined = this.declinedBaseLayerStamps(base); + return this.materializeBaseLayer(base as never, { + designatePrimary: !declined.primary, + provisionCompanion: !declined.companion, + }) as unknown as T; + } + + /** + * Which document-sensitive stamps did this registry resolve the object `base` + * names and then DECLINE to apply to its base layer? + * See {@link materializeServedObjectOnto} for why the question is asked at all. + * + * All-`false` — "no opinion" — for a body with no usable name and for a name + * this registry does not know, which is the honest answer in both cases and is + * also the safe one: an object that never reached `registerObject` has no + * registry verdict to converge onto, and nothing could have folded such a body + * either (the fold reads this same contributor list), so the body IS its own + * base layer and the pure transform is exactly right for it. + */ + static declinedBaseLayerStamps(this: Pick, base: unknown): { primary: boolean; companion: boolean } { + const noOpinion = { primary: false, companion: false }; + const name = (base as { name?: unknown }).name; + if (typeof name !== 'string' || name === '') return noOpinion; + const resolved = this.getObject(name) as + | { nameField?: unknown; fields?: unknown } + | undefined; + if (!resolved) return noOpinion; + const fields = resolved.fields; + const resolvedFields = + fields && typeof fields === 'object' && !Array.isArray(fields) + ? (fields as Record) + : undefined; + return { + primary: resolved.nameField === undefined, + companion: resolvedFields?.[SEARCH_COMPANION_FIELD] === undefined, + }; + } + + /** The body of `SchemaRegistry#provisionSearchCompanionOnto`; the method's docblock there is its contract. */ + static provisionSearchCompanionOnto(this: Pick, base: T): T { + if (base === null || typeof base !== 'object') return base; + if (!this.searchCompanion) return base; + return provisionSearchCompanion(base as never) as unknown as T; + } + + /** The body of `SchemaRegistry#stripProvisionedSearchCompanionFrom`; the method's docblock there is its contract. */ + static stripProvisionedSearchCompanionFrom(base: T): T { + if (base === null || typeof base !== 'object') return base; + const fields = (base as { fields?: unknown }).fields; + if (!fields || typeof fields !== 'object' || Array.isArray(fields)) return base; + const present = (fields as Record)[SEARCH_COMPANION_FIELD]; + if (present === undefined) return base; + + // Re-stamp a copy WITHOUT the column and compare: the canonical definition + // comes from the provisioning function itself, and an object the seam would + // not provision for yields no stamp and therefore no removal. + const withoutCompanion = { ...(fields as Record) }; + delete withoutCompanion[SEARCH_COMPANION_FIELD]; + const restamped = provisionSearchCompanion( + { ...(base as Record), fields: withoutCompanion } as never, + ) as unknown as { fields?: Record }; + const canonical = restamped.fields?.[SEARCH_COMPANION_FIELD]; + if (canonical === undefined) return base; + if (stableStringify(present) !== stableStringify(canonical)) return base; + + return { ...(base as Record), fields: withoutCompanion } as unknown as T; + } + + /** The body of `SchemaRegistry#stripMaterializedStampsFrom`; the method's docblock there is its contract. */ + static stripMaterializedStampsFrom(this: Pick, base: T): T { + if (base === null || typeof base !== 'object') return base; + return this.stripDeploymentTenancyFrom( + this.stripProvisionedPrimaryFrom( + this.stripProvisionedSearchCompanionFrom(this.stripProvisionedTenantIndexFrom(base)), + ), + ); + } +} diff --git a/packages/objectql/src/registry-package-addressing.ts b/packages/objectql/src/registry-package-addressing.ts new file mode 100644 index 00000000000..ae672564e9b --- /dev/null +++ b/packages/objectql/src/registry-package-addressing.ts @@ -0,0 +1,783 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33949 codemod] The "Package-scoped addressing" section of `SchemaRegistry`: + * the bodies of `overlaySlotBinding`, `overlayRowBinding`, `pairRowBinding`, + * `ownersHoldingAName`, `compositeHoldersOf`, `packageScopedHoldersOf`, + * `bundleMembersHeldBy`, `gateSlotBinding`, `getPackageScopedItem`, + * `getEnvironmentScopedItem`, `getEnvironmentScopedActions`, `readsByPackage`, + * `bareSlotEntriesOf`, `rankedForPackage`, `removePackageOverlaySlots`, + * `orderedOverlayTier`, `getArtifactItem`, `removeRuntimeShadow`, + * `removeOverlayEntry`, `listItems`, `isPackageDisabled`, + * `isObjectPackageDisabled`, `isFlowOfDisabledPackage`, + * `isActionWithheldByDisabledPackage`, `hasDisabledPackage`, + * `getRegisteredTypes` moved out of `registry.ts` whole by + * `scripts/codemod-split-registry.mjs`. + * + * A MOVE, not a redesign. {@link PackageAddressingFacet} is a stateless facet of `SchemaRegistry` + * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` + * method of the same name exactly as it stood: not re-indented, not + * formatted, not edited. The method stays on the class as a one-call stub with + * its signature, so the published surface, an instance spy and a subclass + * override are what they were. A public or protected method keeps its + * docblock on the class, the contract; a private method's docblock moved here + * with its body. The state stays on `SchemaRegistry`. + * + * The bodies reach the class through {@link PackageAddressingHost}: exactly the + * `SchemaRegistry` members they read, private ones included. Every stub hands + * its body, by a `.call`, the same host: `packageAddressingHost` in + * `registry-facet-hosts.ts`, built once per registry by the class's static + * block. Methods go as closures that call them on the registry at call time, so + * a subclass override or an instance spy is still the one reached; fields and + * accessors go as GETTERS, read when the body reads them, as they were on the + * class, and a field a body assigns goes with a SETTER that assigns it on the + * registry. + * + * Values that stayed in `registry.ts` are imported from it (lever (a) of + * ruling 6591753301); every use is inside a function body, so the import + * cycle is never read at evaluation time. `index.ts` re-exports nothing from + * this module. + */ + +import { ITEM_KEY_DISCRIMINATORS, readDiscriminatorValue as discriminatorValue, isCodeArtifactBody, isTenantAuthored } from '@objectstack/metadata-core'; +import { customizationBasePackageId } from '@objectstack/spec/kernel'; +import { isPackageScopedReadType } from './package-scoped-read.js'; +import { + OVERLAY_SLOT_MARK, bundleBaseKey, collectBundle, entriesOfMember, overlaySlotEntriesOf, + packagelessActionSlotEntriesOf, parseOverlaySlotKey, pickBundleMember, storedRowPackageOf, +} from './registry-top-level.js'; +import type { BundleEntry, ObjectExtensionFoldOptions } from './registry-top-level.js'; +import { + NOT_A_PAIR_ROW_OF_A_HOLDER, actionHoldersOnOwnerKeyOf, declaredOverrideBase, pairRowDeclaresAnotherHolder, +} from './registry.js'; +import type { SchemaRegistry } from './registry.js'; + +/** + * The `SchemaRegistry` members the bodies of {@link PackageAddressingFacet} read — every one of + * them, and no other. Each is typed AS the class member (an indexed access on + * the class), so a change to a member's signature on the class is a change + * here too. `readonly` members are the getters; the others are fields a body + * assigns. + */ +export interface PackageAddressingHost { + bareSlotEntriesOf: SchemaRegistry['bareSlotEntriesOf']; + bundleMembersHeldBy: SchemaRegistry['bundleMembersHeldBy']; + compositeHoldersOf: SchemaRegistry['compositeHoldersOf']; + gateSlotBinding: SchemaRegistry['gateSlotBinding']; + getAllObjects: SchemaRegistry['getAllObjects']; + getObjectOwner: SchemaRegistry['getObjectOwner']; + getPackage: SchemaRegistry['getPackage']; + hasDisabledPackage: SchemaRegistry['hasDisabledPackage']; + invalidateAll: SchemaRegistry['invalidateAll']; + isBundleAddition: SchemaRegistry['isBundleAddition']; + isFlowOfDisabledPackage: SchemaRegistry['isFlowOfDisabledPackage']; + isObjectPackageDisabled: SchemaRegistry['isObjectPackageDisabled']; + isPackageDisabled: SchemaRegistry['isPackageDisabled']; + log: SchemaRegistry['log']; + readonly metadata: SchemaRegistry['metadata']; + noteItem: SchemaRegistry['noteItem']; + readonly objectContributors: SchemaRegistry['objectContributors']; + overlaySlotBinding: SchemaRegistry['overlaySlotBinding']; + ownersHoldingAName: SchemaRegistry['ownersHoldingAName']; + packagelessActionEntryToRetire: SchemaRegistry['packagelessActionEntryToRetire']; + pairRowBinding: SchemaRegistry['pairRowBinding']; + rankedForPackage: SchemaRegistry['rankedForPackage']; + resolveObjectKey: SchemaRegistry['resolveObjectKey']; + resolveOwnerLayer: SchemaRegistry['resolveOwnerLayer']; + retirePackagelessActionSlot: SchemaRegistry['retirePackagelessActionSlot']; + settledPairRowBinding: SchemaRegistry['settledPairRowBinding']; +} + +/** The bodies of the "Package-scoped addressing" section of `SchemaRegistry`: a stateless facet, called by the class's stubs. */ +export class PackageAddressingFacet { + /** + * [ADR-0150 D4, "registry overlay slot (ADR-0005)"] The package a bare-slot + * entry of `(type, name)` customizes — the per-package overlay slot. + * + * The registry still stores ONE bare slot per name (every `sys_metadata` + * hydration and write-through writes it, and {@link getItem} serves it first + * to every caller). What changes here is what that entry is BOUND to: an + * overlay binds to the `(package, name)` it customizes, so a package-scoped + * read of another package's same-named item never sees it. + * + * The binding is the entry's `_packageId` — the stored row's `package_id` + * column, or the artifact envelope the hydration grafted onto it (ADR-0010 + * §3.3; `withRowBinding` drops a body's own stamp for a package-less row) — + * read as follows: + * + * - none, `''` or the `sys_metadata` sentinel (a package-less row), when no + * package holds the name in a composite slot → `null`: the environment's + * own item (ADR-0150 D1 bucket 4); + * - a package-less row when exactly one package holds the name → that + * package: "a package-less row that bears a package-held name is a + * customization, never a second item" (ADR-0150 D3), the ADR-0005 + * overlay of the one holder, read at call time, so a package installed + * after the row was saved makes it that package's overlay; + * - a package-less row when two or more packages hold the name → + * `undefined`: a customization of a name it cannot say whose, so it binds + * to no package and is no environment item either (never a guess); + * - a package that holds the name in its composite slot → that package + * (an ADR-0005 overlay of its item); + * - a customization pair's row, when some holder of the name is a package + * the pair customizes (`isPairOf`: the App's pair is shared by every + * package of the App, ADR-0146 D3) → {@link pairRowBinding}, the exact + * read's owner check (`pairRowOnAddress`, metadata-protocol): the holder + * its `overrides.package` declares, else the App whose own pair it is + * (#28390 ruling A), else its one owner, else no package; + * - a paired `.custom` package holding no composite slot of the name, + * whose base does → the base (ADR-0146 D9: the pair's declared override + * customizes its base's item); + * - any other package → that package: its own stored item of the name. + * + * `holders` is the composite holder set, passed in when the caller already + * has it. + * + * [ADR-0150 D4] An `action` is identified by `(package, owning object, + * name)`, so for a package-less action the holders counted above are only + * those whose own item has the same owner key ({@link standaloneActionOwnerKey}, + * the router's own test; every object-less spelling is one key): a + * package-less action on another object customizes no holder's action. It + * is the environment's own item (`null`) when no holder shares its owner + * key, that holder's customization when exactly one does, and no package's + * (`undefined`) when two or more do. + */ + static overlaySlotBinding(this: Pick, type: string, item: unknown, name: string, collection: Map, holders?: ReadonlySet): string | null | undefined { + const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; + const composite = holders ?? new Set(this.compositeHoldersOf(collection, name)); + if (typeof stamped !== 'string' || stamped === '' || stamped === 'sys_metadata') { + const customized = type === 'action' ? actionHoldersOnOwnerKeyOf(item, name, collection, composite) : [...composite]; + if (customized.length === 0) return null; + return customized.length === 1 ? customized[0] : undefined; + } + if (composite.has(stamped)) return stamped; + const paired = this.pairRowBinding(type, stamped, item, name, collection, composite); + if (paired !== NOT_A_PAIR_ROW_OF_A_HOLDER) return paired; + const base = customizationBasePackageId(stamped); + if (base !== null && composite.has(base)) return base; + // [#33127] …or whose base holds it as its own stored row (its package + // overlay slot): the pair's row customizes that base's item, as beside a + // composite slot, and is never a holder of its own. One bare slot never + // held both rows; the package overlay slots keep each. + if (base !== null && overlaySlotEntriesOf(collection, name, ITEM_KEY_DISCRIMINATORS[type]) + .some(({ key }) => parseOverlaySlotKey(key)?.packageId === base)) return base; + return stamped; + } + + /** The body of `SchemaRegistry#overlayRowBinding`; the method's docblock there is its contract. */ + static overlayRowBinding(this: Pick, type: string, name: string, item: unknown, options?: { includeDisabledPackages?: boolean }): string | null | undefined { + const collection = this.metadata.get(type); + if (!collection) return undefined; + // [#32692] `includeDisabledPackages` counts a disabled holder as served: + // the read-side hydrator's envelope for a pair row must not move with a + // reversible disable (ADR-0150 D3). + const off = (id: string) => !options?.includeDisabledPackages && this.isPackageDisabled(id); + const composite = new Set(this.compositeHoldersOf(collection, name)); + const servedComposite = new Set([...composite].filter((id) => !off(id))); + const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; + if (typeof stamped === 'string' && stamped !== '' && stamped !== 'sys_metadata' && !composite.has(stamped)) { + const holders = new Set(composite); + for (const { item: entry } of this.bareSlotEntriesOf(type, collection, name)) { + const bound = this.overlaySlotBinding(type, entry, name, collection, composite); + if (typeof bound === 'string') holders.add(bound); + } + // The pair's own rows bind to it when no composite slot pairs it; it holds no name of its own here. + const served = new Set([...holders].filter((id) => id !== stamped && !off(id))); + const settled = this.settledPairRowBinding(stamped, item, served); + if (Array.isArray(settled)) { + const named = this.ownersHoldingAName(type, name, collection, settled as readonly string[], item, composite, stamped); + return named.length === 1 ? named[0] : undefined; + } + if (settled !== NOT_A_PAIR_ROW_OF_A_HOLDER) return settled as string | undefined; + } + return this.overlaySlotBinding(type, item, name, collection, servedComposite); + } + + /** + * [ADR-0150 D4, epic ruling 6761802819 on #31699 F2, #32408] The holder a + * customization pair's row `item` (stored under `pairId`, which holds no + * composite slot of the name) binds to, read as the exact read's one owner + * check reads it (`pairRowOnAddress`, `@objectstack/metadata-protocol`), so + * every reader of {@link overlaySlotBinding} — the holder set, a holder's + * bundle members, a package's item and the environment's item — answers + * the owner the exact address serves it to: + * + * - the row declares the base it customizes (`overrides.package`, ADR-0146 + * D2) → that holder when the pair customizes it; a declared holder the + * pair does not customize, or a declared package that holds no item of + * the name, binds it to no package (it is never the declaring row of + * another holder); + * - else a holder that is the App whose own pair it is → that App (#28390 + * ruling A: the App's address keeps its pair's row); + * - else exactly one holder the pair customizes → that holder; + * - else two or more → no package (`undefined`): the row cannot say whose + * it is, never a pick between them. + * + * The undeclared arms count the holders the exact read counts + * (`packageScopedHoldersOf`, which the exact read passes): for a bundled + * type, a package whose holding is a bundle ADDITION to another holder's + * bundle holds no name (#28802), so it is no second owner. The + * addition is judged as {@link packageScopedHoldersOf} judges it, over every + * holder of the name with every row bound to it ({@link ownersHoldingAName}): + * a row declaring a package as its base makes that package hold the row's + * member, so it is no addition when another holder ships that member (an + * undeclared row beside it is then no package's, as the exact read refuses + * it naming both). A row that declares an addition's package as its base is + * still that package's. When the count would read an undeclared row of + * another shared pair (a binding that waits on that pair's own count), the + * row binds to no package (epic seat 1's ruling). The exact read adopts + * this binding ({@link overlayRowBinding}, #32561), so it never serves such + * a row on a package's address. + * + * Every holder is counted, a disabled one included (`composite` is the + * holder set before {@link packageScopedHoldersOf} filters the served + * ones): a binding never moves with a reversible disable (ADR-0150 D3, + * #32183). The exact read counts the served holders, so with one of two + * owners disabled it serves a row this binds to no package: it adopts this + * binding over the served holders only ({@link overlayRowBinding}, #32561), + * as the enabled holder's address keeps the row (epic ruling R4-A). + * + * {@link NOT_A_PAIR_ROW_OF_A_HOLDER} when no holder of the name is a package + * `pairId` customizes: the row keeps the per-package reading of + * {@link overlaySlotBinding}. + */ + static pairRowBinding(this: Pick, type: string, pairId: string, item: unknown, name: string, collection: Map, composite: ReadonlySet): string | undefined | typeof NOT_A_PAIR_ROW_OF_A_HOLDER { + const settled = this.settledPairRowBinding(pairId, item, composite); + if (!Array.isArray(settled)) return settled as string | undefined | typeof NOT_A_PAIR_ROW_OF_A_HOLDER; + const named = this.ownersHoldingAName(type, name, collection, settled as readonly string[], item, composite, pairId); + return named.length === 1 ? named[0] : undefined; + } + + /** + * [#28802, #32408] `owners` less every one whose holding of the bundled + * `(type, name)` is an addition to another holder's bundle + * ({@link isBundleAddition}), judged as {@link packageScopedHoldersOf} + * judges it: over EVERY holder of the name (a package outside the App + * included, so an owner that adds to it is an addition), each holder's + * members being its composite members and the bare-slot rows that bind to + * it without this count ({@link bundleMembersHeldBy}) — a pair row + * declaring the holder, an App's own pair row (#28390 ruling A), a + * per-package pair's row, the holder's own stored row. The undeclared rows + * of `pairId`, the pair this arm decides, are left out, since the members + * they add are what it answers (the exact read adopts the answer, #32561). An + * undeclared row of ANOTHER shared pair would make this count wait on that + * pair's, which may wait on this one: then `owners` is answered whole, so + * the row binds to no package (never a guess; epic seat 1's ruling, which + * the exact read adopts, #32561). An + * addition that ships the member `row` is ITS member too, so it stays an + * owner: the row may customize that package's member, and two owners bind + * it to no package. `owners` unchanged for a type that is no bundle, or with + * fewer than two. + */ + static ownersHoldingAName(this: Pick, type: string, name: string, collection: Map, owners: readonly string[], row: unknown, composite: ReadonlySet, pairId: string): readonly string[] { + const disc = ITEM_KEY_DISCRIMINATORS[type]; + if (!disc || owners.length < 2) return owners; + const bound = new Map(); + for (const { item } of this.bareSlotEntriesOf(type, collection, name)) { + const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; + const settled = typeof stamped === 'string' && !composite.has(stamped) + ? this.settledPairRowBinding(stamped, item, composite) + : NOT_A_PAIR_ROW_OF_A_HOLDER; + if (Array.isArray(settled)) { + if (stamped === pairId) continue; + return owners; + } + const holder = settled !== NOT_A_PAIR_ROW_OF_A_HOLDER + ? settled as string | undefined + : this.overlaySlotBinding(type, item, name, collection, composite); + if (typeof holder === 'string') bound.set(holder, [...(bound.get(holder) ?? []), item]); + } + const memberOf = (item: unknown) => discriminatorValue(item, disc.field) || disc.canonical; + const membersOf = new Map>(); + const codeMembersOf = new Map>(); + for (const id of new Set([...composite, ...bound.keys()])) { + const { local } = collectBundle(collection, name, disc, id); + membersOf.set(id, new Set([...local.map((e) => e.item), ...(bound.get(id) ?? [])].map(memberOf))); + codeMembersOf.set(id, new Set(local.map((e) => memberOf(e.item)))); + } + const rowMember = memberOf(row); + return owners.filter((id) => membersOf.get(id)!.has(rowMember) || !this.isBundleAddition(id, membersOf, codeMembersOf)); + } + + /** + * The packages holding `(type, name)` through a composite + * `:` slot, in the item store's order — the registry's + * identity key (ADR-0150 D2), read off the KEY, as {@link getItem}'s + * prefer-local reads it. A bundled type's members are one holder. + */ + static compositeHoldersOf(collection: Map, name: string): string[] { + const holders = new Set(); + const suffix = `:${name}`; + for (const key of collection.keys()) { + const base = bundleBaseKey(key); + if (base !== name && base.endsWith(suffix)) holders.add(base.slice(0, -suffix.length)); + } + return [...holders]; + } + + /** The body of `SchemaRegistry#packageScopedHoldersOf`; the method's docblock there is its contract. */ + static packageScopedHoldersOf(this: Pick, type: string, name: string, callerPackageId?: string | null, options?: { includeDisabledPackages?: boolean }): string[] { + const collection = this.metadata.get(type); + if (!collection) return []; + const composite = this.compositeHoldersOf(collection, name); + const holders = new Set(composite); + const compositeSet = new Set(composite); + for (const { item } of this.bareSlotEntriesOf(type, collection, name)) { + const bound = this.overlaySlotBinding(type, item, name, collection, compositeSet); + if (typeof bound === 'string') holders.add(bound); + } + const served = (ids: string[]): string[] => options?.includeDisabledPackages === true + ? ids + : ids.filter((id) => id === callerPackageId || !this.isPackageDisabled(id)); + const disc = ITEM_KEY_DISCRIMINATORS[type]; + if (!disc || holders.size < 2) return served([...holders]); + const membersOf = new Map>(); + const codeMembersOf = new Map>(); + for (const id of holders) { + membersOf.set(id, this.bundleMembersHeldBy(type, collection, name, id, compositeSet, disc)); + codeMembersOf.set(id, new Set(collectBundle(collection, name, disc, id).local.map((e) => discriminatorValue(e.item, disc.field) || disc.canonical))); + } + return served([...holders].filter((id) => id === callerPackageId || !this.isBundleAddition(id, membersOf, codeMembersOf))); + } + + /** + * [#28802] The members (discriminator values) of `name`'s bundle that + * `packageId` holds: its composite group's, and the bare-slot entries that + * bind to it ({@link overlaySlotBinding}; with `atGate`, as the bare-name + * gate reads them, {@link gateSlotBinding}). + */ + static bundleMembersHeldBy(this: Pick, type: string, collection: Map, name: string, packageId: string, holders: ReadonlySet, disc: { field: string; canonical: string }, atGate = false): Set { + const { local } = collectBundle(collection, name, disc, packageId); + const bound = this.bareSlotEntriesOf(type, collection, name) + .filter(({ item }) => (atGate + ? this.gateSlotBinding(type, item, name, collection, holders) + : this.overlaySlotBinding(type, item, name, collection, holders)) === packageId); + return new Set([...local, ...bound].map((e) => discriminatorValue(e.item, disc.field) || disc.canonical)); + } + + /** + * [#32408, epic ruling 6838245909 option B] A bare-slot row's binding as the + * bare-name gate reads a holder's members ({@link registeredBundleHolding}: + * both doors and the cold boot's seal): {@link overlaySlotBinding}'s, except + * that a row of a customization pair lends its member to a holder only + * where the per-package reading binds it to that holder too (the pair's + * base when it holds the name in a composite slot, else the pair itself: + * the reading every reader applied before {@link pairRowBinding}). + * + * Where the pair-row binding moved the row (to X, a package of the App, + * from the App or from the pair), the gate counts it for no holder. The App + * no longer holds a locale it shipped none of through a row declared for X + * (the narrowing the changeset declares), and X does not gain the row's + * locale either: a package of another namespace on X that ships only that + * locale stays an addition to X's template, refused at both doors in either + * registration order and reported by the seal, as before the binding + * moved. Admitting it waits on the by-name read following the binding (a + * follow-up under #32408): in that state the by-name read answers by + * registration order. The reads keep {@link overlaySlotBinding}. + */ + static gateSlotBinding(this: Pick, type: string, item: unknown, name: string, collection: Map, holders: ReadonlySet): string | null | undefined { + const bound = this.overlaySlotBinding(type, item, name, collection, holders); + const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; + if (typeof stamped !== 'string' || stamped === '' || stamped === 'sys_metadata' || holders.has(stamped)) return bound; + const base = customizationBasePackageId(stamped); + const perPackage = base !== null && holders.has(base) ? base : stamped; + return bound === perPackage ? bound : undefined; + } + + /** The body of `SchemaRegistry#getPackageScopedItem`; the method's docblock there is its contract. */ + static getPackageScopedItem(this: Pick, type: string, name: string, packageId: string, member?: string): T | undefined { + const collection = this.metadata.get(type); + if (!collection) return undefined; + const holders = new Set(this.compositeHoldersOf(collection, name)); + // [#33127] Every stored row bound to it, in the order its stored-row read serves them ({@link rankedForPackage}). + const bound = this.rankedForPackage(this.bareSlotEntriesOf(type, collection, name) + .filter(({ item }) => this.overlaySlotBinding(type, item, name, collection, holders) === packageId + && !pairRowDeclaresAnotherHolder(item, packageId, holders)), packageId); + const disc = ITEM_KEY_DISCRIMINATORS[type]; + if (disc) { + const { local } = collectBundle(collection, name, disc, packageId); + return pickBundleMember([...bound, ...local], disc, member); + } + if (bound.length > 0) return bound[0].item as T; + return collection.get(`${packageId}:${name}`) as T | undefined; + } + + /** The body of `SchemaRegistry#getEnvironmentScopedItem`; the method's docblock there is its contract. */ + static getEnvironmentScopedItem(this: Pick, type: string, name: string, member?: string): T | undefined { + const collection = this.metadata.get(type); + if (!collection) return undefined; + const holders = new Set(this.compositeHoldersOf(collection, name)); + const unbound = this.bareSlotEntriesOf(type, collection, name) + .filter(({ item }) => this.overlaySlotBinding(type, item, name, collection, holders) === null); + const disc = ITEM_KEY_DISCRIMINATORS[type]; + if (disc) return pickBundleMember(unbound, disc, member); + return unbound[0]?.item as T | undefined; + } + + /** The body of `SchemaRegistry#getEnvironmentScopedActions`; the method's docblock there is its contract. */ + static getEnvironmentScopedActions(this: Pick, name: string): T[] { + const collection = this.metadata.get('action'); + if (!collection) return []; + const holders = new Set(this.compositeHoldersOf(collection, name)); + return this.bareSlotEntriesOf('action', collection, name) + .filter(({ item }) => this.overlaySlotBinding('action', item, name, collection, holders) === null) + .map(({ item }) => item as T); + } + + /** The body of `SchemaRegistry#readsByPackage`; the method's docblock there is its contract. */ + static readsByPackage(type: string): boolean { + return isPackageScopedReadType(type); + } + + /** + * The bare-slot entries of `(type, name)`: one, or a bundle's members, canonical first. + * [#33127, ADR-0150 D4] …then its package overlay slots ({@link overlaySlotKey}): every + * stored row of the name, each bound to its package, read as the bare slot's entry is. + * [ADR-0150 D4] An action's package-less items of the other owner keys + * ({@link packagelessActionSlotKey}) follow the bare slot's: each is one + * more package-less item of the name, judged as the bare slot's is. + */ + static bareSlotEntriesOf(type: string, collection: Map, name: string): BundleEntry[] { + const disc = ITEM_KEY_DISCRIMINATORS[type]; + const bare = disc + ? collectBundle(collection, name, disc).bare + : collection.has(name) ? [{ key: name, item: collection.get(name) }] : []; + const otherOwnerKeys = type === 'action' ? packagelessActionSlotEntriesOf(collection, name) : []; + return [...bare, ...otherOwnerKeys, ...overlaySlotEntriesOf(collection, name, disc)]; + } + + /** + * [#33127, ADR-0150 D4] `entries`, the stored rows that bind to `packageId` + * ({@link overlaySlotBinding}), in the order the stored-row read serves them + * for that package (`servedOverlayRowCandidates`, metadata-protocol): the + * declared override of its customization pair (a row of another package + * whose `overrides.package` names it), then its own row, then the + * package-less row, then any other row bound to it (a pair's undeclared + * row). Ties keep the store's order. So the item the registry answers for a + * package does not follow the order its rows were registered in. + */ + static rankedForPackage(entries: readonly BundleEntry[], packageId: string): BundleEntry[] { + const rank = (entry: BundleEntry): number => { + const rowPackage = storedRowPackageOf(entry); + if (rowPackage === packageId) return 1; + if (rowPackage === undefined) return 2; + return declaredOverrideBase(entry.item) === packageId ? 0 : 3; + }; + return entries.map((entry, index) => ({ entry, index, rank: rank(entry) })) + .sort((a, b) => a.rank - b.rank || a.index - b.index) + .map(({ entry }) => entry); + } + + /** The body of `SchemaRegistry#removePackageOverlaySlots`; the method's docblock there is its contract. */ + static removePackageOverlaySlots(this: Pick, type: string, name: string, member: string | undefined, select: (packageId: string) => boolean): string[] { + const collection = this.metadata.get(type); + if (!collection) return []; + const disc = ITEM_KEY_DISCRIMINATORS[type]; + const slots = overlaySlotEntriesOf(collection, name, disc); + const removed = new Set(); + for (const { key } of disc && member !== undefined ? entriesOfMember(slots, disc, member) : slots) { + const packageId = parseOverlaySlotKey(key)!.packageId; + if (!select(packageId)) continue; + this.noteItem(type, key); + collection.delete(key); + removed.add(packageId); + this.log(`[Registry] Removed package overlay slot ${type}: ${name} of ${packageId} (its stored row is gone)`); + } + if (removed.size > 0 && (type === 'picklist' || type === 'action')) this.invalidateAll(); + return [...removed]; + } + + /** + * [#33127] The overlay tier of a read naming no package ({@link getItem}), + * when the bare slot is empty and package overlay slots hold rows of the + * name: the rows of `preferred` first ({@link rankedForPackage}), then each + * other package's, holder by holder in the item store's order (the code + * holders, as the composite fallback reads them), then by package id. Never + * the order the rows were registered in. + */ + static orderedOverlayTier(this: Pick, type: string, collection: Map, name: string, entries: readonly BundleEntry[], preferred?: string): BundleEntry[] { + if (entries.length <= 1) return [...entries]; + const composite = new Set(this.compositeHoldersOf(collection, name)); + const byHolder = new Map(); + for (const entry of entries) { + const bound = this.overlaySlotBinding(type, entry.item, name, collection, composite); + const holder = typeof bound === 'string' ? bound : `${OVERLAY_SLOT_MARK}${storedRowPackageOf(entry) ?? ''}`; + byHolder.set(holder, [...(byHolder.get(holder) ?? []), entry]); + } + const order = [...composite]; + const holders = [...byHolder.keys()].sort((a, b) => { + if (a === preferred || b === preferred) return a === preferred ? -1 : 1; + const ia = order.indexOf(a); + const ib = order.indexOf(b); + if (ia !== ib) return ia === -1 ? 1 : ib === -1 ? -1 : ia - ib; + return a < b ? -1 : a > b ? 1 : 0; + }); + return holders.flatMap((holder) => this.rankedForPackage(byHolder.get(holder)!, holder)); + } + + /** The body of `SchemaRegistry#getArtifactItem`; the method's docblock there is its contract. */ + static getArtifactItem(this: Pick, type: string, name: string, currentPackageId?: string, member?: string): T | undefined { + if (type === 'object' || type === 'objects') { + // [ADR-0029 D9.6] Artifact identity is read from the OWNER contributor's + // layer, NEVER from the merged body. The merged body deliberately keeps + // the overlay's `_provenance: 'org'` (D9.2 — the resolved schema must + // not move), so applying the artifact test to it answers about whichever + // layer is on top rather than about the code layer, which is exactly how + // this predicate came to answer `false` for a name a code package still + // ships (#6853 P3) and silently disarmed `saveMetaItem`'s overlay gate + // and tier 3 of the delete heal. + // + // What comes BACK is the code-layer body (owner + its extenders), which + // is what every consumer of this lookup wants — the packaged baseline an + // overlay customizes, and the `_lock`/`_packageId`/`_provenance` + // envelope that must win over an overlay (ADR-0010 §3.3). With no + // overlay registered it is byte-for-byte the previous answer. + const fqn = this.resolveObjectKey(name); + const ownerLayer = fqn === undefined ? undefined : this.resolveOwnerLayer(fqn); + return ownerLayer && isCodeArtifactBody(ownerLayer) ? (ownerLayer as T) : undefined; + } + const collection = this.metadata.get(type); + if (!collection) return undefined; + + // [#7730] Bundle-aware form of the three tiers below, for a type whose + // identity is (name, discriminator). Same order — prefer-local composite, + // then any composite, then the bare-key fallback with its package check — + // over bundle GROUPS instead of single keys, with the canonical member + // first inside each group. + const disc = ITEM_KEY_DISCRIMINATORS[type]; + if (disc) { + const { bare, local, other } = collectBundle(collection, name, disc, currentPackageId); + const narrow = (entries: BundleEntry[]) => (member === undefined ? entries : entriesOfMember(entries, disc, member)); + for (const { item } of narrow([...local, ...other])) { + if (isCodeArtifactBody(item)) return item as T; + } + for (const { item } of narrow(bare)) { + if (isCodeArtifactBody(item) && (!currentPackageId || item._packageId === currentPackageId)) return item as T; + } + return undefined; + } + + // ADR-0048 prefer-local: when the caller resolves within a package, the + // artifact owned by that package wins over a first-match composite scan, + // so two installed packages shipping the same name don't resolve by Map + // iteration order. + if (currentPackageId) { + const local = collection.get(`${currentPackageId}:${name}`) as any; + if (local && local._packageId && local._packageId !== 'sys_metadata' && !isTenantAuthored(local)) return local as T; + } + for (const [key, item] of collection) { + if (key !== name && key.endsWith(`:${name}`)) { + const it = item as any; + if (it && it._packageId && it._packageId !== 'sys_metadata' && !isTenantAuthored(it)) return item as T; + } + } + // Bare-key fallback: a runtime/DB overlay rehydrated under the plain name. + // ADR-0048 (#1828) — when the caller resolves *within a package*, only accept + // this entry if it belongs to that package. The bare slot holds a single + // row (last hydrate wins), so once the unscoped list stopped collapsing + // colliding rows, an overlay from package A hydrated here would otherwise be + // grafted as the "artifact" of package B's same-name row — mislabeling its + // `_packageId`/lock. A package-less caller (`currentPackageId` omitted) keeps + // the legacy best-effort first-match. + const direct = collection.get(name) as any; + if ( + direct && direct._packageId && direct._packageId !== 'sys_metadata' && !isTenantAuthored(direct) && + (!currentPackageId || direct._packageId === currentPackageId) + ) { + return direct as T; + } + return undefined; + } + + /** The body of `SchemaRegistry#removeRuntimeShadow`; the method's docblock there is its contract. */ + static removeRuntimeShadow(this: Pick, type: string, name: string, member?: string): boolean { + const collection = this.metadata.get(type); + if (!collection) return false; + + // [#7730] For a discriminated type the shadow sits at `name@` + // — nothing is ever stored under the plain name — so the plain-key test + // would silently answer "no shadow" for every email template. + // + // [#22877] The overlay store now keys a row by its member + // (`sys_metadata.bundle_member`), so a by-name reset removes ONE member's + // row and the heal that follows it names that member: resetting `zh-CN` + // must leave the `en-US` overlay registered, because its row is still + // stored and still served. Sweeping the group, as this did while the + // store kept one row per name, dropped every other member's overlay until + // the next boot. A caller naming no member keeps the group sweep, the + // shape `unregisterItem` takes for a withdrawal by name. + const disc = ITEM_KEY_DISCRIMINATORS[type]; + if (disc) { + const { bare, local, other } = collectBundle(collection, name, disc); + const shadows = member === undefined ? bare : entriesOfMember(bare, disc, member); + if (shadows.length === 0) return false; + // [#23074] A named member's OWN artifact, the test this tier has always + // made for a name: a shadow is lifted only where a packaged artifact + // takes its place. An overlay of a member the package does NOT ship has + // none, so this tier declines, and the heal's last tier retires the + // entry (`removeOverlayEntry`): the protocol grafts the artifact + // envelope onto the shipped member alone, so that overlay is registered + // as the tenant row it is and the retirement takes it. (#22877 stage 2 + // lifted it here on ANY member's artifact, because the bundle's envelope + // grafted onto it made the retirement refuse it as code.) The group + // sweep, naming no member, keeps the bundle's artifact, any member's. + const artifacts = member === undefined ? [...local, ...other] : entriesOfMember([...local, ...other], disc, member); + const artifact = artifacts.find(({ item }) => item?._packageId && item._packageId !== 'sys_metadata'); + if (!artifact) return false; + for (const { key } of shadows) { + collection.delete(key); + this.log(`[Registry] Removed runtime shadow ${type}: ${key} (artifact ${artifact.item._packageId} restored)`); + } + return true; + } + + if (!collection.has(name)) return false; + for (const [key, item] of collection) { + if (key !== name && key.endsWith(`:${name}`)) { + const it = item as any; + if (it && it._packageId && it._packageId !== 'sys_metadata') { + // [ADR-0150 D4] …the package-less action's own entry, which may sit + // in another owner key's slot ({@link packagelessActionEntryToRetire}). + if (type === 'action') this.retirePackagelessActionSlot(collection, name, this.packagelessActionEntryToRetire(collection, name)); + else collection.delete(name); + // [#28356] An object may carry the shadow as a bound action; the + // artifact it uncovers may bind elsewhere ({@link attachBoundActions}). + if (type === 'action') this.invalidateAll(); + this.log(`[Registry] Removed runtime shadow ${type}: ${name} (artifact ${it._packageId} restored)`); + return true; + } + } + } + return false; + } + + /** The body of `SchemaRegistry#removeOverlayEntry`; the method's docblock there is its contract. */ + static removeOverlayEntry(this: Pick, type: string, name: string, member?: string): boolean { + const collection = this.metadata.get(type); + if (!collection) return false; + + // [#7730] Same relocation as {@link removeRuntimeShadow}: a discriminated + // type's overlay slot is `name@`, so the plain-key read + // finds nothing and the deleted overlay would keep being served for the + // life of the process — the very residue #5079 removed. The refusal is + // still per entry: a bundle member that IS a packaged artifact stays. + // [#22877] …and a named member retires that member's entry only. + const disc = ITEM_KEY_DISCRIMINATORS[type]; + if (disc) { + const { bare } = collectBundle(collection, name, disc); + let removed = false; + for (const { key, item } of member === undefined ? bare : entriesOfMember(bare, disc, member)) { + if (isCodeArtifactBody(item)) continue; + collection.delete(key); + this.log(`[Registry] Removed overlay entry ${type}: ${key} (no layer serves it any more)`); + removed = true; + } + return removed; + } + + if (!collection.has(name)) return false; + // [ADR-0150 D4] A package-less action's own entry may sit in another + // owner key's slot beside the bare one ({@link packagelessActionEntryToRetire}). + const key = type === 'action' ? this.packagelessActionEntryToRetire(collection, name) : name; + const plain = collection.get(key) as any; + if (plain && plain._packageId && plain._packageId !== 'sys_metadata' && !isTenantAuthored(plain)) { + return false; + } + if (type === 'action') this.retirePackagelessActionSlot(collection, name, key); + else collection.delete(name); + // [#28356] …and an object may carry the retired entry as a bound action + // ({@link attachBoundActions}): drop the cached folds, as + // {@link unregisterItem} does, or a deleted runtime action stays listed. + if (type === 'action') this.invalidateAll(); + this.log(`[Registry] Removed overlay entry ${type}: ${name} (no layer serves it any more)`); + return true; + } + + /** The body of `SchemaRegistry#listItems`; the method's docblock there is its contract. */ + static listItems(this: Pick, type: string, packageId?: string, options?: { includeDisabledPackages?: boolean } & ObjectExtensionFoldOptions): T[] { + // Special handling for 'object' and 'objects' types - use objectContributors + if (type === 'object' || type === 'objects') { + return this.getAllObjects(packageId, { + withoutExtensionsOf: options?.withoutExtensionsOf, + onlyExtensionsOf: options?.onlyExtensionsOf, + }) as unknown as T[]; + } + + const items = Array.from(this.metadata.get(type)?.values() || []) as T[]; + let result = items; + if (packageId) { + result = result.filter((item: any) => item._packageId === packageId); + } + // Hide metadata owned by a disabled package so the console (app switcher, + // view lists, dashboards, …) stops surfacing it after a disable. The + // `package` type itself is never filtered — the Packages page must still + // list disabled packages so they can be re-enabled. Disable is reversible: + // items remain registered and reappear on enable. + if (type !== 'package' && options?.includeDisabledPackages !== true) { + result = result.filter((item: any) => !this.isPackageDisabled((item as any)?._packageId)); + } + return result; + } + + /** The body of `SchemaRegistry#isPackageDisabled`; the method's docblock there is its contract. */ + static isPackageDisabled(this: Pick, packageId?: string): boolean { + if (!packageId) return false; + const pkg = this.getPackage(packageId); + return pkg?.enabled === false || pkg?.status === 'disabled'; + } + + /** The body of `SchemaRegistry#isObjectPackageDisabled`; the method's docblock there is its contract. */ + static isObjectPackageDisabled(this: Pick, name: string): boolean { + const fqn = this.resolveObjectKey(name); + if (fqn === undefined) return false; + return this.isPackageDisabled(this.getObjectOwner(fqn)?.packageId); + } + + /** The body of `SchemaRegistry#isFlowOfDisabledPackage`; the method's docblock there is its contract. */ + static isFlowOfDisabledPackage(this: Pick, name: string): boolean { + const collection = this.metadata.get('flow'); + if (!collection || typeof name !== 'string' || name === '') return false; + let found = false; + for (const [key, flow] of collection) { + if (key !== name && !key.endsWith(`:${name}`)) continue; + if (!this.isPackageDisabled((flow as { _packageId?: string } | undefined)?._packageId)) return false; + found = true; + } + return found; + } + + /** The body of `SchemaRegistry#isActionWithheldByDisabledPackage`; the method's docblock there is its contract. */ + static isActionWithheldByDisabledPackage(this: Pick, action: unknown): boolean { + if (!action || typeof action !== 'object') return false; + const a = action as { _packageId?: string; objectName?: unknown; type?: unknown; target?: unknown; params?: unknown }; + if (this.isPackageDisabled(a._packageId)) return true; + if (!this.hasDisabledPackage()) return false; + if (typeof a.objectName === 'string' && a.objectName !== '' && this.isObjectPackageDisabled(a.objectName)) return true; + if (a.type === 'flow' && typeof a.target === 'string' && this.isFlowOfDisabledPackage(a.target)) return true; + if (Array.isArray(a.params)) { + for (const param of a.params) { + const override = (param as { objectOverride?: unknown } | null | undefined)?.objectOverride; + if (typeof override === 'string' && override !== '' && this.isObjectPackageDisabled(override)) return true; + } + } + return false; + } + + /** [#28877] Whether any installed package is disabled — the cheap gate before the launch walk. */ + static hasDisabledPackage(this: Pick): boolean { + for (const pkg of this.metadata.get('package')?.values() ?? []) { + const p = pkg as { enabled?: boolean; status?: string } | undefined; + if (p?.enabled === false || p?.status === 'disabled') return true; + } + return false; + } + + /** The body of `SchemaRegistry#getRegisteredTypes`; the method's docblock there is its contract. */ + static getRegisteredTypes(this: Pick): string[] { + const types = Array.from(this.metadata.keys()); + // Always include 'object' even if stored separately + if (!types.includes('object') && this.objectContributors.size > 0) { + types.push('object'); + } + return types; + } +} diff --git a/packages/objectql/src/registry-packages.ts b/packages/objectql/src/registry-packages.ts new file mode 100644 index 00000000000..307544795d3 --- /dev/null +++ b/packages/objectql/src/registry-packages.ts @@ -0,0 +1,708 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33949 codemod] The "Package management" section of `SchemaRegistry`: the + * bodies of `installPackage`, `coOwnersOf`, `refuseCoOwnedObjectNameCollision`, + * `refuseNamespaceNameConflicts`, `refuseNamespaceObjectNameClashes`, + * `refuseResidualNamespaceNameConflicts`, `recordResidualNamespaceHolder`, + * `refuseNamespaceHolderConflicts`, `uninstallPackage`, `getPackage`, + * `recordResidualOwnerDependencies`, `residualOwnerDependenciesOf`, + * `getAllPackages`, `enablePackage`, `disablePackage`, `updatePackageManifest` + * moved out of `registry.ts` whole by `scripts/codemod-split-registry.mjs`. + * + * A MOVE, not a redesign. {@link PackagesFacet} is a stateless facet of `SchemaRegistry` + * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` + * method of the same name exactly as it stood: not re-indented, not + * formatted, not edited. The method stays on the class as a one-call stub with + * its signature, so the published surface, an instance spy and a subclass + * override are what they were. A public or protected method keeps its + * docblock on the class, the contract; a private method's docblock moved here + * with its body. The state stays on `SchemaRegistry`. + * + * The bodies reach the class through {@link PackagesHost}: exactly the + * `SchemaRegistry` members they read, private ones included. Every stub hands + * its body, by a `.call`, the same host: `packagesHost` in + * `registry-facet-hosts.ts`, built once per registry by the class's static + * block. Methods go as closures that call them on the registry at call time, so + * a subclass override or an instance spy is still the one reached; fields and + * accessors go as GETTERS, read when the body reads them, as they were on the + * class, and a field a body assigns goes with a SETTER that assigns it on the + * registry. + * + * Values that stayed in `registry.ts` are imported from it (lever (a) of + * ruling 6591753301); every use is inside a function body, so the import + * cycle is never read at evaluation time. `index.ts` re-exports nothing from + * this module. + */ + +import { isTenantAuthored } from '@objectstack/metadata-core'; +import { InstalledPackageSourceSchema, findNamespaceObjectNameClashes, validateNamespaceNotAddressLiteral } from '@objectstack/spec/kernel'; +import type { InstalledPackage, InstalledPackageSource, NamespaceHolder, ObjectNameHolder, ObjectStackManifest } from '@objectstack/spec/kernel'; +import { artifactPackageId, declaredDependencyIds } from '@objectstack/core'; +import { isNamespaceJudged, namespaceNameConflicts, namespacePackageOf } from './namespace-co-owner-names.js'; +import type { NamespacePackage } from './namespace-co-owner-names.js'; +import { + computeFQN, declaredOwnedObjectNames, isShareableNamespace, toRecordManifest, withDeclaredFunctionEntries, +} from './registry-top-level.js'; +import type { ArtifactInstallScope, CoOwnershipSource } from './registry-top-level.js'; +import { + ArtifactObjectNameConflictError, NamespaceAddressLiteralError, NamespaceConflictError, NamespaceNameConflictError, + NamespaceObjectNameConflictError, +} from './registry.js'; +import type { SchemaRegistry } from './registry.js'; + +/** + * The `SchemaRegistry` members the bodies of {@link PackagesFacet} read — every one of + * them, and no other. Each is typed AS the class member (an indexed access on + * the class), so a change to a member's signature on the class is a change + * here too. `readonly` members are the getters; the others are fields a body + * assigns. + */ +export interface PackagesHost { + announcePackageEnabled: SchemaRegistry['announcePackageEnabled']; + announcePackageUninstalled: SchemaRegistry['announcePackageUninstalled']; + readonly bareNameHoldersSealed: SchemaRegistry['bareNameHoldersSealed']; + coOwnersOf: SchemaRegistry['coOwnersOf']; + readonly collisionPolicy: SchemaRegistry['collisionPolicy']; + debug: SchemaRegistry['debug']; + forgetBareNameClaims: SchemaRegistry['forgetBareNameClaims']; + forgetDefaultRecordPageClaims: SchemaRegistry['forgetDefaultRecordPageClaims']; + forgetSecurityCatalogClaims: SchemaRegistry['forgetSecurityCatalogClaims']; + getNamespaceOwners: SchemaRegistry['getNamespaceOwners']; + getObjectOwner: SchemaRegistry['getObjectOwner']; + getPackage: SchemaRegistry['getPackage']; + readonly initialDisabledPackageIds: SchemaRegistry['initialDisabledPackageIds']; + invalidateAll: SchemaRegistry['invalidateAll']; + isPackageDisabled: SchemaRegistry['isPackageDisabled']; + listItems: SchemaRegistry['listItems']; + log: SchemaRegistry['log']; + readonly metadata: SchemaRegistry['metadata']; + readonly namespaceRegistry: SchemaRegistry['namespaceRegistry']; + noteItem: SchemaRegistry['noteItem']; + noteItemType: SchemaRegistry['noteItemType']; + readonly objectContributors: SchemaRegistry['objectContributors']; + readonly packageBodyItems: SchemaRegistry['packageBodyItems']; + recordBareNameClaims: SchemaRegistry['recordBareNameClaims']; + recordDefaultRecordPageClaims: SchemaRegistry['recordDefaultRecordPageClaims']; + recordSecurityCatalogClaims: SchemaRegistry['recordSecurityCatalogClaims']; + refuseBareNameConflicts: SchemaRegistry['refuseBareNameConflicts']; + refuseCoOwnedObjectNameCollision: SchemaRegistry['refuseCoOwnedObjectNameCollision']; + refuseDefaultRecordPageConflicts: SchemaRegistry['refuseDefaultRecordPageConflicts']; + refuseNamespaceHolderConflicts: SchemaRegistry['refuseNamespaceHolderConflicts']; + refuseNamespaceNameConflicts: SchemaRegistry['refuseNamespaceNameConflicts']; + refuseNamespaceObjectNameClashes: SchemaRegistry['refuseNamespaceObjectNameClashes']; + refuseSecurityCatalogNameConflicts: SchemaRegistry['refuseSecurityCatalogNameConflicts']; + registerNamespace: SchemaRegistry['registerNamespace']; + readonly residualNamespaceHolders: SchemaRegistry['residualNamespaceHolders']; + readonly residualOwnerDependencies: SchemaRegistry['residualOwnerDependencies']; + setPackageNavContributions: SchemaRegistry['setPackageNavContributions']; + unregisterItemsByPackage: SchemaRegistry['unregisterItemsByPackage']; + unregisterNamespace: SchemaRegistry['unregisterNamespace']; + unregisterObjectsByPackage: SchemaRegistry['unregisterObjectsByPackage']; + warnAddressLiteralNamespaceOnce: SchemaRegistry['warnAddressLiteralNamespaceOnce']; +} + +/** The bodies of the "Package management" section of `SchemaRegistry`: a stateless facet, called by the class's stubs. */ +export class PackagesFacet { + /** The body of `SchemaRegistry#installPackage`; the method's docblock there is its contract. */ + static installPackage(this: Pick, manifest: ObjectStackManifest, source: InstalledPackageSource, settings?: Record, scope?: ArtifactInstallScope): InstalledPackage { + // [#23972] `source` is the CALLER's statement of where this package came + // from — the door that registers it is the only party that knows + // (`registerApp`: code or marketplace; `installPackage` in + // `@objectstack/metadata-protocol`, the `POST /packages` fallback and the + // `sys_packages` rehydrate: authored). The registry records it and never + // infers it, so an untyped caller that omits it, or spells a value outside + // the closed set, is refused here — ahead of every check and mutation + // below, so a refused call leaves no row behind — rather than recording a + // row the read door would serve without an origin. + if (!InstalledPackageSourceSchema.safeParse(source).success) { + throw new TypeError( + `[Registry] installPackage('${String(manifest?.id)}') needs the package's source, one of ` + + `${InstalledPackageSourceSchema.options.map((v) => `'${v}'`).join(' | ')}; received ` + + `${source === undefined ? 'none' : JSON.stringify(source)}. The door that registers a package ` + + `states where it came from; the registry does not guess.`, + ); + } + // [ADR-0130 D1] The co-owners of this package — the OTHER packages the same + // release artifact is delivering. Computed once and read by both halves of + // the install-time pair below, so the relaxation and the refusal cannot + // disagree about who counts as a co-owner. + // + // Everything inside one artifact is built, versioned, downloaded and + // installed as one atomic act by one publisher, and D1 makes that joint + // delivery the ownership proof itself — nothing else asserts it, and D8 + // deliberately adds no manifest field that could drift from it. + // + // The self-exclusion is by the SAME key the registry installs under + // ({@link artifactPackageId}), not by `manifest.id` alone: a manifest + // carrying only `name` is installed under that name, and a co-owner set + // that still contained it would let the package "co-own" with itself. + // + // [ADR-0141 D5] …and, beside the artifact, the packages a declared + // dependency makes co-owners of the namespace — see {@link coOwnersOf}. + const selfId = artifactPackageId(manifest); + const coOwners = this.coOwnersOf(manifest, selfId, scope); + + // ADR-0048 Phase 1 — install-time namespace gate. Refuse a package whose + // namespace is already owned by an installed package that is not a co-owner + // of it; this is the constraint the object/table layer enforces implicitly + // (a duplicate `CREATE TABLE _` fails at the DB), made explicit and + // early. Same-package reinstall/reload is excluded (owner === manifest.id), + // shareable platform namespaces (base/system/sys) are exempt, and — since + // ADR-0130 D1 — so is a co-owner delivered by the same artifact. + // + // ⛔ This relaxation may not exist without the object-name check below it. + // Namespace exclusivity has been silently proxying for "no two packages + // define the same object name" (ADR-0048 §3.2's own grounding), so dropping + // the proxy alone is the one path in ADR-0130 that can damage customer data. + // ADR-0130 D3 states the pair as a machine constraint for that reason, and + // `registry-artifact-co-ownership.test.ts` asserts it as ONE proposition. + if (manifest.namespace && !isShareableNamespace(manifest.namespace)) { + const conflictOwner = this.getNamespaceOwners(manifest.namespace).find( + (owner) => owner !== manifest.id && !coOwners.has(owner), + ); + if (conflictOwner) { + if (this.collisionPolicy === 'warn') { + console.warn( + `[Registry] Namespace conflict (downgraded to warning via ` + + `OS_METADATA_COLLISION=warn): namespace "${manifest.namespace}" is ` + + `already owned by "${conflictOwner}"; installing "${manifest.id}" ` + + `anyway. See ADR-0048.`, + ); + } else { + throw new NamespaceConflictError(manifest.namespace, conflictOwner, manifest.id); + } + } + } + + // [ADR-0130 D3] The other half of the pair: per-object-name uniqueness + // across the co-owners of one artifact, install-time and ahead of any DDL. + // Placed ahead of EVERY mutation this method makes, so a refused package + // leaves no record, no namespace ownership and no half-applied install + // behind — the same disposition the namespace gate above has. + this.refuseCoOwnedObjectNameCollision(manifest, selfId, coOwners); + + // [ADR-0150 D2, child N] The object rule above, extended to the twelve + // bucket 2/3 types phase 1 does not cover: inside one namespace, a + // `(type, name)` has one package holder, so that NAMESPACE/NAME names one + // item (`namespace-co-owner-names.ts`). Ahead of every mutation, like the + // check above; at every install, boot included, like it; with no + // `collisionPolicy` downgrade, like the bare-name rule below. + this.refuseNamespaceNameConflicts(manifest); + + // [ADR-0150 D5, child G1] A namespace never spells a literal segment of + // the REST address grammar: the router reads the literal first, so the + // package's qualified addresses would reach another door. Ahead of every + // mutation, with no `collisionPolicy` downgrade — at the running install + // door only. + // + // A package the boot registers keeps booting: no release before this rule + // refused one, and what the literal costs it is its qualified path alone, + // which never existed before the rule did. Two readings name it, the + // bare-name rule's (`refuseBareNameConflicts`): until the cold boot seals + // the holders, every package registering is one the deployment holds + // (installed, or shipped in its code); after it, a package already + // registered under its own id AND the same namespace is a reload, a + // re-install or an upgrade (the namespace gate's same-package reading + // above). An upgrade that MOVES to a literal namespace is a new occupation + // of it and is refused. A held package is warned once and installs; it is + // read by `?package=`. + { + const literalRefusal = validateNamespaceNotAddressLiteral(manifest.namespace); + if (literalRefusal) { + const packageId = selfId ?? manifest.id; + const alreadyHeld = !this.bareNameHoldersSealed + || this.getPackage(packageId)?.manifest?.namespace === manifest.namespace; + if (!alreadyHeld) { + throw new NamespaceAddressLiteralError(manifest.namespace as string, literalRefusal, packageId); + } + this.warnAddressLiteralNamespaceOnce(packageId, literalRefusal); + } + } + + // [ADR-0150 D6, child K] A namespace never equals an object name, in + // either direction, and no object is short-named `custom` — the spec's one + // predicate, against what the environment already holds. Ahead of every + // mutation, at every install, boot included, with no `collisionPolicy` + // downgrade, like the check above. + this.refuseNamespaceObjectNameClashes(manifest, selfId); + + // The security catalog's one-name-one-holder rule (ADR-0048 §3.4 narrowed + // for positions, permission sets and capabilities; maintainer ruling Q4 = A + // on #15196) — also ahead of every mutation, for the same reason. No + // co-owner exemption: two packages of one artifact sharing a catalog name + // are as ambiguous to a bare-name assignment as two strangers are, and no + // `collisionPolicy` downgrade (`security-catalog-namespace.ts`). + this.refuseSecurityCatalogNameConflicts(manifest, selfId); + + // One default record page per object per deployment (maintainer ruling A + // on objectstack#23015; `default-record-page.ts`) — also ahead of every + // mutation, for the same reason. No co-owner exemption and no + // `collisionPolicy` downgrade: two packages of one artifact shipping a + // default for one object leave the record view to registration order + // exactly as two strangers do. + this.refuseDefaultRecordPageConflicts(manifest, selfId); + + // One name, one package holder for the bare-name types — a picklist, a + // mapping, a dataset, an API, an object-less action, a flow, a webhook, a + // connector, an email template: the names their consumers resolve with no + // package (ADR-0048 §3.4 narrowed; maintainer ruling C on #30798, phase 1; + // `bare-name-one-holder.ts`). Also ahead of every mutation, with no + // co-owner exemption and no `collisionPolicy` downgrade, for the security + // catalog's reasons. Judged only once the cold boot has sealed the holders: + // during a boot every package registers again, and a deployment that + // already holds a clash is the cold boot's to report. + this.refuseBareNameConflicts(manifest, selfId); + + const now = new Date().toISOString(); + + // [#18877] 「缺省 = 保持,有旗 = 设置」 — an install that was not asked to move + // the lifecycle state does not move it. The install verb carries no + // lifecycle argument at all, so at THIS layer every install is + // 「缺省」 and the whole rule is: an EXISTING row keeps its own + // `enabled` / `status` / `statusChangedAt`, and the boot seed decides only + // for an id that has no row yet. The door above is where 「有旗」 is spelled + // (`enableOnInstall` ⇒ `enablePackage` / `disablePackage`, + // `packages/runtime/src/domains/packages.ts`). + // + // ⚠️ The seed set is a BOOT HYDRATION input, not a standing verdict. It is + // filled once, before any registration, from the durable disable file + // (`AppPlugin.seedPersistedDisabledPackages`) and no lifecycle verb updates + // it. Consulting it on every install is what made the most recent explicit + // operator action revertible by an unrelated re-install: disable → restart + // → `PATCH /packages/:id/enable` (200, registry true, disk cleared) → + // flag-absent `install(m, { overwrite: true })` → the seed still listed the + // id → the row landed disabled again, and since #18752 the disk follows the + // row, so the enable was gone after the next restart with no error + // anywhere. Reading the ROW first is what makes the seed's answer + // unreachable once a row exists — and a row always exists by the time an + // operator can have acted on it. + const existing = this.getPackage(manifest.id); + const seeded = this.initialDisabledPackageIds.has(manifest.id); + const lifecycle: Pick = existing + ? { + status: existing.status, + enabled: existing.enabled, + // Carried, never restamped: `statusChangedAt` answers "when did an + // operator last move this package", and a re-install is not such a + // move. A row that never carried one still does not. + ...(existing.statusChangedAt !== undefined + ? { statusChangedAt: existing.statusChangedAt } + : {}), + } + : { + status: seeded ? 'disabled' : 'installed', + enabled: !seeded, + ...(seeded ? { statusChangedAt: now } : {}), + }; + + const pkg: InstalledPackage = { + // The RECORD's manifest, not the caller's live object — see + // {@link toRecordManifest}. Every other read below (`manifest.id`, + // `manifest.namespace`) deliberately keeps reading the ARGUMENT: the + // projection is what the registry hands out, never what it decides with. + // + // {@link withDeclaredFunctionEntries} runs FIRST and at this boundary + // only: it makes the two authored `functions` spellings structurally + // equal so the structural projection reports BOTH, without teaching the + // projection a key name. + manifest: toRecordManifest(withDeclaredFunctionEntries(manifest)), + source, + ...lifecycle, + installedAt: now, + updatedAt: now, + settings, + }; + + // Register namespace if present + if (manifest.namespace) { + this.registerNamespace(manifest.namespace, manifest.id); + } + + this.noteItemType('package'); + if (!this.metadata.has('package')) { + this.metadata.set('package', new Map()); + } + const collection = this.metadata.get('package')!; + if (collection.has(manifest.id)) { + // Re-install of an already-registered package manifest (rebuild / HMR) is + // a normal path, not an error — keep it at debug (#3420). + this.debug(`[Registry] Overwriting package: ${manifest.id}`); + } + this.noteItem('package', manifest.id); + collection.set(manifest.id, pkg); + // A row that lands disabled withholds the package's `extend` contributions + // from every served fold ({@link withholdDisabledExtensions}), and a fold + // cached before the row existed read it as enabled. + if (this.isPackageDisabled(manifest.id)) this.invalidateAll(); + // The names this package now holds, recorded whether or not an item store + // entry will carry them (see `securityCatalogClaims`). + if (selfId !== undefined) this.recordSecurityCatalogClaims(selfId, manifest); + // …and the default record pages it declares (see `defaultRecordPageClaims`). + if (selfId !== undefined) this.recordDefaultRecordPageClaims(selfId, manifest); + // …and its bare names of the covered types (see `bareNameClaims`). + if (selfId !== undefined) this.recordBareNameClaims(selfId, manifest); + this.log(`[Registry] Installed package: ${manifest.id} (${manifest.name})`); + return pkg; + } + + /** + * The co-owners of the incoming package's namespace, each with the source + * that makes it one. Both halves of the install-time pair read this one map, + * so the namespace relaxation and the object-name refusal cannot disagree + * about who counts. + * + * Two sources: + * + * - **`artifact`** (ADR-0130 D1) — the other packages the same release + * artifact is delivering, as the load path's scope names them. + * - **a declared dependency** (ADR-0141 D5) — for a non-shareable namespace + * N the incoming package P uses: + * - `dependency`: P declares an installed X whose namespace is N; or a + * sitting owner of N declares P. The relation is declared by the + * dependent alone and read from either end, so the parent registering + * second — an upgrade, or a boot that hydrated the module first — meets + * its module as a co-owner instead of `NAMESPACE_CONFLICT`. + * - `sibling`: a sitting owner S of N and P each directly declare one + * installed package Q whose namespace is N — two modules under one + * declared parent, neither declaring the other. + * + * Direct declarations only, never the closure the save door walks. Q's + * namespace is read from Q's installed record: ⛔ two packages that merely + * declare one common package co-own nothing unless that package is installed + * and holds N, or any two packages sharing a utility dependency could share a + * namespace. Nothing is persisted: the relation is the declarations of the + * packages the registry holds now. + */ + static coOwnersOf(this: Pick, manifest: ObjectStackManifest, selfId: string | undefined, scope: ArtifactInstallScope | undefined): Map { + const coOwners = new Map(); + for (const id of scope?.packageIds ?? []) if (id !== selfId) coOwners.set(id, 'artifact'); + + const namespace = manifest.namespace; + if (!namespace || isShareableNamespace(namespace) || selfId === undefined) return coOwners; + + const holdsNamespace = (id: string) => this.getPackage(id)?.manifest?.namespace === namespace; + // P's declared parents that hold N — the parent arm from P's side, and the + // Q set the sibling arm matches against. + const parents = new Set(declaredDependencyIds(manifest).filter((id) => id !== selfId && holdsNamespace(id))); + for (const id of parents) if (!coOwners.has(id)) coOwners.set(id, 'dependency'); + + for (const owner of this.getNamespaceOwners(namespace)) { + if (owner === selfId || coOwners.has(owner)) continue; + const declaredByOwner = declaredDependencyIds(this.getPackage(owner)?.manifest); + if (declaredByOwner.includes(selfId)) coOwners.set(owner, 'dependency'); + else if (declaredByOwner.some((id) => parents.has(id))) coOwners.set(owner, 'sibling'); + } + return coOwners; + } + + /** + * [ADR-0130 D3] Refuse an install in which a CO-OWNER of the incoming package + * — a package delivered by the same release artifact — already owns an object + * name the incoming package claims. + * + * ## Why the refusal is scoped to co-owners + * + * It is exactly the surface D1 opened. Before D1 the namespace gate refused + * two packages sharing a namespace outright, and that refusal was doing this + * job by proxy; a name claimed by a package from a DIFFERENT artifact is still + * that gate's business and still refused there. So this check adds no refusal + * the platform did not already make in some form — per #14122 §6.2 it can only + * reject a configuration that would have failed at the DB anyway, earlier and + * more legibly — while the co-owner case, which D1 has just admitted, is the + * one that would otherwise reach the DB unchecked. + * + * ## One resolution, shared with the read path + * + * The name is resolved through {@link computeFQN} — the same call + * `registerObject` makes on the very definitions this manifest is about to + * register — rather than by a private spelling. A gate that resolved names its + * own way could refuse an entry `registerObject` would have accepted under a + * different key, or wave through the one it would have refused. + * + * ## The one carve-out + * + * A tenant-authored sitting owner is skipped, because `registerObject`'s + * late-install path (ADR-0029 D9 §6.1) does not refuse that case either: a + * code package that ships a name a `sys_metadata` row already holds TAKES + * ownership and re-classifies the tenant contribution as its overlay. Refusing + * here would break an install the very next call would have completed. + * + * @throws {ArtifactObjectNameConflictError} ADR-0112 envelope, naming both + * packages and the object. + */ + static refuseCoOwnedObjectNameCollision(this: Pick, manifest: ObjectStackManifest, selfId: string | undefined, coOwners: ReadonlyMap): void { + // No artifact scope, or an artifact carrying one package: nothing this + // method can refuse. Stated as an early return rather than left to fall out + // of the loop, because "the single-package path is untouched" (D7) is a + // property of this code, not a coincidence of its inputs. + if (coOwners.size === 0) return; + + for (const shortName of declaredOwnedObjectNames(manifest)) { + const fqn = computeFQN(manifest.namespace, shortName); + const owner = this.getObjectOwner(fqn); + const ownerId = owner?.packageId; + if (owner === undefined || ownerId === undefined || ownerId === selfId) continue; + // Not a co-owner: a cross-artifact claim, which the namespace gate above + // owns and refuses. ⛔ Not widened here — that would be a new refusal for + // configurations D1 never admitted, outside this card and outside D3. + if (!coOwners.has(ownerId)) continue; + if (isTenantAuthored(owner.definition)) continue; + throw new ArtifactObjectNameConflictError(fqn, ownerId, selfId ?? manifest.id, coOwners.get(ownerId)); + } + } + + /** + * [ADR-0150 D2, child N] Refuse an install whose package declares an item of + * a covered type under a name another installed package of its namespace + * already holds (`namespace-co-owner-names.ts`). + * + * The sitting holders are the installed package records — their manifests, + * as {@link installPackage} recorded them — of the incoming package's + * namespace: the record is the one place every door's install lands, so an + * uninstall takes its names with it and nothing else is kept. The same + * package's own record is never a second holder (a reload, a re-install or + * an upgrade). + * + * @throws {NamespaceNameConflictError} ADR-0112 envelope, naming every holder. + */ + static refuseNamespaceNameConflicts(this: Pick, manifest: ObjectStackManifest): void { + this.refuseNamespaceHolderConflicts(namespacePackageOf(manifest)); + } + + /** + * [ADR-0150 D6, child K] Refuse an install whose namespace equals an object + * name the environment holds or the package itself ships, whose object name + * equals an installed namespace, or whose object is short-named `custom` + * (`findNamespaceObjectNameClashes`, `@objectstack/spec/kernel`). + * + * The incoming side is the package's namespace and the objects it ships + * (`manifest.objects` and each nested `plugins[]` entry's, the objects + * `registerApp` registers under it). The installed side is every namespace + * the namespace gate has recorded and every object the registry holds, + * minus what this same package already holds (a reload, a re-install or an + * upgrade is never its own clash). A clash already present between two + * installed holders is not this install's to report. + * + * @throws {NamespaceObjectNameConflictError} ADR-0112 envelope, every clash named. + */ + static refuseNamespaceObjectNameClashes(this: Pick, manifest: ObjectStackManifest, selfId: string | undefined): void { + const incomingId = selfId ?? manifest.id; + const isSelf = (packageId: string | undefined): boolean => + packageId !== undefined && (packageId === incomingId || packageId === manifest.id); + const namespace = typeof manifest.namespace === 'string' && manifest.namespace !== '' ? manifest.namespace : undefined; + + const incomingObjects: ObjectNameHolder[] = []; + const seen = new Set(); + const shipped = (body: ObjectStackManifest): void => { + for (const shortName of declaredOwnedObjectNames(body)) { + const name = computeFQN(namespace, shortName); + if (seen.has(name)) continue; + seen.add(name); + incomingObjects.push({ name, ...(namespace ? { namespace } : {}), ...(incomingId ? { packageId: incomingId } : {}) }); + } + }; + shipped(manifest); + const plugins = (manifest as { plugins?: unknown }).plugins; + if (Array.isArray(plugins)) { + for (const plugin of plugins) if (plugin && typeof plugin === 'object') shipped(plugin as ObjectStackManifest); + } + + const installedNamespaces: NamespaceHolder[] = []; + for (const [ns, owners] of this.namespaceRegistry) { + for (const owner of owners) if (!isSelf(owner)) installedNamespaces.push({ namespace: ns, packageId: owner }); + } + const installedObjects: ObjectNameHolder[] = []; + for (const fqn of this.objectContributors.keys()) { + const ownerId = this.getObjectOwner(fqn)?.packageId; + if (isSelf(ownerId) || seen.has(fqn)) continue; + installedObjects.push({ name: fqn, ...(ownerId ? { packageId: ownerId } : {}) }); + } + + const clashes = findNamespaceObjectNameClashes( + { namespaces: namespace ? [{ namespace, ...(incomingId ? { packageId: incomingId } : {}) }] : [], objects: incomingObjects }, + { namespaces: installedNamespaces, objects: installedObjects }, + ); + if (clashes.length > 0) throw new NamespaceObjectNameConflictError(clashes, incomingId ?? '(unnamed package)'); + } + + /** The body of `SchemaRegistry#refuseResidualNamespaceNameConflicts`; the method's docblock there is its contract. */ + static refuseResidualNamespaceNameConflicts(this: Pick, residual: NamespacePackage | undefined): void { + this.refuseNamespaceHolderConflicts(residual); + } + + /** The body of `SchemaRegistry#recordResidualNamespaceHolder`; the method's docblock there is its contract. */ + static recordResidualNamespaceHolder(this: Pick, residual: NamespacePackage | undefined): void { + if (residual === undefined || !isNamespaceJudged(residual.namespace)) return; + this.residualNamespaceHolders.set(residual.packageId, residual); + } + + /** + * The one walk both refusals above share: `incoming` against every installed + * package record of its namespace and every recorded residual holder of it, + * read through the same predicate, thrown in one envelope. + */ + static refuseNamespaceHolderConflicts(this: Pick, incoming: NamespacePackage | undefined): void { + if (incoming === undefined || !isNamespaceJudged(incoming.namespace) || incoming.names.length === 0) return; + const sitting: NamespacePackage[] = []; + for (const record of this.metadata.get('package')?.values() ?? []) { + const recorded = (record as InstalledPackage | undefined)?.manifest; + if (recorded?.namespace !== incoming.namespace) continue; + const holder = namespacePackageOf(recorded); + if (holder !== undefined) sitting.push(holder); + } + for (const residual of this.residualNamespaceHolders.values()) { + if (residual.namespace === incoming.namespace) sitting.push(residual); + } + const conflicts = namespaceNameConflicts(incoming, sitting); + if (conflicts.length > 0) throw new NamespaceNameConflictError(conflicts); + } + + /** The body of `SchemaRegistry#uninstallPackage`; the method's docblock there is its contract. */ + static uninstallPackage(this: Pick, id: string, options?: { keepBootSeed?: boolean }): boolean { + const pkg = this.getPackage(id); + if (!pkg) { + console.warn(`[Registry] Package not found for uninstall: ${id}`); + return false; + } + + // [#7970] Unregister objects FIRST — this is the one step that can REFUSE + // (ADR-0029: the package owns an object another package `extend`s), so + // every mutation below is downstream of the refusal point and a refused + // uninstall removes nothing at all. The namespace release used to sit + // ABOVE this line, which meant reaching the guard that exists to keep a + // registry whole cost the namespace on the way in: the package stayed + // installed with its objects and its record intact, while its namespace no + // longer resolved, for the life of the process. Safe as the first step + // because the verb reads `objectContributors` only — it depends on nothing + // the steps below establish. + this.unregisterObjectsByPackage(id); + + // Unregister namespace + if (pkg.manifest.namespace) { + this.unregisterNamespace(pkg.manifest.namespace, id); + } + + // [#7221] …and everything else the package shipped. The object verb above + // reaches `objectContributors` only, so without this an uninstall dropped + // the package record while its `page`/`view`/`flow`/`app`/`api` entries + // stayed resolvable through `getItem`/`listItems` for the life of the + // process. Runs AFTER the object verb because that one can refuse + // (ADR-0029 extenders): a refused uninstall must remove nothing at all. + this.unregisterItemsByPackage(id); + // …and the security catalog names it held, so an uninstalled package's + // names are free for the next package to declare. + this.forgetSecurityCatalogClaims(id); + // …and the default record pages it declared, so the next package may + // declare a default for those objects. + this.forgetDefaultRecordPageClaims(id); + // …and its bare names of the covered types. + this.forgetBareNameClaims(id); + // [#33050] …and the record of what its body shipped: a later install of + // the id is a first registration, with nothing of its own to withdraw. + this.packageBodyItems.delete(id); + // [#31720] …and the namespace names a stack's residual holds under this + // id: the residual's items are registered under their owner id, so + // `unregisterItemsByPackage(id)` above has just withdrawn them (the + // owner id is one of the stack's own bodies in the `preserve` shape). + this.residualNamespaceHolders.delete(id); + // [#30581] …and the navigation entries it put into other packages' apps: + // with no row left, the fold's disabled check cannot withhold them. + this.setPackageNavContributions(id, []); + + // [#18877] The boot seed forgets the id together with the row. A package + // that no longer exists has no lifecycle state to preserve, so the next + // install of this id must be a FRESH install — and without this line the + // seed would outlive the row and re-disable it, which is exactly the + // 「听不见的持久回退」 this card removed from the install path. Runs with the + // record removal below rather than at the top: every mutation here is + // downstream of `unregisterObjectsByPackage`, the one step that can REFUSE + // (ADR-0029), so a refused uninstall still removes nothing at all. + if (options?.keepBootSeed !== true) this.initialDisabledPackageIds.delete(id); + + // [#28877] A disabled package's objects and flows withheld other packages' + // actions ({@link isActionWithheldByDisabledPackage}); with the record gone + // nothing is disabled, so every cached fold is re-derived. + if (this.isPackageDisabled(id)) this.invalidateAll(); + + // Remove package record + const collection = this.metadata.get('package'); + if (collection) { + collection.delete(id); + this.log(`[Registry] Uninstalled package: ${id}`); + // …and, last, whoever keeps its own state keyed by this package's id + // (the engine's manifest map) drops it, on every uninstall door at once. + this.announcePackageUninstalled(id); + return true; + } + return false; + } + + /** The body of `SchemaRegistry#getPackage`; the method's docblock there is its contract. */ + static getPackage(this: Pick, id: string): InstalledPackage | undefined { + return this.metadata.get('package')?.get(id) as InstalledPackage | undefined; + } + + /** The body of `SchemaRegistry#recordResidualOwnerDependencies`; the method's docblock there is its contract. */ + static recordResidualOwnerDependencies(this: Pick, ownerId: string, dependencies: readonly string[]): void { + this.residualOwnerDependencies.set(ownerId, Object.freeze([...dependencies])); + } + + /** The body of `SchemaRegistry#residualOwnerDependenciesOf`; the method's docblock there is its contract. */ + static residualOwnerDependenciesOf(this: Pick, id: string): readonly string[] | undefined { + return this.residualOwnerDependencies.get(id); + } + + /** The body of `SchemaRegistry#getAllPackages`; the method's docblock there is its contract. */ + static getAllPackages(this: Pick): InstalledPackage[] { + return this.listItems('package'); + } + + /** The body of `SchemaRegistry#enablePackage`; the method's docblock there is its contract. */ + static enablePackage(this: Pick, id: string): InstalledPackage | undefined { + const pkg = this.getPackage(id); + if (pkg) { + const wasDisabled = this.isPackageDisabled(id); + pkg.enabled = true; + pkg.status = 'installed'; + pkg.statusChangedAt = new Date().toISOString(); + pkg.updatedAt = new Date().toISOString(); + this.log(`[Registry] Enabled package: ${id}`); + // [#24400] A disabled package's bound actions are not attached + // ({@link boundCodeActions}), so every object's fold is re-derived. + this.invalidateAll(); + if (wasDisabled) this.announcePackageEnabled(id); + } + return pkg; + } + + /** The body of `SchemaRegistry#disablePackage`; the method's docblock there is its contract. */ + static disablePackage(this: Pick, id: string): InstalledPackage | undefined { + const pkg = this.getPackage(id); + if (pkg) { + pkg.enabled = false; + pkg.status = 'disabled'; + pkg.statusChangedAt = new Date().toISOString(); + pkg.updatedAt = new Date().toISOString(); + this.log(`[Registry] Disabled package: ${id}`); + // [#24400] A disabled package's bound actions are not attached + // ({@link boundCodeActions}), so every object's fold is re-derived. + this.invalidateAll(); + } + return pkg; + } + + /** The body of `SchemaRegistry#updatePackageManifest`; the method's docblock there is its contract. */ + static updatePackageManifest(this: Pick, id: string, patch: { name?: string; description?: string; version?: string; dependencies?: Record }): InstalledPackage | undefined { + const pkg = this.getPackage(id); + if (!pkg) return undefined; + const manifest = pkg.manifest as Record; + if (patch.name !== undefined) manifest.name = patch.name; + if (patch.description !== undefined) manifest.description = patch.description; + if (patch.version !== undefined) manifest.version = patch.version; + if (patch.dependencies !== undefined) manifest.dependencies = { ...patch.dependencies }; + pkg.updatedAt = new Date().toISOString(); + this.log(`[Registry] Updated package manifest: ${id}`); + return pkg; + } +} diff --git a/packages/objectql/src/registry-picklists.ts b/packages/objectql/src/registry-picklists.ts new file mode 100644 index 00000000000..26cda79a1bc --- /dev/null +++ b/packages/objectql/src/registry-picklists.ts @@ -0,0 +1,505 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33949 codemod] The "Plugins, kinds, shared picklists and reset" section of + * `SchemaRegistry`: the bodies of `getAllPlugins`, `getAllKinds`, + * `registerPicklistExtension`, `resolvePicklistOptions`, + * `withPendingManifests`, `findUnresolvedPicklistReferences`, + * `findOrphanPicklistExtensions`, `findOutsideClosurePicklistReferences`, + * `sealPicklistVocabulary`, `refusePicklistArrival`, + * `registeringPicklistArrival`, `admitPicklistArrival`, + * `refusePicklistItemArrival`, `picklistArrivalRefusal`, + * `findDisagreeingPicklistReferences`, `auditPicklistReferences`, + * `judgePicklistReference`, `picklistContributions`, + * `assertPicklistValuesDistinct`, `resolvePicklistFields`, + * `resolveFieldPicklistOptions`, `unregisterPicklistExtensionsByPackage`, + * `invalidate`, `invalidateAll`, `reset` moved out of `registry.ts` whole by + * `scripts/codemod-split-registry.mjs`. + * + * A MOVE, not a redesign. {@link PicklistsFacet} is a stateless facet of `SchemaRegistry` + * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` + * method of the same name exactly as it stood: not re-indented, not + * formatted, not edited. The method stays on the class as a one-call stub with + * its signature, so the published surface, an instance spy and a subclass + * override are what they were. A public or protected method keeps its + * docblock on the class, the contract; a private method's docblock moved here + * with its body. The state stays on `SchemaRegistry`. + * + * The bodies reach the class through {@link PicklistsHost}: exactly the + * `SchemaRegistry` members they read, private ones included. Every stub hands + * its body, by a `.call`, the same host: `picklistsHost` in + * `registry-facet-hosts.ts`, built once per registry by the class's static + * block. Methods go as closures that call them on the registry at call time, so + * a subclass override or an instance spy is still the one reached; fields and + * accessors go as GETTERS, read when the body reads them, as they were on the + * class, and a field a body assigns goes with a SETTER that assigns it on the + * registry. + * + * The module imports `registry.ts` for types only, so the two modules form no + * evaluation cycle. `index.ts` re-exports nothing from this module. + */ + +import { isTenantAuthored } from '@objectstack/metadata-core'; +import type { InstalledPackage, ObjectStackManifest } from '@objectstack/spec/kernel'; +import { artifactPackageId } from '@objectstack/core'; +import { collectManifestPicklistNames, collectPicklistReferences, describePicklistReferencesBrokenByInstall, describeUnresolvedPicklistReferences, duplicatePicklistValueError, findDuplicatePicklistValue, mergePicklistOptions, resolvePicklistFieldsOnto } from './picklist-resolution.js'; +import type { DisagreeingPicklistReference, PicklistArrivalAct, PicklistContribution, PicklistOptionLike, UnresolvedPicklistExtension, UnresolvedPicklistReference } from './picklist-resolution.js'; +import type { PackageScopedHolder, PackageScopedRefusal, PackageScopedRegistry } from './package-scoped-resolver.js'; +import { NO_PACKAGE_REFERRER, picklistReferrerOf, sameHolder } from './registry-top-level.js'; +import type { PicklistReferrer } from './registry-top-level.js'; +import type { SchemaRegistry } from './registry.js'; + +/** + * The `SchemaRegistry` members the bodies of {@link PicklistsFacet} read — every one of + * them, and no other. Each is typed AS the class member (an indexed access on + * the class), so a change to a member's signature on the class is a change + * here too. `readonly` members are the getters; the others are fields a body + * assigns. + */ +export interface PicklistsHost { + _objectRevision: SchemaRegistry['_objectRevision']; + readonly appNavContributions: SchemaRegistry['appNavContributions']; + auditPicklistReferences: SchemaRegistry['auditPicklistReferences']; + getAllPackages: SchemaRegistry['getAllPackages']; + getEnvironmentScopedItem: SchemaRegistry['getEnvironmentScopedItem']; + getPackage: SchemaRegistry['getPackage']; + getPackageScopedItem: SchemaRegistry['getPackageScopedItem']; + invalidateAll: SchemaRegistry['invalidateAll']; + judgePicklistArrival: SchemaRegistry['judgePicklistArrival']; + judgePicklistReference: SchemaRegistry['judgePicklistReference']; + listItems: SchemaRegistry['listItems']; + log: SchemaRegistry['log']; + readonly mergedObjectCache: SchemaRegistry['mergedObjectCache']; + readonly metadata: SchemaRegistry['metadata']; + readonly namespaceRegistry: SchemaRegistry['namespaceRegistry']; + notePicklistExtension: SchemaRegistry['notePicklistExtension']; + readonly objectContributors: SchemaRegistry['objectContributors']; + readonly objectKeysByShortName: SchemaRegistry['objectKeysByShortName']; + packageScopedHoldersOf: SchemaRegistry['packageScopedHoldersOf']; + picklistArrivalDepth: SchemaRegistry['picklistArrivalDepth']; + picklistArrivalRefusal: SchemaRegistry['picklistArrivalRefusal']; + picklistContributions: SchemaRegistry['picklistContributions']; + readonly picklistExtensionContributions: SchemaRegistry['picklistExtensionContributions']; + picklistReferenceVerdict: SchemaRegistry['picklistReferenceVerdict']; + picklistVocabularySealed: SchemaRegistry['picklistVocabularySealed']; + refusePicklistArrival: SchemaRegistry['refusePicklistArrival']; + refusePicklistItemArrival: SchemaRegistry['refusePicklistItemArrival']; + registeringPicklistArrival: SchemaRegistry['registeringPicklistArrival']; + readonly residualOwnerDependencies: SchemaRegistry['residualOwnerDependencies']; + residualOwnerDependenciesOf: SchemaRegistry['residualOwnerDependenciesOf']; + resolveFieldPicklistOptions: SchemaRegistry['resolveFieldPicklistOptions']; + resolvePicklistOptions: SchemaRegistry['resolvePicklistOptions']; +} + +/** The bodies of the "Plugins, kinds, shared picklists and reset" section of `SchemaRegistry`: a stateless facet, called by the class's stubs. */ +export class PicklistsFacet { + /** The body of `SchemaRegistry#getAllPlugins`; the method's docblock there is its contract. */ + static getAllPlugins(this: Pick): ObjectStackManifest[] { + return this.listItems('plugin'); + } + + /** The body of `SchemaRegistry#getAllKinds`; the method's docblock there is its contract. */ + static getAllKinds(this: Pick): { id: string; description?: string }[] { + return this.listItems('kind'); + } + + /** The body of `SchemaRegistry#registerPicklistExtension`; the method's docblock there is its contract. */ + static registerPicklistExtension(this: Pick, extension: { extend: string; options: readonly PicklistOptionLike[] }, packageId?: string): void { + const target = extension.extend; + // [ADR-0150 D3, #31700; epic seat ruling R2-A, 6812862622] After the + // seal, an extension registered on its own (outside an install judged as + // a whole) meets the one re-judge first: its target, judged caller-less, + // must be one list ({@link refusePicklistItemArrival}). + this.refusePicklistItemArrival( + packageId, + { picklistExtensions: [extension] }, + `a \`picklistExtensions\` entry of package '${packageId}' extending picklist '${target}'`, + ); + const packageKey = packageId ?? ''; + const own = this.picklistExtensionContributions.get(target)?.get(packageKey); + const merged = new Map(own?.options); + for (const option of extension.options ?? []) { + if (option && typeof option === 'object' && option.value !== undefined) merged.set(String(option.value), option); + } + const candidate: PicklistContribution = { packageId, kind: 'extension', options: [...merged.values()] }; + const others = this.picklistContributions(target).filter( + (c) => !(c.kind === 'extension' && (c.packageId ?? '') === packageKey), + ); + // The candidate's own list first: a value repeated INSIDE this one entry + // is refused too, and against the owner's values it then reads in merge + // order (the list first, this extension second). + const repeated = findDuplicatePicklistValue([{ ...candidate, options: extension.options ?? [] }]); + const duplicate = repeated ?? findDuplicatePicklistValue([...others, candidate]); + if (duplicate) throw duplicatePicklistValueError(target, duplicate); + + let byPackage = this.picklistExtensionContributions.get(target); + if (!byPackage) this.picklistExtensionContributions.set(target, byPackage = new Map()); + this.notePicklistExtension(target, packageKey); + byPackage.set(packageKey, { packageId, options: merged }); + this.invalidateAll(); + this.log(`[Registry] Registered picklist extension: ${target} (+${extension.options?.length ?? 0}) from ${packageId}`); + } + + /** The body of `SchemaRegistry#resolvePicklistOptions`; the method's docblock there is its contract. */ + static resolvePicklistOptions(this: Pick, name: string, callerPackageId?: string | null): PicklistOptionLike[] | undefined { + const contributions = this.picklistContributions(name, callerPackageId); + if (!contributions.some((c) => c.kind === 'picklist')) return undefined; + return mergePicklistOptions(contributions); + } + + /** + * The registry as R reads it once `manifests` (an artifact the install door + * is judging, not yet registered) are installed: each manifest answers + * `getPackage` for its own id — its `dependencies` and `namespace`, which is + * all the closure reads — ahead of an installed version of it, and each + * `picklist` a manifest declares is held by that manifest's package, next + * to the registry's holders. So a reference to a list a sibling in the + * artifact declares is judged by R's one rule — reached through the + * referrer's declared closure, else refused naming every holder — exactly as + * the served field will be once the artifact registers (epic seat review on + * #31731). ⛔ No closure is computed here; R computes it from this view. + * + * [#31700, ruling R2-A] `replaces: false` (a per-item seam, + * {@link judgePicklistArrival}): the manifest adds lists to a package and + * stands for no version of it, so `getPackage` keeps answering the + * installed record. A manifest with no package id registers its lists as + * the environment's (`registerItem` with no package), so the view holds + * them there. + */ + static withPendingManifests(this: Pick, manifests: readonly unknown[], options: { replaces?: boolean } = {}): PackageScopedRegistry { + const pending = new Map(); + const pendingLists = new Map>(); + const pendingEnvironmentLists = new Set(); + for (const manifest of manifests) { + const id = artifactPackageId(manifest); + if (id === undefined) { + for (const name of collectManifestPicklistNames(manifest)) pendingEnvironmentLists.add(name); + continue; + } + if (options.replaces !== false) pending.set(id, { manifest } as unknown as InstalledPackage); + for (const name of collectManifestPicklistNames(manifest)) { + let holders = pendingLists.get(name); + if (!holders) pendingLists.set(name, holders = new Set()); + holders.add(id); + } + } + const pendingHolders = (type: string, name: string): ReadonlySet => + (type === 'picklist' ? pendingLists.get(name) : undefined) ?? new Set(); + // A residual owner's declared dependencies (ADR-0130 D4) are read by the + // one closure walk through the registry it is handed — this view too. + const view: PackageScopedRegistry & Pick = { + residualOwnerDependenciesOf: (id) => this.residualOwnerDependenciesOf(id), + getPackage: (id) => pending.get(id) ?? this.getPackage(id), + getAllPackages: () => [ + ...this.getAllPackages().filter((row) => !pending.has(artifactPackageId(row.manifest) ?? '')), + ...pending.values(), + ], + // [#32183] The asked holder set (served, or every holder for a + // durable judgment such as {@link picklistReferenceVerdict}), with the + // artifact's own lists beside it. + packageScopedHoldersOf: (type, name, callerPackageId, options) => [ + ...new Set([...this.packageScopedHoldersOf(type, name, callerPackageId, options), ...pendingHolders(type, name)]), + ], + getPackageScopedItem: (type: string, name: string, packageId: string, member?: string): T | undefined => + pendingHolders(type, name).has(packageId) + ? ({ name, _packageId: packageId } as unknown as T) + : this.getPackageScopedItem(type, name, packageId, member), + getEnvironmentScopedItem: (type: string, name: string, member?: string): T | undefined => + type === 'picklist' && pendingEnvironmentLists.has(name) + ? ({ name } as unknown as T) + : this.getEnvironmentScopedItem(type, name, member), + }; + return view; + } + + /** The body of `SchemaRegistry#findUnresolvedPicklistReferences`; the method's docblock there is its contract. */ + static findUnresolvedPicklistReferences(this: Pick): UnresolvedPicklistReference[] { + return this.auditPicklistReferences().unresolved; + } + + /** The body of `SchemaRegistry#findOrphanPicklistExtensions`; the method's docblock there is its contract. */ + static findOrphanPicklistExtensions(this: Pick): UnresolvedPicklistExtension[] { + const orphans: UnresolvedPicklistExtension[] = []; + for (const [target, byPackage] of this.picklistExtensionContributions) { + const verdict = this.picklistReferenceVerdict(target, null); + if (verdict.status === 'resolved') continue; + for (const entry of byPackage.values()) { + orphans.push(verdict.status === 'refused' + ? { picklist: target, packageId: entry.packageId, refusal: verdict.message } + : { picklist: target, packageId: entry.packageId }); + } + } + return orphans; + } + + /** The body of `SchemaRegistry#findOutsideClosurePicklistReferences`; the method's docblock there is its contract. */ + static findOutsideClosurePicklistReferences(this: Pick): UnresolvedPicklistReference[] { + return this.auditPicklistReferences().outsideClosure; + } + + /** The body of `SchemaRegistry#sealPicklistVocabulary`; the method's docblock there is its contract. */ + static sealPicklistVocabulary(this: Pick): void { + this.picklistVocabularySealed = true; + } + + /** The body of `SchemaRegistry#refusePicklistArrival`; the method's docblock there is its contract. */ + static refusePicklistArrival(this: Pick, manifests: readonly unknown[], options: { act?: PicklistArrivalAct } = {}): void { + if (!this.picklistVocabularySealed || this.picklistArrivalDepth > 0) return; + const refusal = this.picklistArrivalRefusal(manifests, { replaces: true, ownRefusals: 'all', act: options.act ?? 'install' }); + if (refusal) throw refusal; + } + + /** The body of `SchemaRegistry#registeringPicklistArrival`; the method's docblock there is its contract. */ + static registeringPicklistArrival(this: Pick, register: () => T): T { + this.picklistArrivalDepth += 1; + try { + return register(); + } finally { + this.picklistArrivalDepth -= 1; + } + } + + /** The body of `SchemaRegistry#admitPicklistArrival`; the method's docblock there is its contract. */ + static admitPicklistArrival(this: Pick, manifests: readonly unknown[], register: () => T, options: { act?: PicklistArrivalAct } = {}): T { + this.refusePicklistArrival(manifests, options); + return this.registeringPicklistArrival(register); + } + + /** + * [ADR-0150 D3, #31700; epic seat ruling R2-A, 6812862622] The per-item + * seams' half: one picklist, extension or object body registered under + * `packageId` on its own, after the seal and outside an arrival judged as a + * whole. Judged as an ADDITION to what the package holds + * (`replaces: false`): its installed items stay, and its identity and + * dependencies are the installed record's. Its own reference or extension + * is refused only when the resolver refuses it as ambiguous — the class two + * namespaces' holders open; a name nothing holds, or holds only outside the + * package's closure, keeps the seam's answer (served no options, named by + * the boot audit). What it does to everyone else's references is judged in + * full. ⛔ Never a registration with no package: the environment catalog + * (`sys_metadata`), whose stored row is not refused here. A tenant-authored + * list under a package is judged as any list of it (seat ruling R3-A); a + * tenant-authored object layer is not asked, as it writes no package's + * reference ({@link registerObject}). + */ + static refusePicklistItemArrival(this: Pick, packageId: string | undefined, part: Record, subject: string): void { + if (!this.picklistVocabularySealed || this.picklistArrivalDepth > 0 || !packageId) return; + const refusal = this.picklistArrivalRefusal([{ id: packageId, ...part }], { + replaces: false, + ownRefusals: 'ambiguous', + act: 'register', + subject, + }); + if (refusal) throw refusal; + } + + /** The refusal {@link judgePicklistArrival}'s verdict amounts to, or `undefined`. */ + static picklistArrivalRefusal(this: Pick, manifests: readonly unknown[], options: { replaces: boolean; ownRefusals: 'all' | 'ambiguous'; act: PicklistArrivalAct; subject?: string }): Error | undefined { + const verdict = this.judgePicklistArrival(manifests, options); + return describeUnresolvedPicklistReferences(verdict.unresolved, verdict.orphans) + ?? describePicklistReferencesBrokenByInstall( + manifests.map((m) => artifactPackageId(m)).filter((id): id is string => id !== undefined), + verdict.references, + verdict.extensions, + verdict.disagreeing, + { act: options.act, subject: options.subject }, + ); + } + + /** The body of `SchemaRegistry#findDisagreeingPicklistReferences`; the method's docblock there is its contract. */ + static findDisagreeingPicklistReferences(this: Pick): DisagreeingPicklistReference[] { + const disagreeing: DisagreeingPicklistReference[] = []; + for (const [fqn, contributors] of this.objectContributors) { + const referrerOf = picklistReferrerOf(contributors); + const seen = new Set(); + for (const contributor of contributors) { + if (contributor.ownership === 'overlay' || isTenantAuthored(contributor.definition)) continue; + for (const ref of collectPicklistReferences(fqn, contributor.definition, contributor.packageId)) { + const key = `${ref.field}\u0000${ref.picklist}`; + if (seen.has(key)) continue; + seen.add(key); + const referrers = referrerOf(ref.field, ref.picklist); + if (referrers.length < 2) continue; + const reached: { packageId: string | undefined; holder: PackageScopedHolder }[] = []; + for (const referrer of referrers) { + const verdict = this.picklistReferenceVerdict(ref.picklist, referrer); + if (verdict.status !== 'resolved') break; + reached.push({ packageId: referrer ?? undefined, holder: verdict.holder }); + } + if (reached.length < referrers.length) continue; + if (reached.every((r) => sameHolder(r.holder, reached[0].holder))) continue; + disagreeing.push({ object: fqn, field: ref.field, picklist: ref.picklist, referrers: reached }); + } + } + } + return disagreeing; + } + + /** The field half of the audit, split by the resolver's verdict (above). */ + static auditPicklistReferences(this: Pick): { + unresolved: UnresolvedPicklistReference[]; + outsideClosure: UnresolvedPicklistReference[]; + } { + const unresolved: UnresolvedPicklistReference[] = []; + const outsideClosure: UnresolvedPicklistReference[] = []; + for (const [fqn, contributors] of this.objectContributors) { + for (const contributor of contributors) { + if (contributor.ownership === 'overlay' || isTenantAuthored(contributor.definition)) continue; + for (const ref of collectPicklistReferences(fqn, contributor.definition, contributor.packageId)) { + const judged = this.judgePicklistReference(ref.picklist, ref.packageId); + if (judged === 'resolved') continue; + const entry = judged.refusal ? { ...ref, refusal: judged.refusal.message } : ref; + (judged.outsideClosure ? outsideClosure : unresolved).push(entry); + } + } + } + return { unresolved, outsideClosure }; + } + + /** + * One reference, judged: `'resolved'` when the name reaches a list from + * `callerPackageId`, else what it is — the refusal (when the resolver + * refused) and whether it refused because every holder is outside the + * caller's declared closure. + */ + static judgePicklistReference(this: Pick, name: string, callerPackageId: string | undefined): 'resolved' | { refusal?: PackageScopedRefusal; outsideClosure: boolean } { + const verdict = this.picklistReferenceVerdict(name, callerPackageId); + if (verdict.status === 'resolved') return 'resolved'; + if (verdict.status !== 'refused') return { outsideClosure: false }; + return { refusal: verdict, outsideClosure: verdict.reason === 'outside-closure' }; + } + + /** + * The list `name` reaches from `callerPackageId` (its options, when that is + * one), followed by every extension of the SAME list. + * + * [ADR-0150 D3, child E6; epic seat ruling X on the E6 card, #31678] A + * `picklistExtensions` entry's `extend` is a CONTRIBUTION to another + * package's item, not a reference its package reads: it resolves + * caller-less through R — the environment's own list, else the unique + * package holder, else a refusal naming every holder — the treatment D3 + * gives an environment overlay. So an extension adds to the list its target + * names environment-wide, and to a field's list only when that is the same + * list. Loosens nothing (no first match is picked) and guesses nothing. + */ + static picklistContributions(this: Pick, name: string, callerPackageId?: string | null): PicklistContribution[] { + const contributions: PicklistContribution[] = []; + const resolution = this.picklistReferenceVerdict(name, callerPackageId); + const base = resolution.status === 'resolved' ? resolution : undefined; + if (base) { + const item = base.item; + contributions.push({ + packageId: item?._packageId, + kind: 'picklist', + options: Array.isArray(item?.options) ? item.options : [], + }); + } + const extended = base ? this.picklistReferenceVerdict(name, null) : undefined; + if (base && !(extended?.status === 'resolved' && sameHolder(extended.holder, base.holder))) return contributions; + for (const entry of this.picklistExtensionContributions.get(name)?.values() ?? []) { + contributions.push({ packageId: entry.packageId, kind: 'extension', options: [...entry.options.values()] }); + } + return contributions; + } + + /** + * The list half of the additive rule: a picklist registered AFTER an + * extension already added one of its values is refused the same way, so the + * verdict does not depend on which package registered first. + */ + static assertPicklistValuesDistinct(this: Pick, name: string, picklist: { options?: PicklistOptionLike[] }, packageId: string | undefined): void { + // The list is not registered yet, so no resolution reaches it: judged + // against every extension held for the name, as the additive rule reads. + const extensions: PicklistContribution[] = [...(this.picklistExtensionContributions.get(name)?.values() ?? [])] + .map((entry) => ({ packageId: entry.packageId, kind: 'extension' as const, options: [...entry.options.values()] })); + const duplicate = findDuplicatePicklistValue([ + { packageId, kind: 'picklist', options: Array.isArray(picklist?.options) ? picklist.options : [] }, + ...extensions, + ]); + if (duplicate) throw duplicatePicklistValueError(name, duplicate); + } + + /** + * Resolve every picklist-bound field of a body (by reference when there is + * none). [ADR-0150 D3, child E6] Each field's list is resolved from the + * package that WROTE that field's reference (`referrerOf`): the packaged + * layer whose own declaration carries it, else no package — a reference + * only an environment-authored body (a tenant overlay, a stored row) writes + * is the environment's reference, never the package's it customizes (D3). + */ + static resolvePicklistFields(this: Pick, body: T, referrerOf: PicklistReferrer = NO_PACKAGE_REFERRER): T { + return resolvePicklistFieldsOnto(body, (picklist, field) => + this.resolveFieldPicklistOptions(picklist, referrerOf(field, picklist))); + } + + /** + * [ADR-0150 D3, child E6; epic seat review on #31731] One field's list, when + * several packaged layers write its reference: each referrer's resolution + * is R's, and the field is served only when every one of them resolves to + * the SAME holder. Any other answer — one of them refused or reaching + * nothing, or two reaching different holders — serves no options, so the + * record validator refuses the field's values; the boot audit names each + * referrer whose own resolution refuses, with R's sentence + * ({@link findUnresolvedPicklistReferences}). ⛔ No referrer is preferred by + * fold order. + */ + static resolveFieldPicklistOptions(this: Pick, picklist: string, referrers: readonly (string | null)[]): PicklistOptionLike[] | undefined { + const [first, ...others] = referrers; + if (others.length > 0) { + const verdicts = referrers.map((referrer) => this.picklistReferenceVerdict(picklist, referrer)); + const [head] = verdicts; + if (head.status !== 'resolved') return undefined; + if (!verdicts.every((v) => v.status === 'resolved' && sameHolder(v.holder, head.holder))) return undefined; + } + return this.resolvePicklistOptions(picklist, first ?? null); + } + + /** Drop a package's `picklistExtensions`; `true` when it had any. */ + static unregisterPicklistExtensionsByPackage(this: Pick, packageId: string): boolean { + let dropped = false; + for (const [target, byPackage] of this.picklistExtensionContributions) { + if (byPackage.delete(packageId)) dropped = true; + if (byPackage.size === 0) this.picklistExtensionContributions.delete(target); + } + return dropped; + } + + /** The body of `SchemaRegistry#invalidate`; the method's docblock there is its contract. */ + static invalidate(this: Pick, fqnOrName: string): void { + this._objectRevision += 1; + if (this.mergedObjectCache.has(fqnOrName)) { + this.mergedObjectCache.delete(fqnOrName); + return; + } + // Short-name path: drop any cached merge whose FQN ends with `__` or equals ``. + const suffix = `__${fqnOrName}`; + for (const fqn of Array.from(this.mergedObjectCache.keys())) { + if (fqn === fqnOrName || fqn.endsWith(suffix)) { + this.mergedObjectCache.delete(fqn); + } + } + } + + /** The body of `SchemaRegistry#invalidateAll`; the method's docblock there is its contract. */ + static invalidateAll(this: Pick): void { + this.mergedObjectCache.clear(); + this._objectRevision += 1; + } + + /** The body of `SchemaRegistry#reset`; the method's docblock there is its contract. */ + static reset(this: Pick): void { + this.objectContributors.clear(); + // [#10945] The short-name index is derived from `objectContributors`; a + // reset that cleared only one of them would resolve names to keys that no + // longer exist. + this.objectKeysByShortName.clear(); + this.mergedObjectCache.clear(); + this.namespaceRegistry.clear(); + this.metadata.clear(); + this.appNavContributions.clear(); + this.picklistExtensionContributions.clear(); + this.residualOwnerDependencies.clear(); + this._objectRevision += 1; + this.log('[Registry] Reset complete'); + } +} diff --git a/packages/objectql/src/registry-platform-global.ts b/packages/objectql/src/registry-platform-global.ts new file mode 100644 index 00000000000..653b6a4fa83 --- /dev/null +++ b/packages/objectql/src/registry-platform-global.ts @@ -0,0 +1,753 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33949 codemod] The "Platform-global declaration and uninstall checks" + * section of `SchemaRegistry`: the bodies of + * `setDeploymentPlatformGlobalObjects`, `applyDeploymentTenancy`, + * `stripDeploymentTenancyFrom`, `deploymentWithholdsTenant`, + * `stripProvisionedTenantIndexFrom`, `stripProvisionedPrimaryFrom`, + * `subtractExtenderContributions`, `resolveOwnerLayer`, `getObject`, + * `resolveObjectKey`, `getAllObjects`, `getObjectContributors`, + * `getObjectExtensionsOf`, `getObjectOwner`, `getPackagedObjectOwner`, + * `getPackagedObjectBase`, `extendContributionRefusal`, + * `registerObjectExtension`, `extensionTargetKey`, `withdrawObjectExtension`, + * `removeObjectOverlay`, `assertSingleOwnerPerObject`, + * `unregisterObjectsByPackage`, `unregisterObject` moved out of `registry.ts` + * whole by `scripts/codemod-split-registry.mjs`. + * + * A MOVE, not a redesign. {@link PlatformGlobalFacet} is a stateless facet of `SchemaRegistry` + * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` + * method of the same name exactly as it stood: not re-indented, not + * formatted, not edited. The method stays on the class as a one-call stub with + * its signature, so the published surface, an instance spy and a subclass + * override are what they were. A public or protected method keeps its + * docblock on the class, the contract; a private method's docblock moved here + * with its body. The state stays on `SchemaRegistry`. + * + * The bodies reach the class through {@link PlatformGlobalHost}: exactly the + * `SchemaRegistry` members they read, private ones included. Every stub hands + * its body, by a `.call`, the same host: `platformGlobalHost` in + * `registry-facet-hosts.ts`, built once per registry by the class's static + * block. Methods go as closures that call them on the registry at call time, so + * a subclass override or an instance spy is still the one reached; fields and + * accessors go as GETTERS, read when the body reads them, as they were on the + * class, and a field a body assigns goes with a SETTER that assigns it on the + * registry. + * + * Values that stayed in `registry.ts` are imported from it (lever (a) of + * ruling 6591753301); every use is inside a function body, so the import + * cycle is never read at evaluation time. `index.ts` re-exports nothing from + * this module. + */ + +import { isInjectedColumnDefinition, provisionPrimary, resolveInjectedSystemColumns } from '@objectstack/spec/data'; +import type { ServiceObject } from '@objectstack/spec/data'; +import { TENANT_SCOPE_FIELD_DEF, isCodeArtifactBody } from '@objectstack/metadata-core'; +import { + DEFAULT_EXTENDER_PRIORITY, TENANT_SCOPE_INDEX, provisionTenantScopeIndex, stableStringify, withoutField, +} from './registry-top-level.js'; +import type { ObjectContributor, ObjectExtensionFoldOptions } from './registry-top-level.js'; +import { extendContributionRefusal } from './registry.js'; +import type { SchemaRegistry } from './registry.js'; + +/** + * The `SchemaRegistry` members the bodies of {@link PlatformGlobalFacet} read — every one of + * them, and no other. Each is typed AS the class member (an indexed access on + * the class), so a change to a member's signature on the class is a change + * here too. `readonly` members are the getters; the others are fields a body + * assigns. + */ +export interface PlatformGlobalHost { + _objectRevision: SchemaRegistry['_objectRevision']; + applyDeploymentTenancy: SchemaRegistry['applyDeploymentTenancy']; + assertPackageUninstallable: SchemaRegistry['assertPackageUninstallable']; + closeObjectEntry: SchemaRegistry['closeObjectEntry']; + deploymentPlatformGlobalObjects: SchemaRegistry['deploymentPlatformGlobalObjects']; + deploymentWithholdsTenant: SchemaRegistry['deploymentWithholdsTenant']; + readonly extensionSpellingWarned: SchemaRegistry['extensionSpellingWarned']; + foldExtenders: SchemaRegistry['foldExtenders']; + getObjectOwner: SchemaRegistry['getObjectOwner']; + getPackagedObjectOwner: SchemaRegistry['getPackagedObjectOwner']; + invalidateAll: SchemaRegistry['invalidateAll']; + log: SchemaRegistry['log']; + readonly mergedObjectCache: SchemaRegistry['mergedObjectCache']; + readonly multiTenant: SchemaRegistry['multiTenant']; + readonly objectContributors: SchemaRegistry['objectContributors']; + readonly objectKeysByShortName: SchemaRegistry['objectKeysByShortName']; + packagedRules: SchemaRegistry['packagedRules']; + registerObject: SchemaRegistry['registerObject']; + resolveObject: SchemaRegistry['resolveObject']; + resolveObjectKey: SchemaRegistry['resolveObjectKey']; + resolveObjectLeavingExtensionsOut: SchemaRegistry['resolveObjectLeavingExtensionsOut']; + stripProvisionedTenantIndexFrom: SchemaRegistry['stripProvisionedTenantIndexFrom']; +} + +/** The bodies of the "Platform-global declaration and uninstall checks" section of `SchemaRegistry`: a stateless facet, called by the class's stubs. */ +export class PlatformGlobalFacet { + /** The body of `SchemaRegistry#setDeploymentPlatformGlobalObjects`; the method's docblock there is its contract. */ + static setDeploymentPlatformGlobalObjects(this: Pick, names: Iterable): { + readonly replanned: readonly string[]; + readonly keptAuthoredColumn: readonly string[]; + } { + this.deploymentPlatformGlobalObjects = new Set(names); + const replanned = new Set(); + const keptAuthoredColumn = new Set(); + if (this.deploymentPlatformGlobalObjects.size === 0) { + return { replanned: [], keptAuthoredColumn: [] }; + } + for (const contributors of this.objectContributors.values()) { + for (const contributor of contributors) { + const before = contributor.definition; + const name = (before as { name?: unknown })?.name; + if (typeof name !== 'string') continue; + const orgField = (before.fields as Record | undefined)?.organization_id; + const authoredColumn = orgField !== undefined && !isInjectedColumnDefinition(orgField, TENANT_SCOPE_FIELD_DEF); + // Asked of the body WITHOUT its column: the plan decides whether the + // deployment withholds one, and the column's presence is not an input. + if (!this.deploymentWithholdsTenant(withoutField(before, 'organization_id'))) continue; + if (authoredColumn) { + keptAuthoredColumn.add(name); + continue; + } + // [#24314] An `extend` layer has nothing to re-plan: `registerObject` + // plans base layers only, so it carries no platform column or index. + if (contributor.ownership === 'extend') continue; + // The tenant index comes off FIRST, while the body is still in the + // state it was stamped in (the strip re-stamps to prove the entry is + // the platform's own). + const unindexed = this.stripProvisionedTenantIndexFrom(before); + const after = this.applyDeploymentTenancy(unindexed); + if (after !== before) { + contributor.definition = after; + replanned.add(name); + } + } + } + this.invalidateAll(); + return { replanned: [...replanned].sort(), keptAuthoredColumn: [...keptAuthoredColumn].sort() }; + } + + /** + * [ADR-0131 D7] Record the deployment's platform-global declaration on one + * base-layer body: a declared object carries no `organization_id` here and + * declares `systemFields.tenant: false`. + * + * Why the record and not only the missing column: `systemFields.tenant: + * false` is the vocabulary every reader of a registered object already + * answers "no organization column" from — the security layer's + * `tenancyDisabled` (so Layer 0 composes nothing and a wildcard + * `organization_id` policy is not applicable, with no second reading of the + * declaration), the tenant-index predicate above, the lifecycle's + * provenance (`absent`), and the `/meta` read exits' own injection pass, + * which re-plans a served body WITHOUT the deployment and would otherwise + * add the column back. The object is then exactly what a deployment-level + * object that declares the key itself is (ADR-0131 D7: governed by object + * permission, not by the wall), on this deployment only. + * + * Withheld — `schema` returned by reference — when the object is not + * declared, when its own declaration already plans no tenant column, and + * when it DECLARES its own `organization_id` (not the platform's + * definition, byte for byte): an authored column is the author's, and the + * object stays walled on it. The decision reads only the body, so the read + * exits ({@link materializeServedObjectOnto}) reach the same answer. + */ + static applyDeploymentTenancy(this: Pick, schema: ServiceObject): ServiceObject { + if (!this.deploymentWithholdsTenant(schema)) return schema; + const fields = schema.fields as Record | undefined; + const orgField = fields?.organization_id; + if (orgField !== undefined && !isInjectedColumnDefinition(orgField, TENANT_SCOPE_FIELD_DEF)) return schema; + const sf = (schema as { systemFields?: unknown }).systemFields; + const out = withoutField(schema, 'organization_id') as ServiceObject & { systemFields?: unknown }; + return { + ...out, + systemFields: { + ...(sf && typeof sf === 'object' && !Array.isArray(sf) ? (sf as Record) : {}), + tenant: false, + }, + } as ServiceObject; + } + + /** + * [ADR-0131 D7] The write-side inverse of {@link applyDeploymentTenancy}: + * take the recorded `systemFields.tenant: false` back off a declared + * object's body on its way IN, so a Studio GET → edit → PUT on the declaring + * deployment persists the body the author wrote (#4326), not a deployment + * fact that would outlive the declaration in `sys_metadata`. + * + * Exact in the way its siblings are: only a declared object, only the + * `tenant: false` member, and the `systemFields` key itself only when that + * member was all it held. The trade is theirs too — an author who wrote + * `systemFields: { tenant: false }` on a declared object has it dropped on + * the first save that carries it, and on this deployment the answer is + * re-derived at every load, so nothing it provisions changes. + * + * Returns `base` by reference when nothing was owed. + */ + static stripDeploymentTenancyFrom(this: Pick, base: T): T { + if (base === null || typeof base !== 'object') return base; + const sf = (base as { systemFields?: unknown }).systemFields; + if (!sf || typeof sf !== 'object' || Array.isArray(sf)) return base; + if ((sf as { tenant?: unknown }).tenant !== false) return base; + const { tenant: _tenant, ...restSystemFields } = sf as Record; + const out = { ...(base as Record) }; + if (Object.keys(restSystemFields).length === 0) delete out.systemFields; + else out.systemFields = restSystemFields; + // Redundant with the derivation only where the deployment's plan withholds + // the column from the body without the record — i.e. on a declared object. + return this.deploymentWithholdsTenant(out as unknown as ServiceObject) ? (out as unknown as T) : base; + } + + /** + * [ADR-0131 D7] Does the deployment's declaration — and nothing else — plan + * this body with no tenant column? The ONE reading of the declaration in + * this registry: the spec's plan answers it, with and without the + * deployment input, so the registry never re-derives which objects are + * declared. + */ + static deploymentWithholdsTenant(this: Pick, schema: ServiceObject): boolean { + if (this.deploymentPlatformGlobalObjects.size === 0) return false; + return ( + resolveInjectedSystemColumns(schema).tenant && + !resolveInjectedSystemColumns(schema, { platformGlobalObjects: this.deploymentPlatformGlobalObjects }).tenant + ); + } + + /** + * [#8375] Remove the tenant-scope index entry {@link materializeBaseLayer} + * appended — and only that one. + * + * ## Why this strip is harder than the two beside it + * + * `indexes` is a CONCATENATING key under `mergeObjectDefinitions`, not a + * last-writer-wins scalar like `nameField` or a keyed record like `fields`. + * The two existing strips can ask "is this value the platform's?" of a single + * slot; here the platform's entry sits in a list beside the author's, entries + * may legitimately repeat, and a strip that removes "any entry that looks like + * the platform's" silently deletes an author's own declaration, while a strip + * that removes nothing bakes a phantom index into `sys_metadata.metadata`, its + * checksum and every history diff on the first Studio GET → edit → PUT + * (#4326). + * + * ## The bound, and why it is exact in BOTH directions + * + * Not "does an entry match" but "would the seam have produced EXACTLY this + * list": remove the LAST entry identical to the platform's own (the seam + * APPENDS, so its entry is the last matching one), re-stamp the remainder + * through `provisionTenantScopeIndex` — the stamping function itself, so the + * strip and the stamp cannot drift — and keep the removal only when the + * re-stamped list is byte-identical to the list that arrived. Anything else + * returns `base` untouched. + * + * That single comparison decides every case correctly, and each of these was + * measured: + * + * - the platform's appended entry on a body with no `indexes` of its own → + * removed, and the KEY is deleted rather than left as `indexes: []`, so the + * round trip is byte-identical and not merely equivalent; + * - an author's own `{ fields: ['organization_id'] }` on a SINGLE-TENANT + * deployment → the re-stamp adds nothing, the lists differ, kept; + * - an author's own tenant index declared BEFORE their other indexes → the + * re-stamp appends at the end, the ORDER differs, kept; + * - a named entry (`{ name: 'my_tenant_idx', fields: [...] }`) or a composite + * → not identical to the platform's entry, never a candidate, kept. + * + * ⚠️ The one case it cannot separate, stated plainly because it is the same + * trade {@link stripProvisionedPrimaryFrom} documents: an author who declares + * `{ fields: ['organization_id'] }` as their LAST index on a MULTI-TENANT + * deployment has written exactly what the seam appends, so it is dropped on + * the first save that carries it. What bounds the harm is identical: the entry + * is RE-DERIVED at every load — `registerObject` runs this seam over every + * base layer — so the resolved answer, and the index the driver materializes, + * is the same with or without the stored entry, now and at every future boot. + * + * Returns `base` by reference when nothing was owed. + */ + static stripProvisionedTenantIndexFrom(this: Pick, base: T): T { + if (base === null || typeof base !== 'object') return base; + const present = (base as { indexes?: unknown }).indexes; + if (!Array.isArray(present) || present.length === 0) return base; + + const platformEntry = stableStringify(TENANT_SCOPE_INDEX); + let at = -1; + for (let i = present.length - 1; i >= 0; i--) { + if (stableStringify(present[i]) === platformEntry) { at = i; break; } + } + if (at === -1) return base; + + const kept = present.filter((_entry, i) => i !== at); + const without = { ...(base as Record) }; + // An emptied list is the ABSENCE of the key, not an empty array: the seam + // added `indexes` to a body that had none, so the inverse must take the key + // away again or the round trip stores a shape the author never wrote. + if (kept.length === 0) delete without.indexes; + else without.indexes = kept; + + const restamped = provisionTenantScopeIndex(without as never, { + multiTenant: this.multiTenant, + }) as { indexes?: unknown }; + if (stableStringify(restamped.indexes) !== stableStringify(present)) return base; + + return without as unknown as T; + } + + /** + * [#8268] Remove an ADR-0079 title designation that is REDUNDANT WITH THE + * DERIVATION — the pointer {@link materializeServedObjectOnto} adds, and only + * that one. + * + * Same EXACTNESS discipline as {@link stripProvisionedSearchCompanionFrom}, + * and recomputed from `provisionPrimary` itself rather than transcribed, so + * the strip and the stamp cannot drift: the pointer comes off only when it is + * identical to what the seam would designate for a body carrying no pointer + * at all. A pointer naming any OTHER field is the author's own deliberate + * choice — it overrides the derivation, which is the entire reason to write + * one — and is kept untouched. + * + * ⚠️ The narrow case this trades away, stated plainly because it is real and + * it is NOT the same trade `stripProvisionedSearchCompanionFrom` makes. That + * one removes a value byte-identical to a platform-canonical FIELD + * DEFINITION, where coincidence with an author's own writing is implausible. + * This one compares a single string, so an author who explicitly declared the + * field the derivation would have picked anyway loses that declaration on a + * round trip. What bounds the harm is that the two are indistinguishable by + * construction — the read serves one body, and "the author wrote + * `nameField: 'name'`" and "the read derived `nameField: 'name'`" are the + * same bytes — and that the stripped pointer is REDERIVED on every load: + * `registerObject` runs `provisionPrimary` over every base layer, so the + * resolved answer is identical with or without the stored key, now and at + * each future boot. The designation is only ever lost if a later edit changes + * which field the derivation picks, and that edit re-serves and re-stores the + * new designation through this same pair. + */ + static stripProvisionedPrimaryFrom(base: T): T { + if (base === null || typeof base !== 'object') return base; + const present = (base as { nameField?: unknown }).nameField; + if (typeof present !== 'string' || present === '') return base; + + // Re-stamp a copy WITHOUT the pointer and compare: the canonical answer + // comes from the provisioning function itself, and an object the seam would + // designate nothing for yields no stamp and therefore no removal. + const withoutPointer = { ...(base as Record) }; + delete withoutPointer.nameField; + const restamped = provisionPrimary(withoutPointer as never, { synthesize: false }) as unknown as + { nameField?: unknown }; + if (restamped.nameField !== present) return base; + + return withoutPointer as unknown as T; + } + + /** + * [#8027] Remove from `base` the `indexes` entries the `extend` contributors + * are about to contribute — the half of {@link foldObjectExtendersOnto} that + * makes the fold idempotent. + * + * Only the CONCATENATING key is considered; `fields` and the scalar props + * already re-apply cleanly. Matching is by deep value equality on the + * serialised entry, so it fires only on an entry byte-identical to the one + * the extender declares. + * + * [ADR-0029 D9.2a, 2026-10-30 note] `validations` are no longer subtracted + * here: the fold merges them BY NAME ({@link mergeValidationsByName}), which + * removes every base copy of an extender's rule name — reworded or not — + * before appending the extender's rule, so a folded base re-folds to itself + * with nothing taken off first. Subtracting first would also take the + * tenant's `message` / `active` off a reworded copy before the fold could + * read them. + * + * Returns `base` BY REFERENCE when nothing matched, which is the ordinary + * case (an unfolded base), so the pre-#8027 body is preserved exactly. + */ + static subtractExtenderContributions(contributors: ObjectContributor[], base: ServiceObject): ServiceObject { + const key = (entry: unknown): string => stableStringify(entry); + let out: ServiceObject | undefined; + for (const listKey of ['indexes'] as const) { + const baseList = (base as Record)[listKey]; + if (!Array.isArray(baseList) || baseList.length === 0) continue; + const contributed = new Set(); + for (const contrib of contributors) { + if (contrib.ownership !== 'extend') continue; + const list = (contrib.definition as unknown as Record)[listKey]; + if (Array.isArray(list)) for (const entry of list) contributed.add(key(entry)); + } + if (contributed.size === 0) continue; + const kept = baseList.filter((entry) => !contributed.has(key(entry))); + if (kept.length === baseList.length) continue; + out ??= { ...base }; + (out as unknown as Record)[listKey] = kept; + } + return out ?? base; + } + + /** + * [ADR-0029 D9.6] The CODE-LAYER resolution of an object: the OWNER's + * declaration with its extenders folded on, deliberately ignoring any tenant + * `overlay` layer. + * + * This is what artifact identity has to be read from, and it is NOT implied + * by D9.2 — D9.2 leaves the merged body identical on purpose, `_provenance: + * 'org'` included, so asking the MERGED body "does a code package ship + * this?" answers about whatever last overwrote the base rather than about + * the code layer. That is precisely how `isArtifactBacked` came to answer + * `false` for a name a package still ships (#6853 P3), disarming both gates + * that read it. + * + * With no overlay registered this is byte-for-byte `resolveObject`'s answer, + * which is why the honest predicate costs nothing on the common shape. + * + * Uncached on purpose: a second cache would have to stay correct at every + * one of `mergedObjectCache`'s invalidation points, and the fold is a + * shallow spread over a contributor list that is almost always length 1. + */ + static resolveOwnerLayer(this: Pick, fqn: string): ServiceObject | undefined { + const contributors = this.objectContributors.get(fqn); + if (!contributors || contributors.length === 0) return undefined; + const ownerContrib = contributors.find(c => c.ownership === 'own'); + if (!ownerContrib) return undefined; + return this.foldExtenders(contributors, ownerContrib, 'code'); + } + + /** The body of `SchemaRegistry#getObject`; the method's docblock there is its contract. */ + static getObject(this: Pick, name: string): ServiceObject | undefined { + const fqn = this.resolveObjectKey(name); + return fqn === undefined ? undefined : this.resolveObject(fqn); + } + + /** + * [#6808] The name→FQN half of {@link getObject}, extracted so the READ and + * the name-addressed REMOVAL ({@link unregisterObject}) cannot disagree + * about which contributor entry a bare name addresses. + * + * That disagreement is not hypothetical — it is the shape of the bug this + * was extracted for: `deleteMetaItem`'s registry heal reached one of the two + * places an `object` lives, and the surface data CRUD dispatches on + * (`getObject`) kept serving a deleted object. A remover that resolved names + * its own way would re-open the same seam one layer down: it could remove an + * entry `getObject` never served, leaving the served one behind. + * + * Returns `undefined` when nothing is registered under the name, so + * `getObject` keeps its exact previous behaviour: `resolveObject` on an + * unknown FQN also answered `undefined`. + * + * [#10945] The short-name half is answered from {@link objectKeysByShortName} + * rather than by scanning every contributor key. That keeps the extraction + * above intact where it matters most — BOTH paths still resolve through this + * one method, so they read one index and cannot diverge — while removing the + * full-registry walk that made kernel boot quadratic in stored metadata. + */ + static resolveObjectKey(this: Pick, name: string): string | undefined { + // Canonical: short name lookup. O(1) via {@link objectKeysByShortName}, + // whose array IS the list the pre-#10945 full-registry scan built — same + // members, same order — so `matches[0]` still names the first key + // registered under an ambiguous short name and the warning still names + // every one of them. + const matches = this.objectKeysByShortName.get(name); + if (matches !== undefined && matches.length > 0) { + if (matches.length > 1) { + console.warn( + `[SchemaRegistry] Ambiguous short name "${name}" matches: ${matches.join(', ')}. ` + + `Returning first match. Use FQN to disambiguate.` + ); + } + return matches[0]; + } + + // Fallback: explicit FQN + return this.objectContributors.has(name) ? name : undefined; + } + + /** The body of `SchemaRegistry#getAllObjects`; the method's docblock there is its contract. */ + static getAllObjects(this: Pick, packageId?: string, options?: ObjectExtensionFoldOptions): ServiceObject[] { + const results: ServiceObject[] = []; + + for (const fqn of this.objectContributors.keys()) { + // If filtering by package, check if this package contributes + if (packageId) { + const contributors = this.objectContributors.get(fqn); + const hasContribution = contributors?.some(c => c.packageId === packageId); + if (!hasContribution) continue; + } + + const merged = this.resolveObjectLeavingExtensionsOut(fqn, options); + if (merged) { + // Tag with contributor info for UI + (merged as any)._packageId = this.getObjectOwner(fqn)?.packageId; + results.push(merged); + } + } + + return results; + } + + /** The body of `SchemaRegistry#getObjectContributors`; the method's docblock there is its contract. */ + static getObjectContributors(this: Pick, fqn: string): ObjectContributor[] { + return this.objectContributors.get(fqn) || []; + } + + /** The body of `SchemaRegistry#getObjectExtensionsOf`; the method's docblock there is its contract. */ + static getObjectExtensionsOf(this: Pick, packageId: string): Array<{ extend: string; priority?: number } & Record> { + const out: Array<{ extend: string; priority?: number } & Record> = []; + for (const [fqn, contributors] of this.objectContributors) { + const contribution = contributors.find((c) => c.ownership === 'extend' && c.packageId === packageId); + if (!contribution) continue; + const body: Record = {}; + for (const [key, value] of Object.entries(contribution.definition)) { + if (value === undefined || key === 'name' || key.startsWith('_')) continue; + body[key] = value; + } + out.push({ + extend: fqn, + ...body, + ...(contribution.priority !== DEFAULT_EXTENDER_PRIORITY ? { priority: contribution.priority } : {}), + }); + } + return out; + } + + /** The body of `SchemaRegistry#getObjectOwner`; the method's docblock there is its contract. */ + static getObjectOwner(this: Pick, fqn: string): ObjectContributor | undefined { + const contributors = this.objectContributors.get(fqn); + return contributors?.find(c => c.ownership === 'own'); + } + + /** The body of `SchemaRegistry#getPackagedObjectOwner`; the method's docblock there is its contract. */ + static getPackagedObjectOwner(this: Pick, name: string): ObjectContributor | undefined { + const fqn = this.resolveObjectKey(name); + if (fqn === undefined) return undefined; + const owner = this.objectContributors.get(fqn)?.find(c => c.ownership === 'own'); + return owner && isCodeArtifactBody(owner.definition) ? owner : undefined; + } + + /** The body of `SchemaRegistry#getPackagedObjectBase`; the method's docblock there is its contract. */ + static getPackagedObjectBase(this: Pick, name: string): ServiceObject | undefined { + const owner = this.getPackagedObjectOwner(name); + if (!owner) return undefined; + const fqn = this.resolveObjectKey(name); + const contributors = (fqn === undefined ? undefined : this.objectContributors.get(fqn)) ?? []; + // [ADR-0029 D9.2a, 2026-10-30 note] By name, as the fold merges them: an + // extender's rule of a name the owner already ships is one rule, not two. + const extended = contributors.some((c) => + c.ownership === 'extend' && isCodeArtifactBody(c.definition) + && Array.isArray(c.definition.validations) && c.definition.validations.length > 0); + if (!extended) return owner.definition; + const rules = this.packagedRules(contributors) ?? []; + return { ...owner.definition, validations: rules as ServiceObject['validations'] }; + } + + /** The body of `SchemaRegistry#extendContributionRefusal`; the method's docblock there is its contract. */ + static extendContributionRefusal(schema: { name?: unknown; defaultRowScope?: unknown; membershipKeys?: unknown }, packageId: string | undefined): Error | undefined { + return extendContributionRefusal(schema, packageId); + } + + /** The body of `SchemaRegistry#registerObjectExtension`; the method's docblock there is its contract. */ + static registerObjectExtension(this: Pick, extension: { extend: string; priority?: number } & Record, packageId: string): string { + const { extend, priority, ...contribution } = extension; + return this.registerObject( + { ...contribution, name: extend } as unknown as ServiceObject, + packageId, + undefined, + 'extend', + typeof priority === 'number' ? priority : DEFAULT_EXTENDER_PRIORITY, + ); + } + + /** + * [#28858, ADR-0146 D10] The key an extension of `name` is filed under: the + * key {@link getObject} reads `name` as. That is `name` itself whenever an + * entry is registered under it, or nothing is registered that `name` folds + * to. It differs only for a short name `getObject` folds to a legacy + * `__` key ({@link resolveObjectKey}); filing such an extension + * under the short name would open a second, owner-less entry beside the + * object it meant, which nothing serves and nothing checks. + * + * The folded spelling is named once at `warn`, with the key to write. ⛔ + * Never a refusal: the manifest loop and the boot hydration run while + * packages are still registering (AGENTS.md *Startup registry reads*). The + * `object_extension` item door refuses the same spelling at save (#28834), + * where the author is present to fix it. A legacy owner that registers + * AFTER the extension is not yet there to fold to, so that extension is + * filed under its own spelling, as before. + */ + static extensionTargetKey(this: Pick, name: string, packageId: string | undefined): string { + if (name === '' || this.objectContributors.has(name)) return name; + const resolved = this.resolveObjectKey(name); + if (resolved === undefined || resolved === name) return name; + const key = `${name}|${resolved}|${packageId ?? ''}`; + if (!this.extensionSpellingWarned.has(key)) { + this.extensionSpellingWarned.add(key); + console.warn( + `[Registry] An extension${packageId ? ` from package "${packageId}"` : ''} names its object '${name}', ` + + `which is registered as '${resolved}': the extension is filed under '${resolved}', the object it ` + + `reads as. Write \`extend: '${resolved}'\` to name the object by the key it is registered under.`, + ); + } + return resolved; + } + + /** The body of `SchemaRegistry#withdrawObjectExtension`; the method's docblock there is its contract. */ + static withdrawObjectExtension(this: Pick, name: string, packageId: string): boolean { + const fqn = this.resolveObjectKey(name) ?? name; + const contributors = this.objectContributors.get(fqn); + if (!contributors) return false; + const idx = contributors.findIndex(c => c.ownership === 'extend' && c.packageId === packageId); + if (idx === -1) return false; + contributors.splice(idx, 1); + if (contributors.length === 0) this.closeObjectEntry(fqn); + // The same two invalidations every other contributor mutation performs. + this.mergedObjectCache.delete(fqn); + this._objectRevision += 1; + this.log(`[Registry] Withdrew extend contribution to ${fqn} from ${packageId}`); + return true; + } + + /** The body of `SchemaRegistry#removeObjectOverlay`; the method's docblock there is its contract. */ + static removeObjectOverlay(this: Pick, name: string): boolean { + const fqn = this.resolveObjectKey(name); + if (fqn === undefined) return false; + const contributors = this.objectContributors.get(fqn); + if (!contributors) return false; + const idx = contributors.findIndex(c => c.ownership === 'overlay'); + if (idx === -1) return false; + const [removed] = contributors.splice(idx, 1); + // The same two invalidations every other contributor mutation performs. + this.mergedObjectCache.delete(fqn); + this._objectRevision += 1; + this.log(`[Registry] Removed overlay layer of object: ${fqn} (was bound to ${removed.packageId})`); + return true; + } + + /** The body of `SchemaRegistry#assertSingleOwnerPerObject`; the method's docblock there is its contract. */ + static assertSingleOwnerPerObject(this: Pick): void { + const violations: string[] = []; + for (const [fqn, contributors] of this.objectContributors.entries()) { + const owners = contributors.filter(c => c.ownership === 'own'); + if (owners.length === 0) { + // [ADR-0029 D9.5] The ORPHAN OVERLAY class. Reachable by uninstalling + // the packaged owner while a `sys_metadata` row for its object still + // exists under a binding `unregisterObjectsByPackage` cannot reach by + // package id (the sentinel). It must be LOUD rather than silent: + // `resolveObject` would otherwise warn once and answer `undefined` for + // a name the tenant can still see in `sys_metadata`. + const overlays = contributors.filter(c => c.ownership === 'overlay'); + if (overlays.length > 0) { + const bindings = overlays.map(c => c.packageId).join(', ') || '(none)'; + violations.push( + `Object "${fqn}" has an orphan overlay layer [${bindings}] and no owner. ` + + `An overlay layers OVER an owned object — re-install the package that owns it, ` + + `or delete the sys_metadata row.` + ); + } else { + const extenders = contributors.map(c => c.packageId).join(', ') || '(none)'; + violations.push( + `Object "${fqn}" has no owner — only extend contributions from [${extenders}]. ` + + `Exactly one package must register it with ownership 'own'.` + ); + } + } else if (owners.length > 1) { + const names = owners.map(c => c.packageId).join(', '); + violations.push( + `Object "${fqn}" has ${owners.length} owners [${names}] — exactly one is allowed.` + ); + } + } + if (violations.length > 0) { + throw new Error( + `[Registry] single-owner-per-object check failed (ADR-0029):\n ` + + violations.join('\n ') + ); + } + } + + /** The body of `SchemaRegistry#unregisterObjectsByPackage`; the method's docblock there is its contract. */ + static unregisterObjectsByPackage(this: Pick, packageId: string, force: boolean = false): void { + // [#7970] REFUSAL PASS — see {@link assertPackageUninstallable}, which holds + // the one copy of the predicate ([#21276] extracted so it can be asked + // without uninstalling). + if (!force) this.assertPackageUninstallable(packageId); + + // MUTATION PASS — carries no refusal of its own; the pass above already + // proved every removal below is allowed. Keep it that way: a second copy of + // the predicate here is the two-places-that-must-agree shape this ordering + // fix was chosen over. + for (const [fqn, contributors] of this.objectContributors.entries()) { + // Find this package's contributions + const packageContribs = contributors.filter(c => c.packageId === packageId); + + for (const contrib of packageContribs) { + // Remove contribution + const idx = contributors.indexOf(contrib); + if (idx !== -1) { + contributors.splice(idx, 1); + this.log(`[Registry] Removed ${contrib.ownership} contribution to ${fqn} from ${packageId}`); + } + + // [ADR-0029 D9.7] An overlay layer leaves with the base it layers + // over. Nothing durable is lost — the layer is a runtime projection of + // a `sys_metadata` row this does not touch, and a re-install + // re-hydrates it — whereas leaving it behind would MANUFACTURE the + // orphan-overlay violation D9.5 names, on every uninstall of a + // customized packaged object. Keyed off the OWNER's removal, not off + // the overlay's own binding: a sentinel-bound layer names no package + // and this walk is addressed by package id. + if (contrib.ownership === 'own') { + const overlayIdx = contributors.findIndex(c => c.ownership === 'overlay'); + if (overlayIdx !== -1) { + const [dropped] = contributors.splice(overlayIdx, 1); + this.log( + `[Registry] Removed overlay layer of ${fqn} with its owner ` + + `(layer was bound to ${dropped.packageId})` + ); + } + } + } + + // Clean up empty contributor lists + if (contributors.length === 0) { + this.closeObjectEntry(fqn); + } + + // Invalidate cache + this.mergedObjectCache.delete(fqn); + this._objectRevision += 1; + } + } + + /** The body of `SchemaRegistry#unregisterObject`; the method's docblock there is its contract. */ + static unregisterObject(this: Pick, name: string, options: { force?: boolean } = {}): boolean { + const fqn = this.resolveObjectKey(name); + if (fqn === undefined) return false; + const contributors = this.objectContributors.get(fqn) ?? []; + + const owner = contributors.find(c => c.ownership === 'own'); + if (!options.force) { + const otherExtenders = contributors.filter( + c => c.ownership === 'extend' && c.packageId !== owner?.packageId + ); + if (otherExtenders.length > 0) { + throw new Error( + `Cannot unregister object "${fqn}": it is extended by ` + + `${otherExtenders.map(c => c.packageId).join(', ')}. Unregister the extenders first.` + ); + } + } + + // The whole entry goes: the object no longer exists, so no contribution to + // it does either. Leaving the extenders behind would be the owner-less + // state the guard above exists to prevent. + this.closeObjectEntry(fqn); + // The same two invalidations every other contributor mutation performs — + // the merged-object cache would otherwise keep answering `resolveObject` + // for a name with no contributors, and registry-derived caches (the + // engine's roll-up summary index) would never learn the set moved. + this.mergedObjectCache.delete(fqn); + this._objectRevision += 1; + // Namespaces are deliberately NOT touched: a namespace is registered per + // PACKAGE and shared by every object that package ships, so dropping one + // object must not unregister it. `unregisterObjectsByPackage` leaves it + // alone too — `uninstallPackage` owns that half. + this.log( + `[Registry] Unregistered object: ${fqn} ` + + `(${contributors.length} contribution(s), owner ${owner?.packageId ?? '(none)'})` + ); + return true; + } +} diff --git a/packages/objectql/src/registry-restore.ts b/packages/objectql/src/registry-restore.ts new file mode 100644 index 00000000000..1a93b882a9b --- /dev/null +++ b/packages/objectql/src/registry-restore.ts @@ -0,0 +1,362 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33949 codemod] The "Restore on refusal" section of `SchemaRegistry`: the + * bodies of `restoringOnRefusal`, `recordCurrencyFollowRefusalOfRun`, + * `followRefusalAsServed`, `recordRestoreOnRefusal`, `noteAppNavContributions`, + * `noteObjectEntry`, `noteNamespace`, `noteItemType`, `noteItem`, + * `noteItemOrder`, `notePicklistExtension`, `noteSecurityCatalogClaim`, + * `noteBareNameClaim`, `noteDefaultRecordPageClaims` moved out of `registry.ts` + * whole by `scripts/codemod-split-registry.mjs`. + * + * A MOVE, not a redesign. {@link RestoreFacet} is a stateless facet of `SchemaRegistry` + * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` + * method of the same name exactly as it stood: not re-indented, not + * formatted, not edited. The method stays on the class as a one-call stub with + * its signature, so the published surface, an instance spy and a subclass + * override are what they were. A public or protected method keeps its + * docblock on the class, the contract; a private method's docblock moved here + * with its body. The state stays on `SchemaRegistry`. + * + * The bodies reach the class through {@link RestoreHost}: exactly the + * `SchemaRegistry` members they read, private ones included. Every stub hands + * its body, by a `.call`, the same host: `restoreHost` in + * `registry-facet-hosts.ts`, built once per registry by the class's static + * block. Methods go as closures that call them on the registry at call time, so + * a subclass override or an instance spy is still the one reached; fields and + * accessors go as GETTERS, read when the body reads them, as they were on the + * class, and a field a body assigns goes with a SETTER that assigns it on the + * registry. + * + * Values that stayed in `registry.ts` are imported from it (lever (a) of + * ruling 6591753301); every use is inside a function body, so the import + * cycle is never read at evaluation time. `index.ts` re-exports nothing from + * this module. + */ + +import type { ObjectOwnership, ServiceObject } from '@objectstack/spec/data'; +import { isTenantAuthored } from '@objectstack/metadata-core'; +import type { SecurityCatalogType } from '@objectstack/core'; +import type { BareNameOneHolderType } from './bare-name-one-holder.js'; +import { recordCurrencyFollowRefusalOverEnvironmentObjects } from './record-currency-aggregate-door.js'; +import type { RecordCurrencyFollowRefusedError } from './record-currency-aggregate-door.js'; +import type { FollowDoorLayer } from './registry-top-level.js'; +import { RegistrationRestorePoint } from './registry.js'; +import type { SchemaRegistry } from './registry.js'; + +/** + * The `SchemaRegistry` members the bodies of {@link RestoreFacet} read — every one of + * them, and no other. Each is typed AS the class member (an indexed access on + * the class), so a change to a member's signature on the class is a change + * here too. `readonly` members are the getters; the others are fields a body + * assigns. + */ +export interface RestoreHost { + readonly appNavContributions: SchemaRegistry['appNavContributions']; + readonly bareNameClaims: SchemaRegistry['bareNameClaims']; + readonly bareNameHoldersAreSealed: SchemaRegistry['bareNameHoldersAreSealed']; + closeObjectEntry: SchemaRegistry['closeObjectEntry']; + readonly defaultRecordPageClaims: SchemaRegistry['defaultRecordPageClaims']; + followRefusalAsServed: SchemaRegistry['followRefusalAsServed']; + getAllObjects: SchemaRegistry['getAllObjects']; + getObject: SchemaRegistry['getObject']; + invalidateAll: SchemaRegistry['invalidateAll']; + readonly mergedObjectCache: SchemaRegistry['mergedObjectCache']; + readonly metadata: SchemaRegistry['metadata']; + readonly namespaceRegistry: SchemaRegistry['namespaceRegistry']; + readonly objectContributors: SchemaRegistry['objectContributors']; + readonly picklistExtensionContributions: SchemaRegistry['picklistExtensionContributions']; + recordCurrencyFollowRefusalOf: SchemaRegistry['recordCurrencyFollowRefusalOf']; + recordCurrencyFollowRefusalOfRun: SchemaRegistry['recordCurrencyFollowRefusalOfRun']; + resolveObjectKey: SchemaRegistry['resolveObjectKey']; + resolveOwnerLayer: SchemaRegistry['resolveOwnerLayer']; + restorePoint: SchemaRegistry['restorePoint']; + readonly securityCatalogClaims: SchemaRegistry['securityCatalogClaims']; +} + +/** The bodies of the "Restore on refusal" section of `SchemaRegistry`: a stateless facet, called by the class's stubs. */ +export class RestoreFacet { + /** The body of `SchemaRegistry#restoringOnRefusal`; the method's docblock there is its contract. */ + static restoringOnRefusal(this: Pick, register: () => T): T { + const open = this.restorePoint; + const point = new RegistrationRestorePoint(); + this.restorePoint = point; + try { + const result = register(); + if (result instanceof Promise) { + throw new TypeError('[Registry] restoringOnRefusal takes a synchronous registration; it was handed one that returned a promise.'); + } + if (open) { + open.adopt(point); + } else { + const follow = this.recordCurrencyFollowRefusalOfRun(point); + if (follow) throw follow; + } + return result; + } catch (err) { + point.restore(); + this.invalidateAll(); + throw err; + } finally { + this.restorePoint = open; + } + } + + /** + * [ADR-0144 D2 and D6, amended] The record-currency follow door, asked of + * one whole registration run at its end: the refusal of the first object + * layer the run registered that the door refuses, or `undefined`. + * + * A single save asks the door before its one body registers + * ({@link objectRegistrationRefusal}'s `againstRegistry`). A run registers + * several, in an order a later one can still complete — a package's + * `objectExtensions` after its `objects`, a later package of the artifact, + * its residual, on a reload the new version of an object an earlier body + * follows — so no registration inside it can ask. Its end can: each layer is + * asked as a single save of it would be, with the registry holding the whole + * run, as its object is served ({@link servedFoldWithLayer}), and against + * the object as it was served with what the run replaced in the layer's slot: + * a pair the run leaves as it found it is not the run's to answer for + * (#33593 ruling R7). A pair with an end outside the run is judged against the + * object that is registered, as a single save judges it. Where that object + * is the environment's own, lying over a package's, and only it earns the + * refusal, the refusal says so ({@link followRefusalAsServed}). + * + * ⛔ Asked only of a layer the served object reads. An owner under a + * tenant's overlay is not one: the overlay's body is the base that is + * served ({@link resolveObject}), the run did not register it, and asking + * the owner beneath it would refuse a package over a code field the served + * object holds. Nor is a layer a later write of the run took + * out again (the same package registered twice). + * + * Asked only once the cold boot has sealed the registry, for the reason the + * single save's question is: before that an `extend` contribution or the + * other end can still register (AGENTS.md, startup registry reads). D6's + * roll-up half is not asked again: {@link registerObject} asked it of every + * layer as it registered. + */ + static recordCurrencyFollowRefusalOfRun(this: Pick, point: RegistrationRestorePoint): Error | undefined { + if (!this.bareNameHoldersAreSealed) return undefined; + const layers = point.registeredObjects(); + if (layers.length === 0) return undefined; + // One read for the whole question: nothing registers while it is asked. + const all = this.getAllObjects(); + for (const { fqn, schema, contributor, replaced } of layers) { + const sitting = this.objectContributors.get(fqn); + if (!sitting?.includes(contributor)) continue; + if (contributor.ownership === 'own' && sitting.some((c) => c.ownership === 'overlay')) continue; + const layer: FollowDoorLayer = { packageId: contributor.packageId, priority: contributor.priority, before: replaced }; + const refusal = this.recordCurrencyFollowRefusalOf(schema, contributor.ownership, layer, () => all); + if (refusal) return this.followRefusalAsServed(refusal, schema, contributor.ownership, layer, all); + } + return undefined; + } + + /** + * [ADR-0144 D2 and D6, amended] A run's follow refusal that the packages' + * own definitions would not earn, said as what it is. + * + * An object can be served from the environment's own object of the same + * name, lying over the definition a package ships ({@link resolveObject}'s + * base). The run's end judges a pair against what is served, so a package + * that is right in itself is refused when that served object lacks the other + * end, and the door's sentence then tells its author to correct a declaration + * that is not wrong. So the question is asked once more with each such + * object read through its package's definition ({@link resolveOwnerLayer}). + * Where that clears the refusal, the sentence gains the fact and the objects + * it is true of ({@link recordCurrencyFollowRefusalOverEnvironmentObjects}); + * the refusal itself, its code and its status stand. Any other refusal is + * returned as it is. Only a refused run pays for the second question. + */ + static followRefusalAsServed(this: Pick, refusal: RecordCurrencyFollowRefusedError, schema: ServiceObject, ownership: ObjectOwnership, layer: FollowDoorLayer, all: readonly ServiceObject[]): RecordCurrencyFollowRefusedError { + const overPackage: string[] = []; + for (const [fqn, sitting] of this.objectContributors) { + const overlay = sitting.find((c) => c.ownership === 'overlay'); + if (overlay && isTenantAuthored(overlay.definition) && sitting.some((c) => c.ownership === 'own')) overPackage.push(fqn); + } + if (overPackage.length === 0) return refusal; + /** Is the layer still refused with `throughPackage` read as their packages define them? */ + const refusedReading = (throughPackage: ReadonlySet): boolean => { + const read = (object: ServiceObject | undefined): ServiceObject | undefined => { + const fqn = object === undefined ? undefined : this.resolveObjectKey(object.name); + return fqn !== undefined && throughPackage.has(fqn) ? this.resolveOwnerLayer(fqn) ?? object : object; + }; + const objects = all.map((object) => read(object) ?? object); + return this.recordCurrencyFollowRefusalOf(schema, ownership, layer, () => objects, (name) => read(this.getObject(name))) !== undefined; + }; + if (refusedReading(new Set(overPackage))) return refusal; + // The ones that decide it, each on its own; all of them when only together they do. + const deciding = overPackage.filter((fqn) => !refusedReading(new Set([fqn]))); + return recordCurrencyFollowRefusalOverEnvironmentObjects(refusal, deciding.length > 0 ? deciding : overPackage); + } + + /** The body of `SchemaRegistry#recordRestoreOnRefusal`; the method's docblock there is its contract. */ + static recordRestoreOnRefusal(this: Pick, key: string, capture: () => () => void): void { + this.restorePoint?.note(`outside\u0000${key}`, capture); + } + + /** Record one app's list of navigation contributions before its first write. */ + static noteAppNavContributions(this: Pick, app: string): void { + this.restorePoint?.note(`navContributions\u0000${app}`, () => { + const saved = this.appNavContributions.get(app); + const copy = saved ? [...saved] : undefined; + return () => { + if (copy) this.appNavContributions.set(app, copy); + else this.appNavContributions.delete(app); + }; + }); + } + + /** Record an object's contributor list (and each contributor's mutable slots) before its first write. */ + static noteObjectEntry(this: Pick, fqn: string): void { + this.restorePoint?.note(`object\u0000${fqn}`, () => { + const list = this.objectContributors.get(fqn); + if (!list) return () => this.closeObjectEntry(fqn); + // The late-install branch re-classifies a sitting contributor in place, + // so its ownership and priority are kept beside the reference. + const saved = list.map((c) => ({ c, ownership: c.ownership, priority: c.priority })); + return () => { + for (const s of saved) { + s.c.ownership = s.ownership; + s.c.priority = s.priority; + } + list.splice(0, list.length, ...saved.map((s) => s.c)); + this.mergedObjectCache.delete(fqn); + }; + }); + } + + /** Record a namespace's owner set before its first write. */ + static noteNamespace(this: Pick, namespace: string): void { + this.restorePoint?.note(`namespace\u0000${namespace}`, () => { + const owners = this.namespaceRegistry.get(namespace); + const saved = owners ? new Set(owners) : undefined; + return () => { + if (saved) this.namespaceRegistry.set(namespace, saved); + else this.namespaceRegistry.delete(namespace); + }; + }); + } + + /** Record whether an item type had a store at all, before the run can create one. */ + static noteItemType(this: Pick, type: string): void { + this.restorePoint?.note(`type\u0000${type}`, () => { + const hadType = this.metadata.has(type); + return () => { + if (!hadType && this.metadata.get(type)?.size === 0) this.metadata.delete(type); + }; + }); + } + + /** Record one item slot before its first write. */ + static noteItem(this: Pick, type: string, key: string): void { + this.restorePoint?.note(`item\u0000${type}\u0000${key}`, () => { + const collection = this.metadata.get(type); + const had = collection?.has(key) ?? false; + const value = collection?.get(key); + return () => { + const now = this.metadata.get(type); + if (had) now?.set(key, value); + else now?.delete(key); + }; + }); + } + + /** + * [#33050] Record the ORDER of one item type's slots before the run's first + * removal from it ({@link replacingPackageBody}). A removed slot put back by + * {@link noteItem} alone would come back last, and the store's order is read: + * it is the order {@link listItems} answers and the first composite match a + * caller naming no package is served. Put back after every later write of + * the run is undone, so the slots come back where they stood. + */ + static noteItemOrder(this: Pick, type: string): void { + this.restorePoint?.note(`itemOrder\u0000${type}`, () => { + const saved = [...(this.metadata.get(type) ?? [])]; + return () => { + const now = this.metadata.get(type); + if (!now) return; + now.clear(); + for (const [key, value] of saved) now.set(key, value); + }; + }); + } + + /** Record this package's extension of one picklist before its first write. */ + static notePicklistExtension(this: Pick, target: string, packageKey: string): void { + this.restorePoint?.note(`picklistExtension\u0000${target}\u0000${packageKey}`, () => { + const byPackage = this.picklistExtensionContributions.get(target); + const saved = byPackage?.get(packageKey); + return () => { + let now = this.picklistExtensionContributions.get(target); + if (saved) { + if (!now) this.picklistExtensionContributions.set(target, now = new Map()); + now.set(packageKey, saved); + } else if (now) { + now.delete(packageKey); + if (now.size === 0) this.picklistExtensionContributions.delete(target); + } + }; + }); + } + + /** Record who holds one security catalog name before its first write. */ + static noteSecurityCatalogClaim(this: Pick, type: SecurityCatalogType, name: string): void { + this.restorePoint?.note(`catalogClaim\u0000${type}\u0000${name}`, () => { + const saved = this.securityCatalogClaims.get(type)?.get(name); + const copy = saved ? new Set(saved) : undefined; + return () => { + let byName = this.securityCatalogClaims.get(type); + if (copy !== undefined) { + if (!byName) this.securityCatalogClaims.set(type, byName = new Map()); + byName.set(name, copy); + } else { + byName?.delete(name); + } + }; + }); + } + + /** Record who holds one bare name (`bare-name-one-holder.ts`) before its first write. */ + static noteBareNameClaim(this: Pick, type: BareNameOneHolderType, name: string): void { + this.restorePoint?.note(`bareNameClaim\u0000${type}\u0000${name}`, () => { + const saved = this.bareNameClaims.get(type)?.get(name); + const copy = saved ? new Set(saved) : undefined; + return () => { + let byName = this.bareNameClaims.get(type); + if (copy !== undefined) { + if (!byName) this.bareNameClaims.set(type, byName = new Map()); + byName.set(name, copy); + } else { + byName?.delete(name); + } + }; + }); + } + + /** + * Record the default record page claims before `packageId`'s first rewrite + * of them ({@link recordDefaultRecordPageClaims} drops all of the package's + * claims, then adds what the manifest declares). Put back per object, and + * only for the objects `packageId` held a claim on before or after, each in + * its holders' original order. + */ + static noteDefaultRecordPageClaims(this: Pick, packageId: string): void { + // Keyed by slot (`recordPageSlotKey`): an object's default, or one app's + // page of it. Every slot is put back the same way. + this.restorePoint?.note(`defaultPageClaims\u0000${packageId}`, () => { + const saved = new Map>(); + for (const [object, byPackage] of this.defaultRecordPageClaims) saved.set(object, new Map(byPackage)); + return () => { + const objects = new Set(); + for (const [object, byPackage] of saved) if (byPackage.has(packageId)) objects.add(object); + for (const [object, byPackage] of this.defaultRecordPageClaims) if (byPackage.has(packageId)) objects.add(object); + for (const object of objects) { + const before = saved.get(object); + if (before) this.defaultRecordPageClaims.set(object, new Map(before)); + else this.defaultRecordPageClaims.delete(object); + } + }; + }); + } +} diff --git a/packages/objectql/src/registry-storage.ts b/packages/objectql/src/registry-storage.ts new file mode 100644 index 00000000000..08b97811d47 --- /dev/null +++ b/packages/objectql/src/registry-storage.ts @@ -0,0 +1,187 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#33949 codemod] The + * "Logging control, object storage and generic metadata storage" section of + * `SchemaRegistry`: the bodies of `log`, `warn`, `debug`, `openObjectEntry`, + * `closeObjectEntry`, `setInitialDisabledPackageIds`, `onPackageEnabled`, + * `announcePackageEnabled`, `onPackageUninstalled`, + * `announcePackageUninstalled` moved out of `registry.ts` whole by + * `scripts/codemod-split-registry.mjs`. + * + * A MOVE, not a redesign. {@link StorageFacet} is a stateless facet of `SchemaRegistry` + * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` + * method of the same name exactly as it stood: not re-indented, not + * formatted, not edited. The method stays on the class as a one-call stub with + * its signature, so the published surface, an instance spy and a subclass + * override are what they were. A public or protected method keeps its + * docblock on the class, the contract; a private method's docblock moved here + * with its body. The state stays on `SchemaRegistry`. + * + * The bodies reach the class through {@link StorageHost}: exactly the + * `SchemaRegistry` members they read, private ones included. Every stub hands + * its body, by a `.call`, the same host: `storageHost` in + * `registry-facet-hosts.ts`, built once per registry by the class's static + * block. Methods go as closures that call them on the registry at call time, so + * a subclass override or an instance spy is still the one reached; fields and + * accessors go as GETTERS, read when the body reads them, as they were on the + * class, and a field a body assigns goes with a SETTER that assigns it on the + * registry. + * + * The module imports `registry.ts` for types only, so the two modules form no + * evaluation cycle. `index.ts` re-exports nothing from this module. + */ + +import { parseFQN } from './registry-top-level.js'; +import type { ObjectContributor } from './registry-top-level.js'; +import type { SchemaRegistry } from './registry.js'; + +/** + * The `SchemaRegistry` members the bodies of {@link StorageFacet} read — every one of + * them, and no other. Each is typed AS the class member (an indexed access on + * the class), so a change to a member's signature on the class is a change + * here too. `readonly` members are the getters; the others are fields a body + * assigns. + */ +export interface StorageHost { + readonly _logLevel: SchemaRegistry['_logLevel']; + initialDisabledPackageIds: SchemaRegistry['initialDisabledPackageIds']; + readonly objectContributors: SchemaRegistry['objectContributors']; + readonly objectKeysByShortName: SchemaRegistry['objectKeysByShortName']; + readonly packageEnabledListeners: SchemaRegistry['packageEnabledListeners']; + readonly packageUninstalledListeners: SchemaRegistry['packageUninstalledListeners']; +} + +/** The bodies of the "Logging control, object storage and generic metadata storage" section of `SchemaRegistry`: a stateless facet, called by the class's stubs. */ +export class StorageFacet { + /** The body of `SchemaRegistry#log`; the method's docblock there is its contract. */ + static log(this: Pick, msg: string): void { + if (this._logLevel === 'silent' || this._logLevel === 'error' || this._logLevel === 'warn') return; + console.log(msg); + } + + /** + * [#14553] A diagnostic an operator filters FOR — emitted at every level down + * to `warn`, silent only where the deployment has asked for silence + * (`error`, `silent`). + * + * The level ladder is `debug < info < warn < error < silent`, so this is the + * mirror of {@link log}: that one speaks only ABOVE `warn` and is therefore + * the wrong channel for anything a `warn`-level deployment must not miss. + * `OS_REGISTRY_LOG=warn` is what the objectql vitest config selects and what + * a quiet production boot selects, and a relocation nobody can see there was + * the defect this method exists to end. + */ + static warn(this: Pick, msg: string): void { + if (this._logLevel === 'silent' || this._logLevel === 'error') return; + console.warn(msg); + } + + /** + * Debug-only diagnostic: emitted solely when `logLevel === 'debug'`, so it + * stays out of the default (`'info'`) boot log. Use for expected-but-noisy + * housekeeping — e.g. re-registration on a metadata rebuild / HMR reload, + * which looks like an error (`console.warn`) but is a normal path (#3420). + */ + static debug(this: Pick, msg: string): void { + if (this._logLevel !== 'debug') return; + console.debug(msg); + } + + /** + * [#10945] The ONLY writer of a new `objectContributors` key. Returns the + * contributor list for `fqn`, creating an empty one — and indexing it under + * its short name — on first use. + * + * A choke point rather than a discipline: the short-name index is maintained + * inside the same statement that adds the key, so no future caller can add a + * contributor list and forget the index half. + */ + static openObjectEntry(this: Pick, fqn: string): ObjectContributor[] { + const existing = this.objectContributors.get(fqn); + if (existing) return existing; + + const contributors: ObjectContributor[] = []; + this.objectContributors.set(fqn, contributors); + const { shortName } = parseFQN(fqn); + const keys = this.objectKeysByShortName.get(shortName); + // Append, never replace: `matches[0]` is first-registered-wins, and both + // containers are appended to here in the same order, so the index array is + // `objectContributors`' key order narrowed to this short name. + if (keys) keys.push(fqn); + else this.objectKeysByShortName.set(shortName, [fqn]); + return contributors; + } + + /** + * [#10945] The ONLY remover of an `objectContributors` key — the mirror of + * {@link openObjectEntry}. A no-op when the key is not registered, so removal + * stays idempotent. + * + * Empty short-name buckets are deleted rather than left behind: a retained + * empty array would make {@link resolveObjectKey}'s index hit and its FQN + * fallback disagree for a name that is both (a legacy `__` key + * removed while a plain key of the same short name is still registered). + */ + static closeObjectEntry(this: Pick, fqn: string): void { + if (!this.objectContributors.delete(fqn)) return; + const { shortName } = parseFQN(fqn); + const keys = this.objectKeysByShortName.get(shortName); + if (!keys) return; + const idx = keys.indexOf(fqn); + if (idx !== -1) keys.splice(idx, 1); + if (keys.length === 0) this.objectKeysByShortName.delete(shortName); + } + + /** The body of `SchemaRegistry#setInitialDisabledPackageIds`; the method's docblock there is its contract. */ + static setInitialDisabledPackageIds(this: Pick, ids: Iterable): void { + this.initialDisabledPackageIds = new Set(ids); + } + + /** The body of `SchemaRegistry#onPackageEnabled`; the method's docblock there is its contract. */ + static onPackageEnabled(this: Pick, listener: (packageId: string) => void): () => void { + this.packageEnabledListeners.add(listener); + return () => { this.packageEnabledListeners.delete(listener); }; + } + + /** Tell every {@link onPackageEnabled} listener. Never throws: the enable has happened, and a failing listener is logged at `error`. */ + static announcePackageEnabled(this: Pick, id: string): void { + for (const listener of this.packageEnabledListeners) { + try { + listener(id); + } catch (err) { + console.error( + `[Registry] package "${id}" was enabled, but a listener bringing state derived from it current failed ` + + `(${(err as Error)?.message ?? err}) — that state may still read as it did while the package was disabled.`, + ); + } + } + } + + /** The body of `SchemaRegistry#onPackageUninstalled`; the method's docblock there is its contract. */ + static onPackageUninstalled(this: Pick, listener: (packageId: string) => void): () => void { + this.packageUninstalledListeners.add(listener); + return () => { this.packageUninstalledListeners.delete(listener); }; + } + + /** + * Tell every {@link onPackageUninstalled} listener. Never throws: the + * uninstall has already happened, so a throwing listener must not read as a + * refused one to the door that called {@link uninstallPackage}. It is logged + * at `error` instead, because that listener's state now disagrees with this + * registry while everything looks healthy. + */ + static announcePackageUninstalled(this: Pick, id: string): void { + for (const listener of this.packageUninstalledListeners) { + try { + listener(id); + } catch (err) { + console.error( + `[Registry] package "${id}" was uninstalled, but a listener keeping state keyed by its id failed to drop it ` + + `(${(err as Error)?.message ?? err}) — that state still names the package until the next restart, ` + + 'while the registry no longer does. Restart the runtime to bring the two back in line.', + ); + } + } + } +} diff --git a/packages/objectql/src/registry-top-level.ts b/packages/objectql/src/registry-top-level.ts new file mode 100644 index 00000000000..363aa49d44c --- /dev/null +++ b/packages/objectql/src/registry-top-level.ts @@ -0,0 +1,2748 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +// [#33949 codemod] The top-level declarations ahead of `SchemaRegistry` in +// `registry.ts` that moved out whole by `scripts/codemod-split-registry.mjs` +// (lever (b) of ruling 6591753301). +// +// A MOVE, not a redesign: each declaration below is its `registry.ts` text +// exactly, with the comments above it, in the order it stood there. A LEAF: +// it imports neither `registry.ts` nor any `registry-*` module, so it is +// evaluated before the class and forms no cycle with it. `registry.ts` +// re-exports, by name, every declaration here it exported before, so every +// importer of `./registry.js` reads what it read. + +import { AUDIT_PROVENANCE_FIELDS, BASE_FIELD_DISPLAY_WHITELIST, LEGACY_API_METHODS, MANAGED_BY_WRITE_LOCKED_BUCKETS, checkManagedApiMethodAffordances, isTenancyDisabled, resolveInjectedSystemColumns } from '@objectstack/spec/data'; +import type { ObjectExtensionParsed, ObjectOwnership, ServiceObject } from '@objectstack/spec/data'; +import { AUDIT_FIELD_DEFS, AUDIT_FIELD_GOVERNANCE, OWNER_FIELD_DEF, OWNING_BUSINESS_UNIT_FIELD_DEF, QUEUE_FIELD_DEF, TENANT_SCOPE_FIELD_DEF, readDiscriminatorValue as discriminatorValue, isTenantAuthored } from '@objectstack/metadata-core'; +import type { FieldPatchTargetMiss } from '@objectstack/metadata-core'; +import { scalarOverridesPackagedBase } from '@objectstack/spec/system'; +import type { SystemFieldName } from '@objectstack/spec/system'; +import { checkFieldCompleteness } from '@objectstack/spec/kernel'; +import type { ObjectStackManifest } from '@objectstack/spec/kernel'; +import { DEFAULT_FLOW_FUNCTION_EFFECT } from '@objectstack/spec/automation'; +import { ActionSchema } from '@objectstack/spec/ui'; +import type { SecurityCatalogType } from '@objectstack/core'; +import { describeSecurityCatalogHolder, securityCatalogTypeLabel } from './security-catalog-namespace.js'; +import type { SecurityCatalogHolder } from './security-catalog-namespace.js'; +import { bareNameReason, bareNameTypeLabel, describeBareNameHolder } from './bare-name-one-holder.js'; +import type { BareNameHolder, BareNameOneHolderType } from './bare-name-one-holder.js'; +import type { DefaultRecordPageHolder } from './default-record-page.js'; +import type { PicklistOptionLike } from './picklist-resolution.js'; +import type { PackageScopedHolder } from './package-scoped-resolver.js'; +import { GLOBAL_ACTION_OBJECT_KEY, isObjectLessActionKey, standaloneActionOwnerKey } from './action-governance.js'; + +/** + * [#24400] A bound top-level action whose target object no package has + * registered — see {@link SchemaRegistry.findUnboundActionTargets}. + */ +export interface UnboundActionTarget { + /** The action's `name`. */ + action: string; + /** The object its `objectName` names. */ + objectName: string; + /** The package that ships the action. */ + packageId: string | undefined; +} + +/** The boot refusal's error: the `INVALID_METADATA` envelope (ADR-0112) the picklist refusal also answers. */ +export interface UnboundActionTargetError extends Error { + code: 'INVALID_METADATA'; + status: 422; + httpStatus: 422; +} + +/** + * [#24400] The refusal for bound top-level actions whose target object no + * loaded package registers — every one of them, so an author fixes the set in + * one pass. `undefined` when there are none. + * + * Refused rather than dropped: such an action registers, is listed on no + * object, and no client renders it — the silent drop the extension guidance + * would otherwise lead an author into. + */ +export function describeUnboundActionTargets( + unbound: readonly UnboundActionTarget[], +): UnboundActionTargetError | undefined { + if (unbound.length === 0) return undefined; + const lines = unbound.map( + (u) => `action '${u.action}' (${u.packageId ? `package '${u.packageId}'` : 'a registration with no package'}) ` + + `is bound to object '${u.objectName}'`, + ); + const err = new Error( + `${lines.length === 1 ? 'A top-level action is' : `${lines.length} top-level actions are`} bound by ` + + `\`objectName\` to an object that no loaded package registers: ${lines.join('; ')}. A bound action ` + + 'is attached to its object when the object is served, so with no such object it would be listed ' + + 'nowhere. Register the object (in the package or in one it depends on), or correct the ' + + '`objectName`.', + ) as UnboundActionTargetError; + err.code = 'INVALID_METADATA'; + err.status = 422; + err.httpStatus = 422; + return err; +} + +/** + * [#25649] The refusal of a code package's `action` that does not satisfy + * `ActionSchema` — the `INVALID_METADATA` envelope (ADR-0112) the unbound-target + * refusal above answers, with `issues[]` locating each failure. + */ +interface PackageActionSchemaError extends Error { + code: 'INVALID_METADATA'; + status: 422; + httpStatus: 422; + issues: Array<{ path: string; message: string; code: string }>; +} + +/** + * [#25649] A code package's `action` item, as its `ActionSchema` parse — the + * body {@link SchemaRegistry.registerItem} stores — or the loud refusal of one + * that does not parse. + * + * Why the stored item has to BE the parse: `ActionSchema` is not only a judge, + * it is a producer. Its transform lowers `requiresMembershipReach` and + * `requiresFeature` into the CEL `visible` predicate and deletes the two keys, + * and nothing downstream reads either key raw — so an action stored as authored + * carries both gates declared and inert. A platform object's actions are parsed + * where they are authored (`ObjectSchema.create`), and a strict `defineStack` + * parses a stack's actions; a programmatic `registerApp`, a nested plugin's + * manifest, a `strict: false` stack or a raw plugin manifest reached this store + * with no parse at all, and {@link SchemaRegistry.attachBoundActions} served + * what it found. Parsing at this seam, once, means every reader — the bound + * attachment, the router's rung 2, `GET /meta/action` — holds the one parsed + * body, by reference. + * + * The parse is idempotent over its own output (a lowered action carries no + * `requiresFeature` left to lower), so an action a strict `defineStack` already + * parsed is stored deep-equal to what it arrived as. Besides the lowering, the + * parse fills the schema's defaults (`type`, `refreshAfter`, …), turns a + * CEL-string shorthand into its `{ dialect, source }` envelope, and refuses an + * undeclared key; the ADR-0010 envelope (`_packageId`, `_packageVersion`, + * `_provenance`, `_lock`) is declared on the schema and passes through. + * + * Refused rather than stored unparsed: an action that fails the parse would be + * served with whatever gates it declares never taking effect, and `defineStack` + * and `os validate` refuse the same declaration at authoring time. + * + * [#25744] The one parse for both places a code package declares an action: + * a standalone `action` item ({@link SchemaRegistry.registerItem}) and an + * object's own `actions` ({@link parsePackageObjectActions}, called by + * {@link SchemaRegistry.registerObject}). `owner` names the object an own + * action is declared on, so the refusal says which object and `issues[]` + * locates each failure inside its body. + */ +function parsePackageAction( + item: unknown, + name: string, + packageId: string, + owner?: { object: string; index: number }, +): unknown { + const parsed = ActionSchema.safeParse(item); + if (parsed.success) return parsed.data; + // [#25744] An object's own action is located inside the object body it was + // declared in (`actions..`), the path `ObjectSchema` reports. + const prefix = owner ? ['actions', String(owner.index)] : []; + const issues = parsed.error.issues.map((issue) => ({ + path: [...prefix, ...issue.path.map(String)].join('.'), + message: issue.message, + code: issue.code, + })); + const subject = owner + ? `Action '${name}' of object '${owner.object}' in package '${packageId}'` + : `Action '${name}' of package '${packageId}'`; + const err = new Error( + `${subject} does not satisfy the action contract (ActionSchema): ` + + issues.map((issue) => (issue.path ? `${issue.path}: ${issue.message}` : issue.message)).join('; ') + + '. Every action a package ships is stored as its schema parse — the parse that lowers ' + + '`requiresFeature` and `requiresMembershipReach` into `visible` — so an action that does not parse ' + + 'is refused rather than served with its declared gates inert. Correct the declaration; `defineStack` ' + + 'and `os validate` report the same issues.', + ) as PackageActionSchemaError; + err.code = 'INVALID_METADATA'; + err.status = 422; + err.httpStatus = 422; + err.issues = issues; + throw err; +} + +/** + * [#25744] A code package's object, its own `actions` stored as their + * `ActionSchema` parse ({@link parsePackageAction}) — or the loud refusal of + * the first one that does not parse. + * + * The object-path twin of {@link SchemaRegistry.registerItem}'s action parse. + * `ObjectSchema`'s `actions` is `ActionSchema[]`, so a platform object + * (`ObjectSchema.create`) and a strict `defineStack` already hand this method + * parsed actions, and the parse being idempotent over its own output stores + * them deep-equal to what arrived. A raw manifest, a `strict: false` stack or a + * programmatic `registerApp` reached the store with its objects' actions as + * written, so a declared `requiresFeature` / `requiresMembershipReach` was + * served raw, never lowered into `visible`. + * + * Returns `schema` by reference when it declares no `actions` array, and a + * shallow copy carrying the parsed array otherwise: the caller's body is + * never mutated. + */ +function parsePackageObjectActions(schema: ServiceObject, packageId: string): ServiceObject { + const actions = (schema as { actions?: unknown }).actions; + if (!Array.isArray(actions) || actions.length === 0) return schema; + const parsed = actions.map((action, index) => { + const declared = (action as { name?: unknown } | null | undefined)?.name; + const name = typeof declared === 'string' && declared !== '' ? declared : `#${index}`; + return parsePackageAction(action, name, packageId, { object: schema.name, index }); + }); + return { ...schema, actions: parsed } as ServiceObject; +} + +/** + * [#24472] The key {@link SchemaRegistry}'s bound-action collision audit is + * carried under. A symbol, and ⛔ deliberately NOT re-exported from the package + * entry (`index.ts`, `core.ts`), so the audit is no NAMEABLE member of the + * registry's published face: only the ObjectQL plugin's boot and late-artifact + * reports reach it, importing the key from this module. + */ +export const FIND_BOUND_ACTION_COLLISIONS: unique symbol = Symbol('objectstack.objectql.findBoundActionCollisions'); + +/** + * [#24472] A bound top-level action that its target object does NOT list, + * because the object already lists a DIFFERENT declaration under the same + * name — see {@link FIND_BOUND_ACTION_COLLISIONS}. + */ +export interface BoundActionCollision { + /** The object both declarations are for. */ + objectName: string; + /** The action name they share. */ + action: string; + /** The package whose declaration the object serves — its owner, for the object's own action. */ + servedPackageId: string | undefined; + /** The package whose bound declaration the object does not list. */ + shadowedPackageId: string | undefined; +} + +const describePackage = (packageId: string | undefined): string => + packageId ? `package '${packageId}'` : 'a registration with no package'; + +/** + * [#24472] The author-visible warning for one {@link BoundActionCollision}. + * Reported, never refused: a refusal would be a narrowing, and the decision + * for this collision is to report it first. + */ +export function describeBoundActionCollision(collision: BoundActionCollision): string { + const { objectName, action, servedPackageId, shadowedPackageId } = collision; + return `Action '${action}' on object '${objectName}' is declared twice, and the two declarations differ: ` + + `${describePackage(servedPackageId)} declares the one the object serves, and ` + + `${describePackage(shadowedPackageId)} binds another by \`objectName\`, which the object does not list. ` + + `Both register their handler under the one key '${objectName}:${action}', so when both carry one, the ` + + 'handler that runs is whichever package registered last — not necessarily the declaration the object ' + + 'lists. Rename one of the two actions, or drop the duplicate declaration.'; +} + +/** + * Keys that say where an action declaration came from or where it sits, not + * what it declares: the registry's stamps (`_packageId`, `_provenance`, the + * `_lock` envelope…), the `protection` block those stamps translate, and + * `objectName`, which an object's own action leaves to its position. + */ +const isActionProvenanceKey = (key: string): boolean => + key.startsWith('_') || key === 'protection' || key === 'objectName'; + +/** + * [#24472] Are two action declarations the SAME declaration — one carried + * twice (a same-stack merge's copy beside the top-level item, one declaration + * shipped by two packages, a copy re-bridged under another package's stamp)? + * Structural equality minus {@link isActionProvenanceKey} keys at the top + * level; a key set to `undefined` reads as absent, callables compare by + * reference, and past depth 32 the answer is "different" — the reported side. + */ +function sameActionDeclaration(a: unknown, b: unknown, depth = 0): boolean { + if (Object.is(a, b)) return true; + if (depth >= 32 || typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false; + if (Array.isArray(a) !== Array.isArray(b)) return false; + if (Array.isArray(a)) { + const other = b as unknown[]; + return a.length === other.length && a.every((item, i) => sameActionDeclaration(item, other[i], depth + 1)); + } + const left = a as Record; + const right = b as Record; + const keysOf = (o: Record) => + Object.keys(o).filter((k) => o[k] !== undefined && (depth > 0 || !isActionProvenanceKey(k))); + const leftKeys = keysOf(left); + return leftKeys.length === keysOf(right).length + && leftKeys.every((k) => right[k] !== undefined && sameActionDeclaration(left[k], right[k], depth + 1)); +} + +/** A manifest and its nested `plugins[]`, which register under its package (`registerPlugin`). */ +function manifestAndNestedPlugins(manifest: any): any[] { + const plugins = Array.isArray(manifest?.plugins) + ? manifest.plugins.filter((p: unknown) => p !== null && typeof p === 'object') + : []; + return [manifest, ...plugins]; +} + +/** + * [#24400] The bound top-level actions one manifest brings — its own + * `actions` and its nested plugins', each with the object it is bound to — + * read BEFORE the manifest registers, by the post-boot install door. + */ +export function collectManifestBoundActions(manifest: any, packageId: string | undefined): UnboundActionTarget[] { + const out: UnboundActionTarget[] = []; + for (const source of manifestAndNestedPlugins(manifest)) { + for (const action of Array.isArray(source?.actions) ? source.actions : []) { + if (!action || typeof action !== 'object' || typeof action.name !== 'string') continue; + const objectName = standaloneActionOwnerKey(action); + if (isObjectLessActionKey(objectName)) continue; + out.push({ action: action.name, objectName, packageId }); + } + } + return out; +} + +/** [#24400] The object names one manifest declares, nested plugins included, in either authored spelling. */ +export function collectManifestObjectNames(manifest: any): string[] { + const names: string[] = []; + for (const source of manifestAndNestedPlugins(manifest)) { + const objects = source?.objects; + if (Array.isArray(objects)) { + for (const o of objects) if (typeof o?.name === 'string') names.push(o.name); + } else if (objects && typeof objects === 'object') { + names.push(...Object.keys(objects)); + } + } + return names; +} + +/** + * Stable sort by `order` (unset reads as `0`), by reference when no action + * sets one — the rule `defineStack()` applies to each object's merged actions + * (`sortActionsByOrder` in `@objectstack/spec`'s stack builder, which is + * internal there), applied here to the attached actions among themselves. + */ +function sortActionsByOrder(actions: T[]): T[] { + if (!actions.some((a) => a?.order !== undefined)) return actions; + return actions.slice().sort((a, b) => (a?.order ?? 0) - (b?.order ?? 0)); +} + +/** + * Reserved namespaces that do not get FQN prefix applied. + * Objects in these namespaces keep their short names (e.g., "user" — short name IS the canonical key). + */ +export const RESERVED_NAMESPACES = new Set(['base', 'system']); + +/** + * Default priorities for the three contributor kinds. + * + * [ADR-0029 D9.3] `DEFAULT_OVERLAY_PRIORITY` sits BETWEEN the owner and the + * extenders so a `getObjectContributors()` read lists the stack in layer + * order. It is ORDERING ONLY: {@link SchemaRegistry.resolveObject} selects its + * base layer by KIND, never by "highest priority wins" — extender priority is + * author-declared (`ext.priority ?? 200` in the `objectExtensions` loop), so a + * package could otherwise re-rank a tenant's overlay out of the base slot by + * declaring `priority: 140`. Declared numbers order PEERS; they must not be + * able to change WHICH LAYER IS THE BASE. + */ +export const DEFAULT_OWNER_PRIORITY = 100; +export const DEFAULT_OVERLAY_PRIORITY = 150; +export const DEFAULT_EXTENDER_PRIORITY = 200; + +/** + * Contributor Record + * Tracks how a package contributes to an object. + * + * Three kinds (ADR-0029 D9): + * + * - `own` — the single owning package. Defines the base schema, the table and + * the namespace claim. Exactly one per object name, unconditionally + * ({@link SchemaRegistry.assertSingleOwnerPerObject}). + * - `overlay` — [D9.1] a TENANT customization layer over the owner's + * definition, hydrated from a `sys_metadata` row. It REPLACES the base layer + * at resolution time (D9.2) and owns nothing: it claims no namespace, + * decides no package membership, holds no table. That is why the + * single-owner assertion needs no "unless it is an overlay" clause — which + * matters beyond tidiness, since ADR-0028 D5/D6 rest on that sentence being + * unconditional. `ownership: 'overlay'` is LOADER-SET at the hydration seams + * and reachable from no authoring surface. + * - `extend` — additive contributions from other packages, folded on top of + * whichever layer is the base. + */ +export interface ObjectContributor { + // Optional (#12623): `registerObject`'s own `packageId` parameter is now + // optional, and this is the exact value it was called with — a bare + // `registerObject(schema)` call stores `undefined` here, matching the + // runtime behavior every call site without a tsc test-layer gate already + // exercised (JS does not enforce TS arity). + packageId: string | undefined; + namespace: string; + ownership: ObjectOwnership; + priority: number; + definition: ServiceObject; +} + +/** + * Compute canonical registry key for an object. + * + * Under the current naming convention, object names are canonical identifiers + * and are used as-is (no namespace__ prefix). The namespace parameter is + * retained for backward compatibility but no longer affects the returned key. + * + * @param namespace - The package namespace (unused, kept for API compatibility) + * @param shortName - The object's name (already the canonical identifier) + * @returns The object name unchanged + * + * @example + * computeFQN('crm', 'account') // => 'account' + * computeFQN(undefined, 'task') // => 'task' + */ +export function computeFQN(_namespace: string | undefined, shortName: string): string { + return shortName; +} + +/** + * Parse FQN back to namespace and short name. + * + * @param fqn - Object name (e.g., "account" or legacy "crm__account" for backward compat) + * @returns { namespace, shortName } - namespace is undefined for unprefixed names + */ +export function parseFQN(fqn: string): { namespace: string | undefined; shortName: string } { + const idx = fqn.indexOf('__'); + if (idx === -1) { + return { namespace: undefined, shortName: fqn }; + } + return { + namespace: fqn.slice(0, idx), + shortName: fqn.slice(idx + 2), + }; +} + +/** + * The three SCALAR props {@link mergeObjectDefinitions} resolves last-writer-wins + * — the exact set the #8284 ruling and ADR-0029 D9.2a both cover, and the same three + * {@link scalarOverridesPackagedBase} answers for. + */ +const OBJECT_FOLD_SCALAR_KEYS = ['label', 'pluralLabel', 'description'] as const; + +type ObjectFoldScalarKey = (typeof OBJECT_FOLD_SCALAR_KEYS)[number]; + +/** + * [ADR-0146 D10, second rule] An object extension's per-field display patches: + * the slot `ObjectExtensionSchema.fieldPatches` declares, carried onto the + * extension def by the manifest loop (`engine.ts`) and applied by + * {@link mergeObjectDefinitions}. ⛔ No second shape: the type IS the spec's, + * and the keys applied are read from `BASE_FIELD_DISPLAY_WHITELIST`, so a + * widened whitelist widens what the merge applies too. + */ +type ObjectFieldPatches = NonNullable; + +/** An `extend` contributor's body: a partial object that may carry {@link ObjectFieldPatches}. */ +type ObjectExtensionBody = Partial & { fieldPatches?: ObjectFieldPatches }; + +/** [#28835] One extension's `fields` as the fold merged them, with the package that declared them. */ +type FoldedExtensionFields = { packageId: string | undefined; fields: unknown }; + +const isPlainRecord = (v: unknown): v is Record => + v !== null && typeof v === 'object' && !Array.isArray(v); + +/** + * The address of one patched display key in a fold's D9.2a yield set + * ({@link SchemaRegistry.tenantPatchedDisplayKeys}): a field key, or one key of + * the option addressed by its stored value. + */ +function fieldPatchYieldKey(field: string, key: string, option?: string): string { + return option === undefined ? `${field}\u0000${key}` : `${field}\u0000options\u0000${option}\u0000${key}`; +} + +/** A picklist item as the registry stores it: the two keys the merge reads. */ +type PicklistItemLike = { options?: PicklistOptionLike[]; _packageId?: string }; + +/** + * [ADR-0150 D3, child E6] Who wrote one field's picklist reference: every + * package whose own layer declares it, in fold order, or `[null]` — one caller + * with no package — when no packaged layer does. + */ +type PicklistReferrer = (field: string | undefined, picklist: string) => readonly (string | null)[]; + +/** Every reference is the environment's: a body no packaged layer declares. */ +const NO_PACKAGE_REFERRER: PicklistReferrer = () => [null]; + +/** Are two package-scoped holders the same one? */ +function sameHolder(a: PackageScopedHolder, b: PackageScopedHolder): boolean { + if (a.kind === 'package') return b.kind === 'package' && a.packageId === b.packageId; + return a.kind === b.kind; +} + +/** The `picklist` a body's field declares (name-keyed or array-spelled), when it declares one. */ +function declaredPicklistOf(body: unknown, field: string): string | undefined { + const fields = isPlainRecord(body) ? body.fields : undefined; + let declared: unknown; + if (Array.isArray(fields)) { + declared = fields.find((f) => isPlainRecord(f) && f.name === field); + } else if (isPlainRecord(fields) && Object.prototype.hasOwnProperty.call(fields, field)) { + declared = fields[field]; + } + const picklist = isPlainRecord(declared) ? declared.picklist : undefined; + return typeof picklist === 'string' && picklist !== '' ? picklist : undefined; +} + +/** + * [ADR-0150 D3, child E6] The referrers of each picklist reference a served + * body carries, read from the contributors that fold into it: EVERY packaged + * layer (the owner, then each extension) whose own declaration names that same + * list on that field, each package once. Several layers can write one + * reference; none of them is picked over the others (epic seat review on + * #31731): the field is served a list only when every one of them reaches the + * same holder ({@link SchemaRegistry.resolvePicklistFields}). A reference no + * packaged layer writes came from an environment-authored body — a tenant + * overlay, a stored row, an object created in this environment — and resolves + * as the environment's (`null`), never from the package that body customizes + * (D3). + */ +function picklistReferrerOf(contributors: readonly ObjectContributor[] | undefined): PicklistReferrer { + if (!contributors || contributors.length === 0) return NO_PACKAGE_REFERRER; + const packaged = [ + ...contributors.filter((c) => c.ownership === 'own'), + ...contributors.filter((c) => c.ownership === 'extend'), + ].filter((c) => !isTenantAuthored(c.definition)); + return (field, picklist) => { + if (field === undefined) return [null]; + const referrers = new Set(); + for (const c of packaged) { + if (declaredPicklistOf(c.definition, field) === picklist) referrers.add(c.packageId ?? null); + } + return referrers.size > 0 ? [...referrers] : [null]; + }; +} + +/** A field of an object body's name-keyed `fields`, when it is a record. */ +function recordFieldOf(body: unknown, name: string): Record | undefined { + const fields = isPlainRecord(body) ? body.fields : undefined; + if (!isPlainRecord(fields) || !Object.prototype.hasOwnProperty.call(fields, name)) return undefined; + const field = fields[name]; + return isPlainRecord(field) ? field : undefined; +} + +/** + * [ADR-0146 D10, second rule] The fields a field patch reaches: the names the + * `own` contributor's declaration carries in the name-keyed `fields` the merge + * reads — "the target object's own fields", as `ObjectExtensionSchema.fieldPatches` + * publishes it. A field an extension adds is never one of them, whatever the + * priorities, and neither is an array-spelled entry: the merge, the patch and + * the miss report all read the record spelling, so they cannot disagree. + * The declaration is the one the registry stores, so the platform columns + * {@link applySystemFields} injects at registration (`created_at` and the + * rest) are among these names: the fold and the install door + * (`getObjectOwner`) admit a patch of one, while `os validate`, which judges + * the authored declaration, refuses it. + * `undefined` when no owner is registered — no opinion, as + * {@link SchemaRegistry.tenantPatchedDisplayKeys} has none there either. + */ +function ownFieldNamesOf(owner: unknown): ReadonlySet | undefined { + if (owner === undefined) return undefined; + const fields = isPlainRecord(owner) ? owner.fields : undefined; + return new Set(isPlainRecord(fields) ? Object.keys(fields).filter((name) => isPlainRecord(fields[name])) : []); +} + +/** + * [ADR-0146 D10, second rule; #28809] What a field patch is judged against in + * the fold: the owner's declaration (`owner`) with only the fields of + * `ownFields` the fold carries at that point in the record spelling + * (`carried`) — which fields a patch reaches is the owner's own, unchanged — + * each field AS THE OWNER DECLARES IT, never as an extension's `fields` + * redeclared it. The install door (`getObjectOwner(name).definition`) and + * `os validate` (the declared object) cannot see another package's extension, + * so the owner's own field and options are the one answer all three readers + * share: an option an extension adds by taking the field over through `fields` + * is not one a patch can name. + */ +function ownFieldsTarget( + owner: unknown, + ownFields: ReadonlySet, + carried: (name: string) => boolean, +): Record { + const fields: Record = {}; + for (const name of ownFields) { + const field = recordFieldOf(owner, name); + if (field && carried(name)) fields[name] = field; + } + return { ...(isPlainRecord(owner) ? owner : {}), fields }; +} + +/** The option of a field's own `options` whose stored `value` is `value`. */ +function optionByValue(field: Record, value: string): Record | undefined { + const options = field.options; + if (!Array.isArray(options)) return undefined; + const found = options.find((o) => isPlainRecord(o) && o.value === value); + return isPlainRecord(found) ? found : undefined; +} + +/** + * [ADR-0029 D9.2a] The scalar yield's comparison, asked of one display key of a + * field or of one of its options. It IS {@link scalarOverridesPackagedBase} — + * the one comparison behind every D9.2a yield, keyed by any attribute (its own + * docblock: the exported key union names the keys its callers ask about, and + * the implementation underneath reads any) — ⛔ never a second copy of it. + */ +function displayKeyOverridesPackagedBase(packaged: unknown, key: string, value: unknown): boolean { + return scalarOverridesPackagedBase(packaged, key as Parameters[1], value); +} + +/** + * [ADR-0146 D10, second rule] One base field with an extension's patch applied: + * the whitelisted field keys, then each option the patch addresses by its + * stored `value`. A key the tenant authored on the fold's base yields + * (`tenantPatched`, ADR-0029 D9.2a). Returns `field` BY REFERENCE when nothing + * applies. + * + * A field bound to a picklist is left alone under `options`: the served options + * are the picklist's, written over the field after the fold + * ({@link SchemaRegistry.finishServedObject}), so an option label written here + * would never be served. The target check names that patch instead. + */ +function patchedField( + name: string, + field: Record, + patch: ObjectFieldPatches[string], + tenantPatched: ReadonlySet | undefined, +): Record { + let next: Record | undefined; + const patchKeys = patch as Record; + for (const key of BASE_FIELD_DISPLAY_WHITELIST.field) { + const value = patchKeys[key]; + if (value === undefined || tenantPatched?.has(fieldPatchYieldKey(name, key))) continue; + (next ??= { ...field })[key] = value; + } + const optionPatches = patch.options; + const options = field.options; + const picklistBound = typeof field.picklist === 'string' && field.picklist !== ''; + if (optionPatches && Array.isArray(options) && !picklistBound) { + let patchedOptions: unknown[] | undefined; + options.forEach((option, index) => { + if (!isPlainRecord(option) || typeof option.value !== 'string') return; + if (!Object.prototype.hasOwnProperty.call(optionPatches, option.value)) return; + const optionPatch = optionPatches[option.value] as Record | undefined; + if (!optionPatch) return; + let patchedOption: Record | undefined; + for (const key of BASE_FIELD_DISPLAY_WHITELIST.option) { + const value = optionPatch[key]; + if (value === undefined || tenantPatched?.has(fieldPatchYieldKey(name, key, option.value))) continue; + (patchedOption ??= { ...option })[key] = value; + } + if (patchedOption) (patchedOptions ??= [...options])[index] = patchedOption; + }); + if (patchedOptions) (next ??= { ...field }).options = patchedOptions; + } + return next ?? field; +} + +/** + * [ADR-0146 D10, second rule] `fields` with every patch of `patches` applied + * that names one of the owner's own fields (`ownFields`, {@link ownFieldNamesOf}) + * present in `fields`. A patch naming any other field — one the object lacks, + * or one only an extension adds — is skipped here, and named by the fold + * ({@link SchemaRegistry.reportFieldPatchMisses}). Nothing applies when no + * owner is registered. Returns `fields` BY REFERENCE when nothing applies. + * + * [#28809] The fold hands this only the patches the judge admitted against the + * owner's declaration ({@link admittedFieldPatches}). An admitted option patch + * whose option an earlier layer left out (an extension's `fields` + * redeclaration, a tenant overlay base) finds no option here, so nothing of it + * applies; the fold names it after the merge, by the same judge asked of the + * merged fields. + */ +function applyFieldPatches( + fields: ServiceObject['fields'], + patches: ObjectFieldPatches, + tenantPatched: ReadonlySet | undefined, + ownFields: ReadonlySet | undefined, +): ServiceObject['fields'] { + if (!isPlainRecord(fields) || !ownFields) return fields; + let out: Record | undefined; + for (const [name, patch] of Object.entries(patches)) { + if (!patch || !ownFields.has(name)) continue; + const field = recordFieldOf({ fields: out ?? fields }, name); + if (!field) continue; + const next = patchedField(name, field, patch, tenantPatched); + if (next !== field) (out ??= { ...fields })[name] = next; + } + return (out ?? fields) as ServiceObject['fields']; +} + +/** + * [ADR-0146 D10, second rule; #28809] `patches` without what the judge missed + * against the owner's declaration: a missed field's whole patch, a missed + * option's entry under `options`. The fold applies exactly what the install + * door and `os validate` admit, and names exactly what they refuse. Returns + * `patches` BY REFERENCE when nothing was missed. + */ +function admittedFieldPatches( + patches: ObjectFieldPatches, + misses: readonly FieldPatchTargetMiss[], +): ObjectFieldPatches { + if (misses.length === 0) return patches; + const out: Record = { ...patches }; + for (const miss of misses) { + const patch = out[miss.field]; + if (!patch) continue; + if (miss.option === undefined) { + delete out[miss.field]; + continue; + } + if (!patch.options) continue; + const { [miss.option]: _missed, ...options } = patch.options; + out[miss.field] = { ...patch, options }; + } + return out as ObjectFieldPatches; +} + +/** + * [#28356] Which layer an object fold serves: `'served'`, what every read + * answers (the bound actions the registry serves, runtime-authored included), + * or `'code'`, the code-layer resolution {@link SchemaRegistry.getArtifactItem} + * answers (the code packages' bound actions alone). + */ +type ServedObjectLayer = 'served' | 'code'; + +/** + * [#29903, #29872] The options that leave `extend` contributions out of an + * object fold ({@link SchemaRegistry.getAllObjects}, `listItems`, + * `foldObjectExtendersOnto`): + * + * - `withoutExtensionsOf` — that package's contributions are left out; + * - `onlyExtensionsOf` — every OTHER package's contributions are left out, + * so only that package's own extensions fold. The protocol's per-package + * layer passes it, naming the listed package. + * + * Both may be given; a contribution either one leaves out is left out. The + * base layer (`own` / `overlay`) is never left out. + * + * [#30756] `asEnabled` asks the opposite question of a DISABLED package: the + * fold as it will serve once that package is enabled, its `extend` + * contributions folded in instead of withheld ({@link SchemaRegistry.withholdDisabledExtensions}). + * Every other disabled package's stay withheld. The enable door's pre-check + * reads it, as the disable door's reads `withoutExtensionsOf`; for a package + * that is not disabled it changes nothing. + */ +export interface ObjectExtensionFoldOptions { + withoutExtensionsOf?: string; + onlyExtensionsOf?: string; + asEnabled?: string; +} + +/** Whether `options` leaves any contribution out (otherwise the fold is the ordinary one). */ +function leavesExtensionsOut(options: ObjectExtensionFoldOptions | undefined): options is ObjectExtensionFoldOptions { + return options?.withoutExtensionsOf !== undefined || options?.onlyExtensionsOf !== undefined; +} + +/** [#29903, #29872] Whether `options` leaves `contributor` out of the fold. */ +function isLeftOutExtension(contributor: ObjectContributor, options: ObjectExtensionFoldOptions): boolean { + if (contributor.ownership !== 'extend') return false; + if (options.withoutExtensionsOf !== undefined && contributor.packageId === options.withoutExtensionsOf) return true; + return options.onlyExtensionsOf !== undefined && contributor.packageId !== options.onlyExtensionsOf; +} + +/** + * The `extend` contributions a disable withholds from a served fold, and the + * member names only they add ({@link SchemaRegistry.withholdDisabledExtensions}). + */ +interface WithheldExtensions { + kept: ObjectContributor[]; + withheldFields: ReadonlySet; + withheldRules: ReadonlySet; + /** Each withheld index by its structural key ({@link indexKeysOf}). */ + withheldIndexes: ReadonlySet; +} + +/** The field names a body declares in the record spelling the merge reads. */ +function fieldNamesOf(body: unknown): string[] { + const fields = isPlainRecord(body) ? body.fields : undefined; + return isPlainRecord(fields) ? Object.keys(fields) : []; +} + +/** The validation rule names a body declares. */ +function ruleNamesOf(body: unknown): string[] { + const rules = isPlainRecord(body) ? body.validations : undefined; + if (!Array.isArray(rules)) return []; + return rules + .map((rule) => (isPlainRecord(rule) ? rule.name : undefined)) + .filter((name): name is string => typeof name === 'string' && name !== ''); +} + +/** The indexes a body declares, each by its structural key (the key the fold's index subtraction compares by). */ +function indexKeysOf(body: unknown): string[] { + const indexes = isPlainRecord(body) ? body.indexes : undefined; + return Array.isArray(indexes) ? indexes.map((index) => stableStringify(index)) : []; +} + +/** + * `base` without the fields, rules and indexes a withheld extension alone adds. By + * reference when it carries none of them, which is the ordinary case (a base + * that was never folded). + */ +function withoutWithheldMembers(base: ServiceObject, withheld: WithheldExtensions): ServiceObject { + const fields = isPlainRecord((base as { fields?: unknown }).fields) ? (base as { fields: Record }).fields : undefined; + const rules = (base as { validations?: unknown }).validations; + const dropFields = fields !== undefined && Object.keys(fields).some((name) => withheld.withheldFields.has(name)); + const dropRules = Array.isArray(rules) + && rules.some((rule) => isPlainRecord(rule) && typeof rule.name === 'string' && withheld.withheldRules.has(rule.name)); + const indexes = (base as { indexes?: unknown }).indexes; + const dropIndexes = Array.isArray(indexes) && indexes.some((index) => withheld.withheldIndexes.has(stableStringify(index))); + if (!dropFields && !dropRules && !dropIndexes) return base; + const out = { ...base } as ServiceObject & Record; + if (dropFields) { + out.fields = Object.fromEntries(Object.entries(fields!).filter(([name]) => !withheld.withheldFields.has(name))) as ServiceObject['fields']; + } + if (dropRules) { + out.validations = (rules as unknown[]).filter( + (rule) => !(isPlainRecord(rule) && typeof rule.name === 'string' && withheld.withheldRules.has(rule.name)), + ) as ServiceObject['validations']; + } + if (dropIndexes) { + out.indexes = (indexes as unknown[]).filter((index) => !withheld.withheldIndexes.has(stableStringify(index))) as ServiceObject['indexes']; + } + return out; +} + +/** + * Deep merge two ServiceObject definitions. + * + * The merge set is CLOSED and enumerable — this is NOT "every prop, later + * value wins": + * - `fields` and `indexes` are merged ADDITIVELY (`fields` keyed by name). + * - `validations` are merged BY NAME ({@link mergeValidationsByName}, + * [ADR-0029 D9.2a, 2026-10-30 note]): rules of different names add, and a + * rule of a name already folded replaces it — with `tenantRules`, carrying + * the tenant's `message` and `active`, never the tenant's condition. + * - The three {@link OBJECT_FOLD_SCALAR_KEYS} (`label`, `pluralLabel`, + * `description`) are overridden last-writer-wins, subject to the + * `tenantAuthored` yield rule below. [#30391] A `label` that renames the + * object without a `pluralLabel` beside it also takes the running + * `pluralLabel` off: the plural named the old label. + * - `fieldPatches` [ADR-0146 D10, second rule] is APPLIED, never carried: + * after the merges above, each patch writes its `BASE_FIELD_DISPLAY_WHITELIST` + * keys onto the named field and its option labels onto the options whose + * stored `value` it names, each key yielding where `tenantPatched` says the + * tenant authored it — the same D9.2a rule as the scalars. A patch reaches + * the owner's own fields only (`ownFields`): one naming any other field, + * a field an extension adds included, is skipped here; the fold names it. + * + * **Every other top-level prop on `extension` is silently discarded.** + * `merged` starts as `{ ...base }` and nothing outside the list above is ever + * copied onto it — a caller that hands this function an extension body + * carrying an undeclared key, e.g. `tenancy: { enabled: false }`, gets a + * valid-but-inert no-op HERE: no error, no warning from this function, the + * base's existing value simply wins as if the extension had never named the + * key (issue #12680). + * + * Declarative authors never reach that silence, though: `ObjectExtensionSchema` + * (`packages/spec/src/data/object.zod.ts`) is `.strict()` — its shape is + * exactly `extend` / `fields` / `fieldPatches` / `label` / `pluralLabel` / + * `description` / `validations` / `indexes` / `priority`, so an `objectExtensions` entry + * naming `tenancy`, `permissions`, or any other key outside that list fails + * LOUDLY at authoring time with a prescription (#4001; the same rule is + * documented at `content/docs/data-modeling/object-extensions.mdx`). This + * function's discard is reachable only by a caller that bypasses that + * schema — a direct, programmatic `registry.registerObject(def, pkg, + * undefined, 'extend', …)` call, which is how this package's own pin + * (`registry-object-extension-nonenumerated-prop-discard.test.ts`) exercises + * the rule. Extender-writable `tenancy`/`permissions` would in any case be a + * separate, much bigger decision that has NOT been made. + * + * Unlike `tenancy`/`permissions`, this one is not blocked by schema: + * `_provenance` is a real top-level prop an `extend` contributor carries + * TODAY. Copying it through (as "later value + * wins" would) lets a third-party extender flip a tenant-authored object's + * `_provenance` to the extending package's — reaching, by a different route, + * the exact outcome ADR-0029 D9.3's priority-reranking guard exists to + * prevent. `registry-object-overlay-layer.test.ts` ("an extender declaring + * priority 140 does not become the base layer") fails if this merge set is + * widened to copy it through. + * + * [ADR-0029 D9.2a] …the SCALAR override above is conditional. `tenantAuthored` names + * the scalars the fold's BASE has authored away from the packaged owner's + * value; an extender yields on those. See + * {@link SchemaRegistry.tenantAuthoredScalars} for why the set is computed once + * over the base rather than re-derived from the running `merged`; `tenantRules` + * ({@link SchemaRegistry.tenantRuleSettings}) is computed once for the same reason. + */ +function mergeObjectDefinitions( + base: ServiceObject, + extension: ObjectExtensionBody, + tenantAuthored?: ReadonlySet, + tenantRules?: ReadonlyMap, + tenantPatched?: ReadonlySet, + ownFields?: ReadonlySet, +): ServiceObject { + const merged = { ...base }; + + // Merge fields additively + if (extension.fields) { + merged.fields = { ...base.fields, ...extension.fields }; + } + + // [ADR-0029 D9.2a, 2026-10-30 note] Merge validations by name + if (extension.validations) { + merged.validations = mergeValidationsByName( + base.validations, + extension.validations, + tenantRules, + ) as ServiceObject['validations']; + } + + // Merge indexes additively + if (extension.indexes) { + merged.indexes = [...(base.indexes || []), ...extension.indexes]; + } + + // Override scalar props (last writer wins) — [ADR-0029 D9.2a] unless the base has been + // authored by the tenant, in which case the extender's packaged default yields. + const yields = (key: ObjectFoldScalarKey): boolean => tenantAuthored?.has(key) === true; + if (extension.label !== undefined && !yields('label')) { + // [#30391] `pluralLabel` is the plural OF `label`: an extension that renames + // the object and declares no plural leaves the running plural naming the + // old label, so the object is served as one that declares no + // `pluralLabel`. A plural the tenant authored stays (the D9.2a yield), and + // an extension that repeats the label it extends renames nothing. + if (extension.pluralLabel === undefined && extension.label !== merged.label && !yields('pluralLabel')) { + delete merged.pluralLabel; + } + merged.label = extension.label; + } + if (extension.pluralLabel !== undefined && !yields('pluralLabel')) { + merged.pluralLabel = extension.pluralLabel; + } + if (extension.description !== undefined && !yields('description')) { + merged.description = extension.description; + } + + // [ADR-0146 D10, second rule] The extension's display patches of the + // owner's own fields (`ownFields`) — never of a field an extension adds, + // whatever the priorities, or the answer would depend on which extender the + // fold reached first — applied AFTER the additive merges, with each key + // yielding where the tenant authored it (`tenantPatched`, ADR-0029 D9.2a), + // as the scalars above yield. Two extenders patching one key resolve as the + // scalars do: the fold applies contributors in ascending `priority`, so the + // higher one is written last. + if (extension.fieldPatches) { + merged.fields = applyFieldPatches(merged.fields, extension.fieldPatches, tenantPatched, ownFields); + } + + return merged; +} + +/** + * [#8027] Key-order-insensitive JSON, for comparing two `indexes` entries, or + * two validation rules' keys, BY VALUE — see + * {@link SchemaRegistry.subtractExtenderContributions} and + * {@link changedPackagedRuleKeys}. + * + * Plain `JSON.stringify` would report the same rule as two different rules + * whenever the two copies were built by different code paths (a stored overlay + * row that went through `JSON.parse` vs the contributor's in-memory literal), + * which is exactly the pair being compared, so key order cannot be assumed. + * Arrays keep their order — an index's `fields` list is ordered and two + * spellings of it are genuinely two different indexes. + */ +function stableStringify(value: unknown): string { + if (value === null || typeof value !== 'object') return JSON.stringify(value) ?? 'null'; + if (Array.isArray(value)) return `[${value.map(stableStringify).join(',')}]`; + const entries = Object.entries(value as Record) + .filter(([, v]) => v !== undefined) + .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)); + return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${stableStringify(v)}`).join(',')}}`; +} + +/** + * [ADR-0029 D9.2a, 2026-10-30 note] What a tenant's same-named copy of a + * packaged validation rule may set: its sentence and its switch. Every other key + * a rule carries — `type`, `condition` and each variant's own predicate keys, + * `events`, `severity`, `priority`, and the editor keys — is the package's. + */ +type TenantRuleSettings = { message?: string; active?: boolean }; + +/** A rule's `name` when it has one; a rule without a string name has no identity to merge by and is always additive. */ +function validationRuleName(rule: unknown): string | undefined { + const name = (rule as { name?: unknown } | null | undefined)?.name; + return typeof name === 'string' && name !== '' ? name : undefined; +} + +/** + * The tenant settings a base layer's rules carry, keyed by rule name — the + * first copy of a name wins, so a stored row that predates the by-name fold + * (the tenant's reworded copy, then the extension's original appended after + * it) still answers with the tenant's sentence. + */ +function tenantRuleSettingsOf(rules: unknown): Map { + const out = new Map(); + if (!Array.isArray(rules)) return out; + for (const rule of rules) { + const name = validationRuleName(rule); + if (name === undefined || out.has(name)) continue; + const { message, active } = rule as { message?: unknown; active?: unknown }; + out.set(name, { + ...(typeof message === 'string' ? { message } : {}), + ...(typeof active === 'boolean' ? { active } : {}), + }); + } + return out; +} + +/** The packaged rule, carrying the tenant's sentence and switch where the tenant's copy sets them. */ +function withTenantRuleSettings(packaged: R, settings: TenantRuleSettings | undefined): R { + if (!settings || (settings.message === undefined && settings.active === undefined)) return packaged; + return { ...packaged, ...settings }; +} + +/** + * [ADR-0029 D9.2a, 2026-10-30 note] Merge `validations` BY NAME, the way + * `fields` merge by key: a later layer's rule replaces every earlier rule of its + * name and is appended in fold order, and a rule of a new name is added. With + * `tenantRules` (the fold's base is a tenant overlay), the rule carries the + * tenant's `message` and `active` for its name; its condition stays the + * later layer's. + */ +function mergeValidationsByName( + base: readonly unknown[] | undefined, + extension: readonly unknown[], + tenantRules: ReadonlyMap | undefined, +): unknown[] { + const names = new Set(); + for (const rule of extension) { + const name = validationRuleName(rule); + if (name !== undefined) names.add(name); + } + const kept = (base ?? []).filter((rule) => { + const name = validationRuleName(rule); + return name === undefined || !names.has(name); + }); + const added = extension.map((rule) => { + const name = validationRuleName(rule); + return name === undefined ? rule : withTenantRuleSettings(rule, tenantRules?.get(name)); + }); + return [...kept, ...added]; +} + +/** The value a rule's key has when the rule leaves it out — `BASE_VALIDATION_SHAPE`'s defaults. */ +const VALIDATION_RULE_DEFAULTS: Readonly> = { + events: ['insert', 'update'], + priority: 100, + severity: 'error', +}; + +/** + * The keys on which a tenant's copy of a packaged rule differs from the + * package's, beyond the two it may set (`message`, `active`) and the loader's + * `_`-prefixed stamps. A key a rule leaves out is read at its schema default, + * so a stored copy whose parse stamped the defaults is not a change. Empty when + * the copy changes nothing but its sentence and its switch. + */ +function changedPackagedRuleKeys(copy: unknown, packaged: unknown): string[] { + const enforced = (rule: unknown): Record => { + const out: Record = { ...VALIDATION_RULE_DEFAULTS }; + for (const [key, value] of Object.entries((rule ?? {}) as Record)) { + if (key === 'message' || key === 'active' || key.startsWith('_') || value === undefined) continue; + out[key] = value; + } + return out; + }; + const a = enforced(copy); + const b = enforced(packaged); + return [...new Set([...Object.keys(a), ...Object.keys(b)])] + .filter((key) => stableStringify(a[key]) !== stableStringify(b[key])) + .sort((x, y) => (x === 'type' ? -1 : y === 'type' ? 1 : x < y ? -1 : x > y ? 1 : 0)); +} + +/** + * Global Schema Registry + * Unified storage for all metadata types (Objects, Apps, Flows, Layouts, etc.) + * + * ## Namespace & Ownership Model + * + * Objects use a namespace-based FQN system: + * - `namespace`: Short identifier from package manifest (e.g., "crm", "todo") + * - `name`: canonical object name (e.g., "account", "sys_user") + * - Reserved namespaces (`base`, `system`) don't get prefixed + * + * Ownership modes: + * - `own`: One package owns the object (creates the table, defines base schema) + * - `extend`: Multiple packages can extend an object (add fields, merge by priority) + * + * ## Package vs App Distinction + * - **Package**: The unit of installation, stored under type 'package'. + * Each InstalledPackage wraps a ManifestSchema with lifecycle state. + * - **App**: A UI navigation shell (AppSchema), registered under type 'apps'. + * Apps are extracted from packages during registration. + * - A package may contain 0, 1, or many apps. + */ +export type RegistryLogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; + +/** All valid {@link RegistryLogLevel} values — used to validate `OS_REGISTRY_LOG`. */ +export const REGISTRY_LOG_LEVELS: readonly RegistryLogLevel[] = [ + 'debug', 'info', 'warn', 'error', 'silent', +]; + +/** + * Construction options for {@link SchemaRegistry}. + */ +export interface SchemaRegistryOptions { + /** + * Whether the host kernel runs in multi-tenant mode. The `organization_id` + * column itself is auto-injected regardless of this flag (lookup → + * sys_organization, on every registered object that doesn't already declare + * it, isn't `managedBy` an external subsystem, and hasn't opted out via + * `systemFields`/`tenancy.enabled:false`). When `true` the injected column + * is additionally INDEXED — single-tenant stacks skip the index since + * nothing ever filters by organization. + * + * Sourced from the `OS_MULTI_ORG_ENABLED` env var when not explicitly set — + * matches how the SecurityPlugin and CLI startup banner pick the mode. + * Default is `false` (single-tenant) so local `dev`/`start` runs seed + * demo data inline at boot; set `OS_MULTI_ORG_ENABLED=true` for cloud / + * production multi-org deployments. Pass an explicit boolean to override + * (useful in tests). + */ + multiTenant?: boolean; + + /** + * Whether to provision the hidden `__search` search-normalization companion + * column on registered objects that have an eligible display/name field + * (#2486 — pinyin search recall). Sourced from `OS_SEARCH_PINYIN_ENABLED` + * via `resolveSearchPinyinEnabled()` when not explicitly set (the CLI boot + * path stamps the locale-derived decision into that env var before the + * kernel constructs engines). Default off — non-Chinese deployments carry + * no extra column. Pass an explicit boolean to override (useful in tests). + */ + searchCompanion?: boolean; + + /** + * Policy for the install-time namespace gate (ADR-0048 Phase 1) — installing + * a package whose `manifest.namespace` is already owned by a *different* + * installed package. + * + * - `'error'` (default): throw {@link NamespaceConflictError} at install time, + * naming both packages. Makes the namespace land-grab a loud, early failure + * instead of a mid-install `CREATE TABLE` blow-up. + * - `'warn'`: log a warning and let the install proceed. For deliberate + * migrations where the conflict is temporarily expected. + * + * Sourced from `OS_METADATA_COLLISION` (`warn` to downgrade) when not set + * explicitly. Same-package reinstall and shareable platform namespaces + * (`base`/`system`/`sys`) are never treated as conflicts. (The per-item + * cross-package collision throw was retired in ADR-0048 §3.4 — distinct + * package ids are always disambiguable by package-scoped resolution — except + * for positions, permission sets and capabilities, whose second-holder + * refusal ({@link SecurityCatalogNameConflictError}) this policy does NOT + * downgrade: no ruling extends `warn` to it.) + */ + collisionPolicy?: 'error' | 'warn'; + + /** + * Verbosity of the registry's own `[Registry] …` log lines. Sourced from the + * `OS_REGISTRY_LOG` env var when not set explicitly (default `'info'`). + * + * The point of the env seam is discoverability (#3420): expected-but-noisy + * housekeeping — re-registering an owned object / overwriting a package + * manifest on a rebuild/HMR/seed-replay — is emitted at `'debug'`, so it stays + * out of a stock `info` boot log but a developer chasing a registration issue + * can surface it with `OS_REGISTRY_LOG=debug`. An unrecognized value falls + * back to `'info'`. + */ + logLevel?: RegistryLogLevel; +} + +/** + * [ADR-0117 D1] The injected BU-ownership column name, spelled out so it greps + * from this file, and annotated with the spec's registered protocol name so the + * two cannot drift: a rename in `SystemFieldName.OWNING_BUSINESS_UNIT_ID` makes + * this line a compile error rather than a silently orphaned injection. + */ +const OWNING_BUSINESS_UNIT_FIELD: typeof SystemFieldName.OWNING_BUSINESS_UNIT_ID = + 'owning_business_unit_id'; + +/** [ADR-0142 D3] The injected ownership-queue column, annotated the same way. */ +const QUEUE_FIELD: typeof SystemFieldName.QUEUE = 'queue'; + +export function applySystemFields( + schema: ServiceObject, + opts: { + multiTenant: boolean; + /** + * [ADR-0131 D7] The objects THIS deployment declares platform-global — the + * plan's one deployment input. A declared object gets no `organization_id` + * here. Omitted or empty ⇒ the plan is byte-identical to the authored one. + */ + platformGlobalObjects?: ReadonlySet; + } +): ServiceObject { + // WHICH columns this object carries is the spec's derivation + // (`resolveInjectedSystemColumns`, #5378) — one answer shared with every + // author-time consumer that must resolve a reference to an injected column + // but cannot load this runtime (`@objectstack/lint`). This function keeps sole + // ownership of WHAT each column looks like, the same split #3786 established + // for the audit family. Do NOT re-derive a condition below: a second copy here + // is exactly the drift the plan exists to prevent. + // + // [#7865] This pass runs for `external` (ADR-0015) objects too — DELIBERATELY + // (maintainer ruling 2026-08-12, direction B). The platform provisions no + // storage for a federated object (`syncObjectSchema` returns early, no DDL), + // so the anchors injected below are registered-but-unprovisioned there; the + // machine-readable marker for that fact is `resolveInjectedColumnProvenance` + // / `unprovisionedInjectedColumns` (re-exported above from + // `@objectstack/metadata-core`), NOT a key on the definitions this function + // spreads — the definitions must stay byte-identical to the shipped tables, + // because the #7859 Layer-0 guard and the #4326 round-trip strip both read + // them by exact identity. Do not add keys here; consumers ask the API. + // + // [ADR-0131 D7] …and the deployment's platform-global declaration is the + // plan's one deployment input: a declared object is planned with no tenant + // column, so nothing below injects `organization_id` for it. Passed only when + // a deployment declared something, so every other call is the one-argument + // plan, byte for byte. + const plan = + opts.platformGlobalObjects && opts.platformGlobalObjects.size > 0 + ? resolveInjectedSystemColumns(schema, { platformGlobalObjects: opts.platformGlobalObjects }) + : resolveInjectedSystemColumns(schema); + + // 1. Hard opt-out at object level (e.g. seed/migration tables). + // Folded into the plan (`systemFields: false` ⇒ every flag false), so the + // cheap path stays cheap without a second reading of the key. + // + // 2. Skip only `better-auth` managed tables. Their column layout is + // driven by better-auth's own migrations (sys_user, sys_session, + // sys_organization, …) and injecting extra columns here would + // collide with what better-auth expects. Other `managedBy` buckets + // (`platform`, `config`, `system`, `append-only`) all need the + // tenant + audit columns for multi-tenant isolation and time-travel + // history — withholding them silently broke RLS reads on + // sys_audit_log / sys_activity (the SecurityPlugin's + // field-existence safety net dropped `organization_id = + // current_user.organization_id` as "field missing", producing + // RLS_DENY_FILTER → 0 rows for every non-admin caller). + // Both opt-outs are the plan's rows 1-2; the bail below covers them. + // + // Honor explicit opt-out via either `systemFields.tenant === false` + // OR `tenancy.enabled === false`. The latter is the schema-level + // declaration that the table is a shared/global catalog (e.g. + // sys_package — the Marketplace registry). Without this, the + // registry would still inject `organization_id`, and the + // SecurityPlugin's RLS layer would filter every cross-org read down + // to 0 rows even though the schema explicitly disabled multi-tenancy. + // + // The `organization_id` COLUMN is provisioned unconditionally (subject only + // to the explicit opt-outs above) — its existence no longer depends on the + // global multi-tenant flag. Decoupling "does the column exist" from "is + // tenancy enabled" is what stops sudo writers (audit / messaging / inbox / + // outbox …) from failing with "no column named organization_id" on + // single-tenant stacks: they can always stamp the column, and org-scoping + // stamps it on insert (under `single` with the Default Organization), so it + // is never NULL (ADR-0131 D1). The multi-tenant flag now governs only + // whether the column is INDEXED — on a single-tenant DB nothing ever filters + // by organization, so the index would be dead weight. That is also why the + // plan takes no `multiTenant` input: the flag cannot change what EXISTS, only + // what is indexed, so an author-time consumer needs no runtime context. + const wantTenant = plan.tenant; + const wantAudit = plan.audit; + + // Ownership is auto-provisioned by DEFAULT on user-authored business + // objects (correct-by-default for AI authors). It is withheld only where a + // per-record owner is meaningless: any platform-managed table (`managedBy` + // is set — config/append-only/system/platform; `better-auth` already + // returned above), the `sys_*` namespace, or an explicit opt-out via + // `ownership: 'business_unit' | 'org' | 'none'` on the schema (Dataverse-style + // — catalog / junction tables, plus D1's unit-owned tier, which takes the BU + // anchor instead). Note this is the SAFE default direction: forgetting the + // opt-out leaves a harmless spare column, whereas the old opt-IN model let + // authors silently ship objects with no working ownership at all. + // `ownership` is a declared ObjectSchema field (record-ownership model), read + // off the typed schema by the plan — no `as any` (#3175). + // + // [ADR-0117 D1 / #5677] The plan treats the value as a `string`, not the + // enum, on purpose: it is also called on pre-parse input. That widening is + // what let the engine recognise D1's fourth tier `'business_unit'` BEFORE the + // schema could emit it — the ordering #5677 → #5678 exists to guarantee, since + // a tier the schema emits first would be judged by a no-overlap literal test + // and get the INVERSE of what D1 declares. As of #5678 the spec enum reads + // `'user' | 'business_unit' | 'org' | 'none'` and the tier is authorable. + // + // [ADR-0117 D1 / #5677] The ownership decision is a POSITIVE LIST, deliberately + // — it used to read `ownership !== 'org' && ownership !== 'none'`, i.e. a + // DENY-list, so ANY value outside the two exclusions fell through to "inject + // `owner_id`". That default was safe only while the enum had exactly three + // members: D1's `business_unit` tier means "owned by a UNIT, not a person" + // (`owner_id` ❌, `owning_business_unit_id` ✅), and under the deny-list it + // would have been stamped with `owner_id` — the exact inverse. The same + // argument applies to any FIFTH tier a later ADR adds, which is why the list + // stays positive rather than being "completed" to the current enum. + // + // The `sys_*` / `managedBy` ineligibility and the per-tier table + // + // ownership owner_id owning_business_unit_id + // undefined/user ✅ ✅ + // business_unit ❌ ✅ + // org ❌ ❌ + // none ❌ ❌ + // + // all live in `resolveInjectedSystemColumns` now, so the author-time linter + // reaches the same verdict instead of guessing (#5378). + const wantOwner = plan.owner; + const wantOwningBusinessUnit = plan.owningBusinessUnit; + // [ADR-0142 D3] The queue column rides with the owner anchor (the plan's + // `queue` is its `owner`): "sits in Q" is `queue == Q AND owner_id IS NULL`. + const wantQueue = plan.queue; + + // Nothing to INJECT — the two opt-out rows (`systemFields: false`, + // `managedBy: 'better-auth'`) and objects whose every column is already + // declared. + // + // [#8608] It still routes through {@link provisionTenantScopeIndex}, like + // every other exit below. An object that injects nothing can still CARRY the + // tenant column — the author declares their own `organization_id` — and the + // wall composes its predicate on exactly that column (measured, not inferred: + // plugin-security's `tenancyDisabled` reads `tenancy.enabled` and + // `systemFields.tenant` only, so `systemFields: false` leaves the wall live). + // Returning `schema` here was the last exit that decided the index by NOT + // asking, which is how the withholding survived #8375's convergence. + if (!wantTenant && !wantAudit && !wantOwner && !wantOwningBusinessUnit && !wantQueue) { + return provisionTenantScopeIndex(schema, opts); + } + + const additions: Record = {}; + // Platform-owned field settings that must WIN over a declared field, rather + // than lose to it like `additions` does (#4447). + const overrides: Record = {}; + + if (wantTenant && !schema.fields?.organization_id) { + // [#6562] The authorable shape is the shared table's, spread verbatim. + // + // [#6810] Nothing is spread ON TOP of it any more. This line used to read + // `{ ...TENANT_SCOPE_FIELD_DEF, indexed: opts.multiTenant }`, and `indexed` + // is the one key that was never a `FieldSchema` key at all — removed in the + // 16.x line (#2377, ADR-0049) because a field-level index flag built no + // index, and `FieldSchema` is a `strictObject`, so a document carrying it is + // rejected BY NAME ("never a FieldSchema key; a field-level index flag built + // no index"). #6562's reasoning for leaving it here — that its only consumer + // reads the REGISTERED schema, never a served document — held for the + // consumer and not for the document: `registerObject` runs this function + // BEFORE storing and `getItem('object', …)` serves that post-injection + // document, so the key reached `/meta`, where `decorateMetadataItem` + // re-parsed the served body and stamped `_diagnostics: { valid: false, + // errors: [{ path: 'fields.organization_id', code: 'unrecognized_keys' }] }` + // on EVERY registry-backed object — both tenancy modes, both read exits. A + // defect report the platform wrote about its own column, on a document the + // author never wrote and could not fix, in the channel Studio renders + // invalid-metadata banners from. + additions.organization_id = { ...TENANT_SCOPE_FIELD_DEF }; + + // [#6810] The tenant INDEX that goes with this column is no longer decided + // here. It is {@link provisionTenantScopeIndex}, called on the merged result + // at the tail of this function — see #8375 for why a decision spelled out at + // this one site could not be reached by the `/meta` read exits. + } + + if (wantAudit) { + for (const name of AUDIT_PROVENANCE_FIELDS) { + const declared = (schema.fields as Record | undefined)?.[name]; + if (!declared) { + additions[name] = AUDIT_FIELD_DEFS[name]; + continue; + } + // [#4447] The audit family's GOVERNANCE is platform-owned, so a declared + // `created_at` cannot make the audit anchor client-writable. + // + // The injection above is skipped when the object already carries the + // field, and the merge below lets `schema.fields` win — correct for an + // authored business field, wrong for this family. It is how `created_at` + // became writable on an ordinary PATCH: the showcase artifact ships a + // materialized `created_at` carrying only FieldSchema DEFAULTS + // (`readonly: false`), which shadowed `AUDIT_FIELD_DEFS.created_at` + // (`readonly: true`), so the engine's `stripReadonlyFields` had nothing + // to key off and the forged value was written straight through — with no + // `droppedFields` either, because from the platform's point of view + // nothing was dropped. + // + // Its two siblings only LOOKED protected: the audit hook force-advances + // `updated_at`/`updated_by` on every update, so a forged value is + // overwritten rather than refused. `created_at` is insert-only, so + // nothing overwrote it — one field out of the trio genuinely unguarded. + // + // Presentation stays the author's (label, description, hidden, group, + // ordering …); only the keys that decide WHO MAY WRITE IT are forced. + // That leaves the deliberate back-dating path intact: `preserveAudit` + // (#3479/#3493) and `isSystem` writes still reinstate the original + // timeline, because they are checked downstream of `readonly`, not by it. + overrides[name] = { ...declared, ...AUDIT_FIELD_GOVERNANCE[name] }; + } + } + + // Canonical reassignable owner. `system: true` marks it platform-provided + // (so tooling/migrations recognise it), but — unlike the audit `*_by` + // lookups — it is NOT `readonly`: ownership is transferable, so it stays + // editable in forms and assignable via the API. SecurityPlugin auto-stamps + // it to the acting user on insert when left NULL. + if (wantOwner && !schema.fields?.owner_id) { + additions.owner_id = { ...OWNER_FIELD_DEF }; + } + + // [ADR-0117 D1] Record-level business-unit ownership. Shaped after + // `organization_id` (server-stamped scope anchor), NOT after `owner_id` + // (a user-assignable business field) — the two differ in exactly the keys + // that decide who may write and whether it renders: + // + // • `readonly: true` — D3 requires the value to be derived and validated + // server-side (`record.organization_id` must equal the unit's org), so a + // client-supplied value is overwritten, never trusted. Reassignment is a + // TRANSFER-class operation gated on `allowTransfer` (D4), which has not + // landed; until it does, no client write path may set this column. + // • `hidden: true` — nothing stamps the column yet (D2's policy is + // undecided, D8's backfill unwritten), so it is provisioned-but-inert. + // Surfacing a permanently-NULL lookup on every business object's layout + // would advertise a capability the runtime does not deliver (Prime + // Directive #10). Presentation is a one-key flip for the PR that lands + // stamping — a capability grant is not. + // • `required: false` — nullable, like every other injected column. + // + // ⚠️ None of the three presumes ADR-0117 D2 (`pinned`/`follow_owner`/ + // `transferable`), which is NOT YET RULED. They are the fail-closed shape: + // they grant no capability, so every D2 outcome remains reachable. D2's + // `owningBusinessUnit.required` is an INSERT-time rejection rule, not column + // nullability — it cannot be read off this definition either. `system: true` + // writes (the future stamping middleware, seeds) are checked downstream of + // `readonly`, so the flag blocks clients without blocking the platform — + // exactly how `organization_id` is stamped today. + // + // No index: the hierarchical predicate that would use one (`… IN (units)`, + // D6) ships with the enterprise scope resolver. An index on a column nothing + // writes and nothing filters is dead weight — the same reasoning that gates + // `organization_id`'s index on `multiTenant`. + if (wantOwningBusinessUnit && !schema.fields?.[OWNING_BUSINESS_UNIT_FIELD]) { + additions[OWNING_BUSINESS_UNIT_FIELD] = { ...OWNING_BUSINESS_UNIT_FIELD_DEF }; + } + + // [ADR-0142 D3] The ownership-queue column: the queue machine name a record + // with no owner SITS IN, and an owned record's home queue. Text, not a lookup + // (a declared queue has no row to point at); `readonly: false` like + // `owner_id`, because its writers are adjudicated where the owner's are — the + // assignment seam stamps it as the platform, and a client's move is a + // transfer-class write at plugin-security's step 3.5. `hidden: true`: the + // Console's queue affordances read it by name, and a raw machine name on every + // layout is not a reading a person needs. Landed with its writers, never ahead + // of them (ADR-0078). A declared `queue` never reaches this line on an + // eligible object: `registerObject` refuses it ({@link objectQueueColumnRefusal}), + // so the `!schema.fields` guard below only ever admits the platform's own + // definition coming back through a re-registration. + if (wantQueue && !schema.fields?.[QUEUE_FIELD]) { + additions[QUEUE_FIELD] = { ...QUEUE_FIELD_DEF }; + } + + // [#8375] The tenant INDEX is decided over the MERGED result, never over the + // input — so this function and the read-exit seam ask the same question of the + // same document shape. Both cases below route through it, including the one + // that adds no field at all (an object whose every system column is already + // declared still owes the index decision). + if (Object.keys(additions).length === 0 && Object.keys(overrides).length === 0) { + return provisionTenantScopeIndex(schema, opts); + } + + return provisionTenantScopeIndex( + { + ...schema, + // `additions` LOSE to an author's field (a declared `owner_id` is theirs); + // `overrides` WIN over it (the audit family's governance is not authorable). + fields: { ...additions, ...(schema.fields ?? {}), ...overrides }, + }, + opts, + ); +} + +/** + * [#6810, made shareable by #8375] Declare the platform's tenant-scope index on + * an object that carries a platform-provisioned `organization_id`, on a + * multi-tenant deployment — the ONE implementation of that decision. + * + * ## Why it is a function and not four lines inside `applySystemFields` + * + * It was those four lines, and that is precisely how the stamp diverged. The + * `/meta` read exits converge the injected system COLUMNS through + * `applyInjectedSystemColumns` (`@objectstack/metadata-core`, #6562) — which + * cannot reach `applySystemFields`: `@objectstack/objectql` depends on + * `@objectstack/metadata-protocol` depends on `@objectstack/metadata-core`, so + * the import runs UP the dependency graph. So the converger re-implemented the + * half it could reach (the fields map) and silently did not implement the half + * it could not (this index), and `GET /meta/object/:name` served a multi-tenant + * object reporting no tenant index while the registry's own answer carried one + * (#8375). + * + * The fix is not an `indexes` line added to that copy — a second implementation + * is what drifted in the first place. It is this: ONE function, called by the + * producer below and by {@link SchemaRegistry.materializeBaseLayer}, which is + * the seam #8268 built so a stamp converges at every read exit the day it is + * added. `__search` converges by delegating to the registry and has never + * drifted; this stamp now converges the same way. + * + * ## The predicate, stated exactly + * + * "The object carries a tenant column" — not "this call just injected one", and + * [#8459] not "the PLATFORM provisioned it". The first distinction is the whole + * reason the decision moved out of the injection branch: at a read exit the + * column is ALREADY present (`governServedItem` runs + * `applyInjectedSystemColumns` before the seam), so a condition reading + * `!schema.fields?.organization_id` is false exactly where the convergence is + * needed. {@link carriesTenantScopeColumn} answers the question the old nesting + * was really asking, at either site, in either order. + * + * The second distinction is the 2026-08-13 maintainer ruling on #8459. This + * predicate also required the column to be the platform's OWN definition + * (`isInjectedColumnDefinition`, byte-for-byte), which #8375 preserved exactly + * rather than widening inside a convergence fix. The effect was that an author + * who declared their own `organization_id` — adding a label, making it + * `required`, pointing it at their own org table — kept the column and silently + * lost the index on it, while `computeTenantLayer0Filter` (plugin-security) + * went on AND-composing `organization_id = ` onto essentially every read of + * that object. The deployment's hottest predicate, unindexed, reached by an + * additive-looking authoring move that removed a guarantee the author never knew + * they held. + * + * The ruling is one rule, stated once: on a walled deployment the wall's + * predicate is indexed, whoever typed the column. ⛔ Not a type judgement — an + * `organization_id` declared as `text` gets the index too. Inspecting the + * declared type to decide whether it is "really" the tenant anchor was + * considered and REJECTED: it is a third predicate at the site where #8375 just + * reduced two to one, and its boundary is itself something an author gets + * subtly wrong (a `text` org code would lose the index and look fine). + * + * ⚠️ Recorded gap, deliberately left uncovered by that ruling: an author who + * wants NO index on the tenant column has no way to say so. If that need + * appears it becomes an explicit declared key — ⛔ it is not grounds to restore + * the injected-column condition. What exists today for a DIFFERENT index shape + * is {@link declaresTenantIndex}: declare your own single-column tenant index + * and the platform declares none. + * + * `multiTenant: false` declares NO index rather than a false one: on an unwalled + * stack nothing filters by organization, so the index is dead weight — the same + * intent the retired `indexed` flag's value carried (#6810), expressed as + * presence instead of a boolean. + * + * No `name` on the entry: each driver derives its own (SQL's `buildIndexName` is + * table-qualified, which a hardcoded name could not be without colliding across + * tables on Postgres; Mongo's index names are per-collection). `unique` is left + * at its default `false` — a plain lookup index, never a constraint. + * + * Idempotent, via {@link declaresTenantIndex}: it runs twice on every + * registration (once at the tail of `applySystemFields`, once at the seam) and + * the second call is a no-op. Returns `schema` BY REFERENCE when nothing is + * owed, so a registry-sourced body pays a comparison and no copy. + */ + +/** + * [#8375] The index entry the platform declares for the tenant scope column — + * the ONE spelling of it, so the stamp and its write-side strip cannot disagree + * about what "the platform's own entry" looks like. + */ +const TENANT_SCOPE_INDEX: { fields: string[] } = { fields: ['organization_id'] }; + +function provisionTenantScopeIndex( + schema: ServiceObject, + opts: { multiTenant: boolean; platformGlobalObjects?: ReadonlySet }, +): ServiceObject { + if (!opts.multiTenant) return schema; + if (!carriesTenantScopeColumn(schema, opts.platformGlobalObjects)) return schema; + if (declaresTenantIndex(schema)) return schema; + + return { + ...schema, + // [#6810] Author-declared indexes keep their position; the platform's + // tenant index is APPENDED, never merged into or reordering theirs. + // The write-side inverse depends on that append being at the END — + // see {@link SchemaRegistry.stripProvisionedTenantIndexFrom}. + indexes: [...((schema as any).indexes ?? []), { ...TENANT_SCOPE_INDEX }], + } as ServiceObject; +} + +/** + * [#8375, widened by #8459, rebound to the wall by #8608] Is this object + * tenant-scoped — i.e. does the WALL filter it by `organization_id`? + * + * ## One question, one authority + * + * This read `resolveInjectedSystemColumns(schema).tenant` — the spec's + * INJECTION plan — on the reasoning that an object withholding the COLUMN has + * nothing for an index to serve. That reasoning holds for three of the plan's + * opt-out rows and fails on the fourth. `systemFields: false` is the hard + * object-level opt-out, so the plan answers "inject nothing"; plugin-security + * derives `tenancyDisabled` from exactly two clauses — + * + * tenancy.enabled === false || systemFields.tenant === false + * + * — and `systemFields: false` is in neither. An object using the hard opt-out + * while declaring its OWN `organization_id` therefore had the wall predicate + * AND-composed onto essentially every read with no index behind it: the same + * "hottest predicate unindexed" shape #6810 and #8459 exist to prevent, reached + * by a third route. Measured end to end on #8608 rather than inferred — for one + * such object the registry answered `indexes: null` while plugin-security's + * `getReadFilter` answered `{ organization_id: }`. + * + * Triage ruling, 2026-08-14 (option A), applying the standing meta-rule that + * when one question has two disagreeing implementations the GOVERNED side wins + * by default and the ungoverned side rebinds to it: **the wall's derivation is + * authoritative and the index follows it.** ⛔ Option B — teaching + * plugin-security to read `systemFields: false` as tenancy-disabled — was + * REJECTED, because it narrows a wall and only an explicit product ruling can + * do that. So this predicate is now the wall's, in the wall's own terms: + * + * 1. the wall's two clauses, and nothing else, decide "is tenancy off here"; + * 2. the object carries `organization_id` — either the platform provisions it + * (the injection plan) or the author declared it. + * + * `managedBy: 'better-auth'` is deliberately NOT a third clause: the wall does + * not read it either, so a better-auth table that DECLARES `organization_id` + * (`sys_member`) is walled on that column and is now indexed on it. Re-adding + * an exclusion the wall does not have is precisely the drift this rebinding + * closes — and `declaresTenantIndex` already covers the tables that declare + * their own tenant index (`sys_invitation`, `sys_team`). + * + * ⚠️ The clause-1 rows are NOT a leftover of the condition #8459 lifted: an + * object that declares itself non-tenant-scoped is one the wall composes NO + * predicate on (`computeTenantLayer0Filter` returns `null` when + * `tenancyDisabled`), so an index there would serve nothing — the same + * reasoning as `multiTenant: false`, one scope down. What #8459 removed is a + * different condition: that the `organization_id` present be the platform's own + * definition byte-for-byte (`isInjectedColumnDefinition`). Where the column + * comes from is no longer part of the question; whether the object is WALLED + * still is. + * + * ⛔ Do NOT reduce clause 2 to its field-map half alone + * (`fields.organization_id !== undefined`), however closely that reads to the + * ruling's sentence. Measured: it breaks the WRITE path for ordinary + * platform-provisioned objects. The save path strips the injected COLUMNS + * before it strips the materialized stamps + * (`stripMaterializedFromRegistry(type, stripServedSystemColumns(type, item))`, + * `@objectstack/metadata-protocol`), so by the time + * {@link SchemaRegistry.stripProvisionedTenantIndexFrom} re-stamps the + * remainder through this function, the body no longer HAS an + * `organization_id` — a field-map-only predicate answers "not tenant-scoped", + * the re-stamp adds nothing, the lists differ, the strip refuses, and the + * platform's own index entry is baked into `sys_metadata.metadata`, its + * checksum and every history diff (the #4326 regression). The injection-plan + * branch is what answers on a stripped body; the field-map branch only ever + * ADDS objects whose column that plan cannot see. And the addition is safe on + * the write path for the mirrored reason: on exactly those rows the plan also + * strips nothing (`plan.names` is `{ id }`), so the author's declared column is + * still present when the re-stamp asks. + * + * [#15225 → #15813] Briefly exported at module level for the engine's + * bulk-event producer, which answered "is this object walled?" from it — a + * binding of TWO of the wall's three object clauses, structurally blind to + * the deployment's #12699 carve-out (the #15706 finding). That producer now + * reads the Layer 0 verdict plugin-security RECORDS on the operation + * (`OperationContext.tenantLayer0Verdict`) and consults no schema predicate at + * all, so this is private again: the registry's own reading, for the two + * sites above, and ⛔ not a contract for any other package to answer the + * wall from — the wall answers for itself (ADR-0131 D8). + */ +function carriesTenantScopeColumn( + schema: ServiceObject, + platformGlobalObjects?: ReadonlySet, +): boolean { + // Clause 1 — the wall's own two clauses, spelled here because + // plugin-security spells them there (option C, the single exported + // predicate, is bounded to no new `@objectstack/spec` export and no + // plugin-security behaviour change, so the convergence itself is a + // follow-up; `isTenancyDisabled` is already the shared reading of the first). + if (isTenancyDisabled(schema)) return false; + if ((schema as { systemFields?: { tenant?: boolean } }).systemFields?.tenant === false) { + return false; + } + // Clause 2 — there is a column for the wall's predicate to filter on. + // [ADR-0131 D7] The injection plan is asked with the deployment's + // platform-global declaration, the same input `applySystemFields` planned the + // column from: a declared object carries no injected tenant column, so there + // is nothing for an index to serve. + const plan = + platformGlobalObjects && platformGlobalObjects.size > 0 + ? resolveInjectedSystemColumns(schema, { platformGlobalObjects }) + : resolveInjectedSystemColumns(schema); + return ( + plan.tenant || + (schema as { fields?: Record }).fields?.organization_id != null + ); +} + +/** + * [#6810] Is the tenant index already declared on this object? + * + * The append is the one part of this injection that is not naturally + * idempotent — the field injection re-runs harmlessly because + * `!schema.fields?.organization_id` stops it, an array push does not — so an + * author who hand-wrote `indexes: [{ fields: ['organization_id'] }]` must not + * end up with the platform's duplicate beside it. + * + * Matched on the single-column shape this function emits, deliberately: an + * author's composite (`['organization_id', 'code']`) is a leading-column match + * on some dialects and not on others, so it is not treated as a substitute. + */ +function declaresTenantIndex(schema: ServiceObject): boolean { + const declared = (schema as any).indexes; + if (!Array.isArray(declared)) return false; + return declared.some( + (idx: any) => + Array.isArray(idx?.fields) && + idx.fields.length === 1 && + idx.fields[0] === 'organization_id', + ); +} + +/** + * Reconcile a managed object's `enable.apiMethods` against the generic-write + * affordances it actually grants (ADR-0092 / ADR-0103). + * + * A `managedBy` object whose resolved affordances forbid a write verb, yet + * which *also* advertises that verb in `enable.apiMethods`, is internally + * contradictory: the HTTP exposure gate (ADR-0049) would admit the request and + * let it 403 at the engine instead of answering a clean 405, and the metadata + * misrepresents what the API offers. + * + * This is the registration-time backstop that makes the contradiction + * impossible to *ship*: any write verb whose CRUD affordance the object does + * not grant — via the `managedBy` bucket default plus `userActions` overrides, + * exactly as `resolveCrudAffordances` computes for the UI — is stripped, + * with a warning. Reads are never touched. + * + * The judgement itself is NOT here. `checkManagedApiMethodAffordances` + * (`@objectstack/spec/data`) owns the verb → affordance table, and + * `@objectstack/lint`'s `validateManagedApiMethods` — the authoring-time gate + * that reports the same contradiction where the AUTHOR is, rather than in a + * boot log nobody reads (#7521) — reads that same predicate. This function is + * only the registration-time REACTION to it: strip and warn. If a verdict seems + * wrong, fix the predicate, never this reaction — a second table here is the + * declared≠enforced drift the predicate exists to detect. + * + * WARN and strip, never throw — deliberately (the #7521 ruling, 2026-08-11): + * failing registration closed would let one metadata typo kill a control-plane + * boot, which is too harsh for ops. The author-time gate is where this blocks. + * + * Runs for every managed bucket (ADR-0103). Objects that legitimately take + * user-context writes declare `userActions` opening those verbs, so their + * apiMethods are kept: `sys_user` keeps `update` (`userActions.edit: true`; its + * writes are field-whitelist-clamped by the identity guard), and the `system` + * writable set (RBAC link tables, prefs, messaging config) keeps its CRUD + * verbs. Engine-owned `system` / `append-only` objects and the other identity + * tables — which grant no write affordance — derive down to reads only. Any + * future managed object that advertises a write verb it does not permit is the + * drift this backstop catches. + * + * Returns the input unchanged when nothing is stripped; otherwise a new schema + * with a rewritten `enable.apiMethods` (immutable, like {@link applySystemFields}). + */ +export function reconcileManagedApiMethods( + schema: ServiceObject, + opts?: { warn?: (msg: string) => void }, +): ServiceObject { + const conflicts = checkManagedApiMethodAffordances(schema); + if (conflicts.length === 0) return schema; + + const methods = (schema as any).enable.apiMethods as unknown[]; + const strippedIndexes = new Set(conflicts.map((c) => c.index)); + const kept = methods.filter((_m, i) => !strippedIndexes.has(i)); + const stripped = conflicts.map((c) => c.verb); + + const managedBy = (schema as any).managedBy; + // [ADR-0153 D4] On a write-locked bucket (`synced`) `userActions` cannot open + // a write — the parse refuses it — so the remedy is to drop the verbs and + // write through a declared action (D5). Every other bucket keeps its sentence. + const remedy = MANAGED_BY_WRITE_LOCKED_BUCKETS.has(managedBy) + ? `userActions cannot open a write on this bucket (ADR-0153 D4): remove [${stripped.join(', ')}] ` + + `from enable.apiMethods and send writes through a declared action (ADR-0153 D5).` + : `Declare userActions to open a verb the object legitimately takes from a user context.`; + const warn = opts?.warn ?? ((msg: string) => console.warn(msg)); + warn( + `[Registry] Object "${schema.name}" is managedBy:'${managedBy}' but advertised ` + + `generic write verb(s) [${stripped.join(', ')}] in enable.apiMethods its resolved affordances ` + + `do not permit — stripping them (ADR-0092/ADR-0103). ${remedy} Kept: [${kept.join(', ')}]. ` + + `\`os lint\` reports the same contradiction as \`object/managed-api-method-unaffordable\` ` + + `with full authoring context.`, + ); + + return { + ...schema, + enable: { ...(schema as any).enable, apiMethods: kept }, + }; +} + +/** Objects already warned about stripped legacy `apiMethods` (once per object). */ +const warnedLegacyApiMethods = new Set(); + +/** + * Registration-time diagnostic for retired LEGACY `apiMethods` values (#3543, + * P2 of #3391). Since the enum shrink, the 8 legacy verbs (`upsert`/ + * `aggregate`/`history`/`search`/`restore`/`purge`/`import`/`export`) are no + * longer authorable: a declared legacy value is IGNORED — the effective API + * surface derives from the six primitives alone. The Zod parse path already + * strips-and-warns (`stripLegacyApiMethods` in `@objectstack/spec/data`), but + * carries no object name; this registration-time diagnostic adds the per-object + * view for schemas that reach the registry without passing through Zod (raw + * `ServiceObject` literals, out-of-band metadata). Real metadata does not + * upgrade in lockstep with the spec, so this is a PERMANENT compatibility + * diagnostic, not a transition. + * + * Emitted once per object (not per request — the hot path stays free). Pure + * observation: it never mutates the schema (the derivation resolver ignores + * legacy values on its own). + */ +export function warnStrippedLegacyApiMethods( + schema: ServiceObject, + opts?: { warn?: (msg: string) => void }, +): void { + const methods = (schema as any).enable?.apiMethods; + if (!Array.isArray(methods) || methods.length === 0) return; + const legacy = methods.filter((m: string) => (LEGACY_API_METHODS as readonly string[]).includes(m)); + if (legacy.length === 0) return; + const name = String((schema as any).name ?? ''); + if (warnedLegacyApiMethods.has(name)) return; + warnedLegacyApiMethods.add(name); + const remaining = methods.filter((m: string) => !(LEGACY_API_METHODS as readonly string[]).includes(m)); + const warn = opts?.warn ?? ((msg: string) => console.warn(msg)); + warn( + `[Registry] Object "${name}" declares retired legacy apiMethods value(s) ` + + `[${legacy.join(', ')}] in enable.apiMethods — the authorable values are now the six ` + + `primitives only, because every other operation is derived from them or retired, so these ` + + `are IGNORED: the effective API surface derives from the ` + + `six primitives (get/list/create/update/delete/bulk) alone ` + + `(['create','update'] ⇒ upsert/import; ['list'] ⇒ aggregate/search/export; ` + + `['get'] + trackHistory ⇒ history).` + + (remaining.length === 0 + ? ` ⚠ Ignoring them leaves NO primitives — this object's API resolves ` + + `to deny-all (fully closed).` + : '') + + ` Remove the value(s); codemod: node scripts/codemod/apimethods-legacy-to-primitives.mjs.`, + ); +} + +/** Objects already diagnosed for functional completeness (once per object). */ +const warnedFunctionalCompleteness = new Set(); + +/** + * [ADR-0078 Phase 4] Registration-time functional-completeness diagnostic. + * + * The author-time gate (`@objectstack/lint`'s `validate-functional-completeness`) + * only protects metadata that passes through `os build` / `validate` / `lint`. + * The registry is the one choke point EVERY door goes through — declared + * stacks, plugin-provided objects, `extend` contributions, `saveMetaItem`, raw + * `registerObject` calls — including the doors that skip Zod and lint entirely. + * Two shipped instances prove those doors are real, not hypothetical: #3896 + * (Setup authoring inserted `sys_sharing_rule` rows directly, bypassing the + * schema that "required" `criteria`) and cloud's `rowColor.mapping` (an + * `as never` cast bypassed tsc, then the strip-era parse dropped the key). + * + * Same shared predicate as the author-time gate (`checkFieldCompleteness` in + * `@objectstack/spec/kernel`), so the rule ids in this warning are the SAME ids + * the lint reports — an operator or an AI reading the boot log can grep the id + * straight into the docs and the suppression story. Judgement lives only in the + * predicate; if a rule seems wrong, fix it there, never here. + * + * WARN, never throw — deliberately, and not as a soft default: an inert field + * must not kill a boot that thousands of healthy objects share (ADR-0078 §1 + * maps error-severity to "the INSTANCE is dead", not "the system is dead"). + * The author-time gate is where errors block; the registry's job is to make + * sure the silence never survives to runtime unobserved. + * + * Emitted once per object name with every finding aggregated into that one + * line (not per request, not per finding — the hot path stays free and a + * 3-dead-field object is one greppable line, not three). + */ +export function warnFunctionalCompleteness( + schema: ServiceObject, + opts?: { warn?: (msg: string) => void }, +): void { + const fields = (schema as { fields?: Record }).fields; + if (!fields || typeof fields !== 'object') return; + const name = String((schema as { name?: unknown }).name ?? ''); + if (warnedFunctionalCompleteness.has(name)) return; + + const findings: string[] = []; + for (const [fieldName, def] of Object.entries(fields)) { + for (const f of checkFieldCompleteness(def)) { + findings.push(`${fieldName}: [${f.severity}] ${f.rule} — add \`${f.fix}\``); + } + } + if (findings.length === 0) return; + warnedFunctionalCompleteness.add(name); + + const warn = opts?.warn ?? ((msg: string) => console.warn(msg)); + warn( + `[Registry] Object "${name}" registered with ${findings.length} functionally-incomplete ` + + `field(s) — Zod-valid but runtime-DEAD: the consumer silently skips each one, so it reads ` + + `0/null/never-resolves while every authoring surface reports success (ADR-0078). ` + + findings.join(' · ') + + ` — \`os lint\` reports the same rule ids with full context.`, + ); +} + +/** + * [#16319] The `error.code` the field-`type` refusal below carries. + * + * `INVALID_METADATA` — already registered under `@objectstack/objectql` in + * `ERROR_CODE_LEDGER`, and already this package's spelling for "a stored or + * programmatically-supplied metadata body does not satisfy the spec schema" + * (`engine.ts`'s two plugin-view expansion refusals answer with it). ⛔ Not a + * new code: minting one widens the published error-code face, which is a + * Clause-② contract change and not this repair's to take. The CONDITION is + * named by {@link OBJECT_FIELD_TYPE_REFUSED_ERROR_NAME}, which is what a + * consumer discriminates on beyond the code — `INVALID_METADATA` is shared with + * several unrelated refusals in this package. + */ +export const OBJECT_FIELD_TYPE_REFUSED_CODE = 'INVALID_METADATA' as const; + +/** + * {@link ExtensionDefaultRowScopeRefusedError}'s code — `INVALID_METADATA`, + * this package's spelling for "a programmatically supplied metadata body does + * not satisfy the spec schema" (see {@link OBJECT_FIELD_TYPE_REFUSED_CODE}): + * `ObjectExtensionSchema` is strict and declares no `defaultRowScope`, so the + * parse refuses the same body at authoring. ⛔ Not a new code. The condition is + * named by the error's `name`. + */ +export const EXTENSION_DEFAULT_ROW_SCOPE_REFUSED_CODE = 'INVALID_METADATA' as const; + +/** + * {@link ExtensionMembershipKeysRefusedError}'s code — `INVALID_METADATA`, for + * the reason {@link EXTENSION_DEFAULT_ROW_SCOPE_REFUSED_CODE} gives: + * `ObjectExtensionSchema` is strict and declares no `membershipKeys`, so the + * parse refuses the same body at authoring. ⛔ Not a new code. The condition is + * named by the error's `name`. + */ +export const EXTENSION_MEMBERSHIP_KEYS_REFUSED_CODE = 'INVALID_METADATA' as const; + +/** + * {@link ValidationRuleCopyRefusedError}'s code — `INVALID_METADATA`, for the + * reason {@link EXTENSION_DEFAULT_ROW_SCOPE_REFUSED_CODE} gives: a body this + * registry will not take as written. ⛔ Not a new code. The condition is named + * by the error's `name`. + */ +export const VALIDATION_RULE_COPY_REFUSED_CODE = 'INVALID_METADATA' as const; + +/** + * {@link ObjectQueueColumnRefusedError}'s code — `INVALID_METADATA`, as + * {@link ObjectFieldTypeRefusedError}'s is: a body this registry cannot + * register as written. ⛔ Not a new code; the condition is named by the + * error's `name`. + */ +export const OBJECT_QUEUE_COLUMN_REFUSED_CODE = 'INVALID_METADATA' as const; + +/** + * Platform namespaces that multiple packages may legitimately share, so the + * install-time namespace-uniqueness gate (ADR-0048 Phase 1) must never fire on + * them: the FQN-exempt reserved namespaces (`base`, `system`) plus `sys` + * (system objects such as `sys_metadata` are contributed by many packages — see + * {@link SchemaRegistry.registerNamespace}, which is intentionally many-to-one). + */ +function isShareableNamespace(ns: string): boolean { + return RESERVED_NAMESPACES.has(ns) || ns === 'sys'; +} + +/** + * Sentinel for "this value is not data and does not belong in the record". + * A distinct symbol rather than `undefined`, so a manifest that genuinely + * carries `undefined` is not confused with a value that was dropped. + */ +const NOT_RECORD_DATA = Symbol('objectstack.registry.notRecordData'); + +/** Whether `value` is a bare `{}`-shaped object (or a null-prototype bag). */ +function isPlainDataObject(value: object): boolean { + const proto = Object.getPrototypeOf(value); + return proto === Object.prototype || proto === null; +} + +/** + * One step of {@link toRecordManifest} — project a value, or report that it is + * not data. `seen` carries the ancestors of the current value, so a CYCLE is + * detected at the second visit and the back-reference is dropped rather than + * followed. Siblings are re-projected independently (the ancestor is deleted on + * the way out), so a manifest that legitimately mentions the same sub-object + * twice keeps both copies. + */ +function projectRecordValue(value: unknown, seen: Set): unknown { + if (value === null) return null; + const kind = typeof value; + if (kind === 'string' || kind === 'number' || kind === 'boolean') return value; + // `undefined`, `function`, `symbol`, `bigint` — none of which survive + // `JSON.stringify` as themselves (`bigint` is the one that THROWS). + if (kind !== 'object') return NOT_RECORD_DATA; + + const obj = value as object; + // A `Date` is exotic but has a declared JSON form, so it is data. Copied + // rather than shared: the record must not alias a mutable runtime value. + if (obj instanceof Date) return new Date(obj.getTime()); + if (seen.has(obj)) return NOT_RECORD_DATA; + + if (Array.isArray(obj)) { + seen.add(obj); + const out: unknown[] = []; + for (const entry of obj) { + const projected = projectRecordValue(entry, seen); + // Non-data entries are OMITTED, not held as `null` holes: nothing reads a + // manifest collection by index (every one of them is name-keyed), and a + // `plugins: [null, null, …]` on the wire says less than `plugins: []`. + if (projected !== NOT_RECORD_DATA) out.push(projected); + } + seen.delete(obj); + return out; + } + + if (!isPlainDataObject(obj)) return NOT_RECORD_DATA; + + seen.add(obj); + const out: Record = {}; + for (const [key, entry] of Object.entries(obj)) { + const projected = projectRecordValue(entry, seen); + if (projected !== NOT_RECORD_DATA) out[key] = projected; + } + seen.delete(obj); + return out; +} + +/** + * Project an authored manifest into the SERIALIZABLE RECORD the registry keeps. + * + * {@link SchemaRegistry.installPackage} used to store the caller's object + * verbatim. For a code-defined stack (`objectstack.config.ts`) that object is + * the LIVE runtime one — `plugins: [new ConnectorRestPlugin(), …]` — and a + * plugin instance holds the engine once it initialises. Since the engine grew + * `actionActivation -> store -> engine` that reference closes a CYCLE, so + * `JSON.stringify` of the registry item THREW and every read door that + * serialises a package answered `500 INTERNAL_ERROR` on a stock showcase boot: + * `GET /packages`, `GET /packages/:id`, `GET /meta/package/:id`. Before that + * cycle existed the same doors serialised the whole engine graph into the + * payload instead, which is the same defect with a quieter symptom. + * + * Measured on that boot: of the 26 installed packages exactly ONE manifest key + * was unserializable — `plugins`, on `com.example.showcase` — and only AFTER + * plugin init; the same manifest serialised cleanly during boot. So an + * install-time `JSON.stringify` probe would have called the record healthy. The + * projection therefore drops by SHAPE, at install, and never depends on when it + * is asked. + * + * The registry item is a RECORD, not the runtime. The kernel keeps the live + * object (`ObjectQL.manifests`), and the one reader of `manifest.plugins[]` — + * `ObjectQL.registerApp`'s nested-plugin seam — reads its OWN parameter, never + * the record, so nothing downstream loses a member it was using. + * + * The rule is STRUCTURAL rather than a key denylist, because the fault is not + * "the key is called `plugins`" — it is "a live object reached the record". + * JSON data survives; everything else is dropped: + * + * - primitives, plain objects, arrays and `Date` are data; + * - functions / symbols / bigints are dropped (a function was already + * invisible to `JSON.stringify`; dropping it only makes the record honest, + * and a bigint would have thrown); + * - class instances, `Map`, `Set` and every other exotic object are dropped — + * a declarative manifest has none, and a code-defined stack's are host + * wiring; + * - a reference cycle among plain data is dropped at the back-edge, so a + * self-referencing manifest degrades to a missing field instead of throwing. + * + * ⛔ This is the PRODUCER's repair, not a consumer-side tolerance: no reader is + * taught to survive an unserializable record, because the record is never + * unserializable to begin with (AGENTS.md Prime Directive #12). + */ +function toRecordManifest(manifest: ObjectStackManifest): ObjectStackManifest { + // The ROOT is projected key-by-key unconditionally: a host whose + // `defineStack()` returns a class instance must still yield a record, and + // `projectRecordValue` would drop the whole thing. + if (manifest === null || typeof manifest !== 'object') return manifest; + const seen = new Set([manifest as object]); + const out: Record = {}; + for (const [key, value] of Object.entries(manifest as Record)) { + const projected = projectRecordValue(value, seen); + if (projected !== NOT_RECORD_DATA) out[key] = projected; + } + return out as ObjectStackManifest; +} + +/** + * Rewrite a BARE callable `functions` map entry into the declared form + * (`{ handler, effect }`) before {@link toRecordManifest} projects the body. + * + * ## The under-report this closes + * + * `functions` accepts two authored spellings, and the projection treats them + * unequally through no fault of its own: + * + * - `{ sweepProjectHealth: { handler: fn, effect: 'writes' } }` — the value is + * a plain object, so it survives with its callable dropped: `{ effect: + * 'writes' }`. The function is still REPORTED, minus what cannot serialise. + * - `{ summarizeCompletedTask: fn }` — the value IS the callable, so the whole + * ENTRY is dropped, key and all. The function vanishes from the record. + * + * `examples/app-showcase` ships one of each, so `GET /packages` reported ONE + * function for a package declaring TWO. A machine-readable read door that + * under-reports is a defect independent of how its rows are declared. + * + * ## Why it is fixed HERE and not in the projection + * + * {@link toRecordManifest}'s rule is STRUCTURAL — 「a live object reached the + * record」, ⛔ never a key denylist — and teaching it that a function found + * under `functions.` means something other than a function found + * anywhere else would overturn exactly that. So the repair is at the assembly + * boundary instead: the two spellings are made structurally EQUAL before the + * projection runs, and the projection then leaves `{ effect }` for both. + * + * ⛔ No ref is minted. `packages/cli`'s `lowerCallables` mints refs with + * `uniqueName(base, taken)` and dedupes by function IDENTITY, so a ref minted + * here would not be guaranteed equal to the one `objectstack build` mints — a + * record could then assert a handler that resolves in no sibling module. An + * ABSENT `handler` on a record is the honest statement 「declared here, not + * serialisable」, and `RecordStagePackageBodySchema` in `@objectstack/spec` + * declares exactly that. + * + * `effect` is the declaration schema's OWN default + * ({@link DEFAULT_FLOW_FUNCTION_EFFECT}, `'pure'`, which + * `FlowFunctionDeclarationSchema.effect` carries as `.default(…)`), ⛔ not a + * value invented here: the bare spelling IS that declaration written the short + * way, and `normalizeFlowFunctionEntry` already reads it the same way at boot. + * The ARRAY form needs nothing — its entries are objects that carry their own + * `name`, so the projection keeps them — and its `effect` is `.optional()` with + * no default, so nothing is written where the schema declares nothing. + * + * ⛔ The caller's manifest is never mutated: the live object is what every + * other read in `installPackage` uses, and a copy is returned only when an + * entry really needed rewriting. + */ +function withDeclaredFunctionEntries(manifest: ObjectStackManifest): ObjectStackManifest { + if (manifest === null || typeof manifest !== 'object') return manifest; + const functions = (manifest as { functions?: unknown }).functions; + if (!functions || typeof functions !== 'object' || Array.isArray(functions)) return manifest; + + let rewrote = false; + const declared: Record = {}; + for (const [name, entry] of Object.entries(functions as Record)) { + if (typeof entry === 'function') { + declared[name] = { handler: entry, effect: DEFAULT_FLOW_FUNCTION_EFFECT }; + rewrote = true; + } else { + declared[name] = entry; + } + } + if (!rewrote) return manifest; + return { ...manifest, functions: declared } as ObjectStackManifest; +} + +/** + * [#16159] The ADR-0112 `code` strings this file's three install-time and + * registration refusals carry, as constants a consumer can import. + * + * Every one of the three classes below already tells the reader, in its own + * docblock, that it "carries the ADR-0112 envelope (`code` + `status`)". That + * convention is sound and `instanceof` is not: `@objectstack/objectql` + * declares BOTH realms in its own `exports` (`import` reaches + * `dist/index.mjs`, `require` reaches `dist/index.js`), so a consumer holding + * the other realm's copy of a class gets `instanceof` === false — measured, + * and silent (#14936). A `code` compare is the check that survives crossing + * that boundary. Until now a consumer following the convention had to + * RE-SPELL the string itself, which acquires a `check:error-code-provenance` + * stamp site in the consumer's own package and can then drift from what this + * engine throws with no compile error to say so. + * + * ⛔ The strings are byte-identical to the literals they replace. This moves + * where a spelling lives, never what it says; renaming any of these codes is + * a separate breaking decision and never a rider on this conversion. + * + * The `*_CODE` NAME is load-bearing rather than cosmetic, in two gates at + * once: it is the shape `check:error-code-provenance`'s `constdef` pattern + * can see, and `readonly code = X_CODE;` is the shape + * `check:dispatcher-error-vocabulary` classifies as `classconst` (the three + * rows in `packages/runtime/src/dispatcher-error-vocabulary.ts` move from + * `classfield` to `classconst` with this change, and the scanner resolves the + * constant back to the same value). ⛔ Never rename out of that shape to + * quiet a gate: a spelling a gate cannot see is the failure mode the gate + * exists to catch, not a clean result. + * + * Shape and placement follow the six `*_CODE` constants already in this + * package (`DUPLICATE_RECORD_CODE`, `HOOK_TARGET_REBIND_ERROR_CODE`, + * `HOOK_UNSCOPED_DATA_ACCESS_CODE`, + * `MULTI_UPDATE_HOOK_KEY_DIVERGENCE_CODE`, `EMPTY_CREDENTIAL_REFUSAL_CODE`, + * `SYSTEM_WRITE_ORGANIZATION_REQUIRED_CODE`) — re-exported from the + * `index.ts` barrel and, like all six, deliberately NOT from the lean + * `core.ts` entry, which carries none of them. + * + * ⛔ Deliberately NOT a recognizer factory: #16156 measured that a + * `makeRecognizer(code)` signature still requires every call site to supply + * the code, which RELOCATES the literal rather than removing it. + */ +export const NAMESPACE_CONFLICT_CODE = 'NAMESPACE_CONFLICT' as const; + +/** {@link ArtifactObjectNameConflictError}'s code — ADR-0130 D3, one artifact. */ +export const DUPLICATE_ARTIFACT_OBJECT_NAME_CODE = 'DUPLICATE_ARTIFACT_OBJECT_NAME' as const; + +/** {@link ObjectOwnershipConflictError}'s code — ADR-0029 D3, single owner per name. */ +export const OBJECT_OWNERSHIP_CONFLICT_CODE = 'OBJECT_OWNERSHIP_CONFLICT' as const; + +/** + * {@link DefaultRecordPageConflictError}'s code — one default record page per + * object per deployment (maintainer ruling A on objectstack#23015). The save + * door in `@objectstack/metadata-protocol` stamps the same string for the same + * condition; that package cannot import this one, so it spells it once and the + * ledger lists it under both. + */ +export const DEFAULT_RECORD_PAGE_CONFLICT_CODE = 'DEFAULT_RECORD_PAGE_CONFLICT' as const; + +/** One second holder {@link SecurityCatalogNameConflictError} refused. */ +export interface SecurityCatalogNameConflict { + /** The catalog type, as the registry keys it. */ + readonly catalogType: SecurityCatalogType; + /** The name both holders claim. */ + readonly name: string; + /** The package whose registration this refusal stopped. */ + readonly incomingPackageId: string; + /** Who already holds the name. */ + readonly existingHolder: SecurityCatalogHolder; +} + +/** + * The rule both wordings of {@link SecurityCatalogNameConflictError} state, in + * the words the permission set `name` describe (`@objectstack/spec`) and the + * permission sets guide use. [ADR-0150 D2/D9] A permission set is identified + * by its package and name, so its name has one holder per namespace, judged by + * `permissionSetHoldersMeet` (`@objectstack/core`); positions and capabilities + * keep one name per deployment. Text only: which holders are refused is decided + * by the doors, never by this sentence. + */ +const SECURITY_CATALOG_NAME_RULE = + 'Positions and capabilities each hold one name per deployment. A permission set holds one name per ' + + 'namespace: packages of two namespaces may each hold a set of one name, while two packages of one ' + + 'namespace, a package with no namespace, a platform package (`base`, `system`, `sys`), the environment ' + + 'and the platform-requested `guest_portal` share one set of it.'; + +/** + * The cold-boot wording of {@link SecurityCatalogNameConflictError}: every + * package registered before the environment catalog hydrated, so each line + * names the package that declares the name and the holder it meets, and the + * remedy is stated for an operator restarting a deployment, not for an install. + */ +function coldBootConflictMessage(conflicts: readonly SecurityCatalogNameConflict[]): string { + const lines = conflicts.map( + (c) => + `package "${c.incomingPackageId}" declares the ${securityCatalogTypeLabel(c.catalogType)} "${c.name}", ` + + `already held by ${describeSecurityCatalogHolder(c.existingHolder)}`, + ); + return ( + `Security catalog name conflict at boot: ${conflicts.length === 1 ? 'a name' : `${conflicts.length} names`} ` + + `a configured package declares ${conflicts.length === 1 ? 'is' : 'are'} already held by another holder — ` + + `${lines.join('; ')}. ${SECURITY_CATALOG_NAME_RULE} Under that rule the environment's item and the ` + + `package's share one definition of the name, so with two holders the environment's stored item would be ` + + `served in place of the package's definition. The environment catalog loads from ` + + `sys_metadata before this check, so the boot is refused. Rename the item in the package, or rename or ` + + `delete the environment's item (its environment-wide sys_metadata row: \`os migrate security-catalog-overlays\` ` + + `lists every such row with no server running, and with --apply deletes them), then restart. ` + + `See ADR-0048.` + ); +} + +/** One second holder {@link BareNameConflictError} refused (or a cold boot reported). */ +export interface BareNameConflict { + /** The covered type, as the registry keys it. */ + readonly type: BareNameOneHolderType; + /** The name both holders claim. */ + readonly name: string; + /** The package whose registration this refusal stopped (at a cold boot: a later-registered holder). */ + readonly incomingPackageId: string; + /** Who already holds the name. */ + readonly existingHolder: BareNameHolder; + /** The rename that clears it — the incoming package's namespace prefixed — when it has one. */ + readonly rename?: string; + /** [#31697] For an action bound to an object under a built-in key ({@link BUILT_IN_BOUND_ACTION_KEYS}): that object. */ + readonly objectName?: string; +} + +/** One conflict's line, the same at every door. */ +function bareNameConflictLine(c: BareNameConflict): string { + const what = c.objectName !== undefined + ? `action "${c.name}" bound to "${c.objectName}"` + : `${bareNameTypeLabel(c.type)} "${c.name}"`; + return ( + `package "${c.incomingPackageId}" declares the ${what}, ` + + `already held by ${describeBareNameHolder(c.existingHolder)}` + + (c.rename ? ` — rename it to "${c.rename}" in "${c.incomingPackageId}"` : '') + ); +} + +/** Why a second holder is refused, for the types `conflicts` carry (`bareNameReason`). */ +const bareNameReasonFor = (conflicts: readonly BareNameConflict[]): string => + bareNameReason(conflicts.map((c) => c.type)); + +/** The cold-boot warning for the names two installed packages already hold (`bare-name-one-holder.ts`). */ +export function bareNameColdBootWarning(conflicts: readonly BareNameConflict[]): string { + return ( + `Bare-name conflict at boot: ${conflicts.length === 1 ? 'a second holder' : `${conflicts.length} second holders`} ` + + `among the installed packages — ${conflicts.map(bareNameConflictLine).join('; ')}. ${bareNameReasonFor(conflicts)} ` + + `The boot continues and changes nothing about which holder each caller is served; a further package ` + + `declaring the name is refused at install. Rename the item in one package (its namespace prefixed, as ` + + `above), or uninstall one of the two packages. See ADR-0048.` + ); +} + +/** One second default record page {@link DefaultRecordPageConflictError} refused. */ +export interface DefaultRecordPageConflict { + /** The object both pages are the default record page of. */ + readonly object: string; + /** + * The app both pages are the record page of `object` inside (`PageSchema.apps`, + * ruling A on objectstack#28979); absent for the deployment's default. + */ + readonly app?: string; + /** The page whose registration this refusal stopped. */ + readonly incoming: DefaultRecordPageHolder; + /** The page that already holds the object's default record page. */ + readonly existing: DefaultRecordPageHolder; +} + +/** + * {@link MisplacedDefaultPageError}'s code — `INVALID_METADATA`, already this + * package's spelling for "a programmatically supplied metadata body does not + * satisfy the spec schema" (see {@link OBJECT_FIELD_TYPE_REFUSED_CODE}): the + * page parse refuses the same body at `os validate` and at save. ⛔ Not a new + * code. The condition is named by the error's `name`. + */ +export const MISPLACED_DEFAULT_PAGE_CODE = 'INVALID_METADATA' as const; + +/** + * [ADR-0130 D1] What the install gate is told about the artifact now installing. + * + * `installPackage` has no notion of which release artifact a manifest arrived + * in, and ADR-0130 D8 deliberately adds **no** owner/publisher field to the + * manifest to give it one. So the knowledge is threaded as an install-time + * SCOPE: the load path that reads an artifact (`manifest.register()` → + * `resolveArtifactPackageOrder` → `registerApp`) already holds the artifact's + * package list, and passes it down. + * + * Two consequences worth stating, because both are load-bearing: + * + * - **It is per-install, not stored.** Nothing persists a co-ownership claim, + * so nothing can drift from the artifact that is the claim (D8's objection to + * a manifest field, applied to the runtime). + * - **Absent scope means today's behaviour, structurally.** Every caller that + * installs ONE package — `protocol.installPackage`, `POST /packages`, a bare + * `registerApp` — passes no scope, so the co-owner set is empty and both + * halves of the ADR-0130 D1/D3 pair are no-ops. A single-`manifest` artifact + * passes a one-element scope, whose only member is the package itself, which + * the gate excludes anyway (D7). + */ +export interface ArtifactInstallScope { + /** + * The ids of every package delivered by the artifact currently installing — + * read through {@link artifactPackageId}, so they are the same strings the + * registry keys installed packages by. + */ + readonly packageIds: readonly string[]; +} + +/** + * Why two packages co-own one namespace at the install gate: + * + * - `artifact` — the same release artifact delivers both (ADR-0130 D1); + * - `dependency` — one declares the other in `manifest.dependencies` (ADR-0141 D5); + * - `sibling` — both declare one installed package that holds the namespace (ADR-0141 D5). + */ +export type CoOwnershipSource = 'artifact' | 'dependency' | 'sibling'; + +/** + * The object names one manifest CLAIMS OWNERSHIP of at install time — the names + * `registerApp` is about to hand `registerObject` with `ownership: 'own'`. + * + * Both authored shapes are read, because `registerApp` reads both: an array of + * object definitions, or a name-keyed map (whose KEY is the name — `registerApp` + * overwrites `objDef.name` with it). + * + * ⛔ Glob strings are skipped, not treated as names. `ManifestSchema.objects` is + * `z.array(z.string())` — file patterns — and an unassembled manifest that still + * carries them registers no object here; a pattern is not an object name, and + * refusing on one would refuse a package that claims nothing. + * + * ⛔ `objectExtensions` are not read: an extension contributes fields to an + * object it does not own (`ownership: 'extend'`), which is the co-ownership + * ADR-0130 exists to permit, not the collision it refuses. + */ +function declaredOwnedObjectNames(manifest: ObjectStackManifest): string[] { + const objects = (manifest as { objects?: unknown }).objects; + if (!objects || typeof objects !== 'object') return []; + if (Array.isArray(objects)) { + return objects + .map((o) => (o && typeof o === 'object' ? (o as { name?: unknown }).name : undefined)) + .filter((n): n is string => typeof n === 'string' && n !== ''); + } + return Object.keys(objects as Record).filter((n) => n !== ''); +} + +/** + * [ADR-0130 D3] Raised when two packages delivered by ONE release artifact both + * claim the same object name. + * + * This refusal is the other half of the D1 gate relaxation, and D3 specifies the + * pair as a machine constraint rather than an instruction because this is the + * only part of ADR-0130 that can reach customer data. Today's namespace + * exclusivity is silently carrying a second guarantee: ADR-0048 §3.2 grounds the + * namespace gate on "two packages with namespace `crm` both try to create + * `crm_account` and the second fails at the DB", so "no two packages share a + * namespace" has been proxying for **"no two packages define the same object + * name."** Relax the first without adding the second and two co-owning packages + * defining `crm_account` produce either a duplicate `CREATE TABLE` or — + * driver-dependent — one package silently overwriting the other's table + * definition. + * + * So the check is install-time and ahead of every mutation `installPackage` + * makes, which puts it ahead of all DDL: the refusal lands before the second + * package is recorded at all, rather than half-applying an install and blowing + * up at table creation. + * + * Carries the ADR-0112 envelope (`code` + `status`) — the shape this + * repository's rejection tests assert against, never a bare throw. + */ +/** + * The words {@link ArtifactObjectNameConflictError} uses per co-ownership + * source, so a refusal between packages of different artifacts never claims + * they arrived in one (ADR-0141 D5). + */ +const CO_OWNERSHIP_WORDING: Record = { + artifact: { + conflict: 'inside one release artifact', + relation: 'delivered by the same artifact', + allowed: 'Sharing a namespace across packages in one artifact is allowed (ADR-0130 D1)', + }, + dependency: { + conflict: 'between packages that share a namespace through a declared dependency', + relation: 'which shares the namespace because one of the two declares the other in `manifest.dependencies`', + allowed: 'Sharing a namespace through a declared dependency is allowed (ADR-0141 D5)', + }, + sibling: { + conflict: 'between sibling packages that share a namespace under one declared parent', + relation: 'which shares the namespace because both declare the same installed package in `manifest.dependencies`', + allowed: 'Sharing a namespace between modules of one declared parent is allowed (ADR-0141 D5)', + }, +}; + +/** One object {@link PackageUninstallExtendedError} names, with the packages extending it. */ +interface ExtendedObjectOfUninstall { + /** The object's fully-qualified name, as the registry keys it. */ + readonly objectName: string; + /** Every OTHER package contributing an `extend` to it, in registration order. */ + readonly extenders: readonly string[]; +} + +/** How many extended objects the refusal spells out before it counts the rest. */ +const EXTENDED_OBJECTS_LISTED = 5; + +/** + * Separator between an item's name and its bundle discriminator inside a + * storage key: `pkg:auth.welcome@zh-CN`. + * + * `@` is unambiguous here in both directions. A discriminated type's `name` can + * never contain it (`EmailTemplateDefinitionSchema` pins dotted snake_case), + * and a scoped package id that does (`@acme/crm:auth.welcome@zh-CN`) is still + * parsed correctly because {@link bundleBaseKey} only looks for the separator + * AFTER the last `:`. + */ +const BUNDLE_KEY_SEPARATOR = '@'; + +/** `pkg:auth.welcome` + `zh-CN` → `pkg:auth.welcome@zh-CN`. */ +function withDiscriminator(baseKey: string, value: string): string { + return `${baseKey}${BUNDLE_KEY_SEPARATOR}${value}`; +} + +/** + * Strip a storage key's bundle suffix: `pkg:auth.welcome@zh-CN` → + * `pkg:auth.welcome`. Keys of undiscriminated types come back unchanged, and so + * does a scoped package id's own `@` (the search starts after the last `:`). + */ +function bundleBaseKey(key: string): string { + const at = key.indexOf(BUNDLE_KEY_SEPARATOR, key.lastIndexOf(':') + 1); + return at === -1 ? key : key.slice(0, at); +} + +/** One member of an i18n bundle, as stored. */ +type BundleEntry = { key: string; item: any }; + +/** + * [#33127, ADR-0150 D4 "registry overlay slot (ADR-0005)"] The mark that opens + * and separates a package overlay slot's storage key: the slot of the stored + * row of `(type, name[, member])` bound to one package, beside the bare slot + * (a package-less row) and the composite slots (code artifacts). A NUL never + * occurs in a package id or a name, so no reader of the other two key forms + * mistakes this one for its own: it is never a bare key (`name`, `name@member`) + * and never ends `:name` ({@link bundleBaseKey}, {@link collectBundle}, the + * composite scans), so it adds no holder and is no artifact. + */ +const OVERLAY_SLOT_MARK = '\u0000'; + +/** `com.acme.alpha` + `auth.welcome@zh-CN` → the package overlay slot key of that member. */ +function overlaySlotKey(packageId: string, bareKey: string): string { + return `${OVERLAY_SLOT_MARK}${packageId}${OVERLAY_SLOT_MARK}${bareKey}`; +} + +/** The package and the bare key of a package overlay slot key, or `undefined` for any other key. */ +function parseOverlaySlotKey(key: string): { packageId: string; bareKey: string } | undefined { + if (!key.startsWith(OVERLAY_SLOT_MARK)) return undefined; + const sep = key.indexOf(OVERLAY_SLOT_MARK, 1); + return sep < 0 ? undefined : { packageId: key.slice(1, sep), bareKey: key.slice(sep + 1) }; +} + +/** + * [#33127] The package overlay slots of `(name)` in `collection`, every member + * of a bundled type (`disc`), in the store's order: the stored rows bound to a + * package, one slot per row package. + */ +function overlaySlotEntriesOf(collection: Map, name: string, disc?: { field: string; canonical: string }): BundleEntry[] { + const out: BundleEntry[] = []; + for (const [key, item] of collection) { + const slot = parseOverlaySlotKey(key); + if (slot === undefined) continue; + if ((disc ? bundleBaseKey(slot.bareKey) : slot.bareKey) === name) out.push({ key, item }); + } + return out; +} + +/** The package a bare-slot or package overlay slot entry's stored row is bound to: the slot's package, else the entry's stamp. */ +function storedRowPackageOf(entry: BundleEntry): string | undefined { + const slot = parseOverlaySlotKey(entry.key); + if (slot !== undefined) return slot.packageId; + const stamped = (entry.item as { _packageId?: unknown } | null | undefined)?._packageId; + return typeof stamped === 'string' && stamped !== '' && stamped !== 'sys_metadata' ? stamped : undefined; +} + +/** + * [ADR-0150 D4] The mark that opens and separates a package-less action's + * owner-key slot. An action is identified by `(owner, owning object, name)`, + * so the platform's action bound to `lead` and the environment's object-less + * action of the same name are two items under two keys, never one contested + * name. The bare slot `name` holds the package-less action registered last; + * each package-less action of another owner key it displaced keeps its own + * slot under this mark, one per owner key. Like {@link OVERLAY_SLOT_MARK}, a + * control character never occurs in an object or action name, so no reader of + * the other key forms mistakes this one for its own: it is never a bare key + * and never ends `:name`, so it adds no package holder and is no artifact. + */ +const PACKAGELESS_ACTION_SLOT_MARK = '\u0001'; + +/** [ADR-0150 D4] A package-less action's owner key: its object, or `global` for every object-less spelling. */ +function packagelessActionOwnerKey(action: unknown): string { + const owner = standaloneActionOwnerKey(action); + return isObjectLessActionKey(owner) ? GLOBAL_ACTION_OBJECT_KEY : owner; +} + +/** `lead` + `approve` → the owner-key slot of the package-less `approve` bound to `lead`. */ +function packagelessActionSlotKey(ownerKey: string, name: string): string { + return `${PACKAGELESS_ACTION_SLOT_MARK}${ownerKey}${PACKAGELESS_ACTION_SLOT_MARK}${name}`; +} + +/** + * [ADR-0150 D4] The owner-key slots of the package-less action `name` + * ({@link packagelessActionSlotKey}), in the store's order: the package-less + * actions of the name beside the one in the bare slot, one per owner key. + */ +function packagelessActionSlotEntriesOf(collection: Map, name: string): BundleEntry[] { + const out: BundleEntry[] = []; + for (const [key, item] of collection) { + if (!isPackagelessActionSlotKey(key)) continue; + const sep = key.indexOf(PACKAGELESS_ACTION_SLOT_MARK, 1); + if (sep > 0 && key.slice(sep + 1) === name) out.push({ key, item }); + } + return out; +} + +/** Is `key` a package-less action's owner-key slot ({@link packagelessActionSlotKey})? */ +function isPackagelessActionSlotKey(key: string): boolean { + return key.startsWith(PACKAGELESS_ACTION_SLOT_MARK); +} + +/** + * [ADR-0150 D4] Is a package-less action registration a stored row — the + * environment's `sys_metadata` hydration or write-through — rather than code + * the host or a plugin registered with no package? A stored row is + * tenant-authored (ADR-0010 `_provenance: 'org'`), or wears the envelope of + * the package whose action it customizes (`_packageId`, which code registering + * with no package never carries). + */ +function isStoredActionRow(item: unknown): boolean { + if (isTenantAuthored(item)) return true; + const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; + return typeof stamped === 'string' && stamped !== ''; +} + +/** + * [#32408, contract review F2, delta review N1] What a door's registration + * brings to a bundled name: the package and the members it adds to its + * composite slot. + */ +interface BundleArrival { + readonly packageId: string; + readonly members: ReadonlySet; +} + +/** + * `collection` with `arrival`'s members in its composite slot of `name`, as a + * copy (the registry's own map is never touched): a member already stored is + * kept as stored, a new one is a stub carrying its discriminator, which is + * all a holder's member read takes from it. + */ +function withArrivingMembers( + collection: Map, + name: string, + disc: { field: string; canonical: string }, + arrival: BundleArrival, +): Map { + const copy = new Map(collection); + for (const member of arrival.members) { + const key = withDiscriminator(`${arrival.packageId}:${name}`, member); + if (!copy.has(key)) copy.set(key, { name, [disc.field]: member }); + } + return copy; +} + +/** + * The members of `(type, name)`'s bundle, grouped by the SAME precedence tiers + * {@link SchemaRegistry.getItem} applies to an undiscriminated type — so + * bundling changes WHICH ROWS EXIST, never which layer wins: + * + * - `bare` — bare-key group (`name@`): the ADR-0005 runtime/DB overlay + * - `local` — the `currentPackageId` composite group (ADR-0048 prefer-local) + * - `other` — every other composite group, in Map insertion order + * + * Within a tier the `canonical` member is first, then insertion order — so a + * bare-name read of a bundle is decided by the spec's declared fallback locale + * rather than by which locale the author happened to declare first. + */ +function collectBundle( + collection: Map, + name: string, + disc: { field: string; canonical: string }, + currentPackageId?: string, +): { bare: BundleEntry[]; local: BundleEntry[]; other: BundleEntry[] } { + const bare: BundleEntry[] = []; + const local: BundleEntry[] = []; + const other: BundleEntry[] = []; + const localBase = currentPackageId ? `${currentPackageId}:${name}` : undefined; + for (const [key, item] of collection) { + const base = bundleBaseKey(key); + if (base === name) bare.push({ key, item }); + else if (base.endsWith(`:${name}`)) (localBase && base === localBase ? local : other).push({ key, item }); + } + const canonicalFirst = (entries: BundleEntry[]) => { + const idx = entries.findIndex((e) => discriminatorValue(e.item, disc.field) === disc.canonical); + return idx <= 0 ? entries : [entries[idx], ...entries.filter((_, i) => i !== idx)]; + }; + return { bare: canonicalFirst(bare), local: canonicalFirst(local), other: canonicalFirst(other) }; +} + +/** + * [#22877] `entries` narrowed to ONE member of their bundle, order kept. An + * entry's member is its discriminator value, or the canonical member for an + * item that declares none — the value {@link SchemaRegistry.registerItem} keys + * it by. `member` is a discriminator value as a caller names it (`'zh-CN'`); a + * blank one names the canonical member, as `itemDiscriminator` reads a body. + */ +function entriesOfMember( + entries: readonly BundleEntry[], + disc: { field: string; canonical: string }, + member: string, +): BundleEntry[] { + const wanted = member.trim() || disc.canonical; + return entries.filter((e) => (discriminatorValue(e.item, disc.field) || disc.canonical) === wanted); +} + +/** + * [#31575] One answer out of bundle `entries` already in tier order, as + * {@link SchemaRegistry.getItem} picks it: the named `member`, else the + * canonical member, else the first entry. + */ +function pickBundleMember( + entries: readonly BundleEntry[], + disc: { field: string; canonical: string }, + member: string | undefined, +): T | undefined { + if (member !== undefined) return entriesOfMember(entries, disc, member)[0]?.item as T | undefined; + return (entriesOfMember(entries, disc, disc.canonical)[0] ?? entries[0])?.item as T | undefined; +} + +/** + * `schema` without the field `name` — `schema` itself, by reference, when it + * does not carry one. Never mutates its input. + */ +function withoutField(schema: ServiceObject, name: string): ServiceObject { + const fields = schema.fields as Record | undefined; + if (!fields || fields[name] === undefined) return schema; + const { [name]: _dropped, ...rest } = fields; + return { ...schema, fields: rest } as ServiceObject; +} + +/** + * [#33050] One item slot (or one claimed name) of a package body, as + * {@link SchemaRegistry.replacingPackageBody} records it: the type and the key, + * joined by a character no type name carries. + */ +function packageBodySlot(type: string, key: string): string { + return `${type}\u0000${key}`; +} + +/** + * [#33593] How the follow door is asked of one layer + * ({@link SchemaRegistry.recordCurrencyFollowRefusalOf}): its slot's package, + * the priority it sorts at (the slot's, when omitted), and `before` — what the + * slot held before the change being judged, `null` for an empty slot. + */ +interface FollowDoorLayer { + readonly packageId: string | undefined; + readonly priority?: number; + readonly before: ObjectContributor | null; +} + +/** + * One object layer a registration run registered: the body its registration's + * doors judged, and the contributor it became — the registry's own entry, so + * the run's end can tell a layer a later write of the run took out again, and + * reads the kind the layer holds by then. + */ +interface RunRegisteredObject { + readonly fqn: string; + readonly schema: ServiceObject; + readonly contributor: ObjectContributor; + /** + * What the layer's slot held before the run's first write to it, or `null` + * when it was empty: the run's end judges the layer against the object as + * it was served with this in its place, so a pair the run leaves as it + * found it is not the run's to answer for (#33593 ruling R7). + */ + readonly replaced: ObjectContributor | null; +} + +/** + * [#31697] Does one of an action's stored environment rows (the protocol's + * by-name row read: active, the environment's own) belong to no package and + * bind no object — the row the environment's own object-less item comes + * from? Its `package_id` column decides the binding; its body's owner key + * ({@link standaloneActionOwnerKey}) the object. A body that does not parse + * is no such row. + */ +function storesObjectLessPackagelessRow(rows: readonly { package_id?: string | null; metadata?: unknown }[]): boolean { + return rows.some((row) => { + if (row?.package_id !== null && row?.package_id !== undefined && row?.package_id !== '') return false; + let body: unknown; + try { + body = typeof row?.metadata === 'string' ? JSON.parse(row.metadata) : row?.metadata; + } catch { + return false; + } + return body !== null && typeof body === 'object' && isObjectLessActionKey(standaloneActionOwnerKey(body)); + }); +} + +// [#33949 codemod] Declared above without `export`, and read by `registry.ts` or a section module. +export { + BundleArrival, BundleEntry, CO_OWNERSHIP_WORDING, EXTENDED_OBJECTS_LISTED, ExtendedObjectOfUninstall, + FoldedExtensionFields, FollowDoorLayer, NO_PACKAGE_REFERRER, OBJECT_FOLD_SCALAR_KEYS, OVERLAY_SLOT_MARK, + ObjectExtensionBody, ObjectFoldScalarKey, PicklistItemLike, PicklistReferrer, QUEUE_FIELD, RunRegisteredObject, + SECURITY_CATALOG_NAME_RULE, ServedObjectLayer, TENANT_SCOPE_INDEX, TenantRuleSettings, WithheldExtensions, + admittedFieldPatches, bareNameConflictLine, bareNameReasonFor, bundleBaseKey, changedPackagedRuleKeys, + coldBootConflictMessage, collectBundle, declaredOwnedObjectNames, displayKeyOverridesPackagedBase, entriesOfMember, + fieldNamesOf, fieldPatchYieldKey, indexKeysOf, isLeftOutExtension, isPackagelessActionSlotKey, isPlainRecord, + isShareableNamespace, isStoredActionRow, leavesExtensionsOut, mergeObjectDefinitions, mergeValidationsByName, + optionByValue, overlaySlotEntriesOf, overlaySlotKey, ownFieldNamesOf, ownFieldsTarget, packageBodySlot, + packagelessActionOwnerKey, packagelessActionSlotEntriesOf, packagelessActionSlotKey, parseOverlaySlotKey, + parsePackageAction, parsePackageObjectActions, pickBundleMember, picklistReferrerOf, provisionTenantScopeIndex, + recordFieldOf, ruleNamesOf, sameActionDeclaration, sameHolder, sortActionsByOrder, stableStringify, + storedRowPackageOf, storesObjectLessPackagelessRow, tenantRuleSettingsOf, toRecordManifest, validationRuleName, + withArrivingMembers, withDeclaredFunctionEntries, withDiscriminator, withTenantRuleSettings, withoutField, + withoutWithheldMembers, +}; diff --git a/packages/objectql/src/registry.ts b/packages/objectql/src/registry.ts index f7679f63329..3b06e9ff690 100644 --- a/packages/objectql/src/registry.ts +++ b/packages/objectql/src/registry.ts @@ -1,72 +1,41 @@ // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. -import { ServiceObject, ObjectSchema, ObjectOwnership, provisionPrimary, resolveInjectedSystemColumns, resolveInjectedColumnProvenance as injectedColumnProvenance, isInjectedColumnDefinition, isTenancyDisabled, checkManagedApiMethodAffordances, MANAGED_BY_WRITE_LOCKED_BUCKETS, LEGACY_API_METHODS, AUDIT_PROVENANCE_FIELDS, materializeTreeSelfReferences, BASE_FIELD_DISPLAY_WHITELIST, type ObjectExtensionParsed } from '@objectstack/spec/data'; +import { ServiceObject, ObjectOwnership, resolveInjectedSystemColumns, resolveInjectedColumnProvenance as injectedColumnProvenance } from '@objectstack/spec/data'; // [#4513] The audit-family governance table, and [#6562] the injected-column // DEFINITION tables it governs — see the re-exports below for why both live in a // package `objectql` and `metadata-protocol` both depend on. import { AUDIT_FIELD_GOVERNANCE, - applyAuditFieldGovernance, AUDIT_FIELD_DEFS, TENANT_SCOPE_FIELD_DEF, OWNER_FIELD_DEF, OWNING_BUSINESS_UNIT_FIELD_DEF, QUEUE_FIELD_DEF, - // [#7774] The i18n-bundle identity table and its value reader, sunk into - // metadata-core so this registry and the `/meta` list merge in - // `@objectstack/metadata-protocol` answer "what is this type's identity?" - // from one place. Re-exported below under its original name. ITEM_KEY_DISCRIMINATORS, readDiscriminatorValue as discriminatorValue, - // [commit fa5d137ab] The ADR-0029 D9.6 provenance pair, sunk into metadata-core for the - // same reason as the table above: `@objectstack/service-automation`'s flow - // precedence asks the same question ("does a code package ship this name?") - // and reached it by importing this package, which it does not declare — so - // the bundler inlined a copy of this implementation into ITS dist. Both names - // keep their original spelling here, and `isCodeArtifactBody` is re-exported - // below, so this package's public API is unchanged. isCodeArtifactBody, isTenantAuthored, - // [#16319] The field-`type` admission vocabulary — the predicate, the shared - // sentence and the discriminator name. It lives one package down for the - // ordinary reason: the reporting seam is `@objectstack/metadata-protocol`'s - // `loadMetaFromDb`, which THIS package depends on, so the reverse import is a - // cycle. See the module's own header. describeUndeclarableFieldType, findUndeclarableFieldType, OBJECT_FIELD_TYPE_REFUSED_ERROR_NAME, type ObjectFieldTypeRefusal, type ObjectFieldTypeViolation, - // [ADR-0146 D10, second rule] The field-patch target check — the install - // door's and `os validate`'s judge, asked here so the fold names a patch it - // cannot apply by the same verdict those doors refuse it by. - fieldPatchTargetMisses, type FieldPatchTargetMiss, - // [ADR-0146 D3 · ruling record 6691022506] The pair of a package, looked up - // through the App that carries it — the uninstall refusal's "is its pair - // installed?" asks it, over this registry's installed manifests. pairsOf, installedManifestsOf, - // [ADR-0150 D4, #32408] …and which holders a pair's row is the - // customization of, the way the exact read's owner check reads it. appOf, isPairOf, pairOf, - // [ADR-0150 D3, #28802] The one closure walk: a dependent's bundle addition - // under a dependency's name is no holder of that name for a bare read. packageDependencyClosure, } from '@objectstack/metadata-core'; // [ADR-0029 D9.2a] `scalarOverridesPackagedBase` is the #8284 comparison, imported rather // than re-spelled: the object FOLD asks the same question one layer down (has // this scalar been authored away from the packaged default?), and the ruling // required the same mechanism, not a second comparison shape. -import { SystemFieldName, scalarOverridesPackagedBase } from '@objectstack/spec/system'; import { resolveTenancyPosture, resolveSearchPinyinEnabled } from '@objectstack/types'; import { postureEnforcesWall } from '@objectstack/spec/security'; -import { provisionSearchCompanion, SEARCH_COMPANION_FIELD } from './search-companion.js'; -import { ObjectStackManifest, ManifestSchema, InstalledPackage, InstalledPackageSchema, InstalledPackageSourceSchema, type InstalledPackageSource, checkFieldCompleteness, customizationBasePackageId, findNamespaceObjectNameClashes, type NamespaceObjectNameClash, type ObjectNameHolder, type NamespaceHolder, validateNamespaceNotAddressLiteral } from '@objectstack/spec/kernel'; -import { DEFAULT_FLOW_FUNCTION_EFFECT } from '@objectstack/spec/automation'; -import { AppSchema, ActionSchema } from '@objectstack/spec/ui'; +import { provisionSearchCompanion } from './search-companion.js'; +import { ObjectStackManifest, InstalledPackage, type InstalledPackageSource, customizationBasePackageId, type NamespaceObjectNameClash } from '@objectstack/spec/kernel'; import { applyProtection } from '@objectstack/spec/shared'; // [ADR-0130 D1] The ONE derivation of the key a package is installed under // (`id || name`). The install gate's co-ownership set must name packages by the @@ -77,34 +46,20 @@ import { artifactPackageId, declaredDependencyIds, PackageScopedNameRefusedError // rule, the holders and the one place a declaration is read from (module doc). import { BUILT_IN_SECURITY_CATALOG_NAMES, - declaredSecurityCatalogNames, describeSecurityCatalogHolder, - ENVIRONMENT_HELD_SECURITY_CATALOG_TYPES, isSecurityCatalogType, - securityCatalogHolderKey, securityCatalogTypeLabel, type SecurityCatalogHolder, } from './security-catalog-namespace.js'; // One name, one package holder for the bare-name types whose consumers resolve // without package context — the rule, the holders, the cold boot (module doc). import { - BUILT_IN_BARE_NAMES, - bareNameHolderKey, - bareNameReason, - bareNameTypeLabel, - BARE_NAME_ONE_HOLDER_TYPES, - bareNameHoldersMeet, - declaredBareNames, declaredBundleMembers, - isBundleAdditionPair, type BundleHolding, - declaredBuiltInBoundActions, - describeBareNameHolder, handlerOwnerPackageId, holdsBareName, isBareNameRegistryType, isOverridableBaseline, - suggestedBareNameRename, type BareNameColdBootMode, type BareNameHolder, type BareNameOneHolderType, @@ -116,10 +71,7 @@ import { isPackageScopedReadType, readPackageScopedItem } from './package-scoped // uncovered bucket 2/3 types has one package holder — the one predicate the // package door and `os validate` both ask (module doc). import { - isNamespaceJudged, namespaceNameConflictMessage, - namespaceNameConflicts, - namespacePackageOf, type NamespaceNameConflict, type NamespacePackage, type NamespaceUniqueType, @@ -127,14 +79,9 @@ import { // One default record page per object per deployment — the holders and the one // place a manifest's default record pages are read from (module doc). import { - declaredDefaultRecordPages, - misplacedDefaultPages, type MisplacedDefaultPage, - defaultRecordPageHolderKey, describeDefaultRecordPageHolder, type DefaultRecordPageHolder, - holdsRecordPageSlot, - recordPageSlotKey, recordPageSlotsOf, type RecordPageSlot, } from './default-record-page.js'; @@ -145,9 +92,6 @@ import { misplacedDefaultPageRefusal } from '@objectstack/spec/ui'; // fold cannot disagree about what a missing group is — the drift that would // make the build's silence indistinguishable from the silence being fixed. import { - findNavGroup as resolveNavGroup, - navContributionGroupDiagnostic, - formatNavContributionGroupDiagnostic, type NavContributionGroupDiagnostic, } from './nav-contribution-diagnostics.js'; // The shared-picklist judgments (merge, served shape, unknown-name refusal) — @@ -155,15 +99,8 @@ import { // spelling of each. See the module header. import { collectManifestPicklistExtensions, - collectManifestPicklistNames, collectManifestPicklistReferences, collectPicklistReferences, - describePicklistReferencesBrokenByInstall, - describeUnresolvedPicklistReferences, - duplicatePicklistValueError, - findDuplicatePicklistValue, - mergePicklistOptions, - resolvePicklistFieldsOnto, type PicklistContribution, type PicklistOptionLike, type DisagreeingPicklistReference, @@ -186,1126 +123,73 @@ import { standaloneActionOwnerKey, isObjectLessActionKey, GLOBAL_ACTION_OBJECT_K import { actionOwnerScopedRegistry, resolveActionByPackage } from './action-package-addressing.js'; import { RecordCurrencyFollowRefusedError, - recordCurrencyFollowRefusal, - recordCurrencyFollowRefusalOverEnvironmentObjects, - recordCurrencySummaryRefusal, } from './record-currency-aggregate-door.js'; - -/** - * [#24400] A bound top-level action whose target object no package has - * registered — see {@link SchemaRegistry.findUnboundActionTargets}. - */ -export interface UnboundActionTarget { - /** The action's `name`. */ - action: string; - /** The object its `objectName` names. */ - objectName: string; - /** The package that ships the action. */ - packageId: string | undefined; -} - -/** The boot refusal's error: the `INVALID_METADATA` envelope (ADR-0112) the picklist refusal also answers. */ -export interface UnboundActionTargetError extends Error { - code: 'INVALID_METADATA'; - status: 422; - httpStatus: 422; -} - -/** - * [#24400] The refusal for bound top-level actions whose target object no - * loaded package registers — every one of them, so an author fixes the set in - * one pass. `undefined` when there are none. - * - * Refused rather than dropped: such an action registers, is listed on no - * object, and no client renders it — the silent drop the extension guidance - * would otherwise lead an author into. - */ -export function describeUnboundActionTargets( - unbound: readonly UnboundActionTarget[], -): UnboundActionTargetError | undefined { - if (unbound.length === 0) return undefined; - const lines = unbound.map( - (u) => `action '${u.action}' (${u.packageId ? `package '${u.packageId}'` : 'a registration with no package'}) ` - + `is bound to object '${u.objectName}'`, - ); - const err = new Error( - `${lines.length === 1 ? 'A top-level action is' : `${lines.length} top-level actions are`} bound by ` - + `\`objectName\` to an object that no loaded package registers: ${lines.join('; ')}. A bound action ` - + 'is attached to its object when the object is served, so with no such object it would be listed ' - + 'nowhere. Register the object (in the package or in one it depends on), or correct the ' - + '`objectName`.', - ) as UnboundActionTargetError; - err.code = 'INVALID_METADATA'; - err.status = 422; - err.httpStatus = 422; - return err; -} - -/** - * [#25649] The refusal of a code package's `action` that does not satisfy - * `ActionSchema` — the `INVALID_METADATA` envelope (ADR-0112) the unbound-target - * refusal above answers, with `issues[]` locating each failure. - */ -interface PackageActionSchemaError extends Error { - code: 'INVALID_METADATA'; - status: 422; - httpStatus: 422; - issues: Array<{ path: string; message: string; code: string }>; -} - -/** - * [#25649] A code package's `action` item, as its `ActionSchema` parse — the - * body {@link SchemaRegistry.registerItem} stores — or the loud refusal of one - * that does not parse. - * - * Why the stored item has to BE the parse: `ActionSchema` is not only a judge, - * it is a producer. Its transform lowers `requiresMembershipReach` and - * `requiresFeature` into the CEL `visible` predicate and deletes the two keys, - * and nothing downstream reads either key raw — so an action stored as authored - * carries both gates declared and inert. A platform object's actions are parsed - * where they are authored (`ObjectSchema.create`), and a strict `defineStack` - * parses a stack's actions; a programmatic `registerApp`, a nested plugin's - * manifest, a `strict: false` stack or a raw plugin manifest reached this store - * with no parse at all, and {@link SchemaRegistry.attachBoundActions} served - * what it found. Parsing at this seam, once, means every reader — the bound - * attachment, the router's rung 2, `GET /meta/action` — holds the one parsed - * body, by reference. - * - * The parse is idempotent over its own output (a lowered action carries no - * `requiresFeature` left to lower), so an action a strict `defineStack` already - * parsed is stored deep-equal to what it arrived as. Besides the lowering, the - * parse fills the schema's defaults (`type`, `refreshAfter`, …), turns a - * CEL-string shorthand into its `{ dialect, source }` envelope, and refuses an - * undeclared key; the ADR-0010 envelope (`_packageId`, `_packageVersion`, - * `_provenance`, `_lock`) is declared on the schema and passes through. - * - * Refused rather than stored unparsed: an action that fails the parse would be - * served with whatever gates it declares never taking effect, and `defineStack` - * and `os validate` refuse the same declaration at authoring time. - * - * [#25744] The one parse for both places a code package declares an action: - * a standalone `action` item ({@link SchemaRegistry.registerItem}) and an - * object's own `actions` ({@link parsePackageObjectActions}, called by - * {@link SchemaRegistry.registerObject}). `owner` names the object an own - * action is declared on, so the refusal says which object and `issues[]` - * locates each failure inside its body. - */ -function parsePackageAction( - item: unknown, - name: string, - packageId: string, - owner?: { object: string; index: number }, -): unknown { - const parsed = ActionSchema.safeParse(item); - if (parsed.success) return parsed.data; - // [#25744] An object's own action is located inside the object body it was - // declared in (`actions..`), the path `ObjectSchema` reports. - const prefix = owner ? ['actions', String(owner.index)] : []; - const issues = parsed.error.issues.map((issue) => ({ - path: [...prefix, ...issue.path.map(String)].join('.'), - message: issue.message, - code: issue.code, - })); - const subject = owner - ? `Action '${name}' of object '${owner.object}' in package '${packageId}'` - : `Action '${name}' of package '${packageId}'`; - const err = new Error( - `${subject} does not satisfy the action contract (ActionSchema): ` - + issues.map((issue) => (issue.path ? `${issue.path}: ${issue.message}` : issue.message)).join('; ') - + '. Every action a package ships is stored as its schema parse — the parse that lowers ' - + '`requiresFeature` and `requiresMembershipReach` into `visible` — so an action that does not parse ' - + 'is refused rather than served with its declared gates inert. Correct the declaration; `defineStack` ' - + 'and `os validate` report the same issues.', - ) as PackageActionSchemaError; - err.code = 'INVALID_METADATA'; - err.status = 422; - err.httpStatus = 422; - err.issues = issues; - throw err; -} - -/** - * [#25744] A code package's object, its own `actions` stored as their - * `ActionSchema` parse ({@link parsePackageAction}) — or the loud refusal of - * the first one that does not parse. - * - * The object-path twin of {@link SchemaRegistry.registerItem}'s action parse. - * `ObjectSchema`'s `actions` is `ActionSchema[]`, so a platform object - * (`ObjectSchema.create`) and a strict `defineStack` already hand this method - * parsed actions, and the parse being idempotent over its own output stores - * them deep-equal to what arrived. A raw manifest, a `strict: false` stack or a - * programmatic `registerApp` reached the store with its objects' actions as - * written, so a declared `requiresFeature` / `requiresMembershipReach` was - * served raw, never lowered into `visible`. - * - * Returns `schema` by reference when it declares no `actions` array, and a - * shallow copy carrying the parsed array otherwise: the caller's body is - * never mutated. - */ -function parsePackageObjectActions(schema: ServiceObject, packageId: string): ServiceObject { - const actions = (schema as { actions?: unknown }).actions; - if (!Array.isArray(actions) || actions.length === 0) return schema; - const parsed = actions.map((action, index) => { - const declared = (action as { name?: unknown } | null | undefined)?.name; - const name = typeof declared === 'string' && declared !== '' ? declared : `#${index}`; - return parsePackageAction(action, name, packageId, { object: schema.name, index }); - }); - return { ...schema, actions: parsed } as ServiceObject; -} - -/** - * [#24472] The key {@link SchemaRegistry}'s bound-action collision audit is - * carried under. A symbol, and ⛔ deliberately NOT re-exported from the package - * entry (`index.ts`, `core.ts`), so the audit is no NAMEABLE member of the - * registry's published face: only the ObjectQL plugin's boot and late-artifact - * reports reach it, importing the key from this module. - */ -export const FIND_BOUND_ACTION_COLLISIONS: unique symbol = Symbol('objectstack.objectql.findBoundActionCollisions'); - -/** - * [#24472] A bound top-level action that its target object does NOT list, - * because the object already lists a DIFFERENT declaration under the same - * name — see {@link FIND_BOUND_ACTION_COLLISIONS}. - */ -export interface BoundActionCollision { - /** The object both declarations are for. */ - objectName: string; - /** The action name they share. */ - action: string; - /** The package whose declaration the object serves — its owner, for the object's own action. */ - servedPackageId: string | undefined; - /** The package whose bound declaration the object does not list. */ - shadowedPackageId: string | undefined; -} - -const describePackage = (packageId: string | undefined): string => - packageId ? `package '${packageId}'` : 'a registration with no package'; - -/** - * [#24472] The author-visible warning for one {@link BoundActionCollision}. - * Reported, never refused: a refusal would be a narrowing, and the decision - * for this collision is to report it first. - */ -export function describeBoundActionCollision(collision: BoundActionCollision): string { - const { objectName, action, servedPackageId, shadowedPackageId } = collision; - return `Action '${action}' on object '${objectName}' is declared twice, and the two declarations differ: ` - + `${describePackage(servedPackageId)} declares the one the object serves, and ` - + `${describePackage(shadowedPackageId)} binds another by \`objectName\`, which the object does not list. ` - + `Both register their handler under the one key '${objectName}:${action}', so when both carry one, the ` - + 'handler that runs is whichever package registered last — not necessarily the declaration the object ' - + 'lists. Rename one of the two actions, or drop the duplicate declaration.'; -} - -/** - * Keys that say where an action declaration came from or where it sits, not - * what it declares: the registry's stamps (`_packageId`, `_provenance`, the - * `_lock` envelope…), the `protection` block those stamps translate, and - * `objectName`, which an object's own action leaves to its position. - */ -const isActionProvenanceKey = (key: string): boolean => - key.startsWith('_') || key === 'protection' || key === 'objectName'; - -/** - * [#24472] Are two action declarations the SAME declaration — one carried - * twice (a same-stack merge's copy beside the top-level item, one declaration - * shipped by two packages, a copy re-bridged under another package's stamp)? - * Structural equality minus {@link isActionProvenanceKey} keys at the top - * level; a key set to `undefined` reads as absent, callables compare by - * reference, and past depth 32 the answer is "different" — the reported side. - */ -function sameActionDeclaration(a: unknown, b: unknown, depth = 0): boolean { - if (Object.is(a, b)) return true; - if (depth >= 32 || typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false; - if (Array.isArray(a) !== Array.isArray(b)) return false; - if (Array.isArray(a)) { - const other = b as unknown[]; - return a.length === other.length && a.every((item, i) => sameActionDeclaration(item, other[i], depth + 1)); - } - const left = a as Record; - const right = b as Record; - const keysOf = (o: Record) => - Object.keys(o).filter((k) => o[k] !== undefined && (depth > 0 || !isActionProvenanceKey(k))); - const leftKeys = keysOf(left); - return leftKeys.length === keysOf(right).length - && leftKeys.every((k) => right[k] !== undefined && sameActionDeclaration(left[k], right[k], depth + 1)); -} - -/** A manifest and its nested `plugins[]`, which register under its package (`registerPlugin`). */ -function manifestAndNestedPlugins(manifest: any): any[] { - const plugins = Array.isArray(manifest?.plugins) - ? manifest.plugins.filter((p: unknown) => p !== null && typeof p === 'object') - : []; - return [manifest, ...plugins]; -} - -/** - * [#24400] The bound top-level actions one manifest brings — its own - * `actions` and its nested plugins', each with the object it is bound to — - * read BEFORE the manifest registers, by the post-boot install door. - */ -export function collectManifestBoundActions(manifest: any, packageId: string | undefined): UnboundActionTarget[] { - const out: UnboundActionTarget[] = []; - for (const source of manifestAndNestedPlugins(manifest)) { - for (const action of Array.isArray(source?.actions) ? source.actions : []) { - if (!action || typeof action !== 'object' || typeof action.name !== 'string') continue; - const objectName = standaloneActionOwnerKey(action); - if (isObjectLessActionKey(objectName)) continue; - out.push({ action: action.name, objectName, packageId }); - } - } - return out; -} - -/** [#24400] The object names one manifest declares, nested plugins included, in either authored spelling. */ -export function collectManifestObjectNames(manifest: any): string[] { - const names: string[] = []; - for (const source of manifestAndNestedPlugins(manifest)) { - const objects = source?.objects; - if (Array.isArray(objects)) { - for (const o of objects) if (typeof o?.name === 'string') names.push(o.name); - } else if (objects && typeof objects === 'object') { - names.push(...Object.keys(objects)); - } - } - return names; -} - -/** - * Stable sort by `order` (unset reads as `0`), by reference when no action - * sets one — the rule `defineStack()` applies to each object's merged actions - * (`sortActionsByOrder` in `@objectstack/spec`'s stack builder, which is - * internal there), applied here to the attached actions among themselves. - */ -function sortActionsByOrder(actions: T[]): T[] { - if (!actions.some((a) => a?.order !== undefined)) return actions; - return actions.slice().sort((a, b) => (a?.order ?? 0) - (b?.order ?? 0)); -} - -/** - * Reserved namespaces that do not get FQN prefix applied. - * Objects in these namespaces keep their short names (e.g., "user" — short name IS the canonical key). - */ -export const RESERVED_NAMESPACES = new Set(['base', 'system']); - -/** - * Default priorities for the three contributor kinds. - * - * [ADR-0029 D9.3] `DEFAULT_OVERLAY_PRIORITY` sits BETWEEN the owner and the - * extenders so a `getObjectContributors()` read lists the stack in layer - * order. It is ORDERING ONLY: {@link SchemaRegistry.resolveObject} selects its - * base layer by KIND, never by "highest priority wins" — extender priority is - * author-declared (`ext.priority ?? 200` in the `objectExtensions` loop), so a - * package could otherwise re-rank a tenant's overlay out of the base slot by - * declaring `priority: 140`. Declared numbers order PEERS; they must not be - * able to change WHICH LAYER IS THE BASE. - */ -export const DEFAULT_OWNER_PRIORITY = 100; -export const DEFAULT_OVERLAY_PRIORITY = 150; -export const DEFAULT_EXTENDER_PRIORITY = 200; - -/** - * Contributor Record - * Tracks how a package contributes to an object. - * - * Three kinds (ADR-0029 D9): - * - * - `own` — the single owning package. Defines the base schema, the table and - * the namespace claim. Exactly one per object name, unconditionally - * ({@link SchemaRegistry.assertSingleOwnerPerObject}). - * - `overlay` — [D9.1] a TENANT customization layer over the owner's - * definition, hydrated from a `sys_metadata` row. It REPLACES the base layer - * at resolution time (D9.2) and owns nothing: it claims no namespace, - * decides no package membership, holds no table. That is why the - * single-owner assertion needs no "unless it is an overlay" clause — which - * matters beyond tidiness, since ADR-0028 D5/D6 rest on that sentence being - * unconditional. `ownership: 'overlay'` is LOADER-SET at the hydration seams - * and reachable from no authoring surface. - * - `extend` — additive contributions from other packages, folded on top of - * whichever layer is the base. - */ -export interface ObjectContributor { - // Optional (#12623): `registerObject`'s own `packageId` parameter is now - // optional, and this is the exact value it was called with — a bare - // `registerObject(schema)` call stores `undefined` here, matching the - // runtime behavior every call site without a tsc test-layer gate already - // exercised (JS does not enforce TS arity). - packageId: string | undefined; - namespace: string; - ownership: ObjectOwnership; - priority: number; - definition: ServiceObject; -} - -/** - * Compute canonical registry key for an object. - * - * Under the current naming convention, object names are canonical identifiers - * and are used as-is (no namespace__ prefix). The namespace parameter is - * retained for backward compatibility but no longer affects the returned key. - * - * @param namespace - The package namespace (unused, kept for API compatibility) - * @param shortName - The object's name (already the canonical identifier) - * @returns The object name unchanged - * - * @example - * computeFQN('crm', 'account') // => 'account' - * computeFQN(undefined, 'task') // => 'task' - */ -export function computeFQN(_namespace: string | undefined, shortName: string): string { - return shortName; -} - -/** - * Parse FQN back to namespace and short name. - * - * @param fqn - Object name (e.g., "account" or legacy "crm__account" for backward compat) - * @returns { namespace, shortName } - namespace is undefined for unprefixed names - */ -export function parseFQN(fqn: string): { namespace: string | undefined; shortName: string } { - const idx = fqn.indexOf('__'); - if (idx === -1) { - return { namespace: undefined, shortName: fqn }; - } - return { - namespace: fqn.slice(0, idx), - shortName: fqn.slice(idx + 2), - }; -} - -/** - * The three SCALAR props {@link mergeObjectDefinitions} resolves last-writer-wins - * — the exact set the #8284 ruling and ADR-0029 D9.2a both cover, and the same three - * {@link scalarOverridesPackagedBase} answers for. - */ -const OBJECT_FOLD_SCALAR_KEYS = ['label', 'pluralLabel', 'description'] as const; - -type ObjectFoldScalarKey = (typeof OBJECT_FOLD_SCALAR_KEYS)[number]; - -/** - * [ADR-0146 D10, second rule] An object extension's per-field display patches: - * the slot `ObjectExtensionSchema.fieldPatches` declares, carried onto the - * extension def by the manifest loop (`engine.ts`) and applied by - * {@link mergeObjectDefinitions}. ⛔ No second shape: the type IS the spec's, - * and the keys applied are read from `BASE_FIELD_DISPLAY_WHITELIST`, so a - * widened whitelist widens what the merge applies too. - */ -type ObjectFieldPatches = NonNullable; - -/** An `extend` contributor's body: a partial object that may carry {@link ObjectFieldPatches}. */ -type ObjectExtensionBody = Partial & { fieldPatches?: ObjectFieldPatches }; - -/** [#28835] One extension's `fields` as the fold merged them, with the package that declared them. */ -type FoldedExtensionFields = { packageId: string | undefined; fields: unknown }; - -const isPlainRecord = (v: unknown): v is Record => - v !== null && typeof v === 'object' && !Array.isArray(v); - -/** - * The address of one patched display key in a fold's D9.2a yield set - * ({@link SchemaRegistry.tenantPatchedDisplayKeys}): a field key, or one key of - * the option addressed by its stored value. - */ -function fieldPatchYieldKey(field: string, key: string, option?: string): string { - return option === undefined ? `${field}\u0000${key}` : `${field}\u0000options\u0000${option}\u0000${key}`; -} - -/** A picklist item as the registry stores it: the two keys the merge reads. */ -type PicklistItemLike = { options?: PicklistOptionLike[]; _packageId?: string }; - -/** - * [ADR-0150 D3, child E6] Who wrote one field's picklist reference: every - * package whose own layer declares it, in fold order, or `[null]` — one caller - * with no package — when no packaged layer does. - */ -type PicklistReferrer = (field: string | undefined, picklist: string) => readonly (string | null)[]; - -/** Every reference is the environment's: a body no packaged layer declares. */ -const NO_PACKAGE_REFERRER: PicklistReferrer = () => [null]; - -/** Are two package-scoped holders the same one? */ -function sameHolder(a: PackageScopedHolder, b: PackageScopedHolder): boolean { - if (a.kind === 'package') return b.kind === 'package' && a.packageId === b.packageId; - return a.kind === b.kind; -} - -/** The `picklist` a body's field declares (name-keyed or array-spelled), when it declares one. */ -function declaredPicklistOf(body: unknown, field: string): string | undefined { - const fields = isPlainRecord(body) ? body.fields : undefined; - let declared: unknown; - if (Array.isArray(fields)) { - declared = fields.find((f) => isPlainRecord(f) && f.name === field); - } else if (isPlainRecord(fields) && Object.prototype.hasOwnProperty.call(fields, field)) { - declared = fields[field]; - } - const picklist = isPlainRecord(declared) ? declared.picklist : undefined; - return typeof picklist === 'string' && picklist !== '' ? picklist : undefined; -} - -/** - * [ADR-0150 D3, child E6] The referrers of each picklist reference a served - * body carries, read from the contributors that fold into it: EVERY packaged - * layer (the owner, then each extension) whose own declaration names that same - * list on that field, each package once. Several layers can write one - * reference; none of them is picked over the others (epic seat review on - * #31731): the field is served a list only when every one of them reaches the - * same holder ({@link SchemaRegistry.resolvePicklistFields}). A reference no - * packaged layer writes came from an environment-authored body — a tenant - * overlay, a stored row, an object created in this environment — and resolves - * as the environment's (`null`), never from the package that body customizes - * (D3). - */ -function picklistReferrerOf(contributors: readonly ObjectContributor[] | undefined): PicklistReferrer { - if (!contributors || contributors.length === 0) return NO_PACKAGE_REFERRER; - const packaged = [ - ...contributors.filter((c) => c.ownership === 'own'), - ...contributors.filter((c) => c.ownership === 'extend'), - ].filter((c) => !isTenantAuthored(c.definition)); - return (field, picklist) => { - if (field === undefined) return [null]; - const referrers = new Set(); - for (const c of packaged) { - if (declaredPicklistOf(c.definition, field) === picklist) referrers.add(c.packageId ?? null); - } - return referrers.size > 0 ? [...referrers] : [null]; - }; -} - -/** A field of an object body's name-keyed `fields`, when it is a record. */ -function recordFieldOf(body: unknown, name: string): Record | undefined { - const fields = isPlainRecord(body) ? body.fields : undefined; - if (!isPlainRecord(fields) || !Object.prototype.hasOwnProperty.call(fields, name)) return undefined; - const field = fields[name]; - return isPlainRecord(field) ? field : undefined; -} - -/** - * [ADR-0146 D10, second rule] The fields a field patch reaches: the names the - * `own` contributor's declaration carries in the name-keyed `fields` the merge - * reads — "the target object's own fields", as `ObjectExtensionSchema.fieldPatches` - * publishes it. A field an extension adds is never one of them, whatever the - * priorities, and neither is an array-spelled entry: the merge, the patch and - * the miss report all read the record spelling, so they cannot disagree. - * The declaration is the one the registry stores, so the platform columns - * {@link applySystemFields} injects at registration (`created_at` and the - * rest) are among these names: the fold and the install door - * (`getObjectOwner`) admit a patch of one, while `os validate`, which judges - * the authored declaration, refuses it. - * `undefined` when no owner is registered — no opinion, as - * {@link SchemaRegistry.tenantPatchedDisplayKeys} has none there either. - */ -function ownFieldNamesOf(owner: unknown): ReadonlySet | undefined { - if (owner === undefined) return undefined; - const fields = isPlainRecord(owner) ? owner.fields : undefined; - return new Set(isPlainRecord(fields) ? Object.keys(fields).filter((name) => isPlainRecord(fields[name])) : []); -} - -/** - * [ADR-0146 D10, second rule; #28809] What a field patch is judged against in - * the fold: the owner's declaration (`owner`) with only the fields of - * `ownFields` the fold carries at that point in the record spelling - * (`carried`) — which fields a patch reaches is the owner's own, unchanged — - * each field AS THE OWNER DECLARES IT, never as an extension's `fields` - * redeclared it. The install door (`getObjectOwner(name).definition`) and - * `os validate` (the declared object) cannot see another package's extension, - * so the owner's own field and options are the one answer all three readers - * share: an option an extension adds by taking the field over through `fields` - * is not one a patch can name. - */ -function ownFieldsTarget( - owner: unknown, - ownFields: ReadonlySet, - carried: (name: string) => boolean, -): Record { - const fields: Record = {}; - for (const name of ownFields) { - const field = recordFieldOf(owner, name); - if (field && carried(name)) fields[name] = field; - } - return { ...(isPlainRecord(owner) ? owner : {}), fields }; -} - -/** The option of a field's own `options` whose stored `value` is `value`. */ -function optionByValue(field: Record, value: string): Record | undefined { - const options = field.options; - if (!Array.isArray(options)) return undefined; - const found = options.find((o) => isPlainRecord(o) && o.value === value); - return isPlainRecord(found) ? found : undefined; -} - -/** - * [ADR-0029 D9.2a] The scalar yield's comparison, asked of one display key of a - * field or of one of its options. It IS {@link scalarOverridesPackagedBase} — - * the one comparison behind every D9.2a yield, keyed by any attribute (its own - * docblock: the exported key union names the keys its callers ask about, and - * the implementation underneath reads any) — ⛔ never a second copy of it. - */ -function displayKeyOverridesPackagedBase(packaged: unknown, key: string, value: unknown): boolean { - return scalarOverridesPackagedBase(packaged, key as Parameters[1], value); -} - -/** - * [ADR-0146 D10, second rule] One base field with an extension's patch applied: - * the whitelisted field keys, then each option the patch addresses by its - * stored `value`. A key the tenant authored on the fold's base yields - * (`tenantPatched`, ADR-0029 D9.2a). Returns `field` BY REFERENCE when nothing - * applies. - * - * A field bound to a picklist is left alone under `options`: the served options - * are the picklist's, written over the field after the fold - * ({@link SchemaRegistry.finishServedObject}), so an option label written here - * would never be served. The target check names that patch instead. - */ -function patchedField( - name: string, - field: Record, - patch: ObjectFieldPatches[string], - tenantPatched: ReadonlySet | undefined, -): Record { - let next: Record | undefined; - const patchKeys = patch as Record; - for (const key of BASE_FIELD_DISPLAY_WHITELIST.field) { - const value = patchKeys[key]; - if (value === undefined || tenantPatched?.has(fieldPatchYieldKey(name, key))) continue; - (next ??= { ...field })[key] = value; - } - const optionPatches = patch.options; - const options = field.options; - const picklistBound = typeof field.picklist === 'string' && field.picklist !== ''; - if (optionPatches && Array.isArray(options) && !picklistBound) { - let patchedOptions: unknown[] | undefined; - options.forEach((option, index) => { - if (!isPlainRecord(option) || typeof option.value !== 'string') return; - if (!Object.prototype.hasOwnProperty.call(optionPatches, option.value)) return; - const optionPatch = optionPatches[option.value] as Record | undefined; - if (!optionPatch) return; - let patchedOption: Record | undefined; - for (const key of BASE_FIELD_DISPLAY_WHITELIST.option) { - const value = optionPatch[key]; - if (value === undefined || tenantPatched?.has(fieldPatchYieldKey(name, key, option.value))) continue; - (patchedOption ??= { ...option })[key] = value; - } - if (patchedOption) (patchedOptions ??= [...options])[index] = patchedOption; - }); - if (patchedOptions) (next ??= { ...field }).options = patchedOptions; - } - return next ?? field; -} - -/** - * [ADR-0146 D10, second rule] `fields` with every patch of `patches` applied - * that names one of the owner's own fields (`ownFields`, {@link ownFieldNamesOf}) - * present in `fields`. A patch naming any other field — one the object lacks, - * or one only an extension adds — is skipped here, and named by the fold - * ({@link SchemaRegistry.reportFieldPatchMisses}). Nothing applies when no - * owner is registered. Returns `fields` BY REFERENCE when nothing applies. - * - * [#28809] The fold hands this only the patches the judge admitted against the - * owner's declaration ({@link admittedFieldPatches}). An admitted option patch - * whose option an earlier layer left out (an extension's `fields` - * redeclaration, a tenant overlay base) finds no option here, so nothing of it - * applies; the fold names it after the merge, by the same judge asked of the - * merged fields. - */ -function applyFieldPatches( - fields: ServiceObject['fields'], - patches: ObjectFieldPatches, - tenantPatched: ReadonlySet | undefined, - ownFields: ReadonlySet | undefined, -): ServiceObject['fields'] { - if (!isPlainRecord(fields) || !ownFields) return fields; - let out: Record | undefined; - for (const [name, patch] of Object.entries(patches)) { - if (!patch || !ownFields.has(name)) continue; - const field = recordFieldOf({ fields: out ?? fields }, name); - if (!field) continue; - const next = patchedField(name, field, patch, tenantPatched); - if (next !== field) (out ??= { ...fields })[name] = next; - } - return (out ?? fields) as ServiceObject['fields']; -} - -/** - * [ADR-0146 D10, second rule; #28809] `patches` without what the judge missed - * against the owner's declaration: a missed field's whole patch, a missed - * option's entry under `options`. The fold applies exactly what the install - * door and `os validate` admit, and names exactly what they refuse. Returns - * `patches` BY REFERENCE when nothing was missed. - */ -function admittedFieldPatches( - patches: ObjectFieldPatches, - misses: readonly FieldPatchTargetMiss[], -): ObjectFieldPatches { - if (misses.length === 0) return patches; - const out: Record = { ...patches }; - for (const miss of misses) { - const patch = out[miss.field]; - if (!patch) continue; - if (miss.option === undefined) { - delete out[miss.field]; - continue; - } - if (!patch.options) continue; - const { [miss.option]: _missed, ...options } = patch.options; - out[miss.field] = { ...patch, options }; - } - return out as ObjectFieldPatches; -} - -/** - * [#28356] Which layer an object fold serves: `'served'`, what every read - * answers (the bound actions the registry serves, runtime-authored included), - * or `'code'`, the code-layer resolution {@link SchemaRegistry.getArtifactItem} - * answers (the code packages' bound actions alone). - */ -type ServedObjectLayer = 'served' | 'code'; - -/** - * [#29903, #29872] The options that leave `extend` contributions out of an - * object fold ({@link SchemaRegistry.getAllObjects}, `listItems`, - * `foldObjectExtendersOnto`): - * - * - `withoutExtensionsOf` — that package's contributions are left out; - * - `onlyExtensionsOf` — every OTHER package's contributions are left out, - * so only that package's own extensions fold. The protocol's per-package - * layer passes it, naming the listed package. - * - * Both may be given; a contribution either one leaves out is left out. The - * base layer (`own` / `overlay`) is never left out. - * - * [#30756] `asEnabled` asks the opposite question of a DISABLED package: the - * fold as it will serve once that package is enabled, its `extend` - * contributions folded in instead of withheld ({@link SchemaRegistry.withholdDisabledExtensions}). - * Every other disabled package's stay withheld. The enable door's pre-check - * reads it, as the disable door's reads `withoutExtensionsOf`; for a package - * that is not disabled it changes nothing. - */ -export interface ObjectExtensionFoldOptions { - withoutExtensionsOf?: string; - onlyExtensionsOf?: string; - asEnabled?: string; -} - -/** Whether `options` leaves any contribution out (otherwise the fold is the ordinary one). */ -function leavesExtensionsOut(options: ObjectExtensionFoldOptions | undefined): options is ObjectExtensionFoldOptions { - return options?.withoutExtensionsOf !== undefined || options?.onlyExtensionsOf !== undefined; -} - -/** [#29903, #29872] Whether `options` leaves `contributor` out of the fold. */ -function isLeftOutExtension(contributor: ObjectContributor, options: ObjectExtensionFoldOptions): boolean { - if (contributor.ownership !== 'extend') return false; - if (options.withoutExtensionsOf !== undefined && contributor.packageId === options.withoutExtensionsOf) return true; - return options.onlyExtensionsOf !== undefined && contributor.packageId !== options.onlyExtensionsOf; -} - -/** - * The `extend` contributions a disable withholds from a served fold, and the - * member names only they add ({@link SchemaRegistry.withholdDisabledExtensions}). - */ -interface WithheldExtensions { - kept: ObjectContributor[]; - withheldFields: ReadonlySet; - withheldRules: ReadonlySet; - /** Each withheld index by its structural key ({@link indexKeysOf}). */ - withheldIndexes: ReadonlySet; -} - -/** The field names a body declares in the record spelling the merge reads. */ -function fieldNamesOf(body: unknown): string[] { - const fields = isPlainRecord(body) ? body.fields : undefined; - return isPlainRecord(fields) ? Object.keys(fields) : []; -} - -/** The validation rule names a body declares. */ -function ruleNamesOf(body: unknown): string[] { - const rules = isPlainRecord(body) ? body.validations : undefined; - if (!Array.isArray(rules)) return []; - return rules - .map((rule) => (isPlainRecord(rule) ? rule.name : undefined)) - .filter((name): name is string => typeof name === 'string' && name !== ''); -} - -/** The indexes a body declares, each by its structural key (the key the fold's index subtraction compares by). */ -function indexKeysOf(body: unknown): string[] { - const indexes = isPlainRecord(body) ? body.indexes : undefined; - return Array.isArray(indexes) ? indexes.map((index) => stableStringify(index)) : []; -} - -/** - * `base` without the fields, rules and indexes a withheld extension alone adds. By - * reference when it carries none of them, which is the ordinary case (a base - * that was never folded). - */ -function withoutWithheldMembers(base: ServiceObject, withheld: WithheldExtensions): ServiceObject { - const fields = isPlainRecord((base as { fields?: unknown }).fields) ? (base as { fields: Record }).fields : undefined; - const rules = (base as { validations?: unknown }).validations; - const dropFields = fields !== undefined && Object.keys(fields).some((name) => withheld.withheldFields.has(name)); - const dropRules = Array.isArray(rules) - && rules.some((rule) => isPlainRecord(rule) && typeof rule.name === 'string' && withheld.withheldRules.has(rule.name)); - const indexes = (base as { indexes?: unknown }).indexes; - const dropIndexes = Array.isArray(indexes) && indexes.some((index) => withheld.withheldIndexes.has(stableStringify(index))); - if (!dropFields && !dropRules && !dropIndexes) return base; - const out = { ...base } as ServiceObject & Record; - if (dropFields) { - out.fields = Object.fromEntries(Object.entries(fields!).filter(([name]) => !withheld.withheldFields.has(name))) as ServiceObject['fields']; - } - if (dropRules) { - out.validations = (rules as unknown[]).filter( - (rule) => !(isPlainRecord(rule) && typeof rule.name === 'string' && withheld.withheldRules.has(rule.name)), - ) as ServiceObject['validations']; - } - if (dropIndexes) { - out.indexes = (indexes as unknown[]).filter((index) => !withheld.withheldIndexes.has(stableStringify(index))) as ServiceObject['indexes']; - } - return out; -} - -/** - * Deep merge two ServiceObject definitions. - * - * The merge set is CLOSED and enumerable — this is NOT "every prop, later - * value wins": - * - `fields` and `indexes` are merged ADDITIVELY (`fields` keyed by name). - * - `validations` are merged BY NAME ({@link mergeValidationsByName}, - * [ADR-0029 D9.2a, 2026-10-30 note]): rules of different names add, and a - * rule of a name already folded replaces it — with `tenantRules`, carrying - * the tenant's `message` and `active`, never the tenant's condition. - * - The three {@link OBJECT_FOLD_SCALAR_KEYS} (`label`, `pluralLabel`, - * `description`) are overridden last-writer-wins, subject to the - * `tenantAuthored` yield rule below. [#30391] A `label` that renames the - * object without a `pluralLabel` beside it also takes the running - * `pluralLabel` off: the plural named the old label. - * - `fieldPatches` [ADR-0146 D10, second rule] is APPLIED, never carried: - * after the merges above, each patch writes its `BASE_FIELD_DISPLAY_WHITELIST` - * keys onto the named field and its option labels onto the options whose - * stored `value` it names, each key yielding where `tenantPatched` says the - * tenant authored it — the same D9.2a rule as the scalars. A patch reaches - * the owner's own fields only (`ownFields`): one naming any other field, - * a field an extension adds included, is skipped here; the fold names it. - * - * **Every other top-level prop on `extension` is silently discarded.** - * `merged` starts as `{ ...base }` and nothing outside the list above is ever - * copied onto it — a caller that hands this function an extension body - * carrying an undeclared key, e.g. `tenancy: { enabled: false }`, gets a - * valid-but-inert no-op HERE: no error, no warning from this function, the - * base's existing value simply wins as if the extension had never named the - * key (issue #12680). - * - * Declarative authors never reach that silence, though: `ObjectExtensionSchema` - * (`packages/spec/src/data/object.zod.ts`) is `.strict()` — its shape is - * exactly `extend` / `fields` / `fieldPatches` / `label` / `pluralLabel` / - * `description` / `validations` / `indexes` / `priority`, so an `objectExtensions` entry - * naming `tenancy`, `permissions`, or any other key outside that list fails - * LOUDLY at authoring time with a prescription (#4001; the same rule is - * documented at `content/docs/data-modeling/object-extensions.mdx`). This - * function's discard is reachable only by a caller that bypasses that - * schema — a direct, programmatic `registry.registerObject(def, pkg, - * undefined, 'extend', …)` call, which is how this package's own pin - * (`registry-object-extension-nonenumerated-prop-discard.test.ts`) exercises - * the rule. Extender-writable `tenancy`/`permissions` would in any case be a - * separate, much bigger decision that has NOT been made. - * - * Unlike `tenancy`/`permissions`, this one is not blocked by schema: - * `_provenance` is a real top-level prop an `extend` contributor carries - * TODAY. Copying it through (as "later value - * wins" would) lets a third-party extender flip a tenant-authored object's - * `_provenance` to the extending package's — reaching, by a different route, - * the exact outcome ADR-0029 D9.3's priority-reranking guard exists to - * prevent. `registry-object-overlay-layer.test.ts` ("an extender declaring - * priority 140 does not become the base layer") fails if this merge set is - * widened to copy it through. - * - * [ADR-0029 D9.2a] …the SCALAR override above is conditional. `tenantAuthored` names - * the scalars the fold's BASE has authored away from the packaged owner's - * value; an extender yields on those. See - * {@link SchemaRegistry.tenantAuthoredScalars} for why the set is computed once - * over the base rather than re-derived from the running `merged`; `tenantRules` - * ({@link SchemaRegistry.tenantRuleSettings}) is computed once for the same reason. - */ -function mergeObjectDefinitions( - base: ServiceObject, - extension: ObjectExtensionBody, - tenantAuthored?: ReadonlySet, - tenantRules?: ReadonlyMap, - tenantPatched?: ReadonlySet, - ownFields?: ReadonlySet, -): ServiceObject { - const merged = { ...base }; - - // Merge fields additively - if (extension.fields) { - merged.fields = { ...base.fields, ...extension.fields }; - } - - // [ADR-0029 D9.2a, 2026-10-30 note] Merge validations by name - if (extension.validations) { - merged.validations = mergeValidationsByName( - base.validations, - extension.validations, - tenantRules, - ) as ServiceObject['validations']; - } - - // Merge indexes additively - if (extension.indexes) { - merged.indexes = [...(base.indexes || []), ...extension.indexes]; - } - - // Override scalar props (last writer wins) — [ADR-0029 D9.2a] unless the base has been - // authored by the tenant, in which case the extender's packaged default yields. - const yields = (key: ObjectFoldScalarKey): boolean => tenantAuthored?.has(key) === true; - if (extension.label !== undefined && !yields('label')) { - // [#30391] `pluralLabel` is the plural OF `label`: an extension that renames - // the object and declares no plural leaves the running plural naming the - // old label, so the object is served as one that declares no - // `pluralLabel`. A plural the tenant authored stays (the D9.2a yield), and - // an extension that repeats the label it extends renames nothing. - if (extension.pluralLabel === undefined && extension.label !== merged.label && !yields('pluralLabel')) { - delete merged.pluralLabel; - } - merged.label = extension.label; - } - if (extension.pluralLabel !== undefined && !yields('pluralLabel')) { - merged.pluralLabel = extension.pluralLabel; - } - if (extension.description !== undefined && !yields('description')) { - merged.description = extension.description; - } - - // [ADR-0146 D10, second rule] The extension's display patches of the - // owner's own fields (`ownFields`) — never of a field an extension adds, - // whatever the priorities, or the answer would depend on which extender the - // fold reached first — applied AFTER the additive merges, with each key - // yielding where the tenant authored it (`tenantPatched`, ADR-0029 D9.2a), - // as the scalars above yield. Two extenders patching one key resolve as the - // scalars do: the fold applies contributors in ascending `priority`, so the - // higher one is written last. - if (extension.fieldPatches) { - merged.fields = applyFieldPatches(merged.fields, extension.fieldPatches, tenantPatched, ownFields); - } - - return merged; -} - -/** - * [#8027] Key-order-insensitive JSON, for comparing two `indexes` entries, or - * two validation rules' keys, BY VALUE — see - * {@link SchemaRegistry.subtractExtenderContributions} and - * {@link changedPackagedRuleKeys}. - * - * Plain `JSON.stringify` would report the same rule as two different rules - * whenever the two copies were built by different code paths (a stored overlay - * row that went through `JSON.parse` vs the contributor's in-memory literal), - * which is exactly the pair being compared, so key order cannot be assumed. - * Arrays keep their order — an index's `fields` list is ordered and two - * spellings of it are genuinely two different indexes. - */ -function stableStringify(value: unknown): string { - if (value === null || typeof value !== 'object') return JSON.stringify(value) ?? 'null'; - if (Array.isArray(value)) return `[${value.map(stableStringify).join(',')}]`; - const entries = Object.entries(value as Record) - .filter(([, v]) => v !== undefined) - .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)); - return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${stableStringify(v)}`).join(',')}}`; -} - -/** - * [ADR-0029 D9.2a, 2026-10-30 note] What a tenant's same-named copy of a - * packaged validation rule may set: its sentence and its switch. Every other key - * a rule carries — `type`, `condition` and each variant's own predicate keys, - * `events`, `severity`, `priority`, and the editor keys — is the package's. - */ -type TenantRuleSettings = { message?: string; active?: boolean }; - -/** A rule's `name` when it has one; a rule without a string name has no identity to merge by and is always additive. */ -function validationRuleName(rule: unknown): string | undefined { - const name = (rule as { name?: unknown } | null | undefined)?.name; - return typeof name === 'string' && name !== '' ? name : undefined; -} - -/** - * The tenant settings a base layer's rules carry, keyed by rule name — the - * first copy of a name wins, so a stored row that predates the by-name fold - * (the tenant's reworded copy, then the extension's original appended after - * it) still answers with the tenant's sentence. - */ -function tenantRuleSettingsOf(rules: unknown): Map { - const out = new Map(); - if (!Array.isArray(rules)) return out; - for (const rule of rules) { - const name = validationRuleName(rule); - if (name === undefined || out.has(name)) continue; - const { message, active } = rule as { message?: unknown; active?: unknown }; - out.set(name, { - ...(typeof message === 'string' ? { message } : {}), - ...(typeof active === 'boolean' ? { active } : {}), - }); - } - return out; -} - -/** The packaged rule, carrying the tenant's sentence and switch where the tenant's copy sets them. */ -function withTenantRuleSettings(packaged: R, settings: TenantRuleSettings | undefined): R { - if (!settings || (settings.message === undefined && settings.active === undefined)) return packaged; - return { ...packaged, ...settings }; -} - -/** - * [ADR-0029 D9.2a, 2026-10-30 note] Merge `validations` BY NAME, the way - * `fields` merge by key: a later layer's rule replaces every earlier rule of its - * name and is appended in fold order, and a rule of a new name is added. With - * `tenantRules` (the fold's base is a tenant overlay), the rule carries the - * tenant's `message` and `active` for its name; its condition stays the - * later layer's. - */ -function mergeValidationsByName( - base: readonly unknown[] | undefined, - extension: readonly unknown[], - tenantRules: ReadonlyMap | undefined, -): unknown[] { - const names = new Set(); - for (const rule of extension) { - const name = validationRuleName(rule); - if (name !== undefined) names.add(name); - } - const kept = (base ?? []).filter((rule) => { - const name = validationRuleName(rule); - return name === undefined || !names.has(name); - }); - const added = extension.map((rule) => { - const name = validationRuleName(rule); - return name === undefined ? rule : withTenantRuleSettings(rule, tenantRules?.get(name)); - }); - return [...kept, ...added]; -} - -/** The value a rule's key has when the rule leaves it out — `BASE_VALIDATION_SHAPE`'s defaults. */ -const VALIDATION_RULE_DEFAULTS: Readonly> = { - events: ['insert', 'update'], - priority: 100, - severity: 'error', +// [#33949 codemod] The top-level declarations that moved whole to `registry-top-level.ts`, as this file still reads them. +import { + CO_OWNERSHIP_WORDING, DEFAULT_EXTENDER_PRIORITY, DEFAULT_OVERLAY_PRIORITY, DEFAULT_OWNER_PRIORITY, + DEFAULT_RECORD_PAGE_CONFLICT_CODE, DUPLICATE_ARTIFACT_OBJECT_NAME_CODE, EXTENDED_OBJECTS_LISTED, + EXTENSION_DEFAULT_ROW_SCOPE_REFUSED_CODE, EXTENSION_MEMBERSHIP_KEYS_REFUSED_CODE, FIND_BOUND_ACTION_COLLISIONS, + MISPLACED_DEFAULT_PAGE_CODE, NAMESPACE_CONFLICT_CODE, NO_PACKAGE_REFERRER, OBJECT_FIELD_TYPE_REFUSED_CODE, + OBJECT_OWNERSHIP_CONFLICT_CODE, OBJECT_QUEUE_COLUMN_REFUSED_CODE, QUEUE_FIELD, REGISTRY_LOG_LEVELS, + SECURITY_CATALOG_NAME_RULE, VALIDATION_RULE_COPY_REFUSED_CODE, bareNameColdBootWarning, bareNameConflictLine, + bareNameReasonFor, bundleBaseKey, coldBootConflictMessage, collectBundle, overlaySlotEntriesOf, overlaySlotKey, + packageBodySlot, parseOverlaySlotKey, parsePackageAction, picklistReferrerOf, sameActionDeclaration, sameHolder, + storesObjectLessPackagelessRow, withArrivingMembers, withDiscriminator, +} from './registry-top-level.js'; +import type { + ArtifactInstallScope, BareNameConflict, BoundActionCollision, BundleArrival, BundleEntry, CoOwnershipSource, + DefaultRecordPageConflict, ExtendedObjectOfUninstall, FoldedExtensionFields, FollowDoorLayer, ObjectContributor, + ObjectExtensionFoldOptions, ObjectFoldScalarKey, PicklistItemLike, PicklistReferrer, RegistryLogLevel, + RunRegisteredObject, SchemaRegistryOptions, SecurityCatalogNameConflict, ServedObjectLayer, TenantRuleSettings, + UnboundActionTarget, WithheldExtensions, +} from './registry-top-level.js'; +// [#33949 codemod] Re-exported by name, so every importer of `./registry.js` reads what it read. +export { + type ArtifactInstallScope, type BareNameConflict, type BoundActionCollision, type CoOwnershipSource, + DEFAULT_EXTENDER_PRIORITY, DEFAULT_OVERLAY_PRIORITY, DEFAULT_OWNER_PRIORITY, DEFAULT_RECORD_PAGE_CONFLICT_CODE, + DUPLICATE_ARTIFACT_OBJECT_NAME_CODE, type DefaultRecordPageConflict, EXTENSION_DEFAULT_ROW_SCOPE_REFUSED_CODE, + EXTENSION_MEMBERSHIP_KEYS_REFUSED_CODE, FIND_BOUND_ACTION_COLLISIONS, MISPLACED_DEFAULT_PAGE_CODE, + NAMESPACE_CONFLICT_CODE, OBJECT_FIELD_TYPE_REFUSED_CODE, OBJECT_OWNERSHIP_CONFLICT_CODE, + OBJECT_QUEUE_COLUMN_REFUSED_CODE, type ObjectContributor, type ObjectExtensionFoldOptions, REGISTRY_LOG_LEVELS, + RESERVED_NAMESPACES, type RegistryLogLevel, type SchemaRegistryOptions, type SecurityCatalogNameConflict, + type UnboundActionTarget, type UnboundActionTargetError, VALIDATION_RULE_COPY_REFUSED_CODE, applySystemFields, + bareNameColdBootWarning, collectManifestBoundActions, collectManifestObjectNames, computeFQN, + describeBoundActionCollision, describeUnboundActionTargets, parseFQN, reconcileManagedApiMethods, + warnFunctionalCompleteness, warnStrippedLegacyApiMethods, +} from './registry-top-level.js'; +// [#33949 codemod] Logging control, object storage and generic metadata storage: the methods of the same names below hand the bodies what they read (`StorageHost`). +import { StorageFacet } from './registry-storage.js'; +// [#33949 codemod] Restore on refusal: the methods of the same names below hand the bodies what they read (`RestoreHost`). +import { RestoreFacet } from './registry-restore.js'; +// [#33949 codemod] Namespaces, default record pages and security catalog names: the methods of the same names below hand the bodies what they read (`NamespacesAndClaimsHost`). +import { NamespacesAndClaimsFacet } from './registry-namespaces-and-claims.js'; +// [#33949 codemod] Bare names, one name one package holder: the methods of the same names below hand the bodies what they read (`BareNamesHost`). +import { BareNamesFacet } from './registry-bare-names.js'; +// [#33949 codemod] Object registration (ownership model): the methods of the same names below hand the bodies what they read (`ObjectRegistrationHost`). +import { ObjectRegistrationFacet } from './registry-object-registration.js'; +// [#33949 codemod] Platform-global declaration and uninstall checks: the methods of the same names below hand the bodies what they read (`PlatformGlobalHost`). +import { PlatformGlobalFacet } from './registry-platform-global.js'; +// [#33949 codemod] Generic metadata items and package bodies: the methods of the same names below hand the bodies what they read (`ItemsHost`). +import { ItemsFacet } from './registry-items.js'; +// [#33949 codemod] Package-scoped addressing: the methods of the same names below hand the bodies what they read (`PackageAddressingHost`). +import { PackageAddressingFacet } from './registry-package-addressing.js'; +// [#33949 codemod] Package management: the methods of the same names below hand the bodies what they read (`PackagesHost`). +import { PackagesFacet } from './registry-packages.js'; +// [#33949 codemod] Apps, navigation contributions and their diagnostics: the methods of the same names below hand the bodies what they read (`AppsHost`). +import { AppsFacet } from './registry-apps.js'; +// [#33949 codemod] Plugins, kinds, shared picklists and reset: the methods of the same names below hand the bodies what they read (`PicklistsHost`). +import { PicklistsFacet } from './registry-picklists.js'; +// [#33949 codemod] The facets' hosts, one per registry, which the static block at the end of the class builds. +import { + appsHost, bareNamesHost, itemsHost, namespacesAndClaimsHost, objectRegistrationHost, packageAddressingHost, + packagesHost, picklistsHost, platformGlobalHost, restoreHost, storageHost, +} from './registry-facet-hosts.js'; +// [#33949 codemod] Exported for the section modules above, which import them (lever (a)); `index.ts` re-exports by name, so none reaches the package. +export { + NOT_A_PAIR_ROW_OF_A_HOLDER, RegistrationRestorePoint, ValidationRuleCopyRefusedError, actionHoldersOnOwnerKeyOf, + declaredOverrideBase, pairRowDeclaresAnotherHolder, }; -/** - * The keys on which a tenant's copy of a packaged rule differs from the - * package's, beyond the two it may set (`message`, `active`) and the loader's - * `_`-prefixed stamps. A key a rule leaves out is read at its schema default, - * so a stored copy whose parse stamped the defaults is not a change. Empty when - * the copy changes nothing but its sentence and its switch. - */ -function changedPackagedRuleKeys(copy: unknown, packaged: unknown): string[] { - const enforced = (rule: unknown): Record => { - const out: Record = { ...VALIDATION_RULE_DEFAULTS }; - for (const [key, value] of Object.entries((rule ?? {}) as Record)) { - if (key === 'message' || key === 'active' || key.startsWith('_') || value === undefined) continue; - out[key] = value; - } - return out; - }; - const a = enforced(copy); - const b = enforced(packaged); - return [...new Set([...Object.keys(a), ...Object.keys(b)])] - .filter((key) => stableStringify(a[key]) !== stableStringify(b[key])) - .sort((x, y) => (x === 'type' ? -1 : y === 'type' ? 1 : x < y ? -1 : x > y ? 1 : 0)); -} - -/** - * Global Schema Registry - * Unified storage for all metadata types (Objects, Apps, Flows, Layouts, etc.) - * - * ## Namespace & Ownership Model - * - * Objects use a namespace-based FQN system: - * - `namespace`: Short identifier from package manifest (e.g., "crm", "todo") - * - `name`: canonical object name (e.g., "account", "sys_user") - * - Reserved namespaces (`base`, `system`) don't get prefixed - * - * Ownership modes: - * - `own`: One package owns the object (creates the table, defines base schema) - * - `extend`: Multiple packages can extend an object (add fields, merge by priority) - * - * ## Package vs App Distinction - * - **Package**: The unit of installation, stored under type 'package'. - * Each InstalledPackage wraps a ManifestSchema with lifecycle state. - * - **App**: A UI navigation shell (AppSchema), registered under type 'apps'. - * Apps are extracted from packages during registration. - * - A package may contain 0, 1, or many apps. - */ -export type RegistryLogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; - -/** All valid {@link RegistryLogLevel} values — used to validate `OS_REGISTRY_LOG`. */ -export const REGISTRY_LOG_LEVELS: readonly RegistryLogLevel[] = [ - 'debug', 'info', 'warn', 'error', 'silent', -]; - -/** - * Construction options for {@link SchemaRegistry}. - */ -export interface SchemaRegistryOptions { - /** - * Whether the host kernel runs in multi-tenant mode. The `organization_id` - * column itself is auto-injected regardless of this flag (lookup → - * sys_organization, on every registered object that doesn't already declare - * it, isn't `managedBy` an external subsystem, and hasn't opted out via - * `systemFields`/`tenancy.enabled:false`). When `true` the injected column - * is additionally INDEXED — single-tenant stacks skip the index since - * nothing ever filters by organization. - * - * Sourced from the `OS_MULTI_ORG_ENABLED` env var when not explicitly set — - * matches how the SecurityPlugin and CLI startup banner pick the mode. - * Default is `false` (single-tenant) so local `dev`/`start` runs seed - * demo data inline at boot; set `OS_MULTI_ORG_ENABLED=true` for cloud / - * production multi-org deployments. Pass an explicit boolean to override - * (useful in tests). - */ - multiTenant?: boolean; - - /** - * Whether to provision the hidden `__search` search-normalization companion - * column on registered objects that have an eligible display/name field - * (#2486 — pinyin search recall). Sourced from `OS_SEARCH_PINYIN_ENABLED` - * via `resolveSearchPinyinEnabled()` when not explicitly set (the CLI boot - * path stamps the locale-derived decision into that env var before the - * kernel constructs engines). Default off — non-Chinese deployments carry - * no extra column. Pass an explicit boolean to override (useful in tests). - */ - searchCompanion?: boolean; - - /** - * Policy for the install-time namespace gate (ADR-0048 Phase 1) — installing - * a package whose `manifest.namespace` is already owned by a *different* - * installed package. - * - * - `'error'` (default): throw {@link NamespaceConflictError} at install time, - * naming both packages. Makes the namespace land-grab a loud, early failure - * instead of a mid-install `CREATE TABLE` blow-up. - * - `'warn'`: log a warning and let the install proceed. For deliberate - * migrations where the conflict is temporarily expected. - * - * Sourced from `OS_METADATA_COLLISION` (`warn` to downgrade) when not set - * explicitly. Same-package reinstall and shareable platform namespaces - * (`base`/`system`/`sys`) are never treated as conflicts. (The per-item - * cross-package collision throw was retired in ADR-0048 §3.4 — distinct - * package ids are always disambiguable by package-scoped resolution — except - * for positions, permission sets and capabilities, whose second-holder - * refusal ({@link SecurityCatalogNameConflictError}) this policy does NOT - * downgrade: no ruling extends `warn` to it.) - */ - collisionPolicy?: 'error' | 'warn'; - - /** - * Verbosity of the registry's own `[Registry] …` log lines. Sourced from the - * `OS_REGISTRY_LOG` env var when not set explicitly (default `'info'`). - * - * The point of the env seam is discoverability (#3420): expected-but-noisy - * housekeeping — re-registering an owned object / overwriting a package - * manifest on a rebuild/HMR/seed-replay — is emitted at `'debug'`, so it stays - * out of a stock `info` boot log but a developer chasing a registration issue - * can surface it with `OS_REGISTRY_LOG=debug`. An unrecognized value falls - * back to `'info'`. - */ - logLevel?: RegistryLogLevel; -} - /** * Augment a registered object with implicit system fields. * @@ -1416,748 +300,6 @@ export { */ export { AUDIT_FIELD_GOVERNANCE }; -/** - * [ADR-0117 D1] The injected BU-ownership column name, spelled out so it greps - * from this file, and annotated with the spec's registered protocol name so the - * two cannot drift: a rename in `SystemFieldName.OWNING_BUSINESS_UNIT_ID` makes - * this line a compile error rather than a silently orphaned injection. - */ -const OWNING_BUSINESS_UNIT_FIELD: typeof SystemFieldName.OWNING_BUSINESS_UNIT_ID = - 'owning_business_unit_id'; - -/** [ADR-0142 D3] The injected ownership-queue column, annotated the same way. */ -const QUEUE_FIELD: typeof SystemFieldName.QUEUE = 'queue'; - -export function applySystemFields( - schema: ServiceObject, - opts: { - multiTenant: boolean; - /** - * [ADR-0131 D7] The objects THIS deployment declares platform-global — the - * plan's one deployment input. A declared object gets no `organization_id` - * here. Omitted or empty ⇒ the plan is byte-identical to the authored one. - */ - platformGlobalObjects?: ReadonlySet; - } -): ServiceObject { - // WHICH columns this object carries is the spec's derivation - // (`resolveInjectedSystemColumns`, #5378) — one answer shared with every - // author-time consumer that must resolve a reference to an injected column - // but cannot load this runtime (`@objectstack/lint`). This function keeps sole - // ownership of WHAT each column looks like, the same split #3786 established - // for the audit family. Do NOT re-derive a condition below: a second copy here - // is exactly the drift the plan exists to prevent. - // - // [#7865] This pass runs for `external` (ADR-0015) objects too — DELIBERATELY - // (maintainer ruling 2026-08-12, direction B). The platform provisions no - // storage for a federated object (`syncObjectSchema` returns early, no DDL), - // so the anchors injected below are registered-but-unprovisioned there; the - // machine-readable marker for that fact is `resolveInjectedColumnProvenance` - // / `unprovisionedInjectedColumns` (re-exported above from - // `@objectstack/metadata-core`), NOT a key on the definitions this function - // spreads — the definitions must stay byte-identical to the shipped tables, - // because the #7859 Layer-0 guard and the #4326 round-trip strip both read - // them by exact identity. Do not add keys here; consumers ask the API. - // - // [ADR-0131 D7] …and the deployment's platform-global declaration is the - // plan's one deployment input: a declared object is planned with no tenant - // column, so nothing below injects `organization_id` for it. Passed only when - // a deployment declared something, so every other call is the one-argument - // plan, byte for byte. - const plan = - opts.platformGlobalObjects && opts.platformGlobalObjects.size > 0 - ? resolveInjectedSystemColumns(schema, { platformGlobalObjects: opts.platformGlobalObjects }) - : resolveInjectedSystemColumns(schema); - - // 1. Hard opt-out at object level (e.g. seed/migration tables). - // Folded into the plan (`systemFields: false` ⇒ every flag false), so the - // cheap path stays cheap without a second reading of the key. - // - // 2. Skip only `better-auth` managed tables. Their column layout is - // driven by better-auth's own migrations (sys_user, sys_session, - // sys_organization, …) and injecting extra columns here would - // collide with what better-auth expects. Other `managedBy` buckets - // (`platform`, `config`, `system`, `append-only`) all need the - // tenant + audit columns for multi-tenant isolation and time-travel - // history — withholding them silently broke RLS reads on - // sys_audit_log / sys_activity (the SecurityPlugin's - // field-existence safety net dropped `organization_id = - // current_user.organization_id` as "field missing", producing - // RLS_DENY_FILTER → 0 rows for every non-admin caller). - // Both opt-outs are the plan's rows 1-2; the bail below covers them. - // - // Honor explicit opt-out via either `systemFields.tenant === false` - // OR `tenancy.enabled === false`. The latter is the schema-level - // declaration that the table is a shared/global catalog (e.g. - // sys_package — the Marketplace registry). Without this, the - // registry would still inject `organization_id`, and the - // SecurityPlugin's RLS layer would filter every cross-org read down - // to 0 rows even though the schema explicitly disabled multi-tenancy. - // - // The `organization_id` COLUMN is provisioned unconditionally (subject only - // to the explicit opt-outs above) — its existence no longer depends on the - // global multi-tenant flag. Decoupling "does the column exist" from "is - // tenancy enabled" is what stops sudo writers (audit / messaging / inbox / - // outbox …) from failing with "no column named organization_id" on - // single-tenant stacks: they can always stamp the column, and org-scoping - // stamps it on insert (under `single` with the Default Organization), so it - // is never NULL (ADR-0131 D1). The multi-tenant flag now governs only - // whether the column is INDEXED — on a single-tenant DB nothing ever filters - // by organization, so the index would be dead weight. That is also why the - // plan takes no `multiTenant` input: the flag cannot change what EXISTS, only - // what is indexed, so an author-time consumer needs no runtime context. - const wantTenant = plan.tenant; - const wantAudit = plan.audit; - - // Ownership is auto-provisioned by DEFAULT on user-authored business - // objects (correct-by-default for AI authors). It is withheld only where a - // per-record owner is meaningless: any platform-managed table (`managedBy` - // is set — config/append-only/system/platform; `better-auth` already - // returned above), the `sys_*` namespace, or an explicit opt-out via - // `ownership: 'business_unit' | 'org' | 'none'` on the schema (Dataverse-style - // — catalog / junction tables, plus D1's unit-owned tier, which takes the BU - // anchor instead). Note this is the SAFE default direction: forgetting the - // opt-out leaves a harmless spare column, whereas the old opt-IN model let - // authors silently ship objects with no working ownership at all. - // `ownership` is a declared ObjectSchema field (record-ownership model), read - // off the typed schema by the plan — no `as any` (#3175). - // - // [ADR-0117 D1 / #5677] The plan treats the value as a `string`, not the - // enum, on purpose: it is also called on pre-parse input. That widening is - // what let the engine recognise D1's fourth tier `'business_unit'` BEFORE the - // schema could emit it — the ordering #5677 → #5678 exists to guarantee, since - // a tier the schema emits first would be judged by a no-overlap literal test - // and get the INVERSE of what D1 declares. As of #5678 the spec enum reads - // `'user' | 'business_unit' | 'org' | 'none'` and the tier is authorable. - // - // [ADR-0117 D1 / #5677] The ownership decision is a POSITIVE LIST, deliberately - // — it used to read `ownership !== 'org' && ownership !== 'none'`, i.e. a - // DENY-list, so ANY value outside the two exclusions fell through to "inject - // `owner_id`". That default was safe only while the enum had exactly three - // members: D1's `business_unit` tier means "owned by a UNIT, not a person" - // (`owner_id` ❌, `owning_business_unit_id` ✅), and under the deny-list it - // would have been stamped with `owner_id` — the exact inverse. The same - // argument applies to any FIFTH tier a later ADR adds, which is why the list - // stays positive rather than being "completed" to the current enum. - // - // The `sys_*` / `managedBy` ineligibility and the per-tier table - // - // ownership owner_id owning_business_unit_id - // undefined/user ✅ ✅ - // business_unit ❌ ✅ - // org ❌ ❌ - // none ❌ ❌ - // - // all live in `resolveInjectedSystemColumns` now, so the author-time linter - // reaches the same verdict instead of guessing (#5378). - const wantOwner = plan.owner; - const wantOwningBusinessUnit = plan.owningBusinessUnit; - // [ADR-0142 D3] The queue column rides with the owner anchor (the plan's - // `queue` is its `owner`): "sits in Q" is `queue == Q AND owner_id IS NULL`. - const wantQueue = plan.queue; - - // Nothing to INJECT — the two opt-out rows (`systemFields: false`, - // `managedBy: 'better-auth'`) and objects whose every column is already - // declared. - // - // [#8608] It still routes through {@link provisionTenantScopeIndex}, like - // every other exit below. An object that injects nothing can still CARRY the - // tenant column — the author declares their own `organization_id` — and the - // wall composes its predicate on exactly that column (measured, not inferred: - // plugin-security's `tenancyDisabled` reads `tenancy.enabled` and - // `systemFields.tenant` only, so `systemFields: false` leaves the wall live). - // Returning `schema` here was the last exit that decided the index by NOT - // asking, which is how the withholding survived #8375's convergence. - if (!wantTenant && !wantAudit && !wantOwner && !wantOwningBusinessUnit && !wantQueue) { - return provisionTenantScopeIndex(schema, opts); - } - - const additions: Record = {}; - // Platform-owned field settings that must WIN over a declared field, rather - // than lose to it like `additions` does (#4447). - const overrides: Record = {}; - - if (wantTenant && !schema.fields?.organization_id) { - // [#6562] The authorable shape is the shared table's, spread verbatim. - // - // [#6810] Nothing is spread ON TOP of it any more. This line used to read - // `{ ...TENANT_SCOPE_FIELD_DEF, indexed: opts.multiTenant }`, and `indexed` - // is the one key that was never a `FieldSchema` key at all — removed in the - // 16.x line (#2377, ADR-0049) because a field-level index flag built no - // index, and `FieldSchema` is a `strictObject`, so a document carrying it is - // rejected BY NAME ("never a FieldSchema key; a field-level index flag built - // no index"). #6562's reasoning for leaving it here — that its only consumer - // reads the REGISTERED schema, never a served document — held for the - // consumer and not for the document: `registerObject` runs this function - // BEFORE storing and `getItem('object', …)` serves that post-injection - // document, so the key reached `/meta`, where `decorateMetadataItem` - // re-parsed the served body and stamped `_diagnostics: { valid: false, - // errors: [{ path: 'fields.organization_id', code: 'unrecognized_keys' }] }` - // on EVERY registry-backed object — both tenancy modes, both read exits. A - // defect report the platform wrote about its own column, on a document the - // author never wrote and could not fix, in the channel Studio renders - // invalid-metadata banners from. - additions.organization_id = { ...TENANT_SCOPE_FIELD_DEF }; - - // [#6810] The tenant INDEX that goes with this column is no longer decided - // here. It is {@link provisionTenantScopeIndex}, called on the merged result - // at the tail of this function — see #8375 for why a decision spelled out at - // this one site could not be reached by the `/meta` read exits. - } - - if (wantAudit) { - for (const name of AUDIT_PROVENANCE_FIELDS) { - const declared = (schema.fields as Record | undefined)?.[name]; - if (!declared) { - additions[name] = AUDIT_FIELD_DEFS[name]; - continue; - } - // [#4447] The audit family's GOVERNANCE is platform-owned, so a declared - // `created_at` cannot make the audit anchor client-writable. - // - // The injection above is skipped when the object already carries the - // field, and the merge below lets `schema.fields` win — correct for an - // authored business field, wrong for this family. It is how `created_at` - // became writable on an ordinary PATCH: the showcase artifact ships a - // materialized `created_at` carrying only FieldSchema DEFAULTS - // (`readonly: false`), which shadowed `AUDIT_FIELD_DEFS.created_at` - // (`readonly: true`), so the engine's `stripReadonlyFields` had nothing - // to key off and the forged value was written straight through — with no - // `droppedFields` either, because from the platform's point of view - // nothing was dropped. - // - // Its two siblings only LOOKED protected: the audit hook force-advances - // `updated_at`/`updated_by` on every update, so a forged value is - // overwritten rather than refused. `created_at` is insert-only, so - // nothing overwrote it — one field out of the trio genuinely unguarded. - // - // Presentation stays the author's (label, description, hidden, group, - // ordering …); only the keys that decide WHO MAY WRITE IT are forced. - // That leaves the deliberate back-dating path intact: `preserveAudit` - // (#3479/#3493) and `isSystem` writes still reinstate the original - // timeline, because they are checked downstream of `readonly`, not by it. - overrides[name] = { ...declared, ...AUDIT_FIELD_GOVERNANCE[name] }; - } - } - - // Canonical reassignable owner. `system: true` marks it platform-provided - // (so tooling/migrations recognise it), but — unlike the audit `*_by` - // lookups — it is NOT `readonly`: ownership is transferable, so it stays - // editable in forms and assignable via the API. SecurityPlugin auto-stamps - // it to the acting user on insert when left NULL. - if (wantOwner && !schema.fields?.owner_id) { - additions.owner_id = { ...OWNER_FIELD_DEF }; - } - - // [ADR-0117 D1] Record-level business-unit ownership. Shaped after - // `organization_id` (server-stamped scope anchor), NOT after `owner_id` - // (a user-assignable business field) — the two differ in exactly the keys - // that decide who may write and whether it renders: - // - // • `readonly: true` — D3 requires the value to be derived and validated - // server-side (`record.organization_id` must equal the unit's org), so a - // client-supplied value is overwritten, never trusted. Reassignment is a - // TRANSFER-class operation gated on `allowTransfer` (D4), which has not - // landed; until it does, no client write path may set this column. - // • `hidden: true` — nothing stamps the column yet (D2's policy is - // undecided, D8's backfill unwritten), so it is provisioned-but-inert. - // Surfacing a permanently-NULL lookup on every business object's layout - // would advertise a capability the runtime does not deliver (Prime - // Directive #10). Presentation is a one-key flip for the PR that lands - // stamping — a capability grant is not. - // • `required: false` — nullable, like every other injected column. - // - // ⚠️ None of the three presumes ADR-0117 D2 (`pinned`/`follow_owner`/ - // `transferable`), which is NOT YET RULED. They are the fail-closed shape: - // they grant no capability, so every D2 outcome remains reachable. D2's - // `owningBusinessUnit.required` is an INSERT-time rejection rule, not column - // nullability — it cannot be read off this definition either. `system: true` - // writes (the future stamping middleware, seeds) are checked downstream of - // `readonly`, so the flag blocks clients without blocking the platform — - // exactly how `organization_id` is stamped today. - // - // No index: the hierarchical predicate that would use one (`… IN (units)`, - // D6) ships with the enterprise scope resolver. An index on a column nothing - // writes and nothing filters is dead weight — the same reasoning that gates - // `organization_id`'s index on `multiTenant`. - if (wantOwningBusinessUnit && !schema.fields?.[OWNING_BUSINESS_UNIT_FIELD]) { - additions[OWNING_BUSINESS_UNIT_FIELD] = { ...OWNING_BUSINESS_UNIT_FIELD_DEF }; - } - - // [ADR-0142 D3] The ownership-queue column: the queue machine name a record - // with no owner SITS IN, and an owned record's home queue. Text, not a lookup - // (a declared queue has no row to point at); `readonly: false` like - // `owner_id`, because its writers are adjudicated where the owner's are — the - // assignment seam stamps it as the platform, and a client's move is a - // transfer-class write at plugin-security's step 3.5. `hidden: true`: the - // Console's queue affordances read it by name, and a raw machine name on every - // layout is not a reading a person needs. Landed with its writers, never ahead - // of them (ADR-0078). A declared `queue` never reaches this line on an - // eligible object: `registerObject` refuses it ({@link objectQueueColumnRefusal}), - // so the `!schema.fields` guard below only ever admits the platform's own - // definition coming back through a re-registration. - if (wantQueue && !schema.fields?.[QUEUE_FIELD]) { - additions[QUEUE_FIELD] = { ...QUEUE_FIELD_DEF }; - } - - // [#8375] The tenant INDEX is decided over the MERGED result, never over the - // input — so this function and the read-exit seam ask the same question of the - // same document shape. Both cases below route through it, including the one - // that adds no field at all (an object whose every system column is already - // declared still owes the index decision). - if (Object.keys(additions).length === 0 && Object.keys(overrides).length === 0) { - return provisionTenantScopeIndex(schema, opts); - } - - return provisionTenantScopeIndex( - { - ...schema, - // `additions` LOSE to an author's field (a declared `owner_id` is theirs); - // `overrides` WIN over it (the audit family's governance is not authorable). - fields: { ...additions, ...(schema.fields ?? {}), ...overrides }, - }, - opts, - ); -} - -/** - * [#6810, made shareable by #8375] Declare the platform's tenant-scope index on - * an object that carries a platform-provisioned `organization_id`, on a - * multi-tenant deployment — the ONE implementation of that decision. - * - * ## Why it is a function and not four lines inside `applySystemFields` - * - * It was those four lines, and that is precisely how the stamp diverged. The - * `/meta` read exits converge the injected system COLUMNS through - * `applyInjectedSystemColumns` (`@objectstack/metadata-core`, #6562) — which - * cannot reach `applySystemFields`: `@objectstack/objectql` depends on - * `@objectstack/metadata-protocol` depends on `@objectstack/metadata-core`, so - * the import runs UP the dependency graph. So the converger re-implemented the - * half it could reach (the fields map) and silently did not implement the half - * it could not (this index), and `GET /meta/object/:name` served a multi-tenant - * object reporting no tenant index while the registry's own answer carried one - * (#8375). - * - * The fix is not an `indexes` line added to that copy — a second implementation - * is what drifted in the first place. It is this: ONE function, called by the - * producer below and by {@link SchemaRegistry.materializeBaseLayer}, which is - * the seam #8268 built so a stamp converges at every read exit the day it is - * added. `__search` converges by delegating to the registry and has never - * drifted; this stamp now converges the same way. - * - * ## The predicate, stated exactly - * - * "The object carries a tenant column" — not "this call just injected one", and - * [#8459] not "the PLATFORM provisioned it". The first distinction is the whole - * reason the decision moved out of the injection branch: at a read exit the - * column is ALREADY present (`governServedItem` runs - * `applyInjectedSystemColumns` before the seam), so a condition reading - * `!schema.fields?.organization_id` is false exactly where the convergence is - * needed. {@link carriesTenantScopeColumn} answers the question the old nesting - * was really asking, at either site, in either order. - * - * The second distinction is the 2026-08-13 maintainer ruling on #8459. This - * predicate also required the column to be the platform's OWN definition - * (`isInjectedColumnDefinition`, byte-for-byte), which #8375 preserved exactly - * rather than widening inside a convergence fix. The effect was that an author - * who declared their own `organization_id` — adding a label, making it - * `required`, pointing it at their own org table — kept the column and silently - * lost the index on it, while `computeTenantLayer0Filter` (plugin-security) - * went on AND-composing `organization_id = ` onto essentially every read of - * that object. The deployment's hottest predicate, unindexed, reached by an - * additive-looking authoring move that removed a guarantee the author never knew - * they held. - * - * The ruling is one rule, stated once: on a walled deployment the wall's - * predicate is indexed, whoever typed the column. ⛔ Not a type judgement — an - * `organization_id` declared as `text` gets the index too. Inspecting the - * declared type to decide whether it is "really" the tenant anchor was - * considered and REJECTED: it is a third predicate at the site where #8375 just - * reduced two to one, and its boundary is itself something an author gets - * subtly wrong (a `text` org code would lose the index and look fine). - * - * ⚠️ Recorded gap, deliberately left uncovered by that ruling: an author who - * wants NO index on the tenant column has no way to say so. If that need - * appears it becomes an explicit declared key — ⛔ it is not grounds to restore - * the injected-column condition. What exists today for a DIFFERENT index shape - * is {@link declaresTenantIndex}: declare your own single-column tenant index - * and the platform declares none. - * - * `multiTenant: false` declares NO index rather than a false one: on an unwalled - * stack nothing filters by organization, so the index is dead weight — the same - * intent the retired `indexed` flag's value carried (#6810), expressed as - * presence instead of a boolean. - * - * No `name` on the entry: each driver derives its own (SQL's `buildIndexName` is - * table-qualified, which a hardcoded name could not be without colliding across - * tables on Postgres; Mongo's index names are per-collection). `unique` is left - * at its default `false` — a plain lookup index, never a constraint. - * - * Idempotent, via {@link declaresTenantIndex}: it runs twice on every - * registration (once at the tail of `applySystemFields`, once at the seam) and - * the second call is a no-op. Returns `schema` BY REFERENCE when nothing is - * owed, so a registry-sourced body pays a comparison and no copy. - */ - -/** - * [#8375] The index entry the platform declares for the tenant scope column — - * the ONE spelling of it, so the stamp and its write-side strip cannot disagree - * about what "the platform's own entry" looks like. - */ -const TENANT_SCOPE_INDEX: { fields: string[] } = { fields: ['organization_id'] }; - -function provisionTenantScopeIndex( - schema: ServiceObject, - opts: { multiTenant: boolean; platformGlobalObjects?: ReadonlySet }, -): ServiceObject { - if (!opts.multiTenant) return schema; - if (!carriesTenantScopeColumn(schema, opts.platformGlobalObjects)) return schema; - if (declaresTenantIndex(schema)) return schema; - - return { - ...schema, - // [#6810] Author-declared indexes keep their position; the platform's - // tenant index is APPENDED, never merged into or reordering theirs. - // The write-side inverse depends on that append being at the END — - // see {@link SchemaRegistry.stripProvisionedTenantIndexFrom}. - indexes: [...((schema as any).indexes ?? []), { ...TENANT_SCOPE_INDEX }], - } as ServiceObject; -} - -/** - * [#8375, widened by #8459, rebound to the wall by #8608] Is this object - * tenant-scoped — i.e. does the WALL filter it by `organization_id`? - * - * ## One question, one authority - * - * This read `resolveInjectedSystemColumns(schema).tenant` — the spec's - * INJECTION plan — on the reasoning that an object withholding the COLUMN has - * nothing for an index to serve. That reasoning holds for three of the plan's - * opt-out rows and fails on the fourth. `systemFields: false` is the hard - * object-level opt-out, so the plan answers "inject nothing"; plugin-security - * derives `tenancyDisabled` from exactly two clauses — - * - * tenancy.enabled === false || systemFields.tenant === false - * - * — and `systemFields: false` is in neither. An object using the hard opt-out - * while declaring its OWN `organization_id` therefore had the wall predicate - * AND-composed onto essentially every read with no index behind it: the same - * "hottest predicate unindexed" shape #6810 and #8459 exist to prevent, reached - * by a third route. Measured end to end on #8608 rather than inferred — for one - * such object the registry answered `indexes: null` while plugin-security's - * `getReadFilter` answered `{ organization_id: }`. - * - * Triage ruling, 2026-08-14 (option A), applying the standing meta-rule that - * when one question has two disagreeing implementations the GOVERNED side wins - * by default and the ungoverned side rebinds to it: **the wall's derivation is - * authoritative and the index follows it.** ⛔ Option B — teaching - * plugin-security to read `systemFields: false` as tenancy-disabled — was - * REJECTED, because it narrows a wall and only an explicit product ruling can - * do that. So this predicate is now the wall's, in the wall's own terms: - * - * 1. the wall's two clauses, and nothing else, decide "is tenancy off here"; - * 2. the object carries `organization_id` — either the platform provisions it - * (the injection plan) or the author declared it. - * - * `managedBy: 'better-auth'` is deliberately NOT a third clause: the wall does - * not read it either, so a better-auth table that DECLARES `organization_id` - * (`sys_member`) is walled on that column and is now indexed on it. Re-adding - * an exclusion the wall does not have is precisely the drift this rebinding - * closes — and `declaresTenantIndex` already covers the tables that declare - * their own tenant index (`sys_invitation`, `sys_team`). - * - * ⚠️ The clause-1 rows are NOT a leftover of the condition #8459 lifted: an - * object that declares itself non-tenant-scoped is one the wall composes NO - * predicate on (`computeTenantLayer0Filter` returns `null` when - * `tenancyDisabled`), so an index there would serve nothing — the same - * reasoning as `multiTenant: false`, one scope down. What #8459 removed is a - * different condition: that the `organization_id` present be the platform's own - * definition byte-for-byte (`isInjectedColumnDefinition`). Where the column - * comes from is no longer part of the question; whether the object is WALLED - * still is. - * - * ⛔ Do NOT reduce clause 2 to its field-map half alone - * (`fields.organization_id !== undefined`), however closely that reads to the - * ruling's sentence. Measured: it breaks the WRITE path for ordinary - * platform-provisioned objects. The save path strips the injected COLUMNS - * before it strips the materialized stamps - * (`stripMaterializedFromRegistry(type, stripServedSystemColumns(type, item))`, - * `@objectstack/metadata-protocol`), so by the time - * {@link SchemaRegistry.stripProvisionedTenantIndexFrom} re-stamps the - * remainder through this function, the body no longer HAS an - * `organization_id` — a field-map-only predicate answers "not tenant-scoped", - * the re-stamp adds nothing, the lists differ, the strip refuses, and the - * platform's own index entry is baked into `sys_metadata.metadata`, its - * checksum and every history diff (the #4326 regression). The injection-plan - * branch is what answers on a stripped body; the field-map branch only ever - * ADDS objects whose column that plan cannot see. And the addition is safe on - * the write path for the mirrored reason: on exactly those rows the plan also - * strips nothing (`plan.names` is `{ id }`), so the author's declared column is - * still present when the re-stamp asks. - * - * [#15225 → #15813] Briefly exported at module level for the engine's - * bulk-event producer, which answered "is this object walled?" from it — a - * binding of TWO of the wall's three object clauses, structurally blind to - * the deployment's #12699 carve-out (the #15706 finding). That producer now - * reads the Layer 0 verdict plugin-security RECORDS on the operation - * (`OperationContext.tenantLayer0Verdict`) and consults no schema predicate at - * all, so this is private again: the registry's own reading, for the two - * sites above, and ⛔ not a contract for any other package to answer the - * wall from — the wall answers for itself (ADR-0131 D8). - */ -function carriesTenantScopeColumn( - schema: ServiceObject, - platformGlobalObjects?: ReadonlySet, -): boolean { - // Clause 1 — the wall's own two clauses, spelled here because - // plugin-security spells them there (option C, the single exported - // predicate, is bounded to no new `@objectstack/spec` export and no - // plugin-security behaviour change, so the convergence itself is a - // follow-up; `isTenancyDisabled` is already the shared reading of the first). - if (isTenancyDisabled(schema)) return false; - if ((schema as { systemFields?: { tenant?: boolean } }).systemFields?.tenant === false) { - return false; - } - // Clause 2 — there is a column for the wall's predicate to filter on. - // [ADR-0131 D7] The injection plan is asked with the deployment's - // platform-global declaration, the same input `applySystemFields` planned the - // column from: a declared object carries no injected tenant column, so there - // is nothing for an index to serve. - const plan = - platformGlobalObjects && platformGlobalObjects.size > 0 - ? resolveInjectedSystemColumns(schema, { platformGlobalObjects }) - : resolveInjectedSystemColumns(schema); - return ( - plan.tenant || - (schema as { fields?: Record }).fields?.organization_id != null - ); -} - -/** - * [#6810] Is the tenant index already declared on this object? - * - * The append is the one part of this injection that is not naturally - * idempotent — the field injection re-runs harmlessly because - * `!schema.fields?.organization_id` stops it, an array push does not — so an - * author who hand-wrote `indexes: [{ fields: ['organization_id'] }]` must not - * end up with the platform's duplicate beside it. - * - * Matched on the single-column shape this function emits, deliberately: an - * author's composite (`['organization_id', 'code']`) is a leading-column match - * on some dialects and not on others, so it is not treated as a substitute. - */ -function declaresTenantIndex(schema: ServiceObject): boolean { - const declared = (schema as any).indexes; - if (!Array.isArray(declared)) return false; - return declared.some( - (idx: any) => - Array.isArray(idx?.fields) && - idx.fields.length === 1 && - idx.fields[0] === 'organization_id', - ); -} - -/** - * Reconcile a managed object's `enable.apiMethods` against the generic-write - * affordances it actually grants (ADR-0092 / ADR-0103). - * - * A `managedBy` object whose resolved affordances forbid a write verb, yet - * which *also* advertises that verb in `enable.apiMethods`, is internally - * contradictory: the HTTP exposure gate (ADR-0049) would admit the request and - * let it 403 at the engine instead of answering a clean 405, and the metadata - * misrepresents what the API offers. - * - * This is the registration-time backstop that makes the contradiction - * impossible to *ship*: any write verb whose CRUD affordance the object does - * not grant — via the `managedBy` bucket default plus `userActions` overrides, - * exactly as `resolveCrudAffordances` computes for the UI — is stripped, - * with a warning. Reads are never touched. - * - * The judgement itself is NOT here. `checkManagedApiMethodAffordances` - * (`@objectstack/spec/data`) owns the verb → affordance table, and - * `@objectstack/lint`'s `validateManagedApiMethods` — the authoring-time gate - * that reports the same contradiction where the AUTHOR is, rather than in a - * boot log nobody reads (#7521) — reads that same predicate. This function is - * only the registration-time REACTION to it: strip and warn. If a verdict seems - * wrong, fix the predicate, never this reaction — a second table here is the - * declared≠enforced drift the predicate exists to detect. - * - * WARN and strip, never throw — deliberately (the #7521 ruling, 2026-08-11): - * failing registration closed would let one metadata typo kill a control-plane - * boot, which is too harsh for ops. The author-time gate is where this blocks. - * - * Runs for every managed bucket (ADR-0103). Objects that legitimately take - * user-context writes declare `userActions` opening those verbs, so their - * apiMethods are kept: `sys_user` keeps `update` (`userActions.edit: true`; its - * writes are field-whitelist-clamped by the identity guard), and the `system` - * writable set (RBAC link tables, prefs, messaging config) keeps its CRUD - * verbs. Engine-owned `system` / `append-only` objects and the other identity - * tables — which grant no write affordance — derive down to reads only. Any - * future managed object that advertises a write verb it does not permit is the - * drift this backstop catches. - * - * Returns the input unchanged when nothing is stripped; otherwise a new schema - * with a rewritten `enable.apiMethods` (immutable, like {@link applySystemFields}). - */ -export function reconcileManagedApiMethods( - schema: ServiceObject, - opts?: { warn?: (msg: string) => void }, -): ServiceObject { - const conflicts = checkManagedApiMethodAffordances(schema); - if (conflicts.length === 0) return schema; - - const methods = (schema as any).enable.apiMethods as unknown[]; - const strippedIndexes = new Set(conflicts.map((c) => c.index)); - const kept = methods.filter((_m, i) => !strippedIndexes.has(i)); - const stripped = conflicts.map((c) => c.verb); - - const managedBy = (schema as any).managedBy; - // [ADR-0153 D4] On a write-locked bucket (`synced`) `userActions` cannot open - // a write — the parse refuses it — so the remedy is to drop the verbs and - // write through a declared action (D5). Every other bucket keeps its sentence. - const remedy = MANAGED_BY_WRITE_LOCKED_BUCKETS.has(managedBy) - ? `userActions cannot open a write on this bucket (ADR-0153 D4): remove [${stripped.join(', ')}] ` + - `from enable.apiMethods and send writes through a declared action (ADR-0153 D5).` - : `Declare userActions to open a verb the object legitimately takes from a user context.`; - const warn = opts?.warn ?? ((msg: string) => console.warn(msg)); - warn( - `[Registry] Object "${schema.name}" is managedBy:'${managedBy}' but advertised ` + - `generic write verb(s) [${stripped.join(', ')}] in enable.apiMethods its resolved affordances ` + - `do not permit — stripping them (ADR-0092/ADR-0103). ${remedy} Kept: [${kept.join(', ')}]. ` + - `\`os lint\` reports the same contradiction as \`object/managed-api-method-unaffordable\` ` + - `with full authoring context.`, - ); - - return { - ...schema, - enable: { ...(schema as any).enable, apiMethods: kept }, - }; -} - -/** Objects already warned about stripped legacy `apiMethods` (once per object). */ -const warnedLegacyApiMethods = new Set(); - -/** - * Registration-time diagnostic for retired LEGACY `apiMethods` values (#3543, - * P2 of #3391). Since the enum shrink, the 8 legacy verbs (`upsert`/ - * `aggregate`/`history`/`search`/`restore`/`purge`/`import`/`export`) are no - * longer authorable: a declared legacy value is IGNORED — the effective API - * surface derives from the six primitives alone. The Zod parse path already - * strips-and-warns (`stripLegacyApiMethods` in `@objectstack/spec/data`), but - * carries no object name; this registration-time diagnostic adds the per-object - * view for schemas that reach the registry without passing through Zod (raw - * `ServiceObject` literals, out-of-band metadata). Real metadata does not - * upgrade in lockstep with the spec, so this is a PERMANENT compatibility - * diagnostic, not a transition. - * - * Emitted once per object (not per request — the hot path stays free). Pure - * observation: it never mutates the schema (the derivation resolver ignores - * legacy values on its own). - */ -export function warnStrippedLegacyApiMethods( - schema: ServiceObject, - opts?: { warn?: (msg: string) => void }, -): void { - const methods = (schema as any).enable?.apiMethods; - if (!Array.isArray(methods) || methods.length === 0) return; - const legacy = methods.filter((m: string) => (LEGACY_API_METHODS as readonly string[]).includes(m)); - if (legacy.length === 0) return; - const name = String((schema as any).name ?? ''); - if (warnedLegacyApiMethods.has(name)) return; - warnedLegacyApiMethods.add(name); - const remaining = methods.filter((m: string) => !(LEGACY_API_METHODS as readonly string[]).includes(m)); - const warn = opts?.warn ?? ((msg: string) => console.warn(msg)); - warn( - `[Registry] Object "${name}" declares retired legacy apiMethods value(s) ` + - `[${legacy.join(', ')}] in enable.apiMethods — the authorable values are now the six ` + - `primitives only, because every other operation is derived from them or retired, so these ` + - `are IGNORED: the effective API surface derives from the ` + - `six primitives (get/list/create/update/delete/bulk) alone ` + - `(['create','update'] ⇒ upsert/import; ['list'] ⇒ aggregate/search/export; ` + - `['get'] + trackHistory ⇒ history).` + - (remaining.length === 0 - ? ` ⚠ Ignoring them leaves NO primitives — this object's API resolves ` + - `to deny-all (fully closed).` - : '') + - ` Remove the value(s); codemod: node scripts/codemod/apimethods-legacy-to-primitives.mjs.`, - ); -} - -/** Objects already diagnosed for functional completeness (once per object). */ -const warnedFunctionalCompleteness = new Set(); - -/** - * [ADR-0078 Phase 4] Registration-time functional-completeness diagnostic. - * - * The author-time gate (`@objectstack/lint`'s `validate-functional-completeness`) - * only protects metadata that passes through `os build` / `validate` / `lint`. - * The registry is the one choke point EVERY door goes through — declared - * stacks, plugin-provided objects, `extend` contributions, `saveMetaItem`, raw - * `registerObject` calls — including the doors that skip Zod and lint entirely. - * Two shipped instances prove those doors are real, not hypothetical: #3896 - * (Setup authoring inserted `sys_sharing_rule` rows directly, bypassing the - * schema that "required" `criteria`) and cloud's `rowColor.mapping` (an - * `as never` cast bypassed tsc, then the strip-era parse dropped the key). - * - * Same shared predicate as the author-time gate (`checkFieldCompleteness` in - * `@objectstack/spec/kernel`), so the rule ids in this warning are the SAME ids - * the lint reports — an operator or an AI reading the boot log can grep the id - * straight into the docs and the suppression story. Judgement lives only in the - * predicate; if a rule seems wrong, fix it there, never here. - * - * WARN, never throw — deliberately, and not as a soft default: an inert field - * must not kill a boot that thousands of healthy objects share (ADR-0078 §1 - * maps error-severity to "the INSTANCE is dead", not "the system is dead"). - * The author-time gate is where errors block; the registry's job is to make - * sure the silence never survives to runtime unobserved. - * - * Emitted once per object name with every finding aggregated into that one - * line (not per request, not per finding — the hot path stays free and a - * 3-dead-field object is one greppable line, not three). - */ -export function warnFunctionalCompleteness( - schema: ServiceObject, - opts?: { warn?: (msg: string) => void }, -): void { - const fields = (schema as { fields?: Record }).fields; - if (!fields || typeof fields !== 'object') return; - const name = String((schema as { name?: unknown }).name ?? ''); - if (warnedFunctionalCompleteness.has(name)) return; - - const findings: string[] = []; - for (const [fieldName, def] of Object.entries(fields)) { - for (const f of checkFieldCompleteness(def)) { - findings.push(`${fieldName}: [${f.severity}] ${f.rule} — add \`${f.fix}\``); - } - } - if (findings.length === 0) return; - warnedFunctionalCompleteness.add(name); - - const warn = opts?.warn ?? ((msg: string) => console.warn(msg)); - warn( - `[Registry] Object "${name}" registered with ${findings.length} functionally-incomplete ` + - `field(s) — Zod-valid but runtime-DEAD: the consumer silently skips each one, so it reads ` + - `0/null/never-resolves while every authoring surface reports success (ADR-0078). ` + - findings.join(' · ') + - ` — \`os lint\` reports the same rule ids with full context.`, - ); -} - -/** - * [#16319] The `error.code` the field-`type` refusal below carries. - * - * `INVALID_METADATA` — already registered under `@objectstack/objectql` in - * `ERROR_CODE_LEDGER`, and already this package's spelling for "a stored or - * programmatically-supplied metadata body does not satisfy the spec schema" - * (`engine.ts`'s two plugin-view expansion refusals answer with it). ⛔ Not a - * new code: minting one widens the published error-code face, which is a - * Clause-② contract change and not this repair's to take. The CONDITION is - * named by {@link OBJECT_FIELD_TYPE_REFUSED_ERROR_NAME}, which is what a - * consumer discriminates on beyond the code — `INVALID_METADATA` is shared with - * several unrelated refusals in this package. - */ -export const OBJECT_FIELD_TYPE_REFUSED_CODE = 'INVALID_METADATA' as const; - /** * [#16319] A field declaration reached the registration door with a `type` that * is absent, or that is not a member of `FieldType`. @@ -2218,16 +360,6 @@ class ObjectFieldTypeRefusedError extends Error implements ObjectFieldTypeRefusa } } -/** - * {@link ExtensionDefaultRowScopeRefusedError}'s code — `INVALID_METADATA`, - * this package's spelling for "a programmatically supplied metadata body does - * not satisfy the spec schema" (see {@link OBJECT_FIELD_TYPE_REFUSED_CODE}): - * `ObjectExtensionSchema` is strict and declares no `defaultRowScope`, so the - * parse refuses the same body at authoring. ⛔ Not a new code. The condition is - * named by the error's `name`. - */ -export const EXTENSION_DEFAULT_ROW_SCOPE_REFUSED_CODE = 'INVALID_METADATA' as const; - /** * [ADR-0095 D1, 2026-10-18 note; ruling of record 6250748677 on #24164, letter * E′ item 4] An `extend` contribution carried `defaultRowScope`. An object's @@ -2277,15 +409,6 @@ class ExtensionDefaultRowScopeRefusedError extends Error { } } -/** - * {@link ExtensionMembershipKeysRefusedError}'s code — `INVALID_METADATA`, for - * the reason {@link EXTENSION_DEFAULT_ROW_SCOPE_REFUSED_CODE} gives: - * `ObjectExtensionSchema` is strict and declares no `membershipKeys`, so the - * parse refuses the same body at authoring. ⛔ Not a new code. The condition is - * named by the error's `name`. - */ -export const EXTENSION_MEMBERSHIP_KEYS_REFUSED_CODE = 'INVALID_METADATA' as const; - /** * [#25234 ruling A, refusal 10] An `extend` contribution carried * `membershipKeys`. A membership key is declared by the package that owns the @@ -2356,14 +479,6 @@ export function extendContributionRefusal( return undefined; } -/** - * {@link ValidationRuleCopyRefusedError}'s code — `INVALID_METADATA`, for the - * reason {@link EXTENSION_DEFAULT_ROW_SCOPE_REFUSED_CODE} gives: a body this - * registry will not take as written. ⛔ Not a new code. The condition is named - * by the error's `name`. - */ -export const VALIDATION_RULE_COPY_REFUSED_CODE = 'INVALID_METADATA' as const; - /** * [ADR-0029 D9.2a, 2026-10-30 note; ruling of record 6524229156, letter C′] A * tenant body carries a rule of the same `name` as a rule a code layer ships — @@ -2420,14 +535,6 @@ export function objectFieldTypeRefusal(schema: unknown): ObjectFieldTypeRefusal return violation ? new ObjectFieldTypeRefusedError(violation) : null; } -/** - * {@link ObjectQueueColumnRefusedError}'s code — `INVALID_METADATA`, as - * {@link ObjectFieldTypeRefusedError}'s is: a body this registry cannot - * register as written. ⛔ Not a new code; the condition is named by the - * error's `name`. - */ -export const OBJECT_QUEUE_COLUMN_REFUSED_CODE = 'INVALID_METADATA' as const; - /** * [ADR-0142 D3, merge question 15] An object the platform injects `queue` on * (`ownership: 'user'`, the default tier) declared its own field named @@ -2477,263 +584,6 @@ export function objectQueueColumnRefusal(schema: unknown): Error | null { return new ObjectQueueColumnRefusedError(typeof name === 'string' ? name : '(unnamed)'); } -/** - * Platform namespaces that multiple packages may legitimately share, so the - * install-time namespace-uniqueness gate (ADR-0048 Phase 1) must never fire on - * them: the FQN-exempt reserved namespaces (`base`, `system`) plus `sys` - * (system objects such as `sys_metadata` are contributed by many packages — see - * {@link SchemaRegistry.registerNamespace}, which is intentionally many-to-one). - */ -function isShareableNamespace(ns: string): boolean { - return RESERVED_NAMESPACES.has(ns) || ns === 'sys'; -} - -/** - * Sentinel for "this value is not data and does not belong in the record". - * A distinct symbol rather than `undefined`, so a manifest that genuinely - * carries `undefined` is not confused with a value that was dropped. - */ -const NOT_RECORD_DATA = Symbol('objectstack.registry.notRecordData'); - -/** Whether `value` is a bare `{}`-shaped object (or a null-prototype bag). */ -function isPlainDataObject(value: object): boolean { - const proto = Object.getPrototypeOf(value); - return proto === Object.prototype || proto === null; -} - -/** - * One step of {@link toRecordManifest} — project a value, or report that it is - * not data. `seen` carries the ancestors of the current value, so a CYCLE is - * detected at the second visit and the back-reference is dropped rather than - * followed. Siblings are re-projected independently (the ancestor is deleted on - * the way out), so a manifest that legitimately mentions the same sub-object - * twice keeps both copies. - */ -function projectRecordValue(value: unknown, seen: Set): unknown { - if (value === null) return null; - const kind = typeof value; - if (kind === 'string' || kind === 'number' || kind === 'boolean') return value; - // `undefined`, `function`, `symbol`, `bigint` — none of which survive - // `JSON.stringify` as themselves (`bigint` is the one that THROWS). - if (kind !== 'object') return NOT_RECORD_DATA; - - const obj = value as object; - // A `Date` is exotic but has a declared JSON form, so it is data. Copied - // rather than shared: the record must not alias a mutable runtime value. - if (obj instanceof Date) return new Date(obj.getTime()); - if (seen.has(obj)) return NOT_RECORD_DATA; - - if (Array.isArray(obj)) { - seen.add(obj); - const out: unknown[] = []; - for (const entry of obj) { - const projected = projectRecordValue(entry, seen); - // Non-data entries are OMITTED, not held as `null` holes: nothing reads a - // manifest collection by index (every one of them is name-keyed), and a - // `plugins: [null, null, …]` on the wire says less than `plugins: []`. - if (projected !== NOT_RECORD_DATA) out.push(projected); - } - seen.delete(obj); - return out; - } - - if (!isPlainDataObject(obj)) return NOT_RECORD_DATA; - - seen.add(obj); - const out: Record = {}; - for (const [key, entry] of Object.entries(obj)) { - const projected = projectRecordValue(entry, seen); - if (projected !== NOT_RECORD_DATA) out[key] = projected; - } - seen.delete(obj); - return out; -} - -/** - * Project an authored manifest into the SERIALIZABLE RECORD the registry keeps. - * - * {@link SchemaRegistry.installPackage} used to store the caller's object - * verbatim. For a code-defined stack (`objectstack.config.ts`) that object is - * the LIVE runtime one — `plugins: [new ConnectorRestPlugin(), …]` — and a - * plugin instance holds the engine once it initialises. Since the engine grew - * `actionActivation -> store -> engine` that reference closes a CYCLE, so - * `JSON.stringify` of the registry item THREW and every read door that - * serialises a package answered `500 INTERNAL_ERROR` on a stock showcase boot: - * `GET /packages`, `GET /packages/:id`, `GET /meta/package/:id`. Before that - * cycle existed the same doors serialised the whole engine graph into the - * payload instead, which is the same defect with a quieter symptom. - * - * Measured on that boot: of the 26 installed packages exactly ONE manifest key - * was unserializable — `plugins`, on `com.example.showcase` — and only AFTER - * plugin init; the same manifest serialised cleanly during boot. So an - * install-time `JSON.stringify` probe would have called the record healthy. The - * projection therefore drops by SHAPE, at install, and never depends on when it - * is asked. - * - * The registry item is a RECORD, not the runtime. The kernel keeps the live - * object (`ObjectQL.manifests`), and the one reader of `manifest.plugins[]` — - * `ObjectQL.registerApp`'s nested-plugin seam — reads its OWN parameter, never - * the record, so nothing downstream loses a member it was using. - * - * The rule is STRUCTURAL rather than a key denylist, because the fault is not - * "the key is called `plugins`" — it is "a live object reached the record". - * JSON data survives; everything else is dropped: - * - * - primitives, plain objects, arrays and `Date` are data; - * - functions / symbols / bigints are dropped (a function was already - * invisible to `JSON.stringify`; dropping it only makes the record honest, - * and a bigint would have thrown); - * - class instances, `Map`, `Set` and every other exotic object are dropped — - * a declarative manifest has none, and a code-defined stack's are host - * wiring; - * - a reference cycle among plain data is dropped at the back-edge, so a - * self-referencing manifest degrades to a missing field instead of throwing. - * - * ⛔ This is the PRODUCER's repair, not a consumer-side tolerance: no reader is - * taught to survive an unserializable record, because the record is never - * unserializable to begin with (AGENTS.md Prime Directive #12). - */ -function toRecordManifest(manifest: ObjectStackManifest): ObjectStackManifest { - // The ROOT is projected key-by-key unconditionally: a host whose - // `defineStack()` returns a class instance must still yield a record, and - // `projectRecordValue` would drop the whole thing. - if (manifest === null || typeof manifest !== 'object') return manifest; - const seen = new Set([manifest as object]); - const out: Record = {}; - for (const [key, value] of Object.entries(manifest as Record)) { - const projected = projectRecordValue(value, seen); - if (projected !== NOT_RECORD_DATA) out[key] = projected; - } - return out as ObjectStackManifest; -} - -/** - * Rewrite a BARE callable `functions` map entry into the declared form - * (`{ handler, effect }`) before {@link toRecordManifest} projects the body. - * - * ## The under-report this closes - * - * `functions` accepts two authored spellings, and the projection treats them - * unequally through no fault of its own: - * - * - `{ sweepProjectHealth: { handler: fn, effect: 'writes' } }` — the value is - * a plain object, so it survives with its callable dropped: `{ effect: - * 'writes' }`. The function is still REPORTED, minus what cannot serialise. - * - `{ summarizeCompletedTask: fn }` — the value IS the callable, so the whole - * ENTRY is dropped, key and all. The function vanishes from the record. - * - * `examples/app-showcase` ships one of each, so `GET /packages` reported ONE - * function for a package declaring TWO. A machine-readable read door that - * under-reports is a defect independent of how its rows are declared. - * - * ## Why it is fixed HERE and not in the projection - * - * {@link toRecordManifest}'s rule is STRUCTURAL — 「a live object reached the - * record」, ⛔ never a key denylist — and teaching it that a function found - * under `functions.` means something other than a function found - * anywhere else would overturn exactly that. So the repair is at the assembly - * boundary instead: the two spellings are made structurally EQUAL before the - * projection runs, and the projection then leaves `{ effect }` for both. - * - * ⛔ No ref is minted. `packages/cli`'s `lowerCallables` mints refs with - * `uniqueName(base, taken)` and dedupes by function IDENTITY, so a ref minted - * here would not be guaranteed equal to the one `objectstack build` mints — a - * record could then assert a handler that resolves in no sibling module. An - * ABSENT `handler` on a record is the honest statement 「declared here, not - * serialisable」, and `RecordStagePackageBodySchema` in `@objectstack/spec` - * declares exactly that. - * - * `effect` is the declaration schema's OWN default - * ({@link DEFAULT_FLOW_FUNCTION_EFFECT}, `'pure'`, which - * `FlowFunctionDeclarationSchema.effect` carries as `.default(…)`), ⛔ not a - * value invented here: the bare spelling IS that declaration written the short - * way, and `normalizeFlowFunctionEntry` already reads it the same way at boot. - * The ARRAY form needs nothing — its entries are objects that carry their own - * `name`, so the projection keeps them — and its `effect` is `.optional()` with - * no default, so nothing is written where the schema declares nothing. - * - * ⛔ The caller's manifest is never mutated: the live object is what every - * other read in `installPackage` uses, and a copy is returned only when an - * entry really needed rewriting. - */ -function withDeclaredFunctionEntries(manifest: ObjectStackManifest): ObjectStackManifest { - if (manifest === null || typeof manifest !== 'object') return manifest; - const functions = (manifest as { functions?: unknown }).functions; - if (!functions || typeof functions !== 'object' || Array.isArray(functions)) return manifest; - - let rewrote = false; - const declared: Record = {}; - for (const [name, entry] of Object.entries(functions as Record)) { - if (typeof entry === 'function') { - declared[name] = { handler: entry, effect: DEFAULT_FLOW_FUNCTION_EFFECT }; - rewrote = true; - } else { - declared[name] = entry; - } - } - if (!rewrote) return manifest; - return { ...manifest, functions: declared } as ObjectStackManifest; -} - -/** - * [#16159] The ADR-0112 `code` strings this file's three install-time and - * registration refusals carry, as constants a consumer can import. - * - * Every one of the three classes below already tells the reader, in its own - * docblock, that it "carries the ADR-0112 envelope (`code` + `status`)". That - * convention is sound and `instanceof` is not: `@objectstack/objectql` - * declares BOTH realms in its own `exports` (`import` reaches - * `dist/index.mjs`, `require` reaches `dist/index.js`), so a consumer holding - * the other realm's copy of a class gets `instanceof` === false — measured, - * and silent (#14936). A `code` compare is the check that survives crossing - * that boundary. Until now a consumer following the convention had to - * RE-SPELL the string itself, which acquires a `check:error-code-provenance` - * stamp site in the consumer's own package and can then drift from what this - * engine throws with no compile error to say so. - * - * ⛔ The strings are byte-identical to the literals they replace. This moves - * where a spelling lives, never what it says; renaming any of these codes is - * a separate breaking decision and never a rider on this conversion. - * - * The `*_CODE` NAME is load-bearing rather than cosmetic, in two gates at - * once: it is the shape `check:error-code-provenance`'s `constdef` pattern - * can see, and `readonly code = X_CODE;` is the shape - * `check:dispatcher-error-vocabulary` classifies as `classconst` (the three - * rows in `packages/runtime/src/dispatcher-error-vocabulary.ts` move from - * `classfield` to `classconst` with this change, and the scanner resolves the - * constant back to the same value). ⛔ Never rename out of that shape to - * quiet a gate: a spelling a gate cannot see is the failure mode the gate - * exists to catch, not a clean result. - * - * Shape and placement follow the six `*_CODE` constants already in this - * package (`DUPLICATE_RECORD_CODE`, `HOOK_TARGET_REBIND_ERROR_CODE`, - * `HOOK_UNSCOPED_DATA_ACCESS_CODE`, - * `MULTI_UPDATE_HOOK_KEY_DIVERGENCE_CODE`, `EMPTY_CREDENTIAL_REFUSAL_CODE`, - * `SYSTEM_WRITE_ORGANIZATION_REQUIRED_CODE`) — re-exported from the - * `index.ts` barrel and, like all six, deliberately NOT from the lean - * `core.ts` entry, which carries none of them. - * - * ⛔ Deliberately NOT a recognizer factory: #16156 measured that a - * `makeRecognizer(code)` signature still requires every call site to supply - * the code, which RELOCATES the literal rather than removing it. - */ -export const NAMESPACE_CONFLICT_CODE = 'NAMESPACE_CONFLICT' as const; - -/** {@link ArtifactObjectNameConflictError}'s code — ADR-0130 D3, one artifact. */ -export const DUPLICATE_ARTIFACT_OBJECT_NAME_CODE = 'DUPLICATE_ARTIFACT_OBJECT_NAME' as const; - -/** {@link ObjectOwnershipConflictError}'s code — ADR-0029 D3, single owner per name. */ -export const OBJECT_OWNERSHIP_CONFLICT_CODE = 'OBJECT_OWNERSHIP_CONFLICT' as const; - -/** - * {@link DefaultRecordPageConflictError}'s code — one default record page per - * object per deployment (maintainer ruling A on objectstack#23015). The save - * door in `@objectstack/metadata-protocol` stamps the same string for the same - * condition; that package cannot import this one, so it spells it once and the - * ledger lists it under both. - */ -export const DEFAULT_RECORD_PAGE_CONFLICT_CODE = 'DEFAULT_RECORD_PAGE_CONFLICT' as const; - /** * Raised when a package is installed whose `manifest.namespace` is already owned * by a **different** installed package in this installation (ADR-0048 Phase 1). @@ -2795,58 +645,6 @@ export class NamespaceConflictError extends Error { } } -/** One second holder {@link SecurityCatalogNameConflictError} refused. */ -export interface SecurityCatalogNameConflict { - /** The catalog type, as the registry keys it. */ - readonly catalogType: SecurityCatalogType; - /** The name both holders claim. */ - readonly name: string; - /** The package whose registration this refusal stopped. */ - readonly incomingPackageId: string; - /** Who already holds the name. */ - readonly existingHolder: SecurityCatalogHolder; -} - -/** - * The rule both wordings of {@link SecurityCatalogNameConflictError} state, in - * the words the permission set `name` describe (`@objectstack/spec`) and the - * permission sets guide use. [ADR-0150 D2/D9] A permission set is identified - * by its package and name, so its name has one holder per namespace, judged by - * `permissionSetHoldersMeet` (`@objectstack/core`); positions and capabilities - * keep one name per deployment. Text only: which holders are refused is decided - * by the doors, never by this sentence. - */ -const SECURITY_CATALOG_NAME_RULE = - 'Positions and capabilities each hold one name per deployment. A permission set holds one name per ' + - 'namespace: packages of two namespaces may each hold a set of one name, while two packages of one ' + - 'namespace, a package with no namespace, a platform package (`base`, `system`, `sys`), the environment ' + - 'and the platform-requested `guest_portal` share one set of it.'; - -/** - * The cold-boot wording of {@link SecurityCatalogNameConflictError}: every - * package registered before the environment catalog hydrated, so each line - * names the package that declares the name and the holder it meets, and the - * remedy is stated for an operator restarting a deployment, not for an install. - */ -function coldBootConflictMessage(conflicts: readonly SecurityCatalogNameConflict[]): string { - const lines = conflicts.map( - (c) => - `package "${c.incomingPackageId}" declares the ${securityCatalogTypeLabel(c.catalogType)} "${c.name}", ` + - `already held by ${describeSecurityCatalogHolder(c.existingHolder)}`, - ); - return ( - `Security catalog name conflict at boot: ${conflicts.length === 1 ? 'a name' : `${conflicts.length} names`} ` + - `a configured package declares ${conflicts.length === 1 ? 'is' : 'are'} already held by another holder — ` + - `${lines.join('; ')}. ${SECURITY_CATALOG_NAME_RULE} Under that rule the environment's item and the ` + - `package's share one definition of the name, so with two holders the environment's stored item would be ` + - `served in place of the package's definition. The environment catalog loads from ` + - `sys_metadata before this check, so the boot is refused. Rename the item in the package, or rename or ` + - `delete the environment's item (its environment-wide sys_metadata row: \`os migrate security-catalog-overlays\` ` + - `lists every such row with no server running, and with --apply deletes them), then restart. ` + - `See ADR-0048.` - ); -} - /** * Raised when a package registers a position, permission set or capability * whose name another holder already holds — an installed package, the @@ -2919,49 +717,6 @@ export class SecurityCatalogNameConflictError extends Error { } } -/** One second holder {@link BareNameConflictError} refused (or a cold boot reported). */ -export interface BareNameConflict { - /** The covered type, as the registry keys it. */ - readonly type: BareNameOneHolderType; - /** The name both holders claim. */ - readonly name: string; - /** The package whose registration this refusal stopped (at a cold boot: a later-registered holder). */ - readonly incomingPackageId: string; - /** Who already holds the name. */ - readonly existingHolder: BareNameHolder; - /** The rename that clears it — the incoming package's namespace prefixed — when it has one. */ - readonly rename?: string; - /** [#31697] For an action bound to an object under a built-in key ({@link BUILT_IN_BOUND_ACTION_KEYS}): that object. */ - readonly objectName?: string; -} - -/** One conflict's line, the same at every door. */ -function bareNameConflictLine(c: BareNameConflict): string { - const what = c.objectName !== undefined - ? `action "${c.name}" bound to "${c.objectName}"` - : `${bareNameTypeLabel(c.type)} "${c.name}"`; - return ( - `package "${c.incomingPackageId}" declares the ${what}, ` + - `already held by ${describeBareNameHolder(c.existingHolder)}` + - (c.rename ? ` — rename it to "${c.rename}" in "${c.incomingPackageId}"` : '') - ); -} - -/** Why a second holder is refused, for the types `conflicts` carry (`bareNameReason`). */ -const bareNameReasonFor = (conflicts: readonly BareNameConflict[]): string => - bareNameReason(conflicts.map((c) => c.type)); - -/** The cold-boot warning for the names two installed packages already hold (`bare-name-one-holder.ts`). */ -export function bareNameColdBootWarning(conflicts: readonly BareNameConflict[]): string { - return ( - `Bare-name conflict at boot: ${conflicts.length === 1 ? 'a second holder' : `${conflicts.length} second holders`} ` + - `among the installed packages — ${conflicts.map(bareNameConflictLine).join('; ')}. ${bareNameReasonFor(conflicts)} ` + - `The boot continues and changes nothing about which holder each caller is served; a further package ` + - `declaring the name is refused at install. Rename the item in one package (its namespace prefixed, as ` + - `above), or uninstall one of the two packages. See ADR-0048.` - ); -} - /** * Raised when a package registers an item of a covered type (a picklist, * mapping, dataset, API or object-less action) or binds a function whose name @@ -3052,21 +807,6 @@ export function sealBareNameHoldersAtColdBoot( return conflicts; } -/** One second default record page {@link DefaultRecordPageConflictError} refused. */ -export interface DefaultRecordPageConflict { - /** The object both pages are the default record page of. */ - readonly object: string; - /** - * The app both pages are the record page of `object` inside (`PageSchema.apps`, - * ruling A on objectstack#28979); absent for the deployment's default. - */ - readonly app?: string; - /** The page whose registration this refusal stopped. */ - readonly incoming: DefaultRecordPageHolder; - /** The page that already holds the object's default record page. */ - readonly existing: DefaultRecordPageHolder; -} - /** * Raised when a package registers a record page as the default record page of * an object another page already holds — another package's page, an @@ -3139,15 +879,6 @@ export class DefaultRecordPageConflictError extends Error { } } -/** - * {@link MisplacedDefaultPageError}'s code — `INVALID_METADATA`, already this - * package's spelling for "a programmatically supplied metadata body does not - * satisfy the spec schema" (see {@link OBJECT_FIELD_TYPE_REFUSED_CODE}): the - * page parse refuses the same body at `os validate` and at save. ⛔ Not a new - * code. The condition is named by the error's `name`. - */ -export const MISPLACED_DEFAULT_PAGE_CODE = 'INVALID_METADATA' as const; - /** * Raised when a package registers a page declaring `isDefault: true` that is * not a record page bound to an object (maintainer ruling A on @@ -3213,132 +944,17 @@ export function findEnvironmentHeldSecurityCatalogNames(registry: SchemaRegistry * reading of "who holds a name", never a second one beside the cold boot's). * * The rule each entry obeys is the cold boot's, because it is the same private - * reading: a package holds a name through an item registered under it (its - * composite slot) or its install claim, never through the bare slot; only the - * types the environment catalog can hold are read; built-in names are skipped. - * Sorted by type (positions first), then name, as the cold-boot refusal lists - * its conflicts. Read-only: it records nothing. - */ -export function findPackageHeldSecurityCatalogNames( - registry: SchemaRegistry, -): Array<{ readonly catalogType: SecurityCatalogType; readonly name: string; readonly packageIds: readonly string[] }> { - return registry['packageHeldSecurityCatalogNames'](); -} - -/** - * [ADR-0130 D1] What the install gate is told about the artifact now installing. - * - * `installPackage` has no notion of which release artifact a manifest arrived - * in, and ADR-0130 D8 deliberately adds **no** owner/publisher field to the - * manifest to give it one. So the knowledge is threaded as an install-time - * SCOPE: the load path that reads an artifact (`manifest.register()` → - * `resolveArtifactPackageOrder` → `registerApp`) already holds the artifact's - * package list, and passes it down. - * - * Two consequences worth stating, because both are load-bearing: - * - * - **It is per-install, not stored.** Nothing persists a co-ownership claim, - * so nothing can drift from the artifact that is the claim (D8's objection to - * a manifest field, applied to the runtime). - * - **Absent scope means today's behaviour, structurally.** Every caller that - * installs ONE package — `protocol.installPackage`, `POST /packages`, a bare - * `registerApp` — passes no scope, so the co-owner set is empty and both - * halves of the ADR-0130 D1/D3 pair are no-ops. A single-`manifest` artifact - * passes a one-element scope, whose only member is the package itself, which - * the gate excludes anyway (D7). - */ -export interface ArtifactInstallScope { - /** - * The ids of every package delivered by the artifact currently installing — - * read through {@link artifactPackageId}, so they are the same strings the - * registry keys installed packages by. - */ - readonly packageIds: readonly string[]; -} - -/** - * Why two packages co-own one namespace at the install gate: - * - * - `artifact` — the same release artifact delivers both (ADR-0130 D1); - * - `dependency` — one declares the other in `manifest.dependencies` (ADR-0141 D5); - * - `sibling` — both declare one installed package that holds the namespace (ADR-0141 D5). - */ -export type CoOwnershipSource = 'artifact' | 'dependency' | 'sibling'; - -/** - * The object names one manifest CLAIMS OWNERSHIP of at install time — the names - * `registerApp` is about to hand `registerObject` with `ownership: 'own'`. - * - * Both authored shapes are read, because `registerApp` reads both: an array of - * object definitions, or a name-keyed map (whose KEY is the name — `registerApp` - * overwrites `objDef.name` with it). - * - * ⛔ Glob strings are skipped, not treated as names. `ManifestSchema.objects` is - * `z.array(z.string())` — file patterns — and an unassembled manifest that still - * carries them registers no object here; a pattern is not an object name, and - * refusing on one would refuse a package that claims nothing. - * - * ⛔ `objectExtensions` are not read: an extension contributes fields to an - * object it does not own (`ownership: 'extend'`), which is the co-ownership - * ADR-0130 exists to permit, not the collision it refuses. - */ -function declaredOwnedObjectNames(manifest: ObjectStackManifest): string[] { - const objects = (manifest as { objects?: unknown }).objects; - if (!objects || typeof objects !== 'object') return []; - if (Array.isArray(objects)) { - return objects - .map((o) => (o && typeof o === 'object' ? (o as { name?: unknown }).name : undefined)) - .filter((n): n is string => typeof n === 'string' && n !== ''); - } - return Object.keys(objects as Record).filter((n) => n !== ''); -} - -/** - * [ADR-0130 D3] Raised when two packages delivered by ONE release artifact both - * claim the same object name. - * - * This refusal is the other half of the D1 gate relaxation, and D3 specifies the - * pair as a machine constraint rather than an instruction because this is the - * only part of ADR-0130 that can reach customer data. Today's namespace - * exclusivity is silently carrying a second guarantee: ADR-0048 §3.2 grounds the - * namespace gate on "two packages with namespace `crm` both try to create - * `crm_account` and the second fails at the DB", so "no two packages share a - * namespace" has been proxying for **"no two packages define the same object - * name."** Relax the first without adding the second and two co-owning packages - * defining `crm_account` produce either a duplicate `CREATE TABLE` or — - * driver-dependent — one package silently overwriting the other's table - * definition. - * - * So the check is install-time and ahead of every mutation `installPackage` - * makes, which puts it ahead of all DDL: the refusal lands before the second - * package is recorded at all, rather than half-applying an install and blowing - * up at table creation. - * - * Carries the ADR-0112 envelope (`code` + `status`) — the shape this - * repository's rejection tests assert against, never a bare throw. - */ -/** - * The words {@link ArtifactObjectNameConflictError} uses per co-ownership - * source, so a refusal between packages of different artifacts never claims - * they arrived in one (ADR-0141 D5). + * reading: a package holds a name through an item registered under it (its + * composite slot) or its install claim, never through the bare slot; only the + * types the environment catalog can hold are read; built-in names are skipped. + * Sorted by type (positions first), then name, as the cold-boot refusal lists + * its conflicts. Read-only: it records nothing. */ -const CO_OWNERSHIP_WORDING: Record = { - artifact: { - conflict: 'inside one release artifact', - relation: 'delivered by the same artifact', - allowed: 'Sharing a namespace across packages in one artifact is allowed (ADR-0130 D1)', - }, - dependency: { - conflict: 'between packages that share a namespace through a declared dependency', - relation: 'which shares the namespace because one of the two declares the other in `manifest.dependencies`', - allowed: 'Sharing a namespace through a declared dependency is allowed (ADR-0141 D5)', - }, - sibling: { - conflict: 'between sibling packages that share a namespace under one declared parent', - relation: 'which shares the namespace because both declare the same installed package in `manifest.dependencies`', - allowed: 'Sharing a namespace between modules of one declared parent is allowed (ADR-0141 D5)', - }, -}; +export function findPackageHeldSecurityCatalogNames( + registry: SchemaRegistry, +): Array<{ readonly catalogType: SecurityCatalogType; readonly name: string; readonly packageIds: readonly string[] }> { + return registry['packageHeldSecurityCatalogNames'](); +} export class ArtifactObjectNameConflictError extends Error { readonly code = DUPLICATE_ARTIFACT_OBJECT_NAME_CODE; @@ -3560,17 +1176,6 @@ export class ObjectOwnershipConflictError extends Error { } } -/** One object {@link PackageUninstallExtendedError} names, with the packages extending it. */ -interface ExtendedObjectOfUninstall { - /** The object's fully-qualified name, as the registry keys it. */ - readonly objectName: string; - /** Every OTHER package contributing an `extend` to it, in registration order. */ - readonly extenders: readonly string[]; -} - -/** How many extended objects the refusal spells out before it counts the rest. */ -const EXTENDED_OBJECTS_LISTED = 5; - /** * [#23505] The uninstall refusal of ADR-0029: a package cannot leave while * another package `extend`s an object it owns (a manifest's @@ -3716,257 +1321,6 @@ export { isCodeArtifactBody }; */ export { ITEM_KEY_DISCRIMINATORS }; -/** - * Separator between an item's name and its bundle discriminator inside a - * storage key: `pkg:auth.welcome@zh-CN`. - * - * `@` is unambiguous here in both directions. A discriminated type's `name` can - * never contain it (`EmailTemplateDefinitionSchema` pins dotted snake_case), - * and a scoped package id that does (`@acme/crm:auth.welcome@zh-CN`) is still - * parsed correctly because {@link bundleBaseKey} only looks for the separator - * AFTER the last `:`. - */ -const BUNDLE_KEY_SEPARATOR = '@'; - -/** `pkg:auth.welcome` + `zh-CN` → `pkg:auth.welcome@zh-CN`. */ -function withDiscriminator(baseKey: string, value: string): string { - return `${baseKey}${BUNDLE_KEY_SEPARATOR}${value}`; -} - -/** - * Strip a storage key's bundle suffix: `pkg:auth.welcome@zh-CN` → - * `pkg:auth.welcome`. Keys of undiscriminated types come back unchanged, and so - * does a scoped package id's own `@` (the search starts after the last `:`). - */ -function bundleBaseKey(key: string): string { - const at = key.indexOf(BUNDLE_KEY_SEPARATOR, key.lastIndexOf(':') + 1); - return at === -1 ? key : key.slice(0, at); -} - -/** One member of an i18n bundle, as stored. */ -type BundleEntry = { key: string; item: any }; - -/** - * [#33127, ADR-0150 D4 "registry overlay slot (ADR-0005)"] The mark that opens - * and separates a package overlay slot's storage key: the slot of the stored - * row of `(type, name[, member])` bound to one package, beside the bare slot - * (a package-less row) and the composite slots (code artifacts). A NUL never - * occurs in a package id or a name, so no reader of the other two key forms - * mistakes this one for its own: it is never a bare key (`name`, `name@member`) - * and never ends `:name` ({@link bundleBaseKey}, {@link collectBundle}, the - * composite scans), so it adds no holder and is no artifact. - */ -const OVERLAY_SLOT_MARK = '\u0000'; - -/** `com.acme.alpha` + `auth.welcome@zh-CN` → the package overlay slot key of that member. */ -function overlaySlotKey(packageId: string, bareKey: string): string { - return `${OVERLAY_SLOT_MARK}${packageId}${OVERLAY_SLOT_MARK}${bareKey}`; -} - -/** The package and the bare key of a package overlay slot key, or `undefined` for any other key. */ -function parseOverlaySlotKey(key: string): { packageId: string; bareKey: string } | undefined { - if (!key.startsWith(OVERLAY_SLOT_MARK)) return undefined; - const sep = key.indexOf(OVERLAY_SLOT_MARK, 1); - return sep < 0 ? undefined : { packageId: key.slice(1, sep), bareKey: key.slice(sep + 1) }; -} - -/** - * [#33127] The package overlay slots of `(name)` in `collection`, every member - * of a bundled type (`disc`), in the store's order: the stored rows bound to a - * package, one slot per row package. - */ -function overlaySlotEntriesOf(collection: Map, name: string, disc?: { field: string; canonical: string }): BundleEntry[] { - const out: BundleEntry[] = []; - for (const [key, item] of collection) { - const slot = parseOverlaySlotKey(key); - if (slot === undefined) continue; - if ((disc ? bundleBaseKey(slot.bareKey) : slot.bareKey) === name) out.push({ key, item }); - } - return out; -} - -/** The package a bare-slot or package overlay slot entry's stored row is bound to: the slot's package, else the entry's stamp. */ -function storedRowPackageOf(entry: BundleEntry): string | undefined { - const slot = parseOverlaySlotKey(entry.key); - if (slot !== undefined) return slot.packageId; - const stamped = (entry.item as { _packageId?: unknown } | null | undefined)?._packageId; - return typeof stamped === 'string' && stamped !== '' && stamped !== 'sys_metadata' ? stamped : undefined; -} - -/** - * [ADR-0150 D4] The mark that opens and separates a package-less action's - * owner-key slot. An action is identified by `(owner, owning object, name)`, - * so the platform's action bound to `lead` and the environment's object-less - * action of the same name are two items under two keys, never one contested - * name. The bare slot `name` holds the package-less action registered last; - * each package-less action of another owner key it displaced keeps its own - * slot under this mark, one per owner key. Like {@link OVERLAY_SLOT_MARK}, a - * control character never occurs in an object or action name, so no reader of - * the other key forms mistakes this one for its own: it is never a bare key - * and never ends `:name`, so it adds no package holder and is no artifact. - */ -const PACKAGELESS_ACTION_SLOT_MARK = '\u0001'; - -/** [ADR-0150 D4] A package-less action's owner key: its object, or `global` for every object-less spelling. */ -function packagelessActionOwnerKey(action: unknown): string { - const owner = standaloneActionOwnerKey(action); - return isObjectLessActionKey(owner) ? GLOBAL_ACTION_OBJECT_KEY : owner; -} - -/** `lead` + `approve` → the owner-key slot of the package-less `approve` bound to `lead`. */ -function packagelessActionSlotKey(ownerKey: string, name: string): string { - return `${PACKAGELESS_ACTION_SLOT_MARK}${ownerKey}${PACKAGELESS_ACTION_SLOT_MARK}${name}`; -} - -/** - * [ADR-0150 D4] The owner-key slots of the package-less action `name` - * ({@link packagelessActionSlotKey}), in the store's order: the package-less - * actions of the name beside the one in the bare slot, one per owner key. - */ -function packagelessActionSlotEntriesOf(collection: Map, name: string): BundleEntry[] { - const out: BundleEntry[] = []; - for (const [key, item] of collection) { - if (!isPackagelessActionSlotKey(key)) continue; - const sep = key.indexOf(PACKAGELESS_ACTION_SLOT_MARK, 1); - if (sep > 0 && key.slice(sep + 1) === name) out.push({ key, item }); - } - return out; -} - -/** Is `key` a package-less action's owner-key slot ({@link packagelessActionSlotKey})? */ -function isPackagelessActionSlotKey(key: string): boolean { - return key.startsWith(PACKAGELESS_ACTION_SLOT_MARK); -} - -/** - * [ADR-0150 D4] Is a package-less action registration a stored row — the - * environment's `sys_metadata` hydration or write-through — rather than code - * the host or a plugin registered with no package? A stored row is - * tenant-authored (ADR-0010 `_provenance: 'org'`), or wears the envelope of - * the package whose action it customizes (`_packageId`, which code registering - * with no package never carries). - */ -function isStoredActionRow(item: unknown): boolean { - if (isTenantAuthored(item)) return true; - const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; - return typeof stamped === 'string' && stamped !== ''; -} - -/** - * [#32408, contract review F2, delta review N1] What a door's registration - * brings to a bundled name: the package and the members it adds to its - * composite slot. - */ -interface BundleArrival { - readonly packageId: string; - readonly members: ReadonlySet; -} - -/** - * `collection` with `arrival`'s members in its composite slot of `name`, as a - * copy (the registry's own map is never touched): a member already stored is - * kept as stored, a new one is a stub carrying its discriminator, which is - * all a holder's member read takes from it. - */ -function withArrivingMembers( - collection: Map, - name: string, - disc: { field: string; canonical: string }, - arrival: BundleArrival, -): Map { - const copy = new Map(collection); - for (const member of arrival.members) { - const key = withDiscriminator(`${arrival.packageId}:${name}`, member); - if (!copy.has(key)) copy.set(key, { name, [disc.field]: member }); - } - return copy; -} - -/** - * The members of `(type, name)`'s bundle, grouped by the SAME precedence tiers - * {@link SchemaRegistry.getItem} applies to an undiscriminated type — so - * bundling changes WHICH ROWS EXIST, never which layer wins: - * - * - `bare` — bare-key group (`name@`): the ADR-0005 runtime/DB overlay - * - `local` — the `currentPackageId` composite group (ADR-0048 prefer-local) - * - `other` — every other composite group, in Map insertion order - * - * Within a tier the `canonical` member is first, then insertion order — so a - * bare-name read of a bundle is decided by the spec's declared fallback locale - * rather than by which locale the author happened to declare first. - */ -function collectBundle( - collection: Map, - name: string, - disc: { field: string; canonical: string }, - currentPackageId?: string, -): { bare: BundleEntry[]; local: BundleEntry[]; other: BundleEntry[] } { - const bare: BundleEntry[] = []; - const local: BundleEntry[] = []; - const other: BundleEntry[] = []; - const localBase = currentPackageId ? `${currentPackageId}:${name}` : undefined; - for (const [key, item] of collection) { - const base = bundleBaseKey(key); - if (base === name) bare.push({ key, item }); - else if (base.endsWith(`:${name}`)) (localBase && base === localBase ? local : other).push({ key, item }); - } - const canonicalFirst = (entries: BundleEntry[]) => { - const idx = entries.findIndex((e) => discriminatorValue(e.item, disc.field) === disc.canonical); - return idx <= 0 ? entries : [entries[idx], ...entries.filter((_, i) => i !== idx)]; - }; - return { bare: canonicalFirst(bare), local: canonicalFirst(local), other: canonicalFirst(other) }; -} - -/** - * [#22877] `entries` narrowed to ONE member of their bundle, order kept. An - * entry's member is its discriminator value, or the canonical member for an - * item that declares none — the value {@link SchemaRegistry.registerItem} keys - * it by. `member` is a discriminator value as a caller names it (`'zh-CN'`); a - * blank one names the canonical member, as `itemDiscriminator` reads a body. - */ -function entriesOfMember( - entries: readonly BundleEntry[], - disc: { field: string; canonical: string }, - member: string, -): BundleEntry[] { - const wanted = member.trim() || disc.canonical; - return entries.filter((e) => (discriminatorValue(e.item, disc.field) || disc.canonical) === wanted); -} - -/** - * [#31575] One answer out of bundle `entries` already in tier order, as - * {@link SchemaRegistry.getItem} picks it: the named `member`, else the - * canonical member, else the first entry. - */ -function pickBundleMember( - entries: readonly BundleEntry[], - disc: { field: string; canonical: string }, - member: string | undefined, -): T | undefined { - if (member !== undefined) return entriesOfMember(entries, disc, member)[0]?.item as T | undefined; - return (entriesOfMember(entries, disc, disc.canonical)[0] ?? entries[0])?.item as T | undefined; -} - -/** - * `schema` without the field `name` — `schema` itself, by reference, when it - * does not carry one. Never mutates its input. - */ -function withoutField(schema: ServiceObject, name: string): ServiceObject { - const fields = schema.fields as Record | undefined; - if (!fields || fields[name] === undefined) return schema; - const { [name]: _dropped, ...rest } = fields; - return { ...schema, fields: rest } as ServiceObject; -} - -/** - * [#33050] One item slot (or one claimed name) of a package body, as - * {@link SchemaRegistry.replacingPackageBody} records it: the type and the key, - * joined by a character no type name carries. - */ -function packageBodySlot(type: string, key: string): string { - return `${type}\u0000${key}`; -} - /** * [#28697] The undo record of one registration run under * {@link SchemaRegistry.restoringOnRefusal}: for each registry entry the run @@ -4034,58 +1388,6 @@ class RegistrationRestorePoint { } } -/** - * [#33593] How the follow door is asked of one layer - * ({@link SchemaRegistry.recordCurrencyFollowRefusalOf}): its slot's package, - * the priority it sorts at (the slot's, when omitted), and `before` — what the - * slot held before the change being judged, `null` for an empty slot. - */ -interface FollowDoorLayer { - readonly packageId: string | undefined; - readonly priority?: number; - readonly before: ObjectContributor | null; -} - -/** - * One object layer a registration run registered: the body its registration's - * doors judged, and the contributor it became — the registry's own entry, so - * the run's end can tell a layer a later write of the run took out again, and - * reads the kind the layer holds by then. - */ -interface RunRegisteredObject { - readonly fqn: string; - readonly schema: ServiceObject; - readonly contributor: ObjectContributor; - /** - * What the layer's slot held before the run's first write to it, or `null` - * when it was empty: the run's end judges the layer against the object as - * it was served with this in its place, so a pair the run leaves as it - * found it is not the run's to answer for (#33593 ruling R7). - */ - readonly replaced: ObjectContributor | null; -} - -/** - * [#31697] Does one of an action's stored environment rows (the protocol's - * by-name row read: active, the environment's own) belong to no package and - * bind no object — the row the environment's own object-less item comes - * from? Its `package_id` column decides the binding; its body's owner key - * ({@link standaloneActionOwnerKey}) the object. A body that does not parse - * is no such row. - */ -function storesObjectLessPackagelessRow(rows: readonly { package_id?: string | null; metadata?: unknown }[]): boolean { - return rows.some((row) => { - if (row?.package_id !== null && row?.package_id !== undefined && row?.package_id !== '') return false; - let body: unknown; - try { - body = typeof row?.metadata === 'string' ? JSON.parse(row.metadata) : row?.metadata; - } catch { - return false; - } - return body !== null && typeof body === 'object' && isObjectLessActionKey(standaloneActionOwnerKey(body)); - }); -} - export class SchemaRegistry { // ========================================== // Logging control @@ -4169,36 +1471,18 @@ export class SchemaRegistry { set logLevel(level: RegistryLogLevel) { this._logLevel = level; } private log(msg: string): void { - if (this._logLevel === 'silent' || this._logLevel === 'error' || this._logLevel === 'warn') return; - console.log(msg); + // [#33949 codemod] The body moved to `registry-storage.ts` (see `StorageHost`). + return StorageFacet.log.call(storageHost.of(this), msg); } - /** - * [#14553] A diagnostic an operator filters FOR — emitted at every level down - * to `warn`, silent only where the deployment has asked for silence - * (`error`, `silent`). - * - * The level ladder is `debug < info < warn < error < silent`, so this is the - * mirror of {@link log}: that one speaks only ABOVE `warn` and is therefore - * the wrong channel for anything a `warn`-level deployment must not miss. - * `OS_REGISTRY_LOG=warn` is what the objectql vitest config selects and what - * a quiet production boot selects, and a relocation nobody can see there was - * the defect this method exists to end. - */ private warn(msg: string): void { - if (this._logLevel === 'silent' || this._logLevel === 'error') return; - console.warn(msg); + // [#33949 codemod] The body moved to `registry-storage.ts`, its docblock with it (see `StorageHost`). + return StorageFacet.warn.call(storageHost.of(this), msg); } - /** - * Debug-only diagnostic: emitted solely when `logLevel === 'debug'`, so it - * stays out of the default (`'info'`) boot log. Use for expected-but-noisy - * housekeeping — e.g. re-registration on a metadata rebuild / HMR reload, - * which looks like an error (`console.warn`) but is a normal path (#3420). - */ private debug(msg: string): void { - if (this._logLevel !== 'debug') return; - console.debug(msg); + // [#33949 codemod] The body moved to `registry-storage.ts`, its docblock with it (see `StorageHost`). + return StorageFacet.debug.call(storageHost.of(this), msg); } // ========================================== @@ -4242,49 +1526,14 @@ export class SchemaRegistry { */ private objectKeysByShortName = new Map(); - /** - * [#10945] The ONLY writer of a new `objectContributors` key. Returns the - * contributor list for `fqn`, creating an empty one — and indexing it under - * its short name — on first use. - * - * A choke point rather than a discipline: the short-name index is maintained - * inside the same statement that adds the key, so no future caller can add a - * contributor list and forget the index half. - */ private openObjectEntry(fqn: string): ObjectContributor[] { - const existing = this.objectContributors.get(fqn); - if (existing) return existing; - - const contributors: ObjectContributor[] = []; - this.objectContributors.set(fqn, contributors); - const { shortName } = parseFQN(fqn); - const keys = this.objectKeysByShortName.get(shortName); - // Append, never replace: `matches[0]` is first-registered-wins, and both - // containers are appended to here in the same order, so the index array is - // `objectContributors`' key order narrowed to this short name. - if (keys) keys.push(fqn); - else this.objectKeysByShortName.set(shortName, [fqn]); - return contributors; + // [#33949 codemod] The body moved to `registry-storage.ts`, its docblock with it (see `StorageHost`). + return StorageFacet.openObjectEntry.call(storageHost.of(this), fqn); } - /** - * [#10945] The ONLY remover of an `objectContributors` key — the mirror of - * {@link openObjectEntry}. A no-op when the key is not registered, so removal - * stays idempotent. - * - * Empty short-name buckets are deleted rather than left behind: a retained - * empty array would make {@link resolveObjectKey}'s index hit and its FQN - * fallback disagree for a name that is both (a legacy `__` key - * removed while a plain key of the same short name is still registered). - */ private closeObjectEntry(fqn: string): void { - if (!this.objectContributors.delete(fqn)) return; - const { shortName } = parseFQN(fqn); - const keys = this.objectKeysByShortName.get(shortName); - if (!keys) return; - const idx = keys.indexOf(fqn); - if (idx !== -1) keys.splice(idx, 1); - if (keys.length === 0) this.objectKeysByShortName.delete(shortName); + // [#33949 codemod] The body moved to `registry-storage.ts`, its docblock with it (see `StorageHost`). + return StorageFacet.closeObjectEntry.call(storageHost.of(this), fqn); } /** FQN → Merged ServiceObject (cached, invalidated on changes) */ @@ -4365,7 +1614,8 @@ export class SchemaRegistry { * previously seeded set. */ setInitialDisabledPackageIds(ids: Iterable): void { - this.initialDisabledPackageIds = new Set(ids); + // [#33949 codemod] The body moved to `registry-storage.ts` (see `StorageHost`). + return StorageFacet.setInitialDisabledPackageIds.call(storageHost.of(this), ids); } /** See {@link onPackageUninstalled}. */ @@ -4388,22 +1638,13 @@ export class SchemaRegistry { * recompute nothing, and the engine recomputes it on enable. */ onPackageEnabled(listener: (packageId: string) => void): () => void { - this.packageEnabledListeners.add(listener); - return () => { this.packageEnabledListeners.delete(listener); }; + // [#33949 codemod] The body moved to `registry-storage.ts` (see `StorageHost`). + return StorageFacet.onPackageEnabled.call(storageHost.of(this), listener); } - /** Tell every {@link onPackageEnabled} listener. Never throws: the enable has happened, and a failing listener is logged at `error`. */ private announcePackageEnabled(id: string): void { - for (const listener of this.packageEnabledListeners) { - try { - listener(id); - } catch (err) { - console.error( - `[Registry] package "${id}" was enabled, but a listener bringing state derived from it current failed ` - + `(${(err as Error)?.message ?? err}) — that state may still read as it did while the package was disabled.`, - ); - } - } + // [#33949 codemod] The body moved to `registry-storage.ts`, its docblock with it (see `StorageHost`). + return StorageFacet.announcePackageEnabled.call(storageHost.of(this), id); } /** @@ -4419,29 +1660,13 @@ export class SchemaRegistry { * id left in it kept an uninstalled package read-only until a restart. */ onPackageUninstalled(listener: (packageId: string) => void): () => void { - this.packageUninstalledListeners.add(listener); - return () => { this.packageUninstalledListeners.delete(listener); }; + // [#33949 codemod] The body moved to `registry-storage.ts` (see `StorageHost`). + return StorageFacet.onPackageUninstalled.call(storageHost.of(this), listener); } - /** - * Tell every {@link onPackageUninstalled} listener. Never throws: the - * uninstall has already happened, so a throwing listener must not read as a - * refused one to the door that called {@link uninstallPackage}. It is logged - * at `error` instead, because that listener's state now disagrees with this - * registry while everything looks healthy. - */ private announcePackageUninstalled(id: string): void { - for (const listener of this.packageUninstalledListeners) { - try { - listener(id); - } catch (err) { - console.error( - `[Registry] package "${id}" was uninstalled, but a listener keeping state keyed by its id failed to drop it ` - + `(${(err as Error)?.message ?? err}) — that state still names the package until the next restart, ` - + 'while the registry no longer does. Restart the runtime to bring the two back in line.', - ); - } - } + // [#33949 codemod] The body moved to `registry-storage.ts`, its docblock with it (see `StorageHost`). + return StorageFacet.announcePackageUninstalled.call(storageHost.of(this), id); } // ========================================== @@ -4496,96 +1721,15 @@ export class SchemaRegistry { * registered, and a pair a later member completes would be refused. */ restoringOnRefusal(register: () => T): T { - const open = this.restorePoint; - const point = new RegistrationRestorePoint(); - this.restorePoint = point; - try { - const result = register(); - if (result instanceof Promise) { - throw new TypeError('[Registry] restoringOnRefusal takes a synchronous registration; it was handed one that returned a promise.'); - } - if (open) { - open.adopt(point); - } else { - const follow = this.recordCurrencyFollowRefusalOfRun(point); - if (follow) throw follow; - } - return result; - } catch (err) { - point.restore(); - this.invalidateAll(); - throw err; - } finally { - this.restorePoint = open; - } + // [#33949 codemod] The body moved to `registry-restore.ts` (see `RestoreHost`). + return (RestoreFacet.restoringOnRefusal).call(restoreHost.of(this), register); } - /** - * [ADR-0144 D2 and D6, amended] The record-currency follow door, asked of - * one whole registration run at its end: the refusal of the first object - * layer the run registered that the door refuses, or `undefined`. - * - * A single save asks the door before its one body registers - * ({@link objectRegistrationRefusal}'s `againstRegistry`). A run registers - * several, in an order a later one can still complete — a package's - * `objectExtensions` after its `objects`, a later package of the artifact, - * its residual, on a reload the new version of an object an earlier body - * follows — so no registration inside it can ask. Its end can: each layer is - * asked as a single save of it would be, with the registry holding the whole - * run, as its object is served ({@link servedFoldWithLayer}), and against - * the object as it was served with what the run replaced in the layer's slot: - * a pair the run leaves as it found it is not the run's to answer for - * (#33593 ruling R7). A pair with an end outside the run is judged against the - * object that is registered, as a single save judges it. Where that object - * is the environment's own, lying over a package's, and only it earns the - * refusal, the refusal says so ({@link followRefusalAsServed}). - * - * ⛔ Asked only of a layer the served object reads. An owner under a - * tenant's overlay is not one: the overlay's body is the base that is - * served ({@link resolveObject}), the run did not register it, and asking - * the owner beneath it would refuse a package over a code field the served - * object holds. Nor is a layer a later write of the run took - * out again (the same package registered twice). - * - * Asked only once the cold boot has sealed the registry, for the reason the - * single save's question is: before that an `extend` contribution or the - * other end can still register (AGENTS.md, startup registry reads). D6's - * roll-up half is not asked again: {@link registerObject} asked it of every - * layer as it registered. - */ private recordCurrencyFollowRefusalOfRun(point: RegistrationRestorePoint): Error | undefined { - if (!this.bareNameHoldersAreSealed) return undefined; - const layers = point.registeredObjects(); - if (layers.length === 0) return undefined; - // One read for the whole question: nothing registers while it is asked. - const all = this.getAllObjects(); - for (const { fqn, schema, contributor, replaced } of layers) { - const sitting = this.objectContributors.get(fqn); - if (!sitting?.includes(contributor)) continue; - if (contributor.ownership === 'own' && sitting.some((c) => c.ownership === 'overlay')) continue; - const layer: FollowDoorLayer = { packageId: contributor.packageId, priority: contributor.priority, before: replaced }; - const refusal = this.recordCurrencyFollowRefusalOf(schema, contributor.ownership, layer, () => all); - if (refusal) return this.followRefusalAsServed(refusal, schema, contributor.ownership, layer, all); - } - return undefined; + // [#33949 codemod] The body moved to `registry-restore.ts`, its docblock with it (see `RestoreHost`). + return RestoreFacet.recordCurrencyFollowRefusalOfRun.call(restoreHost.of(this), point); } - /** - * [ADR-0144 D2 and D6, amended] A run's follow refusal that the packages' - * own definitions would not earn, said as what it is. - * - * An object can be served from the environment's own object of the same - * name, lying over the definition a package ships ({@link resolveObject}'s - * base). The run's end judges a pair against what is served, so a package - * that is right in itself is refused when that served object lacks the other - * end, and the door's sentence then tells its author to correct a declaration - * that is not wrong. So the question is asked once more with each such - * object read through its package's definition ({@link resolveOwnerLayer}). - * Where that clears the refusal, the sentence gains the fact and the objects - * it is true of ({@link recordCurrencyFollowRefusalOverEnvironmentObjects}); - * the refusal itself, its code and its status stand. Any other refusal is - * returned as it is. Only a refused run pays for the second question. - */ private followRefusalAsServed( refusal: RecordCurrencyFollowRefusedError, schema: ServiceObject, @@ -4593,25 +1737,8 @@ export class SchemaRegistry { layer: FollowDoorLayer, all: readonly ServiceObject[], ): RecordCurrencyFollowRefusedError { - const overPackage: string[] = []; - for (const [fqn, sitting] of this.objectContributors) { - const overlay = sitting.find((c) => c.ownership === 'overlay'); - if (overlay && isTenantAuthored(overlay.definition) && sitting.some((c) => c.ownership === 'own')) overPackage.push(fqn); - } - if (overPackage.length === 0) return refusal; - /** Is the layer still refused with `throughPackage` read as their packages define them? */ - const refusedReading = (throughPackage: ReadonlySet): boolean => { - const read = (object: ServiceObject | undefined): ServiceObject | undefined => { - const fqn = object === undefined ? undefined : this.resolveObjectKey(object.name); - return fqn !== undefined && throughPackage.has(fqn) ? this.resolveOwnerLayer(fqn) ?? object : object; - }; - const objects = all.map((object) => read(object) ?? object); - return this.recordCurrencyFollowRefusalOf(schema, ownership, layer, () => objects, (name) => read(this.getObject(name))) !== undefined; - }; - if (refusedReading(new Set(overPackage))) return refusal; - // The ones that decide it, each on its own; all of them when only together they do. - const deciding = overPackage.filter((fqn) => !refusedReading(new Set([fqn]))); - return recordCurrencyFollowRefusalOverEnvironmentObjects(refusal, deciding.length > 0 ? deciding : overPackage); + // [#33949 codemod] The body moved to `registry-restore.ts`, its docblock with it (see `RestoreHost`). + return RestoreFacet.followRefusalAsServed.call(restoreHost.of(this), refusal, schema, ownership, layer, all); } /** @@ -4624,172 +1751,58 @@ export class SchemaRegistry { * nothing, as the registry's own entries are not recorded either. */ recordRestoreOnRefusal(key: string, capture: () => () => void): void { - this.restorePoint?.note(`outside\u0000${key}`, capture); + // [#33949 codemod] The body moved to `registry-restore.ts` (see `RestoreHost`). + return RestoreFacet.recordRestoreOnRefusal.call(restoreHost.of(this), key, capture); } - /** Record one app's list of navigation contributions before its first write. */ private noteAppNavContributions(app: string): void { - this.restorePoint?.note(`navContributions\u0000${app}`, () => { - const saved = this.appNavContributions.get(app); - const copy = saved ? [...saved] : undefined; - return () => { - if (copy) this.appNavContributions.set(app, copy); - else this.appNavContributions.delete(app); - }; - }); + // [#33949 codemod] The body moved to `registry-restore.ts`, its docblock with it (see `RestoreHost`). + return RestoreFacet.noteAppNavContributions.call(restoreHost.of(this), app); } - /** Record an object's contributor list (and each contributor's mutable slots) before its first write. */ private noteObjectEntry(fqn: string): void { - this.restorePoint?.note(`object\u0000${fqn}`, () => { - const list = this.objectContributors.get(fqn); - if (!list) return () => this.closeObjectEntry(fqn); - // The late-install branch re-classifies a sitting contributor in place, - // so its ownership and priority are kept beside the reference. - const saved = list.map((c) => ({ c, ownership: c.ownership, priority: c.priority })); - return () => { - for (const s of saved) { - s.c.ownership = s.ownership; - s.c.priority = s.priority; - } - list.splice(0, list.length, ...saved.map((s) => s.c)); - this.mergedObjectCache.delete(fqn); - }; - }); + // [#33949 codemod] The body moved to `registry-restore.ts`, its docblock with it (see `RestoreHost`). + return RestoreFacet.noteObjectEntry.call(restoreHost.of(this), fqn); } - /** Record a namespace's owner set before its first write. */ private noteNamespace(namespace: string): void { - this.restorePoint?.note(`namespace\u0000${namespace}`, () => { - const owners = this.namespaceRegistry.get(namespace); - const saved = owners ? new Set(owners) : undefined; - return () => { - if (saved) this.namespaceRegistry.set(namespace, saved); - else this.namespaceRegistry.delete(namespace); - }; - }); + // [#33949 codemod] The body moved to `registry-restore.ts`, its docblock with it (see `RestoreHost`). + return RestoreFacet.noteNamespace.call(restoreHost.of(this), namespace); } - /** Record whether an item type had a store at all, before the run can create one. */ private noteItemType(type: string): void { - this.restorePoint?.note(`type\u0000${type}`, () => { - const hadType = this.metadata.has(type); - return () => { - if (!hadType && this.metadata.get(type)?.size === 0) this.metadata.delete(type); - }; - }); + // [#33949 codemod] The body moved to `registry-restore.ts`, its docblock with it (see `RestoreHost`). + return RestoreFacet.noteItemType.call(restoreHost.of(this), type); } - /** Record one item slot before its first write. */ private noteItem(type: string, key: string): void { - this.restorePoint?.note(`item\u0000${type}\u0000${key}`, () => { - const collection = this.metadata.get(type); - const had = collection?.has(key) ?? false; - const value = collection?.get(key); - return () => { - const now = this.metadata.get(type); - if (had) now?.set(key, value); - else now?.delete(key); - }; - }); + // [#33949 codemod] The body moved to `registry-restore.ts`, its docblock with it (see `RestoreHost`). + return RestoreFacet.noteItem.call(restoreHost.of(this), type, key); } - /** - * [#33050] Record the ORDER of one item type's slots before the run's first - * removal from it ({@link replacingPackageBody}). A removed slot put back by - * {@link noteItem} alone would come back last, and the store's order is read: - * it is the order {@link listItems} answers and the first composite match a - * caller naming no package is served. Put back after every later write of - * the run is undone, so the slots come back where they stood. - */ private noteItemOrder(type: string): void { - this.restorePoint?.note(`itemOrder\u0000${type}`, () => { - const saved = [...(this.metadata.get(type) ?? [])]; - return () => { - const now = this.metadata.get(type); - if (!now) return; - now.clear(); - for (const [key, value] of saved) now.set(key, value); - }; - }); + // [#33949 codemod] The body moved to `registry-restore.ts`, its docblock with it (see `RestoreHost`). + return RestoreFacet.noteItemOrder.call(restoreHost.of(this), type); } - /** Record this package's extension of one picklist before its first write. */ private notePicklistExtension(target: string, packageKey: string): void { - this.restorePoint?.note(`picklistExtension\u0000${target}\u0000${packageKey}`, () => { - const byPackage = this.picklistExtensionContributions.get(target); - const saved = byPackage?.get(packageKey); - return () => { - let now = this.picklistExtensionContributions.get(target); - if (saved) { - if (!now) this.picklistExtensionContributions.set(target, now = new Map()); - now.set(packageKey, saved); - } else if (now) { - now.delete(packageKey); - if (now.size === 0) this.picklistExtensionContributions.delete(target); - } - }; - }); + // [#33949 codemod] The body moved to `registry-restore.ts`, its docblock with it (see `RestoreHost`). + return RestoreFacet.notePicklistExtension.call(restoreHost.of(this), target, packageKey); } - /** Record who holds one security catalog name before its first write. */ private noteSecurityCatalogClaim(type: SecurityCatalogType, name: string): void { - this.restorePoint?.note(`catalogClaim\u0000${type}\u0000${name}`, () => { - const saved = this.securityCatalogClaims.get(type)?.get(name); - const copy = saved ? new Set(saved) : undefined; - return () => { - let byName = this.securityCatalogClaims.get(type); - if (copy !== undefined) { - if (!byName) this.securityCatalogClaims.set(type, byName = new Map()); - byName.set(name, copy); - } else { - byName?.delete(name); - } - }; - }); + // [#33949 codemod] The body moved to `registry-restore.ts`, its docblock with it (see `RestoreHost`). + return RestoreFacet.noteSecurityCatalogClaim.call(restoreHost.of(this), type, name); } - /** Record who holds one bare name (`bare-name-one-holder.ts`) before its first write. */ private noteBareNameClaim(type: BareNameOneHolderType, name: string): void { - this.restorePoint?.note(`bareNameClaim\u0000${type}\u0000${name}`, () => { - const saved = this.bareNameClaims.get(type)?.get(name); - const copy = saved ? new Set(saved) : undefined; - return () => { - let byName = this.bareNameClaims.get(type); - if (copy !== undefined) { - if (!byName) this.bareNameClaims.set(type, byName = new Map()); - byName.set(name, copy); - } else { - byName?.delete(name); - } - }; - }); + // [#33949 codemod] The body moved to `registry-restore.ts`, its docblock with it (see `RestoreHost`). + return RestoreFacet.noteBareNameClaim.call(restoreHost.of(this), type, name); } - /** - * Record the default record page claims before `packageId`'s first rewrite - * of them ({@link recordDefaultRecordPageClaims} drops all of the package's - * claims, then adds what the manifest declares). Put back per object, and - * only for the objects `packageId` held a claim on before or after, each in - * its holders' original order. - */ private noteDefaultRecordPageClaims(packageId: string): void { - // Keyed by slot (`recordPageSlotKey`): an object's default, or one app's - // page of it. Every slot is put back the same way. - this.restorePoint?.note(`defaultPageClaims\u0000${packageId}`, () => { - const saved = new Map>(); - for (const [object, byPackage] of this.defaultRecordPageClaims) saved.set(object, new Map(byPackage)); - return () => { - const objects = new Set(); - for (const [object, byPackage] of saved) if (byPackage.has(packageId)) objects.add(object); - for (const [object, byPackage] of this.defaultRecordPageClaims) if (byPackage.has(packageId)) objects.add(object); - for (const object of objects) { - const before = saved.get(object); - if (before) this.defaultRecordPageClaims.set(object, new Map(before)); - else this.defaultRecordPageClaims.delete(object); - } - }; - }); + // [#33949 codemod] The body moved to `registry-restore.ts`, its docblock with it (see `RestoreHost`). + return RestoreFacet.noteDefaultRecordPageClaims.call(restoreHost.of(this), packageId); } // ========================================== @@ -4801,49 +1814,32 @@ export class SchemaRegistry { * Multiple packages can share the same namespace (e.g. 'sys'). */ registerNamespace(namespace: string, packageId: string): void { - if (!namespace) return; - this.noteNamespace(namespace); - - let owners = this.namespaceRegistry.get(namespace); - if (!owners) { - owners = new Set(); - this.namespaceRegistry.set(namespace, owners); - } - owners.add(packageId); - this.log(`[Registry] Registered namespace: ${namespace} → ${packageId}`); + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts` (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.registerNamespace.call(namespacesAndClaimsHost.of(this), namespace, packageId); } /** * Unregister a namespace when a package is uninstalled. */ unregisterNamespace(namespace: string, packageId: string): void { - const owners = this.namespaceRegistry.get(namespace); - if (owners) { - this.noteNamespace(namespace); - owners.delete(packageId); - if (owners.size === 0) { - this.namespaceRegistry.delete(namespace); - } - this.log(`[Registry] Unregistered namespace: ${namespace} ← ${packageId}`); - } + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts` (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.unregisterNamespace.call(namespacesAndClaimsHost.of(this), namespace, packageId); } /** * Get the packages that use a namespace. */ getNamespaceOwner(namespace: string): string | undefined { - const owners = this.namespaceRegistry.get(namespace); - if (!owners || owners.size === 0) return undefined; - // Return the first registered package for backwards compatibility - return owners.values().next().value; + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts` (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.getNamespaceOwner.call(namespacesAndClaimsHost.of(this), namespace); } /** * Get all packages that share a namespace. */ getNamespaceOwners(namespace: string): string[] { - const owners = this.namespaceRegistry.get(namespace); - return owners ? Array.from(owners) : []; + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts` (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.getNamespaceOwners.call(namespacesAndClaimsHost.of(this), namespace); } // ========================================== @@ -4870,53 +1866,9 @@ export class SchemaRegistry { */ private defaultRecordPageClaims = new Map>(); - /** - * Every page holding the default record page of `object`, each tagged with - * whether it holds through the registry's bare slot (an environment item, - * which stands in for every slot of its name — module doc, "What is not a - * second default"). - * - * Read, per page name: the bare slot when there is one — its own body - * decides, and the composite slots and claims of that name are shadowed by - * it; otherwise every composite `PACKAGE:NAME` slot, and every install claim. - * A bare-slot item carrying a real package stamp is that package's page as - * overlaid in this environment (the protocol grafts the package's envelope - * onto an overlay of a packaged page), and is named as that package's page. - * A disabled package is still installed, and its pages still hold. - */ private defaultRecordPageSlots(slot: RecordPageSlot): Array<{ holder: DefaultRecordPageHolder; bare: boolean }> { - const found = new Map(); - const add = (holder: DefaultRecordPageHolder, bare: boolean) => { - found.set(defaultRecordPageHolderKey(holder), { holder, bare }); - }; - const bareNames = new Set(); - const composites: Array<{ packageId: string; page: string; item: unknown }> = []; - for (const [key, item] of this.metadata.get('page') ?? []) { - const sep = key.lastIndexOf(':'); - const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; - if (sep < 0) { - bareNames.add(key); - if (!holdsRecordPageSlot(item, slot)) continue; - const packaged = - typeof stamped === 'string' && stamped !== '' && stamped !== 'sys_metadata' && !isTenantAuthored(item); - add(packaged ? { kind: 'package', packageId: stamped, page: key } : { kind: 'environment', page: key }, true); - } else { - composites.push({ - packageId: typeof stamped === 'string' && stamped !== '' ? stamped : key.slice(0, sep), - page: key.slice(sep + 1), - item, - }); - } - } - for (const { packageId, page, item } of composites) { - if (bareNames.has(page) || !holdsRecordPageSlot(item, slot)) continue; - add({ kind: 'package', packageId, page }, false); - } - for (const [packageId, page] of this.defaultRecordPageClaims.get(recordPageSlotKey(slot)) ?? []) { - if (bareNames.has(page)) continue; - add({ kind: 'package', packageId, page }, false); - } - return [...found.values()]; + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts`, its docblock with it (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.defaultRecordPageSlots.call(namespacesAndClaimsHost.of(this), slot); } /** @@ -4935,91 +1887,28 @@ export class SchemaRegistry { * the save door meets an app-scoped page with. */ defaultRecordPageHoldersOf(object: string, app?: string): DefaultRecordPageHolder[] { - return this.defaultRecordPageSlots(app === undefined ? { object } : { object, app }).map(({ holder }) => holder); + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts` (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.defaultRecordPageHoldersOf.call(namespacesAndClaimsHost.of(this), object, app); } - /** - * The holders of `object`'s default record page that a package registering - * `page` as that default would meet as a SECOND default — every holder but - * the package's own pages and the bare-slot item of the same name (the - * environment's overlay of the very page being registered). - */ private otherDefaultRecordPageHolders(slot: RecordPageSlot, page: string, packageId: string): DefaultRecordPageHolder[] { - return this.defaultRecordPageSlots(slot) - .filter(({ holder, bare }) => { - if (holder.kind === 'package' && holder.packageId === packageId) return false; - return !(bare && holder.page === page); - }) - .map(({ holder }) => holder); + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts`, its docblock with it (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.otherDefaultRecordPageHolders.call(namespacesAndClaimsHost.of(this), slot, page, packageId); } - /** - * The package door's half of the rule: refuse a package whose declared - * default record pages meet another holder — a second page of the same - * manifest for the same object, or a page another package or the environment - * holds — ahead of every mutation {@link installPackage} makes, so a refused - * package leaves no record, no namespace ownership and no claim behind. - * - * Every conflict is collected, so one refusal reports all of them. A package - * with no identity (`artifactPackageId` answers nothing) registers under no - * package at all and has nothing to hold; it is not judged here. - * - * @throws {DefaultRecordPageConflictError} ADR-0112 envelope, naming both - * pages of each conflict. - */ private refuseDefaultRecordPageConflicts(manifest: ObjectStackManifest, selfId: string | undefined): void { - // `isDefault: true` on a page that is not a record page bound to an object - // is refused on its own account first (maintainer ruling A on - // objectstack#23610) — whatever the package's identity, since the page - // itself, not a holder, is what is wrong. - const misplaced = misplacedDefaultPages(manifest); - if (misplaced.length > 0) throw new MisplacedDefaultPageError(selfId, misplaced); - if (selfId === undefined) return; - const conflicts: DefaultRecordPageConflict[] = []; - // slot → the first page this manifest declares for it. - const firstDeclared = new Map(); - for (const { page, ...slot } of declaredDefaultRecordPages(manifest)) { - const incoming: DefaultRecordPageHolder = { kind: 'package', packageId: selfId, page }; - const key = recordPageSlotKey(slot); - const first = firstDeclared.get(key); - if (first === undefined) { - firstDeclared.set(key, page); - for (const existing of this.otherDefaultRecordPageHolders(slot, page, selfId)) { - conflicts.push({ ...slot, incoming, existing }); - } - } else if (first !== page) { - // Two pages of one manifest for one slot. One page declared twice - // under one name is one page (the stack contract refuses the repeat). - conflicts.push({ ...slot, incoming, existing: { kind: 'package', packageId: selfId, page: first } }); - } - } - if (conflicts.length > 0) throw new DefaultRecordPageConflictError(conflicts); + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts`, its docblock with it (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.refuseDefaultRecordPageConflicts.call(namespacesAndClaimsHost.of(this), manifest, selfId); } - /** - * Replace `packageId`'s claims with what `manifest` declares (see - * {@link defaultRecordPageClaims} for why a re-install replaces). - */ private recordDefaultRecordPageClaims(packageId: string, manifest: ObjectStackManifest): void { - this.noteDefaultRecordPageClaims(packageId); - this.forgetDefaultRecordPageClaims(packageId); - for (const { page, ...slot } of declaredDefaultRecordPages(manifest)) { - const key = recordPageSlotKey(slot); - let byPackage = this.defaultRecordPageClaims.get(key); - if (!byPackage) { - byPackage = new Map(); - this.defaultRecordPageClaims.set(key, byPackage); - } - if (!byPackage.has(packageId)) byPackage.set(packageId, page); - } + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts`, its docblock with it (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.recordDefaultRecordPageClaims.call(namespacesAndClaimsHost.of(this), packageId, manifest); } - /** Drop every default record page claim `packageId` holds. */ private forgetDefaultRecordPageClaims(packageId: string): void { - for (const [object, byPackage] of this.defaultRecordPageClaims) { - byPackage.delete(packageId); - if (byPackage.size === 0) this.defaultRecordPageClaims.delete(object); - } + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts`, its docblock with it (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.forgetDefaultRecordPageClaims.call(namespacesAndClaimsHost.of(this), packageId); } // ========================================== @@ -5046,119 +1935,29 @@ export class SchemaRegistry { */ private securityCatalogClaims = new Map>>(); - /** - * Every holder of `(type, name)` other than `exceptPackageId` — the ONE - * predicate both refusals ({@link refuseSecurityCatalogNameConflicts} at the - * package door, the item seam in {@link registerItem}) ask. - * - * Read, in this order: a built-in (only when `builtIns` — see - * {@link BUILT_IN_SECURITY_CATALOG_NAMES} for why the item seam does not ask); - * the bare slot (an override a package bound to itself, else an - * environment-authored item — the latter only when `environment`); every - * composite `:` slot; the package claims. A package that - * holds the name more than one way is one holder. A disabled package is still - * installed, and still holds its names. - * - * [ADR-0150 D2/D9, #31698] For a permission set, a package holder counts only - * when it meets the incoming package under {@link permissionSetHoldersMeet} - * (`incomingNamespace`, the namespace the incoming package declares): two - * packages of two namespaces each hold their own set of the name. Positions - * and capabilities keep one holder per name. - */ private securityCatalogHoldersOtherThan( type: SecurityCatalogType, name: string, exceptPackageId: string | undefined, opts: { builtIns: boolean; environment: boolean; incomingNamespace: string | undefined }, ): SecurityCatalogHolder[] { - const found = new Map(); - const add = (holder: SecurityCatalogHolder) => { - if (holder.kind === 'package' && holder.packageId === exceptPackageId) return; - if (holder.kind === 'package' && type === 'permission' - && !permissionSetHoldersMeet(opts.incomingNamespace, this.bareNameHolderNamespace(holder.packageId), name)) return; - found.set(securityCatalogHolderKey(holder), holder); - }; - if (opts.builtIns && BUILT_IN_SECURITY_CATALOG_NAMES[type].has(name)) add({ kind: 'built-in' }); - const suffix = `:${name}`; - for (const [key, item] of this.metadata.get(type) ?? []) { - const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; - if (key === name) { - if (typeof stamped === 'string' && stamped !== '') add({ kind: 'package', packageId: stamped }); - else if (opts.environment) add({ kind: 'environment' }); - } else if (key.endsWith(suffix)) { - add({ - kind: 'package', - packageId: typeof stamped === 'string' && stamped !== '' ? stamped : key.slice(0, -suffix.length), - }); - } - } - for (const claimant of this.securityCatalogClaims.get(type)?.get(name) ?? []) add({ kind: 'package', packageId: claimant }); - return [...found.values()]; + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts`, its docblock with it (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.securityCatalogHoldersOtherThan.call(namespacesAndClaimsHost.of(this), type, name, exceptPackageId, opts); } - /** - * The package door's half of the rule: refuse a package whose declared - * positions, permission sets or capabilities name something another holder - * already holds — built-ins included — ahead of every mutation - * {@link installPackage} makes, so a refused package leaves no record, no - * namespace ownership and no claim behind (the namespace gate's disposition). - * - * Every conflict is collected, so one refusal reports all of them. A package - * with no identity (`artifactPackageId` answers nothing) registers under no - * package at all and has nothing to hold; it is not judged here. - * - * @throws {SecurityCatalogNameConflictError} ADR-0112 envelope, naming both - * holders of each conflicting name. - */ private refuseSecurityCatalogNameConflicts(manifest: ObjectStackManifest, selfId: string | undefined): void { - if (selfId === undefined) return; - const conflicts: SecurityCatalogNameConflict[] = []; - const seen = new Set(); - const incomingNamespace = typeof manifest.namespace === 'string' && manifest.namespace !== '' ? manifest.namespace : undefined; - for (const { type, name } of declaredSecurityCatalogNames(manifest)) { - // One package declaring one name twice is one holder (an in-package - // repeat is the package's own business, and not this rule's). - const key = `${type}|${name}`; - if (seen.has(key)) continue; - seen.add(key); - for (const existingHolder of this.securityCatalogHoldersOtherThan(type, name, selfId, { builtIns: true, environment: true, incomingNamespace })) { - conflicts.push({ catalogType: type, name, incomingPackageId: selfId, existingHolder }); - } - } - if (conflicts.length > 0) throw new SecurityCatalogNameConflictError(conflicts); + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts`, its docblock with it (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.refuseSecurityCatalogNameConflicts.call(namespacesAndClaimsHost.of(this), manifest, selfId); } - /** - * Record `packageId`'s claims. Additive, like the item store beside it: a - * re-install does not unregister the items an earlier install of the same - * package registered, so it does not release their names either — and a - * re-install whose manifest carries no collections at all (`POST - * /api/v1/packages` stores the manifest alone) must not strip the names the - * package's code still declares. {@link uninstallPackage} is what releases - * them. - */ private recordSecurityCatalogClaims(packageId: string, manifest: ObjectStackManifest): void { - for (const { type, name } of declaredSecurityCatalogNames(manifest)) { - let byName = this.securityCatalogClaims.get(type); - if (!byName) { - byName = new Map(); - this.securityCatalogClaims.set(type, byName); - } - this.noteSecurityCatalogClaim(type, name); - let claimants = byName.get(name); - if (!claimants) byName.set(name, claimants = new Set()); - claimants.add(packageId); - } + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts`, its docblock with it (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.recordSecurityCatalogClaims.call(namespacesAndClaimsHost.of(this), packageId, manifest); } - /** Drop every claim `packageId` holds. */ private forgetSecurityCatalogClaims(packageId: string): void { - for (const byName of this.securityCatalogClaims.values()) { - for (const [name, claimants] of byName) { - claimants.delete(packageId); - if (claimants.size === 0) byName.delete(name); - } - } + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts`, its docblock with it (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.forgetSecurityCatalogClaims.call(namespacesAndClaimsHost.of(this), packageId); } /** @@ -5172,121 +1971,23 @@ export class SchemaRegistry { * last moment the names are known (ADR-0090 D5, ADR-0131 D4). */ securityCatalogNamesHeldBy(packageId: string, type: SecurityCatalogType): string[] { - const names = new Set(); - for (const [name, claimants] of this.securityCatalogClaims.get(type) ?? []) { - if (claimants.has(packageId)) names.add(name); - } - const prefix = `${packageId}:`; - for (const [key, item] of this.metadata.get(type) ?? []) { - if (key.startsWith(prefix)) names.add(key.slice(prefix.length)); - else if ((item as { _packageId?: unknown } | null | undefined)?._packageId === packageId) names.add(key); - } - return [...names].sort(); + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts` (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.securityCatalogNamesHeldBy.call(namespacesAndClaimsHost.of(this), packageId, type); } - /** - * The cold-boot half of the rule (`security-catalog-namespace.ts`, "The cold - * boot"; maintainer ruling letter A on #22307, record 6063176077): every - * package-held position and permission-set name the environment catalog also - * holds, as the conflicts {@link SecurityCatalogNameConflictError} reports — - * the package that declares the name as the incoming side, the environment - * catalog as the holder. Read-only: it records nothing and refuses nothing; - * the engine plugin asks it once `sys_metadata` has hydrated, and refuses the - * boot on any answer. - * - * - **The environment's names** are the bare-slot items of the two types the - * environment can author ({@link ENVIRONMENT_HELD_SECURITY_CATALOG_TYPES}). - * Every bare-slot item is the environment's whatever `_packageId` it wears: - * only a registration with no package writes the bare slot, and at a cold - * boot hydration grafts the package's envelope onto the stored row (the - * protocol's artifact-protection merge), so the stamp names the very - * package the row collides with. - * - **A package holds a name** through an item registered under it (its - * composite slot) or its install claim — never through the bare slot. - * - **Built-in names are skipped**, as the item seam skips the environment - * holder for them: the platform declares its built-in positions itself - * (after this check, in its own `start()`), and an environment item under a - * built-in name is a stored definition that shadows the declaration at - * read (ADR-0005) — a legitimate shadow, not a second holder. - * - * Sorted by type, then name, so two boots of one database report alike. - * - * Computed as {@link packageHeldSecurityCatalogNames} met with the bare slot, - * so the package half is ONE reading: the cold boot asks it here, and - * `os migrate security-catalog-overlays` asks it through the exported - * {@link findPackageHeldSecurityCatalogNames} and meets it with the stored rows - * (maintainer ruling letter B on #22371, record 6074838935). - * - * Private, like the rest of this rule's registry half; the engine plugin, - * which owns the boot sequence it is asked in, reaches it through the - * module-level {@link findEnvironmentHeldSecurityCatalogNames}, which the - * package entries do not re-export. - */ private environmentHeldSecurityCatalogConflicts(): SecurityCatalogNameConflict[] { - const conflicts: SecurityCatalogNameConflict[] = []; - for (const { catalogType, name, packageIds } of this.packageHeldSecurityCatalogNames()) { - // The environment's item: the bare slot, whatever `_packageId` it wears. - // A held name never contains `:` and is never a built-in, so this is the - // bare-slot reading the method header states. - if (!this.metadata.get(catalogType)?.has(name)) continue; - for (const packageId of packageIds) { - conflicts.push({ catalogType, name, incomingPackageId: packageId, existingHolder: { kind: 'environment' } }); - } - } - return conflicts; + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts`, its docblock with it (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.environmentHeldSecurityCatalogConflicts.call(namespacesAndClaimsHost.of(this)); } - /** - * Every position and permission-set name a package holds, with its holders — - * the package half of {@link environmentHeldSecurityCatalogConflicts}, and the - * reading {@link findPackageHeldSecurityCatalogNames} exports. - * - * The candidate names are every composite slot's name and every install - * claim of the two types the environment catalog can hold - * ({@link ENVIRONMENT_HELD_SECURITY_CATALOG_TYPES}); each one's holders are - * {@link securityCatalogPackageHolders}'s answer, the one predicate. Built-in - * names are skipped, as the cold boot skips them (see the method above). - * Sorted by type in that list's order, then name. - * - * Also read BY NAME from `@objectstack/metadata-protocol` - * (`protocol-save-door-refusals.ts`, `registryPackageHoldersOf`): renaming - * this member means updating that caller and - * `protocol-permission-set-name-registry-reading.test.ts`. - */ private packageHeldSecurityCatalogNames(): Array<{ catalogType: SecurityCatalogType; name: string; packageIds: string[] }> { - const held: Array<{ catalogType: SecurityCatalogType; name: string; packageIds: string[] }> = []; - for (const type of ENVIRONMENT_HELD_SECURITY_CATALOG_TYPES) { - const candidates = new Set(); - for (const key of this.metadata.get(type)?.keys() ?? []) { - const sep = key.lastIndexOf(':'); - if (sep > 0) candidates.add(key.slice(sep + 1)); - } - for (const name of this.securityCatalogClaims.get(type)?.keys() ?? []) candidates.add(name); - for (const name of [...candidates].sort()) { - if (name === '' || BUILT_IN_SECURITY_CATALOG_NAMES[type].has(name)) continue; - const packageIds = this.securityCatalogPackageHolders(type, name); - if (packageIds.length > 0) held.push({ catalogType: type, name, packageIds }); - } - } - return held; + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts`, its docblock with it (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.packageHeldSecurityCatalogNames.call(namespacesAndClaimsHost.of(this)); } - /** - * The packages holding `(type, name)` through an item registered under them - * (a composite `:` slot) or an install claim — the package - * half of {@link securityCatalogHoldersOtherThan}'s reading, without the bare - * slot, whose stamp is not a claim (see {@link environmentHeldSecurityCatalogConflicts}). - */ private securityCatalogPackageHolders(type: SecurityCatalogType, name: string): string[] { - const holders = new Set(); - const suffix = `:${name}`; - for (const [key, item] of this.metadata.get(type) ?? []) { - if (key === name || !key.endsWith(suffix)) continue; - const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; - holders.add(typeof stamped === 'string' && stamped !== '' ? stamped : key.slice(0, -suffix.length)); - } - for (const claimant of this.securityCatalogClaims.get(type)?.get(name) ?? []) holders.add(claimant); - return [...holders]; + // [#33949 codemod] The body moved to `registry-namespaces-and-claims.ts`, its docblock with it (see `NamespacesAndClaimsHost`). + return NamespacesAndClaimsFacet.securityCatalogPackageHolders.call(namespacesAndClaimsHost.of(this), type, name); } // ========================================== @@ -5315,19 +2016,9 @@ export class SchemaRegistry { /** The packages already warned that their namespace spells an address literal. */ private readonly addressLiteralNamespaceWarned = new Set(); - /** - * [ADR-0150 D5, child G1] Warn, once per package, that an installed - * package's namespace spells an address-grammar literal: it keeps booting, - * its items are read with `?package=`, and the remedy is a rename. - */ private warnAddressLiteralNamespaceOnce(packageId: string, refusal: string): void { - if (this.addressLiteralNamespaceWarned.has(packageId)) return; - this.addressLiteralNamespaceWarned.add(packageId); - console.warn( - `[Registry] Package '${packageId}' is installed under a namespace the address grammar reads as a route. ` - + `${refusal} It keeps booting: its items are read with \`?package=${packageId}\`, and a new install ` - + `of this namespace is refused. Renaming the namespace gives the package its qualified address.`, - ); + // [#33949 codemod] The body moved to `registry-bare-names.ts`, its docblock with it (see `BareNamesHost`). + return BareNamesFacet.warnAddressLiteralNamespaceOnce.call(bareNamesHost.of(this), packageId, refusal); } /** Has the cold boot sealed the bare-name holders (the doors now refuse)? */ @@ -5335,78 +2026,21 @@ export class SchemaRegistry { return this.bareNameHoldersSealed; } - /** - * The packages holding `(type, name)`, in registration order — the item - * store's order (whose first is the holder a caller with no package is - * served by {@link getItem}), then any holder by claim alone. A package holds - * a name through an item registered under it (its composite - * `:` slot, when the item is one the rule covers — - * `holdsBareName`: an action bound to an object holds nothing) or its claim: - * recorded at install, or, for a function, at the hook binder's bind - * ({@link claimBareNameFunctions}). ⛔ Never the bare slot: that is the - * environment catalog (the ADR-0005 overlay), whatever `_packageId` its - * hydrated row wears. ⛔ Never a type's overridable baseline - * ({@link isOverridableBaseline}): the platform's copies, which the - * consumer serves another package's item over by contract. A bundled type's - * key carries its member (`pkg:name@locale`, `bundleBaseKey`): the NAME is - * what is held, so every member of one package's bundle is that one holder. - */ private bareNamePackageHolders(type: BareNameOneHolderType, name: string): string[] { - const holders = new Set(); - if (isBareNameRegistryType(type)) { - const suffix = `:${name}`; - for (const [key, item] of this.metadata.get(type) ?? []) { - const base = bundleBaseKey(key); - if (base === name || !base.endsWith(suffix) || !holdsBareName(type, item)) continue; - const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; - holders.add(typeof stamped === 'string' && stamped !== '' ? stamped : base.slice(0, -suffix.length)); - } - } - for (const claimant of this.bareNameClaims.get(type)?.get(name) ?? []) holders.add(claimant); - return [...holders].filter((packageId) => !isOverridableBaseline(type, packageId)); + // [#33949 codemod] The body moved to `registry-bare-names.ts`, its docblock with it (see `BareNamesHost`). + return BareNamesFacet.bareNamePackageHolders.call(bareNamesHost.of(this), type, name); } - /** - * The rename a refusal carries for `name` in `packageId` — its namespace - * prefixed ({@link suggestedBareNameRename}) — read from the manifest the - * door holds, else from the installed record. None for a shareable platform - * namespace (`base` / `system` / `sys`): it names no one package. - */ private bareNameRenameFor(packageId: string, name: string, namespace?: string): string | undefined { - const ns = namespace ?? this.getPackage(packageId)?.manifest?.namespace; - if (!ns || isShareableNamespace(ns)) return undefined; - return suggestedBareNameRename(ns, name); + // [#33949 codemod] The body moved to `registry-bare-names.ts`, its docblock with it (see `BareNamesHost`). + return BareNamesFacet.bareNameRenameFor.call(bareNamesHost.of(this), packageId, name, namespace); } - /** - * [ADR-0150 D2/D9, child B] The namespace a package holder declares — its - * installed record's `manifest.namespace` — or `undefined`: no record, or - * none declared. What {@link bareNameHoldersMeet} compares for a type whose - * rule has narrowed to the namespace. - */ private bareNameHolderNamespace(packageId: string): string | undefined { - const namespace = this.getPackage(packageId)?.manifest?.namespace; - return typeof namespace === 'string' && namespace !== '' ? namespace : undefined; + // [#33949 codemod] The body moved to `registry-bare-names.ts`, its docblock with it (see `BareNamesHost`). + return BareNamesFacet.bareNameHolderNamespace.call(bareNamesHost.of(this), packageId); } - /** - * Every holder `incomingPackageId` would join by holding `(type, name)` — - * the ONE predicate both doors ask ({@link refuseBareNameConflicts} at the - * package door, the item seam in {@link registerItem}). Empty before the - * seal, and empty when `incomingPackageId` already holds the name: a reload, - * a re-install or an upgrade of a package that came through the boot beside - * another holder is not a new holder (module doc, "The cold boot"). A - * built-in name is read only when `builtIns` (the package door), as the - * security catalog reads its own. A package holder counts only when it - * meets the incoming package under {@link bareNameHoldersMeet}: for a type - * narrowed to the namespace, a holder of another namespace does not - * (`incomingNamespace`, the namespace the incoming package declares) — - * unless one holding is an addition to the other's bundle - * ({@link bareNameBundleAdditionMeets}; `incomingHolding`, read only then). - * `incomingArrival` is what the registration brings (the members it adds): - * a holder's members are read as if it were registered, so the door judges - * the state the cold boot's seal would judge (#32408, delta review N1). - */ private bareNameHoldersOtherThan( type: BareNameOneHolderType, name: string, @@ -5418,21 +2052,8 @@ export class SchemaRegistry { incomingArrival?: { readonly members: ReadonlySet }; }, ): BareNameHolder[] { - if (!this.bareNameHoldersSealed) return []; - const packages = this.bareNamePackageHolders(type, name); - if (packages.includes(incomingPackageId)) return []; - const found = new Map(); - if (opts.builtIns && BUILT_IN_BARE_NAMES[type].has(name)) found.set(bareNameHolderKey({ kind: 'built-in' }), { kind: 'built-in' }); - for (const packageId of packages) { - if ( - !bareNameHoldersMeet(type, opts.incomingNamespace, this.bareNameHolderNamespace(packageId), name) - && !this.bareNameBundleAdditionMeets(type, name, opts.incomingHolding, packageId, - opts.incomingArrival && { packageId: incomingPackageId, ...opts.incomingArrival }) - ) continue; - const holder: BareNameHolder = { kind: 'package', packageId }; - found.set(bareNameHolderKey(holder), holder); - } - return [...found.values()]; + // [#33949 codemod] The body moved to `registry-bare-names.ts`, its docblock with it (see `BareNamesHost`). + return BareNamesFacet.bareNameHoldersOtherThan.call(bareNamesHost.of(this), type, name, incomingPackageId, opts); } /** @@ -5461,14 +2082,6 @@ export class SchemaRegistry { return { packageId, members, reach: new Set(packageDependencyClosure(packageId, this)?.dependencies ?? []) }; } - /** - * [ADR-0150 D9, #31699] Do two package holders of a bundled name meet - * because one holding is an ADDITION to the other's bundle - * ({@link isBundleAdditionPair}) — whatever their namespaces, so a type - * narrowed to the namespace keeps refusing a dependent's added member as - * it did? `false` for a type that is no bundle, or when the incoming - * holding cannot be read. - */ private bareNameBundleAdditionMeets( type: BareNameOneHolderType, name: string, @@ -5476,10 +2089,8 @@ export class SchemaRegistry { holderPackageId: string, arrival?: BundleArrival, ): boolean { - if (!ITEM_KEY_DISCRIMINATORS[type] || !incomingHolding) return false; - const incoming = incomingHolding(); - const holder = this.registeredBundleHolding(type, name, holderPackageId, arrival); - return incoming !== undefined && holder !== undefined && isBundleAdditionPair(incoming, holder); + // [#33949 codemod] The body moved to `registry-bare-names.ts`, its docblock with it (see `BareNamesHost`). + return BareNamesFacet.bareNameBundleAdditionMeets.call(bareNamesHost.of(this), type, name, incomingHolding, holderPackageId, arrival); } /** @@ -5500,81 +2111,19 @@ export class SchemaRegistry { return { packageId, members: declaredBundleMembers(manifest, type, name), reach }; } - /** - * The package door's half of the rule: once sealed, refuse a package whose - * declared items of a covered registry type bear a name another holder holds, - * ahead of every mutation {@link installPackage} makes — the security - * catalog's disposition, for its reasons: every conflict collected into one - * refusal, no co-owner exemption, no `collisionPolicy` downgrade, and a - * package with no identity not judged. - * - * @throws {BareNameConflictError} ADR-0112 envelope, naming both holders of - * each conflicting name and the rename. - */ private refuseBareNameConflicts(manifest: ObjectStackManifest, selfId: string | undefined): void { - if (selfId === undefined || !this.bareNameHoldersSealed) return; - const conflicts: BareNameConflict[] = []; - const seen = new Set(); - const incomingNamespace = typeof manifest.namespace === 'string' && manifest.namespace !== '' ? manifest.namespace : undefined; - for (const { type, name } of declaredBareNames(manifest)) { - // One package declaring one name twice is one holder. - const key = `${type}|${name}`; - if (seen.has(key)) continue; - seen.add(key); - for (const existingHolder of this.bareNameHoldersOtherThan(type, name, selfId, { - builtIns: true, - incomingNamespace, - incomingHolding: () => this.declaredBundleHolding(manifest, selfId, type, name), - incomingArrival: { members: declaredBundleMembers(manifest, type, name) }, - })) { - conflicts.push({ - type, - name, - incomingPackageId: selfId, - existingHolder, - rename: this.bareNameRenameFor(selfId, name, manifest.namespace), - }); - } - } - // [#31697, contract review G6] An action bound to an object under a key the - // platform holds (`BUILT_IN_BOUND_ACTION_KEYS`): the platform's own UI posts - // to that route by its fixed name. - for (const { name, objectName } of declaredBuiltInBoundActions(manifest)) { - if (this.getPackage(selfId) !== undefined && declaredBuiltInBoundActions(this.getPackage(selfId)?.manifest) - .some((held) => held.name === name && held.objectName === objectName)) continue; - conflicts.push({ - type: 'action', name, objectName, incomingPackageId: selfId, existingHolder: { kind: 'built-in' }, - rename: this.bareNameRenameFor(selfId, name, manifest.namespace), - }); - } - if (conflicts.length > 0) throw new BareNameConflictError(conflicts); + // [#33949 codemod] The body moved to `registry-bare-names.ts`, its docblock with it (see `BareNamesHost`). + return BareNamesFacet.refuseBareNameConflicts.call(bareNamesHost.of(this), manifest, selfId); } - /** - * Record `packageId`'s bare-name claims — additive, like - * {@link recordSecurityCatalogClaims} and for its reasons; released by - * {@link uninstallPackage}. Recorded before the seal too: the cold boot reads - * them. - */ private recordBareNameClaims(packageId: string, manifest: ObjectStackManifest): void { - for (const { type, name } of declaredBareNames(manifest)) { - let byName = this.bareNameClaims.get(type); - if (!byName) this.bareNameClaims.set(type, byName = new Map()); - this.noteBareNameClaim(type, name); - let claimants = byName.get(name); - if (!claimants) byName.set(name, claimants = new Set()); - claimants.add(packageId); - } + // [#33949 codemod] The body moved to `registry-bare-names.ts`, its docblock with it (see `BareNamesHost`). + return BareNamesFacet.recordBareNameClaims.call(bareNamesHost.of(this), packageId, manifest); } - /** Drop every bare-name claim `packageId` holds. */ private forgetBareNameClaims(packageId: string): void { - for (const byName of this.bareNameClaims.values()) { - for (const [name, claimants] of byName) { - claimants.delete(packageId); - if (claimants.size === 0) byName.delete(name); - } - } + // [#33949 codemod] The body moved to `registry-bare-names.ts`, its docblock with it (see `BareNamesHost`). + return BareNamesFacet.forgetBareNameClaims.call(bareNamesHost.of(this), packageId); } /** @@ -5595,31 +2144,8 @@ export class SchemaRegistry { * conflicting function and the rename. */ claimBareNameFunctions(owner: string | undefined, names: readonly string[]): void { - if (!owner || names.length === 0) return; - const packageId = handlerOwnerPackageId(owner); - const unique = [...new Set(names.filter((n) => typeof n === 'string' && n !== ''))]; - if (this.bareNameHoldersSealed) { - const conflicts: BareNameConflict[] = []; - for (const name of unique) { - for (const existingHolder of this.bareNameHoldersOtherThan('function', name, packageId, { - builtIns: true, - incomingNamespace: this.bareNameHolderNamespace(packageId), - })) { - conflicts.push({ - type: 'function', name, incomingPackageId: packageId, existingHolder, - rename: this.bareNameRenameFor(packageId, name), - }); - } - } - if (conflicts.length > 0) throw new BareNameConflictError(conflicts); - } - let byName = this.bareNameClaims.get('function'); - if (!byName) this.bareNameClaims.set('function', byName = new Map()); - for (const name of unique) { - let claimants = byName.get(name); - if (!claimants) byName.set(name, claimants = new Set()); - claimants.add(packageId); - } + // [#33949 codemod] The body moved to `registry-bare-names.ts` (see `BareNamesHost`). + return BareNamesFacet.claimBareNameFunctions.call(bareNamesHost.of(this), owner, names); } /** @@ -5651,15 +2177,8 @@ export class SchemaRegistry { * as an owner-less bind claims nothing. */ releaseBareNameFunctions(owner: string | undefined, stillHolds: (name: string) => boolean): void { - if (!owner) return; - const byName = this.bareNameClaims.get('function'); - if (!byName) return; - const packageId = handlerOwnerPackageId(owner); - for (const [name, claimants] of [...byName]) { - if (!claimants.has(packageId) || stillHolds(name)) continue; - claimants.delete(packageId); - if (claimants.size === 0) byName.delete(name); - } + // [#33949 codemod] The body moved to `registry-bare-names.ts` (see `BareNamesHost`). + return BareNamesFacet.releaseBareNameFunctions.call(bareNamesHost.of(this), owner, stillHolds); } /** @@ -5676,63 +2195,8 @@ export class SchemaRegistry { * {@link sealBareNameHoldersAtColdBoot}'s mode. */ sealBareNameHolders(): BareNameConflict[] { - this.bareNameHoldersSealed = true; - const conflicts: BareNameConflict[] = []; - for (const type of BARE_NAME_ONE_HOLDER_TYPES) { - const candidates = new Set(); - if (isBareNameRegistryType(type)) { - for (const key of this.metadata.get(type)?.keys() ?? []) { - // A bundled type's key ends `@`: the NAME is the candidate. - const base = bundleBaseKey(key); - const sep = base.lastIndexOf(':'); - if (sep > 0) candidates.add(base.slice(sep + 1)); - } - } - for (const name of this.bareNameClaims.get(type)?.keys() ?? []) candidates.add(name); - for (const name of [...candidates].sort()) { - if (name === '') continue; - const packages = this.bareNamePackageHolders(type, name); - if (BUILT_IN_BARE_NAMES[type].has(name)) { - for (const packageId of packages) { - conflicts.push({ - type, name, incomingPackageId: packageId, existingHolder: { kind: 'built-in' }, - rename: this.bareNameRenameFor(packageId, name), - }); - } - continue; - } - // Each later holder against the first earlier one it meets - // (`bareNameHoldersMeet`): for a type that has not narrowed, always the - // first-registered; for one narrowed to the namespace, the first of its - // own namespace, and none when every earlier holder declares another. - packages.forEach((packageId, index) => { - const namespace = this.bareNameHolderNamespace(packageId); - const first = packages - .slice(0, index) - .find((earlier) => - bareNameHoldersMeet(type, namespace, this.bareNameHolderNamespace(earlier), name) - || this.bareNameBundleAdditionMeets(type, name, () => this.registeredBundleHolding(type, name, packageId), earlier)); - if (first === undefined) return; - conflicts.push({ - type, name, incomingPackageId: packageId, existingHolder: { kind: 'package', packageId: first }, - rename: this.bareNameRenameFor(packageId, name), - }); - }); - } - } - // [#31697, contract review G6] A package the boot registered that declares - // an action bound to a key the platform holds (`BUILT_IN_BOUND_ACTION_KEYS`). - for (const pkg of this.getAllPackages()) { - const packageId = pkg.manifest?.id; - if (typeof packageId !== 'string') continue; - for (const { name, objectName } of declaredBuiltInBoundActions(pkg.manifest)) { - conflicts.push({ - type: 'action', name, objectName, incomingPackageId: packageId, existingHolder: { kind: 'built-in' }, - rename: this.bareNameRenameFor(packageId, name), - }); - } - } - return conflicts; + // [#33949 codemod] The body moved to `registry-bare-names.ts` (see `BareNamesHost`). + return BareNamesFacet.sealBareNameHolders.call(bareNamesHost.of(this)); } // ========================================== @@ -5801,134 +2265,20 @@ export class SchemaRegistry { againstRegistry?: boolean; } = {}, ): Error | null { - const againstRegistry = options.againstRegistry === true && this.bareNameHoldersAreSealed; - const door = this.judgeRegistrationDoors(schema, packageId, ownership, { bodyOnly: !againstRegistry, againstRegistry }); - return 'refusal' in door ? door.refusal : null; + // [#33949 codemod] The body moved to `registry-object-registration.ts` (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.objectRegistrationRefusal.call(objectRegistrationHost.of(this), schema, packageId, ownership, options); } - /** - * [#29673] The registration doors of {@link registerObject}, moved here - * whole so {@link objectRegistrationRefusal} asks exactly what the - * registration throws: the refusal, or the body to register (an `extend` - * body re-keyed to the object it extends). `bodyOnly` skips the doors - * that read the registry's other objects, and `againstRegistry` (a sealed - * registry asked about a body whose unit is whole) is the only way these - * doors ask the follow door (see {@link objectRegistrationRefusal}); a - * registration run asks it of every body it registered, at its end - * ({@link restoringOnRefusal}). - */ private judgeRegistrationDoors( schema: ServiceObject, packageId: string | undefined, ownership: ObjectOwnership, opts: { bodyOnly?: boolean; againstRegistry?: boolean } = {}, ): { refusal: Error } | { schema: ServiceObject } { - // [#16319] ⭐ THE FIELD-`type` DOOR — first statement in the method, and - // its position is the whole point. - // - // MAINTAINER RULING 2026-09-10 (director seat batch #111 item 2): 「一个没写 - // type(或拼错)的字段 应该禁止加载」. This registry is the ONE choke point every - // metadata door passes — declared stacks, plugin/package manifests, - // `saveMetaItem`, the `sys_metadata` boot rehydration and raw - // `registerObject` calls — so one check here closes all of them at once, - // which is why the ruling put it here rather than at the four downstream - // guesses it also retires. - // - // ⛔ BEFORE `applySystemFields` and every other materialization below, so - // the object and field the refusal names are the AUTHOR's own and never a - // platform injection; and ⛔ before the contributor list is opened, so a - // refused declaration leaves no half-registered entry behind. - // - // ⛔ The whole object is refused; the offending field is never dropped on - // its own. See {@link ObjectFieldTypeRefusedError}. - const fieldTypeRefusal = objectFieldTypeRefusal(schema); - if (fieldTypeRefusal) return { refusal: fieldTypeRefusal }; - - // [ADR-0142 D3] Beside it, for its reason: an eligible object's own - // `queue` field is refused before the injection could be skipped for it — - // see {@link ObjectQueueColumnRefusedError}. A base layer only: an `extend` - // body is never planned on its own (#24314, below), so its `queue` is - // governed in the fold instead ({@link withoutExtensionQueueField}), - // whichever order the layers register in. - if (ownership !== 'extend') { - const queueRefusal = objectQueueColumnRefusal(schema); - if (queueRefusal) return { refusal: queueRefusal }; - } - - // [ADR-0095 D1 note; ruling E′ item 4] An `extend` contribution never sets - // the object's default row scope — see - // {@link ExtensionDefaultRowScopeRefusedError}. Here, beside the field-`type` - // door and for the same reason: before any materialization and before the - // contributor list is opened, so a refused contribution leaves nothing behind. - // [#25234 ruling A, refusal 10] Nor does it declare a membership key — see - // {@link ExtensionMembershipKeysRefusedError}. The same door, for the same - // reason, so a refused contribution leaves nothing behind. [#28574] Both - // are asked through {@link extendContributionRefusal}, the one copy the - // metadata save and publish doors also ask of an `object_extension` item. - if (ownership === 'extend') { - const extendRefusal = extendContributionRefusal(schema, packageId); - if (extendRefusal) return { refusal: extendRefusal }; - // [#28858] …filed under the key the object it extends is registered - // under — see {@link extensionTargetKey}. Every entry that registers an - // extension reaches this line: the manifest `objectExtensions` loop and - // a stored `object_extension` row ({@link registerObjectExtension}). - const target = this.extensionTargetKey(String(schema.name ?? ''), packageId); - if (target !== schema.name) schema = { ...schema, name: target }; - } - // [#29673] The pre-flight question stops here: the door below reads the - // registry's other objects, not the body alone. - if (opts.bodyOnly) return { schema }; - // [ADR-0144 D6] A `summary` roll-up that reads a `record`-mode amount — - // `sum` / `avg` / `min` / `max` of a child field whose rows each carry - // their own currency — is refused here, the door every metadata path - // passes, before any materialization: the pair is judged at whichever - // registration completes it. See `record-currency-aggregate-door.ts`. - const recordCurrencySummary = recordCurrencySummaryRefusal( - schema, - (name) => this.getObject(name), - () => this.getAllObjects(), - ); - if (recordCurrencySummary) return { refusal: recordCurrencySummary }; - // [ADR-0144 D2 and D6, amended] Beside it, for the same reason: a - // following amount whose parent holds no code field under that name, and a - // `record` roll-up over a child amount that follows nothing — the halves - // `defineStack` judges where a stack holds both objects, for the - // single-object save that holds one. Asked here only of a caller whose - // unit is whole and who relays the refusal (`againstRegistry`, which - // {@link objectRegistrationRefusal} grants only once the cold boot has - // sealed the registry): before the seal an `extend` contribution or the - // other end can still register and contradict the verdict (AGENTS.md, - // startup registry reads), and a multi-registration unit after it — - // `registerApp`'s objects before its `objectExtensions`, the packages of - // one artifact, a batch writer's rows in its own order — has the same - // hazard at each of its registrations. So `registerObject` never asks - // it. The unit does, once it is whole: a batch writer through this - // option, a registration run at its end - // ({@link recordCurrencyFollowRefusalOfRun}). - if (opts.againstRegistry === true && this.bareNameHoldersAreSealed) { - const recordCurrencyFollow = this.recordCurrencyFollowRefusalOf(schema, ownership, { packageId, before: null }); - if (recordCurrencyFollow) return { refusal: recordCurrencyFollow }; - } - return { schema }; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.judgeRegistrationDoors.call(objectRegistrationHost.of(this), schema, packageId, ownership, opts); } - /** - * [ADR-0144 D2 and D6, amended] The follow door's question of one layer, as - * `ownership`, against the registry as it stands: the one copy a single - * save ({@link judgeRegistrationDoors}) and a registration run - * ({@link recordCurrencyFollowRefusalOfRun}) ask. `all` answers the - * registered objects; a caller asking of several bodies reads them once. - * `lookup` answers one of them by name, and is the served object unless the - * caller reads an object another way ({@link followRefusalAsServed}). - * - * [#33593 ruling A, amended] The door judges the layer's object AS SERVED - * with the layer in its slot ({@link servedFoldWithLayer}) — never the bare - * body, so an `extend` contribution's following amount is judged as an own - * body's is (R1), and an owner judges the amounts the extensions already - * registered under it add (R3). An object no owner serves is no verdict - * (R2). `layer.before` is what the slot held before: a pair already refused - * with it in place is not this layer's to answer for (R7). - */ private recordCurrencyFollowRefusalOf( schema: ServiceObject, ownership: ObjectOwnership, @@ -5936,64 +2286,23 @@ export class SchemaRegistry { all: () => readonly ServiceObject[] = () => this.getAllObjects(), lookup: (name: string) => ServiceObject | undefined = (name) => this.getObject(name), ): RecordCurrencyFollowRefusedError | undefined { - const fqn = String(schema.name ?? ''); - const served = this.servedFoldWithLayer(fqn, ownership, layer.packageId, { - packageId: layer.packageId, - namespace: '', - ownership, - priority: layer.priority ?? this.followDoorSlotPriority(fqn, ownership, layer.packageId), - definition: { ...schema, name: fqn }, - }); - if (served === undefined) return undefined; - const before = this.servedFoldWithLayer(fqn, ownership, layer.packageId, layer.before ?? undefined); - return recordCurrencyFollowRefusal(served, { - lookup, - all, - extensionPackageOf: (name, field) => { - const key = this.resolveObjectKey(name); - if (key === undefined) return undefined; - // As the served fold reads the object ({@link resolveObject}): a - // disabled package's extension is withheld; the last to add wins. - const adders = this.getObjectContributors(key).filter((c) => c.ownership === 'extend' - && !this.isPackageDisabled(c.packageId) - && Object.prototype.hasOwnProperty.call((c.definition as { fields?: object }).fields ?? {}, field)); - return adders.length === 0 ? undefined : adders[adders.length - 1].packageId; - }, - }, before); + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.recordCurrencyFollowRefusalOf.call(objectRegistrationHost.of(this), schema, ownership, layer, all, lookup); } - /** - * [#33593 ruling R1] The object `fqn` as {@link resolveObject} would serve - * it with `layer` filling the slot `ownership` and `packageId` name (one - * owner, one overlay, one extension per package — the replacement rules - * {@link registerObject} applies), or with that slot empty when `layer` is - * `undefined`. `undefined` when no owner would serve it. Uncached: it is a - * question about a registry the caller has not written. - */ private servedFoldWithLayer( fqn: string, ownership: ObjectOwnership, packageId: string | undefined, layer: ObjectContributor | undefined, ): ServiceObject | undefined { - const others = (this.objectContributors.get(fqn) ?? []).filter((c) => c.ownership !== ownership - || (ownership === 'extend' && c.packageId !== packageId)); - const contributors = layer === undefined ? others : [...others, layer].sort((a, b) => a.priority - b.priority); - const owner = contributors.find((c) => c.ownership === 'own'); - if (!owner) return undefined; - const base = contributors.find((c) => c.ownership === 'overlay') ?? owner; - const withheld = this.withholdDisabledExtensions(contributors); - return withheld - ? this.foldExtendersOntoDefinition(withheld.kept, withoutWithheldMembers(base.definition, withheld)) - : this.foldExtenders(contributors, base); - } - - /** The priority a layer about to register in the slot would take: the sitting one's, else the kind's default. */ + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.servedFoldWithLayer.call(objectRegistrationHost.of(this), fqn, ownership, packageId, layer); + } + private followDoorSlotPriority(fqn: string, ownership: ObjectOwnership, packageId: string | undefined): number { - const sitting = (this.objectContributors.get(fqn) ?? []).find((c) => c.ownership === ownership - && (ownership !== 'extend' || c.packageId === packageId)); - if (sitting) return sitting.priority; - return ownership === 'own' ? DEFAULT_OWNER_PRIORITY : ownership === 'overlay' ? DEFAULT_OVERLAY_PRIORITY : DEFAULT_EXTENDER_PRIORITY; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.followDoorSlotPriority.call(objectRegistrationHost.of(this), fqn, ownership, packageId); } /** @@ -6032,278 +2341,8 @@ export class SchemaRegistry { ? DEFAULT_OVERLAY_PRIORITY : DEFAULT_EXTENDER_PRIORITY ): string { - // [#29673] The registration DOORS — every refusal this method answers - // before any materialization and before the contributor list is opened — - // live in {@link judgeRegistrationDoors}, so the metadata doors that make - // a body live can ask the same question BEFORE they promote it - // ({@link objectRegistrationRefusal}) instead of meeting the throw after - // the row is already active. Still the first statement here, and the - // position is still the whole point (the comments inside say why). - const door = this.judgeRegistrationDoors(schema, packageId, ownership); - if ('refusal' in door) throw door.refusal; - schema = door.schema; - // The body as the doors judged it, before anything below materializes it: - // what an open registration run is asked about at its end (below, where - // the layer is stored). - const judged = schema; - - // [ADR-0150 D3, #31700; epic seat ruling R2-A, 6812862622] After the - // boot sealed the picklist vocabulary, a packaged body registered on its - // own (outside an install judged as a whole: a plugin or the metadata - // service registering through the registry directly, a reloaded object) - // meets the one re-judge before its layer is stored: a field it writes - // must not leave another package's field of the same name writing to a - // different list ({@link refusePicklistItemArrival}). ⛔ Never an overlay - // or a tenant-authored body: it fills no package's slot and writes no - // package's reference, resolving as the environment's (`picklistReferrerOf`; - // swept for seat ruling R3-A). - if (packageId && (ownership === 'own' || ownership === 'extend') && !isTenantAuthored(schema)) { - this.refusePicklistItemArrival( - packageId, - ownership === 'own' - ? { objects: [schema] } - : { objectExtensions: [{ extend: schema.name, fields: (schema as { fields?: unknown }).fields }] }, - `object '${schema.name}' of package '${packageId}'`, - ); - } - - // [#25744] A code package's object keeps its own actions as their - // `ActionSchema` parse — `requiresFeature` / `requiresMembershipReach` - // lowered into `visible` — or is refused here, beside the doors above and - // for their reason: before any materialization and before the contributor - // list is opened, so a refused object leaves nothing behind and a refused - // re-registration leaves the registered one in place - // ({@link parsePackageObjectActions}). The test is `registerItem`'s action - // parse's, asked of the body as `applyProtection` below will stamp it (a - // package id it does not already carry, `'package'` provenance): ⛔ a - // registration with no `packageId`, the `sys_metadata` sentinel, or a - // tenant-authored body (`_provenance: 'org'` — the save door's write-through - // and the boot rehydration) is never judged here. - if (packageId && isCodeArtifactBody({ - _packageId: (schema as { _packageId?: unknown })._packageId ?? packageId, - _provenance: (schema as { _provenance?: unknown })._provenance ?? 'package', - })) { - schema = parsePackageObjectActions(schema, packageId); - } - - // [#23065] A `tree` field that names no `reference` references the object - // that declares it, and this is where that is written down for every door - // the parse does not cover: a metadata save stores the authored body - // verbatim and the `sys_metadata` rehydration and a raw `registerObject` - // never parse at all. `ObjectSchema`'s parse writes the same thing through - // the same spec helper, so the two doors cannot disagree, and every reader - // of the registered schema — the referential delete walk, the write-time - // reference check, `expand`, nested relation conditions — sees an ordinary - // self-reference. ⛔ No reader resolves it on its own. An extension - // registers under the object it extends (`name: ext.extend`), which is the - // object its trees reference. - const treeFields = materializeTreeSelfReferences(schema.name, schema.fields); - if (treeFields !== schema.fields) schema = { ...schema, fields: treeFields }; - - // Apply system-field injection (multi-tenant org_id, future owner/audit) - // BEFORE FQN computation and contributor storage so every consumer of - // the registered schema (driver syncSchema, REST projector, hooks) - // sees the same canonical shape. Author-declared fields win — see - // applySystemFields(). - // - // [#24314] A BASE layer only (`own` / `overlay`). An `extend` body is a - // bag of fields for an object someone else declares, so it is never - // planned on its own: it carries none of the declarations the plan reads - // (`managedBy`, `systemFields`, `tenancy`, `ownership`), and planning it - // as an ordinary object injected the full column set onto targets whose - // own declaration withholds it. Measured on a walled boot: an extension of - // `sys_organization` (`managedBy: 'better-auth'`) added `organization_id`, - // NULL on every row, and the wall then hid the organisation from its own - // members; the same on `sys_user`. The fold also let the injected - // definition REPLACE a column the owner declares itself: plugin-auth's - // own `sys_sso_provider` extension swapped the authored `organization_id` - // for the platform's lookup. So the extended object's system columns are - // exactly its base layer's plan, whichever order the layers register in. - if (ownership !== 'extend') { - schema = applySystemFields(schema, { - multiTenant: this.multiTenant, - platformGlobalObjects: this.deploymentPlatformGlobalObjects, - }); - } - - // [ADR-0092 / #1591] Reconcile generic-write `apiMethods` against the CRUD - // affordances a better-auth-managed object actually grants — strip verbs - // the identity write guard would reject anyway, so the HTTP exposure gate - // answers a clean 405 and the metadata can't contradict itself. No-op for - // every non-`better-auth` object. - schema = reconcileManagedApiMethods(schema); - - // [#3543] One-shot per-object diagnostic for retired LEGACY apiMethods - // values (the derived 8) — ignored by the derivation resolver; this adds - // the object-name context the parse-time strip warning lacks. Runs after - // reconcile so we diagnose what actually ships. - warnStrippedLegacyApiMethods(schema); - - // [ADR-0078 Phase 4] One-shot per-object functional-completeness - // diagnostic — the registry is the choke point every metadata door goes - // through, including the ones that skip Zod and lint (#3896, raw - // registerObject). Same shared predicate and rule ids as `os lint`; - // warn-never-throw (an inert FIELD must not kill the boot). - warnFunctionalCompleteness(schema); - - // [ADR-0079] Object-materialization seam — DESIGNATE-ONLY primary-title - // provisioning. Runs AFTER `applySystemFields` (so any designated field - // co-exists with the injected system columns) and ONLY for owned objects - // (extensions must not redesignate the owner's title). `synthesize: false` - // means: when a title-eligible field already EXISTS, set `nameField` to it - // (so `nameField` is reliably populated for normal/user/AI-built objects, - // which always carry a text label); when NOTHING is title-eligible, the - // object is left exactly as-is. We deliberately do NOT synthesize a `name` - // column here — that would materialize a new DB column on title-less - // system/append-only tables (sys_record_share, sys_member, …) via the - // driver's `syncSchema`, a schema-migration-bearing change. Those keep - // resolving their title on read via `resolveDisplayField` / `titleFormat`. - // - // [ADR-0029 D9, blast-radius row 2] The gate asks "is this a BASE LAYER", - // not "is this the owner". An `overlay` REPLACES the base at resolution - // time (D9.2), so the overlay body IS the resolved object — gating title - // provisioning on `own` alone would silently change `nameField` on every - // overlaid object, which is the same designation the packaged twin gets. - // Extensions still never redesignate: they merge into a base that has - // already been provisioned. - if (ownership === 'own' || ownership === 'overlay') { - schema = this.materializeBaseLayer(schema); - } - - const shortName = schema.name; - const fqn = computeFQN(namespace, shortName); - - // Ensure namespace is registered. [#12623] `packageId` is now optional - // on this method, so a namespace passed without one has no owner to - // record — skip rather than registering `undefined` as an owner (which - // would also mean widening `registerNamespace`'s own required-string - // contract, out of scope here). - // [#28697] Recorded before the first write below (the namespace claim), - // so a refusal raised after it — the ownership conflict — puts this - // object's layers back exactly ({@link restoringOnRefusal}). - this.noteObjectEntry(fqn); - - if (namespace && packageId) { - this.registerNamespace(namespace, packageId); - } - - // Get or create contributor list (#10945: the one creation choke point — - // it keeps `objectKeysByShortName` in step with `objectContributors`) - const contributors = this.openObjectEntry(fqn); - - // [#33593 ruling R7] What this layer's slot held, for the open run's end. - let replaced: ObjectContributor | null = null; - // Validate ownership rules - if (ownership === 'own') { - const existingOwner = contributors.find(c => c.ownership === 'own'); - if (existingOwner && isTenantAuthored(existingOwner.definition) && !isTenantAuthored(schema)) { - // ── LATE INSTALL (ADR-0029 D9 §6.1) ── - // - // A code package registers an object a TENANT row already holds. That - // happens whenever the hydration seams run before the package does — - // a `sys_metadata` row written on a kernel where nothing shipped the - // name, then a package that later ships it (install, upgrade, or a - // boot whose order puts `loadMetaFromDb` first). Before D9 the tenant - // row had taken the `own` slot by default, so this threw "already - // owned by" under a foreign id, or SPLICED THE TENANT'S BODY AWAY - // under the same id. - // - // D9's recommended default, and the only outcome that loses nothing: - // the CODE layer becomes the owner and the tenant contribution is - // re-classified as its overlay layer — exactly the two layers that - // would exist had the package registered first. The resolved object - // does not move (the tenant body is the base either way, D9.2); what - // changes is that the packaged definition now survives underneath it, - // so `isArtifactBacked` is honest from this moment on. - // - // Deliberately narrow: it fires ONLY when the sitting owner is - // tenant-authored (`_provenance: 'org'`) and the incoming body is not. - // Two code packages claiming one name is D3's cross-package refusal - // and still throws below, whatever the ids. - const staleOverlay = contributors.findIndex(c => c.ownership === 'overlay'); - if (staleOverlay !== -1) contributors.splice(staleOverlay, 1); - existingOwner.ownership = 'overlay'; - existingOwner.priority = DEFAULT_OVERLAY_PRIORITY; - this.log( - `[Registry] Late install of "${fqn}": package "${packageId}" takes ownership; ` + - `the tenant contribution (${existingOwner.packageId}) becomes its overlay layer.` - ); - } else if (existingOwner && existingOwner.packageId !== packageId) { - // [ADR-0029 D3] Two packages claiming one name — the cross-package - // refusal, carried as an ADR-0112 envelope (`code` + `status: 422`) - // with the message text unchanged. See {@link ObjectOwnershipConflictError}. - throw new ObjectOwnershipConflictError(fqn, existingOwner.packageId, packageId); - } else if (existingOwner) { - // Remove existing owner contribution from same package (re-registration). - // Normal path (metadata rebuild / HMR / multi-project seed replays the - // same owned object), not an error — keep it at debug so a stock boot - // stays warning-free (#3420). - contributors.splice(contributors.indexOf(existingOwner), 1); - replaced = existingOwner; - this.debug(`[Registry] Re-registering owned object: ${fqn} from ${packageId}`); - } - } else if (ownership === 'overlay') { - // [ADR-0029 D9.9] AT MOST ONE overlay layer per object name, replaced on - // re-write — and keyed by KIND, never by package. The row's `package_id` - // is provenance ON the layer, not a second identity for it: the - // overlay-uniqueness index in `sys_metadata` keys on - // `(type, name, organization_id, COALESCE(package_id, ''))` and can - // legitimately hold two rows for one `(type, name)` bound to two - // packages, which this registry could never represent because - // `computeFQN` is identity. Keying the replacement by package would let - // that unrepresentable shape in through the side door; the mis-bound row - // is refused at the PRODUCER instead (`saveMetaItem`), and counted in - // `loadMetaFromDb`'s per-record `errors` at boot. - const idx = contributors.findIndex(c => c.ownership === 'overlay'); - if (idx !== -1) { - replaced = contributors.splice(idx, 1)[0]; - this.debug(`[Registry] Replacing overlay layer of object: ${fqn} from ${packageId}`); - } - } else { - // extend mode: remove existing extension from same package - const idx = contributors.findIndex(c => c.packageId === packageId && c.ownership === 'extend'); - if (idx !== -1) { - replaced = contributors.splice(idx, 1)[0]; - } - } - - // ADR-0010 §3.7 — translate the author-facing `protection` block - // into the private `_lock` envelope and stamp package provenance - // on the schema before it lands in the contributor list. Mirrors - // registerItem() so object schemas surface lock fields on GET — including - // its note on the `_packageVersion` a manifest-holding caller stamps first. - applyProtection(schema as any, { packageId }); - - // Add new contributor - const contributor: ObjectContributor = { - packageId, - namespace: namespace || '', - ownership, - priority, - definition: { ...schema, name: fqn }, // Store with FQN as name - }; - contributors.push(contributor); - // [ADR-0144 D2 and D6, amended] …and told to the open registration run, - // under the slot the layer just filled (the replacement rules above: one - // owner, one overlay, one extension per package), so the run's end asks - // the record-currency follow door of it with the whole run registered - // ({@link restoringOnRefusal}). Outside a run nothing is recorded. - this.restorePoint?.noteRegisteredObject( - `${fqn}\u0000${ownership}${ownership === 'extend' ? `\u0000${packageId ?? ''}` : ''}`, - { fqn, schema: judged, contributor, replaced }, - ); - - // Sort by priority (ascending: lower priority applied first) - contributors.sort((a, b) => a.priority - b.priority); - - // Invalidate merge cache - this.mergedObjectCache.delete(fqn); - // …and tell registry-derived caches (the engine's roll-up summary index) - // that the object set moved, whichever path got here. - this._objectRevision += 1; - - this.log(`[Registry] Registered object: ${fqn} (${ownership}, priority=${priority}) from ${packageId}`); - return fqn; + // [#33949 codemod] The body moved to `registry-object-registration.ts` (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.registerObject.call(objectRegistrationHost.of(this), schema, packageId, namespace, ownership, priority); } /** @@ -6311,50 +2350,8 @@ export class SchemaRegistry { * Returns the merged object or undefined if not found. */ resolveObject(fqn: string): ServiceObject | undefined { - // Check cache first - const cached = this.mergedObjectCache.get(fqn); - if (cached) return cached; - - const contributors = this.objectContributors.get(fqn); - if (!contributors || contributors.length === 0) { - return undefined; - } - - // Find owner (must exist for a valid object) - const ownerContrib = contributors.find(c => c.ownership === 'own'); - if (!ownerContrib) { - // [ADR-0029 D9.5] An ORPHAN OVERLAY lands here too, and says so: the - // tenant can still see the row in `sys_metadata`, so "extenders" would - // be the wrong noun to hand whoever reads this line. - console.warn( - contributors.some(c => c.ownership === 'overlay') - ? `[Registry] Object "${fqn}" has an overlay layer but no owner. Skipping.` - : `[Registry] Object "${fqn}" has extenders but no owner. Skipping.` - ); - return undefined; - } - - // [ADR-0029 D9.2] The BASE layer is the tenant overlay when one is - // registered, and the owner otherwise — `overlay ?? own`. Selection asks - // the KIND (D9.3), never the priority. - // - // This is deliberately BIT-FOR-BIT what the pre-D9 splice produced: the - // overlay used to BE the owner, so the fold already ran over the overlay - // body. The resolved schema — `_provenance: 'org'` included, which every - // registry-direct consumer reads — does not move. Only what the registry - // REMEMBERS changes: the packaged owner is still here, underneath. - const baseContrib = contributors.find(c => c.ownership === 'overlay') ?? ownerContrib; - // A disabled package's `extend` contributions are withheld from the served - // fold ({@link withholdDisabledExtensions}); the cache is dropped by every - // enable and disable ({@link invalidateAll}). - const withheld = this.withholdDisabledExtensions(contributors); - const merged = withheld - ? this.foldExtendersOntoDefinition(withheld.kept, withoutWithheldMembers(baseContrib.definition, withheld)) - : this.foldExtenders(contributors, baseContrib); - - // Cache the result - this.mergedObjectCache.set(fqn, merged); - return merged; + // [#33949 codemod] The body moved to `registry-object-registration.ts` (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.resolveObject.call(objectRegistrationHost.of(this), fqn); } /** @@ -6369,229 +2366,47 @@ export class SchemaRegistry { * Accepts a short name or an FQN, as {@link getObject} does. */ getStorageObject(name: string): ServiceObject | undefined { - const fqn = this.resolveObjectKey(name); - return fqn === undefined ? undefined : this.resolveStorageObject(fqn); + // [#33949 codemod] The body moved to `registry-object-registration.ts` (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.getStorageObject.call(objectRegistrationHost.of(this), name); } /** {@link getAllObjects} at the storage layer ({@link getStorageObject}), each tagged with its owner's `_packageId`. */ getAllStorageObjects(): ServiceObject[] { - // Nothing disabled: the two layers are one answer, read through the one walk. - if (!this.hasDisabledPackage()) return this.getAllObjects(); - const results: ServiceObject[] = []; - for (const fqn of this.objectContributors.keys()) { - const merged = this.resolveStorageObject(fqn); - if (!merged) continue; - (merged as any)._packageId = this.getObjectOwner(fqn)?.packageId; - results.push(merged); - } - return results; + // [#33949 codemod] The body moved to `registry-object-registration.ts` (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.getAllStorageObjects.call(objectRegistrationHost.of(this)); } - /** See {@link getStorageObject}. Uncached when it differs from the served fold, as {@link resolveOwnerLayer} is. */ private resolveStorageObject(fqn: string): ServiceObject | undefined { - const contributors = this.objectContributors.get(fqn); - if (!contributors || !contributors.some((c) => c.ownership === 'extend' && this.isPackageDisabled(c.packageId))) { - return this.resolveObject(fqn); - } - const ownerContrib = contributors.find(c => c.ownership === 'own'); - if (!ownerContrib) return this.resolveObject(fqn); - return this.foldExtenders(contributors, contributors.find(c => c.ownership === 'overlay') ?? ownerContrib); + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.resolveStorageObject.call(objectRegistrationHost.of(this), fqn); } - /** - * The `extend` contributions of a DISABLED package are withheld from every - * served fold of the object they extend, as the package's own objects, - * flows, navigation and actions are: "disabled" means hidden, refused on the - * data plane, data kept. The served object loses the fields, rules and - * indexes such an extension adds and the patches it makes, so the data - * plane's field map (its default projection and its unknown-field refusal) - * loses them too; the storage layer keeps them ({@link getStorageObject}). - * - * `withheldFields` / `withheldRules` / `withheldIndexes` are the members a - * withheld extension adds that neither the owner's own declaration nor a - * kept extension also declares (indexes compared structurally, as the fold's - * own index subtraction compares them). A base body that already carries them (a tenant overlay saved - * from a served read, a metadata-service copy seeded from a resolved body) - * has them taken off ({@link withoutWithheldMembers}), so where the base - * came from does not decide what a disable hides. - * - * `undefined` when no extension of the object is withheld, so the ordinary - * fold runs unchanged. - * - * [#30756] `asEnabled` names a package whose contributions are kept although - * it is disabled: the fold the enable of that package would leave - * ({@link ObjectExtensionFoldOptions}). - */ private withholdDisabledExtensions(contributors: readonly ObjectContributor[], asEnabled?: string): WithheldExtensions | undefined { - const withheld = contributors.filter((c) => c.ownership === 'extend' - && this.isPackageDisabled(c.packageId) - && (asEnabled === undefined || c.packageId !== asEnabled)); - if (withheld.length === 0) return undefined; - const kept = contributors.filter((c) => !withheld.includes(c)); - const owner = contributors.find((c) => c.ownership === 'own')?.definition; - const keptFields = new Set(ownFieldNamesOf(owner) ?? []); - const keptRules = new Set(ruleNamesOf(owner)); - const keptIndexes = new Set(indexKeysOf(owner)); - for (const c of kept) { - if (c.ownership !== 'extend') continue; - for (const name of fieldNamesOf(c.definition)) keptFields.add(name); - for (const name of ruleNamesOf(c.definition)) keptRules.add(name); - for (const key of indexKeysOf(c.definition)) keptIndexes.add(key); - } - const withheldFields = new Set(); - const withheldRules = new Set(); - const withheldIndexes = new Set(); - for (const c of withheld) { - for (const name of fieldNamesOf(c.definition)) if (!keptFields.has(name)) withheldFields.add(name); - for (const name of ruleNamesOf(c.definition)) if (!keptRules.has(name)) withheldRules.add(name); - for (const key of indexKeysOf(c.definition)) if (!keptIndexes.has(key)) withheldIndexes.add(key); - } - return { kept, withheldFields, withheldRules, withheldIndexes }; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.withholdDisabledExtensions.call(objectRegistrationHost.of(this), contributors, asEnabled); } - /** - * [#29903, #29872, ADR-0146 D8 item 3] An object as {@link resolveObject} - * resolves it, but with the `extend` contributions `options` names left out - * of the fold ({@link ObjectExtensionFoldOptions}), as if those packages had - * never extended it: the same base layer (`overlay ?? own`), every other - * extender folded in the same order, the same tail. With no such - * contribution it IS {@link resolveObject}'s answer (the cached body). - * Uncached otherwise, as {@link resolveOwnerLayer} is. - * - * The reader is the protocol's per-package layer, which serves a package's - * objects to the package's own export as the package declares them: never - * with an extension another package contributes (its `.custom` pair's, or - * any other installed package's). That extension travels in its own - * package's export, as its `objectExtensions` entry - * ({@link getObjectExtensionsOf}); folded in here too, its field and field - * patches travel twice, the second time as if the owner shipped them. - */ private resolveObjectLeavingExtensionsOut(fqn: string, options: ObjectExtensionFoldOptions | undefined): ServiceObject | undefined { - // [#30756] A disabled package folded in as if enabled; one that is not - // disabled changes nothing, so it does not leave the cached fold. - const asEnabled = options?.asEnabled !== undefined && this.isPackageDisabled(options.asEnabled) - ? options.asEnabled - : undefined; - const contributors = leavesExtensionsOut(options) || asEnabled !== undefined ? this.objectContributors.get(fqn) : undefined; - if (!contributors || !contributors.some((c) => isLeftOutExtension(c, options!) - || (asEnabled !== undefined && c.ownership === 'extend' && c.packageId === asEnabled))) return this.resolveObject(fqn); - const kept = contributors.filter((c) => !isLeftOutExtension(c, options!)); - const ownerContrib = kept.find(c => c.ownership === 'own'); - if (!ownerContrib) return undefined; - const base = kept.find(c => c.ownership === 'overlay') ?? ownerContrib; - // …and a disabled package's, as on every served fold ({@link withholdDisabledExtensions}), - // except the one `asEnabled` names. - const withheld = this.withholdDisabledExtensions(kept, asEnabled); - return withheld - ? this.foldExtendersOntoDefinition(withheld.kept, withoutWithheldMembers(base.definition, withheld)) - : this.foldExtenders(kept, base); + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.resolveObjectLeavingExtensionsOut.call(objectRegistrationHost.of(this), fqn, options); } - /** - * Fold every `extend` contribution over a chosen base layer, in the list's - * (priority-sorted) order. Extracted from {@link resolveObject} so the - * code-layer resolution ({@link resolveOwnerLayer}) folds extenders exactly - * the same way rather than growing a second, drifting copy. - */ private foldExtenders( contributors: ObjectContributor[], base: ObjectContributor, layer: ServedObjectLayer = 'served', ): ServiceObject { - return this.foldExtendersOntoDefinition(contributors, base.definition, layer); + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.foldExtenders.call(objectRegistrationHost.of(this), contributors, base, layer); } - /** - * The fold itself, over a base DEFINITION rather than a base contributor, so - * {@link foldObjectExtendersOnto} can apply it to a body that never came - * from this registry without growing a second copy of the merge. - */ private foldExtendersOntoDefinition( contributors: ObjectContributor[], baseDefinition: ServiceObject, layer: ServedObjectLayer = 'served', ): ServiceObject { - // [ADR-0029 D9.2a] Computed ONCE, over the base the fold starts from — never - // re-derived from the running `merged`, which would make an extender's own - // scalar look "authored" to the next extender and silently invert - // extender-vs-extender precedence (D9.3: declared numbers order peers). - const tenantAuthored = this.tenantAuthoredScalars(contributors, baseDefinition); - // [#24314] An `extend` body is not planned at registration, so an audit - // field it declares is governed HERE, by the base layer's plan: where the - // base carries the audit family, a `created_at` an extender declares cannot - // replace the platform's read-only definition with a writable one (#4447). - const governAudit = resolveInjectedSystemColumns(baseDefinition).audit; - // [ADR-0142 D3] The ownership-queue column, for the same reason: the - // registration door's refusal ({@link objectQueueColumnRefusal}) never sees - // an `extend` body, so where the base plans `queue` an extender's own `queue` - // field is dropped here and the platform's column stands. Letting it replace - // the injected definition would read as `author` provenance and switch the - // access path, the claim and the assignment seam off on this object, while - // the gate kept judging the extender's business column. - const governQueue = resolveInjectedSystemColumns(baseDefinition).queue; - // [ADR-0029 D9.2a, 2026-10-30 note] A tenant base's rules are copies: each - // one a code layer ships by name runs as the package declares it, with the - // tenant's `message` and `active`. Computed once, over the base, as the - // scalars above are. - const tenantRules = this.tenantRuleSettings(contributors, baseDefinition); - // [ADR-0146 D10, second rule; ADR-0029 D9.2a] The display keys the tenant - // authored on the fields the extenders patch — computed once, over the - // base, for the scalars' reason above. - const tenantPatched = this.tenantPatchedDisplayKeys(contributors, baseDefinition); - // [ADR-0146 D10, second rule] The fields a patch reaches: the owner's own. - const owner = contributors.find((c) => c.ownership === 'own')?.definition; - const ownFields = ownFieldNamesOf(owner); - let merged = tenantRules - ? this.withPackagedOwnerRules(contributors, baseDefinition, tenantRules) - : { ...baseDefinition }; - // [#28835] Each extension's `fields` as the merge took them, in fold order. - const folded: FoldedExtensionFields[] = []; - for (const contrib of contributors) { - if (contrib.ownership === 'extend') { - let extension = governAudit ? applyAuditFieldGovernance(contrib.definition) : contrib.definition; - if (governQueue) extension = this.withoutExtensionQueueField(extension, contrib.packageId); - // [#28809] The judge, asked of the owner's fields as the OWNER declares - // them (what the install door and `os validate` read), never as an - // extension's `fields` redeclared them; the merge applies only what it - // admits. A field counts as carried as the merge below will carry it: - // in the running body's record spelling, or redeclared by this body. - const patches = (extension as ObjectExtensionBody).fieldPatches; - const running = merged; - const misses = patches && owner && ownFields - ? fieldPatchTargetMisses( - String(baseDefinition.name ?? extension.name ?? ''), - patches, - ownFieldsTarget(owner, ownFields, (name) => !!(recordFieldOf(running, name) ?? recordFieldOf(extension, name))), - ) - : []; - const admitted = misses.length > 0 - ? { ...extension, fieldPatches: admittedFieldPatches(patches!, misses) } as ObjectExtensionBody - : extension; - merged = mergeObjectDefinitions(merged, admitted, tenantAuthored, tenantRules, tenantPatched, ownFields); - // [#28809] …and the same judge once more, over the admitted patches - // against the owner's fields as MERGED, which only names: an admitted - // option an earlier layer left out (an extension's `fields` - // redeclaration, a tenant overlay base) had nothing to relabel, so the - // fold names it as it names any patch it skipped. Never drops, never - // refuses; admission above stays the owner's. - const admittedPatches = (admitted as ObjectExtensionBody).fieldPatches; - const residual = admittedPatches && owner && ownFields - ? fieldPatchTargetMisses( - String(baseDefinition.name ?? extension.name ?? ''), - admittedPatches, - ownFieldsTarget(merged, ownFields, (name) => !!recordFieldOf(merged, name)), - ) - : []; - this.reportFieldPatchMisses([...misses, ...residual], contrib.packageId); - folded.push({ packageId: contrib.packageId, fields: extension.fields }); - } - } - this.reportRedeclaredFields(String(merged.name ?? baseDefinition.name ?? ''), contributors, ownFields, folded); - if (tenantRules) this.reportChangedRuleCopies(contributors, baseDefinition); - // A picklist-bound field is served with its list's options resolved onto - // it — here, in the fold every object read and the write door share, so - // the set a client is offered and the set a write is judged by are one. - return this.finishServedObject(merged, layer, picklistReferrerOf(contributors)); + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.foldExtendersOntoDefinition.call(objectRegistrationHost.of(this), contributors, baseDefinition, layer); } /** @@ -6608,103 +2423,22 @@ export class SchemaRegistry { */ private redeclaredFieldsWarned = new Set(); - /** - * [#28835] Name, once, each field name of the object that more than one - * contributor declares in `fields`: two extensions both adding it, or an - * extension redeclaring a field the owner declares (one of `ownFields`). The - * merge spreads `fields`, so the definition served is the last one the fold - * applied: contributions fold in ascending `priority`, and at equal priority - * in the order they registered, so the one registered last. The line names - * the object, the field, every declaring package in that order, and the one - * whose definition is served. - * - * Judged from the contributors' own declarations (`folded`, as the merge - * took them), never from the running body, so a base that already carries - * the extensions' fields (a folded body handed to - * {@link foldObjectExtendersOnto}, a stored overlay row) is not read as a - * redeclaration. `fieldPatches` is not a declaration and is not counted - * ({@link reportFieldPatchMisses} names a patch that misses). A `.custom` - * pair's `__c` field and a dependent's plain one never share a name. - * - * ⛔ Never a refusal, and the served definition is unchanged: the same - * reasons as {@link reportFieldPatchMisses} (the fold runs on every read - * while a boot is still registering packages). At `warn`: one package's - * definition of the field is not served. - */ private reportRedeclaredFields( object: string, contributors: readonly ObjectContributor[], ownFields: ReadonlySet | undefined, folded: readonly FoldedExtensionFields[], ): void { - if (folded.length === 0) return; - const owner = contributors.find((c) => c.ownership === 'own'); - const declarers = new Map>(); - for (const { packageId, fields } of folded) { - if (!isPlainRecord(fields)) continue; - for (const name of Object.keys(fields)) { - let list = declarers.get(name); - if (!list) { - list = owner && ownFields?.has(name) ? [{ packageId: owner.packageId, owner: true }] : []; - declarers.set(name, list); - } - list.push({ packageId, owner: false }); - } - } - const named = (packageId: string | undefined) => (packageId ? `"${packageId}"` : '(no package)'); - for (const [field, list] of declarers) { - if (list.length < 2) continue; - const key = `${object}|${field}|${list.map((d) => d.packageId ?? '').join(',')}`; - if (this.redeclaredFieldsWarned.has(key)) continue; - this.redeclaredFieldsWarned.add(key); - const extenders = list.filter((d) => !d.owner).map((d) => named(d.packageId)); - const declaredBy = list[0]!.owner - ? `the owner ${named(list[0]!.packageId)} and by extensions from ${extenders.join(', ')}` - : `extensions from ${extenders.join(', ')}`; - console.warn( - `[Registry] Field "${field}" of object "${object}" is declared in \`fields\` by ${declaredBy}; ` + - `the definition served is the one from ${named(list[list.length - 1]!.packageId)}, the last the fold applies ` + - '(ascending `priority`; at equal priority, the one registered last). Rename the field in one extension, ' + - 'or patch an owner field\'s display properties instead of redeclaring it.', - ); - } + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.reportRedeclaredFields.call(objectRegistrationHost.of(this), object, contributors, ownFields, folded); } - /** - * [ADR-0146 D10, second rule] Name, once, each patch the fold did not apply - * because the judge missed it: a field that is not one of the owner's own - * (absent, or added only by an extension), an option value the owner's field - * does not declare (one an extension adds by redeclaring the field through - * `fields` included, #28809), or an option of a picklist-bound field. The - * verdict (`misses`) is the install door's and `os validate`'s own judge - * (`fieldPatchTargetMisses`), asked by the fold of the owner's declaration - * ({@link ownFieldsTarget}) as those doors ask it, so the boot names exactly - * what they refuse — plus, asked again of the merged fields, an admitted - * option an earlier layer left out, which had nothing to relabel. What the - * fold names is what it skipped. With no owner registered there is nothing - * to judge against, and nothing is named. - * - * ⛔ Never a refusal, and worded as the state it observes: the fold runs on - * every read while a boot is still registering packages, and an owner that - * registers again declaring the field (an upgrade) makes the patch apply on - * the next read, with nothing recorded to undo (AGENTS.md *Startup registry - * reads*). At `warn`: the object is visibly unpatched, and nothing durable - * is lost. - */ private reportFieldPatchMisses( misses: readonly FieldPatchTargetMiss[], packageId: string | undefined, ): void { - for (const miss of misses) { - const object = miss.object; - const key = `${object}|${packageId ?? ''}|${miss.field}|${miss.option ?? ''}`; - if (this.fieldPatchMissesWarned.has(key)) continue; - this.fieldPatchMissesWarned.add(key); - console.warn( - `[Registry] An extension of object "${object}"${packageId ? ` from package "${packageId}"` : ''} ` + - `carries a field patch that is not applied while the object has no such target: ${miss.message}`, - ); - } + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.reportFieldPatchMisses.call(objectRegistrationHost.of(this), misses, packageId); } /** @@ -6714,202 +2448,33 @@ export class SchemaRegistry { */ private shadowedExtensionQueueWarned = new Set(); - /** - * [ADR-0142 D3] An extender's body without its own `queue` field, for a base - * that plans the column; the body itself when it declares none or carries - * the platform's own definition (a served document coming back). The first - * drop per object and package is named once, with the remedy the - * registration door's refusal prescribes. - */ private withoutExtensionQueueField(extension: ServiceObject, packageId: string | undefined): ServiceObject { - const fields = extension.fields as Record | undefined; - const declared = fields?.[QUEUE_FIELD]; - if (declared === undefined || isInjectedColumnDefinition(declared, QUEUE_FIELD_DEF)) return extension; - const { [QUEUE_FIELD]: _dropped, ...rest } = fields!; - const key = `${extension.name}|${packageId ?? ''}`; - if (!this.shadowedExtensionQueueWarned.has(key)) { - this.shadowedExtensionQueueWarned.add(key); - console.warn( - `[Registry] An extension of object "${extension.name}"${packageId ? ` from package "${packageId}"` : ''} ` + - 'declares a field named `queue`, which the platform reserves on an object whose records have a user ' + - 'owner: it holds the ownership queue a record without an owner sits in. The extension\'s `queue` is ' + - 'ignored and the platform\'s column stands. Rename the business field (for example `support_queue`).', - ); - } - return { ...extension, fields: rest } as ServiceObject; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.withoutExtensionQueueField.call(objectRegistrationHost.of(this), extension, packageId); } - /** - * The tail every object read ends in — {@link resolveObject} (through - * {@link foldExtendersOntoDefinition}), the code layer, and - * {@link foldObjectExtendersOnto}, which the protocol's stored-row and - * MetadataService layers run. A step added here reaches every one of them. - * - * 1. picklist-bound fields get their list's options resolved onto them; - * 2. [#24400] the bound top-level actions are attached — the code - * packages' and, [#28356], the runtime-authored ones - * ({@link attachBoundActions}). The CODE layer - * ({@link resolveOwnerLayer}, `layer: 'code'`) attaches the code - * packages' alone: a tenant's action is not something a code package - * ships, and `getArtifactItem('object')` answers exactly that question. - */ private finishServedObject( body: T, layer: ServedObjectLayer = 'served', referrerOf: PicklistReferrer = NO_PACKAGE_REFERRER, ): T { - return this.attachBoundActions(this.resolvePicklistFields(body, referrerOf), layer); + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return (ObjectRegistrationFacet.finishServedObject).call(objectRegistrationHost.of(this), body, layer, referrerOf); } - /** - * [#24400] Attach to an object the top-level actions that code packages - * bind to it by `objectName` — whichever package registered them, and in - * either registration order. - * - * `defineStack()` merges a bound action into its target only when the target - * is in the SAME stack (`mergeActionsIntoObjects`). An action bound to an - * object another package owns — the route `ObjectExtensionSchema`'s - * `guidance.actions` names, since an extension cannot carry `actions` — stays - * at the stack's top level and registers here as a standalone `action` item. - * The router already dispatched it (`resolveRouteActionDeclaration`, rung 2), - * but no served object listed it, so no client rendered it. Attaching it - * HERE, at read time, is what makes it order-independent: the object and - * the action may register in either order, and the cached fold is dropped - * when an action registers ({@link registerItem}) or leaves. - * - * What attaches: a standalone `action` item this registry serves - * ({@link boundServedActions}) — one a code package shipped, or [#28356] one - * a runtime author saved through the metadata door, which the save's - * write-through and the boot's `sys_metadata` hydration register here — - * whose owner key ({@link standaloneActionOwnerKey}, the router's own test) - * is this object's name, and whose package is not disabled. Attached BY - * REFERENCE, so the served entry is the very declaration rung 2 resolves: - * its `requiredPermissions`, `requiresMembershipReach` and execution - * identity travel with it unchanged, and nothing about the object's own - * write policy moves. - * - * Deduplicated by NAME, which is also what makes the step idempotent over a - * body that already carries the action — the same-stack merge's copy, a - * MetadataService copy bridged from {@link getAllObjects}, or a stored row - * written back from a served read. An action the object already declares - * keeps the object's declaration, the precedence the router applies (rung 1 - * before rung 2); when the two declarations differ, the ObjectQL plugin - * reports the pair at boot ({@link FIND_BOUND_ACTION_COLLISIONS}). A target no - * package registers is refused at boot, not here — see - * {@link findUnboundActionTargets}. - * - * The object's own `actions` keep their order exactly — re-sorting them by - * `order` here would reorder an object no package bound anything to the - * moment one did. The attached actions follow, stable-sorted among - * themselves by `order`. - * - * Returns `body` by reference when nothing attaches. - */ private attachBoundActions(body: T, layer: ServedObjectLayer = 'served'): T { - if (body === null || typeof body !== 'object') return body; - const objectName = (body as { name?: unknown }).name; - if (typeof objectName !== 'string' || objectName === '') return body; - const declared = (body as { actions?: unknown }).actions; - const base: any[] = Array.isArray(declared) ? declared : []; - // [#28877] On the SERVED layer the object's own actions that launch into a - // disabled package are withheld too ({@link isActionWithheldByDisabledPackage}), - // as an attached one is: the same-stack merge puts a bound action here, not - // in the collection below. The code layer is the artifact as shipped and - // keeps them. A withheld name still blocks an attached twin of that name, - // so nothing stands in for it. - const kept = layer === 'served' ? base.filter((action) => !this.isActionWithheldByDisabledPackage(action)) : base; - const present = new Set(base.map((action) => action?.name)); - const fresh: any[] = []; - const collection = this.metadata.get('action'); - if (collection && collection.size > 0) { - for (const action of layer === 'code' ? this.boundCodeActions() : this.boundServedActions()) { - if (standaloneActionOwnerKey(action) !== objectName || present.has(action.name)) continue; - present.add(action.name); - fresh.push(action); - } - } - if (fresh.length === 0 && kept.length === base.length) return body; - // The object's own actions keep their served order, untouched; the - // attached ones follow, ordered among themselves by `order`. - return { ...body, actions: [...kept, ...sortActionsByOrder(fresh)] }; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return (ObjectRegistrationFacet.attachBoundActions).call(objectRegistrationHost.of(this), body, layer); } - /** - * [#28356] The standalone `action` items bound to an object that this - * registry SERVES — the population {@link attachBoundActions} attaches. - * - * Every bound item in the collection, whoever authored it: a code package's - * (under its `:` key) and a runtime author's, which the - * save door's write-through and the boot's `sys_metadata` hydration register - * (under the bare name, or a bound package's key with `_provenance: 'org'`). - * Leaving the runtime rows out made a bound action saved through the - * metadata door dispatchable (rung 2 reads {@link getItem}) yet listed by no - * served object, so no client drew it. - * - * One item per name where {@link getItem} would pick one: a bare-key entry - * (an ADR-0005 overlay) shadows the composite entries of its name, so a - * customised bound action is attached as customised — the declaration rung 2 - * resolves — and never as the packaged default under it. - * - * The registry holds no org-scoped row (the protocol's hydration declines - * one), so nothing here crosses an organization. A disabled package's items - * are left out, as the rest of its items are withheld. - * - * ⛔ Not the boot audits' population: a runtime row naming an object no - * package registers must not fail a boot, so {@link findUnboundActionTargets} - * and {@link FIND_BOUND_ACTION_COLLISIONS} keep reading - * {@link boundCodeActions}. - */ private boundServedActions(): any[] { - const collection = this.metadata.get('action'); - if (!collection) return []; - // [#33127, ADR-0150 D4] A stored row in a package overlay slot shadows the - // composite entry of the package it customizes (its envelope's), as the - // bare slot shadows every composite of its name; of two rows customizing - // one package, the one its read serves ({@link rankedForPackage}). - const overlaysOf = new Map(); - for (const [key, item] of collection) { - if (parseOverlaySlotKey(key) === undefined) continue; - const holder = storedRowPackageOf({ key: '', item }) ?? parseOverlaySlotKey(key)!.packageId; - const group = `${holder}:${String((item as any)?.name)}`; - overlaysOf.set(group, [...(overlaysOf.get(group) ?? []), { key, item }]); - } - const servedOverlays = new Set(); - for (const [group, entries] of overlaysOf) { - servedOverlays.add(this.rankedForPackage(entries, group.slice(0, group.lastIndexOf(':')))[0].key); - } - const out: any[] = []; - for (const [key, action] of collection) { - if (!action || typeof action !== 'object' || typeof (action as any).name !== 'string') continue; - const name: string = (action as any).name; - // [ADR-0150 D4] A package-less action of another owner key than the - // bare slot's is its own item, never shadowed by it. - if (key !== name && !isPackagelessActionSlotKey(key) && collection.has(name)) continue; - if (parseOverlaySlotKey(key) !== undefined ? !servedOverlays.has(key) : overlaysOf.has(key)) continue; - if (isObjectLessActionKey(standaloneActionOwnerKey(action))) continue; - // [#28877] …and so is an action that launches into a disabled package. - if (this.isActionWithheldByDisabledPackage(action)) continue; - out.push(action); - } - return out; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.boundServedActions.call(objectRegistrationHost.of(this)); } - /** - * [#24400] The standalone `action` items code packages ship that are bound - * to an object — the population {@link findUnboundActionTargets} and - * {@link FIND_BOUND_ACTION_COLLISIONS} audit. A disabled package's actions - * are left out, as the rest of its items are withheld. - */ private boundCodeActions(): any[] { - const out: any[] = []; - for (const action of this.metadata.get('action')?.values() ?? []) { - if (!action || typeof action !== 'object' || typeof (action as any).name !== 'string') continue; - if (!isCodeArtifactBody(action)) continue; - if (isObjectLessActionKey(standaloneActionOwnerKey(action))) continue; - if (this.isPackageDisabled((action as any)._packageId)) continue; - out.push(action); - } - return out; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.boundCodeActions.call(objectRegistrationHost.of(this)); } /** @@ -6923,13 +2488,8 @@ export class SchemaRegistry { * later artifact as it arrives. */ findUnboundActionTargets(): UnboundActionTarget[] { - const out: UnboundActionTarget[] = []; - for (const action of this.boundCodeActions()) { - const objectName = standaloneActionOwnerKey(action); - if (this.resolveObjectKey(objectName) !== undefined) continue; - out.push({ action: action.name, objectName, packageId: action._packageId }); - } - return out; + // [#33949 codemod] The body moved to `registry-object-registration.ts` (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.findUnboundActionTargets.call(objectRegistrationHost.of(this)); } /** @@ -6973,212 +2533,42 @@ export class SchemaRegistry { return out; } - /** - * [ADR-0029 D9.2a] Which of the three fold scalars the BASE layer carries a - * TENANT-AUTHORED value for — i.e. one that no longer equals the packaged - * owner's. - * - * Maintainer ruling, 2026-08-13 (option A, "tenant wins"): an extender's - * scalar applies only while the fold's base still carries the packaged - * owner's value; a diverged base has been authored by the tenant and the - * extender yields. This **amends ADR-0029 D9.2**, whose fold was - * unconditionally last-writer-wins on the scalars, and it is deliberately the - * SAME comparison-based mechanism the #8284 fix applies one layer up — the - * predicate is literally {@link scalarOverridesPackagedBase}, imported from - * `@objectstack/spec`, not a second copy of the shape. One sentence at both - * layers: *an explicit override beats a packaged default.* - * - * ⛔ No provenance flag is stamped on the document and no migration is - * implied: the question is answered from two values, at fold time. - * - * The accepted cost is the point, not a regression — a package can no longer - * relabel an object a tenant has deliberately renamed. There is no escape - * hatch for it, by ruling. - * - * Conservative in the same three ways the predicate is (absent base, non-string - * or empty value, exact equality), plus two of this layer's own: - * - * - **no `own` contributor → no opinion.** An orphan overlay (D9.5) keeps - * today's fold exactly. - * - **base IS the packaged owner → no opinion, by reference.** That is - * {@link resolveObject} with no overlay registered and every - * {@link resolveOwnerLayer} call (D9.6), so the common shape pays one - * identity comparison and cannot change answer. It is an OPTIMISATION, not - * a correctness edge: comparing that base against itself answers "not - * diverged" for every key anyway. ⛔ So it must never be relaxed into - * something weaker than identity — a value-equality test here would start - * answering for bodies that merely LOOK like the owner. - * - * ⛔ The owner is read as the `own` CONTRIBUTOR, deliberately NOT through - * {@link getPackagedObjectOwner} — whose extra `isCodeArtifactBody` test - * (D9.8) would make this decline to protect a RUNTIME-authored object, i.e. - * exactly the object whose owner row the tenant wrote by hand. The two agree - * wherever a packaged owner exists, which is every shape the ADR-0029 D9.2a amendment records; they - * differ only on a tenant-authored owner, and there the ruled sentence still - * reads the same way — the tenant's own row is the explicit override and a - * package's `objectExtensions` entry is the packaged default. The rejected - * alternative is the trap commit 427344c26 named one layer up, in its own form: - * comparing against a body that already has extenders folded onto it - * ({@link resolveOwnerLayer}) would report every extender's scalar as - * "unchanged" and yield nothing, ever. - * - * IDEMPOTENCE is preserved, which matters because - * {@link foldObjectExtendersOnto} is documented as idempotent (#8027) and two - * shipped call sites really do hand it an already-folded base. A base that - * already carries an extender's scalar reads as "diverged", so every extender - * yields and the value stays exactly what the first fold produced — the same - * answer, reached by yielding instead of by re-applying. - */ private tenantAuthoredScalars( contributors: ObjectContributor[], baseDefinition: ServiceObject, ): ReadonlySet | undefined { - const owner = contributors.find((c) => c.ownership === 'own')?.definition; - if (!owner || owner === baseDefinition) return undefined; - let authored: Set | undefined; - for (const key of OBJECT_FOLD_SCALAR_KEYS) { - if (scalarOverridesPackagedBase(owner, key, baseDefinition[key])) { - (authored ??= new Set()).add(key); - } - } - return authored; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it. + return ObjectRegistrationFacet.tenantAuthoredScalars(contributors, baseDefinition); } - /** - * [ADR-0146 D10, second rule; ADR-0029 D9.2a] The display keys a fold's base - * has authored away from the packaged owner's value, on the fields and - * options the `extend` contributors patch — each a {@link fieldPatchYieldKey}. - * A patch yields on those keys, as an extender's scalar yields on - * {@link tenantAuthoredScalars}, and by the same comparison - * ({@link displayKeyOverridesPackagedBase}). - * - * The same two "no opinion" edges as {@link tenantAuthoredScalars}, for its - * reasons: no `own` contributor, or a base that IS the owner's body (by - * reference). Idempotent over a folded base for its reason too: a base that - * already carries a patch's text reads as authored, so the patch yields and - * the value stays what the first fold produced. - */ private tenantPatchedDisplayKeys( contributors: ObjectContributor[], baseDefinition: ServiceObject, ): ReadonlySet | undefined { - const owner = contributors.find((c) => c.ownership === 'own')?.definition; - if (!owner || owner === baseDefinition) return undefined; - let authored: Set | undefined; - for (const contrib of contributors) { - if (contrib.ownership !== 'extend') continue; - const patches = (contrib.definition as ObjectExtensionBody).fieldPatches; - if (!patches) continue; - for (const [name, patch] of Object.entries(patches)) { - const packagedField = recordFieldOf(owner, name); - const baseField = recordFieldOf(baseDefinition, name); - if (!patch || !packagedField || !baseField) continue; - for (const key of BASE_FIELD_DISPLAY_WHITELIST.field) { - if (displayKeyOverridesPackagedBase(packagedField, key, baseField[key])) { - (authored ??= new Set()).add(fieldPatchYieldKey(name, key)); - } - } - for (const value of Object.keys(patch.options ?? {})) { - const packagedOption = optionByValue(packagedField, value); - const baseOption = optionByValue(baseField, value); - if (!packagedOption || !baseOption) continue; - for (const key of BASE_FIELD_DISPLAY_WHITELIST.option) { - if (displayKeyOverridesPackagedBase(packagedOption, key, baseOption[key])) { - (authored ??= new Set()).add(fieldPatchYieldKey(name, key, value)); - } - } - } - } - } - return authored; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it. + return ObjectRegistrationFacet.tenantPatchedDisplayKeys(contributors, baseDefinition); } - /** - * [ADR-0029 D9.2a, 2026-10-30 note] The `message` / `active` a TENANT base - * sets on its rules, by name — `undefined` when the fold's base is a code - * layer, where a later layer's same-named rule replaces the earlier one - * whole, as a field does. - * - * The base is the tenant's when it is not the `own` contributor's body (the - * overlay layer, a stored row, a served copy — the identity test - * {@link tenantAuthoredScalars} makes), or when the owner itself is - * tenant-authored (a runtime-authored object). Idempotent over a folded body: - * its copies already carry what the first fold produced. - */ private tenantRuleSettings( contributors: ObjectContributor[], baseDefinition: ServiceObject, ): ReadonlyMap | undefined { - const owner = contributors.find((c) => c.ownership === 'own')?.definition; - if (!owner) return undefined; - if (owner === baseDefinition && !isTenantAuthored(baseDefinition)) return undefined; - return tenantRuleSettingsOf(baseDefinition.validations); + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it. + return ObjectRegistrationFacet.tenantRuleSettings(contributors, baseDefinition); } - /** - * [ADR-0029 D9.2a, 2026-10-30 note] A tenant base whose rules include copies - * of the packaged OWNER's rules, with each copy replaced by the owner's rule - * carrying the copy's `message` and `active` — so a package fix to the rule - * reaches the tenant, and the copy's own condition never runs. - * - * Only names the base carries: a rule the overlay leaves out stays out, as - * D9.2's replace has always meant. A second copy of one name (a row stored - * while the fold still appended) is dropped. The base is returned as a fresh - * spread either way, as the fold has always started from one. - */ private withPackagedOwnerRules( contributors: ObjectContributor[], baseDefinition: ServiceObject, tenantRules: ReadonlyMap, ): ServiceObject { - const merged = { ...baseDefinition }; - const owner = contributors.find((c) => c.ownership === 'own')?.definition; - if (!owner || owner === baseDefinition || !isCodeArtifactBody(owner)) return merged; - const ownerRules = new Map(); - for (const rule of owner.validations ?? []) { - const name = validationRuleName(rule); - if (name !== undefined && !ownerRules.has(name)) ownerRules.set(name, rule); - } - const baseRules = baseDefinition.validations; - if (ownerRules.size === 0 || !Array.isArray(baseRules) || baseRules.length === 0) return merged; - const seen = new Set(); - const rules: unknown[] = []; - for (const rule of baseRules) { - const name = validationRuleName(rule); - const packaged = name === undefined ? undefined : ownerRules.get(name); - if (packaged === undefined) { - rules.push(rule); - } else if (!seen.has(name!)) { - seen.add(name!); - rules.push(withTenantRuleSettings(packaged, tenantRules.get(name!))); - } - } - merged.validations = rules as ServiceObject['validations']; - return merged; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it. + return ObjectRegistrationFacet.withPackagedOwnerRules(contributors, baseDefinition, tenantRules); } - /** - * [ADR-0029 D9.2a, 2026-10-30 note] Every rule a CODE layer of this object - * ships, by name, in fold order — the packaged owner's (when the owner is a - * code artifact), then each code `extend` contributor's, a later layer's - * rule replacing an earlier one of its name. What a tenant's copy of a rule - * is compared with ({@link tenantRuleCopyRefusal}, - * {@link reportChangedRuleCopies}) and what {@link getPackagedObjectBase} - * serves as the packaged rule set. - */ private packagedRules(contributors: readonly ObjectContributor[]): unknown[] | undefined { - let rules: unknown[] | undefined; - const owner = contributors.find((c) => c.ownership === 'own'); - if (owner && isCodeArtifactBody(owner.definition) && Array.isArray(owner.definition.validations)) { - rules = [...owner.definition.validations]; - } - for (const contrib of contributors) { - if (contrib.ownership !== 'extend' || !isCodeArtifactBody(contrib.definition)) continue; - const list = contrib.definition.validations; - if (!Array.isArray(list) || list.length === 0) continue; - rules = mergeValidationsByName(rules, list, undefined); - } - return rules; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it. + return ObjectRegistrationFacet.packagedRules(contributors); } /** @@ -7193,67 +2583,16 @@ export class SchemaRegistry { * fold enforces. The first changed rule is named, with every key it changes. */ tenantRuleCopyRefusal(name: string, body: unknown): Error | null { - const fqn = this.resolveObjectKey(name); - const contributors = fqn === undefined ? undefined : this.objectContributors.get(fqn); - if (!contributors) return null; - const packaged = new Map(); - for (const rule of this.packagedRules(contributors) ?? []) { - const ruleName = validationRuleName(rule); - if (ruleName !== undefined) packaged.set(ruleName, rule); - } - if (packaged.size === 0) return null; - const rules = (body as { validations?: unknown } | null | undefined)?.validations; - if (!Array.isArray(rules)) return null; - for (const rule of rules) { - const ruleName = validationRuleName(rule); - if (ruleName === undefined || !packaged.has(ruleName)) continue; - const changed = changedPackagedRuleKeys(rule, packaged.get(ruleName)); - if (changed.length > 0) return new ValidationRuleCopyRefusedError(fqn!, ruleName, changed); - } - return null; + // [#33949 codemod] The body moved to `registry-object-registration.ts` (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.tenantRuleCopyRefusal.call(objectRegistrationHost.of(this), name, body); } /** `object|rule|copy|packaged` already reported — once per registry, as {@link shadowedExtensionQueueWarned} is. */ private reportedRuleCopyChanges = new Set(); - /** - * [ADR-0029 D9.2a, 2026-10-30 note] Say, once, that a tenant base carries a - * copy of a packaged rule that changes what the package owns. The fold has - * already enforced the package's rule; the stored copy's condition is not - * what runs, while the object serves normally, so this is an `error` with - * its consequence and its fix. Reported, never refused, here: a stored copy - * also differs after a package upgrade changes the rule, and refusing it - * would drop the tenant's whole layer at boot. - */ private reportChangedRuleCopies(contributors: ObjectContributor[], baseDefinition: ServiceObject): void { - const rules = baseDefinition.validations; - if (!Array.isArray(rules) || rules.length === 0) return; - const packaged = new Map(); - for (const rule of this.packagedRules(contributors) ?? []) { - const ruleName = validationRuleName(rule); - if (ruleName !== undefined) packaged.set(ruleName, rule); - } - if (packaged.size === 0) return; - const reported = new Set(); - for (const rule of rules) { - const ruleName = validationRuleName(rule); - if (ruleName === undefined || reported.has(ruleName) || !packaged.has(ruleName)) continue; - reported.add(ruleName); - const changed = changedPackagedRuleKeys(rule, packaged.get(ruleName)); - if (changed.length === 0) continue; - const key = `${baseDefinition.name}|${ruleName}|${stableStringify(rule)}|${stableStringify(packaged.get(ruleName))}`; - if (this.reportedRuleCopyChanges.has(key)) continue; - this.reportedRuleCopyChanges.add(key); - const keys = changed.map((k) => `\`${k}\``).join(', '); - console.error( - `[Registry] [validation_rule_copy_changed] Object "${baseDefinition.name}": the tenant's copy of ` + - `validation rule "${ruleName}" changes ${keys} from the packaged rule. Only a packaged rule's ` + - '`message` and `active` follow the tenant, so the package\'s rule is enforced with the tenant\'s ' + - `message and switch, and the copy's ${keys} does not run, while the object serves normally. ` + - 'To change the condition, add a rule under a new name, or change it in the package. To keep the ' + - 'packaged rule, save the object again from its served definition.', - ); - } + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.reportChangedRuleCopies.call(objectRegistrationHost.of(this), contributors, baseDefinition); } /** @@ -7314,101 +2653,16 @@ export class SchemaRegistry { * on an unfolded base. */ foldObjectExtendersOnto(name: string, base: T, options?: ObjectExtensionFoldOptions): T { - if (base === null || typeof base !== 'object') return base; - const fqn = this.resolveObjectKey(name); - // With nothing to fold, the body still gets its picklist-bound fields - // resolved — the same step the fold below ends with — so a body this - // registry never saw is served in the one shape every other read uses. - // By reference when no field names a picklist. - if (fqn === undefined) return this.finishServedObject(base); - // [#29903, #29872] The contributions `options` names are left out, as on - // {@link getAllObjects} ({@link ObjectExtensionFoldOptions}). - const registered = this.objectContributors.get(fqn); - const listed = leavesExtensionsOut(options) ? registered?.filter((c) => !isLeftOutExtension(c, options)) : registered; - // A disabled package's `extend` contributions are withheld, and taken off - // a base that already carries them, as on {@link resolveObject}. - const withheld = listed ? this.withholdDisabledExtensions(listed) : undefined; - const contributors = withheld?.kept ?? listed; - const served = withheld ? withoutWithheldMembers(base as unknown as ServiceObject, withheld) : base as unknown as ServiceObject; - if (!contributors || !contributors.some((c) => c.ownership === 'extend')) { - return this.finishServedObject(served as unknown as T, 'served', picklistReferrerOf(contributors)); - } - return this.foldExtendersOntoDefinition( - contributors, - this.subtractExtenderContributions(contributors, served), - ) as unknown as T; + // [#33949 codemod] The body moved to `registry-object-registration.ts` (see `ObjectRegistrationHost`). + return (ObjectRegistrationFacet.foldObjectExtendersOnto).call(objectRegistrationHost.of(this), name, base, options); } - /** - * [#8268] The ADR-0079 object-materialization block itself, as ONE function. - * - * Every stamp this registry puts on a base layer at registration time lives - * HERE, and {@link registerObject} calls it rather than spelling the sequence - * out — so {@link materializeServedObjectOnto}, which replays it onto a body - * that never came through `registerObject`, is the SAME code and not a second - * copy free to drift from it. - * - * That property is the whole point, and it is the lesson three cards taught - * one at a time. Each of #6562 (injected system columns), #8038 (`__search`) - * and #8268 (`nameField`) is the same shape: a stamp the registry applies at - * this seam, absent from the copy a `/meta` read serves, found and filed - * SEPARATELY because nothing tied the stamps together. A fourth stamp added - * to this method is converged at the read exits the day it is added, with no - * fourth card and no fourth patch — which is what "fix the class" means here. - * - * ⛔ Order is contractual, not incidental: `provisionPrimary` runs BEFORE - * `provisionSearchCompanion` because the companion's source is resolved over - * the post-designation document. Keep additions in the order the seam applies - * them. - * - * @param opts.designatePrimary - `false` withholds ONLY the ADR-0079 - * designation step. See {@link materializeServedObjectOnto} for the one - * caller that passes it and why the choice is not this method's to make. - * @param opts.provisionCompanion - `false` withholds ONLY the `__search` - * step, for the same reason and from the same caller (#8376). - */ private materializeBaseLayer( schema: ServiceObject, opts?: { designatePrimary?: boolean; provisionCompanion?: boolean }, ): ServiceObject { - let out = schema; - - // [ADR-0131 D7] The deployment's platform-global declaration, recorded on - // the base layer. FIRST, because every later stamp asks its question of the - // object as this deployment provisions it — the tenant index below must - // see a declared object as carrying no tenant column. See - // {@link applyDeploymentTenancy}; its write-side inverse is the LAST strip - // in {@link stripMaterializedStampsFrom}. - out = this.applyDeploymentTenancy(out); - - // [ADR-0079] DESIGNATE-ONLY primary-title provisioning — `synthesize: - // false` never adds a column, so this is safe against title-less - // system/junction tables (see the call in `registerObject` for the full - // reasoning on why synthesis is refused at this seam). - if (opts?.designatePrimary !== false) { - out = provisionPrimary(out, { synthesize: false }); - } - - // [#2486] Search-normalization companion column (`__search`). Runs AFTER - // `provisionPrimary` so the just-designated `nameField` is visible, and - // ONLY when the deployment enables pinyin search — the column is a real - // additive migration (ADR-0045) that the driver's `syncSchema` - // materializes, populated by plugin-pinyin-search's before-save hooks and - // OR-ed into `$search` by the engine's `expandSearchToFilter`. - if (this.searchCompanion && opts?.provisionCompanion !== false) { - out = provisionSearchCompanion(out); - } - - // [#8375] The tenant-scope index — the fourth stamp of this seam, and the - // first one added AFTER #8268 generalised it. It needed no new method here, - // no new method in the protocol and no new convergence at the read exits: - // being a step of this block IS the convergence, which is the property - // #8268 exists to provide. Runs LAST because it is the only stamp gated on - // a DEPLOYMENT flag rather than on the document, so nothing it appends can - // change the answer the two steps above reached over the fields map. - out = provisionTenantScopeIndex(out, { multiTenant: this.multiTenant }); - - return out; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.materializeBaseLayer.call(objectRegistrationHost.of(this), schema, opts); } /** @@ -7467,43 +2721,13 @@ export class SchemaRegistry { * caller may apply it unconditionally. */ materializeServedObjectOnto(base: T): T { - if (base === null || typeof base !== 'object') return base; - const declined = this.declinedBaseLayerStamps(base); - return this.materializeBaseLayer(base as never, { - designatePrimary: !declined.primary, - provisionCompanion: !declined.companion, - }) as unknown as T; + // [#33949 codemod] The body moved to `registry-object-registration.ts` (see `ObjectRegistrationHost`). + return (ObjectRegistrationFacet.materializeServedObjectOnto).call(objectRegistrationHost.of(this), base); } - /** - * Which document-sensitive stamps did this registry resolve the object `base` - * names and then DECLINE to apply to its base layer? - * See {@link materializeServedObjectOnto} for why the question is asked at all. - * - * All-`false` — "no opinion" — for a body with no usable name and for a name - * this registry does not know, which is the honest answer in both cases and is - * also the safe one: an object that never reached `registerObject` has no - * registry verdict to converge onto, and nothing could have folded such a body - * either (the fold reads this same contributor list), so the body IS its own - * base layer and the pure transform is exactly right for it. - */ private declinedBaseLayerStamps(base: unknown): { primary: boolean; companion: boolean } { - const noOpinion = { primary: false, companion: false }; - const name = (base as { name?: unknown }).name; - if (typeof name !== 'string' || name === '') return noOpinion; - const resolved = this.getObject(name) as - | { nameField?: unknown; fields?: unknown } - | undefined; - if (!resolved) return noOpinion; - const fields = resolved.fields; - const resolvedFields = - fields && typeof fields === 'object' && !Array.isArray(fields) - ? (fields as Record) - : undefined; - return { - primary: resolved.nameField === undefined, - companion: resolvedFields?.[SEARCH_COMPANION_FIELD] === undefined, - }; + // [#33949 codemod] The body moved to `registry-object-registration.ts`, its docblock with it (see `ObjectRegistrationHost`). + return ObjectRegistrationFacet.declinedBaseLayerStamps.call(objectRegistrationHost.of(this), base); } /** @@ -7548,9 +2772,8 @@ export class SchemaRegistry { * of, and hosts pin the pair. */ provisionSearchCompanionOnto(base: T): T { - if (base === null || typeof base !== 'object') return base; - if (!this.searchCompanion) return base; - return provisionSearchCompanion(base as never) as unknown as T; + // [#33949 codemod] The body moved to `registry-object-registration.ts` (see `ObjectRegistrationHost`). + return (ObjectRegistrationFacet.provisionSearchCompanionOnto).call(objectRegistrationHost.of(this), base); } /** @@ -7583,25 +2806,8 @@ export class SchemaRegistry { * Returns `base` by reference when there is nothing to remove. Pure and total. */ stripProvisionedSearchCompanionFrom(base: T): T { - if (base === null || typeof base !== 'object') return base; - const fields = (base as { fields?: unknown }).fields; - if (!fields || typeof fields !== 'object' || Array.isArray(fields)) return base; - const present = (fields as Record)[SEARCH_COMPANION_FIELD]; - if (present === undefined) return base; - - // Re-stamp a copy WITHOUT the column and compare: the canonical definition - // comes from the provisioning function itself, and an object the seam would - // not provision for yields no stamp and therefore no removal. - const withoutCompanion = { ...(fields as Record) }; - delete withoutCompanion[SEARCH_COMPANION_FIELD]; - const restamped = provisionSearchCompanion( - { ...(base as Record), fields: withoutCompanion } as never, - ) as unknown as { fields?: Record }; - const canonical = restamped.fields?.[SEARCH_COMPANION_FIELD]; - if (canonical === undefined) return base; - if (stableStringify(present) !== stableStringify(canonical)) return base; - - return { ...(base as Record), fields: withoutCompanion } as unknown as T; + // [#33949 codemod] The body moved to `registry-object-registration.ts`. + return (ObjectRegistrationFacet.stripProvisionedSearchCompanionFrom)(base); } /** @@ -7631,12 +2837,8 @@ export class SchemaRegistry { * Returns `base` by reference when nothing was owed. */ stripMaterializedStampsFrom(base: T): T { - if (base === null || typeof base !== 'object') return base; - return this.stripDeploymentTenancyFrom( - this.stripProvisionedPrimaryFrom( - this.stripProvisionedSearchCompanionFrom(this.stripProvisionedTenantIndexFrom(base)), - ), - ); + // [#33949 codemod] The body moved to `registry-object-registration.ts` (see `ObjectRegistrationHost`). + return (ObjectRegistrationFacet.stripMaterializedStampsFrom).call(objectRegistrationHost.of(this), base); } // ========================================== @@ -7685,324 +2887,46 @@ export class SchemaRegistry { readonly replanned: readonly string[]; readonly keptAuthoredColumn: readonly string[]; } { - this.deploymentPlatformGlobalObjects = new Set(names); - const replanned = new Set(); - const keptAuthoredColumn = new Set(); - if (this.deploymentPlatformGlobalObjects.size === 0) { - return { replanned: [], keptAuthoredColumn: [] }; - } - for (const contributors of this.objectContributors.values()) { - for (const contributor of contributors) { - const before = contributor.definition; - const name = (before as { name?: unknown })?.name; - if (typeof name !== 'string') continue; - const orgField = (before.fields as Record | undefined)?.organization_id; - const authoredColumn = orgField !== undefined && !isInjectedColumnDefinition(orgField, TENANT_SCOPE_FIELD_DEF); - // Asked of the body WITHOUT its column: the plan decides whether the - // deployment withholds one, and the column's presence is not an input. - if (!this.deploymentWithholdsTenant(withoutField(before, 'organization_id'))) continue; - if (authoredColumn) { - keptAuthoredColumn.add(name); - continue; - } - // [#24314] An `extend` layer has nothing to re-plan: `registerObject` - // plans base layers only, so it carries no platform column or index. - if (contributor.ownership === 'extend') continue; - // The tenant index comes off FIRST, while the body is still in the - // state it was stamped in (the strip re-stamps to prove the entry is - // the platform's own). - const unindexed = this.stripProvisionedTenantIndexFrom(before); - const after = this.applyDeploymentTenancy(unindexed); - if (after !== before) { - contributor.definition = after; - replanned.add(name); - } - } - } - this.invalidateAll(); - return { replanned: [...replanned].sort(), keptAuthoredColumn: [...keptAuthoredColumn].sort() }; + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.setDeploymentPlatformGlobalObjects.call(platformGlobalHost.of(this), names); } - /** - * [ADR-0131 D7] Record the deployment's platform-global declaration on one - * base-layer body: a declared object carries no `organization_id` here and - * declares `systemFields.tenant: false`. - * - * Why the record and not only the missing column: `systemFields.tenant: - * false` is the vocabulary every reader of a registered object already - * answers "no organization column" from — the security layer's - * `tenancyDisabled` (so Layer 0 composes nothing and a wildcard - * `organization_id` policy is not applicable, with no second reading of the - * declaration), the tenant-index predicate above, the lifecycle's - * provenance (`absent`), and the `/meta` read exits' own injection pass, - * which re-plans a served body WITHOUT the deployment and would otherwise - * add the column back. The object is then exactly what a deployment-level - * object that declares the key itself is (ADR-0131 D7: governed by object - * permission, not by the wall), on this deployment only. - * - * Withheld — `schema` returned by reference — when the object is not - * declared, when its own declaration already plans no tenant column, and - * when it DECLARES its own `organization_id` (not the platform's - * definition, byte for byte): an authored column is the author's, and the - * object stays walled on it. The decision reads only the body, so the read - * exits ({@link materializeServedObjectOnto}) reach the same answer. - */ private applyDeploymentTenancy(schema: ServiceObject): ServiceObject { - if (!this.deploymentWithholdsTenant(schema)) return schema; - const fields = schema.fields as Record | undefined; - const orgField = fields?.organization_id; - if (orgField !== undefined && !isInjectedColumnDefinition(orgField, TENANT_SCOPE_FIELD_DEF)) return schema; - const sf = (schema as { systemFields?: unknown }).systemFields; - const out = withoutField(schema, 'organization_id') as ServiceObject & { systemFields?: unknown }; - return { - ...out, - systemFields: { - ...(sf && typeof sf === 'object' && !Array.isArray(sf) ? (sf as Record) : {}), - tenant: false, - }, - } as ServiceObject; + // [#33949 codemod] The body moved to `registry-platform-global.ts`, its docblock with it (see `PlatformGlobalHost`). + return PlatformGlobalFacet.applyDeploymentTenancy.call(platformGlobalHost.of(this), schema); } - /** - * [ADR-0131 D7] The write-side inverse of {@link applyDeploymentTenancy}: - * take the recorded `systemFields.tenant: false` back off a declared - * object's body on its way IN, so a Studio GET → edit → PUT on the declaring - * deployment persists the body the author wrote (#4326), not a deployment - * fact that would outlive the declaration in `sys_metadata`. - * - * Exact in the way its siblings are: only a declared object, only the - * `tenant: false` member, and the `systemFields` key itself only when that - * member was all it held. The trade is theirs too — an author who wrote - * `systemFields: { tenant: false }` on a declared object has it dropped on - * the first save that carries it, and on this deployment the answer is - * re-derived at every load, so nothing it provisions changes. - * - * Returns `base` by reference when nothing was owed. - */ private stripDeploymentTenancyFrom(base: T): T { - if (base === null || typeof base !== 'object') return base; - const sf = (base as { systemFields?: unknown }).systemFields; - if (!sf || typeof sf !== 'object' || Array.isArray(sf)) return base; - if ((sf as { tenant?: unknown }).tenant !== false) return base; - const { tenant: _tenant, ...restSystemFields } = sf as Record; - const out = { ...(base as Record) }; - if (Object.keys(restSystemFields).length === 0) delete out.systemFields; - else out.systemFields = restSystemFields; - // Redundant with the derivation only where the deployment's plan withholds - // the column from the body without the record — i.e. on a declared object. - return this.deploymentWithholdsTenant(out as unknown as ServiceObject) ? (out as unknown as T) : base; + // [#33949 codemod] The body moved to `registry-platform-global.ts`, its docblock with it (see `PlatformGlobalHost`). + return (PlatformGlobalFacet.stripDeploymentTenancyFrom).call(platformGlobalHost.of(this), base); } - /** - * [ADR-0131 D7] Does the deployment's declaration — and nothing else — plan - * this body with no tenant column? The ONE reading of the declaration in - * this registry: the spec's plan answers it, with and without the - * deployment input, so the registry never re-derives which objects are - * declared. - */ private deploymentWithholdsTenant(schema: ServiceObject): boolean { - if (this.deploymentPlatformGlobalObjects.size === 0) return false; - return ( - resolveInjectedSystemColumns(schema).tenant && - !resolveInjectedSystemColumns(schema, { platformGlobalObjects: this.deploymentPlatformGlobalObjects }).tenant - ); + // [#33949 codemod] The body moved to `registry-platform-global.ts`, its docblock with it (see `PlatformGlobalHost`). + return PlatformGlobalFacet.deploymentWithholdsTenant.call(platformGlobalHost.of(this), schema); } - /** - * [#8375] Remove the tenant-scope index entry {@link materializeBaseLayer} - * appended — and only that one. - * - * ## Why this strip is harder than the two beside it - * - * `indexes` is a CONCATENATING key under `mergeObjectDefinitions`, not a - * last-writer-wins scalar like `nameField` or a keyed record like `fields`. - * The two existing strips can ask "is this value the platform's?" of a single - * slot; here the platform's entry sits in a list beside the author's, entries - * may legitimately repeat, and a strip that removes "any entry that looks like - * the platform's" silently deletes an author's own declaration, while a strip - * that removes nothing bakes a phantom index into `sys_metadata.metadata`, its - * checksum and every history diff on the first Studio GET → edit → PUT - * (#4326). - * - * ## The bound, and why it is exact in BOTH directions - * - * Not "does an entry match" but "would the seam have produced EXACTLY this - * list": remove the LAST entry identical to the platform's own (the seam - * APPENDS, so its entry is the last matching one), re-stamp the remainder - * through `provisionTenantScopeIndex` — the stamping function itself, so the - * strip and the stamp cannot drift — and keep the removal only when the - * re-stamped list is byte-identical to the list that arrived. Anything else - * returns `base` untouched. - * - * That single comparison decides every case correctly, and each of these was - * measured: - * - * - the platform's appended entry on a body with no `indexes` of its own → - * removed, and the KEY is deleted rather than left as `indexes: []`, so the - * round trip is byte-identical and not merely equivalent; - * - an author's own `{ fields: ['organization_id'] }` on a SINGLE-TENANT - * deployment → the re-stamp adds nothing, the lists differ, kept; - * - an author's own tenant index declared BEFORE their other indexes → the - * re-stamp appends at the end, the ORDER differs, kept; - * - a named entry (`{ name: 'my_tenant_idx', fields: [...] }`) or a composite - * → not identical to the platform's entry, never a candidate, kept. - * - * ⚠️ The one case it cannot separate, stated plainly because it is the same - * trade {@link stripProvisionedPrimaryFrom} documents: an author who declares - * `{ fields: ['organization_id'] }` as their LAST index on a MULTI-TENANT - * deployment has written exactly what the seam appends, so it is dropped on - * the first save that carries it. What bounds the harm is identical: the entry - * is RE-DERIVED at every load — `registerObject` runs this seam over every - * base layer — so the resolved answer, and the index the driver materializes, - * is the same with or without the stored entry, now and at every future boot. - * - * Returns `base` by reference when nothing was owed. - */ private stripProvisionedTenantIndexFrom(base: T): T { - if (base === null || typeof base !== 'object') return base; - const present = (base as { indexes?: unknown }).indexes; - if (!Array.isArray(present) || present.length === 0) return base; - - const platformEntry = stableStringify(TENANT_SCOPE_INDEX); - let at = -1; - for (let i = present.length - 1; i >= 0; i--) { - if (stableStringify(present[i]) === platformEntry) { at = i; break; } - } - if (at === -1) return base; - - const kept = present.filter((_entry, i) => i !== at); - const without = { ...(base as Record) }; - // An emptied list is the ABSENCE of the key, not an empty array: the seam - // added `indexes` to a body that had none, so the inverse must take the key - // away again or the round trip stores a shape the author never wrote. - if (kept.length === 0) delete without.indexes; - else without.indexes = kept; - - const restamped = provisionTenantScopeIndex(without as never, { - multiTenant: this.multiTenant, - }) as { indexes?: unknown }; - if (stableStringify(restamped.indexes) !== stableStringify(present)) return base; - - return without as unknown as T; + // [#33949 codemod] The body moved to `registry-platform-global.ts`, its docblock with it (see `PlatformGlobalHost`). + return (PlatformGlobalFacet.stripProvisionedTenantIndexFrom).call(platformGlobalHost.of(this), base); } - /** - * [#8268] Remove an ADR-0079 title designation that is REDUNDANT WITH THE - * DERIVATION — the pointer {@link materializeServedObjectOnto} adds, and only - * that one. - * - * Same EXACTNESS discipline as {@link stripProvisionedSearchCompanionFrom}, - * and recomputed from `provisionPrimary` itself rather than transcribed, so - * the strip and the stamp cannot drift: the pointer comes off only when it is - * identical to what the seam would designate for a body carrying no pointer - * at all. A pointer naming any OTHER field is the author's own deliberate - * choice — it overrides the derivation, which is the entire reason to write - * one — and is kept untouched. - * - * ⚠️ The narrow case this trades away, stated plainly because it is real and - * it is NOT the same trade `stripProvisionedSearchCompanionFrom` makes. That - * one removes a value byte-identical to a platform-canonical FIELD - * DEFINITION, where coincidence with an author's own writing is implausible. - * This one compares a single string, so an author who explicitly declared the - * field the derivation would have picked anyway loses that declaration on a - * round trip. What bounds the harm is that the two are indistinguishable by - * construction — the read serves one body, and "the author wrote - * `nameField: 'name'`" and "the read derived `nameField: 'name'`" are the - * same bytes — and that the stripped pointer is REDERIVED on every load: - * `registerObject` runs `provisionPrimary` over every base layer, so the - * resolved answer is identical with or without the stored key, now and at - * each future boot. The designation is only ever lost if a later edit changes - * which field the derivation picks, and that edit re-serves and re-stores the - * new designation through this same pair. - */ private stripProvisionedPrimaryFrom(base: T): T { - if (base === null || typeof base !== 'object') return base; - const present = (base as { nameField?: unknown }).nameField; - if (typeof present !== 'string' || present === '') return base; - - // Re-stamp a copy WITHOUT the pointer and compare: the canonical answer - // comes from the provisioning function itself, and an object the seam would - // designate nothing for yields no stamp and therefore no removal. - const withoutPointer = { ...(base as Record) }; - delete withoutPointer.nameField; - const restamped = provisionPrimary(withoutPointer as never, { synthesize: false }) as unknown as - { nameField?: unknown }; - if (restamped.nameField !== present) return base; - - return withoutPointer as unknown as T; + // [#33949 codemod] The body moved to `registry-platform-global.ts`, its docblock with it. + return (PlatformGlobalFacet.stripProvisionedPrimaryFrom)(base); } - /** - * [#8027] Remove from `base` the `indexes` entries the `extend` contributors - * are about to contribute — the half of {@link foldObjectExtendersOnto} that - * makes the fold idempotent. - * - * Only the CONCATENATING key is considered; `fields` and the scalar props - * already re-apply cleanly. Matching is by deep value equality on the - * serialised entry, so it fires only on an entry byte-identical to the one - * the extender declares. - * - * [ADR-0029 D9.2a, 2026-10-30 note] `validations` are no longer subtracted - * here: the fold merges them BY NAME ({@link mergeValidationsByName}), which - * removes every base copy of an extender's rule name — reworded or not — - * before appending the extender's rule, so a folded base re-folds to itself - * with nothing taken off first. Subtracting first would also take the - * tenant's `message` / `active` off a reworded copy before the fold could - * read them. - * - * Returns `base` BY REFERENCE when nothing matched, which is the ordinary - * case (an unfolded base), so the pre-#8027 body is preserved exactly. - */ private subtractExtenderContributions( contributors: ObjectContributor[], base: ServiceObject, ): ServiceObject { - const key = (entry: unknown): string => stableStringify(entry); - let out: ServiceObject | undefined; - for (const listKey of ['indexes'] as const) { - const baseList = (base as Record)[listKey]; - if (!Array.isArray(baseList) || baseList.length === 0) continue; - const contributed = new Set(); - for (const contrib of contributors) { - if (contrib.ownership !== 'extend') continue; - const list = (contrib.definition as unknown as Record)[listKey]; - if (Array.isArray(list)) for (const entry of list) contributed.add(key(entry)); - } - if (contributed.size === 0) continue; - const kept = baseList.filter((entry) => !contributed.has(key(entry))); - if (kept.length === baseList.length) continue; - out ??= { ...base }; - (out as unknown as Record)[listKey] = kept; - } - return out ?? base; + // [#33949 codemod] The body moved to `registry-platform-global.ts`, its docblock with it. + return PlatformGlobalFacet.subtractExtenderContributions(contributors, base); } - /** - * [ADR-0029 D9.6] The CODE-LAYER resolution of an object: the OWNER's - * declaration with its extenders folded on, deliberately ignoring any tenant - * `overlay` layer. - * - * This is what artifact identity has to be read from, and it is NOT implied - * by D9.2 — D9.2 leaves the merged body identical on purpose, `_provenance: - * 'org'` included, so asking the MERGED body "does a code package ship - * this?" answers about whatever last overwrote the base rather than about - * the code layer. That is precisely how `isArtifactBacked` came to answer - * `false` for a name a package still ships (#6853 P3), disarming both gates - * that read it. - * - * With no overlay registered this is byte-for-byte `resolveObject`'s answer, - * which is why the honest predicate costs nothing on the common shape. - * - * Uncached on purpose: a second cache would have to stay correct at every - * one of `mergedObjectCache`'s invalidation points, and the fold is a - * shallow spread over a contributor list that is almost always length 1. - */ private resolveOwnerLayer(fqn: string): ServiceObject | undefined { - const contributors = this.objectContributors.get(fqn); - if (!contributors || contributors.length === 0) return undefined; - const ownerContrib = contributors.find(c => c.ownership === 'own'); - if (!ownerContrib) return undefined; - return this.foldExtenders(contributors, ownerContrib, 'code'); + // [#33949 codemod] The body moved to `registry-platform-global.ts`, its docblock with it (see `PlatformGlobalHost`). + return PlatformGlobalFacet.resolveOwnerLayer.call(platformGlobalHost.of(this), fqn); } /** @@ -8019,51 +2943,13 @@ export class SchemaRegistry { * 2. Legacy FQN match (e.g., 'crm__account') — backward compat. */ getObject(name: string): ServiceObject | undefined { - const fqn = this.resolveObjectKey(name); - return fqn === undefined ? undefined : this.resolveObject(fqn); + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.getObject.call(platformGlobalHost.of(this), name); } - /** - * [#6808] The name→FQN half of {@link getObject}, extracted so the READ and - * the name-addressed REMOVAL ({@link unregisterObject}) cannot disagree - * about which contributor entry a bare name addresses. - * - * That disagreement is not hypothetical — it is the shape of the bug this - * was extracted for: `deleteMetaItem`'s registry heal reached one of the two - * places an `object` lives, and the surface data CRUD dispatches on - * (`getObject`) kept serving a deleted object. A remover that resolved names - * its own way would re-open the same seam one layer down: it could remove an - * entry `getObject` never served, leaving the served one behind. - * - * Returns `undefined` when nothing is registered under the name, so - * `getObject` keeps its exact previous behaviour: `resolveObject` on an - * unknown FQN also answered `undefined`. - * - * [#10945] The short-name half is answered from {@link objectKeysByShortName} - * rather than by scanning every contributor key. That keeps the extraction - * above intact where it matters most — BOTH paths still resolve through this - * one method, so they read one index and cannot diverge — while removing the - * full-registry walk that made kernel boot quadratic in stored metadata. - */ private resolveObjectKey(name: string): string | undefined { - // Canonical: short name lookup. O(1) via {@link objectKeysByShortName}, - // whose array IS the list the pre-#10945 full-registry scan built — same - // members, same order — so `matches[0]` still names the first key - // registered under an ambiguous short name and the warning still names - // every one of them. - const matches = this.objectKeysByShortName.get(name); - if (matches !== undefined && matches.length > 0) { - if (matches.length > 1) { - console.warn( - `[SchemaRegistry] Ambiguous short name "${name}" matches: ${matches.join(', ')}. ` + - `Returning first match. Use FQN to disambiguate.` - ); - } - return matches[0]; - } - - // Fallback: explicit FQN - return this.objectContributors.has(name) ? name : undefined; + // [#33949 codemod] The body moved to `registry-platform-global.ts`, its docblock with it (see `PlatformGlobalHost`). + return PlatformGlobalFacet.resolveObjectKey.call(platformGlobalHost.of(this), name); } /** @@ -8078,32 +2964,16 @@ export class SchemaRegistry { * naming the listed package. */ getAllObjects(packageId?: string, options?: ObjectExtensionFoldOptions): ServiceObject[] { - const results: ServiceObject[] = []; - - for (const fqn of this.objectContributors.keys()) { - // If filtering by package, check if this package contributes - if (packageId) { - const contributors = this.objectContributors.get(fqn); - const hasContribution = contributors?.some(c => c.packageId === packageId); - if (!hasContribution) continue; - } - - const merged = this.resolveObjectLeavingExtensionsOut(fqn, options); - if (merged) { - // Tag with contributor info for UI - (merged as any)._packageId = this.getObjectOwner(fqn)?.packageId; - results.push(merged); - } - } - - return results; + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.getAllObjects.call(platformGlobalHost.of(this), packageId, options); } /** * Get all contributors for an object. */ getObjectContributors(fqn: string): ObjectContributor[] { - return this.objectContributors.get(fqn) || []; + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.getObjectContributors.call(platformGlobalHost.of(this), fqn); } /** @@ -8129,22 +2999,8 @@ export class SchemaRegistry { * ({@link getAllObjects}'s `onlyExtensionsOf`). */ getObjectExtensionsOf(packageId: string): Array<{ extend: string; priority?: number } & Record> { - const out: Array<{ extend: string; priority?: number } & Record> = []; - for (const [fqn, contributors] of this.objectContributors) { - const contribution = contributors.find((c) => c.ownership === 'extend' && c.packageId === packageId); - if (!contribution) continue; - const body: Record = {}; - for (const [key, value] of Object.entries(contribution.definition)) { - if (value === undefined || key === 'name' || key.startsWith('_')) continue; - body[key] = value; - } - out.push({ - extend: fqn, - ...body, - ...(contribution.priority !== DEFAULT_EXTENDER_PRIORITY ? { priority: contribution.priority } : {}), - }); - } - return out; + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.getObjectExtensionsOf.call(platformGlobalHost.of(this), packageId); } /** @@ -8154,8 +3010,8 @@ export class SchemaRegistry { * table". An overlay is not an owner, so it is never returned here. */ getObjectOwner(fqn: string): ObjectContributor | undefined { - const contributors = this.objectContributors.get(fqn); - return contributors?.find(c => c.ownership === 'own'); + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.getObjectOwner.call(platformGlobalHost.of(this), fqn); } /** @@ -8177,10 +3033,8 @@ export class SchemaRegistry { * the decision and the registration cannot talk about different entries. */ getPackagedObjectOwner(name: string): ObjectContributor | undefined { - const fqn = this.resolveObjectKey(name); - if (fqn === undefined) return undefined; - const owner = this.objectContributors.get(fqn)?.find(c => c.ownership === 'own'); - return owner && isCodeArtifactBody(owner.definition) ? owner : undefined; + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.getPackagedObjectOwner.call(platformGlobalHost.of(this), name); } /** @@ -8206,18 +3060,8 @@ export class SchemaRegistry { * rule, and `undefined` exactly where {@link getPackagedObjectOwner} does. */ getPackagedObjectBase(name: string): ServiceObject | undefined { - const owner = this.getPackagedObjectOwner(name); - if (!owner) return undefined; - const fqn = this.resolveObjectKey(name); - const contributors = (fqn === undefined ? undefined : this.objectContributors.get(fqn)) ?? []; - // [ADR-0029 D9.2a, 2026-10-30 note] By name, as the fold merges them: an - // extender's rule of a name the owner already ships is one rule, not two. - const extended = contributors.some((c) => - c.ownership === 'extend' && isCodeArtifactBody(c.definition) - && Array.isArray(c.definition.validations) && c.definition.validations.length > 0); - if (!extended) return owner.definition; - const rules = this.packagedRules(contributors) ?? []; - return { ...owner.definition, validations: rules as ServiceObject['validations'] }; + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.getPackagedObjectBase.call(platformGlobalHost.of(this), name); } /** @@ -8229,7 +3073,8 @@ export class SchemaRegistry { schema: { name?: unknown; defaultRowScope?: unknown; membershipKeys?: unknown }, packageId: string | undefined, ): Error | undefined { - return extendContributionRefusal(schema, packageId); + // [#33949 codemod] The body moved to `registry-platform-global.ts`. + return PlatformGlobalFacet.extendContributionRefusal(schema, packageId); } /** @@ -8255,14 +3100,8 @@ export class SchemaRegistry { extension: { extend: string; priority?: number } & Record, packageId: string, ): string { - const { extend, priority, ...contribution } = extension; - return this.registerObject( - { ...contribution, name: extend } as unknown as ServiceObject, - packageId, - undefined, - 'extend', - typeof priority === 'number' ? priority : DEFAULT_EXTENDER_PRIORITY, - ); + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.registerObjectExtension.call(platformGlobalHost.of(this), extension, packageId); } /** @@ -8272,37 +3111,9 @@ export class SchemaRegistry { */ private extensionSpellingWarned = new Set(); - /** - * [#28858, ADR-0146 D10] The key an extension of `name` is filed under: the - * key {@link getObject} reads `name` as. That is `name` itself whenever an - * entry is registered under it, or nothing is registered that `name` folds - * to. It differs only for a short name `getObject` folds to a legacy - * `__` key ({@link resolveObjectKey}); filing such an extension - * under the short name would open a second, owner-less entry beside the - * object it meant, which nothing serves and nothing checks. - * - * The folded spelling is named once at `warn`, with the key to write. ⛔ - * Never a refusal: the manifest loop and the boot hydration run while - * packages are still registering (AGENTS.md *Startup registry reads*). The - * `object_extension` item door refuses the same spelling at save (#28834), - * where the author is present to fix it. A legacy owner that registers - * AFTER the extension is not yet there to fold to, so that extension is - * filed under its own spelling, as before. - */ private extensionTargetKey(name: string, packageId: string | undefined): string { - if (name === '' || this.objectContributors.has(name)) return name; - const resolved = this.resolveObjectKey(name); - if (resolved === undefined || resolved === name) return name; - const key = `${name}|${resolved}|${packageId ?? ''}`; - if (!this.extensionSpellingWarned.has(key)) { - this.extensionSpellingWarned.add(key); - console.warn( - `[Registry] An extension${packageId ? ` from package "${packageId}"` : ''} names its object '${name}', ` + - `which is registered as '${resolved}': the extension is filed under '${resolved}', the object it ` + - `reads as. Write \`extend: '${resolved}'\` to name the object by the key it is registered under.`, - ); - } - return resolved; + // [#33949 codemod] The body moved to `registry-platform-global.ts`, its docblock with it (see `PlatformGlobalHost`). + return PlatformGlobalFacet.extensionTargetKey.call(platformGlobalHost.of(this), name, packageId); } /** @@ -8315,18 +3126,8 @@ export class SchemaRegistry { * package contributes no extension to the object). */ withdrawObjectExtension(name: string, packageId: string): boolean { - const fqn = this.resolveObjectKey(name) ?? name; - const contributors = this.objectContributors.get(fqn); - if (!contributors) return false; - const idx = contributors.findIndex(c => c.ownership === 'extend' && c.packageId === packageId); - if (idx === -1) return false; - contributors.splice(idx, 1); - if (contributors.length === 0) this.closeObjectEntry(fqn); - // The same two invalidations every other contributor mutation performs. - this.mergedObjectCache.delete(fqn); - this._objectRevision += 1; - this.log(`[Registry] Withdrew extend contribution to ${fqn} from ${packageId}`); - return true; + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.withdrawObjectExtension.call(platformGlobalHost.of(this), name, packageId); } /** @@ -8346,18 +3147,8 @@ export class SchemaRegistry { * is idempotent). */ removeObjectOverlay(name: string): boolean { - const fqn = this.resolveObjectKey(name); - if (fqn === undefined) return false; - const contributors = this.objectContributors.get(fqn); - if (!contributors) return false; - const idx = contributors.findIndex(c => c.ownership === 'overlay'); - if (idx === -1) return false; - const [removed] = contributors.splice(idx, 1); - // The same two invalidations every other contributor mutation performs. - this.mergedObjectCache.delete(fqn); - this._objectRevision += 1; - this.log(`[Registry] Removed overlay layer of object: ${fqn} (was bound to ${removed.packageId})`); - return true; + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.removeObjectOverlay.call(platformGlobalHost.of(this), name); } /** @@ -8386,44 +3177,8 @@ export class SchemaRegistry { * @throws Error listing every object whose owner count is not exactly 1. */ assertSingleOwnerPerObject(): void { - const violations: string[] = []; - for (const [fqn, contributors] of this.objectContributors.entries()) { - const owners = contributors.filter(c => c.ownership === 'own'); - if (owners.length === 0) { - // [ADR-0029 D9.5] The ORPHAN OVERLAY class. Reachable by uninstalling - // the packaged owner while a `sys_metadata` row for its object still - // exists under a binding `unregisterObjectsByPackage` cannot reach by - // package id (the sentinel). It must be LOUD rather than silent: - // `resolveObject` would otherwise warn once and answer `undefined` for - // a name the tenant can still see in `sys_metadata`. - const overlays = contributors.filter(c => c.ownership === 'overlay'); - if (overlays.length > 0) { - const bindings = overlays.map(c => c.packageId).join(', ') || '(none)'; - violations.push( - `Object "${fqn}" has an orphan overlay layer [${bindings}] and no owner. ` + - `An overlay layers OVER an owned object — re-install the package that owns it, ` + - `or delete the sys_metadata row.` - ); - } else { - const extenders = contributors.map(c => c.packageId).join(', ') || '(none)'; - violations.push( - `Object "${fqn}" has no owner — only extend contributions from [${extenders}]. ` + - `Exactly one package must register it with ownership 'own'.` - ); - } - } else if (owners.length > 1) { - const names = owners.map(c => c.packageId).join(', '); - violations.push( - `Object "${fqn}" has ${owners.length} owners [${names}] — exactly one is allowed.` - ); - } - } - if (violations.length > 0) { - throw new Error( - `[Registry] single-owner-per-object check failed (ADR-0029):\n ` + - violations.join('\n ') - ); - } + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.assertSingleOwnerPerObject.call(platformGlobalHost.of(this)); } /** @@ -8514,56 +3269,8 @@ export class SchemaRegistry { * customization package is installed — {@link assertPackageUninstallable}'s */ unregisterObjectsByPackage(packageId: string, force: boolean = false): void { - // [#7970] REFUSAL PASS — see {@link assertPackageUninstallable}, which holds - // the one copy of the predicate ([#21276] extracted so it can be asked - // without uninstalling). - if (!force) this.assertPackageUninstallable(packageId); - - // MUTATION PASS — carries no refusal of its own; the pass above already - // proved every removal below is allowed. Keep it that way: a second copy of - // the predicate here is the two-places-that-must-agree shape this ordering - // fix was chosen over. - for (const [fqn, contributors] of this.objectContributors.entries()) { - // Find this package's contributions - const packageContribs = contributors.filter(c => c.packageId === packageId); - - for (const contrib of packageContribs) { - // Remove contribution - const idx = contributors.indexOf(contrib); - if (idx !== -1) { - contributors.splice(idx, 1); - this.log(`[Registry] Removed ${contrib.ownership} contribution to ${fqn} from ${packageId}`); - } - - // [ADR-0029 D9.7] An overlay layer leaves with the base it layers - // over. Nothing durable is lost — the layer is a runtime projection of - // a `sys_metadata` row this does not touch, and a re-install - // re-hydrates it — whereas leaving it behind would MANUFACTURE the - // orphan-overlay violation D9.5 names, on every uninstall of a - // customized packaged object. Keyed off the OWNER's removal, not off - // the overlay's own binding: a sentinel-bound layer names no package - // and this walk is addressed by package id. - if (contrib.ownership === 'own') { - const overlayIdx = contributors.findIndex(c => c.ownership === 'overlay'); - if (overlayIdx !== -1) { - const [dropped] = contributors.splice(overlayIdx, 1); - this.log( - `[Registry] Removed overlay layer of ${fqn} with its owner ` + - `(layer was bound to ${dropped.packageId})` - ); - } - } - } - - // Clean up empty contributor lists - if (contributors.length === 0) { - this.closeObjectEntry(fqn); - } - - // Invalidate cache - this.mergedObjectCache.delete(fqn); - this._objectRevision += 1; - } + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.unregisterObjectsByPackage.call(platformGlobalHost.of(this), packageId, force); } /** @@ -8626,42 +3333,8 @@ export class SchemaRegistry { * another package and `force` is not set. */ unregisterObject(name: string, options: { force?: boolean } = {}): boolean { - const fqn = this.resolveObjectKey(name); - if (fqn === undefined) return false; - const contributors = this.objectContributors.get(fqn) ?? []; - - const owner = contributors.find(c => c.ownership === 'own'); - if (!options.force) { - const otherExtenders = contributors.filter( - c => c.ownership === 'extend' && c.packageId !== owner?.packageId - ); - if (otherExtenders.length > 0) { - throw new Error( - `Cannot unregister object "${fqn}": it is extended by ` + - `${otherExtenders.map(c => c.packageId).join(', ')}. Unregister the extenders first.` - ); - } - } - - // The whole entry goes: the object no longer exists, so no contribution to - // it does either. Leaving the extenders behind would be the owner-less - // state the guard above exists to prevent. - this.closeObjectEntry(fqn); - // The same two invalidations every other contributor mutation performs — - // the merged-object cache would otherwise keep answering `resolveObject` - // for a name with no contributors, and registry-derived caches (the - // engine's roll-up summary index) would never learn the set moved. - this.mergedObjectCache.delete(fqn); - this._objectRevision += 1; - // Namespaces are deliberately NOT touched: a namespace is registered per - // PACKAGE and shared by every object that package ships, so dropping one - // object must not unregister it. `unregisterObjectsByPackage` leaves it - // alone too — `uninstallPackage` owns that half. - this.log( - `[Registry] Unregistered object: ${fqn} ` + - `(${contributors.length} contribution(s), owner ${owner?.packageId ?? '(none)'})` - ); - return true; + // [#33949 codemod] The body moved to `registry-platform-global.ts` (see `PlatformGlobalHost`). + return PlatformGlobalFacet.unregisterObject.call(platformGlobalHost.of(this), name, options); } // ========================================== @@ -9075,85 +3748,27 @@ export class SchemaRegistry { this.log(`[Registry] Registered ${type}: ${storageKey}`); } - /** - * [ADR-0150 D4] Before a package-less action `incoming` takes the bare slot - * of `name`, the name's package-less actions it does not replace keep their - * own owner keys: the bare slot's present item, when its owner key is - * another, moves to that key's own slot ({@link packagelessActionSlotKey}). - * So the platform's action bound to `lead` and the environment's object-less - * action of the name are both kept, in either registration order, and each - * is read on its own key. - * - * What `incoming` replaces: the item of its own owner key (one key, one - * item; the later registration stays, as in the bare slot), and, when it is - * a stored row ({@link isStoredActionRow}), the name's stored row on any key - * — the environment's store holds one row of a name, so a row saved again - * bound to another object is that row, never a second item. - */ private keepPackagelessActionOwnerKeys(collection: Map, name: string, incoming: unknown): void { - const ownerKey = packagelessActionOwnerKey(incoming); - const replaces = (item: unknown): boolean => - packagelessActionOwnerKey(item) === ownerKey || (isStoredActionRow(incoming) && isStoredActionRow(item)); - for (const { key, item } of packagelessActionSlotEntriesOf(collection, name)) { - if (!replaces(item)) continue; - this.noteItem('action', key); - collection.delete(key); - } - const occupant = collection.get(name); - if (occupant === undefined || replaces(occupant)) return; - const aside = packagelessActionSlotKey(packagelessActionOwnerKey(occupant), name); - this.noteItem('action', aside); - collection.set(aside, occupant); - this.log(`[Registry] Kept package-less action ${name} on ${packagelessActionOwnerKey(occupant)} beside the one on ${ownerKey}`); + // [#33949 codemod] The body moved to `registry-items.ts`, its docblock with it (see `ItemsHost`). + return ItemsFacet.keepPackagelessActionOwnerKeys.call(itemsHost.of(this), collection, name, incoming); } - /** - * [ADR-0150 D4] The slot a removal of the package-less action `name`'s own - * entry retires ({@link removeRuntimeShadow}, {@link removeOverlayEntry}): - * the bare slot, unless another owner key's slot holds the name's one stored - * row ({@link isStoredActionRow}, the row the removal heals) while the bare - * slot holds code's. - */ private packagelessActionEntryToRetire(collection: Map, name: string): string { - const aside = packagelessActionSlotEntriesOf(collection, name); - if (aside.length === 0 || isStoredActionRow(collection.get(name))) return name; - const rows = aside.filter(({ item }) => isStoredActionRow(item)); - return rows.length === 1 ? rows[0].key : name; + // [#33949 codemod] The body moved to `registry-items.ts`, its docblock with it. + return ItemsFacet.packagelessActionEntryToRetire(collection, name); } - /** - * [ADR-0150 D4] Retire one package-less action slot of `name`. When it is - * the bare slot, the first other owner key's item takes it, so the bare - * slot holds a package-less action of the name whenever one is kept. - */ private retirePackagelessActionSlot(collection: Map, name: string, key: string): void { - collection.delete(key); - if (key !== name) return; - const [next] = packagelessActionSlotEntriesOf(collection, name); - if (next === undefined) return; - this.noteItem('action', next.key); - collection.delete(next.key); - this.noteItem('action', name); - collection.set(name, next.item); + // [#33949 codemod] The body moved to `registry-items.ts`, its docblock with it (see `ItemsHost`). + return ItemsFacet.retirePackagelessActionSlot.call(itemsHost.of(this), collection, name, key); } /** * Validate Metadata against Spec Zod Schemas */ validate(type: string, item: any): unknown { - if (type === 'object') { - return ObjectSchema.parse(item); - } - if (type === 'app') { - return AppSchema.parse(item); - } - if (type === 'package') { - return InstalledPackageSchema.parse(item); - } - if (type === 'plugin') { - return ManifestSchema.parse(item); - } - return true; + // [#33949 codemod] The body moved to `registry-items.ts`. + return ItemsFacet.validate(type, item); } /** @@ -9304,55 +3919,12 @@ export class SchemaRegistry { * * @param packageId The package being uninstalled. * @returns `removed` — the storage keys taken, as `type/key`; and - * `orphanedOverlays` — the bare-key overlays deliberately left behind that - * now have no base, as `type/name`. - */ - unregisterItemsByPackage(packageId: string): { removed: string[]; orphanedOverlays: string[] } { - const prefix = `${packageId}:`; - const removed: string[] = []; - const orphanedOverlays: string[] = []; - - for (const [type, collection] of this.metadata.entries()) { - // Collect before deleting — mutating a Map while iterating its own keys - // is legal but reads as a trap, and the bare-slot probe below wants the - // collection in its post-removal state to be truthful. - const owned = [...collection.keys()].filter(key => key.startsWith(prefix)); - for (const key of owned) { - collection.delete(key); - removed.push(`${type}/${key}`); - this.log(`[Registry] Unregistered ${type}: ${key} (package ${packageId} uninstalled)`); - } - for (const key of owned) { - // The bare (overlay) slot for this item is its key without the package - // prefix — the same `bareKey` the artifact-vs-DB collision warning in - // `registerItem` compares against, discriminator included. - const bareSlot = key.slice(prefix.length); - const label = `${type}/${bareSlot}`; - if (collection.has(bareSlot) && !orphanedOverlays.includes(label)) { - orphanedOverlays.push(label); - } - } - } - - if (orphanedOverlays.length > 0) { - console.warn( - `[Registry] Package "${packageId}" was uninstalled, but ${orphanedOverlays.length} ` + - `runtime/DB overlay row(s) layered over its items were KEPT — they are tenant-authored ` + - `(ADR-0005) and an uninstall does not delete them: ${orphanedOverlays.join(', ')}. ` + - `Each now overlays nothing — re-install the package that owns it, or delete the ` + - `sys_metadata row.`, - ); - } - if (removed.length > 0) { - this.log(`[Registry] Unregistered ${removed.length} item(s) from package: ${packageId}`); - } - // The package's `picklistExtensions` leave with it, and every resolved - // object is re-derived without its lists and its added values. - const droppedExtensions = this.unregisterPicklistExtensionsByPackage(packageId); - // [#24400] …and so is every object its bound actions were attached to. - // [#28877] …and every object whose actions a flow of this package withheld. - if (droppedExtensions || removed.some((r) => r.startsWith('picklist/') || r.startsWith('action/') || r.startsWith('flow/'))) this.invalidateAll(); - return { removed, orphanedOverlays }; + * `orphanedOverlays` — the bare-key overlays deliberately left behind that + * now have no base, as `type/name`. + */ + unregisterItemsByPackage(packageId: string): { removed: string[]; orphanedOverlays: string[] } { + // [#33949 codemod] The body moved to `registry-items.ts` (see `ItemsHost`). + return ItemsFacet.unregisterItemsByPackage.call(itemsHost.of(this), packageId); } // ========================================== @@ -9398,85 +3970,18 @@ export class SchemaRegistry { * here. */ replacingPackageBody(packageId: string | undefined, manifest: ObjectStackManifest, register: () => T): T { - if (!packageId) return register(); - const open = this.openPackageBody; - const body = { packageId, written: new Set() }; - this.openPackageBody = body; - let result: T; - try { - result = register(); - } finally { - this.openPackageBody = open; - } - const previous = this.packageBodyItems.get(packageId); - if (previous !== undefined) { - this.withdrawPackageBodyItemsNotShipped(packageId, previous, body.written); - this.withdrawPackageClaimsNotDeclared(packageId, manifest); - } - this.restorePoint?.note(`packageBody\u0000${packageId}`, () => { - const had = this.packageBodyItems.has(packageId); - const saved = this.packageBodyItems.get(packageId); - return () => { - if (had && saved !== undefined) this.packageBodyItems.set(packageId, saved); - else this.packageBodyItems.delete(packageId); - }; - }); - this.packageBodyItems.set(packageId, body.written); - // A body of the same package registered inside another one's run (none - // does today) ships its slots as part of that one. - if (open?.packageId === packageId) for (const slot of body.written) open.written.add(slot); - return result; + // [#33949 codemod] The body moved to `registry-items.ts` (see `ItemsHost`). + return (ItemsFacet.replacingPackageBody).call(itemsHost.of(this), packageId, manifest, register); } - /** The slots `previous` names that `written` does not, withdrawn (see {@link replacingPackageBody}). */ private withdrawPackageBodyItemsNotShipped(packageId: string, previous: ReadonlySet, written: ReadonlySet): void { - const removed: string[] = []; - for (const slot of previous) { - if (written.has(slot)) continue; - const separator = slot.indexOf('\u0000'); - const type = slot.slice(0, separator); - const key = slot.slice(separator + 1); - const collection = this.metadata.get(type); - if (!collection?.has(key)) continue; - this.noteItemOrder(type); - this.noteItem(type, key); - collection.delete(key); - removed.push(`${type}/${key}`); - } - if (removed.length === 0) return; - this.log( - `[Registry] Withdrew ${removed.length} item(s) package ${packageId}'s new body no longer ships: ${removed.join(', ')}`, - ); - // The read caches {@link unregisterItemsByPackage} drops for the same types. - if (removed.some((r) => r.startsWith('picklist/') || r.startsWith('action/') || r.startsWith('flow/'))) this.invalidateAll(); + // [#33949 codemod] The body moved to `registry-items.ts`, its docblock with it (see `ItemsHost`). + return ItemsFacet.withdrawPackageBodyItemsNotShipped.call(itemsHost.of(this), packageId, previous, written); } - /** - * The bare-name and security catalog claims `packageId` holds that - * `manifest` no longer declares, released (see {@link replacingPackageBody}). - * ⛔ Never a `function` claim: those are the hook binder's - * ({@link claimBareNameFunctions}), recorded and released by its own bind. - */ private withdrawPackageClaimsNotDeclared(packageId: string, manifest: ObjectStackManifest): void { - const bare = new Set(declaredBareNames(manifest).map(({ type, name }) => packageBodySlot(type, name))); - for (const [type, byName] of this.bareNameClaims) { - if (!isBareNameRegistryType(type)) continue; - for (const [name, claimants] of byName) { - if (!claimants.has(packageId) || bare.has(packageBodySlot(type, name))) continue; - this.noteBareNameClaim(type, name); - claimants.delete(packageId); - if (claimants.size === 0) byName.delete(name); - } - } - const catalog = new Set(declaredSecurityCatalogNames(manifest).map(({ type, name }) => packageBodySlot(type, name))); - for (const [type, byName] of this.securityCatalogClaims) { - for (const [name, claimants] of byName) { - if (!claimants.has(packageId) || catalog.has(packageBodySlot(type, name))) continue; - this.noteSecurityCatalogClaim(type, name); - claimants.delete(packageId); - if (claimants.size === 0) byName.delete(name); - } - } + // [#33949 codemod] The body moved to `registry-items.ts`, its docblock with it (see `ItemsHost`). + return ItemsFacet.withdrawPackageClaimsNotDeclared.call(itemsHost.of(this), packageId, manifest); } /** @@ -9502,66 +4007,8 @@ export class SchemaRegistry { * for every other type, which has no members. */ getItem(type: string, name: string, currentPackageId?: string, member?: string): T | undefined { - // Special handling for 'object' and 'objects' types - use objectContributors - if (type === 'object' || type === 'objects') { - return this.getObject(name) as unknown as T | undefined; - } - - const collection = this.metadata.get(type); - if (!collection) return undefined; - - // [#7730] A discriminated type's name addresses a BUNDLE, so every tier - // below is a group rather than a single key. The tiers themselves are - // unchanged (overlay → prefer-local → first composite). - // - // [#22877] A bare-name read answers the CANONICAL member — the one the - // `/meta` by-name doors address when no member is named — wherever it - // sits in that tier order. It used to take the first tier that held ANY - // member and pick canonical-first only inside it, so an overlay of a - // second locale alone (a template customized in `zh-CN` only) answered a - // read that named no member. Only a bundle with no canonical member at - // all still answers its first member, as it always did. A named `member` - // answers that member, in the same tier order, or nothing. - const disc = ITEM_KEY_DISCRIMINATORS[type]; - // [#33127] The package overlay slots join the overlay tier, after the bare slot. - if (disc) { - const { bare, local, other } = collectBundle(collection, name, disc, currentPackageId); - const overlays = this.orderedOverlayTier(type, collection, name, overlaySlotEntriesOf(collection, name, disc), currentPackageId); - const tiers = [...bare, ...overlays, ...local, ...other]; - if (member !== undefined) return (entriesOfMember(tiers, disc, member)[0]?.item as T) ?? undefined; - return ((entriesOfMember(tiers, disc, disc.canonical)[0] ?? tiers[0])?.item as T) ?? undefined; - } - - // A bare-key entry (a runtime/DB overlay rehydrated by restoreMetadataFromDb) - // intentionally shadows the packaged composite item — ADR-0005 overlay - // precedence (a customization wins over its package default). This is - // checked before prefer-local so that precedence holds; note an env-wide - // (package-less) overlay of a name that collides across packages is - // inherently ambiguous by schema (sys_metadata is unique on type+name+org, - // not package) and resolves to the single overlay row. - const direct = collection.get(name); - if (direct) return direct as T; - // [#33127, ADR-0150 D4] …and with no package-less row, a stored row bound - // to a package (its package overlay slot): the caller's package's first, - // else in the holders' order ({@link orderedOverlayTier}), never in the - // order the rows were registered. One stored row reads as it did in the - // bare slot. - const overlays = overlaySlotEntriesOf(collection, name); - if (overlays.length > 0) return this.orderedOverlayTier(type, collection, name, overlays, currentPackageId)[0].item as T; - - // Prefer-local: resolve within the caller's package first. - if (currentPackageId) { - const local = collection.get(`${currentPackageId}:${name}`); - if (local) return local as T; - } - - // Fallback: first composite key matching the bare name (legacy behaviour). - for (const [key, item] of collection) { - if (key.endsWith(`:${name}`)) { - return item as T; - } - } - return undefined; + // [#33949 codemod] The body moved to `registry-items.ts` (see `ItemsHost`). + return (ItemsFacet.getItem).call(itemsHost.of(this), type, name, currentPackageId, member); } // ========================================== @@ -9575,77 +4022,9 @@ export class SchemaRegistry { // keeps its bare-slot-first answer for every caller (ADR-0150 Execution plan, // child R: "not wired to any reader yet"). - /** - * [ADR-0150 D4, "registry overlay slot (ADR-0005)"] The package a bare-slot - * entry of `(type, name)` customizes — the per-package overlay slot. - * - * The registry still stores ONE bare slot per name (every `sys_metadata` - * hydration and write-through writes it, and {@link getItem} serves it first - * to every caller). What changes here is what that entry is BOUND to: an - * overlay binds to the `(package, name)` it customizes, so a package-scoped - * read of another package's same-named item never sees it. - * - * The binding is the entry's `_packageId` — the stored row's `package_id` - * column, or the artifact envelope the hydration grafted onto it (ADR-0010 - * §3.3; `withRowBinding` drops a body's own stamp for a package-less row) — - * read as follows: - * - * - none, `''` or the `sys_metadata` sentinel (a package-less row), when no - * package holds the name in a composite slot → `null`: the environment's - * own item (ADR-0150 D1 bucket 4); - * - a package-less row when exactly one package holds the name → that - * package: "a package-less row that bears a package-held name is a - * customization, never a second item" (ADR-0150 D3), the ADR-0005 - * overlay of the one holder, read at call time, so a package installed - * after the row was saved makes it that package's overlay; - * - a package-less row when two or more packages hold the name → - * `undefined`: a customization of a name it cannot say whose, so it binds - * to no package and is no environment item either (never a guess); - * - a package that holds the name in its composite slot → that package - * (an ADR-0005 overlay of its item); - * - a customization pair's row, when some holder of the name is a package - * the pair customizes (`isPairOf`: the App's pair is shared by every - * package of the App, ADR-0146 D3) → {@link pairRowBinding}, the exact - * read's owner check (`pairRowOnAddress`, metadata-protocol): the holder - * its `overrides.package` declares, else the App whose own pair it is - * (#28390 ruling A), else its one owner, else no package; - * - a paired `.custom` package holding no composite slot of the name, - * whose base does → the base (ADR-0146 D9: the pair's declared override - * customizes its base's item); - * - any other package → that package: its own stored item of the name. - * - * `holders` is the composite holder set, passed in when the caller already - * has it. - * - * [ADR-0150 D4] An `action` is identified by `(package, owning object, - * name)`, so for a package-less action the holders counted above are only - * those whose own item has the same owner key ({@link standaloneActionOwnerKey}, - * the router's own test; every object-less spelling is one key): a - * package-less action on another object customizes no holder's action. It - * is the environment's own item (`null`) when no holder shares its owner - * key, that holder's customization when exactly one does, and no package's - * (`undefined`) when two or more do. - */ private overlaySlotBinding(type: string, item: unknown, name: string, collection: Map, holders?: ReadonlySet): string | null | undefined { - const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; - const composite = holders ?? new Set(this.compositeHoldersOf(collection, name)); - if (typeof stamped !== 'string' || stamped === '' || stamped === 'sys_metadata') { - const customized = type === 'action' ? actionHoldersOnOwnerKeyOf(item, name, collection, composite) : [...composite]; - if (customized.length === 0) return null; - return customized.length === 1 ? customized[0] : undefined; - } - if (composite.has(stamped)) return stamped; - const paired = this.pairRowBinding(type, stamped, item, name, collection, composite); - if (paired !== NOT_A_PAIR_ROW_OF_A_HOLDER) return paired; - const base = customizationBasePackageId(stamped); - if (base !== null && composite.has(base)) return base; - // [#33127] …or whose base holds it as its own stored row (its package - // overlay slot): the pair's row customizes that base's item, as beside a - // composite slot, and is never a holder of its own. One bare slot never - // held both rows; the package overlay slots keep each. - if (base !== null && overlaySlotEntriesOf(collection, name, ITEM_KEY_DISCRIMINATORS[type]) - .some(({ key }) => parseOverlaySlotKey(key)?.packageId === base)) return base; - return stamped; + // [#33949 codemod] The body moved to `registry-package-addressing.ts`, its docblock with it (see `PackageAddressingHost`). + return PackageAddressingFacet.overlaySlotBinding.call(packageAddressingHost.of(this), type, item, name, collection, holders); } /** @@ -9679,87 +4058,14 @@ export class SchemaRegistry { * disable (the read-side hydrator's envelope for a pair row, #32692). */ overlayRowBinding(type: string, name: string, item: unknown, options?: { includeDisabledPackages?: boolean }): string | null | undefined { - const collection = this.metadata.get(type); - if (!collection) return undefined; - // [#32692] `includeDisabledPackages` counts a disabled holder as served: - // the read-side hydrator's envelope for a pair row must not move with a - // reversible disable (ADR-0150 D3). - const off = (id: string) => !options?.includeDisabledPackages && this.isPackageDisabled(id); - const composite = new Set(this.compositeHoldersOf(collection, name)); - const servedComposite = new Set([...composite].filter((id) => !off(id))); - const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; - if (typeof stamped === 'string' && stamped !== '' && stamped !== 'sys_metadata' && !composite.has(stamped)) { - const holders = new Set(composite); - for (const { item: entry } of this.bareSlotEntriesOf(type, collection, name)) { - const bound = this.overlaySlotBinding(type, entry, name, collection, composite); - if (typeof bound === 'string') holders.add(bound); - } - // The pair's own rows bind to it when no composite slot pairs it; it holds no name of its own here. - const served = new Set([...holders].filter((id) => id !== stamped && !off(id))); - const settled = this.settledPairRowBinding(stamped, item, served); - if (Array.isArray(settled)) { - const named = this.ownersHoldingAName(type, name, collection, settled as readonly string[], item, composite, stamped); - return named.length === 1 ? named[0] : undefined; - } - if (settled !== NOT_A_PAIR_ROW_OF_A_HOLDER) return settled as string | undefined; - } - return this.overlaySlotBinding(type, item, name, collection, servedComposite); + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return PackageAddressingFacet.overlayRowBinding.call(packageAddressingHost.of(this), type, name, item, options); } - /** - * [ADR-0150 D4, epic ruling 6761802819 on #31699 F2, #32408] The holder a - * customization pair's row `item` (stored under `pairId`, which holds no - * composite slot of the name) binds to, read as the exact read's one owner - * check reads it (`pairRowOnAddress`, `@objectstack/metadata-protocol`), so - * every reader of {@link overlaySlotBinding} — the holder set, a holder's - * bundle members, a package's item and the environment's item — answers - * the owner the exact address serves it to: - * - * - the row declares the base it customizes (`overrides.package`, ADR-0146 - * D2) → that holder when the pair customizes it; a declared holder the - * pair does not customize, or a declared package that holds no item of - * the name, binds it to no package (it is never the declaring row of - * another holder); - * - else a holder that is the App whose own pair it is → that App (#28390 - * ruling A: the App's address keeps its pair's row); - * - else exactly one holder the pair customizes → that holder; - * - else two or more → no package (`undefined`): the row cannot say whose - * it is, never a pick between them. - * - * The undeclared arms count the holders the exact read counts - * (`packageScopedHoldersOf`, which the exact read passes): for a bundled - * type, a package whose holding is a bundle ADDITION to another holder's - * bundle holds no name (#28802), so it is no second owner. The - * addition is judged as {@link packageScopedHoldersOf} judges it, over every - * holder of the name with every row bound to it ({@link ownersHoldingAName}): - * a row declaring a package as its base makes that package hold the row's - * member, so it is no addition when another holder ships that member (an - * undeclared row beside it is then no package's, as the exact read refuses - * it naming both). A row that declares an addition's package as its base is - * still that package's. When the count would read an undeclared row of - * another shared pair (a binding that waits on that pair's own count), the - * row binds to no package (epic seat 1's ruling). The exact read adopts - * this binding ({@link overlayRowBinding}, #32561), so it never serves such - * a row on a package's address. - * - * Every holder is counted, a disabled one included (`composite` is the - * holder set before {@link packageScopedHoldersOf} filters the served - * ones): a binding never moves with a reversible disable (ADR-0150 D3, - * #32183). The exact read counts the served holders, so with one of two - * owners disabled it serves a row this binds to no package: it adopts this - * binding over the served holders only ({@link overlayRowBinding}, #32561), - * as the enabled holder's address keeps the row (epic ruling R4-A). - * - * {@link NOT_A_PAIR_ROW_OF_A_HOLDER} when no holder of the name is a package - * `pairId` customizes: the row keeps the per-package reading of - * {@link overlaySlotBinding}. - */ private pairRowBinding(type: string, pairId: string, item: unknown, name: string, collection: Map, composite: ReadonlySet): string | undefined | typeof NOT_A_PAIR_ROW_OF_A_HOLDER { - const settled = this.settledPairRowBinding(pairId, item, composite); - if (!Array.isArray(settled)) return settled as string | undefined | typeof NOT_A_PAIR_ROW_OF_A_HOLDER; - const named = this.ownersHoldingAName(type, name, collection, settled as readonly string[], item, composite, pairId); - return named.length === 1 ? named[0] : undefined; + // [#33949 codemod] The body moved to `registry-package-addressing.ts`, its docblock with it (see `PackageAddressingHost`). + return PackageAddressingFacet.pairRowBinding.call(packageAddressingHost.of(this), type, pairId, item, name, collection, composite); } /** @@ -9787,72 +4093,15 @@ export class SchemaRegistry { return owners.length === 1 ? owners[0] : owners; } - /** - * [#28802, #32408] `owners` less every one whose holding of the bundled - * `(type, name)` is an addition to another holder's bundle - * ({@link isBundleAddition}), judged as {@link packageScopedHoldersOf} - * judges it: over EVERY holder of the name (a package outside the App - * included, so an owner that adds to it is an addition), each holder's - * members being its composite members and the bare-slot rows that bind to - * it without this count ({@link bundleMembersHeldBy}) — a pair row - * declaring the holder, an App's own pair row (#28390 ruling A), a - * per-package pair's row, the holder's own stored row. The undeclared rows - * of `pairId`, the pair this arm decides, are left out, since the members - * they add are what it answers (the exact read adopts the answer, #32561). An - * undeclared row of ANOTHER shared pair would make this count wait on that - * pair's, which may wait on this one: then `owners` is answered whole, so - * the row binds to no package (never a guess; epic seat 1's ruling, which - * the exact read adopts, #32561). An - * addition that ships the member `row` is ITS member too, so it stays an - * owner: the row may customize that package's member, and two owners bind - * it to no package. `owners` unchanged for a type that is no bundle, or with - * fewer than two. - */ private ownersHoldingAName(type: string, name: string, collection: Map, owners: readonly string[], row: unknown, composite: ReadonlySet, pairId: string): readonly string[] { - const disc = ITEM_KEY_DISCRIMINATORS[type]; - if (!disc || owners.length < 2) return owners; - const bound = new Map(); - for (const { item } of this.bareSlotEntriesOf(type, collection, name)) { - const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; - const settled = typeof stamped === 'string' && !composite.has(stamped) - ? this.settledPairRowBinding(stamped, item, composite) - : NOT_A_PAIR_ROW_OF_A_HOLDER; - if (Array.isArray(settled)) { - if (stamped === pairId) continue; - return owners; - } - const holder = settled !== NOT_A_PAIR_ROW_OF_A_HOLDER - ? settled as string | undefined - : this.overlaySlotBinding(type, item, name, collection, composite); - if (typeof holder === 'string') bound.set(holder, [...(bound.get(holder) ?? []), item]); - } - const memberOf = (item: unknown) => discriminatorValue(item, disc.field) || disc.canonical; - const membersOf = new Map>(); - const codeMembersOf = new Map>(); - for (const id of new Set([...composite, ...bound.keys()])) { - const { local } = collectBundle(collection, name, disc, id); - membersOf.set(id, new Set([...local.map((e) => e.item), ...(bound.get(id) ?? [])].map(memberOf))); - codeMembersOf.set(id, new Set(local.map((e) => memberOf(e.item)))); - } - const rowMember = memberOf(row); - return owners.filter((id) => membersOf.get(id)!.has(rowMember) || !this.isBundleAddition(id, membersOf, codeMembersOf)); + // [#33949 codemod] The body moved to `registry-package-addressing.ts`, its docblock with it (see `PackageAddressingHost`). + return PackageAddressingFacet.ownersHoldingAName.call(packageAddressingHost.of(this), type, name, collection, owners, row, composite, pairId); } - /** - * The packages holding `(type, name)` through a composite - * `:` slot, in the item store's order — the registry's - * identity key (ADR-0150 D2), read off the KEY, as {@link getItem}'s - * prefer-local reads it. A bundled type's members are one holder. - */ private compositeHoldersOf(collection: Map, name: string): string[] { - const holders = new Set(); - const suffix = `:${name}`; - for (const key of collection.keys()) { - const base = bundleBaseKey(key); - if (base !== name && base.endsWith(suffix)) holders.add(base.slice(0, -suffix.length)); - } - return [...holders]; + // [#33949 codemod] The body moved to `registry-package-addressing.ts`, its docblock with it. + return PackageAddressingFacet.compositeHoldersOf(collection, name); } /** @@ -9899,73 +4148,20 @@ export class SchemaRegistry { */ packageScopedHoldersOf(type: string, name: string, callerPackageId?: string | null, options?: { includeDisabledPackages?: boolean }): string[] { - const collection = this.metadata.get(type); - if (!collection) return []; - const composite = this.compositeHoldersOf(collection, name); - const holders = new Set(composite); - const compositeSet = new Set(composite); - for (const { item } of this.bareSlotEntriesOf(type, collection, name)) { - const bound = this.overlaySlotBinding(type, item, name, collection, compositeSet); - if (typeof bound === 'string') holders.add(bound); - } - const served = (ids: string[]): string[] => options?.includeDisabledPackages === true - ? ids - : ids.filter((id) => id === callerPackageId || !this.isPackageDisabled(id)); - const disc = ITEM_KEY_DISCRIMINATORS[type]; - if (!disc || holders.size < 2) return served([...holders]); - const membersOf = new Map>(); - const codeMembersOf = new Map>(); - for (const id of holders) { - membersOf.set(id, this.bundleMembersHeldBy(type, collection, name, id, compositeSet, disc)); - codeMembersOf.set(id, new Set(collectBundle(collection, name, disc, id).local.map((e) => discriminatorValue(e.item, disc.field) || disc.canonical))); - } - return served([...holders].filter((id) => id === callerPackageId || !this.isBundleAddition(id, membersOf, codeMembersOf))); + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return PackageAddressingFacet.packageScopedHoldersOf.call(packageAddressingHost.of(this), type, name, callerPackageId, options); } - /** - * [#28802] The members (discriminator values) of `name`'s bundle that - * `packageId` holds: its composite group's, and the bare-slot entries that - * bind to it ({@link overlaySlotBinding}; with `atGate`, as the bare-name - * gate reads them, {@link gateSlotBinding}). - */ private bundleMembersHeldBy(type: string, collection: Map, name: string, packageId: string, holders: ReadonlySet, disc: { field: string; canonical: string }, atGate = false): Set { - const { local } = collectBundle(collection, name, disc, packageId); - const bound = this.bareSlotEntriesOf(type, collection, name) - .filter(({ item }) => (atGate - ? this.gateSlotBinding(type, item, name, collection, holders) - : this.overlaySlotBinding(type, item, name, collection, holders)) === packageId); - return new Set([...local, ...bound].map((e) => discriminatorValue(e.item, disc.field) || disc.canonical)); + // [#33949 codemod] The body moved to `registry-package-addressing.ts`, its docblock with it (see `PackageAddressingHost`). + return PackageAddressingFacet.bundleMembersHeldBy.call(packageAddressingHost.of(this), type, collection, name, packageId, holders, disc, atGate); } - /** - * [#32408, epic ruling 6838245909 option B] A bare-slot row's binding as the - * bare-name gate reads a holder's members ({@link registeredBundleHolding}: - * both doors and the cold boot's seal): {@link overlaySlotBinding}'s, except - * that a row of a customization pair lends its member to a holder only - * where the per-package reading binds it to that holder too (the pair's - * base when it holds the name in a composite slot, else the pair itself: - * the reading every reader applied before {@link pairRowBinding}). - * - * Where the pair-row binding moved the row (to X, a package of the App, - * from the App or from the pair), the gate counts it for no holder. The App - * no longer holds a locale it shipped none of through a row declared for X - * (the narrowing the changeset declares), and X does not gain the row's - * locale either: a package of another namespace on X that ships only that - * locale stays an addition to X's template, refused at both doors in either - * registration order and reported by the seal, as before the binding - * moved. Admitting it waits on the by-name read following the binding (a - * follow-up under #32408): in that state the by-name read answers by - * registration order. The reads keep {@link overlaySlotBinding}. - */ private gateSlotBinding(type: string, item: unknown, name: string, collection: Map, holders: ReadonlySet): string | null | undefined { - const bound = this.overlaySlotBinding(type, item, name, collection, holders); - const stamped = (item as { _packageId?: unknown } | null | undefined)?._packageId; - if (typeof stamped !== 'string' || stamped === '' || stamped === 'sys_metadata' || holders.has(stamped)) return bound; - const base = customizationBasePackageId(stamped); - const perPackage = base !== null && holders.has(base) ? base : stamped; - return bound === perPackage ? bound : undefined; + // [#33949 codemod] The body moved to `registry-package-addressing.ts`, its docblock with it (see `PackageAddressingHost`). + return PackageAddressingFacet.gateSlotBinding.call(packageAddressingHost.of(this), type, item, name, collection, holders); } /** @@ -10015,20 +4211,8 @@ export class SchemaRegistry { * identity is a pair; omitted, the canonical member first. */ getPackageScopedItem(type: string, name: string, packageId: string, member?: string): T | undefined { - const collection = this.metadata.get(type); - if (!collection) return undefined; - const holders = new Set(this.compositeHoldersOf(collection, name)); - // [#33127] Every stored row bound to it, in the order its stored-row read serves them ({@link rankedForPackage}). - const bound = this.rankedForPackage(this.bareSlotEntriesOf(type, collection, name) - .filter(({ item }) => this.overlaySlotBinding(type, item, name, collection, holders) === packageId - && !pairRowDeclaresAnotherHolder(item, packageId, holders)), packageId); - const disc = ITEM_KEY_DISCRIMINATORS[type]; - if (disc) { - const { local } = collectBundle(collection, name, disc, packageId); - return pickBundleMember([...bound, ...local], disc, member); - } - if (bound.length > 0) return bound[0].item as T; - return collection.get(`${packageId}:${name}`) as T | undefined; + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return (PackageAddressingFacet.getPackageScopedItem).call(packageAddressingHost.of(this), type, name, packageId, member); } /** @@ -10042,14 +4226,8 @@ export class SchemaRegistry { * @param member - As on {@link getItem}. */ getEnvironmentScopedItem(type: string, name: string, member?: string): T | undefined { - const collection = this.metadata.get(type); - if (!collection) return undefined; - const holders = new Set(this.compositeHoldersOf(collection, name)); - const unbound = this.bareSlotEntriesOf(type, collection, name) - .filter(({ item }) => this.overlaySlotBinding(type, item, name, collection, holders) === null); - const disc = ITEM_KEY_DISCRIMINATORS[type]; - if (disc) return pickBundleMember(unbound, disc, member); - return unbound[0]?.item as T | undefined; + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return (PackageAddressingFacet.getEnvironmentScopedItem).call(packageAddressingHost.of(this), type, name, member); } /** @@ -10061,12 +4239,8 @@ export class SchemaRegistry { * the item on its key, never only the bare slot's of another key. */ getEnvironmentScopedActions(name: string): T[] { - const collection = this.metadata.get('action'); - if (!collection) return []; - const holders = new Set(this.compositeHoldersOf(collection, name)); - return this.bareSlotEntriesOf('action', collection, name) - .filter(({ item }) => this.overlaySlotBinding('action', item, name, collection, holders) === null) - .map(({ item }) => item as T); + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return (PackageAddressingFacet.getEnvironmentScopedActions).call(packageAddressingHost.of(this), name); } /** @@ -10076,7 +4250,8 @@ export class SchemaRegistry { * it reads, such as the protocol's by-name read. */ readsByPackage(type: string): boolean { - return isPackageScopedReadType(type); + // [#33949 codemod] The body moved to `registry-package-addressing.ts`. + return PackageAddressingFacet.readsByPackage(type); } /** @@ -10174,43 +4349,14 @@ export class SchemaRegistry { if (resolution.status === 'refused') throw new PackageScopedNameRefusedError(resolution); } - /** - * The bare-slot entries of `(type, name)`: one, or a bundle's members, canonical first. - * [#33127, ADR-0150 D4] …then its package overlay slots ({@link overlaySlotKey}): every - * stored row of the name, each bound to its package, read as the bare slot's entry is. - * [ADR-0150 D4] An action's package-less items of the other owner keys - * ({@link packagelessActionSlotKey}) follow the bare slot's: each is one - * more package-less item of the name, judged as the bare slot's is. - */ private bareSlotEntriesOf(type: string, collection: Map, name: string): BundleEntry[] { - const disc = ITEM_KEY_DISCRIMINATORS[type]; - const bare = disc - ? collectBundle(collection, name, disc).bare - : collection.has(name) ? [{ key: name, item: collection.get(name) }] : []; - const otherOwnerKeys = type === 'action' ? packagelessActionSlotEntriesOf(collection, name) : []; - return [...bare, ...otherOwnerKeys, ...overlaySlotEntriesOf(collection, name, disc)]; + // [#33949 codemod] The body moved to `registry-package-addressing.ts`, its docblock with it. + return PackageAddressingFacet.bareSlotEntriesOf(type, collection, name); } - /** - * [#33127, ADR-0150 D4] `entries`, the stored rows that bind to `packageId` - * ({@link overlaySlotBinding}), in the order the stored-row read serves them - * for that package (`servedOverlayRowCandidates`, metadata-protocol): the - * declared override of its customization pair (a row of another package - * whose `overrides.package` names it), then its own row, then the - * package-less row, then any other row bound to it (a pair's undeclared - * row). Ties keep the store's order. So the item the registry answers for a - * package does not follow the order its rows were registered in. - */ private rankedForPackage(entries: readonly BundleEntry[], packageId: string): BundleEntry[] { - const rank = (entry: BundleEntry): number => { - const rowPackage = storedRowPackageOf(entry); - if (rowPackage === packageId) return 1; - if (rowPackage === undefined) return 2; - return declaredOverrideBase(entry.item) === packageId ? 0 : 3; - }; - return entries.map((entry, index) => ({ entry, index, rank: rank(entry) })) - .sort((a, b) => a.rank - b.rank || a.index - b.index) - .map(({ entry }) => entry); + // [#33949 codemod] The body moved to `registry-package-addressing.ts`, its docblock with it. + return PackageAddressingFacet.rankedForPackage(entries, packageId); } /** @@ -10226,50 +4372,14 @@ export class SchemaRegistry { * @returns the packages whose slot was retired. */ removePackageOverlaySlots(type: string, name: string, member: string | undefined, select: (packageId: string) => boolean): string[] { - const collection = this.metadata.get(type); - if (!collection) return []; - const disc = ITEM_KEY_DISCRIMINATORS[type]; - const slots = overlaySlotEntriesOf(collection, name, disc); - const removed = new Set(); - for (const { key } of disc && member !== undefined ? entriesOfMember(slots, disc, member) : slots) { - const packageId = parseOverlaySlotKey(key)!.packageId; - if (!select(packageId)) continue; - this.noteItem(type, key); - collection.delete(key); - removed.add(packageId); - this.log(`[Registry] Removed package overlay slot ${type}: ${name} of ${packageId} (its stored row is gone)`); - } - if (removed.size > 0 && (type === 'picklist' || type === 'action')) this.invalidateAll(); - return [...removed]; + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return PackageAddressingFacet.removePackageOverlaySlots.call(packageAddressingHost.of(this), type, name, member, select); } - /** - * [#33127] The overlay tier of a read naming no package ({@link getItem}), - * when the bare slot is empty and package overlay slots hold rows of the - * name: the rows of `preferred` first ({@link rankedForPackage}), then each - * other package's, holder by holder in the item store's order (the code - * holders, as the composite fallback reads them), then by package id. Never - * the order the rows were registered in. - */ private orderedOverlayTier(type: string, collection: Map, name: string, entries: readonly BundleEntry[], preferred?: string): BundleEntry[] { - if (entries.length <= 1) return [...entries]; - const composite = new Set(this.compositeHoldersOf(collection, name)); - const byHolder = new Map(); - for (const entry of entries) { - const bound = this.overlaySlotBinding(type, entry.item, name, collection, composite); - const holder = typeof bound === 'string' ? bound : `${OVERLAY_SLOT_MARK}${storedRowPackageOf(entry) ?? ''}`; - byHolder.set(holder, [...(byHolder.get(holder) ?? []), entry]); - } - const order = [...composite]; - const holders = [...byHolder.keys()].sort((a, b) => { - if (a === preferred || b === preferred) return a === preferred ? -1 : 1; - const ia = order.indexOf(a); - const ib = order.indexOf(b); - if (ia !== ib) return ia === -1 ? 1 : ib === -1 ? -1 : ia - ib; - return a < b ? -1 : a > b ? 1 : 0; - }); - return holders.flatMap((holder) => this.rankedForPackage(byHolder.get(holder)!, holder)); + // [#33949 codemod] The body moved to `registry-package-addressing.ts`, its docblock with it (see `PackageAddressingHost`). + return PackageAddressingFacet.orderedOverlayTier.call(packageAddressingHost.of(this), type, collection, name, entries, preferred); } /** @@ -10291,76 +4401,8 @@ export class SchemaRegistry { * artifact canonical-first, as before. */ getArtifactItem(type: string, name: string, currentPackageId?: string, member?: string): T | undefined { - if (type === 'object' || type === 'objects') { - // [ADR-0029 D9.6] Artifact identity is read from the OWNER contributor's - // layer, NEVER from the merged body. The merged body deliberately keeps - // the overlay's `_provenance: 'org'` (D9.2 — the resolved schema must - // not move), so applying the artifact test to it answers about whichever - // layer is on top rather than about the code layer, which is exactly how - // this predicate came to answer `false` for a name a code package still - // ships (#6853 P3) and silently disarmed `saveMetaItem`'s overlay gate - // and tier 3 of the delete heal. - // - // What comes BACK is the code-layer body (owner + its extenders), which - // is what every consumer of this lookup wants — the packaged baseline an - // overlay customizes, and the `_lock`/`_packageId`/`_provenance` - // envelope that must win over an overlay (ADR-0010 §3.3). With no - // overlay registered it is byte-for-byte the previous answer. - const fqn = this.resolveObjectKey(name); - const ownerLayer = fqn === undefined ? undefined : this.resolveOwnerLayer(fqn); - return ownerLayer && isCodeArtifactBody(ownerLayer) ? (ownerLayer as T) : undefined; - } - const collection = this.metadata.get(type); - if (!collection) return undefined; - - // [#7730] Bundle-aware form of the three tiers below, for a type whose - // identity is (name, discriminator). Same order — prefer-local composite, - // then any composite, then the bare-key fallback with its package check — - // over bundle GROUPS instead of single keys, with the canonical member - // first inside each group. - const disc = ITEM_KEY_DISCRIMINATORS[type]; - if (disc) { - const { bare, local, other } = collectBundle(collection, name, disc, currentPackageId); - const narrow = (entries: BundleEntry[]) => (member === undefined ? entries : entriesOfMember(entries, disc, member)); - for (const { item } of narrow([...local, ...other])) { - if (isCodeArtifactBody(item)) return item as T; - } - for (const { item } of narrow(bare)) { - if (isCodeArtifactBody(item) && (!currentPackageId || item._packageId === currentPackageId)) return item as T; - } - return undefined; - } - - // ADR-0048 prefer-local: when the caller resolves within a package, the - // artifact owned by that package wins over a first-match composite scan, - // so two installed packages shipping the same name don't resolve by Map - // iteration order. - if (currentPackageId) { - const local = collection.get(`${currentPackageId}:${name}`) as any; - if (local && local._packageId && local._packageId !== 'sys_metadata' && !isTenantAuthored(local)) return local as T; - } - for (const [key, item] of collection) { - if (key !== name && key.endsWith(`:${name}`)) { - const it = item as any; - if (it && it._packageId && it._packageId !== 'sys_metadata' && !isTenantAuthored(it)) return item as T; - } - } - // Bare-key fallback: a runtime/DB overlay rehydrated under the plain name. - // ADR-0048 (#1828) — when the caller resolves *within a package*, only accept - // this entry if it belongs to that package. The bare slot holds a single - // row (last hydrate wins), so once the unscoped list stopped collapsing - // colliding rows, an overlay from package A hydrated here would otherwise be - // grafted as the "artifact" of package B's same-name row — mislabeling its - // `_packageId`/lock. A package-less caller (`currentPackageId` omitted) keeps - // the legacy best-effort first-match. - const direct = collection.get(name) as any; - if ( - direct && direct._packageId && direct._packageId !== 'sys_metadata' && !isTenantAuthored(direct) && - (!currentPackageId || direct._packageId === currentPackageId) - ) { - return direct as T; - } - return undefined; + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return (PackageAddressingFacet.getArtifactItem).call(packageAddressingHost.of(this), type, name, currentPackageId, member); } /** @@ -10387,64 +4429,8 @@ export class SchemaRegistry { * any member's artifact remains, as before. */ removeRuntimeShadow(type: string, name: string, member?: string): boolean { - const collection = this.metadata.get(type); - if (!collection) return false; - - // [#7730] For a discriminated type the shadow sits at `name@` - // — nothing is ever stored under the plain name — so the plain-key test - // would silently answer "no shadow" for every email template. - // - // [#22877] The overlay store now keys a row by its member - // (`sys_metadata.bundle_member`), so a by-name reset removes ONE member's - // row and the heal that follows it names that member: resetting `zh-CN` - // must leave the `en-US` overlay registered, because its row is still - // stored and still served. Sweeping the group, as this did while the - // store kept one row per name, dropped every other member's overlay until - // the next boot. A caller naming no member keeps the group sweep, the - // shape `unregisterItem` takes for a withdrawal by name. - const disc = ITEM_KEY_DISCRIMINATORS[type]; - if (disc) { - const { bare, local, other } = collectBundle(collection, name, disc); - const shadows = member === undefined ? bare : entriesOfMember(bare, disc, member); - if (shadows.length === 0) return false; - // [#23074] A named member's OWN artifact, the test this tier has always - // made for a name: a shadow is lifted only where a packaged artifact - // takes its place. An overlay of a member the package does NOT ship has - // none, so this tier declines, and the heal's last tier retires the - // entry (`removeOverlayEntry`): the protocol grafts the artifact - // envelope onto the shipped member alone, so that overlay is registered - // as the tenant row it is and the retirement takes it. (#22877 stage 2 - // lifted it here on ANY member's artifact, because the bundle's envelope - // grafted onto it made the retirement refuse it as code.) The group - // sweep, naming no member, keeps the bundle's artifact, any member's. - const artifacts = member === undefined ? [...local, ...other] : entriesOfMember([...local, ...other], disc, member); - const artifact = artifacts.find(({ item }) => item?._packageId && item._packageId !== 'sys_metadata'); - if (!artifact) return false; - for (const { key } of shadows) { - collection.delete(key); - this.log(`[Registry] Removed runtime shadow ${type}: ${key} (artifact ${artifact.item._packageId} restored)`); - } - return true; - } - - if (!collection.has(name)) return false; - for (const [key, item] of collection) { - if (key !== name && key.endsWith(`:${name}`)) { - const it = item as any; - if (it && it._packageId && it._packageId !== 'sys_metadata') { - // [ADR-0150 D4] …the package-less action's own entry, which may sit - // in another owner key's slot ({@link packagelessActionEntryToRetire}). - if (type === 'action') this.retirePackagelessActionSlot(collection, name, this.packagelessActionEntryToRetire(collection, name)); - else collection.delete(name); - // [#28356] An object may carry the shadow as a bound action; the - // artifact it uncovers may bind elsewhere ({@link attachBoundActions}). - if (type === 'action') this.invalidateAll(); - this.log(`[Registry] Removed runtime shadow ${type}: ${name} (artifact ${it._packageId} restored)`); - return true; - } - } - } - return false; + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return PackageAddressingFacet.removeRuntimeShadow.call(packageAddressingHost.of(this), type, name, member); } /** @@ -10487,44 +4473,8 @@ export class SchemaRegistry { * @returns whether an entry was removed. */ removeOverlayEntry(type: string, name: string, member?: string): boolean { - const collection = this.metadata.get(type); - if (!collection) return false; - - // [#7730] Same relocation as {@link removeRuntimeShadow}: a discriminated - // type's overlay slot is `name@`, so the plain-key read - // finds nothing and the deleted overlay would keep being served for the - // life of the process — the very residue #5079 removed. The refusal is - // still per entry: a bundle member that IS a packaged artifact stays. - // [#22877] …and a named member retires that member's entry only. - const disc = ITEM_KEY_DISCRIMINATORS[type]; - if (disc) { - const { bare } = collectBundle(collection, name, disc); - let removed = false; - for (const { key, item } of member === undefined ? bare : entriesOfMember(bare, disc, member)) { - if (isCodeArtifactBody(item)) continue; - collection.delete(key); - this.log(`[Registry] Removed overlay entry ${type}: ${key} (no layer serves it any more)`); - removed = true; - } - return removed; - } - - if (!collection.has(name)) return false; - // [ADR-0150 D4] A package-less action's own entry may sit in another - // owner key's slot beside the bare one ({@link packagelessActionEntryToRetire}). - const key = type === 'action' ? this.packagelessActionEntryToRetire(collection, name) : name; - const plain = collection.get(key) as any; - if (plain && plain._packageId && plain._packageId !== 'sys_metadata' && !isTenantAuthored(plain)) { - return false; - } - if (type === 'action') this.retirePackagelessActionSlot(collection, name, key); - else collection.delete(name); - // [#28356] …and an object may carry the retired entry as a bound action - // ({@link attachBoundActions}): drop the cached folds, as - // {@link unregisterItem} does, or a deleted runtime action stays listed. - if (type === 'action') this.invalidateAll(); - this.log(`[Registry] Removed overlay entry ${type}: ${name} (no layer serves it any more)`); - return true; + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return PackageAddressingFacet.removeOverlayEntry.call(packageAddressingHost.of(this), type, name, member); } /** @@ -10543,28 +4493,8 @@ export class SchemaRegistry { * every other package that extends them. */ listItems(type: string, packageId?: string, options?: { includeDisabledPackages?: boolean } & ObjectExtensionFoldOptions): T[] { - // Special handling for 'object' and 'objects' types - use objectContributors - if (type === 'object' || type === 'objects') { - return this.getAllObjects(packageId, { - withoutExtensionsOf: options?.withoutExtensionsOf, - onlyExtensionsOf: options?.onlyExtensionsOf, - }) as unknown as T[]; - } - - const items = Array.from(this.metadata.get(type)?.values() || []) as T[]; - let result = items; - if (packageId) { - result = result.filter((item: any) => item._packageId === packageId); - } - // Hide metadata owned by a disabled package so the console (app switcher, - // view lists, dashboards, …) stops surfacing it after a disable. The - // `package` type itself is never filtered — the Packages page must still - // list disabled packages so they can be re-enabled. Disable is reversible: - // items remain registered and reappear on enable. - if (type !== 'package' && options?.includeDisabledPackages !== true) { - result = result.filter((item: any) => !this.isPackageDisabled((item as any)?._packageId)); - } - return result; + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return (PackageAddressingFacet.listItems).call(packageAddressingHost.of(this), type, packageId, options); } /** @@ -10572,9 +4502,8 @@ export class SchemaRegistry { * items with no owning package are treated as enabled. */ isPackageDisabled(packageId?: string): boolean { - if (!packageId) return false; - const pkg = this.getPackage(packageId); - return pkg?.enabled === false || pkg?.status === 'disabled'; + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return PackageAddressingFacet.isPackageDisabled.call(packageAddressingHost.of(this), packageId); } /** @@ -10596,9 +4525,8 @@ export class SchemaRegistry { * decorates offline with it. */ isObjectPackageDisabled(name: string): boolean { - const fqn = this.resolveObjectKey(name); - if (fqn === undefined) return false; - return this.isPackageDisabled(this.getObjectOwner(fqn)?.packageId); + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return PackageAddressingFacet.isObjectPackageDisabled.call(packageAddressingHost.of(this), name); } /** @@ -10612,15 +4540,8 @@ export class SchemaRegistry { * not disabled. */ isFlowOfDisabledPackage(name: string): boolean { - const collection = this.metadata.get('flow'); - if (!collection || typeof name !== 'string' || name === '') return false; - let found = false; - for (const [key, flow] of collection) { - if (key !== name && !key.endsWith(`:${name}`)) continue; - if (!this.isPackageDisabled((flow as { _packageId?: string } | undefined)?._packageId)) return false; - found = true; - } - return found; + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return PackageAddressingFacet.isFlowOfDisabledPackage.call(packageAddressingHost.of(this), name); } /** @@ -10645,40 +4566,21 @@ export class SchemaRegistry { * label) is not withheld. */ isActionWithheldByDisabledPackage(action: unknown): boolean { - if (!action || typeof action !== 'object') return false; - const a = action as { _packageId?: string; objectName?: unknown; type?: unknown; target?: unknown; params?: unknown }; - if (this.isPackageDisabled(a._packageId)) return true; - if (!this.hasDisabledPackage()) return false; - if (typeof a.objectName === 'string' && a.objectName !== '' && this.isObjectPackageDisabled(a.objectName)) return true; - if (a.type === 'flow' && typeof a.target === 'string' && this.isFlowOfDisabledPackage(a.target)) return true; - if (Array.isArray(a.params)) { - for (const param of a.params) { - const override = (param as { objectOverride?: unknown } | null | undefined)?.objectOverride; - if (typeof override === 'string' && override !== '' && this.isObjectPackageDisabled(override)) return true; - } - } - return false; + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return PackageAddressingFacet.isActionWithheldByDisabledPackage.call(packageAddressingHost.of(this), action); } - /** [#28877] Whether any installed package is disabled — the cheap gate before the launch walk. */ private hasDisabledPackage(): boolean { - for (const pkg of this.metadata.get('package')?.values() ?? []) { - const p = pkg as { enabled?: boolean; status?: string } | undefined; - if (p?.enabled === false || p?.status === 'disabled') return true; - } - return false; + // [#33949 codemod] The body moved to `registry-package-addressing.ts`, its docblock with it (see `PackageAddressingHost`). + return PackageAddressingFacet.hasDisabledPackage.call(packageAddressingHost.of(this)); } /** * Get all registered metadata types (Kinds) */ getRegisteredTypes(): string[] { - const types = Array.from(this.metadata.keys()); - // Always include 'object' even if stored separately - if (!types.includes('object') && this.objectContributors.size > 0) { - types.push('object'); - } - return types; + // [#33949 codemod] The body moved to `registry-package-addressing.ts` (see `PackageAddressingHost`). + return PackageAddressingFacet.getRegisteredTypes.call(packageAddressingHost.of(this)); } // ========================================== @@ -10691,432 +4593,36 @@ export class SchemaRegistry { settings?: Record, scope?: ArtifactInstallScope, ): InstalledPackage { - // [#23972] `source` is the CALLER's statement of where this package came - // from — the door that registers it is the only party that knows - // (`registerApp`: code or marketplace; `installPackage` in - // `@objectstack/metadata-protocol`, the `POST /packages` fallback and the - // `sys_packages` rehydrate: authored). The registry records it and never - // infers it, so an untyped caller that omits it, or spells a value outside - // the closed set, is refused here — ahead of every check and mutation - // below, so a refused call leaves no row behind — rather than recording a - // row the read door would serve without an origin. - if (!InstalledPackageSourceSchema.safeParse(source).success) { - throw new TypeError( - `[Registry] installPackage('${String(manifest?.id)}') needs the package's source, one of ` - + `${InstalledPackageSourceSchema.options.map((v) => `'${v}'`).join(' | ')}; received ` - + `${source === undefined ? 'none' : JSON.stringify(source)}. The door that registers a package ` - + `states where it came from; the registry does not guess.`, - ); - } - // [ADR-0130 D1] The co-owners of this package — the OTHER packages the same - // release artifact is delivering. Computed once and read by both halves of - // the install-time pair below, so the relaxation and the refusal cannot - // disagree about who counts as a co-owner. - // - // Everything inside one artifact is built, versioned, downloaded and - // installed as one atomic act by one publisher, and D1 makes that joint - // delivery the ownership proof itself — nothing else asserts it, and D8 - // deliberately adds no manifest field that could drift from it. - // - // The self-exclusion is by the SAME key the registry installs under - // ({@link artifactPackageId}), not by `manifest.id` alone: a manifest - // carrying only `name` is installed under that name, and a co-owner set - // that still contained it would let the package "co-own" with itself. - // - // [ADR-0141 D5] …and, beside the artifact, the packages a declared - // dependency makes co-owners of the namespace — see {@link coOwnersOf}. - const selfId = artifactPackageId(manifest); - const coOwners = this.coOwnersOf(manifest, selfId, scope); - - // ADR-0048 Phase 1 — install-time namespace gate. Refuse a package whose - // namespace is already owned by an installed package that is not a co-owner - // of it; this is the constraint the object/table layer enforces implicitly - // (a duplicate `CREATE TABLE _` fails at the DB), made explicit and - // early. Same-package reinstall/reload is excluded (owner === manifest.id), - // shareable platform namespaces (base/system/sys) are exempt, and — since - // ADR-0130 D1 — so is a co-owner delivered by the same artifact. - // - // ⛔ This relaxation may not exist without the object-name check below it. - // Namespace exclusivity has been silently proxying for "no two packages - // define the same object name" (ADR-0048 §3.2's own grounding), so dropping - // the proxy alone is the one path in ADR-0130 that can damage customer data. - // ADR-0130 D3 states the pair as a machine constraint for that reason, and - // `registry-artifact-co-ownership.test.ts` asserts it as ONE proposition. - if (manifest.namespace && !isShareableNamespace(manifest.namespace)) { - const conflictOwner = this.getNamespaceOwners(manifest.namespace).find( - (owner) => owner !== manifest.id && !coOwners.has(owner), - ); - if (conflictOwner) { - if (this.collisionPolicy === 'warn') { - console.warn( - `[Registry] Namespace conflict (downgraded to warning via ` + - `OS_METADATA_COLLISION=warn): namespace "${manifest.namespace}" is ` + - `already owned by "${conflictOwner}"; installing "${manifest.id}" ` + - `anyway. See ADR-0048.`, - ); - } else { - throw new NamespaceConflictError(manifest.namespace, conflictOwner, manifest.id); - } - } - } - - // [ADR-0130 D3] The other half of the pair: per-object-name uniqueness - // across the co-owners of one artifact, install-time and ahead of any DDL. - // Placed ahead of EVERY mutation this method makes, so a refused package - // leaves no record, no namespace ownership and no half-applied install - // behind — the same disposition the namespace gate above has. - this.refuseCoOwnedObjectNameCollision(manifest, selfId, coOwners); - - // [ADR-0150 D2, child N] The object rule above, extended to the twelve - // bucket 2/3 types phase 1 does not cover: inside one namespace, a - // `(type, name)` has one package holder, so that NAMESPACE/NAME names one - // item (`namespace-co-owner-names.ts`). Ahead of every mutation, like the - // check above; at every install, boot included, like it; with no - // `collisionPolicy` downgrade, like the bare-name rule below. - this.refuseNamespaceNameConflicts(manifest); - - // [ADR-0150 D5, child G1] A namespace never spells a literal segment of - // the REST address grammar: the router reads the literal first, so the - // package's qualified addresses would reach another door. Ahead of every - // mutation, with no `collisionPolicy` downgrade — at the running install - // door only. - // - // A package the boot registers keeps booting: no release before this rule - // refused one, and what the literal costs it is its qualified path alone, - // which never existed before the rule did. Two readings name it, the - // bare-name rule's (`refuseBareNameConflicts`): until the cold boot seals - // the holders, every package registering is one the deployment holds - // (installed, or shipped in its code); after it, a package already - // registered under its own id AND the same namespace is a reload, a - // re-install or an upgrade (the namespace gate's same-package reading - // above). An upgrade that MOVES to a literal namespace is a new occupation - // of it and is refused. A held package is warned once and installs; it is - // read by `?package=`. - { - const literalRefusal = validateNamespaceNotAddressLiteral(manifest.namespace); - if (literalRefusal) { - const packageId = selfId ?? manifest.id; - const alreadyHeld = !this.bareNameHoldersSealed - || this.getPackage(packageId)?.manifest?.namespace === manifest.namespace; - if (!alreadyHeld) { - throw new NamespaceAddressLiteralError(manifest.namespace as string, literalRefusal, packageId); - } - this.warnAddressLiteralNamespaceOnce(packageId, literalRefusal); - } - } - - // [ADR-0150 D6, child K] A namespace never equals an object name, in - // either direction, and no object is short-named `custom` — the spec's one - // predicate, against what the environment already holds. Ahead of every - // mutation, at every install, boot included, with no `collisionPolicy` - // downgrade, like the check above. - this.refuseNamespaceObjectNameClashes(manifest, selfId); - - // The security catalog's one-name-one-holder rule (ADR-0048 §3.4 narrowed - // for positions, permission sets and capabilities; maintainer ruling Q4 = A - // on #15196) — also ahead of every mutation, for the same reason. No - // co-owner exemption: two packages of one artifact sharing a catalog name - // are as ambiguous to a bare-name assignment as two strangers are, and no - // `collisionPolicy` downgrade (`security-catalog-namespace.ts`). - this.refuseSecurityCatalogNameConflicts(manifest, selfId); - - // One default record page per object per deployment (maintainer ruling A - // on objectstack#23015; `default-record-page.ts`) — also ahead of every - // mutation, for the same reason. No co-owner exemption and no - // `collisionPolicy` downgrade: two packages of one artifact shipping a - // default for one object leave the record view to registration order - // exactly as two strangers do. - this.refuseDefaultRecordPageConflicts(manifest, selfId); - - // One name, one package holder for the bare-name types — a picklist, a - // mapping, a dataset, an API, an object-less action, a flow, a webhook, a - // connector, an email template: the names their consumers resolve with no - // package (ADR-0048 §3.4 narrowed; maintainer ruling C on #30798, phase 1; - // `bare-name-one-holder.ts`). Also ahead of every mutation, with no - // co-owner exemption and no `collisionPolicy` downgrade, for the security - // catalog's reasons. Judged only once the cold boot has sealed the holders: - // during a boot every package registers again, and a deployment that - // already holds a clash is the cold boot's to report. - this.refuseBareNameConflicts(manifest, selfId); - - const now = new Date().toISOString(); - - // [#18877] 「缺省 = 保持,有旗 = 设置」 — an install that was not asked to move - // the lifecycle state does not move it. The install verb carries no - // lifecycle argument at all, so at THIS layer every install is - // 「缺省」 and the whole rule is: an EXISTING row keeps its own - // `enabled` / `status` / `statusChangedAt`, and the boot seed decides only - // for an id that has no row yet. The door above is where 「有旗」 is spelled - // (`enableOnInstall` ⇒ `enablePackage` / `disablePackage`, - // `packages/runtime/src/domains/packages.ts`). - // - // ⚠️ The seed set is a BOOT HYDRATION input, not a standing verdict. It is - // filled once, before any registration, from the durable disable file - // (`AppPlugin.seedPersistedDisabledPackages`) and no lifecycle verb updates - // it. Consulting it on every install is what made the most recent explicit - // operator action revertible by an unrelated re-install: disable → restart - // → `PATCH /packages/:id/enable` (200, registry true, disk cleared) → - // flag-absent `install(m, { overwrite: true })` → the seed still listed the - // id → the row landed disabled again, and since #18752 the disk follows the - // row, so the enable was gone after the next restart with no error - // anywhere. Reading the ROW first is what makes the seed's answer - // unreachable once a row exists — and a row always exists by the time an - // operator can have acted on it. - const existing = this.getPackage(manifest.id); - const seeded = this.initialDisabledPackageIds.has(manifest.id); - const lifecycle: Pick = existing - ? { - status: existing.status, - enabled: existing.enabled, - // Carried, never restamped: `statusChangedAt` answers "when did an - // operator last move this package", and a re-install is not such a - // move. A row that never carried one still does not. - ...(existing.statusChangedAt !== undefined - ? { statusChangedAt: existing.statusChangedAt } - : {}), - } - : { - status: seeded ? 'disabled' : 'installed', - enabled: !seeded, - ...(seeded ? { statusChangedAt: now } : {}), - }; - - const pkg: InstalledPackage = { - // The RECORD's manifest, not the caller's live object — see - // {@link toRecordManifest}. Every other read below (`manifest.id`, - // `manifest.namespace`) deliberately keeps reading the ARGUMENT: the - // projection is what the registry hands out, never what it decides with. - // - // {@link withDeclaredFunctionEntries} runs FIRST and at this boundary - // only: it makes the two authored `functions` spellings structurally - // equal so the structural projection reports BOTH, without teaching the - // projection a key name. - manifest: toRecordManifest(withDeclaredFunctionEntries(manifest)), - source, - ...lifecycle, - installedAt: now, - updatedAt: now, - settings, - }; - - // Register namespace if present - if (manifest.namespace) { - this.registerNamespace(manifest.namespace, manifest.id); - } - - this.noteItemType('package'); - if (!this.metadata.has('package')) { - this.metadata.set('package', new Map()); - } - const collection = this.metadata.get('package')!; - if (collection.has(manifest.id)) { - // Re-install of an already-registered package manifest (rebuild / HMR) is - // a normal path, not an error — keep it at debug (#3420). - this.debug(`[Registry] Overwriting package: ${manifest.id}`); - } - this.noteItem('package', manifest.id); - collection.set(manifest.id, pkg); - // A row that lands disabled withholds the package's `extend` contributions - // from every served fold ({@link withholdDisabledExtensions}), and a fold - // cached before the row existed read it as enabled. - if (this.isPackageDisabled(manifest.id)) this.invalidateAll(); - // The names this package now holds, recorded whether or not an item store - // entry will carry them (see `securityCatalogClaims`). - if (selfId !== undefined) this.recordSecurityCatalogClaims(selfId, manifest); - // …and the default record pages it declares (see `defaultRecordPageClaims`). - if (selfId !== undefined) this.recordDefaultRecordPageClaims(selfId, manifest); - // …and its bare names of the covered types (see `bareNameClaims`). - if (selfId !== undefined) this.recordBareNameClaims(selfId, manifest); - this.log(`[Registry] Installed package: ${manifest.id} (${manifest.name})`); - return pkg; + // [#33949 codemod] The body moved to `registry-packages.ts` (see `PackagesHost`). + return PackagesFacet.installPackage.call(packagesHost.of(this), manifest, source, settings, scope); } - /** - * The co-owners of the incoming package's namespace, each with the source - * that makes it one. Both halves of the install-time pair read this one map, - * so the namespace relaxation and the object-name refusal cannot disagree - * about who counts. - * - * Two sources: - * - * - **`artifact`** (ADR-0130 D1) — the other packages the same release - * artifact is delivering, as the load path's scope names them. - * - **a declared dependency** (ADR-0141 D5) — for a non-shareable namespace - * N the incoming package P uses: - * - `dependency`: P declares an installed X whose namespace is N; or a - * sitting owner of N declares P. The relation is declared by the - * dependent alone and read from either end, so the parent registering - * second — an upgrade, or a boot that hydrated the module first — meets - * its module as a co-owner instead of `NAMESPACE_CONFLICT`. - * - `sibling`: a sitting owner S of N and P each directly declare one - * installed package Q whose namespace is N — two modules under one - * declared parent, neither declaring the other. - * - * Direct declarations only, never the closure the save door walks. Q's - * namespace is read from Q's installed record: ⛔ two packages that merely - * declare one common package co-own nothing unless that package is installed - * and holds N, or any two packages sharing a utility dependency could share a - * namespace. Nothing is persisted: the relation is the declarations of the - * packages the registry holds now. - */ private coOwnersOf( manifest: ObjectStackManifest, selfId: string | undefined, scope: ArtifactInstallScope | undefined, ): Map { - const coOwners = new Map(); - for (const id of scope?.packageIds ?? []) if (id !== selfId) coOwners.set(id, 'artifact'); - - const namespace = manifest.namespace; - if (!namespace || isShareableNamespace(namespace) || selfId === undefined) return coOwners; - - const holdsNamespace = (id: string) => this.getPackage(id)?.manifest?.namespace === namespace; - // P's declared parents that hold N — the parent arm from P's side, and the - // Q set the sibling arm matches against. - const parents = new Set(declaredDependencyIds(manifest).filter((id) => id !== selfId && holdsNamespace(id))); - for (const id of parents) if (!coOwners.has(id)) coOwners.set(id, 'dependency'); - - for (const owner of this.getNamespaceOwners(namespace)) { - if (owner === selfId || coOwners.has(owner)) continue; - const declaredByOwner = declaredDependencyIds(this.getPackage(owner)?.manifest); - if (declaredByOwner.includes(selfId)) coOwners.set(owner, 'dependency'); - else if (declaredByOwner.some((id) => parents.has(id))) coOwners.set(owner, 'sibling'); - } - return coOwners; + // [#33949 codemod] The body moved to `registry-packages.ts`, its docblock with it (see `PackagesHost`). + return PackagesFacet.coOwnersOf.call(packagesHost.of(this), manifest, selfId, scope); } - /** - * [ADR-0130 D3] Refuse an install in which a CO-OWNER of the incoming package - * — a package delivered by the same release artifact — already owns an object - * name the incoming package claims. - * - * ## Why the refusal is scoped to co-owners - * - * It is exactly the surface D1 opened. Before D1 the namespace gate refused - * two packages sharing a namespace outright, and that refusal was doing this - * job by proxy; a name claimed by a package from a DIFFERENT artifact is still - * that gate's business and still refused there. So this check adds no refusal - * the platform did not already make in some form — per #14122 §6.2 it can only - * reject a configuration that would have failed at the DB anyway, earlier and - * more legibly — while the co-owner case, which D1 has just admitted, is the - * one that would otherwise reach the DB unchecked. - * - * ## One resolution, shared with the read path - * - * The name is resolved through {@link computeFQN} — the same call - * `registerObject` makes on the very definitions this manifest is about to - * register — rather than by a private spelling. A gate that resolved names its - * own way could refuse an entry `registerObject` would have accepted under a - * different key, or wave through the one it would have refused. - * - * ## The one carve-out - * - * A tenant-authored sitting owner is skipped, because `registerObject`'s - * late-install path (ADR-0029 D9 §6.1) does not refuse that case either: a - * code package that ships a name a `sys_metadata` row already holds TAKES - * ownership and re-classifies the tenant contribution as its overlay. Refusing - * here would break an install the very next call would have completed. - * - * @throws {ArtifactObjectNameConflictError} ADR-0112 envelope, naming both - * packages and the object. - */ private refuseCoOwnedObjectNameCollision( manifest: ObjectStackManifest, selfId: string | undefined, coOwners: ReadonlyMap, ): void { - // No artifact scope, or an artifact carrying one package: nothing this - // method can refuse. Stated as an early return rather than left to fall out - // of the loop, because "the single-package path is untouched" (D7) is a - // property of this code, not a coincidence of its inputs. - if (coOwners.size === 0) return; - - for (const shortName of declaredOwnedObjectNames(manifest)) { - const fqn = computeFQN(manifest.namespace, shortName); - const owner = this.getObjectOwner(fqn); - const ownerId = owner?.packageId; - if (owner === undefined || ownerId === undefined || ownerId === selfId) continue; - // Not a co-owner: a cross-artifact claim, which the namespace gate above - // owns and refuses. ⛔ Not widened here — that would be a new refusal for - // configurations D1 never admitted, outside this card and outside D3. - if (!coOwners.has(ownerId)) continue; - if (isTenantAuthored(owner.definition)) continue; - throw new ArtifactObjectNameConflictError(fqn, ownerId, selfId ?? manifest.id, coOwners.get(ownerId)); - } + // [#33949 codemod] The body moved to `registry-packages.ts`, its docblock with it (see `PackagesHost`). + return PackagesFacet.refuseCoOwnedObjectNameCollision.call(packagesHost.of(this), manifest, selfId, coOwners); } - /** - * [ADR-0150 D2, child N] Refuse an install whose package declares an item of - * a covered type under a name another installed package of its namespace - * already holds (`namespace-co-owner-names.ts`). - * - * The sitting holders are the installed package records — their manifests, - * as {@link installPackage} recorded them — of the incoming package's - * namespace: the record is the one place every door's install lands, so an - * uninstall takes its names with it and nothing else is kept. The same - * package's own record is never a second holder (a reload, a re-install or - * an upgrade). - * - * @throws {NamespaceNameConflictError} ADR-0112 envelope, naming every holder. - */ private refuseNamespaceNameConflicts(manifest: ObjectStackManifest): void { - this.refuseNamespaceHolderConflicts(namespacePackageOf(manifest)); + // [#33949 codemod] The body moved to `registry-packages.ts`, its docblock with it (see `PackagesHost`). + return PackagesFacet.refuseNamespaceNameConflicts.call(packagesHost.of(this), manifest); } - /** - * [ADR-0150 D6, child K] Refuse an install whose namespace equals an object - * name the environment holds or the package itself ships, whose object name - * equals an installed namespace, or whose object is short-named `custom` - * (`findNamespaceObjectNameClashes`, `@objectstack/spec/kernel`). - * - * The incoming side is the package's namespace and the objects it ships - * (`manifest.objects` and each nested `plugins[]` entry's, the objects - * `registerApp` registers under it). The installed side is every namespace - * the namespace gate has recorded and every object the registry holds, - * minus what this same package already holds (a reload, a re-install or an - * upgrade is never its own clash). A clash already present between two - * installed holders is not this install's to report. - * - * @throws {NamespaceObjectNameConflictError} ADR-0112 envelope, every clash named. - */ private refuseNamespaceObjectNameClashes(manifest: ObjectStackManifest, selfId: string | undefined): void { - const incomingId = selfId ?? manifest.id; - const isSelf = (packageId: string | undefined): boolean => - packageId !== undefined && (packageId === incomingId || packageId === manifest.id); - const namespace = typeof manifest.namespace === 'string' && manifest.namespace !== '' ? manifest.namespace : undefined; - - const incomingObjects: ObjectNameHolder[] = []; - const seen = new Set(); - const shipped = (body: ObjectStackManifest): void => { - for (const shortName of declaredOwnedObjectNames(body)) { - const name = computeFQN(namespace, shortName); - if (seen.has(name)) continue; - seen.add(name); - incomingObjects.push({ name, ...(namespace ? { namespace } : {}), ...(incomingId ? { packageId: incomingId } : {}) }); - } - }; - shipped(manifest); - const plugins = (manifest as { plugins?: unknown }).plugins; - if (Array.isArray(plugins)) { - for (const plugin of plugins) if (plugin && typeof plugin === 'object') shipped(plugin as ObjectStackManifest); - } - - const installedNamespaces: NamespaceHolder[] = []; - for (const [ns, owners] of this.namespaceRegistry) { - for (const owner of owners) if (!isSelf(owner)) installedNamespaces.push({ namespace: ns, packageId: owner }); - } - const installedObjects: ObjectNameHolder[] = []; - for (const fqn of this.objectContributors.keys()) { - const ownerId = this.getObjectOwner(fqn)?.packageId; - if (isSelf(ownerId) || seen.has(fqn)) continue; - installedObjects.push({ name: fqn, ...(ownerId ? { packageId: ownerId } : {}) }); - } - - const clashes = findNamespaceObjectNameClashes( - { namespaces: namespace ? [{ namespace, ...(incomingId ? { packageId: incomingId } : {}) }] : [], objects: incomingObjects }, - { namespaces: installedNamespaces, objects: installedObjects }, - ); - if (clashes.length > 0) throw new NamespaceObjectNameConflictError(clashes, incomingId ?? '(unnamed package)'); + // [#33949 codemod] The body moved to `registry-packages.ts`, its docblock with it (see `PackagesHost`). + return PackagesFacet.refuseNamespaceObjectNameClashes.call(packagesHost.of(this), manifest, selfId); } /** @@ -11131,7 +4637,8 @@ export class SchemaRegistry { * `undefined` (no residual, or none of the covered types) refuses nothing. */ refuseResidualNamespaceNameConflicts(residual: NamespacePackage | undefined): void { - this.refuseNamespaceHolderConflicts(residual); + // [#33949 codemod] The body moved to `registry-packages.ts` (see `PackagesHost`). + return PackagesFacet.refuseResidualNamespaceNameConflicts.call(packagesHost.of(this), residual); } /** @@ -11143,32 +4650,16 @@ export class SchemaRegistry { * registering again replaces its own entry. `undefined` records nothing. */ recordResidualNamespaceHolder(residual: NamespacePackage | undefined): void { - if (residual === undefined || !isNamespaceJudged(residual.namespace)) return; - this.residualNamespaceHolders.set(residual.packageId, residual); + // [#33949 codemod] The body moved to `registry-packages.ts` (see `PackagesHost`). + return PackagesFacet.recordResidualNamespaceHolder.call(packagesHost.of(this), residual); } /** The residual holders {@link recordResidualNamespaceHolder} recorded, by owner id. */ private readonly residualNamespaceHolders = new Map(); - /** - * The one walk both refusals above share: `incoming` against every installed - * package record of its namespace and every recorded residual holder of it, - * read through the same predicate, thrown in one envelope. - */ private refuseNamespaceHolderConflicts(incoming: NamespacePackage | undefined): void { - if (incoming === undefined || !isNamespaceJudged(incoming.namespace) || incoming.names.length === 0) return; - const sitting: NamespacePackage[] = []; - for (const record of this.metadata.get('package')?.values() ?? []) { - const recorded = (record as InstalledPackage | undefined)?.manifest; - if (recorded?.namespace !== incoming.namespace) continue; - const holder = namespacePackageOf(recorded); - if (holder !== undefined) sitting.push(holder); - } - for (const residual of this.residualNamespaceHolders.values()) { - if (residual.namespace === incoming.namespace) sitting.push(residual); - } - const conflicts = namespaceNameConflicts(incoming, sitting); - if (conflicts.length > 0) throw new NamespaceNameConflictError(conflicts); + // [#33949 codemod] The body moved to `registry-packages.ts`, its docblock with it (see `PackagesHost`). + return PackagesFacet.refuseNamespaceHolderConflicts.call(packagesHost.of(this), incoming); } /** @@ -11184,86 +4675,13 @@ export class SchemaRegistry { * instead, which never touches the boot seed.) */ uninstallPackage(id: string, options?: { keepBootSeed?: boolean }): boolean { - const pkg = this.getPackage(id); - if (!pkg) { - console.warn(`[Registry] Package not found for uninstall: ${id}`); - return false; - } - - // [#7970] Unregister objects FIRST — this is the one step that can REFUSE - // (ADR-0029: the package owns an object another package `extend`s), so - // every mutation below is downstream of the refusal point and a refused - // uninstall removes nothing at all. The namespace release used to sit - // ABOVE this line, which meant reaching the guard that exists to keep a - // registry whole cost the namespace on the way in: the package stayed - // installed with its objects and its record intact, while its namespace no - // longer resolved, for the life of the process. Safe as the first step - // because the verb reads `objectContributors` only — it depends on nothing - // the steps below establish. - this.unregisterObjectsByPackage(id); - - // Unregister namespace - if (pkg.manifest.namespace) { - this.unregisterNamespace(pkg.manifest.namespace, id); - } - - // [#7221] …and everything else the package shipped. The object verb above - // reaches `objectContributors` only, so without this an uninstall dropped - // the package record while its `page`/`view`/`flow`/`app`/`api` entries - // stayed resolvable through `getItem`/`listItems` for the life of the - // process. Runs AFTER the object verb because that one can refuse - // (ADR-0029 extenders): a refused uninstall must remove nothing at all. - this.unregisterItemsByPackage(id); - // …and the security catalog names it held, so an uninstalled package's - // names are free for the next package to declare. - this.forgetSecurityCatalogClaims(id); - // …and the default record pages it declared, so the next package may - // declare a default for those objects. - this.forgetDefaultRecordPageClaims(id); - // …and its bare names of the covered types. - this.forgetBareNameClaims(id); - // [#33050] …and the record of what its body shipped: a later install of - // the id is a first registration, with nothing of its own to withdraw. - this.packageBodyItems.delete(id); - // [#31720] …and the namespace names a stack's residual holds under this - // id: the residual's items are registered under their owner id, so - // `unregisterItemsByPackage(id)` above has just withdrawn them (the - // owner id is one of the stack's own bodies in the `preserve` shape). - this.residualNamespaceHolders.delete(id); - // [#30581] …and the navigation entries it put into other packages' apps: - // with no row left, the fold's disabled check cannot withhold them. - this.setPackageNavContributions(id, []); - - // [#18877] The boot seed forgets the id together with the row. A package - // that no longer exists has no lifecycle state to preserve, so the next - // install of this id must be a FRESH install — and without this line the - // seed would outlive the row and re-disable it, which is exactly the - // 「听不见的持久回退」 this card removed from the install path. Runs with the - // record removal below rather than at the top: every mutation here is - // downstream of `unregisterObjectsByPackage`, the one step that can REFUSE - // (ADR-0029), so a refused uninstall still removes nothing at all. - if (options?.keepBootSeed !== true) this.initialDisabledPackageIds.delete(id); - - // [#28877] A disabled package's objects and flows withheld other packages' - // actions ({@link isActionWithheldByDisabledPackage}); with the record gone - // nothing is disabled, so every cached fold is re-derived. - if (this.isPackageDisabled(id)) this.invalidateAll(); - - // Remove package record - const collection = this.metadata.get('package'); - if (collection) { - collection.delete(id); - this.log(`[Registry] Uninstalled package: ${id}`); - // …and, last, whoever keeps its own state keyed by this package's id - // (the engine's manifest map) drops it, on every uninstall door at once. - this.announcePackageUninstalled(id); - return true; - } - return false; + // [#33949 codemod] The body moved to `registry-packages.ts` (see `PackagesHost`). + return PackagesFacet.uninstallPackage.call(packagesHost.of(this), id, options); } getPackage(id: string): InstalledPackage | undefined { - return this.metadata.get('package')?.get(id) as InstalledPackage | undefined; + // [#33949 codemod] The body moved to `registry-packages.ts` (see `PackagesHost`). + return PackagesFacet.getPackage.call(packagesHost.of(this), id); } /** @@ -11280,7 +4698,8 @@ export class SchemaRegistry { * wins (a reinstall of the stack restates them). */ recordResidualOwnerDependencies(ownerId: string, dependencies: readonly string[]): void { - this.residualOwnerDependencies.set(ownerId, Object.freeze([...dependencies])); + // [#33949 codemod] The body moved to `registry-packages.ts` (see `PackagesHost`). + return PackagesFacet.recordResidualOwnerDependencies.call(packagesHost.of(this), ownerId, dependencies); } /** @@ -11291,43 +4710,23 @@ export class SchemaRegistry { * declares through every reader of that walk. */ residualOwnerDependenciesOf(id: string): readonly string[] | undefined { - return this.residualOwnerDependencies.get(id); + // [#33949 codemod] The body moved to `registry-packages.ts` (see `PackagesHost`). + return PackagesFacet.residualOwnerDependenciesOf.call(packagesHost.of(this), id); } getAllPackages(): InstalledPackage[] { - return this.listItems('package'); + // [#33949 codemod] The body moved to `registry-packages.ts` (see `PackagesHost`). + return PackagesFacet.getAllPackages.call(packagesHost.of(this)); } enablePackage(id: string): InstalledPackage | undefined { - const pkg = this.getPackage(id); - if (pkg) { - const wasDisabled = this.isPackageDisabled(id); - pkg.enabled = true; - pkg.status = 'installed'; - pkg.statusChangedAt = new Date().toISOString(); - pkg.updatedAt = new Date().toISOString(); - this.log(`[Registry] Enabled package: ${id}`); - // [#24400] A disabled package's bound actions are not attached - // ({@link boundCodeActions}), so every object's fold is re-derived. - this.invalidateAll(); - if (wasDisabled) this.announcePackageEnabled(id); - } - return pkg; + // [#33949 codemod] The body moved to `registry-packages.ts` (see `PackagesHost`). + return PackagesFacet.enablePackage.call(packagesHost.of(this), id); } disablePackage(id: string): InstalledPackage | undefined { - const pkg = this.getPackage(id); - if (pkg) { - pkg.enabled = false; - pkg.status = 'disabled'; - pkg.statusChangedAt = new Date().toISOString(); - pkg.updatedAt = new Date().toISOString(); - this.log(`[Registry] Disabled package: ${id}`); - // [#24400] A disabled package's bound actions are not attached - // ({@link boundCodeActions}), so every object's fold is re-derived. - this.invalidateAll(); - } - return pkg; + // [#33949 codemod] The body moved to `registry-packages.ts` (see `PackagesHost`). + return PackagesFacet.disablePackage.call(packagesHost.of(this), id); } /** @@ -11355,16 +4754,8 @@ export class SchemaRegistry { id: string, patch: { name?: string; description?: string; version?: string; dependencies?: Record }, ): InstalledPackage | undefined { - const pkg = this.getPackage(id); - if (!pkg) return undefined; - const manifest = pkg.manifest as Record; - if (patch.name !== undefined) manifest.name = patch.name; - if (patch.description !== undefined) manifest.description = patch.description; - if (patch.version !== undefined) manifest.version = patch.version; - if (patch.dependencies !== undefined) manifest.dependencies = { ...patch.dependencies }; - pkg.updatedAt = new Date().toISOString(); - this.log(`[Registry] Updated package manifest: ${id}`); - return pkg; + // [#33949 codemod] The body moved to `registry-packages.ts` (see `PackagesHost`). + return PackagesFacet.updatePackageManifest.call(packagesHost.of(this), id, patch); } // ========================================== @@ -11388,13 +4779,13 @@ export class SchemaRegistry { * @throws {PackageScopedNameRefusedError} when the resolver refuses. */ getApp(name: string, currentPackageId?: string): any { - const app = this.resolveItem('app', name, currentPackageId); - if (!app) return app; - return this.applyNavContributions(app); + // [#33949 codemod] The body moved to `registry-apps.ts` (see `AppsHost`). + return AppsFacet.getApp.call(appsHost.of(this), name, currentPackageId); } getAllApps(): any[] { - return this.listItems('app').map((app: any) => this.applyNavContributions(app)); + // [#33949 codemod] The body moved to `registry-apps.ts` (see `AppsHost`). + return AppsFacet.getAllApps.call(appsHost.of(this)); } // ========================================== @@ -11414,21 +4805,8 @@ export class SchemaRegistry { contribution: { app: string; group?: string; priority?: number; items?: any[] }, packageId?: string, ): void { - if (!contribution || !contribution.app) return; - this.noteAppNavContributions(contribution.app); - const list = this.appNavContributions.get(contribution.app) ?? []; - list.push({ - packageId, - group: contribution.group, - priority: contribution.priority ?? 200, - items: Array.isArray(contribution.items) ? contribution.items : [], - }); - this.appNavContributions.set(contribution.app, list); - this.log( - `[Registry] Navigation contribution: ${packageId ?? '(unknown)'} -> ${contribution.app}` + - (contribution.group ? `/${contribution.group}` : '') + - ` (${list[list.length - 1].items.length} items)`, - ); + // [#33949 codemod] The body moved to `registry-apps.ts` (see `AppsHost`). + return AppsFacet.registerAppNavContribution.call(appsHost.of(this), contribution, packageId); } /** @@ -11451,47 +4829,14 @@ export class SchemaRegistry { packageId: string, contributions: ReadonlyArray<{ app: string; group?: string; priority?: number; items?: any[] }>, ): void { - const incoming = new Map>(); - for (const contribution of contributions) { - if (!contribution || !contribution.app) continue; - const list = incoming.get(contribution.app) ?? []; - list.push({ - packageId, - group: contribution.group, - priority: contribution.priority ?? 200, - items: Array.isArray(contribution.items) ? contribution.items : [], - }); - incoming.set(contribution.app, list); - } - for (const [app, list] of this.appNavContributions) { - if (list.some((c) => c.packageId === packageId)) this.noteAppNavContributions(app); - } - for (const app of incoming.keys()) this.noteAppNavContributions(app); - for (const [app, list] of [...this.appNavContributions]) { - const at = list.findIndex((c) => c.packageId === packageId); - if (at === -1) continue; - const kept = list.filter((c) => c.packageId !== packageId); - kept.splice(at, 0, ...(incoming.get(app) ?? [])); - incoming.delete(app); - if (kept.length > 0) this.appNavContributions.set(app, kept); - else this.appNavContributions.delete(app); - } - for (const [app, list] of incoming) { - this.appNavContributions.set(app, [...(this.appNavContributions.get(app) ?? []), ...list]); - } - for (const c of contributions) { - if (!c || !c.app) continue; - this.log( - `[Registry] Navigation contribution: ${packageId} -> ${c.app}` + - (c.group ? `/${c.group}` : '') + - ` (${Array.isArray(c.items) ? c.items.length : 0} items)`, - ); - } + // [#33949 codemod] The body moved to `registry-apps.ts` (see `AppsHost`). + return AppsFacet.setPackageNavContributions.call(appsHost.of(this), packageId, contributions); } /** Contributions registered for an app (empty array when none). */ getAppNavContributions(appName: string): Array<{ packageId?: string; group?: string; priority: number; items: any[] }> { - return this.appNavContributions.get(appName) ?? []; + // [#33949 codemod] The body moved to `registry-apps.ts` (see `AppsHost`). + return AppsFacet.getAppNavContributions.call(appsHost.of(this), appName); } /** @@ -11506,55 +4851,13 @@ export class SchemaRegistry { * the merge must be reachable from there too (ADR-0029 D7). */ applyNavContributions(app: any): any { - const contributions = this.appNavContributions.get(app?.name); - if (!contributions || contributions.length === 0) return app; - - const cloned = structuredClone(app); - const nav: any[] = Array.isArray(cloned.navigation) ? cloned.navigation : (cloned.navigation = []); - - // Lower priority applied first — mirrors object extender ordering. - const sorted = [...contributions].sort((a, b) => a.priority - b.priority); - for (const c of sorted) { - if (!c.items.length) continue; - // [#28117] A disabled package's contributions leave the app, as its - // objects leave the data plane: "disabled" has one meaning (hidden + - // refused + data kept). Asked per read, never cached, so an enable - // restores them with nothing to invalidate; skipped BEFORE the group - // lookup, so a disabled contribution is not relocated or diagnosed either. - if (this.isPackageDisabled(c.packageId)) continue; - if (c.group) { - const group = this.findNavGroup(nav, c.group); - if (group) { - if (!Array.isArray(group.children)) group.children = []; - group.children.push(...c.items); - } else { - // [#14553] The relocation itself is UNCHANGED — the fold stays - // order-independent and contributions into optional groups keep - // working (option A, a refusal plus a registration-order rule, was - // weighed and not taken). What changed is that it is no longer - // silent: this used to be one `this.log(...)` gated at `info`, so a - // deployment at `OS_REGISTRY_LOG=warn` watched its information - // architecture change with nothing in the log at all. - this.recordNavGroupMiss(String(app.name), c); - nav.push(...c.items); - } - } else { - nav.push(...c.items); - } - } - return cloned; + // [#33949 codemod] The body moved to `registry-apps.ts` (see `AppsHost`). + return AppsFacet.applyNavContributions.call(appsHost.of(this), app); } - /** - * Depth-first search for a `type: 'group'` nav item by id. - * - * [#14553] Delegates rather than implements: `os build` asks the identical - * question at compile time over a composed artifact, and two copies of this - * walk is exactly the drift that would let the build call an authoring error - * fine while this fold relocates it. - */ private findNavGroup(items: any[], groupId: string): any | undefined { - return resolveNavGroup(items, groupId); + // [#33949 codemod] The body moved to `registry-apps.ts`, its docblock with it. + return AppsFacet.findNavGroup(items, groupId); } // ========================================== @@ -11580,29 +4883,12 @@ export class SchemaRegistry { */ private navGroupDiagnostics = new Map>(); - /** - * Record — and, the first time, announce — one relocated contribution. - * - * The entry is kept so a caller that is not reading the log - * ({@link getAppNavDiagnostics}: `os doctor`, a boot report, a test) can ask - * the app what happened to it. - */ private recordNavGroupMiss( appName: string, c: { packageId?: string; group?: string; items: any[] }, ): void { - const diagnostic = navContributionGroupDiagnostic({ - app: appName, - group: String(c.group), - ...(c.packageId === undefined ? {} : { packageId: c.packageId }), - items: c.items, - }); - const perApp = this.navGroupDiagnostics.get(appName) ?? new Map(); - const key = `${c.packageId ?? ''}|${c.group}`; - if (perApp.has(key)) return; - perApp.set(key, diagnostic); - this.navGroupDiagnostics.set(appName, perApp); - this.warn(formatNavContributionGroupDiagnostic(diagnostic)); + // [#33949 codemod] The body moved to `registry-apps.ts`, its docblock with it (see `AppsHost`). + return AppsFacet.recordNavGroupMiss.call(appsHost.of(this), appName, c); } /** @@ -11615,7 +4901,8 @@ export class SchemaRegistry { * rather than the process's history. */ getAppNavDiagnostics(appName: string): NavContributionGroupDiagnostic[] { - return [...(this.navGroupDiagnostics.get(appName)?.values() ?? [])]; + // [#33949 codemod] The body moved to `registry-apps.ts` (see `AppsHost`). + return AppsFacet.getAppNavDiagnostics.call(appsHost.of(this), appName); } // ========================================== @@ -11627,7 +4914,8 @@ export class SchemaRegistry { } getAllPlugins(): ObjectStackManifest[] { - return this.listItems('plugin'); + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.getAllPlugins.call(picklistsHost.of(this)); } // ========================================== @@ -11642,7 +4930,8 @@ export class SchemaRegistry { } getAllKinds(): { id: string; description?: string }[] { - return this.listItems('kind'); + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.getAllKinds.call(picklistsHost.of(this)); } // ========================================== @@ -11672,39 +4961,8 @@ export class SchemaRegistry { extension: { extend: string; options: readonly PicklistOptionLike[] }, packageId?: string, ): void { - const target = extension.extend; - // [ADR-0150 D3, #31700; epic seat ruling R2-A, 6812862622] After the - // seal, an extension registered on its own (outside an install judged as - // a whole) meets the one re-judge first: its target, judged caller-less, - // must be one list ({@link refusePicklistItemArrival}). - this.refusePicklistItemArrival( - packageId, - { picklistExtensions: [extension] }, - `a \`picklistExtensions\` entry of package '${packageId}' extending picklist '${target}'`, - ); - const packageKey = packageId ?? ''; - const own = this.picklistExtensionContributions.get(target)?.get(packageKey); - const merged = new Map(own?.options); - for (const option of extension.options ?? []) { - if (option && typeof option === 'object' && option.value !== undefined) merged.set(String(option.value), option); - } - const candidate: PicklistContribution = { packageId, kind: 'extension', options: [...merged.values()] }; - const others = this.picklistContributions(target).filter( - (c) => !(c.kind === 'extension' && (c.packageId ?? '') === packageKey), - ); - // The candidate's own list first: a value repeated INSIDE this one entry - // is refused too, and against the owner's values it then reads in merge - // order (the list first, this extension second). - const repeated = findDuplicatePicklistValue([{ ...candidate, options: extension.options ?? [] }]); - const duplicate = repeated ?? findDuplicatePicklistValue([...others, candidate]); - if (duplicate) throw duplicatePicklistValueError(target, duplicate); - - let byPackage = this.picklistExtensionContributions.get(target); - if (!byPackage) this.picklistExtensionContributions.set(target, byPackage = new Map()); - this.notePicklistExtension(target, packageKey); - byPackage.set(packageKey, { packageId, options: merged }); - this.invalidateAll(); - this.log(`[Registry] Registered picklist extension: ${target} (+${extension.options?.length ?? 0}) from ${packageId}`); + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.registerPicklistExtension.call(picklistsHost.of(this), extension, packageId); } /** @@ -11724,9 +4982,8 @@ export class SchemaRegistry { * reader keeps its answer. */ resolvePicklistOptions(name: string, callerPackageId?: string | null): PicklistOptionLike[] | undefined { - const contributions = this.picklistContributions(name, callerPackageId); - if (!contributions.some((c) => c.kind === 'picklist')) return undefined; - return mergePicklistOptions(contributions); + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.resolvePicklistOptions.call(picklistsHost.of(this), name, callerPackageId); } /** @@ -11754,69 +5011,9 @@ export class SchemaRegistry { }); } - /** - * The registry as R reads it once `manifests` (an artifact the install door - * is judging, not yet registered) are installed: each manifest answers - * `getPackage` for its own id — its `dependencies` and `namespace`, which is - * all the closure reads — ahead of an installed version of it, and each - * `picklist` a manifest declares is held by that manifest's package, next - * to the registry's holders. So a reference to a list a sibling in the - * artifact declares is judged by R's one rule — reached through the - * referrer's declared closure, else refused naming every holder — exactly as - * the served field will be once the artifact registers (epic seat review on - * #31731). ⛔ No closure is computed here; R computes it from this view. - * - * [#31700, ruling R2-A] `replaces: false` (a per-item seam, - * {@link judgePicklistArrival}): the manifest adds lists to a package and - * stands for no version of it, so `getPackage` keeps answering the - * installed record. A manifest with no package id registers its lists as - * the environment's (`registerItem` with no package), so the view holds - * them there. - */ private withPendingManifests(manifests: readonly unknown[], options: { replaces?: boolean } = {}): PackageScopedRegistry { - const pending = new Map(); - const pendingLists = new Map>(); - const pendingEnvironmentLists = new Set(); - for (const manifest of manifests) { - const id = artifactPackageId(manifest); - if (id === undefined) { - for (const name of collectManifestPicklistNames(manifest)) pendingEnvironmentLists.add(name); - continue; - } - if (options.replaces !== false) pending.set(id, { manifest } as unknown as InstalledPackage); - for (const name of collectManifestPicklistNames(manifest)) { - let holders = pendingLists.get(name); - if (!holders) pendingLists.set(name, holders = new Set()); - holders.add(id); - } - } - const pendingHolders = (type: string, name: string): ReadonlySet => - (type === 'picklist' ? pendingLists.get(name) : undefined) ?? new Set(); - // A residual owner's declared dependencies (ADR-0130 D4) are read by the - // one closure walk through the registry it is handed — this view too. - const view: PackageScopedRegistry & Pick = { - residualOwnerDependenciesOf: (id) => this.residualOwnerDependenciesOf(id), - getPackage: (id) => pending.get(id) ?? this.getPackage(id), - getAllPackages: () => [ - ...this.getAllPackages().filter((row) => !pending.has(artifactPackageId(row.manifest) ?? '')), - ...pending.values(), - ], - // [#32183] The asked holder set (served, or every holder for a - // durable judgment such as {@link picklistReferenceVerdict}), with the - // artifact's own lists beside it. - packageScopedHoldersOf: (type, name, callerPackageId, options) => [ - ...new Set([...this.packageScopedHoldersOf(type, name, callerPackageId, options), ...pendingHolders(type, name)]), - ], - getPackageScopedItem: (type: string, name: string, packageId: string, member?: string): T | undefined => - pendingHolders(type, name).has(packageId) - ? ({ name, _packageId: packageId } as unknown as T) - : this.getPackageScopedItem(type, name, packageId, member), - getEnvironmentScopedItem: (type: string, name: string, member?: string): T | undefined => - type === 'picklist' && pendingEnvironmentLists.has(name) - ? ({ name } as unknown as T) - : this.getEnvironmentScopedItem(type, name, member), - }; - return view; + // [#33949 codemod] The body moved to `registry-picklists.ts`, its docblock with it (see `PicklistsHost`). + return PicklistsFacet.withPendingManifests.call(picklistsHost.of(this), manifests, options); } /** @@ -11840,7 +5037,8 @@ export class SchemaRegistry { * closure holds more than once, still refuses the boot here. */ findUnresolvedPicklistReferences(): UnresolvedPicklistReference[] { - return this.auditPicklistReferences().unresolved; + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.findUnresolvedPicklistReferences.call(picklistsHost.of(this)); } /** @@ -11851,17 +5049,8 @@ export class SchemaRegistry { * why), so a refusal here is only ever an ambiguity, every holder named. */ findOrphanPicklistExtensions(): UnresolvedPicklistExtension[] { - const orphans: UnresolvedPicklistExtension[] = []; - for (const [target, byPackage] of this.picklistExtensionContributions) { - const verdict = this.picklistReferenceVerdict(target, null); - if (verdict.status === 'resolved') continue; - for (const entry of byPackage.values()) { - orphans.push(verdict.status === 'refused' - ? { picklist: target, packageId: entry.packageId, refusal: verdict.message } - : { picklist: target, packageId: entry.packageId }); - } - } - return orphans; + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.findOrphanPicklistExtensions.call(picklistsHost.of(this)); } /** @@ -11877,7 +5066,8 @@ export class SchemaRegistry { * refused boot). The fix is the refusal's own: declare the dependency. */ findOutsideClosurePicklistReferences(): UnresolvedPicklistReference[] { - return this.auditPicklistReferences().outsideClosure; + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.findOutsideClosurePicklistReferences.call(picklistsHost.of(this)); } /** @@ -11907,7 +5097,8 @@ export class SchemaRegistry { * {@link registerObject}) — meets {@link judgePicklistArrival} first. */ sealPicklistVocabulary(): void { - this.picklistVocabularySealed = true; + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.sealPicklistVocabulary.call(picklistsHost.of(this)); } /** Has the boot sealed the picklist vocabulary (every arrival is now judged)? */ @@ -11926,9 +5117,8 @@ export class SchemaRegistry { * and field, and each holder. */ refusePicklistArrival(manifests: readonly unknown[], options: { act?: PicklistArrivalAct } = {}): void { - if (!this.picklistVocabularySealed || this.picklistArrivalDepth > 0) return; - const refusal = this.picklistArrivalRefusal(manifests, { replaces: true, ownRefusals: 'all', act: options.act ?? 'install' }); - if (refusal) throw refusal; + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.refusePicklistArrival.call(picklistsHost.of(this), manifests, options); } /** @@ -11937,62 +5127,27 @@ export class SchemaRegistry { * judge its parts one by one, which would read a half-registered artifact. */ registeringPicklistArrival(register: () => T): T { - this.picklistArrivalDepth += 1; - try { - return register(); - } finally { - this.picklistArrivalDepth -= 1; - } + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return (PicklistsFacet.registeringPicklistArrival).call(picklistsHost.of(this), register); } /** {@link refusePicklistArrival}, then `register` inside {@link registeringPicklistArrival}. */ admitPicklistArrival(manifests: readonly unknown[], register: () => T, options: { act?: PicklistArrivalAct } = {}): T { - this.refusePicklistArrival(manifests, options); - return this.registeringPicklistArrival(register); + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return (PicklistsFacet.admitPicklistArrival).call(picklistsHost.of(this), manifests, register, options); } - /** - * [ADR-0150 D3, #31700; epic seat ruling R2-A, 6812862622] The per-item - * seams' half: one picklist, extension or object body registered under - * `packageId` on its own, after the seal and outside an arrival judged as a - * whole. Judged as an ADDITION to what the package holds - * (`replaces: false`): its installed items stay, and its identity and - * dependencies are the installed record's. Its own reference or extension - * is refused only when the resolver refuses it as ambiguous — the class two - * namespaces' holders open; a name nothing holds, or holds only outside the - * package's closure, keeps the seam's answer (served no options, named by - * the boot audit). What it does to everyone else's references is judged in - * full. ⛔ Never a registration with no package: the environment catalog - * (`sys_metadata`), whose stored row is not refused here. A tenant-authored - * list under a package is judged as any list of it (seat ruling R3-A); a - * tenant-authored object layer is not asked, as it writes no package's - * reference ({@link registerObject}). - */ private refusePicklistItemArrival(packageId: string | undefined, part: Record, subject: string): void { - if (!this.picklistVocabularySealed || this.picklistArrivalDepth > 0 || !packageId) return; - const refusal = this.picklistArrivalRefusal([{ id: packageId, ...part }], { - replaces: false, - ownRefusals: 'ambiguous', - act: 'register', - subject, - }); - if (refusal) throw refusal; + // [#33949 codemod] The body moved to `registry-picklists.ts`, its docblock with it (see `PicklistsHost`). + return PicklistsFacet.refusePicklistItemArrival.call(picklistsHost.of(this), packageId, part, subject); } - /** The refusal {@link judgePicklistArrival}'s verdict amounts to, or `undefined`. */ private picklistArrivalRefusal( manifests: readonly unknown[], options: { replaces: boolean; ownRefusals: 'all' | 'ambiguous'; act: PicklistArrivalAct; subject?: string }, ): Error | undefined { - const verdict = this.judgePicklistArrival(manifests, options); - return describeUnresolvedPicklistReferences(verdict.unresolved, verdict.orphans) - ?? describePicklistReferencesBrokenByInstall( - manifests.map((m) => artifactPackageId(m)).filter((id): id is string => id !== undefined), - verdict.references, - verdict.extensions, - verdict.disagreeing, - { act: options.act, subject: options.subject }, - ); + // [#33949 codemod] The body moved to `registry-picklists.ts`, its docblock with it (see `PicklistsHost`). + return PicklistsFacet.picklistArrivalRefusal.call(picklistsHost.of(this), manifests, options); } /** @@ -12171,170 +5326,56 @@ export class SchemaRegistry { * an outside-closure reference (ruling N2 on the E6 card). */ findDisagreeingPicklistReferences(): DisagreeingPicklistReference[] { - const disagreeing: DisagreeingPicklistReference[] = []; - for (const [fqn, contributors] of this.objectContributors) { - const referrerOf = picklistReferrerOf(contributors); - const seen = new Set(); - for (const contributor of contributors) { - if (contributor.ownership === 'overlay' || isTenantAuthored(contributor.definition)) continue; - for (const ref of collectPicklistReferences(fqn, contributor.definition, contributor.packageId)) { - const key = `${ref.field}\u0000${ref.picklist}`; - if (seen.has(key)) continue; - seen.add(key); - const referrers = referrerOf(ref.field, ref.picklist); - if (referrers.length < 2) continue; - const reached: { packageId: string | undefined; holder: PackageScopedHolder }[] = []; - for (const referrer of referrers) { - const verdict = this.picklistReferenceVerdict(ref.picklist, referrer); - if (verdict.status !== 'resolved') break; - reached.push({ packageId: referrer ?? undefined, holder: verdict.holder }); - } - if (reached.length < referrers.length) continue; - if (reached.every((r) => sameHolder(r.holder, reached[0].holder))) continue; - disagreeing.push({ object: fqn, field: ref.field, picklist: ref.picklist, referrers: reached }); - } - } - } - return disagreeing; + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.findDisagreeingPicklistReferences.call(picklistsHost.of(this)); } - /** The field half of the audit, split by the resolver's verdict (above). */ private auditPicklistReferences(): { unresolved: UnresolvedPicklistReference[]; outsideClosure: UnresolvedPicklistReference[]; } { - const unresolved: UnresolvedPicklistReference[] = []; - const outsideClosure: UnresolvedPicklistReference[] = []; - for (const [fqn, contributors] of this.objectContributors) { - for (const contributor of contributors) { - if (contributor.ownership === 'overlay' || isTenantAuthored(contributor.definition)) continue; - for (const ref of collectPicklistReferences(fqn, contributor.definition, contributor.packageId)) { - const judged = this.judgePicklistReference(ref.picklist, ref.packageId); - if (judged === 'resolved') continue; - const entry = judged.refusal ? { ...ref, refusal: judged.refusal.message } : ref; - (judged.outsideClosure ? outsideClosure : unresolved).push(entry); - } - } - } - return { unresolved, outsideClosure }; + // [#33949 codemod] The body moved to `registry-picklists.ts`, its docblock with it (see `PicklistsHost`). + return PicklistsFacet.auditPicklistReferences.call(picklistsHost.of(this)); } - /** - * One reference, judged: `'resolved'` when the name reaches a list from - * `callerPackageId`, else what it is — the refusal (when the resolver - * refused) and whether it refused because every holder is outside the - * caller's declared closure. - */ private judgePicklistReference( name: string, callerPackageId: string | undefined, ): 'resolved' | { refusal?: PackageScopedRefusal; outsideClosure: boolean } { - const verdict = this.picklistReferenceVerdict(name, callerPackageId); - if (verdict.status === 'resolved') return 'resolved'; - if (verdict.status !== 'refused') return { outsideClosure: false }; - return { refusal: verdict, outsideClosure: verdict.reason === 'outside-closure' }; + // [#33949 codemod] The body moved to `registry-picklists.ts`, its docblock with it (see `PicklistsHost`). + return PicklistsFacet.judgePicklistReference.call(picklistsHost.of(this), name, callerPackageId); } - /** - * The list `name` reaches from `callerPackageId` (its options, when that is - * one), followed by every extension of the SAME list. - * - * [ADR-0150 D3, child E6; epic seat ruling X on the E6 card, #31678] A - * `picklistExtensions` entry's `extend` is a CONTRIBUTION to another - * package's item, not a reference its package reads: it resolves - * caller-less through R — the environment's own list, else the unique - * package holder, else a refusal naming every holder — the treatment D3 - * gives an environment overlay. So an extension adds to the list its target - * names environment-wide, and to a field's list only when that is the same - * list. Loosens nothing (no first match is picked) and guesses nothing. - */ private picklistContributions(name: string, callerPackageId?: string | null): PicklistContribution[] { - const contributions: PicklistContribution[] = []; - const resolution = this.picklistReferenceVerdict(name, callerPackageId); - const base = resolution.status === 'resolved' ? resolution : undefined; - if (base) { - const item = base.item; - contributions.push({ - packageId: item?._packageId, - kind: 'picklist', - options: Array.isArray(item?.options) ? item.options : [], - }); - } - const extended = base ? this.picklistReferenceVerdict(name, null) : undefined; - if (base && !(extended?.status === 'resolved' && sameHolder(extended.holder, base.holder))) return contributions; - for (const entry of this.picklistExtensionContributions.get(name)?.values() ?? []) { - contributions.push({ packageId: entry.packageId, kind: 'extension', options: [...entry.options.values()] }); - } - return contributions; + // [#33949 codemod] The body moved to `registry-picklists.ts`, its docblock with it (see `PicklistsHost`). + return PicklistsFacet.picklistContributions.call(picklistsHost.of(this), name, callerPackageId); } - /** - * The list half of the additive rule: a picklist registered AFTER an - * extension already added one of its values is refused the same way, so the - * verdict does not depend on which package registered first. - */ private assertPicklistValuesDistinct( name: string, picklist: { options?: PicklistOptionLike[] }, packageId: string | undefined, ): void { - // The list is not registered yet, so no resolution reaches it: judged - // against every extension held for the name, as the additive rule reads. - const extensions: PicklistContribution[] = [...(this.picklistExtensionContributions.get(name)?.values() ?? [])] - .map((entry) => ({ packageId: entry.packageId, kind: 'extension' as const, options: [...entry.options.values()] })); - const duplicate = findDuplicatePicklistValue([ - { packageId, kind: 'picklist', options: Array.isArray(picklist?.options) ? picklist.options : [] }, - ...extensions, - ]); - if (duplicate) throw duplicatePicklistValueError(name, duplicate); + // [#33949 codemod] The body moved to `registry-picklists.ts`, its docblock with it (see `PicklistsHost`). + return PicklistsFacet.assertPicklistValuesDistinct.call(picklistsHost.of(this), name, picklist, packageId); } - /** - * Resolve every picklist-bound field of a body (by reference when there is - * none). [ADR-0150 D3, child E6] Each field's list is resolved from the - * package that WROTE that field's reference (`referrerOf`): the packaged - * layer whose own declaration carries it, else no package — a reference - * only an environment-authored body (a tenant overlay, a stored row) writes - * is the environment's reference, never the package's it customizes (D3). - */ private resolvePicklistFields(body: T, referrerOf: PicklistReferrer = NO_PACKAGE_REFERRER): T { - return resolvePicklistFieldsOnto(body, (picklist, field) => - this.resolveFieldPicklistOptions(picklist, referrerOf(field, picklist))); + // [#33949 codemod] The body moved to `registry-picklists.ts`, its docblock with it (see `PicklistsHost`). + return (PicklistsFacet.resolvePicklistFields).call(picklistsHost.of(this), body, referrerOf); } - /** - * [ADR-0150 D3, child E6; epic seat review on #31731] One field's list, when - * several packaged layers write its reference: each referrer's resolution - * is R's, and the field is served only when every one of them resolves to - * the SAME holder. Any other answer — one of them refused or reaching - * nothing, or two reaching different holders — serves no options, so the - * record validator refuses the field's values; the boot audit names each - * referrer whose own resolution refuses, with R's sentence - * ({@link findUnresolvedPicklistReferences}). ⛔ No referrer is preferred by - * fold order. - */ private resolveFieldPicklistOptions( picklist: string, referrers: readonly (string | null)[], ): PicklistOptionLike[] | undefined { - const [first, ...others] = referrers; - if (others.length > 0) { - const verdicts = referrers.map((referrer) => this.picklistReferenceVerdict(picklist, referrer)); - const [head] = verdicts; - if (head.status !== 'resolved') return undefined; - if (!verdicts.every((v) => v.status === 'resolved' && sameHolder(v.holder, head.holder))) return undefined; - } - return this.resolvePicklistOptions(picklist, first ?? null); + // [#33949 codemod] The body moved to `registry-picklists.ts`, its docblock with it (see `PicklistsHost`). + return PicklistsFacet.resolveFieldPicklistOptions.call(picklistsHost.of(this), picklist, referrers); } - /** Drop a package's `picklistExtensions`; `true` when it had any. */ private unregisterPicklistExtensionsByPackage(packageId: string): boolean { - let dropped = false; - for (const [target, byPackage] of this.picklistExtensionContributions) { - if (byPackage.delete(packageId)) dropped = true; - if (byPackage.size === 0) this.picklistExtensionContributions.delete(target); - } - return dropped; + // [#33949 codemod] The body moved to `registry-picklists.ts`, its docblock with it (see `PicklistsHost`). + return PicklistsFacet.unregisterPicklistExtensionsByPackage.call(picklistsHost.of(this), packageId); } // ========================================== @@ -12354,43 +5395,323 @@ export class SchemaRegistry { * name are invalidated. */ invalidate(fqnOrName: string): void { - this._objectRevision += 1; - if (this.mergedObjectCache.has(fqnOrName)) { - this.mergedObjectCache.delete(fqnOrName); - return; - } - // Short-name path: drop any cached merge whose FQN ends with `__` or equals ``. - const suffix = `__${fqnOrName}`; - for (const fqn of Array.from(this.mergedObjectCache.keys())) { - if (fqn === fqnOrName || fqn.endsWith(suffix)) { - this.mergedObjectCache.delete(fqn); - } - } + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.invalidate.call(picklistsHost.of(this), fqnOrName); } /** Drop every entry from the merged-schema cache. */ invalidateAll(): void { - this.mergedObjectCache.clear(); - this._objectRevision += 1; + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.invalidateAll.call(picklistsHost.of(this)); } /** * Clear all registry state. Use only for testing. */ reset(): void { - this.objectContributors.clear(); - // [#10945] The short-name index is derived from `objectContributors`; a - // reset that cleared only one of them would resolve names to keys that no - // longer exist. - this.objectKeysByShortName.clear(); - this.mergedObjectCache.clear(); - this.namespaceRegistry.clear(); - this.metadata.clear(); - this.appNavContributions.clear(); - this.picklistExtensionContributions.clear(); - this.residualOwnerDependencies.clear(); - this._objectRevision += 1; - this.log('[Registry] Reset complete'); + // [#33949 codemod] The body moved to `registry-picklists.ts` (see `PicklistsHost`). + return PicklistsFacet.reset.call(picklistsHost.of(this)); + } + + // [#33949 codemod] Installs each facet's host factory (`registry-facet-hosts.ts`): a static block adds + // no member to the class, so the published `.d.ts` does not move. + static { + appsHost.install((registry) => ({ + get appNavContributions() { return registry.appNavContributions; }, + applyNavContributions: (...a) => registry.applyNavContributions(...a), + findNavGroup: (...a) => registry.findNavGroup(...a), + isPackageDisabled: (...a) => registry.isPackageDisabled(...a), + listItems: (...a) => registry.listItems(...a), + log: (...a) => registry.log(...a), + get navGroupDiagnostics() { return registry.navGroupDiagnostics; }, + noteAppNavContributions: (...a) => registry.noteAppNavContributions(...a), + recordNavGroupMiss: (...a) => registry.recordNavGroupMiss(...a), + resolveItem: (...a) => registry.resolveItem(...a), + warn: (...a) => registry.warn(...a), + })); + bareNamesHost.install((registry) => ({ + get addressLiteralNamespaceWarned() { return registry.addressLiteralNamespaceWarned; }, + bareNameBundleAdditionMeets: (...a) => registry.bareNameBundleAdditionMeets(...a), + get bareNameClaims() { return registry.bareNameClaims; }, + bareNameHolderNamespace: (...a) => registry.bareNameHolderNamespace(...a), + bareNameHoldersOtherThan: (...a) => registry.bareNameHoldersOtherThan(...a), + get bareNameHoldersSealed() { return registry.bareNameHoldersSealed; }, + set bareNameHoldersSealed(v) { registry.bareNameHoldersSealed = v; }, + bareNamePackageHolders: (...a) => registry.bareNamePackageHolders(...a), + bareNameRenameFor: (...a) => registry.bareNameRenameFor(...a), + declaredBundleHolding: (...a) => registry.declaredBundleHolding(...a), + getAllPackages: (...a) => registry.getAllPackages(...a), + getPackage: (...a) => registry.getPackage(...a), + get metadata() { return registry.metadata; }, + noteBareNameClaim: (...a) => registry.noteBareNameClaim(...a), + registeredBundleHolding: (...a) => registry.registeredBundleHolding(...a), + })); + itemsHost.install((registry) => ({ + get bareNameClaims() { return registry.bareNameClaims; }, + getObject: (...a) => registry.getObject(...a), + invalidateAll: (...a) => registry.invalidateAll(...a), + log: (...a) => registry.log(...a), + get metadata() { return registry.metadata; }, + noteBareNameClaim: (...a) => registry.noteBareNameClaim(...a), + noteItem: (...a) => registry.noteItem(...a), + noteItemOrder: (...a) => registry.noteItemOrder(...a), + noteSecurityCatalogClaim: (...a) => registry.noteSecurityCatalogClaim(...a), + get openPackageBody() { return registry.openPackageBody; }, + set openPackageBody(v) { registry.openPackageBody = v; }, + orderedOverlayTier: (...a) => registry.orderedOverlayTier(...a), + get packageBodyItems() { return registry.packageBodyItems; }, + get restorePoint() { return registry.restorePoint; }, + get securityCatalogClaims() { return registry.securityCatalogClaims; }, + unregisterPicklistExtensionsByPackage: (...a) => registry.unregisterPicklistExtensionsByPackage(...a), + withdrawPackageBodyItemsNotShipped: (...a) => registry.withdrawPackageBodyItemsNotShipped(...a), + withdrawPackageClaimsNotDeclared: (...a) => registry.withdrawPackageClaimsNotDeclared(...a), + })); + namespacesAndClaimsHost.install((registry) => ({ + bareNameHolderNamespace: (...a) => registry.bareNameHolderNamespace(...a), + get defaultRecordPageClaims() { return registry.defaultRecordPageClaims; }, + defaultRecordPageSlots: (...a) => registry.defaultRecordPageSlots(...a), + forgetDefaultRecordPageClaims: (...a) => registry.forgetDefaultRecordPageClaims(...a), + log: (...a) => registry.log(...a), + get metadata() { return registry.metadata; }, + get namespaceRegistry() { return registry.namespaceRegistry; }, + noteDefaultRecordPageClaims: (...a) => registry.noteDefaultRecordPageClaims(...a), + noteNamespace: (...a) => registry.noteNamespace(...a), + noteSecurityCatalogClaim: (...a) => registry.noteSecurityCatalogClaim(...a), + otherDefaultRecordPageHolders: (...a) => registry.otherDefaultRecordPageHolders(...a), + packageHeldSecurityCatalogNames: (...a) => registry.packageHeldSecurityCatalogNames(...a), + get securityCatalogClaims() { return registry.securityCatalogClaims; }, + securityCatalogHoldersOtherThan: (...a) => registry.securityCatalogHoldersOtherThan(...a), + securityCatalogPackageHolders: (...a) => registry.securityCatalogPackageHolders(...a), + })); + objectRegistrationHost.install((registry) => ({ + get _objectRevision() { return registry._objectRevision; }, + set _objectRevision(v) { registry._objectRevision = v; }, + applyDeploymentTenancy: (...a) => registry.applyDeploymentTenancy(...a), + attachBoundActions: (...a) => registry.attachBoundActions(...a), + get bareNameHoldersAreSealed() { return registry.bareNameHoldersAreSealed; }, + boundCodeActions: (...a) => registry.boundCodeActions(...a), + boundServedActions: (...a) => registry.boundServedActions(...a), + debug: (...a) => registry.debug(...a), + declinedBaseLayerStamps: (...a) => registry.declinedBaseLayerStamps(...a), + get deploymentPlatformGlobalObjects() { return registry.deploymentPlatformGlobalObjects; }, + extensionTargetKey: (...a) => registry.extensionTargetKey(...a), + get fieldPatchMissesWarned() { return registry.fieldPatchMissesWarned; }, + finishServedObject: (...a) => registry.finishServedObject(...a), + foldExtenders: (...a) => registry.foldExtenders(...a), + foldExtendersOntoDefinition: (...a) => registry.foldExtendersOntoDefinition(...a), + followDoorSlotPriority: (...a) => registry.followDoorSlotPriority(...a), + getAllObjects: (...a) => registry.getAllObjects(...a), + getObject: (...a) => registry.getObject(...a), + getObjectContributors: (...a) => registry.getObjectContributors(...a), + getObjectOwner: (...a) => registry.getObjectOwner(...a), + hasDisabledPackage: (...a) => registry.hasDisabledPackage(...a), + isActionWithheldByDisabledPackage: (...a) => registry.isActionWithheldByDisabledPackage(...a), + isPackageDisabled: (...a) => registry.isPackageDisabled(...a), + judgeRegistrationDoors: (...a) => registry.judgeRegistrationDoors(...a), + log: (...a) => registry.log(...a), + materializeBaseLayer: (...a) => registry.materializeBaseLayer(...a), + get mergedObjectCache() { return registry.mergedObjectCache; }, + get metadata() { return registry.metadata; }, + get multiTenant() { return registry.multiTenant; }, + noteObjectEntry: (...a) => registry.noteObjectEntry(...a), + get objectContributors() { return registry.objectContributors; }, + openObjectEntry: (...a) => registry.openObjectEntry(...a), + packagedRules: (...a) => registry.packagedRules(...a), + rankedForPackage: (...a) => registry.rankedForPackage(...a), + recordCurrencyFollowRefusalOf: (...a) => registry.recordCurrencyFollowRefusalOf(...a), + get redeclaredFieldsWarned() { return registry.redeclaredFieldsWarned; }, + refusePicklistItemArrival: (...a) => registry.refusePicklistItemArrival(...a), + registerNamespace: (...a) => registry.registerNamespace(...a), + reportChangedRuleCopies: (...a) => registry.reportChangedRuleCopies(...a), + reportFieldPatchMisses: (...a) => registry.reportFieldPatchMisses(...a), + reportRedeclaredFields: (...a) => registry.reportRedeclaredFields(...a), + get reportedRuleCopyChanges() { return registry.reportedRuleCopyChanges; }, + resolveObject: (...a) => registry.resolveObject(...a), + resolveObjectKey: (...a) => registry.resolveObjectKey(...a), + resolvePicklistFields: (...a) => registry.resolvePicklistFields(...a), + resolveStorageObject: (...a) => registry.resolveStorageObject(...a), + get restorePoint() { return registry.restorePoint; }, + get searchCompanion() { return registry.searchCompanion; }, + servedFoldWithLayer: (...a) => registry.servedFoldWithLayer(...a), + get shadowedExtensionQueueWarned() { return registry.shadowedExtensionQueueWarned; }, + stripDeploymentTenancyFrom: (...a) => registry.stripDeploymentTenancyFrom(...a), + stripProvisionedPrimaryFrom: (...a) => registry.stripProvisionedPrimaryFrom(...a), + stripProvisionedSearchCompanionFrom: (...a) => registry.stripProvisionedSearchCompanionFrom(...a), + stripProvisionedTenantIndexFrom: (...a) => registry.stripProvisionedTenantIndexFrom(...a), + subtractExtenderContributions: (...a) => registry.subtractExtenderContributions(...a), + tenantAuthoredScalars: (...a) => registry.tenantAuthoredScalars(...a), + tenantPatchedDisplayKeys: (...a) => registry.tenantPatchedDisplayKeys(...a), + tenantRuleSettings: (...a) => registry.tenantRuleSettings(...a), + withPackagedOwnerRules: (...a) => registry.withPackagedOwnerRules(...a), + withholdDisabledExtensions: (...a) => registry.withholdDisabledExtensions(...a), + withoutExtensionQueueField: (...a) => registry.withoutExtensionQueueField(...a), + })); + packageAddressingHost.install((registry) => ({ + bareSlotEntriesOf: (...a) => registry.bareSlotEntriesOf(...a), + bundleMembersHeldBy: (...a) => registry.bundleMembersHeldBy(...a), + compositeHoldersOf: (...a) => registry.compositeHoldersOf(...a), + gateSlotBinding: (...a) => registry.gateSlotBinding(...a), + getAllObjects: (...a) => registry.getAllObjects(...a), + getObjectOwner: (...a) => registry.getObjectOwner(...a), + getPackage: (...a) => registry.getPackage(...a), + hasDisabledPackage: (...a) => registry.hasDisabledPackage(...a), + invalidateAll: (...a) => registry.invalidateAll(...a), + isBundleAddition: (...a) => registry.isBundleAddition(...a), + isFlowOfDisabledPackage: (...a) => registry.isFlowOfDisabledPackage(...a), + isObjectPackageDisabled: (...a) => registry.isObjectPackageDisabled(...a), + isPackageDisabled: (...a) => registry.isPackageDisabled(...a), + log: (...a) => registry.log(...a), + get metadata() { return registry.metadata; }, + noteItem: (...a) => registry.noteItem(...a), + get objectContributors() { return registry.objectContributors; }, + overlaySlotBinding: (...a) => registry.overlaySlotBinding(...a), + ownersHoldingAName: (...a) => registry.ownersHoldingAName(...a), + packagelessActionEntryToRetire: (...a) => registry.packagelessActionEntryToRetire(...a), + pairRowBinding: (...a) => registry.pairRowBinding(...a), + rankedForPackage: (...a) => registry.rankedForPackage(...a), + resolveObjectKey: (...a) => registry.resolveObjectKey(...a), + resolveOwnerLayer: (...a) => registry.resolveOwnerLayer(...a), + retirePackagelessActionSlot: (...a) => registry.retirePackagelessActionSlot(...a), + settledPairRowBinding: (...a) => registry.settledPairRowBinding(...a), + })); + packagesHost.install((registry) => ({ + announcePackageEnabled: (...a) => registry.announcePackageEnabled(...a), + announcePackageUninstalled: (...a) => registry.announcePackageUninstalled(...a), + get bareNameHoldersSealed() { return registry.bareNameHoldersSealed; }, + coOwnersOf: (...a) => registry.coOwnersOf(...a), + get collisionPolicy() { return registry.collisionPolicy; }, + debug: (...a) => registry.debug(...a), + forgetBareNameClaims: (...a) => registry.forgetBareNameClaims(...a), + forgetDefaultRecordPageClaims: (...a) => registry.forgetDefaultRecordPageClaims(...a), + forgetSecurityCatalogClaims: (...a) => registry.forgetSecurityCatalogClaims(...a), + getNamespaceOwners: (...a) => registry.getNamespaceOwners(...a), + getObjectOwner: (...a) => registry.getObjectOwner(...a), + getPackage: (...a) => registry.getPackage(...a), + get initialDisabledPackageIds() { return registry.initialDisabledPackageIds; }, + invalidateAll: (...a) => registry.invalidateAll(...a), + isPackageDisabled: (...a) => registry.isPackageDisabled(...a), + listItems: (...a) => registry.listItems(...a), + log: (...a) => registry.log(...a), + get metadata() { return registry.metadata; }, + get namespaceRegistry() { return registry.namespaceRegistry; }, + noteItem: (...a) => registry.noteItem(...a), + noteItemType: (...a) => registry.noteItemType(...a), + get objectContributors() { return registry.objectContributors; }, + get packageBodyItems() { return registry.packageBodyItems; }, + recordBareNameClaims: (...a) => registry.recordBareNameClaims(...a), + recordDefaultRecordPageClaims: (...a) => registry.recordDefaultRecordPageClaims(...a), + recordSecurityCatalogClaims: (...a) => registry.recordSecurityCatalogClaims(...a), + refuseBareNameConflicts: (...a) => registry.refuseBareNameConflicts(...a), + refuseCoOwnedObjectNameCollision: (...a) => registry.refuseCoOwnedObjectNameCollision(...a), + refuseDefaultRecordPageConflicts: (...a) => registry.refuseDefaultRecordPageConflicts(...a), + refuseNamespaceHolderConflicts: (...a) => registry.refuseNamespaceHolderConflicts(...a), + refuseNamespaceNameConflicts: (...a) => registry.refuseNamespaceNameConflicts(...a), + refuseNamespaceObjectNameClashes: (...a) => registry.refuseNamespaceObjectNameClashes(...a), + refuseSecurityCatalogNameConflicts: (...a) => registry.refuseSecurityCatalogNameConflicts(...a), + registerNamespace: (...a) => registry.registerNamespace(...a), + get residualNamespaceHolders() { return registry.residualNamespaceHolders; }, + get residualOwnerDependencies() { return registry.residualOwnerDependencies; }, + setPackageNavContributions: (...a) => registry.setPackageNavContributions(...a), + unregisterItemsByPackage: (...a) => registry.unregisterItemsByPackage(...a), + unregisterNamespace: (...a) => registry.unregisterNamespace(...a), + unregisterObjectsByPackage: (...a) => registry.unregisterObjectsByPackage(...a), + warnAddressLiteralNamespaceOnce: (...a) => registry.warnAddressLiteralNamespaceOnce(...a), + })); + picklistsHost.install((registry) => ({ + get _objectRevision() { return registry._objectRevision; }, + set _objectRevision(v) { registry._objectRevision = v; }, + get appNavContributions() { return registry.appNavContributions; }, + auditPicklistReferences: (...a) => registry.auditPicklistReferences(...a), + getAllPackages: (...a) => registry.getAllPackages(...a), + getEnvironmentScopedItem: (...a) => registry.getEnvironmentScopedItem(...a), + getPackage: (...a) => registry.getPackage(...a), + getPackageScopedItem: (...a) => registry.getPackageScopedItem(...a), + invalidateAll: (...a) => registry.invalidateAll(...a), + judgePicklistArrival: (...a) => registry.judgePicklistArrival(...a), + judgePicklistReference: (...a) => registry.judgePicklistReference(...a), + listItems: (...a) => registry.listItems(...a), + log: (...a) => registry.log(...a), + get mergedObjectCache() { return registry.mergedObjectCache; }, + get metadata() { return registry.metadata; }, + get namespaceRegistry() { return registry.namespaceRegistry; }, + notePicklistExtension: (...a) => registry.notePicklistExtension(...a), + get objectContributors() { return registry.objectContributors; }, + get objectKeysByShortName() { return registry.objectKeysByShortName; }, + packageScopedHoldersOf: (...a) => registry.packageScopedHoldersOf(...a), + get picklistArrivalDepth() { return registry.picklistArrivalDepth; }, + set picklistArrivalDepth(v) { registry.picklistArrivalDepth = v; }, + picklistArrivalRefusal: (...a) => registry.picklistArrivalRefusal(...a), + picklistContributions: (...a) => registry.picklistContributions(...a), + get picklistExtensionContributions() { return registry.picklistExtensionContributions; }, + picklistReferenceVerdict: (...a) => registry.picklistReferenceVerdict(...a), + get picklistVocabularySealed() { return registry.picklistVocabularySealed; }, + set picklistVocabularySealed(v) { registry.picklistVocabularySealed = v; }, + refusePicklistArrival: (...a) => registry.refusePicklistArrival(...a), + refusePicklistItemArrival: (...a) => registry.refusePicklistItemArrival(...a), + registeringPicklistArrival: (...a) => registry.registeringPicklistArrival(...a), + get residualOwnerDependencies() { return registry.residualOwnerDependencies; }, + residualOwnerDependenciesOf: (...a) => registry.residualOwnerDependenciesOf(...a), + resolveFieldPicklistOptions: (...a) => registry.resolveFieldPicklistOptions(...a), + resolvePicklistOptions: (...a) => registry.resolvePicklistOptions(...a), + })); + platformGlobalHost.install((registry) => ({ + get _objectRevision() { return registry._objectRevision; }, + set _objectRevision(v) { registry._objectRevision = v; }, + applyDeploymentTenancy: (...a) => registry.applyDeploymentTenancy(...a), + assertPackageUninstallable: (...a) => registry.assertPackageUninstallable(...a), + closeObjectEntry: (...a) => registry.closeObjectEntry(...a), + get deploymentPlatformGlobalObjects() { return registry.deploymentPlatformGlobalObjects; }, + set deploymentPlatformGlobalObjects(v) { registry.deploymentPlatformGlobalObjects = v; }, + deploymentWithholdsTenant: (...a) => registry.deploymentWithholdsTenant(...a), + get extensionSpellingWarned() { return registry.extensionSpellingWarned; }, + foldExtenders: (...a) => registry.foldExtenders(...a), + getObjectOwner: (...a) => registry.getObjectOwner(...a), + getPackagedObjectOwner: (...a) => registry.getPackagedObjectOwner(...a), + invalidateAll: (...a) => registry.invalidateAll(...a), + log: (...a) => registry.log(...a), + get mergedObjectCache() { return registry.mergedObjectCache; }, + get multiTenant() { return registry.multiTenant; }, + get objectContributors() { return registry.objectContributors; }, + get objectKeysByShortName() { return registry.objectKeysByShortName; }, + packagedRules: (...a) => registry.packagedRules(...a), + registerObject: (...a) => registry.registerObject(...a), + resolveObject: (...a) => registry.resolveObject(...a), + resolveObjectKey: (...a) => registry.resolveObjectKey(...a), + resolveObjectLeavingExtensionsOut: (...a) => registry.resolveObjectLeavingExtensionsOut(...a), + stripProvisionedTenantIndexFrom: (...a) => registry.stripProvisionedTenantIndexFrom(...a), + })); + restoreHost.install((registry) => ({ + get appNavContributions() { return registry.appNavContributions; }, + get bareNameClaims() { return registry.bareNameClaims; }, + get bareNameHoldersAreSealed() { return registry.bareNameHoldersAreSealed; }, + closeObjectEntry: (...a) => registry.closeObjectEntry(...a), + get defaultRecordPageClaims() { return registry.defaultRecordPageClaims; }, + followRefusalAsServed: (...a) => registry.followRefusalAsServed(...a), + getAllObjects: (...a) => registry.getAllObjects(...a), + getObject: (...a) => registry.getObject(...a), + invalidateAll: (...a) => registry.invalidateAll(...a), + get mergedObjectCache() { return registry.mergedObjectCache; }, + get metadata() { return registry.metadata; }, + get namespaceRegistry() { return registry.namespaceRegistry; }, + get objectContributors() { return registry.objectContributors; }, + get picklistExtensionContributions() { return registry.picklistExtensionContributions; }, + recordCurrencyFollowRefusalOf: (...a) => registry.recordCurrencyFollowRefusalOf(...a), + recordCurrencyFollowRefusalOfRun: (...a) => registry.recordCurrencyFollowRefusalOfRun(...a), + resolveObjectKey: (...a) => registry.resolveObjectKey(...a), + resolveOwnerLayer: (...a) => registry.resolveOwnerLayer(...a), + get restorePoint() { return registry.restorePoint; }, + set restorePoint(v) { registry.restorePoint = v; }, + get securityCatalogClaims() { return registry.securityCatalogClaims; }, + })); + storageHost.install((registry) => ({ + get _logLevel() { return registry._logLevel; }, + get initialDisabledPackageIds() { return registry.initialDisabledPackageIds; }, + set initialDisabledPackageIds(v) { registry.initialDisabledPackageIds = v; }, + get objectContributors() { return registry.objectContributors; }, + get objectKeysByShortName() { return registry.objectKeysByShortName; }, + get packageEnabledListeners() { return registry.packageEnabledListeners; }, + get packageUninstalledListeners() { return registry.packageUninstalledListeners; }, + })); } } From 0edc44064f33ec8485834208b114c836e574a339 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:09:27 +0000 Subject: [PATCH 03/12] test(objectql): re-key the injected-column reader rows the registry split moved Six READERS rows named registry.ts sites that the split moved, unedited, into registry-top-level.ts and registry-platform-global.ts. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ --- .../src/federated-injected-column-readers.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/objectql/src/federated-injected-column-readers.test.ts b/packages/objectql/src/federated-injected-column-readers.test.ts index 8606e3f6edf..86abe97ef94 100644 --- a/packages/objectql/src/federated-injected-column-readers.test.ts +++ b/packages/objectql/src/federated-injected-column-readers.test.ts @@ -443,15 +443,15 @@ const READERS: Record = { disposition: 'not-a-read', why: 'names the related object in refusal text', }, - 'registry.ts# :: organization_id': { + 'registry-top-level.ts# :: organization_id': { disposition: 'not-a-read', why: 'the tenant index the registry declares on a provisioned tenant column', }, - 'registry.ts# :: owning_business_unit_id': { + 'registry-top-level.ts# :: owning_business_unit_id': { disposition: 'not-a-read', why: 'the name the injection writes the ADR-0117 D1 anchor under', }, - 'registry.ts# :: queue': { + 'registry-top-level.ts# :: queue': { disposition: 'not-a-read', why: 'the name the injection writes the ADR-0142 D3 ownership-queue column under', }, @@ -494,17 +494,17 @@ const READERS: Record = { disposition: 'not-a-read', why: 'names the metadata type the boot refusal lists; it reads no row', }, - 'registry.ts#declaresTenantIndex :: organization_id': { + 'registry-top-level.ts#declaresTenantIndex :: organization_id': { disposition: 'not-a-read', why: 'reads an index declaration, not a row', }, - 'registry.ts#applyDeploymentTenancy :: organization_id': { + 'registry-platform-global.ts#applyDeploymentTenancy :: organization_id': { disposition: 'not-a-read', why: "drops the platform's own injected definition from a declared object's schema (ADR-0131 D7); " + 'it reads a definition, never a row', }, - 'registry.ts#setDeploymentPlatformGlobalObjects :: organization_id': { + 'registry-platform-global.ts#setDeploymentPlatformGlobalObjects :: organization_id': { disposition: 'not-a-read', why: 'tells an authored organization_id from the injection while re-planning registered schemas ' + From cee244cb3e8e4556665fefed5d0f62caec94d432 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:10:06 +0000 Subject: [PATCH 04/12] chore(objectql): register SchemaRegistry as a facade, and the registry split's changeset Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ --- .changeset/33949-registry-split.md | 10 +++ scripts/check-facade-delegation-only.mjs | 1 + scripts/facade-delegation-registry.json | 110 +++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 .changeset/33949-registry-split.md diff --git a/.changeset/33949-registry-split.md b/.changeset/33949-registry-split.md new file mode 100644 index 00000000000..823e91fab14 --- /dev/null +++ b/.changeset/33949-registry-split.md @@ -0,0 +1,10 @@ +--- +'@objectstack/objectql': patch +--- + +refactor(objectql): `registry.ts` is split into section modules by codemod; a move only, with the public exports unchanged + +Clause-②: no +Metadata-protocol: none + +`packages/objectql/src/registry.ts` (12,438 lines) is split by `scripts/codemod-split-registry.mjs`, the first pass of the `engine.ts` codemod, as ruled on #33949 (item 1, split 1 of 8). `SchemaRegistry` keeps every member's signature and docblock, and each moved method forwards in one call to a facet module (`registry-
.ts`). The top-level declarations ahead of the class move whole into `registry-top-level.ts`, and `registry.ts` re-exports every one it exported by name. The script proves that every moved body and declaration is byte-identical to the base, and that `registry.ts` still exports the same names, each a value or a type as before. No behaviour changes, and nothing an importer of `@objectstack/objectql` reads changes. diff --git a/scripts/check-facade-delegation-only.mjs b/scripts/check-facade-delegation-only.mjs index cce21f464ec..34db74f6813 100644 --- a/scripts/check-facade-delegation-only.mjs +++ b/scripts/check-facade-delegation-only.mjs @@ -194,6 +194,7 @@ const DECLARED_WATCH_HINTS = [ 'packages/metadata-protocol/src/protocol.ts', 'packages/objectql/src/engine.ts', 'packages/drivers/driver-sql/src/sql-driver.ts', + 'packages/objectql/src/registry.ts', ]; const CARD_REF = /^#[1-9][0-9]*$/; diff --git a/scripts/facade-delegation-registry.json b/scripts/facade-delegation-registry.json index 56d94f2beab..6171cf4ec55 100644 --- a/scripts/facade-delegation-registry.json +++ b/scripts/facade-delegation-registry.json @@ -24,6 +24,11 @@ "file": "packages/drivers/driver-sql/src/sql-driver.ts", "class": "SqlDriver", "card": "#30105" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "card": "#33949" } ], "exceptions": [ @@ -50,6 +55,111 @@ "member": "get supports", "card": "#30147", "reason": "accepted terminal exception (ruling 6687801899): codemod limit: inferred-return-type, a get accessor whose object type the base leaves inferred, with a docblock on each key (MOVED_ACCESSORS moves a getter whose type is written, #30147 phase 2a)" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "constructor", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): the first pass never moves a constructor (the engine moved its own in its fourth pass, #30020 R8); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "registeredBundleHolding", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "declaredBundleHolding", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "recordCurrencyFollowRefusalOf", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): its stub keeps the base signature byte-for-byte, and two parameter defaults are closures over `this` (`() => this.getAllObjects()`), which the gate reads as a computed default; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "registerObject", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): its stub keeps the base signature byte-for-byte, and the `priority` default is a conditional over `ownership`, which the gate reads as a computed default; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "[FIND_BOUND_ACTION_COLLISIONS]", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): not-identifier-name: a symbol-keyed method (the engine moved its own under R9, #30020); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "assertPackageUninstallable", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "registerItem", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): inferred-return-type: a public method whose return type is inferred, so moving it would change the published .d.ts; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "unregisterItem", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): inferred-return-type: a public method whose return type is inferred, so moving it would change the published .d.ts; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "settledPairRowBinding", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "isBundleAddition", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "resolveItem", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "refuseAmbiguousObjectLessAction", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): parameter-shadows: a parameter (`readStoredRows`) shadows a stub local; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "picklistReferenceVerdict", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + }, + { + "file": "packages/objectql/src/registry.ts", + "class": "SchemaRegistry", + "member": "judgePicklistArrival", + "card": "#33949", + "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" } ] } From 29dc77d897ee1499805dfa8d3c5a9ccc0765a653 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:33:18 +0000 Subject: [PATCH 05/12] fix(scripts): codemod-split-registry keeps the ADR-anchored declarations in registry.ts Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ --- scripts/codemod-split-registry.mjs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/scripts/codemod-split-registry.mjs b/scripts/codemod-split-registry.mjs index 038976dec46..c257103b04b 100644 --- a/scripts/codemod-split-registry.mjs +++ b/scripts/codemod-split-registry.mjs @@ -157,10 +157,13 @@ const HOSTS_CLASS = 'FacetHost'; * into names nobody reads. */ const PATH_READER_PINS = Object.freeze({ - // Empty since #30055: the six rows the #4918 query-options erasure guard - // held here (its per-file baseline names `registry.ts`, so a moved `as any` - // site would have red `pnpm lint`) retired once their sites were typed, and - // the fourth pass (`--members`) moved the six bodies. + // `pnpm check:adr-symbol-anchors` resolves each ADR's + // `packages/objectql/src/registry.ts#` anchor against THIS file's + // declarations and string tokens; `docs/adr/**` is a governed surface a + // split PR does not edit, so the anchored declarations stay here. + mergeObjectDefinitions: 'ADR-0029 and ADR-0146 anchor `registry.ts#mergeObjectDefinitions` (check:adr-symbol-anchors)', + isShareableNamespace: 'ADR-0130 and ADR-0141 anchor `registry.ts#isShareableNamespace` (check:adr-symbol-anchors)', + TENANT_SCOPE_INDEX: "ADR-0117 anchors `registry.ts#organization_id`, which resolves as the `'organization_id'` token this declaration carries (check:adr-symbol-anchors)", }); /** From 66b07ca7dc4800bcbe41b1712001ae9afce135e9 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:34:08 +0000 Subject: [PATCH 06/12] refactor(objectql): regenerate the registry split against fresh main node scripts/codemod-split-registry.mjs --write on registry.ts at 86350e125d, after the open PR that touched it merged. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ --- packages/objectql/src/registry-bare-names.ts | 4 +- .../src/registry-object-registration.ts | 16 +- .../src/registry-package-addressing.ts | 57 +- packages/objectql/src/registry-packages.ts | 4 +- .../objectql/src/registry-platform-global.ts | 6 +- packages/objectql/src/registry-top-level.ts | 556 +--------------- packages/objectql/src/registry.ts | 613 ++++++++++++++++-- 7 files changed, 635 insertions(+), 621 deletions(-) diff --git a/packages/objectql/src/registry-bare-names.ts b/packages/objectql/src/registry-bare-names.ts index 7ec1f2f4aac..0c1b4a1cecd 100644 --- a/packages/objectql/src/registry-bare-names.ts +++ b/packages/objectql/src/registry-bare-names.ts @@ -38,9 +38,9 @@ import { ITEM_KEY_DISCRIMINATORS } from '@objectstack/metadata-core'; import type { ObjectStackManifest } from '@objectstack/spec/kernel'; import { BARE_NAME_ONE_HOLDER_TYPES, BUILT_IN_BARE_NAMES, bareNameHolderKey, bareNameHoldersMeet, declaredBareNames, declaredBuiltInBoundActions, declaredBundleMembers, handlerOwnerPackageId, holdsBareName, isBareNameRegistryType, isBundleAdditionPair, isOverridableBaseline, suggestedBareNameRename } from './bare-name-one-holder.js'; import type { BareNameHolder, BareNameOneHolderType, BundleHolding } from './bare-name-one-holder.js'; -import { bundleBaseKey, isShareableNamespace } from './registry-top-level.js'; +import { bundleBaseKey } from './registry-top-level.js'; import type { BareNameConflict, BundleArrival } from './registry-top-level.js'; -import { BareNameConflictError } from './registry.js'; +import { BareNameConflictError, isShareableNamespace } from './registry.js'; import type { SchemaRegistry } from './registry.js'; /** diff --git a/packages/objectql/src/registry-object-registration.ts b/packages/objectql/src/registry-object-registration.ts index 690c43145d4..45501631cd4 100644 --- a/packages/objectql/src/registry-object-registration.ts +++ b/packages/objectql/src/registry-object-registration.ts @@ -57,13 +57,13 @@ import { recordCurrencyFollowRefusal, recordCurrencySummaryRefusal } from './rec import type { RecordCurrencyFollowRefusedError } from './record-currency-aggregate-door.js'; import { DEFAULT_EXTENDER_PRIORITY, DEFAULT_OVERLAY_PRIORITY, DEFAULT_OWNER_PRIORITY, NO_PACKAGE_REFERRER, - OBJECT_FOLD_SCALAR_KEYS, QUEUE_FIELD, admittedFieldPatches, applySystemFields, changedPackagedRuleKeys, computeFQN, + OBJECT_FOLD_SCALAR_KEYS, QUEUE_FIELD, admittedFieldPatches, changedPackagedRuleKeys, computeFQN, displayKeyOverridesPackagedBase, fieldNamesOf, fieldPatchYieldKey, indexKeysOf, isLeftOutExtension, - isPackagelessActionSlotKey, isPlainRecord, leavesExtensionsOut, mergeObjectDefinitions, mergeValidationsByName, - optionByValue, ownFieldNamesOf, ownFieldsTarget, parseOverlaySlotKey, parsePackageObjectActions, picklistReferrerOf, - provisionTenantScopeIndex, reconcileManagedApiMethods, recordFieldOf, ruleNamesOf, sortActionsByOrder, - stableStringify, storedRowPackageOf, tenantRuleSettingsOf, validationRuleName, warnFunctionalCompleteness, - warnStrippedLegacyApiMethods, withTenantRuleSettings, withoutWithheldMembers, + isPackagelessActionSlotKey, isPlainRecord, leavesExtensionsOut, mergeValidationsByName, optionByValue, + ownFieldNamesOf, ownFieldsTarget, parseOverlaySlotKey, parsePackageObjectActions, picklistReferrerOf, + reconcileManagedApiMethods, recordFieldOf, ruleNamesOf, sortActionsByOrder, stableStringify, storedRowPackageOf, + tenantRuleSettingsOf, validationRuleName, warnFunctionalCompleteness, warnStrippedLegacyApiMethods, + withTenantRuleSettings, withoutWithheldMembers, } from './registry-top-level.js'; import type { BundleEntry, FoldedExtensionFields, FollowDoorLayer, ObjectContributor, ObjectExtensionBody, @@ -71,8 +71,8 @@ import type { UnboundActionTarget, WithheldExtensions, } from './registry-top-level.js'; import { - ObjectOwnershipConflictError, ValidationRuleCopyRefusedError, extendContributionRefusal, objectFieldTypeRefusal, - objectQueueColumnRefusal, + ObjectOwnershipConflictError, ValidationRuleCopyRefusedError, applySystemFields, extendContributionRefusal, + mergeObjectDefinitions, objectFieldTypeRefusal, objectQueueColumnRefusal, provisionTenantScopeIndex, } from './registry.js'; import type { SchemaRegistry } from './registry.js'; diff --git a/packages/objectql/src/registry-package-addressing.ts b/packages/objectql/src/registry-package-addressing.ts index ae672564e9b..eb272dc3615 100644 --- a/packages/objectql/src/registry-package-addressing.ts +++ b/packages/objectql/src/registry-package-addressing.ts @@ -6,10 +6,10 @@ * `ownersHoldingAName`, `compositeHoldersOf`, `packageScopedHoldersOf`, * `bundleMembersHeldBy`, `gateSlotBinding`, `getPackageScopedItem`, * `getEnvironmentScopedItem`, `getEnvironmentScopedActions`, `readsByPackage`, - * `bareSlotEntriesOf`, `rankedForPackage`, `removePackageOverlaySlots`, - * `orderedOverlayTier`, `getArtifactItem`, `removeRuntimeShadow`, - * `removeOverlayEntry`, `listItems`, `isPackageDisabled`, - * `isObjectPackageDisabled`, `isFlowOfDisabledPackage`, + * `readBareDoc`, `bareSlotEntriesOf`, `rankedForPackage`, + * `removePackageOverlaySlots`, `orderedOverlayTier`, `getArtifactItem`, + * `removeRuntimeShadow`, `removeOverlayEntry`, `listItems`, + * `isPackageDisabled`, `isObjectPackageDisabled`, `isFlowOfDisabledPackage`, * `isActionWithheldByDisabledPackage`, `hasDisabledPackage`, * `getRegisteredTypes` moved out of `registry.ts` whole by * `scripts/codemod-split-registry.mjs`. @@ -41,7 +41,10 @@ import { ITEM_KEY_DISCRIMINATORS, readDiscriminatorValue as discriminatorValue, isCodeArtifactBody, isTenantAuthored } from '@objectstack/metadata-core'; import { customizationBasePackageId } from '@objectstack/spec/kernel'; +import { PackageScopedNameRefusedError, packageScopedRefusalMessage } from '@objectstack/core'; +import type { PackageScopedResolvableType } from '@objectstack/core'; import { isPackageScopedReadType } from './package-scoped-read.js'; +import type { PackageScopedRefusal } from './package-scoped-resolver.js'; import { OVERLAY_SLOT_MARK, bundleBaseKey, collectBundle, entriesOfMember, overlaySlotEntriesOf, packagelessActionSlotEntriesOf, parseOverlaySlotKey, pickBundleMember, storedRowPackageOf, @@ -65,8 +68,10 @@ export interface PackageAddressingHost { compositeHoldersOf: SchemaRegistry['compositeHoldersOf']; gateSlotBinding: SchemaRegistry['gateSlotBinding']; getAllObjects: SchemaRegistry['getAllObjects']; + getItem: SchemaRegistry['getItem']; getObjectOwner: SchemaRegistry['getObjectOwner']; getPackage: SchemaRegistry['getPackage']; + getPackageScopedItem: SchemaRegistry['getPackageScopedItem']; hasDisabledPackage: SchemaRegistry['hasDisabledPackage']; invalidateAll: SchemaRegistry['invalidateAll']; isBundleAddition: SchemaRegistry['isBundleAddition']; @@ -79,6 +84,7 @@ export interface PackageAddressingHost { readonly objectContributors: SchemaRegistry['objectContributors']; overlaySlotBinding: SchemaRegistry['overlaySlotBinding']; ownersHoldingAName: SchemaRegistry['ownersHoldingAName']; + packageScopedHoldersOf: SchemaRegistry['packageScopedHoldersOf']; packagelessActionEntryToRetire: SchemaRegistry['packagelessActionEntryToRetire']; pairRowBinding: SchemaRegistry['pairRowBinding']; rankedForPackage: SchemaRegistry['rankedForPackage']; @@ -428,6 +434,49 @@ export class PackageAddressingFacet { return isPackageScopedReadType(type); } + /** + * [ADR-0150 D1 bucket 1, ruling 6893582352 option A] The by-name read of a + * `doc` that names no package. `doc` keeps its literal global name and its + * addressing (bucket 1); what changes is the context-free read's answer + * when two or more ENABLED packages hold the name: a `409 + * RESOURCE_CONFLICT` naming every holder (`ambiguous-in-environment`), in + * place of {@link getItem}'s first composite, which is a registration-order + * pick (ADR-0146 D8; the bare save, delete, publish and layered read refuse + * the same address already). + * + * Unchanged: one holder; a package-less bare-slot entry (the environment's + * own item) answers first, as {@link getItem} serves it; a disabled package + * holds no name ({@link packageScopedHoldersOf}), so beside one enabled + * holder the read serves that holder's item, never the disabled one's. + * `MetadataFacade.get` serves this answer; the protocol's by-name read asks + * it before any layer for its refusal only (`protocol-meta-item-reads.ts`). + * + * @throws {PackageScopedNameRefusedError} two or more enabled holders. + */ + static readBareDoc(this: Pick, name: string, member?: string): T | undefined { + const bare = this.metadata.get('doc')?.get(name) as { _packageId?: unknown } | undefined; + const stamp = bare?._packageId; + if (bare !== undefined && (typeof stamp !== 'string' || stamp === '' || stamp === 'sys_metadata')) { + return this.getItem('doc', name, undefined, member); + } + const served = this.packageScopedHoldersOf('doc', name); + if (served.length >= 2) { + const base: Omit = { + status: 'refused', + reason: 'ambiguous-in-environment', + type: 'doc' as PackageScopedResolvableType, + name, + callerPackageId: null, + candidates: served.map((packageId) => ({ kind: 'package' as const, packageId })), + }; + throw new PackageScopedNameRefusedError({ ...base, message: packageScopedRefusalMessage(base) }); + } + if (served.length === 1 && this.packageScopedHoldersOf('doc', name, null, { includeDisabledPackages: true }).length > 1) { + return this.getPackageScopedItem('doc', name, served[0], member); + } + return this.getItem('doc', name, undefined, member); + } + /** * The bare-slot entries of `(type, name)`: one, or a bundle's members, canonical first. * [#33127, ADR-0150 D4] …then its package overlay slots ({@link overlaySlotKey}): every diff --git a/packages/objectql/src/registry-packages.ts b/packages/objectql/src/registry-packages.ts index 307544795d3..2a533b9339b 100644 --- a/packages/objectql/src/registry-packages.ts +++ b/packages/objectql/src/registry-packages.ts @@ -42,12 +42,12 @@ import { artifactPackageId, declaredDependencyIds } from '@objectstack/core'; import { isNamespaceJudged, namespaceNameConflicts, namespacePackageOf } from './namespace-co-owner-names.js'; import type { NamespacePackage } from './namespace-co-owner-names.js'; import { - computeFQN, declaredOwnedObjectNames, isShareableNamespace, toRecordManifest, withDeclaredFunctionEntries, + computeFQN, declaredOwnedObjectNames, toRecordManifest, withDeclaredFunctionEntries, } from './registry-top-level.js'; import type { ArtifactInstallScope, CoOwnershipSource } from './registry-top-level.js'; import { ArtifactObjectNameConflictError, NamespaceAddressLiteralError, NamespaceConflictError, NamespaceNameConflictError, - NamespaceObjectNameConflictError, + NamespaceObjectNameConflictError, isShareableNamespace, } from './registry.js'; import type { SchemaRegistry } from './registry.js'; diff --git a/packages/objectql/src/registry-platform-global.ts b/packages/objectql/src/registry-platform-global.ts index 653b6a4fa83..aa4f194526f 100644 --- a/packages/objectql/src/registry-platform-global.ts +++ b/packages/objectql/src/registry-platform-global.ts @@ -43,11 +43,9 @@ import { isInjectedColumnDefinition, provisionPrimary, resolveInjectedSystemColumns } from '@objectstack/spec/data'; import type { ServiceObject } from '@objectstack/spec/data'; import { TENANT_SCOPE_FIELD_DEF, isCodeArtifactBody } from '@objectstack/metadata-core'; -import { - DEFAULT_EXTENDER_PRIORITY, TENANT_SCOPE_INDEX, provisionTenantScopeIndex, stableStringify, withoutField, -} from './registry-top-level.js'; +import { DEFAULT_EXTENDER_PRIORITY, stableStringify, withoutField } from './registry-top-level.js'; import type { ObjectContributor, ObjectExtensionFoldOptions } from './registry-top-level.js'; -import { extendContributionRefusal } from './registry.js'; +import { TENANT_SCOPE_INDEX, extendContributionRefusal, provisionTenantScopeIndex } from './registry.js'; import type { SchemaRegistry } from './registry.js'; /** diff --git a/packages/objectql/src/registry-top-level.ts b/packages/objectql/src/registry-top-level.ts index 363aa49d44c..edc619f94ea 100644 --- a/packages/objectql/src/registry-top-level.ts +++ b/packages/objectql/src/registry-top-level.ts @@ -11,9 +11,9 @@ // re-exports, by name, every declaration here it exported before, so every // importer of `./registry.js` reads what it read. -import { AUDIT_PROVENANCE_FIELDS, BASE_FIELD_DISPLAY_WHITELIST, LEGACY_API_METHODS, MANAGED_BY_WRITE_LOCKED_BUCKETS, checkManagedApiMethodAffordances, isTenancyDisabled, resolveInjectedSystemColumns } from '@objectstack/spec/data'; +import { BASE_FIELD_DISPLAY_WHITELIST, LEGACY_API_METHODS, MANAGED_BY_WRITE_LOCKED_BUCKETS, checkManagedApiMethodAffordances, isTenancyDisabled, resolveInjectedSystemColumns } from '@objectstack/spec/data'; import type { ObjectExtensionParsed, ObjectOwnership, ServiceObject } from '@objectstack/spec/data'; -import { AUDIT_FIELD_DEFS, AUDIT_FIELD_GOVERNANCE, OWNER_FIELD_DEF, OWNING_BUSINESS_UNIT_FIELD_DEF, QUEUE_FIELD_DEF, TENANT_SCOPE_FIELD_DEF, readDiscriminatorValue as discriminatorValue, isTenantAuthored } from '@objectstack/metadata-core'; +import { readDiscriminatorValue as discriminatorValue, isTenantAuthored } from '@objectstack/metadata-core'; import type { FieldPatchTargetMiss } from '@objectstack/metadata-core'; import { scalarOverridesPackagedBase } from '@objectstack/spec/system'; import type { SystemFieldName } from '@objectstack/spec/system'; @@ -799,133 +799,6 @@ function withoutWithheldMembers(base: ServiceObject, withheld: WithheldExtension return out; } -/** - * Deep merge two ServiceObject definitions. - * - * The merge set is CLOSED and enumerable — this is NOT "every prop, later - * value wins": - * - `fields` and `indexes` are merged ADDITIVELY (`fields` keyed by name). - * - `validations` are merged BY NAME ({@link mergeValidationsByName}, - * [ADR-0029 D9.2a, 2026-10-30 note]): rules of different names add, and a - * rule of a name already folded replaces it — with `tenantRules`, carrying - * the tenant's `message` and `active`, never the tenant's condition. - * - The three {@link OBJECT_FOLD_SCALAR_KEYS} (`label`, `pluralLabel`, - * `description`) are overridden last-writer-wins, subject to the - * `tenantAuthored` yield rule below. [#30391] A `label` that renames the - * object without a `pluralLabel` beside it also takes the running - * `pluralLabel` off: the plural named the old label. - * - `fieldPatches` [ADR-0146 D10, second rule] is APPLIED, never carried: - * after the merges above, each patch writes its `BASE_FIELD_DISPLAY_WHITELIST` - * keys onto the named field and its option labels onto the options whose - * stored `value` it names, each key yielding where `tenantPatched` says the - * tenant authored it — the same D9.2a rule as the scalars. A patch reaches - * the owner's own fields only (`ownFields`): one naming any other field, - * a field an extension adds included, is skipped here; the fold names it. - * - * **Every other top-level prop on `extension` is silently discarded.** - * `merged` starts as `{ ...base }` and nothing outside the list above is ever - * copied onto it — a caller that hands this function an extension body - * carrying an undeclared key, e.g. `tenancy: { enabled: false }`, gets a - * valid-but-inert no-op HERE: no error, no warning from this function, the - * base's existing value simply wins as if the extension had never named the - * key (issue #12680). - * - * Declarative authors never reach that silence, though: `ObjectExtensionSchema` - * (`packages/spec/src/data/object.zod.ts`) is `.strict()` — its shape is - * exactly `extend` / `fields` / `fieldPatches` / `label` / `pluralLabel` / - * `description` / `validations` / `indexes` / `priority`, so an `objectExtensions` entry - * naming `tenancy`, `permissions`, or any other key outside that list fails - * LOUDLY at authoring time with a prescription (#4001; the same rule is - * documented at `content/docs/data-modeling/object-extensions.mdx`). This - * function's discard is reachable only by a caller that bypasses that - * schema — a direct, programmatic `registry.registerObject(def, pkg, - * undefined, 'extend', …)` call, which is how this package's own pin - * (`registry-object-extension-nonenumerated-prop-discard.test.ts`) exercises - * the rule. Extender-writable `tenancy`/`permissions` would in any case be a - * separate, much bigger decision that has NOT been made. - * - * Unlike `tenancy`/`permissions`, this one is not blocked by schema: - * `_provenance` is a real top-level prop an `extend` contributor carries - * TODAY. Copying it through (as "later value - * wins" would) lets a third-party extender flip a tenant-authored object's - * `_provenance` to the extending package's — reaching, by a different route, - * the exact outcome ADR-0029 D9.3's priority-reranking guard exists to - * prevent. `registry-object-overlay-layer.test.ts` ("an extender declaring - * priority 140 does not become the base layer") fails if this merge set is - * widened to copy it through. - * - * [ADR-0029 D9.2a] …the SCALAR override above is conditional. `tenantAuthored` names - * the scalars the fold's BASE has authored away from the packaged owner's - * value; an extender yields on those. See - * {@link SchemaRegistry.tenantAuthoredScalars} for why the set is computed once - * over the base rather than re-derived from the running `merged`; `tenantRules` - * ({@link SchemaRegistry.tenantRuleSettings}) is computed once for the same reason. - */ -function mergeObjectDefinitions( - base: ServiceObject, - extension: ObjectExtensionBody, - tenantAuthored?: ReadonlySet, - tenantRules?: ReadonlyMap, - tenantPatched?: ReadonlySet, - ownFields?: ReadonlySet, -): ServiceObject { - const merged = { ...base }; - - // Merge fields additively - if (extension.fields) { - merged.fields = { ...base.fields, ...extension.fields }; - } - - // [ADR-0029 D9.2a, 2026-10-30 note] Merge validations by name - if (extension.validations) { - merged.validations = mergeValidationsByName( - base.validations, - extension.validations, - tenantRules, - ) as ServiceObject['validations']; - } - - // Merge indexes additively - if (extension.indexes) { - merged.indexes = [...(base.indexes || []), ...extension.indexes]; - } - - // Override scalar props (last writer wins) — [ADR-0029 D9.2a] unless the base has been - // authored by the tenant, in which case the extender's packaged default yields. - const yields = (key: ObjectFoldScalarKey): boolean => tenantAuthored?.has(key) === true; - if (extension.label !== undefined && !yields('label')) { - // [#30391] `pluralLabel` is the plural OF `label`: an extension that renames - // the object and declares no plural leaves the running plural naming the - // old label, so the object is served as one that declares no - // `pluralLabel`. A plural the tenant authored stays (the D9.2a yield), and - // an extension that repeats the label it extends renames nothing. - if (extension.pluralLabel === undefined && extension.label !== merged.label && !yields('pluralLabel')) { - delete merged.pluralLabel; - } - merged.label = extension.label; - } - if (extension.pluralLabel !== undefined && !yields('pluralLabel')) { - merged.pluralLabel = extension.pluralLabel; - } - if (extension.description !== undefined && !yields('description')) { - merged.description = extension.description; - } - - // [ADR-0146 D10, second rule] The extension's display patches of the - // owner's own fields (`ownFields`) — never of a field an extension adds, - // whatever the priorities, or the answer would depend on which extender the - // fold reached first — applied AFTER the additive merges, with each key - // yielding where the tenant authored it (`tenantPatched`, ADR-0029 D9.2a), - // as the scalars above yield. Two extenders patching one key resolve as the - // scalars do: the fold applies contributors in ascending `priority`, so the - // higher one is written last. - if (extension.fieldPatches) { - merged.fields = applyFieldPatches(merged.fields, extension.fieldPatches, tenantPatched, ownFields); - } - - return merged; -} - /** * [#8027] Key-order-insensitive JSON, for comparing two `indexes` entries, or * two validation rules' keys, BY VALUE — see @@ -1158,402 +1031,6 @@ const OWNING_BUSINESS_UNIT_FIELD: typeof SystemFieldName.OWNING_BUSINESS_UNIT_ID /** [ADR-0142 D3] The injected ownership-queue column, annotated the same way. */ const QUEUE_FIELD: typeof SystemFieldName.QUEUE = 'queue'; -export function applySystemFields( - schema: ServiceObject, - opts: { - multiTenant: boolean; - /** - * [ADR-0131 D7] The objects THIS deployment declares platform-global — the - * plan's one deployment input. A declared object gets no `organization_id` - * here. Omitted or empty ⇒ the plan is byte-identical to the authored one. - */ - platformGlobalObjects?: ReadonlySet; - } -): ServiceObject { - // WHICH columns this object carries is the spec's derivation - // (`resolveInjectedSystemColumns`, #5378) — one answer shared with every - // author-time consumer that must resolve a reference to an injected column - // but cannot load this runtime (`@objectstack/lint`). This function keeps sole - // ownership of WHAT each column looks like, the same split #3786 established - // for the audit family. Do NOT re-derive a condition below: a second copy here - // is exactly the drift the plan exists to prevent. - // - // [#7865] This pass runs for `external` (ADR-0015) objects too — DELIBERATELY - // (maintainer ruling 2026-08-12, direction B). The platform provisions no - // storage for a federated object (`syncObjectSchema` returns early, no DDL), - // so the anchors injected below are registered-but-unprovisioned there; the - // machine-readable marker for that fact is `resolveInjectedColumnProvenance` - // / `unprovisionedInjectedColumns` (re-exported above from - // `@objectstack/metadata-core`), NOT a key on the definitions this function - // spreads — the definitions must stay byte-identical to the shipped tables, - // because the #7859 Layer-0 guard and the #4326 round-trip strip both read - // them by exact identity. Do not add keys here; consumers ask the API. - // - // [ADR-0131 D7] …and the deployment's platform-global declaration is the - // plan's one deployment input: a declared object is planned with no tenant - // column, so nothing below injects `organization_id` for it. Passed only when - // a deployment declared something, so every other call is the one-argument - // plan, byte for byte. - const plan = - opts.platformGlobalObjects && opts.platformGlobalObjects.size > 0 - ? resolveInjectedSystemColumns(schema, { platformGlobalObjects: opts.platformGlobalObjects }) - : resolveInjectedSystemColumns(schema); - - // 1. Hard opt-out at object level (e.g. seed/migration tables). - // Folded into the plan (`systemFields: false` ⇒ every flag false), so the - // cheap path stays cheap without a second reading of the key. - // - // 2. Skip only `better-auth` managed tables. Their column layout is - // driven by better-auth's own migrations (sys_user, sys_session, - // sys_organization, …) and injecting extra columns here would - // collide with what better-auth expects. Other `managedBy` buckets - // (`platform`, `config`, `system`, `append-only`) all need the - // tenant + audit columns for multi-tenant isolation and time-travel - // history — withholding them silently broke RLS reads on - // sys_audit_log / sys_activity (the SecurityPlugin's - // field-existence safety net dropped `organization_id = - // current_user.organization_id` as "field missing", producing - // RLS_DENY_FILTER → 0 rows for every non-admin caller). - // Both opt-outs are the plan's rows 1-2; the bail below covers them. - // - // Honor explicit opt-out via either `systemFields.tenant === false` - // OR `tenancy.enabled === false`. The latter is the schema-level - // declaration that the table is a shared/global catalog (e.g. - // sys_package — the Marketplace registry). Without this, the - // registry would still inject `organization_id`, and the - // SecurityPlugin's RLS layer would filter every cross-org read down - // to 0 rows even though the schema explicitly disabled multi-tenancy. - // - // The `organization_id` COLUMN is provisioned unconditionally (subject only - // to the explicit opt-outs above) — its existence no longer depends on the - // global multi-tenant flag. Decoupling "does the column exist" from "is - // tenancy enabled" is what stops sudo writers (audit / messaging / inbox / - // outbox …) from failing with "no column named organization_id" on - // single-tenant stacks: they can always stamp the column, and org-scoping - // stamps it on insert (under `single` with the Default Organization), so it - // is never NULL (ADR-0131 D1). The multi-tenant flag now governs only - // whether the column is INDEXED — on a single-tenant DB nothing ever filters - // by organization, so the index would be dead weight. That is also why the - // plan takes no `multiTenant` input: the flag cannot change what EXISTS, only - // what is indexed, so an author-time consumer needs no runtime context. - const wantTenant = plan.tenant; - const wantAudit = plan.audit; - - // Ownership is auto-provisioned by DEFAULT on user-authored business - // objects (correct-by-default for AI authors). It is withheld only where a - // per-record owner is meaningless: any platform-managed table (`managedBy` - // is set — config/append-only/system/platform; `better-auth` already - // returned above), the `sys_*` namespace, or an explicit opt-out via - // `ownership: 'business_unit' | 'org' | 'none'` on the schema (Dataverse-style - // — catalog / junction tables, plus D1's unit-owned tier, which takes the BU - // anchor instead). Note this is the SAFE default direction: forgetting the - // opt-out leaves a harmless spare column, whereas the old opt-IN model let - // authors silently ship objects with no working ownership at all. - // `ownership` is a declared ObjectSchema field (record-ownership model), read - // off the typed schema by the plan — no `as any` (#3175). - // - // [ADR-0117 D1 / #5677] The plan treats the value as a `string`, not the - // enum, on purpose: it is also called on pre-parse input. That widening is - // what let the engine recognise D1's fourth tier `'business_unit'` BEFORE the - // schema could emit it — the ordering #5677 → #5678 exists to guarantee, since - // a tier the schema emits first would be judged by a no-overlap literal test - // and get the INVERSE of what D1 declares. As of #5678 the spec enum reads - // `'user' | 'business_unit' | 'org' | 'none'` and the tier is authorable. - // - // [ADR-0117 D1 / #5677] The ownership decision is a POSITIVE LIST, deliberately - // — it used to read `ownership !== 'org' && ownership !== 'none'`, i.e. a - // DENY-list, so ANY value outside the two exclusions fell through to "inject - // `owner_id`". That default was safe only while the enum had exactly three - // members: D1's `business_unit` tier means "owned by a UNIT, not a person" - // (`owner_id` ❌, `owning_business_unit_id` ✅), and under the deny-list it - // would have been stamped with `owner_id` — the exact inverse. The same - // argument applies to any FIFTH tier a later ADR adds, which is why the list - // stays positive rather than being "completed" to the current enum. - // - // The `sys_*` / `managedBy` ineligibility and the per-tier table - // - // ownership owner_id owning_business_unit_id - // undefined/user ✅ ✅ - // business_unit ❌ ✅ - // org ❌ ❌ - // none ❌ ❌ - // - // all live in `resolveInjectedSystemColumns` now, so the author-time linter - // reaches the same verdict instead of guessing (#5378). - const wantOwner = plan.owner; - const wantOwningBusinessUnit = plan.owningBusinessUnit; - // [ADR-0142 D3] The queue column rides with the owner anchor (the plan's - // `queue` is its `owner`): "sits in Q" is `queue == Q AND owner_id IS NULL`. - const wantQueue = plan.queue; - - // Nothing to INJECT — the two opt-out rows (`systemFields: false`, - // `managedBy: 'better-auth'`) and objects whose every column is already - // declared. - // - // [#8608] It still routes through {@link provisionTenantScopeIndex}, like - // every other exit below. An object that injects nothing can still CARRY the - // tenant column — the author declares their own `organization_id` — and the - // wall composes its predicate on exactly that column (measured, not inferred: - // plugin-security's `tenancyDisabled` reads `tenancy.enabled` and - // `systemFields.tenant` only, so `systemFields: false` leaves the wall live). - // Returning `schema` here was the last exit that decided the index by NOT - // asking, which is how the withholding survived #8375's convergence. - if (!wantTenant && !wantAudit && !wantOwner && !wantOwningBusinessUnit && !wantQueue) { - return provisionTenantScopeIndex(schema, opts); - } - - const additions: Record = {}; - // Platform-owned field settings that must WIN over a declared field, rather - // than lose to it like `additions` does (#4447). - const overrides: Record = {}; - - if (wantTenant && !schema.fields?.organization_id) { - // [#6562] The authorable shape is the shared table's, spread verbatim. - // - // [#6810] Nothing is spread ON TOP of it any more. This line used to read - // `{ ...TENANT_SCOPE_FIELD_DEF, indexed: opts.multiTenant }`, and `indexed` - // is the one key that was never a `FieldSchema` key at all — removed in the - // 16.x line (#2377, ADR-0049) because a field-level index flag built no - // index, and `FieldSchema` is a `strictObject`, so a document carrying it is - // rejected BY NAME ("never a FieldSchema key; a field-level index flag built - // no index"). #6562's reasoning for leaving it here — that its only consumer - // reads the REGISTERED schema, never a served document — held for the - // consumer and not for the document: `registerObject` runs this function - // BEFORE storing and `getItem('object', …)` serves that post-injection - // document, so the key reached `/meta`, where `decorateMetadataItem` - // re-parsed the served body and stamped `_diagnostics: { valid: false, - // errors: [{ path: 'fields.organization_id', code: 'unrecognized_keys' }] }` - // on EVERY registry-backed object — both tenancy modes, both read exits. A - // defect report the platform wrote about its own column, on a document the - // author never wrote and could not fix, in the channel Studio renders - // invalid-metadata banners from. - additions.organization_id = { ...TENANT_SCOPE_FIELD_DEF }; - - // [#6810] The tenant INDEX that goes with this column is no longer decided - // here. It is {@link provisionTenantScopeIndex}, called on the merged result - // at the tail of this function — see #8375 for why a decision spelled out at - // this one site could not be reached by the `/meta` read exits. - } - - if (wantAudit) { - for (const name of AUDIT_PROVENANCE_FIELDS) { - const declared = (schema.fields as Record | undefined)?.[name]; - if (!declared) { - additions[name] = AUDIT_FIELD_DEFS[name]; - continue; - } - // [#4447] The audit family's GOVERNANCE is platform-owned, so a declared - // `created_at` cannot make the audit anchor client-writable. - // - // The injection above is skipped when the object already carries the - // field, and the merge below lets `schema.fields` win — correct for an - // authored business field, wrong for this family. It is how `created_at` - // became writable on an ordinary PATCH: the showcase artifact ships a - // materialized `created_at` carrying only FieldSchema DEFAULTS - // (`readonly: false`), which shadowed `AUDIT_FIELD_DEFS.created_at` - // (`readonly: true`), so the engine's `stripReadonlyFields` had nothing - // to key off and the forged value was written straight through — with no - // `droppedFields` either, because from the platform's point of view - // nothing was dropped. - // - // Its two siblings only LOOKED protected: the audit hook force-advances - // `updated_at`/`updated_by` on every update, so a forged value is - // overwritten rather than refused. `created_at` is insert-only, so - // nothing overwrote it — one field out of the trio genuinely unguarded. - // - // Presentation stays the author's (label, description, hidden, group, - // ordering …); only the keys that decide WHO MAY WRITE IT are forced. - // That leaves the deliberate back-dating path intact: `preserveAudit` - // (#3479/#3493) and `isSystem` writes still reinstate the original - // timeline, because they are checked downstream of `readonly`, not by it. - overrides[name] = { ...declared, ...AUDIT_FIELD_GOVERNANCE[name] }; - } - } - - // Canonical reassignable owner. `system: true` marks it platform-provided - // (so tooling/migrations recognise it), but — unlike the audit `*_by` - // lookups — it is NOT `readonly`: ownership is transferable, so it stays - // editable in forms and assignable via the API. SecurityPlugin auto-stamps - // it to the acting user on insert when left NULL. - if (wantOwner && !schema.fields?.owner_id) { - additions.owner_id = { ...OWNER_FIELD_DEF }; - } - - // [ADR-0117 D1] Record-level business-unit ownership. Shaped after - // `organization_id` (server-stamped scope anchor), NOT after `owner_id` - // (a user-assignable business field) — the two differ in exactly the keys - // that decide who may write and whether it renders: - // - // • `readonly: true` — D3 requires the value to be derived and validated - // server-side (`record.organization_id` must equal the unit's org), so a - // client-supplied value is overwritten, never trusted. Reassignment is a - // TRANSFER-class operation gated on `allowTransfer` (D4), which has not - // landed; until it does, no client write path may set this column. - // • `hidden: true` — nothing stamps the column yet (D2's policy is - // undecided, D8's backfill unwritten), so it is provisioned-but-inert. - // Surfacing a permanently-NULL lookup on every business object's layout - // would advertise a capability the runtime does not deliver (Prime - // Directive #10). Presentation is a one-key flip for the PR that lands - // stamping — a capability grant is not. - // • `required: false` — nullable, like every other injected column. - // - // ⚠️ None of the three presumes ADR-0117 D2 (`pinned`/`follow_owner`/ - // `transferable`), which is NOT YET RULED. They are the fail-closed shape: - // they grant no capability, so every D2 outcome remains reachable. D2's - // `owningBusinessUnit.required` is an INSERT-time rejection rule, not column - // nullability — it cannot be read off this definition either. `system: true` - // writes (the future stamping middleware, seeds) are checked downstream of - // `readonly`, so the flag blocks clients without blocking the platform — - // exactly how `organization_id` is stamped today. - // - // No index: the hierarchical predicate that would use one (`… IN (units)`, - // D6) ships with the enterprise scope resolver. An index on a column nothing - // writes and nothing filters is dead weight — the same reasoning that gates - // `organization_id`'s index on `multiTenant`. - if (wantOwningBusinessUnit && !schema.fields?.[OWNING_BUSINESS_UNIT_FIELD]) { - additions[OWNING_BUSINESS_UNIT_FIELD] = { ...OWNING_BUSINESS_UNIT_FIELD_DEF }; - } - - // [ADR-0142 D3] The ownership-queue column: the queue machine name a record - // with no owner SITS IN, and an owned record's home queue. Text, not a lookup - // (a declared queue has no row to point at); `readonly: false` like - // `owner_id`, because its writers are adjudicated where the owner's are — the - // assignment seam stamps it as the platform, and a client's move is a - // transfer-class write at plugin-security's step 3.5. `hidden: true`: the - // Console's queue affordances read it by name, and a raw machine name on every - // layout is not a reading a person needs. Landed with its writers, never ahead - // of them (ADR-0078). A declared `queue` never reaches this line on an - // eligible object: `registerObject` refuses it ({@link objectQueueColumnRefusal}), - // so the `!schema.fields` guard below only ever admits the platform's own - // definition coming back through a re-registration. - if (wantQueue && !schema.fields?.[QUEUE_FIELD]) { - additions[QUEUE_FIELD] = { ...QUEUE_FIELD_DEF }; - } - - // [#8375] The tenant INDEX is decided over the MERGED result, never over the - // input — so this function and the read-exit seam ask the same question of the - // same document shape. Both cases below route through it, including the one - // that adds no field at all (an object whose every system column is already - // declared still owes the index decision). - if (Object.keys(additions).length === 0 && Object.keys(overrides).length === 0) { - return provisionTenantScopeIndex(schema, opts); - } - - return provisionTenantScopeIndex( - { - ...schema, - // `additions` LOSE to an author's field (a declared `owner_id` is theirs); - // `overrides` WIN over it (the audit family's governance is not authorable). - fields: { ...additions, ...(schema.fields ?? {}), ...overrides }, - }, - opts, - ); -} - -/** - * [#6810, made shareable by #8375] Declare the platform's tenant-scope index on - * an object that carries a platform-provisioned `organization_id`, on a - * multi-tenant deployment — the ONE implementation of that decision. - * - * ## Why it is a function and not four lines inside `applySystemFields` - * - * It was those four lines, and that is precisely how the stamp diverged. The - * `/meta` read exits converge the injected system COLUMNS through - * `applyInjectedSystemColumns` (`@objectstack/metadata-core`, #6562) — which - * cannot reach `applySystemFields`: `@objectstack/objectql` depends on - * `@objectstack/metadata-protocol` depends on `@objectstack/metadata-core`, so - * the import runs UP the dependency graph. So the converger re-implemented the - * half it could reach (the fields map) and silently did not implement the half - * it could not (this index), and `GET /meta/object/:name` served a multi-tenant - * object reporting no tenant index while the registry's own answer carried one - * (#8375). - * - * The fix is not an `indexes` line added to that copy — a second implementation - * is what drifted in the first place. It is this: ONE function, called by the - * producer below and by {@link SchemaRegistry.materializeBaseLayer}, which is - * the seam #8268 built so a stamp converges at every read exit the day it is - * added. `__search` converges by delegating to the registry and has never - * drifted; this stamp now converges the same way. - * - * ## The predicate, stated exactly - * - * "The object carries a tenant column" — not "this call just injected one", and - * [#8459] not "the PLATFORM provisioned it". The first distinction is the whole - * reason the decision moved out of the injection branch: at a read exit the - * column is ALREADY present (`governServedItem` runs - * `applyInjectedSystemColumns` before the seam), so a condition reading - * `!schema.fields?.organization_id` is false exactly where the convergence is - * needed. {@link carriesTenantScopeColumn} answers the question the old nesting - * was really asking, at either site, in either order. - * - * The second distinction is the 2026-08-13 maintainer ruling on #8459. This - * predicate also required the column to be the platform's OWN definition - * (`isInjectedColumnDefinition`, byte-for-byte), which #8375 preserved exactly - * rather than widening inside a convergence fix. The effect was that an author - * who declared their own `organization_id` — adding a label, making it - * `required`, pointing it at their own org table — kept the column and silently - * lost the index on it, while `computeTenantLayer0Filter` (plugin-security) - * went on AND-composing `organization_id = ` onto essentially every read of - * that object. The deployment's hottest predicate, unindexed, reached by an - * additive-looking authoring move that removed a guarantee the author never knew - * they held. - * - * The ruling is one rule, stated once: on a walled deployment the wall's - * predicate is indexed, whoever typed the column. ⛔ Not a type judgement — an - * `organization_id` declared as `text` gets the index too. Inspecting the - * declared type to decide whether it is "really" the tenant anchor was - * considered and REJECTED: it is a third predicate at the site where #8375 just - * reduced two to one, and its boundary is itself something an author gets - * subtly wrong (a `text` org code would lose the index and look fine). - * - * ⚠️ Recorded gap, deliberately left uncovered by that ruling: an author who - * wants NO index on the tenant column has no way to say so. If that need - * appears it becomes an explicit declared key — ⛔ it is not grounds to restore - * the injected-column condition. What exists today for a DIFFERENT index shape - * is {@link declaresTenantIndex}: declare your own single-column tenant index - * and the platform declares none. - * - * `multiTenant: false` declares NO index rather than a false one: on an unwalled - * stack nothing filters by organization, so the index is dead weight — the same - * intent the retired `indexed` flag's value carried (#6810), expressed as - * presence instead of a boolean. - * - * No `name` on the entry: each driver derives its own (SQL's `buildIndexName` is - * table-qualified, which a hardcoded name could not be without colliding across - * tables on Postgres; Mongo's index names are per-collection). `unique` is left - * at its default `false` — a plain lookup index, never a constraint. - * - * Idempotent, via {@link declaresTenantIndex}: it runs twice on every - * registration (once at the tail of `applySystemFields`, once at the seam) and - * the second call is a no-op. Returns `schema` BY REFERENCE when nothing is - * owed, so a registry-sourced body pays a comparison and no copy. - */ - -/** - * [#8375] The index entry the platform declares for the tenant scope column — - * the ONE spelling of it, so the stamp and its write-side strip cannot disagree - * about what "the platform's own entry" looks like. - */ -const TENANT_SCOPE_INDEX: { fields: string[] } = { fields: ['organization_id'] }; - -function provisionTenantScopeIndex( - schema: ServiceObject, - opts: { multiTenant: boolean; platformGlobalObjects?: ReadonlySet }, -): ServiceObject { - if (!opts.multiTenant) return schema; - if (!carriesTenantScopeColumn(schema, opts.platformGlobalObjects)) return schema; - if (declaresTenantIndex(schema)) return schema; - - return { - ...schema, - // [#6810] Author-declared indexes keep their position; the platform's - // tenant index is APPENDED, never merged into or reordering theirs. - // The write-side inverse depends on that append being at the END — - // see {@link SchemaRegistry.stripProvisionedTenantIndexFrom}. - indexes: [...((schema as any).indexes ?? []), { ...TENANT_SCOPE_INDEX }], - } as ServiceObject; -} - /** * [#8375, widened by #8459, rebound to the wall by #8608] Is this object * tenant-scoped — i.e. does the WALL filter it by `organization_id`? @@ -1923,17 +1400,6 @@ export const VALIDATION_RULE_COPY_REFUSED_CODE = 'INVALID_METADATA' as const; */ export const OBJECT_QUEUE_COLUMN_REFUSED_CODE = 'INVALID_METADATA' as const; -/** - * Platform namespaces that multiple packages may legitimately share, so the - * install-time namespace-uniqueness gate (ADR-0048 Phase 1) must never fire on - * them: the FQN-exempt reserved namespaces (`base`, `system`) plus `sys` - * (system objects such as `sys_metadata` are contributed by many packages — see - * {@link SchemaRegistry.registerNamespace}, which is intentionally many-to-one). - */ -function isShareableNamespace(ns: string): boolean { - return RESERVED_NAMESPACES.has(ns) || ns === 'sys'; -} - /** * Sentinel for "this value is not data and does not belong in the record". * A distinct symbol rather than `undefined`, so a manifest that genuinely @@ -2732,15 +2198,15 @@ function storesObjectLessPackagelessRow(rows: readonly { package_id?: string | n export { BundleArrival, BundleEntry, CO_OWNERSHIP_WORDING, EXTENDED_OBJECTS_LISTED, ExtendedObjectOfUninstall, FoldedExtensionFields, FollowDoorLayer, NO_PACKAGE_REFERRER, OBJECT_FOLD_SCALAR_KEYS, OVERLAY_SLOT_MARK, - ObjectExtensionBody, ObjectFoldScalarKey, PicklistItemLike, PicklistReferrer, QUEUE_FIELD, RunRegisteredObject, - SECURITY_CATALOG_NAME_RULE, ServedObjectLayer, TENANT_SCOPE_INDEX, TenantRuleSettings, WithheldExtensions, - admittedFieldPatches, bareNameConflictLine, bareNameReasonFor, bundleBaseKey, changedPackagedRuleKeys, - coldBootConflictMessage, collectBundle, declaredOwnedObjectNames, displayKeyOverridesPackagedBase, entriesOfMember, - fieldNamesOf, fieldPatchYieldKey, indexKeysOf, isLeftOutExtension, isPackagelessActionSlotKey, isPlainRecord, - isShareableNamespace, isStoredActionRow, leavesExtensionsOut, mergeObjectDefinitions, mergeValidationsByName, - optionByValue, overlaySlotEntriesOf, overlaySlotKey, ownFieldNamesOf, ownFieldsTarget, packageBodySlot, - packagelessActionOwnerKey, packagelessActionSlotEntriesOf, packagelessActionSlotKey, parseOverlaySlotKey, - parsePackageAction, parsePackageObjectActions, pickBundleMember, picklistReferrerOf, provisionTenantScopeIndex, + OWNING_BUSINESS_UNIT_FIELD, ObjectExtensionBody, ObjectFoldScalarKey, PicklistItemLike, PicklistReferrer, + QUEUE_FIELD, RunRegisteredObject, SECURITY_CATALOG_NAME_RULE, ServedObjectLayer, TenantRuleSettings, + WithheldExtensions, admittedFieldPatches, applyFieldPatches, bareNameConflictLine, bareNameReasonFor, bundleBaseKey, + carriesTenantScopeColumn, changedPackagedRuleKeys, coldBootConflictMessage, collectBundle, declaredOwnedObjectNames, + declaresTenantIndex, displayKeyOverridesPackagedBase, entriesOfMember, fieldNamesOf, fieldPatchYieldKey, + indexKeysOf, isLeftOutExtension, isPackagelessActionSlotKey, isPlainRecord, isStoredActionRow, leavesExtensionsOut, + mergeValidationsByName, optionByValue, overlaySlotEntriesOf, overlaySlotKey, ownFieldNamesOf, ownFieldsTarget, + packageBodySlot, packagelessActionOwnerKey, packagelessActionSlotEntriesOf, packagelessActionSlotKey, + parseOverlaySlotKey, parsePackageAction, parsePackageObjectActions, pickBundleMember, picklistReferrerOf, recordFieldOf, ruleNamesOf, sameActionDeclaration, sameHolder, sortActionsByOrder, stableStringify, storedRowPackageOf, storesObjectLessPackagelessRow, tenantRuleSettingsOf, toRecordManifest, validationRuleName, withArrivingMembers, withDeclaredFunctionEntries, withDiscriminator, withTenantRuleSettings, withoutField, diff --git a/packages/objectql/src/registry.ts b/packages/objectql/src/registry.ts index 0fe44821a6a..3a2c28cd51c 100644 --- a/packages/objectql/src/registry.ts +++ b/packages/objectql/src/registry.ts @@ -1,6 +1,6 @@ // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. -import { ServiceObject, ObjectOwnership, resolveInjectedSystemColumns, resolveInjectedColumnProvenance as injectedColumnProvenance } from '@objectstack/spec/data'; +import { ServiceObject, ObjectOwnership, resolveInjectedSystemColumns, resolveInjectedColumnProvenance as injectedColumnProvenance, AUDIT_PROVENANCE_FIELDS } from '@objectstack/spec/data'; // [#4513] The audit-family governance table, and [#6562] the injected-column // DEFINITION tables it governs — see the re-exports below for why both live in a // package `objectql` and `metadata-protocol` both depend on. @@ -41,7 +41,7 @@ import { applyProtection } from '@objectstack/spec/shared'; // (`id || name`). The install gate's co-ownership set must name packages by the // same string the artifact loader ordered them by, or a co-owner would be // admitted — or refused — under a key nothing else in the path uses. -import { artifactPackageId, declaredDependencyIds, PackageScopedNameRefusedError, packageScopedRefusalMessage, permissionSetHoldersMeet, resolvePackageScopedHolders, type PackageScopedResolvableType, type SecurityCatalogType } from '@objectstack/core'; +import { artifactPackageId, declaredDependencyIds, PackageScopedNameRefusedError, permissionSetHoldersMeet, resolvePackageScopedHolders, type SecurityCatalogType } from '@objectstack/core'; // One name, one holder for positions, permission sets and capabilities — the // rule, the holders and the one place a declaration is read from (module doc). import { @@ -130,18 +130,19 @@ import { DEFAULT_RECORD_PAGE_CONFLICT_CODE, DUPLICATE_ARTIFACT_OBJECT_NAME_CODE, EXTENDED_OBJECTS_LISTED, EXTENSION_DEFAULT_ROW_SCOPE_REFUSED_CODE, EXTENSION_MEMBERSHIP_KEYS_REFUSED_CODE, FIND_BOUND_ACTION_COLLISIONS, MISPLACED_DEFAULT_PAGE_CODE, NAMESPACE_CONFLICT_CODE, NO_PACKAGE_REFERRER, OBJECT_FIELD_TYPE_REFUSED_CODE, - OBJECT_OWNERSHIP_CONFLICT_CODE, OBJECT_QUEUE_COLUMN_REFUSED_CODE, QUEUE_FIELD, REGISTRY_LOG_LEVELS, - SECURITY_CATALOG_NAME_RULE, VALIDATION_RULE_COPY_REFUSED_CODE, bareNameColdBootWarning, bareNameConflictLine, - bareNameReasonFor, bundleBaseKey, coldBootConflictMessage, collectBundle, overlaySlotEntriesOf, overlaySlotKey, - packageBodySlot, parseOverlaySlotKey, parsePackageAction, picklistReferrerOf, sameActionDeclaration, sameHolder, - storesObjectLessPackagelessRow, withArrivingMembers, withDiscriminator, + OBJECT_OWNERSHIP_CONFLICT_CODE, OBJECT_QUEUE_COLUMN_REFUSED_CODE, OWNING_BUSINESS_UNIT_FIELD, QUEUE_FIELD, + REGISTRY_LOG_LEVELS, RESERVED_NAMESPACES, SECURITY_CATALOG_NAME_RULE, VALIDATION_RULE_COPY_REFUSED_CODE, + applyFieldPatches, bareNameColdBootWarning, bareNameConflictLine, bareNameReasonFor, bundleBaseKey, + carriesTenantScopeColumn, coldBootConflictMessage, collectBundle, declaresTenantIndex, mergeValidationsByName, + overlaySlotEntriesOf, overlaySlotKey, packageBodySlot, parseOverlaySlotKey, parsePackageAction, picklistReferrerOf, + sameActionDeclaration, sameHolder, storesObjectLessPackagelessRow, withArrivingMembers, withDiscriminator, } from './registry-top-level.js'; import type { ArtifactInstallScope, BareNameConflict, BoundActionCollision, BundleArrival, BundleEntry, CoOwnershipSource, DefaultRecordPageConflict, ExtendedObjectOfUninstall, FoldedExtensionFields, FollowDoorLayer, ObjectContributor, - ObjectExtensionFoldOptions, ObjectFoldScalarKey, PicklistItemLike, PicklistReferrer, RegistryLogLevel, - RunRegisteredObject, SchemaRegistryOptions, SecurityCatalogNameConflict, ServedObjectLayer, TenantRuleSettings, - UnboundActionTarget, WithheldExtensions, + ObjectExtensionBody, ObjectExtensionFoldOptions, ObjectFoldScalarKey, PicklistItemLike, PicklistReferrer, + RegistryLogLevel, RunRegisteredObject, SchemaRegistryOptions, SecurityCatalogNameConflict, ServedObjectLayer, + TenantRuleSettings, UnboundActionTarget, WithheldExtensions, } from './registry-top-level.js'; // [#33949 codemod] Re-exported by name, so every importer of `./registry.js` reads what it read. export { @@ -152,10 +153,10 @@ export { NAMESPACE_CONFLICT_CODE, OBJECT_FIELD_TYPE_REFUSED_CODE, OBJECT_OWNERSHIP_CONFLICT_CODE, OBJECT_QUEUE_COLUMN_REFUSED_CODE, type ObjectContributor, type ObjectExtensionFoldOptions, REGISTRY_LOG_LEVELS, RESERVED_NAMESPACES, type RegistryLogLevel, type SchemaRegistryOptions, type SecurityCatalogNameConflict, - type UnboundActionTarget, type UnboundActionTargetError, VALIDATION_RULE_COPY_REFUSED_CODE, applySystemFields, - bareNameColdBootWarning, collectManifestBoundActions, collectManifestObjectNames, computeFQN, - describeBoundActionCollision, describeUnboundActionTargets, parseFQN, reconcileManagedApiMethods, - warnFunctionalCompleteness, warnStrippedLegacyApiMethods, + type UnboundActionTarget, type UnboundActionTargetError, VALIDATION_RULE_COPY_REFUSED_CODE, bareNameColdBootWarning, + collectManifestBoundActions, collectManifestObjectNames, computeFQN, describeBoundActionCollision, + describeUnboundActionTargets, parseFQN, reconcileManagedApiMethods, warnFunctionalCompleteness, + warnStrippedLegacyApiMethods, } from './registry-top-level.js'; // [#33949 codemod] Logging control, object storage and generic metadata storage: the methods of the same names below hand the bodies what they read (`StorageHost`). import { StorageFacet } from './registry-storage.js'; @@ -186,10 +187,138 @@ import { } from './registry-facet-hosts.js'; // [#33949 codemod] Exported for the section modules above, which import them (lever (a)); `index.ts` re-exports by name, so none reaches the package. export { - NOT_A_PAIR_ROW_OF_A_HOLDER, RegistrationRestorePoint, ValidationRuleCopyRefusedError, actionHoldersOnOwnerKeyOf, - declaredOverrideBase, pairRowDeclaresAnotherHolder, + NOT_A_PAIR_ROW_OF_A_HOLDER, RegistrationRestorePoint, TENANT_SCOPE_INDEX, ValidationRuleCopyRefusedError, + actionHoldersOnOwnerKeyOf, declaredOverrideBase, isShareableNamespace, mergeObjectDefinitions, + pairRowDeclaresAnotherHolder, provisionTenantScopeIndex, }; +/** + * Deep merge two ServiceObject definitions. + * + * The merge set is CLOSED and enumerable — this is NOT "every prop, later + * value wins": + * - `fields` and `indexes` are merged ADDITIVELY (`fields` keyed by name). + * - `validations` are merged BY NAME ({@link mergeValidationsByName}, + * [ADR-0029 D9.2a, 2026-10-30 note]): rules of different names add, and a + * rule of a name already folded replaces it — with `tenantRules`, carrying + * the tenant's `message` and `active`, never the tenant's condition. + * - The three {@link OBJECT_FOLD_SCALAR_KEYS} (`label`, `pluralLabel`, + * `description`) are overridden last-writer-wins, subject to the + * `tenantAuthored` yield rule below. [#30391] A `label` that renames the + * object without a `pluralLabel` beside it also takes the running + * `pluralLabel` off: the plural named the old label. + * - `fieldPatches` [ADR-0146 D10, second rule] is APPLIED, never carried: + * after the merges above, each patch writes its `BASE_FIELD_DISPLAY_WHITELIST` + * keys onto the named field and its option labels onto the options whose + * stored `value` it names, each key yielding where `tenantPatched` says the + * tenant authored it — the same D9.2a rule as the scalars. A patch reaches + * the owner's own fields only (`ownFields`): one naming any other field, + * a field an extension adds included, is skipped here; the fold names it. + * + * **Every other top-level prop on `extension` is silently discarded.** + * `merged` starts as `{ ...base }` and nothing outside the list above is ever + * copied onto it — a caller that hands this function an extension body + * carrying an undeclared key, e.g. `tenancy: { enabled: false }`, gets a + * valid-but-inert no-op HERE: no error, no warning from this function, the + * base's existing value simply wins as if the extension had never named the + * key (issue #12680). + * + * Declarative authors never reach that silence, though: `ObjectExtensionSchema` + * (`packages/spec/src/data/object.zod.ts`) is `.strict()` — its shape is + * exactly `extend` / `fields` / `fieldPatches` / `label` / `pluralLabel` / + * `description` / `validations` / `indexes` / `priority`, so an `objectExtensions` entry + * naming `tenancy`, `permissions`, or any other key outside that list fails + * LOUDLY at authoring time with a prescription (#4001; the same rule is + * documented at `content/docs/data-modeling/object-extensions.mdx`). This + * function's discard is reachable only by a caller that bypasses that + * schema — a direct, programmatic `registry.registerObject(def, pkg, + * undefined, 'extend', …)` call, which is how this package's own pin + * (`registry-object-extension-nonenumerated-prop-discard.test.ts`) exercises + * the rule. Extender-writable `tenancy`/`permissions` would in any case be a + * separate, much bigger decision that has NOT been made. + * + * Unlike `tenancy`/`permissions`, this one is not blocked by schema: + * `_provenance` is a real top-level prop an `extend` contributor carries + * TODAY. Copying it through (as "later value + * wins" would) lets a third-party extender flip a tenant-authored object's + * `_provenance` to the extending package's — reaching, by a different route, + * the exact outcome ADR-0029 D9.3's priority-reranking guard exists to + * prevent. `registry-object-overlay-layer.test.ts` ("an extender declaring + * priority 140 does not become the base layer") fails if this merge set is + * widened to copy it through. + * + * [ADR-0029 D9.2a] …the SCALAR override above is conditional. `tenantAuthored` names + * the scalars the fold's BASE has authored away from the packaged owner's + * value; an extender yields on those. See + * {@link SchemaRegistry.tenantAuthoredScalars} for why the set is computed once + * over the base rather than re-derived from the running `merged`; `tenantRules` + * ({@link SchemaRegistry.tenantRuleSettings}) is computed once for the same reason. + */ +function mergeObjectDefinitions( + base: ServiceObject, + extension: ObjectExtensionBody, + tenantAuthored?: ReadonlySet, + tenantRules?: ReadonlyMap, + tenantPatched?: ReadonlySet, + ownFields?: ReadonlySet, +): ServiceObject { + const merged = { ...base }; + + // Merge fields additively + if (extension.fields) { + merged.fields = { ...base.fields, ...extension.fields }; + } + + // [ADR-0029 D9.2a, 2026-10-30 note] Merge validations by name + if (extension.validations) { + merged.validations = mergeValidationsByName( + base.validations, + extension.validations, + tenantRules, + ) as ServiceObject['validations']; + } + + // Merge indexes additively + if (extension.indexes) { + merged.indexes = [...(base.indexes || []), ...extension.indexes]; + } + + // Override scalar props (last writer wins) — [ADR-0029 D9.2a] unless the base has been + // authored by the tenant, in which case the extender's packaged default yields. + const yields = (key: ObjectFoldScalarKey): boolean => tenantAuthored?.has(key) === true; + if (extension.label !== undefined && !yields('label')) { + // [#30391] `pluralLabel` is the plural OF `label`: an extension that renames + // the object and declares no plural leaves the running plural naming the + // old label, so the object is served as one that declares no + // `pluralLabel`. A plural the tenant authored stays (the D9.2a yield), and + // an extension that repeats the label it extends renames nothing. + if (extension.pluralLabel === undefined && extension.label !== merged.label && !yields('pluralLabel')) { + delete merged.pluralLabel; + } + merged.label = extension.label; + } + if (extension.pluralLabel !== undefined && !yields('pluralLabel')) { + merged.pluralLabel = extension.pluralLabel; + } + if (extension.description !== undefined && !yields('description')) { + merged.description = extension.description; + } + + // [ADR-0146 D10, second rule] The extension's display patches of the + // owner's own fields (`ownFields`) — never of a field an extension adds, + // whatever the priorities, or the answer would depend on which extender the + // fold reached first — applied AFTER the additive merges, with each key + // yielding where the tenant authored it (`tenantPatched`, ADR-0029 D9.2a), + // as the scalars above yield. Two extenders patching one key resolve as the + // scalars do: the fold applies contributors in ascending `priority`, so the + // higher one is written last. + if (extension.fieldPatches) { + merged.fields = applyFieldPatches(merged.fields, extension.fieldPatches, tenantPatched, ownFields); + } + + return merged; +} + /** * Augment a registered object with implicit system fields. * @@ -300,6 +429,402 @@ export { */ export { AUDIT_FIELD_GOVERNANCE }; +export function applySystemFields( + schema: ServiceObject, + opts: { + multiTenant: boolean; + /** + * [ADR-0131 D7] The objects THIS deployment declares platform-global — the + * plan's one deployment input. A declared object gets no `organization_id` + * here. Omitted or empty ⇒ the plan is byte-identical to the authored one. + */ + platformGlobalObjects?: ReadonlySet; + } +): ServiceObject { + // WHICH columns this object carries is the spec's derivation + // (`resolveInjectedSystemColumns`, #5378) — one answer shared with every + // author-time consumer that must resolve a reference to an injected column + // but cannot load this runtime (`@objectstack/lint`). This function keeps sole + // ownership of WHAT each column looks like, the same split #3786 established + // for the audit family. Do NOT re-derive a condition below: a second copy here + // is exactly the drift the plan exists to prevent. + // + // [#7865] This pass runs for `external` (ADR-0015) objects too — DELIBERATELY + // (maintainer ruling 2026-08-12, direction B). The platform provisions no + // storage for a federated object (`syncObjectSchema` returns early, no DDL), + // so the anchors injected below are registered-but-unprovisioned there; the + // machine-readable marker for that fact is `resolveInjectedColumnProvenance` + // / `unprovisionedInjectedColumns` (re-exported above from + // `@objectstack/metadata-core`), NOT a key on the definitions this function + // spreads — the definitions must stay byte-identical to the shipped tables, + // because the #7859 Layer-0 guard and the #4326 round-trip strip both read + // them by exact identity. Do not add keys here; consumers ask the API. + // + // [ADR-0131 D7] …and the deployment's platform-global declaration is the + // plan's one deployment input: a declared object is planned with no tenant + // column, so nothing below injects `organization_id` for it. Passed only when + // a deployment declared something, so every other call is the one-argument + // plan, byte for byte. + const plan = + opts.platformGlobalObjects && opts.platformGlobalObjects.size > 0 + ? resolveInjectedSystemColumns(schema, { platformGlobalObjects: opts.platformGlobalObjects }) + : resolveInjectedSystemColumns(schema); + + // 1. Hard opt-out at object level (e.g. seed/migration tables). + // Folded into the plan (`systemFields: false` ⇒ every flag false), so the + // cheap path stays cheap without a second reading of the key. + // + // 2. Skip only `better-auth` managed tables. Their column layout is + // driven by better-auth's own migrations (sys_user, sys_session, + // sys_organization, …) and injecting extra columns here would + // collide with what better-auth expects. Other `managedBy` buckets + // (`platform`, `config`, `system`, `append-only`) all need the + // tenant + audit columns for multi-tenant isolation and time-travel + // history — withholding them silently broke RLS reads on + // sys_audit_log / sys_activity (the SecurityPlugin's + // field-existence safety net dropped `organization_id = + // current_user.organization_id` as "field missing", producing + // RLS_DENY_FILTER → 0 rows for every non-admin caller). + // Both opt-outs are the plan's rows 1-2; the bail below covers them. + // + // Honor explicit opt-out via either `systemFields.tenant === false` + // OR `tenancy.enabled === false`. The latter is the schema-level + // declaration that the table is a shared/global catalog (e.g. + // sys_package — the Marketplace registry). Without this, the + // registry would still inject `organization_id`, and the + // SecurityPlugin's RLS layer would filter every cross-org read down + // to 0 rows even though the schema explicitly disabled multi-tenancy. + // + // The `organization_id` COLUMN is provisioned unconditionally (subject only + // to the explicit opt-outs above) — its existence no longer depends on the + // global multi-tenant flag. Decoupling "does the column exist" from "is + // tenancy enabled" is what stops sudo writers (audit / messaging / inbox / + // outbox …) from failing with "no column named organization_id" on + // single-tenant stacks: they can always stamp the column, and org-scoping + // stamps it on insert (under `single` with the Default Organization), so it + // is never NULL (ADR-0131 D1). The multi-tenant flag now governs only + // whether the column is INDEXED — on a single-tenant DB nothing ever filters + // by organization, so the index would be dead weight. That is also why the + // plan takes no `multiTenant` input: the flag cannot change what EXISTS, only + // what is indexed, so an author-time consumer needs no runtime context. + const wantTenant = plan.tenant; + const wantAudit = plan.audit; + + // Ownership is auto-provisioned by DEFAULT on user-authored business + // objects (correct-by-default for AI authors). It is withheld only where a + // per-record owner is meaningless: any platform-managed table (`managedBy` + // is set — config/append-only/system/platform; `better-auth` already + // returned above), the `sys_*` namespace, or an explicit opt-out via + // `ownership: 'business_unit' | 'org' | 'none'` on the schema (Dataverse-style + // — catalog / junction tables, plus D1's unit-owned tier, which takes the BU + // anchor instead). Note this is the SAFE default direction: forgetting the + // opt-out leaves a harmless spare column, whereas the old opt-IN model let + // authors silently ship objects with no working ownership at all. + // `ownership` is a declared ObjectSchema field (record-ownership model), read + // off the typed schema by the plan — no `as any` (#3175). + // + // [ADR-0117 D1 / #5677] The plan treats the value as a `string`, not the + // enum, on purpose: it is also called on pre-parse input. That widening is + // what let the engine recognise D1's fourth tier `'business_unit'` BEFORE the + // schema could emit it — the ordering #5677 → #5678 exists to guarantee, since + // a tier the schema emits first would be judged by a no-overlap literal test + // and get the INVERSE of what D1 declares. As of #5678 the spec enum reads + // `'user' | 'business_unit' | 'org' | 'none'` and the tier is authorable. + // + // [ADR-0117 D1 / #5677] The ownership decision is a POSITIVE LIST, deliberately + // — it used to read `ownership !== 'org' && ownership !== 'none'`, i.e. a + // DENY-list, so ANY value outside the two exclusions fell through to "inject + // `owner_id`". That default was safe only while the enum had exactly three + // members: D1's `business_unit` tier means "owned by a UNIT, not a person" + // (`owner_id` ❌, `owning_business_unit_id` ✅), and under the deny-list it + // would have been stamped with `owner_id` — the exact inverse. The same + // argument applies to any FIFTH tier a later ADR adds, which is why the list + // stays positive rather than being "completed" to the current enum. + // + // The `sys_*` / `managedBy` ineligibility and the per-tier table + // + // ownership owner_id owning_business_unit_id + // undefined/user ✅ ✅ + // business_unit ❌ ✅ + // org ❌ ❌ + // none ❌ ❌ + // + // all live in `resolveInjectedSystemColumns` now, so the author-time linter + // reaches the same verdict instead of guessing (#5378). + const wantOwner = plan.owner; + const wantOwningBusinessUnit = plan.owningBusinessUnit; + // [ADR-0142 D3] The queue column rides with the owner anchor (the plan's + // `queue` is its `owner`): "sits in Q" is `queue == Q AND owner_id IS NULL`. + const wantQueue = plan.queue; + + // Nothing to INJECT — the two opt-out rows (`systemFields: false`, + // `managedBy: 'better-auth'`) and objects whose every column is already + // declared. + // + // [#8608] It still routes through {@link provisionTenantScopeIndex}, like + // every other exit below. An object that injects nothing can still CARRY the + // tenant column — the author declares their own `organization_id` — and the + // wall composes its predicate on exactly that column (measured, not inferred: + // plugin-security's `tenancyDisabled` reads `tenancy.enabled` and + // `systemFields.tenant` only, so `systemFields: false` leaves the wall live). + // Returning `schema` here was the last exit that decided the index by NOT + // asking, which is how the withholding survived #8375's convergence. + if (!wantTenant && !wantAudit && !wantOwner && !wantOwningBusinessUnit && !wantQueue) { + return provisionTenantScopeIndex(schema, opts); + } + + const additions: Record = {}; + // Platform-owned field settings that must WIN over a declared field, rather + // than lose to it like `additions` does (#4447). + const overrides: Record = {}; + + if (wantTenant && !schema.fields?.organization_id) { + // [#6562] The authorable shape is the shared table's, spread verbatim. + // + // [#6810] Nothing is spread ON TOP of it any more. This line used to read + // `{ ...TENANT_SCOPE_FIELD_DEF, indexed: opts.multiTenant }`, and `indexed` + // is the one key that was never a `FieldSchema` key at all — removed in the + // 16.x line (#2377, ADR-0049) because a field-level index flag built no + // index, and `FieldSchema` is a `strictObject`, so a document carrying it is + // rejected BY NAME ("never a FieldSchema key; a field-level index flag built + // no index"). #6562's reasoning for leaving it here — that its only consumer + // reads the REGISTERED schema, never a served document — held for the + // consumer and not for the document: `registerObject` runs this function + // BEFORE storing and `getItem('object', …)` serves that post-injection + // document, so the key reached `/meta`, where `decorateMetadataItem` + // re-parsed the served body and stamped `_diagnostics: { valid: false, + // errors: [{ path: 'fields.organization_id', code: 'unrecognized_keys' }] }` + // on EVERY registry-backed object — both tenancy modes, both read exits. A + // defect report the platform wrote about its own column, on a document the + // author never wrote and could not fix, in the channel Studio renders + // invalid-metadata banners from. + additions.organization_id = { ...TENANT_SCOPE_FIELD_DEF }; + + // [#6810] The tenant INDEX that goes with this column is no longer decided + // here. It is {@link provisionTenantScopeIndex}, called on the merged result + // at the tail of this function — see #8375 for why a decision spelled out at + // this one site could not be reached by the `/meta` read exits. + } + + if (wantAudit) { + for (const name of AUDIT_PROVENANCE_FIELDS) { + const declared = (schema.fields as Record | undefined)?.[name]; + if (!declared) { + additions[name] = AUDIT_FIELD_DEFS[name]; + continue; + } + // [#4447] The audit family's GOVERNANCE is platform-owned, so a declared + // `created_at` cannot make the audit anchor client-writable. + // + // The injection above is skipped when the object already carries the + // field, and the merge below lets `schema.fields` win — correct for an + // authored business field, wrong for this family. It is how `created_at` + // became writable on an ordinary PATCH: the showcase artifact ships a + // materialized `created_at` carrying only FieldSchema DEFAULTS + // (`readonly: false`), which shadowed `AUDIT_FIELD_DEFS.created_at` + // (`readonly: true`), so the engine's `stripReadonlyFields` had nothing + // to key off and the forged value was written straight through — with no + // `droppedFields` either, because from the platform's point of view + // nothing was dropped. + // + // Its two siblings only LOOKED protected: the audit hook force-advances + // `updated_at`/`updated_by` on every update, so a forged value is + // overwritten rather than refused. `created_at` is insert-only, so + // nothing overwrote it — one field out of the trio genuinely unguarded. + // + // Presentation stays the author's (label, description, hidden, group, + // ordering …); only the keys that decide WHO MAY WRITE IT are forced. + // That leaves the deliberate back-dating path intact: `preserveAudit` + // (#3479/#3493) and `isSystem` writes still reinstate the original + // timeline, because they are checked downstream of `readonly`, not by it. + overrides[name] = { ...declared, ...AUDIT_FIELD_GOVERNANCE[name] }; + } + } + + // Canonical reassignable owner. `system: true` marks it platform-provided + // (so tooling/migrations recognise it), but — unlike the audit `*_by` + // lookups — it is NOT `readonly`: ownership is transferable, so it stays + // editable in forms and assignable via the API. SecurityPlugin auto-stamps + // it to the acting user on insert when left NULL. + if (wantOwner && !schema.fields?.owner_id) { + additions.owner_id = { ...OWNER_FIELD_DEF }; + } + + // [ADR-0117 D1] Record-level business-unit ownership. Shaped after + // `organization_id` (server-stamped scope anchor), NOT after `owner_id` + // (a user-assignable business field) — the two differ in exactly the keys + // that decide who may write and whether it renders: + // + // • `readonly: true` — D3 requires the value to be derived and validated + // server-side (`record.organization_id` must equal the unit's org), so a + // client-supplied value is overwritten, never trusted. Reassignment is a + // TRANSFER-class operation gated on `allowTransfer` (D4), which has not + // landed; until it does, no client write path may set this column. + // • `hidden: true` — nothing stamps the column yet (D2's policy is + // undecided, D8's backfill unwritten), so it is provisioned-but-inert. + // Surfacing a permanently-NULL lookup on every business object's layout + // would advertise a capability the runtime does not deliver (Prime + // Directive #10). Presentation is a one-key flip for the PR that lands + // stamping — a capability grant is not. + // • `required: false` — nullable, like every other injected column. + // + // ⚠️ None of the three presumes ADR-0117 D2 (`pinned`/`follow_owner`/ + // `transferable`), which is NOT YET RULED. They are the fail-closed shape: + // they grant no capability, so every D2 outcome remains reachable. D2's + // `owningBusinessUnit.required` is an INSERT-time rejection rule, not column + // nullability — it cannot be read off this definition either. `system: true` + // writes (the future stamping middleware, seeds) are checked downstream of + // `readonly`, so the flag blocks clients without blocking the platform — + // exactly how `organization_id` is stamped today. + // + // No index: the hierarchical predicate that would use one (`… IN (units)`, + // D6) ships with the enterprise scope resolver. An index on a column nothing + // writes and nothing filters is dead weight — the same reasoning that gates + // `organization_id`'s index on `multiTenant`. + if (wantOwningBusinessUnit && !schema.fields?.[OWNING_BUSINESS_UNIT_FIELD]) { + additions[OWNING_BUSINESS_UNIT_FIELD] = { ...OWNING_BUSINESS_UNIT_FIELD_DEF }; + } + + // [ADR-0142 D3] The ownership-queue column: the queue machine name a record + // with no owner SITS IN, and an owned record's home queue. Text, not a lookup + // (a declared queue has no row to point at); `readonly: false` like + // `owner_id`, because its writers are adjudicated where the owner's are — the + // assignment seam stamps it as the platform, and a client's move is a + // transfer-class write at plugin-security's step 3.5. `hidden: true`: the + // Console's queue affordances read it by name, and a raw machine name on every + // layout is not a reading a person needs. Landed with its writers, never ahead + // of them (ADR-0078). A declared `queue` never reaches this line on an + // eligible object: `registerObject` refuses it ({@link objectQueueColumnRefusal}), + // so the `!schema.fields` guard below only ever admits the platform's own + // definition coming back through a re-registration. + if (wantQueue && !schema.fields?.[QUEUE_FIELD]) { + additions[QUEUE_FIELD] = { ...QUEUE_FIELD_DEF }; + } + + // [#8375] The tenant INDEX is decided over the MERGED result, never over the + // input — so this function and the read-exit seam ask the same question of the + // same document shape. Both cases below route through it, including the one + // that adds no field at all (an object whose every system column is already + // declared still owes the index decision). + if (Object.keys(additions).length === 0 && Object.keys(overrides).length === 0) { + return provisionTenantScopeIndex(schema, opts); + } + + return provisionTenantScopeIndex( + { + ...schema, + // `additions` LOSE to an author's field (a declared `owner_id` is theirs); + // `overrides` WIN over it (the audit family's governance is not authorable). + fields: { ...additions, ...(schema.fields ?? {}), ...overrides }, + }, + opts, + ); +} + +/** + * [#6810, made shareable by #8375] Declare the platform's tenant-scope index on + * an object that carries a platform-provisioned `organization_id`, on a + * multi-tenant deployment — the ONE implementation of that decision. + * + * ## Why it is a function and not four lines inside `applySystemFields` + * + * It was those four lines, and that is precisely how the stamp diverged. The + * `/meta` read exits converge the injected system COLUMNS through + * `applyInjectedSystemColumns` (`@objectstack/metadata-core`, #6562) — which + * cannot reach `applySystemFields`: `@objectstack/objectql` depends on + * `@objectstack/metadata-protocol` depends on `@objectstack/metadata-core`, so + * the import runs UP the dependency graph. So the converger re-implemented the + * half it could reach (the fields map) and silently did not implement the half + * it could not (this index), and `GET /meta/object/:name` served a multi-tenant + * object reporting no tenant index while the registry's own answer carried one + * (#8375). + * + * The fix is not an `indexes` line added to that copy — a second implementation + * is what drifted in the first place. It is this: ONE function, called by the + * producer below and by {@link SchemaRegistry.materializeBaseLayer}, which is + * the seam #8268 built so a stamp converges at every read exit the day it is + * added. `__search` converges by delegating to the registry and has never + * drifted; this stamp now converges the same way. + * + * ## The predicate, stated exactly + * + * "The object carries a tenant column" — not "this call just injected one", and + * [#8459] not "the PLATFORM provisioned it". The first distinction is the whole + * reason the decision moved out of the injection branch: at a read exit the + * column is ALREADY present (`governServedItem` runs + * `applyInjectedSystemColumns` before the seam), so a condition reading + * `!schema.fields?.organization_id` is false exactly where the convergence is + * needed. {@link carriesTenantScopeColumn} answers the question the old nesting + * was really asking, at either site, in either order. + * + * The second distinction is the 2026-08-13 maintainer ruling on #8459. This + * predicate also required the column to be the platform's OWN definition + * (`isInjectedColumnDefinition`, byte-for-byte), which #8375 preserved exactly + * rather than widening inside a convergence fix. The effect was that an author + * who declared their own `organization_id` — adding a label, making it + * `required`, pointing it at their own org table — kept the column and silently + * lost the index on it, while `computeTenantLayer0Filter` (plugin-security) + * went on AND-composing `organization_id = ` onto essentially every read of + * that object. The deployment's hottest predicate, unindexed, reached by an + * additive-looking authoring move that removed a guarantee the author never knew + * they held. + * + * The ruling is one rule, stated once: on a walled deployment the wall's + * predicate is indexed, whoever typed the column. ⛔ Not a type judgement — an + * `organization_id` declared as `text` gets the index too. Inspecting the + * declared type to decide whether it is "really" the tenant anchor was + * considered and REJECTED: it is a third predicate at the site where #8375 just + * reduced two to one, and its boundary is itself something an author gets + * subtly wrong (a `text` org code would lose the index and look fine). + * + * ⚠️ Recorded gap, deliberately left uncovered by that ruling: an author who + * wants NO index on the tenant column has no way to say so. If that need + * appears it becomes an explicit declared key — ⛔ it is not grounds to restore + * the injected-column condition. What exists today for a DIFFERENT index shape + * is {@link declaresTenantIndex}: declare your own single-column tenant index + * and the platform declares none. + * + * `multiTenant: false` declares NO index rather than a false one: on an unwalled + * stack nothing filters by organization, so the index is dead weight — the same + * intent the retired `indexed` flag's value carried (#6810), expressed as + * presence instead of a boolean. + * + * No `name` on the entry: each driver derives its own (SQL's `buildIndexName` is + * table-qualified, which a hardcoded name could not be without colliding across + * tables on Postgres; Mongo's index names are per-collection). `unique` is left + * at its default `false` — a plain lookup index, never a constraint. + * + * Idempotent, via {@link declaresTenantIndex}: it runs twice on every + * registration (once at the tail of `applySystemFields`, once at the seam) and + * the second call is a no-op. Returns `schema` BY REFERENCE when nothing is + * owed, so a registry-sourced body pays a comparison and no copy. + */ + +/** + * [#8375] The index entry the platform declares for the tenant scope column — + * the ONE spelling of it, so the stamp and its write-side strip cannot disagree + * about what "the platform's own entry" looks like. + */ +const TENANT_SCOPE_INDEX: { fields: string[] } = { fields: ['organization_id'] }; + +function provisionTenantScopeIndex( + schema: ServiceObject, + opts: { multiTenant: boolean; platformGlobalObjects?: ReadonlySet }, +): ServiceObject { + if (!opts.multiTenant) return schema; + if (!carriesTenantScopeColumn(schema, opts.platformGlobalObjects)) return schema; + if (declaresTenantIndex(schema)) return schema; + + return { + ...schema, + // [#6810] Author-declared indexes keep their position; the platform's + // tenant index is APPENDED, never merged into or reordering theirs. + // The write-side inverse depends on that append being at the END — + // see {@link SchemaRegistry.stripProvisionedTenantIndexFrom}. + indexes: [...((schema as any).indexes ?? []), { ...TENANT_SCOPE_INDEX }], + } as ServiceObject; +} + /** * [#16319] A field declaration reached the registration door with a `type` that * is absent, or that is not a member of `FieldType`. @@ -584,6 +1109,17 @@ export function objectQueueColumnRefusal(schema: unknown): Error | null { return new ObjectQueueColumnRefusedError(typeof name === 'string' ? name : '(unnamed)'); } +/** + * Platform namespaces that multiple packages may legitimately share, so the + * install-time namespace-uniqueness gate (ADR-0048 Phase 1) must never fire on + * them: the FQN-exempt reserved namespaces (`base`, `system`) plus `sys` + * (system objects such as `sys_metadata` are contributed by many packages — see + * {@link SchemaRegistry.registerNamespace}, which is intentionally many-to-one). + */ +function isShareableNamespace(ns: string): boolean { + return RESERVED_NAMESPACES.has(ns) || ns === 'sys'; +} + /** * Raised when a package is installed whose `manifest.namespace` is already owned * by a **different** installed package in this installation (ADR-0048 Phase 1). @@ -4276,47 +4812,9 @@ export class SchemaRegistry { return readPackageScopedItem(this, { type, name, callerPackageId, member }); } - /** - * [ADR-0150 D1 bucket 1, ruling 6893582352 option A] The by-name read of a - * `doc` that names no package. `doc` keeps its literal global name and its - * addressing (bucket 1); what changes is the context-free read's answer - * when two or more ENABLED packages hold the name: a `409 - * RESOURCE_CONFLICT` naming every holder (`ambiguous-in-environment`), in - * place of {@link getItem}'s first composite, which is a registration-order - * pick (ADR-0146 D8; the bare save, delete, publish and layered read refuse - * the same address already). - * - * Unchanged: one holder; a package-less bare-slot entry (the environment's - * own item) answers first, as {@link getItem} serves it; a disabled package - * holds no name ({@link packageScopedHoldersOf}), so beside one enabled - * holder the read serves that holder's item, never the disabled one's. - * `MetadataFacade.get` serves this answer; the protocol's by-name read asks - * it before any layer for its refusal only (`protocol-meta-item-reads.ts`). - * - * @throws {PackageScopedNameRefusedError} two or more enabled holders. - */ private readBareDoc(name: string, member?: string): T | undefined { - const bare = this.metadata.get('doc')?.get(name) as { _packageId?: unknown } | undefined; - const stamp = bare?._packageId; - if (bare !== undefined && (typeof stamp !== 'string' || stamp === '' || stamp === 'sys_metadata')) { - return this.getItem('doc', name, undefined, member); - } - const served = this.packageScopedHoldersOf('doc', name); - if (served.length >= 2) { - const base: Omit = { - status: 'refused', - reason: 'ambiguous-in-environment', - type: 'doc' as PackageScopedResolvableType, - name, - callerPackageId: null, - candidates: served.map((packageId) => ({ kind: 'package' as const, packageId })), - }; - throw new PackageScopedNameRefusedError({ ...base, message: packageScopedRefusalMessage(base) }); - } - if (served.length === 1 && this.packageScopedHoldersOf('doc', name, null, { includeDisabledPackages: true }).length > 1) { - return this.getPackageScopedItem('doc', name, served[0], member); - } - return this.getItem('doc', name, undefined, member); + // [#33949 codemod] The body moved to `registry-package-addressing.ts`, its docblock with it (see `PackageAddressingHost`). + return (PackageAddressingFacet.readBareDoc).call(packageAddressingHost.of(this), name, member); } /** @@ -5596,8 +6094,10 @@ export class SchemaRegistry { compositeHoldersOf: (...a) => registry.compositeHoldersOf(...a), gateSlotBinding: (...a) => registry.gateSlotBinding(...a), getAllObjects: (...a) => registry.getAllObjects(...a), + getItem: (...a) => registry.getItem(...a), getObjectOwner: (...a) => registry.getObjectOwner(...a), getPackage: (...a) => registry.getPackage(...a), + getPackageScopedItem: (...a) => registry.getPackageScopedItem(...a), hasDisabledPackage: (...a) => registry.hasDisabledPackage(...a), invalidateAll: (...a) => registry.invalidateAll(...a), isBundleAddition: (...a) => registry.isBundleAddition(...a), @@ -5610,6 +6110,7 @@ export class SchemaRegistry { get objectContributors() { return registry.objectContributors; }, overlaySlotBinding: (...a) => registry.overlaySlotBinding(...a), ownersHoldingAName: (...a) => registry.ownersHoldingAName(...a), + packageScopedHoldersOf: (...a) => registry.packageScopedHoldersOf(...a), packagelessActionEntryToRetire: (...a) => registry.packagelessActionEntryToRetire(...a), pairRowBinding: (...a) => registry.pairRowBinding(...a), rankedForPackage: (...a) => registry.rankedForPackage(...a), From c92a4d31b4e9b1b2d98e7b91e0185cbcd904754c Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 23:04:33 +0000 Subject: [PATCH 07/12] fix(scripts): codemod-split-registry keeps REGISTRY_LOG_LEVELS where its two path readers look, and regenerates Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ --- packages/objectql/src/registry-top-level.ts | 5 ----- packages/objectql/src/registry.ts | 21 +++++++++++++-------- scripts/codemod-split-registry.mjs | 3 +++ 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/objectql/src/registry-top-level.ts b/packages/objectql/src/registry-top-level.ts index edc619f94ea..1f4dd516300 100644 --- a/packages/objectql/src/registry-top-level.ts +++ b/packages/objectql/src/registry-top-level.ts @@ -945,11 +945,6 @@ function changedPackagedRuleKeys(copy: unknown, packaged: unknown): string[] { */ export type RegistryLogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; -/** All valid {@link RegistryLogLevel} values — used to validate `OS_REGISTRY_LOG`. */ -export const REGISTRY_LOG_LEVELS: readonly RegistryLogLevel[] = [ - 'debug', 'info', 'warn', 'error', 'silent', -]; - /** * Construction options for {@link SchemaRegistry}. */ diff --git a/packages/objectql/src/registry.ts b/packages/objectql/src/registry.ts index 3a2c28cd51c..83222579261 100644 --- a/packages/objectql/src/registry.ts +++ b/packages/objectql/src/registry.ts @@ -131,11 +131,11 @@ import { EXTENSION_DEFAULT_ROW_SCOPE_REFUSED_CODE, EXTENSION_MEMBERSHIP_KEYS_REFUSED_CODE, FIND_BOUND_ACTION_COLLISIONS, MISPLACED_DEFAULT_PAGE_CODE, NAMESPACE_CONFLICT_CODE, NO_PACKAGE_REFERRER, OBJECT_FIELD_TYPE_REFUSED_CODE, OBJECT_OWNERSHIP_CONFLICT_CODE, OBJECT_QUEUE_COLUMN_REFUSED_CODE, OWNING_BUSINESS_UNIT_FIELD, QUEUE_FIELD, - REGISTRY_LOG_LEVELS, RESERVED_NAMESPACES, SECURITY_CATALOG_NAME_RULE, VALIDATION_RULE_COPY_REFUSED_CODE, - applyFieldPatches, bareNameColdBootWarning, bareNameConflictLine, bareNameReasonFor, bundleBaseKey, - carriesTenantScopeColumn, coldBootConflictMessage, collectBundle, declaresTenantIndex, mergeValidationsByName, - overlaySlotEntriesOf, overlaySlotKey, packageBodySlot, parseOverlaySlotKey, parsePackageAction, picklistReferrerOf, - sameActionDeclaration, sameHolder, storesObjectLessPackagelessRow, withArrivingMembers, withDiscriminator, + RESERVED_NAMESPACES, SECURITY_CATALOG_NAME_RULE, VALIDATION_RULE_COPY_REFUSED_CODE, applyFieldPatches, + bareNameColdBootWarning, bareNameConflictLine, bareNameReasonFor, bundleBaseKey, carriesTenantScopeColumn, + coldBootConflictMessage, collectBundle, declaresTenantIndex, mergeValidationsByName, overlaySlotEntriesOf, + overlaySlotKey, packageBodySlot, parseOverlaySlotKey, parsePackageAction, picklistReferrerOf, sameActionDeclaration, + sameHolder, storesObjectLessPackagelessRow, withArrivingMembers, withDiscriminator, } from './registry-top-level.js'; import type { ArtifactInstallScope, BareNameConflict, BoundActionCollision, BundleArrival, BundleEntry, CoOwnershipSource, @@ -151,9 +151,9 @@ export { DUPLICATE_ARTIFACT_OBJECT_NAME_CODE, type DefaultRecordPageConflict, EXTENSION_DEFAULT_ROW_SCOPE_REFUSED_CODE, EXTENSION_MEMBERSHIP_KEYS_REFUSED_CODE, FIND_BOUND_ACTION_COLLISIONS, MISPLACED_DEFAULT_PAGE_CODE, NAMESPACE_CONFLICT_CODE, OBJECT_FIELD_TYPE_REFUSED_CODE, OBJECT_OWNERSHIP_CONFLICT_CODE, - OBJECT_QUEUE_COLUMN_REFUSED_CODE, type ObjectContributor, type ObjectExtensionFoldOptions, REGISTRY_LOG_LEVELS, - RESERVED_NAMESPACES, type RegistryLogLevel, type SchemaRegistryOptions, type SecurityCatalogNameConflict, - type UnboundActionTarget, type UnboundActionTargetError, VALIDATION_RULE_COPY_REFUSED_CODE, bareNameColdBootWarning, + OBJECT_QUEUE_COLUMN_REFUSED_CODE, type ObjectContributor, type ObjectExtensionFoldOptions, RESERVED_NAMESPACES, + type RegistryLogLevel, type SchemaRegistryOptions, type SecurityCatalogNameConflict, type UnboundActionTarget, + type UnboundActionTargetError, VALIDATION_RULE_COPY_REFUSED_CODE, bareNameColdBootWarning, collectManifestBoundActions, collectManifestObjectNames, computeFQN, describeBoundActionCollision, describeUnboundActionTargets, parseFQN, reconcileManagedApiMethods, warnFunctionalCompleteness, warnStrippedLegacyApiMethods, @@ -319,6 +319,11 @@ function mergeObjectDefinitions( return merged; } +/** All valid {@link RegistryLogLevel} values — used to validate `OS_REGISTRY_LOG`. */ +export const REGISTRY_LOG_LEVELS: readonly RegistryLogLevel[] = [ + 'debug', 'info', 'warn', 'error', 'silent', +]; + /** * Augment a registered object with implicit system fields. * diff --git a/scripts/codemod-split-registry.mjs b/scripts/codemod-split-registry.mjs index c257103b04b..eb7cc556a0b 100644 --- a/scripts/codemod-split-registry.mjs +++ b/scripts/codemod-split-registry.mjs @@ -163,6 +163,9 @@ const PATH_READER_PINS = Object.freeze({ // split PR does not edit, so the anchored declarations stay here. mergeObjectDefinitions: 'ADR-0029 and ADR-0146 anchor `registry.ts#mergeObjectDefinitions` (check:adr-symbol-anchors)', isShareableNamespace: 'ADR-0130 and ADR-0141 anchor `registry.ts#isShareableNamespace` (check:adr-symbol-anchors)', + // `scripts/check-registry-log-declared.mjs` and `scripts/check-rest-log-declared.mjs` + // read the level vocabulary out of `registry.ts` by path (`LEVELS_SOURCE`). + REGISTRY_LOG_LEVELS: 'check:registry-log-declared and check:rest-log-declared read it from registry.ts by path (LEVELS_SOURCE)', TENANT_SCOPE_INDEX: "ADR-0117 anchors `registry.ts#organization_id`, which resolves as the `'organization_id'` token this declaration carries (check:adr-symbol-anchors)", }); From 3683e1eb539ada7c71a255ab8caf93a475b3a0f5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 23:43:44 +0000 Subject: [PATCH 08/12] test(objectql): the tenant-index row stays keyed to registry.ts, where the split now keeps it Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ --- packages/objectql/src/federated-injected-column-readers.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/objectql/src/federated-injected-column-readers.test.ts b/packages/objectql/src/federated-injected-column-readers.test.ts index 86abe97ef94..02a23c02aee 100644 --- a/packages/objectql/src/federated-injected-column-readers.test.ts +++ b/packages/objectql/src/federated-injected-column-readers.test.ts @@ -443,7 +443,7 @@ const READERS: Record = { disposition: 'not-a-read', why: 'names the related object in refusal text', }, - 'registry-top-level.ts# :: organization_id': { + 'registry.ts# :: organization_id': { disposition: 'not-a-read', why: 'the tenant index the registry declares on a provisioned tenant column', }, From fc1f4b5e7c5993e4bffaf103db5863dffad69c4a Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 00:58:03 +0000 Subject: [PATCH 09/12] docs(changeset): the registry split changeset cites the count on fresh main Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ --- .changeset/33949-registry-split.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/33949-registry-split.md b/.changeset/33949-registry-split.md index 823e91fab14..b4f1dd3f27b 100644 --- a/.changeset/33949-registry-split.md +++ b/.changeset/33949-registry-split.md @@ -7,4 +7,4 @@ refactor(objectql): `registry.ts` is split into section modules by codemod; a mo Clause-②: no Metadata-protocol: none -`packages/objectql/src/registry.ts` (12,438 lines) is split by `scripts/codemod-split-registry.mjs`, the first pass of the `engine.ts` codemod, as ruled on #33949 (item 1, split 1 of 8). `SchemaRegistry` keeps every member's signature and docblock, and each moved method forwards in one call to a facet module (`registry-
.ts`). The top-level declarations ahead of the class move whole into `registry-top-level.ts`, and `registry.ts` re-exports every one it exported by name. The script proves that every moved body and declaration is byte-identical to the base, and that `registry.ts` still exports the same names, each a value or a type as before. No behaviour changes, and nothing an importer of `@objectstack/objectql` reads changes. +`packages/objectql/src/registry.ts` (12,482 lines) is split by `scripts/codemod-split-registry.mjs`, the first pass of the `engine.ts` codemod, as ruled on #33949 (item 1, split 1 of 8). `SchemaRegistry` keeps every member's signature and docblock, and each moved method forwards in one call to a facet module (`registry-SECTION.ts`). The top-level declarations ahead of the class move whole into `registry-top-level.ts`, and `registry.ts` re-exports every one it exported by name. The script proves that every moved body and declaration is byte-identical to the base, and that `registry.ts` still exports the same names, each a value or a type as before. No behaviour changes, and nothing an importer of `@objectstack/objectql` reads changes. From b89e3e42b87c2dc1766fbc3ff331de64434175b4 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 05:31:31 +0000 Subject: [PATCH 10/12] chore(gates): registry.ts carries its line ceiling, 6326 lines measured at the regeneration commit plus the margin Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ --- scripts/facade-delegation-registry.json | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/scripts/facade-delegation-registry.json b/scripts/facade-delegation-registry.json index 2567ae9fd74..f0108946729 100644 --- a/scripts/facade-delegation-registry.json +++ b/scripts/facade-delegation-registry.json @@ -183,6 +183,13 @@ "baseCommit": "63db7240aa", "maxLines": 10071, "ruling": "6897467376" + }, + { + "file": "packages/objectql/src/registry.ts", + "baseLines": 6326, + "baseCommit": "c27f4ab0d6", + "maxLines": 6426, + "ruling": "6897467376" } ] } From 08a3d7ae79618d6f2877ffe4f682a8839800cf19 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 08:34:46 +0000 Subject: [PATCH 11/12] chore(scripts): drop codemod-split-registry from the tree; the split script lives in branch history only Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ --- scripts/codemod-split-registry.mjs | 3073 ---------------------------- 1 file changed, 3073 deletions(-) delete mode 100644 scripts/codemod-split-registry.mjs diff --git a/scripts/codemod-split-registry.mjs b/scripts/codemod-split-registry.mjs deleted file mode 100644 index eb7cc556a0b..00000000000 --- a/scripts/codemod-split-registry.mjs +++ /dev/null @@ -1,3073 +0,0 @@ -#!/usr/bin/env node -// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. - -/** - * codemod-split-registry — move whole sections of `SchemaRegistry`'s method - * bodies out of `packages/objectql/src/registry.ts` into section modules, move - * the top-level declarations ahead of the class into one leaf module, - * deterministically, and prove the move: byte-identity, and the module's - * exports unchanged. - * - * node scripts/codemod-split-registry.mjs # plan: print the section table, prove it, write nothing - * node scripts/codemod-split-registry.mjs --write # apply the split, then verify it - * node scripts/codemod-split-registry.mjs --verify --base - * # re-prove a split on disk against registry.ts at - * node scripts/codemod-split-registry.mjs --self-test - * node scripts/codemod-split-registry.mjs --help - * - * ## Why a script, and whose - * - * The hot-file programme splits the platform's largest files one at a time - * (ruling 6897467376 on #33949, item 1: `registry.ts` first). Each split is - * generated by script against fresh `main` right before it is queued, the - * original file stays as the forwarding entry so the public exports are - * unchanged, and a split waits for every open PR touching its file to merge. - * So the split is a COMMAND, not a diff: re-run on the latest `main`, never - * rebased, never hand-edited. - * - * This script is the FIRST PASS of `scripts/codemod-split-engine.mjs` - * (#29986, the objectql `engine.ts` split; the programme's pilot is - * `scripts/codemod-split-sql-driver.mjs` and its other precedent - * `scripts/codemod-split-protocol.mjs`), copied with its five later passes - * left out and its host parameterised: the file `registry.ts`, the class - * `SchemaRegistry`, the module prefix `registry-`, the card `#33949`. The cut - * pattern, the stay rules, the leaf, the facet hosts, the byte-identity - * proof and that pass's six self-test batteries are copied as they stand; - * the copy renamed `engine.ts` / `ObjectQL` / "the engine" to `registry.ts` / - * `SchemaRegistry` / "the registry" throughout, comments included, so a card - * number in a comment below (#30020, #30051, …) is the engine card that - * introduced the rule, not a registry one. Copied, never imported (the - * engine script's constants are module-level), so a later fix to one is a - * decision about the other too. - * - * In short, as that pass does it: - * - * - A method stays on the class with its docblock, modifiers and signature - * byte-for-byte, so the published `.d.ts` keeps its names and an instance - * spy or a subclass override is still the one callers reach. Its body - * moves, unedited, into a static method of a stateless facet class in - * `registry-
.ts`, whose `this` is the facet's host: the - * `SchemaRegistry` members the body reads, typed AS the class members. - * - The stub is one line, `Facet.x.call(xHost.of(this), …)`; the host is ONE - * object per facet and registry (`HOSTS_MODULE`), built on first use by a - * factory the class's static block installs. - * - The top-level declarations ahead of the class (`const` / `function` / - * `interface` / `type`) move whole, with their comments, into - * `LEAF_MODULE`, which never imports `registry.ts` or a `registry-*` - * module; `registry.ts` re-exports each exported one by name. - * - A method or declaration a mechanical rule keeps (`STAY_REASONS`, - * `TOP_STAY_REASONS`) stays whole; the plan prints every one and why. - * - * ## What this script adds: exports-equality (Proof 2) - * - * The ruling's "public exports are unchanged" is checked, not assumed: - * `verifyExports` compiles the base `registry.ts` alone and the split one - * with every generated module in one in-memory TypeScript project and - * requires the two export tables to be equal, name for name, each a value or - * a type as it was. `--write` and `--verify` both run it; a difference is - * exit 1 and nothing is written. - * - * ## Re-running it (the ruling's "regenerated against fresh main") - * - * On a branch freshly cut from the latest `origin/main`, ONE command: - * - * node scripts/codemod-split-registry.mjs --write - * - * then the facade's `ceilings` row in `scripts/facade-delegation-registry.json` - * is re-measured on the written file. `--verify --base origin/main` re-proves - * the result on disk. - * - * ts-morph is a root devDependency, resolved from the root manifest. - * - * Exit codes: 0 done · 1 a proof failed (stop: the generated split is never - * hand-edited to pass) · 2 the script could not run (unknown anchor, missing - * prerequisite, bad arguments) · 3 an input does not parse (`ts-parse.mjs`'s - * refusal, which names the parse errors). - */ - -import { createHash } from 'node:crypto'; -import { execFileSync } from 'node:child_process'; -import { existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'; -import { createRequire } from 'node:module'; -import { dirname, join, relative, resolve } from 'node:path'; -import { fileURLToPath, pathToFileURL } from 'node:url'; - -import { isEntrypoint } from './invoked-as.mjs'; -import { parseSourceFile } from './ts-parse.mjs'; - -const ROOT = join(fileURLToPath(new URL('.', import.meta.url)), '..'); -const SRC_REL = 'packages/objectql/src'; -const SRC_DIR = join(ROOT, SRC_REL); -const HOST_BASENAME = 'registry.ts'; -const HOST_PATH = join(SRC_DIR, HOST_BASENAME); -const CLASS_NAME = 'SchemaRegistry'; -const CARD = '#33949'; -const SCRIPT = 'scripts/codemod-split-registry.mjs'; - -/** The marker every generated file and stub carries; `--verify` keys on it. */ -const MARK = `[${CARD} codemod]`; - -/** A generated or hand-series section module of `registry.ts`, by its import specifier. */ -const SEAM_SPECIFIER = /^\.\/registry-[a-z0-9-]+\.js$/; - -/** - * The sections of `SchemaRegistry`, top to bottom, each named by its first member - * (a field or a method), mapped to the module its movable bodies go to. - * `null` keeps the section on the class. No module shares a file with a - * hand-written one: the script refuses to write over a file it did not - * generate, and the hand series' 25 module names are not reused. - */ -const SECTION_ANCHORS = Object.freeze([ - ['_logLevel', 'Logging control, object storage and generic metadata storage', 'registry-storage.ts'], - ['restorePoint', 'Restore on refusal', 'registry-restore.ts'], - ['registerNamespace', 'Namespaces, default record pages and security catalog names', 'registry-namespaces-and-claims.ts'], - ['bareNameClaims', 'Bare names, one name one package holder', 'registry-bare-names.ts'], - ['objectRegistrationRefusal', 'Object registration (ownership model)', 'registry-object-registration.ts'], - ['setDeploymentPlatformGlobalObjects', 'Platform-global declaration and uninstall checks', 'registry-platform-global.ts'], - ['registerItem', 'Generic metadata items and package bodies', 'registry-items.ts'], - ['overlaySlotBinding', 'Package-scoped addressing', 'registry-package-addressing.ts'], - ['installPackage', 'Package management', 'registry-packages.ts'], - ['registerApp', 'Apps, navigation contributions and their diagnostics', 'registry-apps.ts'], - ['registerPlugin', 'Plugins, kinds, shared picklists and reset', 'registry-picklists.ts'], -]); - -/** - * The leaf the top-level declarations ahead of the class move to, whole - * (lever (b) of ruling `6591753301`). A leaf: it never imports `registry.ts` or - * any `registry-*` module, so it is evaluated before the class and forms no - * cycle with it. - */ -const LEAF_MODULE = 'registry-top-level.ts'; - -/** - * The module that holds each facet's host, one per registry (#30051): a - * `FacetHost` per facet module, built on first use by the factory the class's - * static block installs and kept for the registry's life. It imports types only, - * so it is evaluated before the class whichever module is loaded first, and - * the class's static block can install into it. - */ -const HOSTS_MODULE = 'registry-facet-hosts.ts'; -const HOSTS_CLASS = 'FacetHost'; - -/** - * Names whose text a reader takes out of `registry.ts` BY PATH. Moving it would - * leave that reader reading a stub, or nothing. A class method or a top-level - * declaration of the name stays. Each row names the reader. A row naming - * nothing the file declares stops the script (exit 2), so the table cannot rot - * into names nobody reads. - */ -const PATH_READER_PINS = Object.freeze({ - // `pnpm check:adr-symbol-anchors` resolves each ADR's - // `packages/objectql/src/registry.ts#` anchor against THIS file's - // declarations and string tokens; `docs/adr/**` is a governed surface a - // split PR does not edit, so the anchored declarations stay here. - mergeObjectDefinitions: 'ADR-0029 and ADR-0146 anchor `registry.ts#mergeObjectDefinitions` (check:adr-symbol-anchors)', - isShareableNamespace: 'ADR-0130 and ADR-0141 anchor `registry.ts#isShareableNamespace` (check:adr-symbol-anchors)', - // `scripts/check-registry-log-declared.mjs` and `scripts/check-rest-log-declared.mjs` - // read the level vocabulary out of `registry.ts` by path (`LEVELS_SOURCE`). - REGISTRY_LOG_LEVELS: 'check:registry-log-declared and check:rest-log-declared read it from registry.ts by path (LEVELS_SOURCE)', - TENANT_SCOPE_INDEX: "ADR-0117 anchors `registry.ts#organization_id`, which resolves as the `'organization_id'` token this declaration carries (check:adr-symbol-anchors)", -}); - -/** - * Literal text a reader requires to find in `registry.ts` BY PATH. A class - * method or a top-level declaration whose text carries one stays, so the - * reader still finds it where it looks. Each row names the reader. A needle - * the file no longer carries stops the script (exit 2). - */ -const PATH_READER_NEEDLES = Object.freeze({ -}); - -/** - * Literal text a reader requires to find in `registry.ts` OR the leaf, but reads - * in no section module. A method whose text carries one stays on the class; a - * top-level declaration carrying one moves as any other, because the reader - * reads the leaf it moves to. The same stale-needle refusal applies. - */ -const CLASS_PATH_READER_NEEDLES = Object.freeze({ -}); - -/** - * The `SchemaRegistry` methods a host hands its body as a BOUND reference - * (`this.m.bind(this)`) rather than as a closure that calls it. The pilot - * needed one for a gate that refuses an unbound builder call; no gate reads - * `registry.ts` that way, so the set is empty and every method goes as a - * closure. - */ -const BOUND_HOST_METHODS = new Set(); - -/** Every reason a method stays, with the one-line why the plan prints. */ -const STAY_REASONS = Object.freeze({ - 'no-section': 'declared above the first section anchor', - 'section-kept': 'its section maps to no module', - 'static-this': 'a static method whose body reads `this` (the class; a facet static has its own)', - 'static-name-shared': 'a static method whose name an instance member also takes (one facet cannot hold both)', - 'no-body': 'an overload signature or abstract member', - 'generator': 'a generator', - 'overloaded': 'an overloaded method', - 'decorated': 'a decorated method', - 'not-identifier-name': 'a computed, string or #private name', - 'inferred-return-type': 'a non-private method whose return type is inferred (the .d.ts would infer it through the module)', - 'parameter-shape': 'a destructured or `this` parameter (the stub cannot forward it unchanged)', - 'comment-in-parameters': 'a comment inside its parameter list (one-line parameters would drop it)', - 'path-reader': 'a reader takes its text out of registry.ts by path (PATH_READER_PINS / PATH_READER_NEEDLES / CLASS_PATH_READER_NEEDLES)', - 'arguments': 'its body reads `arguments`', - 'super': 'its body reads `super`', - 'new-target': 'its body reads `new.target`', - 'this-type': 'it names the polymorphic `this` type', - 'bare-this': 'it uses `this` other than as `this.member`', - 'unknown-member': 'it reads a `this.member` the class does not declare', - 'this-write': 'it assigns a `this.member` that is not a field, or deletes one (the host carries a setter for a field only)', - 'method-as-value': 'it reads a method as a value instead of calling it', - 'field-call': 'it calls a field or accessor as a function (the host would change its receiver)', - 'overloaded-host-method': 'it calls an overloaded method (the host closure cannot be typed)', - 'static-write': 'its static reads go through SchemaRegistryStatics and it assigns a static (the forwarder is read-only)', - 'static-as-value': 'its static reads go through SchemaRegistryStatics and it reads a static method as a value (a forwarder is another function)', - 'static-field-call': 'its static reads go through SchemaRegistryStatics and it calls a static field or accessor (the receiver would change)', - 'overloaded-static': 'its static reads go through SchemaRegistryStatics and it calls an overloaded static (one forwarder cannot carry the overloads)', - 'static-route-conflict': 'its facet module reads the statics through SchemaRegistryStatics, imported AS `SchemaRegistry`, and it names `SchemaRegistry` other than as `SchemaRegistry.`', - 'module-write': 'it assigns a module-scope binding of registry.ts (an import is read-only)', - 'already-moved': 'its body is already a hand-series seam stub', - 'parameter-shadows': 'a parameter shadows a stub local (`host`, `readX`, `writeX`)', - 'foreign-private': 'it reads a private or protected instance member off a receiver other than `this` (a module function cannot)', - 'private-static': 'it reads a non-public static of its class (the statics route is `SchemaRegistry`\'s alone; the scoped classes declare none)', - 'facade-taken': 'it hands the registry on, and the class declares a member named `facade` (the identity port would shadow it)', - 'ctor-facet-name': 'a class that declares a member of the name a moved constructor body takes in its facet', - 'definite-assignment': 'a constructor that writes a field only a constructor may assign (TS2564) other than as R8 carries it: a leading `this.F = EXPR;` whose EXPR reads no `this` and no top-level name of registry.ts', -}); - -/** - * The fourth pass's identity port (#30020 R1): the host entry a moved body - * reads the registry itself through, where the method read a bare `this` - * (`ql: this`, `new ScopedContext(ctx, this)`, `(this as any)._flag`). The - * host factory hands it as the registry (`facade: registry`), so at each such site - * the body sees the same object the method saw. - */ -const FACADE_MEMBER = 'facade'; -const FACADE_DOC = ' /** The registry itself: a moved body reads `this.facade` where the method read a bare `this` (#30020). */'; -const facadeIfaceLine = (classType) => `${FACADE_DOC}\n readonly ${FACADE_MEMBER}: ${classType};`; -const FACADE_INSTALL_LINE = ` ${FACADE_MEMBER}: registry,`; -/** The same port on a scoped class's instance host (#30020 S1): `facade: instance`. */ -const SCOPED_FACADE_DOC = ' /** The instance itself: a moved body reads `this.facade` where the method read a bare `this` (#30020). */'; -const SCOPED_FACADE_INSTALL_LINE = ` ${FACADE_MEMBER}: instance,`; - -/** Every reason a top-level declaration ahead of the class stays in registry.ts. */ -const TOP_STAY_REASONS = Object.freeze({ - 'top-kind': 'not a const / function / interface / type declaration of plain names', - 'merged-declaration': 'its name is declared by more than one statement (an overload set or a merged declaration)', - 'path-reader': 'a reader takes its text out of registry.ts by path (PATH_READER_PINS / PATH_READER_NEEDLES)', - 'class-reference': 'it names a class registry.ts declares (the leaf would import registry.ts back)', - 'seam-import': 'it uses an import from a registry-* module (the leaf would reach registry.ts back through it)', - 'needs-stayer': 'it names a declaration that stays (the leaf would import registry.ts back)', -}); - -// ── Loading ts-morph ───────────────────────────────────────────────────────── - -let tsMorph = null; - -/** ts-morph from the root manifest's devDependencies; a missing install is exit 2, never a stack trace. */ -async function loadTsMorph() { - if (tsMorph) return tsMorph; - const manifest = join(ROOT, 'package.json'); - try { - const resolved = createRequire(manifest).resolve('ts-morph'); - const mod = await import(pathToFileURL(resolved).href); - tsMorph = mod.default ?? mod; - return tsMorph; - } catch (error) { - console.error( - `✗ codemod-split-registry: PREREQUISITE NOT MET — ts-morph does not resolve from ${manifest} ` + - `(${error && error.code ? error.code : 'error'}). Run \`pnpm install\` in this worktree.`, - ); - process.exit(2); - } -} - -/** - * One in-memory project per parse: no file system, no lib, no build output. - * The text goes through `ts-parse.mjs`'s checked parse first, which refuses a - * source with parse errors (its exit 3) instead of handing back a recovered - * partial tree; ts-morph then reads the same text, already vouched for. - */ -function parse(tm, fileName, text) { - parseSourceFile(fileName, text); - const project = new tm.Project({ - useInMemoryFileSystem: true, - compilerOptions: { target: tm.ts.ScriptTarget.ES2020, strict: true, noLib: true, allowJs: false }, - }); - project.getFileSystem().writeFileSync(fileName, text); - return project.addSourceFileAtPath(fileName); -} - -/** A reason the script cannot run at all (exit 2) — never a verdict on the split. */ -class CodemodRefusal extends Error {} - -function refuse(message) { - throw new CodemodRefusal(message); -} - -// ── Reading the sections ───────────────────────────────────────────────────── - -/** - * Each member's section: the last anchor at or above it. Refuses an anchor the - * class does not declare, a duplicate anchor, or anchors out of class order. - * Returns a function from a member node to its `[anchor, title, module]` row, - * or null above the first anchor. - */ -function readSections(cls, anchors) { - const firstStart = new Map(); - for (const m of cls.getMembers()) { - const name = typeof m.getName === 'function' ? m.getName() : null; - if (name && !firstStart.has(name)) firstStart.set(name, m.getStart()); - } - const rows = []; - const seen = new Set(); - for (const row of anchors) { - const [anchor] = row; - if (seen.has(anchor)) refuse(`SECTION_ANCHORS names ${anchor} twice`); - seen.add(anchor); - if (!firstStart.has(anchor)) refuse(`SECTION_ANCHORS names ${anchor}, which ${CLASS_NAME} does not declare`); - rows.push({ row, at: firstStart.get(anchor) }); - } - for (let i = 1; i < rows.length; i += 1) { - if (rows[i].at <= rows[i - 1].at) refuse(`SECTION_ANCHORS out of class order: ${rows[i].row[0]} is declared above ${rows[i - 1].row[0]}`); - } - return (member) => { - let hit = null; - for (const r of rows) if (r.at <= member.getStart()) hit = r.row; - return hit; - }; -} -// ── Analysis ───────────────────────────────────────────────────────────────── - -/** The names registry.ts binds at module scope, and which of them it exports. */ -function moduleScope(tm, sf) { - const { Node } = tm; - const bindings = new Map(); // name -> { kind: 'import' | 'local', specifier?, form?, imported?, typeOnly? } - const exported = new Set(); - const importOrder = new Map(); // specifier -> first position among the import declarations - for (const st of sf.getStatements()) { - if (Node.isImportDeclaration(st)) { - const specifier = st.getModuleSpecifierValue(); - const order = importOrder.has(specifier) ? importOrder.get(specifier) : importOrder.size; - importOrder.set(specifier, order); - const declTypeOnly = st.isTypeOnly(); - const def = st.getDefaultImport(); - if (def) bindings.set(def.getText(), { kind: 'import', specifier, form: 'default', typeOnly: declTypeOnly }); - const ns = st.getNamespaceImport(); - if (ns) bindings.set(ns.getText(), { kind: 'import', specifier, form: 'namespace', typeOnly: declTypeOnly }); - for (const spec of st.getNamedImports()) { - bindings.set((spec.getAliasNode() ?? spec.getNameNode()).getText(), { - kind: 'import', specifier, form: 'named', imported: spec.getNameNode().getText(), - typeOnly: declTypeOnly || spec.isTypeOnly(), - }); - } - continue; - } - if (Node.isExportDeclaration(st)) { - if (!st.getModuleSpecifier()) for (const spec of st.getNamedExports()) exported.add(spec.getNameNode().getText()); - continue; - } - const isExported = Node.isExportable(st) && st.isExported(); - const names = []; - if (Node.isVariableStatement(st)) { - for (const decl of st.getDeclarations()) { - for (const id of [decl.getNameNode(), ...decl.getNameNode().getDescendantsOfKind(tm.SyntaxKind.Identifier)]) { - if (Node.isIdentifier(id)) names.push(id.getText()); - } - } - } else if (typeof st.getName === 'function' && st.getName()) { - names.push(st.getName()); - } - for (const name of names) { - bindings.set(name, { kind: 'local' }); - if (isExported) exported.add(name); - } - } - return { bindings, exported, importOrder }; -} - -/** Is `node` inside a type node, below `stop`? */ -function inTypePosition(tm, node, stop) { - for (let n = node.getParent(); n && n !== stop; n = n.getParent()) { - if (tm.ts.isTypeNode(n.compilerNode)) return true; - } - return false; -} - -/** The node that binds the `this` a `this` keyword at `node` reads. */ -function thisContainer(tm, node) { - const { Node } = tm; - for (let n = node.getParent(); n; n = n.getParent()) { - if ( - Node.isFunctionDeclaration(n) || Node.isFunctionExpression(n) || Node.isMethodDeclaration(n) || - Node.isConstructorDeclaration(n) || Node.isGetAccessorDeclaration(n) || Node.isSetAccessorDeclaration(n) || - Node.isClassDeclaration(n) || Node.isClassExpression(n) || Node.isClassStaticBlockDeclaration(n) - ) return n; - } - return null; -} - -/** Is the property access `pa` written (assigned, updated, deleted, destructured into)? */ -function isWritten(tm, pa) { - const { Node, SyntaxKind } = tm; - let target = pa; - for (let p = target.getParent(); p; target = p, p = p.getParent()) { - if ( - Node.isParenthesizedExpression(p) || Node.isArrayLiteralExpression(p) || Node.isObjectLiteralExpression(p) || - Node.isPropertyAssignment(p) || Node.isShorthandPropertyAssignment(p) || Node.isSpreadElement(p) || - Node.isSpreadAssignment(p) - ) continue; - break; - } - const parent = target.getParent(); - if (!parent) return false; - if (Node.isBinaryExpression(parent) && parent.getLeft() === target) { - const op = parent.getOperatorToken().getKind(); - return op >= SyntaxKind.FirstAssignment && op <= SyntaxKind.LastAssignment; - } - if (target === pa && (Node.isPrefixUnaryExpression(parent) || Node.isPostfixUnaryExpression(parent))) { - const op = parent.getOperatorToken(); - return op === SyntaxKind.PlusPlusToken || op === SyntaxKind.MinusMinusToken; - } - if (target === pa && Node.isDeleteExpression(parent)) return true; - if ((Node.isForOfStatement(parent) || Node.isForInStatement(parent)) && parent.getInitializer() === target) return true; - return false; -} - -/** The class's instance members by name: `{ kind: 'method' | 'field' | 'getter' | 'setter', overloaded }`. */ -function instanceMembers(tm, cls) { - const { Node } = tm; - const members = new Map(); - const note = (name, kind, extra = {}) => { - const prev = members.get(name); - if (prev && prev.kind === 'setter' && kind === 'getter') { members.set(name, { kind: 'getter', ...extra }); return; } - if (prev && prev.kind === 'getter' && kind === 'setter') return; - members.set(name, { kind, ...extra }); - }; - for (const m of cls.getMembers()) { - if (Node.isConstructorDeclaration(m)) { - for (const p of m.getParameters()) if (p.isParameterProperty()) note(p.getName(), 'field'); - continue; - } - if (typeof m.isStatic === 'function' && m.isStatic()) continue; - const nameNode = typeof m.getNameNode === 'function' ? m.getNameNode() : null; - if (!nameNode || !Node.isIdentifier(nameNode)) continue; - const name = nameNode.getText(); - if (Node.isMethodDeclaration(m)) note(name, 'method', { overloaded: m.getOverloads().length > 0 }); - else if (Node.isPropertyDeclaration(m)) note(name, 'field'); - else if (Node.isGetAccessorDeclaration(m)) note(name, 'getter'); - else if (Node.isSetAccessorDeclaration(m)) note(name, 'setter'); - } - return members; -} - -/** - * Read one method: why it must stay (empty = it moves), what its body reads - * through the host, and which imports its module needs. - */ -function readMethod(tm, ctx, m, { skip = [] } = {}) { - const { Node, SyntaxKind } = tm; - // The constructor (#30020 R8) is read as a method is, with the statements - // R8 keeps on the class (`skip`, source ranges) left out of the reading. - const isCtor = Node.isConstructorDeclaration(m); - const skipped = (node) => skip.some(([s, e]) => node.getStart() >= s && node.getEnd() <= e); - const reasons = []; - const add = (code, detail) => { - const tag = detail ? `${code}: ${detail}` : code; - if (!reasons.includes(tag)) reasons.push(tag); - }; - const host = new Map(); // member -> 'method' | 'getter' | 'facade' - const writes = new Set(); // fields the body assigns: the host carries a setter for each - const imports = new Map(); // local -> { value } - const moduleRefs = new Map(); // registry.ts top-level name -> { value } - // The identity port (#30020 R1, the fourth pass only): the start of every - // bare `this` in the body, each of which the facet member reads as - // `this.facade`, the host entry that hands the registry itself. - const identity = []; - // The private-static-through-host rule: the statics the body reads as - // `SchemaRegistry.x`, whether one is non-public (that takes the route), what the - // route cannot carry, and whether the body names `SchemaRegistry` any other way. - const classStatics = new Map(); // static -> 'method' | 'getter' - const routeBlockers = []; - let readsNonPublicStatic = false; - let classOther = false; - // The parameters the body reads (a constructor's facet takes only those: R8). - const paramsRead = new Set(); - const result = () => ({ reasons, host, writes, imports, moduleRefs, classStatics, routeBlockers, readsNonPublicStatic, classOther, identity, paramsRead }); - - if (!isCtor && m.isStatic() && ctx.members.has(m.getName())) add('static-name-shared'); - if (!m.getBody()) add('no-body'); - if (m.isAbstract && m.isAbstract()) add('no-body'); - if (!isCtor && m.isGenerator()) add('generator'); - if (m.getOverloads().length > 0) add('overloaded'); - if (!isCtor && m.getDecorators().length > 0) add('decorated'); - const nameNode = isCtor ? null : m.getNameNode(); - // R9 (#30020, the second pass's members mode only): a method keyed by a - // symbol binding (`[SEED_USER_GRANTS]`) moves under that binding's facet name. - const symbolKey = !isCtor && ctx.symbolKeys ? symbolKeyOf(tm, ctx, m) : null; - if (!isCtor && !Node.isIdentifier(nameNode) && !symbolKey) { add('not-identifier-name'); return result(); } - const name = isCtor ? 'constructor' : nameNode.getText(); - if (!isCtor && !m.getReturnTypeNode() && m.getScope() !== 'private') add('inferred-return-type'); - for (const p of m.getParameters()) { - // A defaulted identifier forwards as it is: the stub keeps the default and - // hands the body the value it produced. - if (!Node.isIdentifier(p.getNameNode()) || p.getName() === 'this') add('parameter-shape', p.getName()); - } - const body = m.getBody(); - if (!body) return result(); - - // A comment between the parentheses that no parameter's own text carries. - const params = m.getParameters(); - const open = m.getFirstChildByKindOrThrow(SyntaxKind.OpenParenToken); - const close = m.getFirstChildByKindOrThrow(SyntaxKind.CloseParenToken); - let between = ctx.text.slice(open.getEnd(), close.getStart()); - for (const p of [...params].reverse()) { - between = between.slice(0, p.getStart() - open.getEnd()) + between.slice(p.getEnd() - open.getEnd()); - } - if (/\/\/|\/\*/.test(between)) add('comment-in-parameters'); - - const methodText = m.getText(); - for (const needle of Object.keys(ctx.needles)) if (methodText.includes(needle)) add('path-reader', JSON.stringify(needle)); - for (const needle of Object.keys(ctx.classNeedles)) if (methodText.includes(needle)) add('path-reader', JSON.stringify(needle)); - if (Object.prototype.hasOwnProperty.call(ctx.pins, name)) add('path-reader', name); - - const regions = [...m.getTypeParameters(), ...params, m.getReturnTypeNode(), body].filter(Boolean); - const checker = ctx.sf.getProject().getTypeChecker().compilerObject; - for (const region of regions) { - const nodes = [region, ...region.getDescendants()]; - for (const node of nodes) { - if (skip.length > 0 && skipped(node)) continue; - const kind = node.getKind(); - if (kind === SyntaxKind.SuperKeyword) add('super'); - if (kind === SyntaxKind.ThisType) add('this-type'); - if (Node.isMetaProperty(node) && node.getKeywordToken() === SyntaxKind.NewKeyword) add('new-target'); - if (Node.isPropertyAccessExpression(node) && node.getExpression().getKind() !== SyntaxKind.ThisKeyword) { - // `other.x` where `x` is a private or protected INSTANCE member of the - // class: legal in the class body, refused in a module function. (A - // static is the statics route's question, not this one.) - const foreign = foreignNonPublicMember(tm, ctx, checker, node); - if (foreign) add('foreign-private', foreign); - } - if (kind === SyntaxKind.ThisKeyword) { - if (thisContainer(tm, node) !== m) continue; - if (!isCtor && m.isStatic()) { add('static-this'); continue; } - const parent = node.getParent(); - const inBody = region === body && !inTypePosition(tm, node, m); - if (ctx.identityPort && inBody && Node.isElementAccessExpression(parent) && parent.getExpression() === node) { - // R3 (#30020): `this[k]`, `k` typed as string literals each naming a - // field, reads (and writes) those fields through host getters (and - // setters); the body's text is unchanged. - const keys = literalFieldKeys(ctx, checker, parent.getArgumentExpression()); - if (!keys) { add('bare-this'); continue; } - const outer = parent.getParent(); - if (Node.isCallExpression(outer) && outer.getExpression() === parent) { add('field-call', keys.join('|')); continue; } - const written = isWritten(tm, parent); - if (written && Node.isDeleteExpression(outer)) { add('this-write', keys.join('|')); continue; } - for (const k of keys) { - host.set(k, 'getter'); - if (written) writes.add(k); - } - continue; - } - if (!Node.isPropertyAccessExpression(parent) || parent.getExpression() !== node) { - // R1 (#30020): a `this` that hands the registry on reads the host's - // `facade`, the registry itself. - if (ctx.identityPort && inBody && !Node.isElementAccessExpression(parent)) { - if (ctx.members.has(FACADE_MEMBER)) { add('facade-taken'); continue; } - identity.push(node.getStart()); - host.set(FACADE_MEMBER, 'facade'); - continue; - } - add('bare-this'); - continue; - } - const memberNameNode = parent.getNameNode(); - if (!Node.isIdentifier(memberNameNode)) { add('bare-this'); continue; } - const member = memberNameNode.getText(); - const info = ctx.members.get(member); - if (!info) { add('unknown-member', member); continue; } - if (isWritten(tm, parent)) { - // A field is written through a host setter; a method, an accessor or - // a `delete` cannot be. - if (info.kind === 'field' && !Node.isDeleteExpression(parent.getParent())) writes.add(member); - else add('this-write', member); - } - const outer = parent.getParent(); - const called = Node.isCallExpression(outer) && outer.getExpression() === parent; - if (info.kind === 'method') { - if (!called) add('method-as-value', member); - if (info.overloaded) add('overloaded-host-method', member); - host.set(member, 'method'); - } else if (info.kind === 'setter') { - add('this-write', member); - } else { - if (called) add('field-call', member); - host.set(member, 'getter'); - } - continue; - } - if (kind !== SyntaxKind.Identifier) continue; - // `typeof this` (its `this` is an identifier in a type query): with the - // identity port the value `this` beside it would read `this.facade` and - // the type would name the host, so the body stays as the polymorphic - // `this` type does. - if (ctx.identityPort && node.getText() === 'this' && Node.isTypeQuery(node.getParent())) add('this-type'); - if (node.getText() === 'arguments') { - const parent = node.getParent(); - const isName = Node.isPropertyAccessExpression(parent) && parent.getNameNode() === node; - if (!isName) add('arguments'); - } - const parent = node.getParent(); - let symbol; - if (Node.isShorthandPropertyAssignment(parent) && parent.getNameNode() === node) { - symbol = checker.getShorthandAssignmentValueSymbol(parent.compilerNode); - } else { - symbol = checker.getSymbolAtLocation(node.compilerNode); - } - if (!symbol || !symbol.declarations) continue; - for (const decl of symbol.declarations) { - if (decl.getSourceFile() !== ctx.sf.compilerNode) continue; - const ts = tm.ts; - const typePos = inTypePosition(tm, node, m); - if (ts.isParameter(decl) && decl.parent === m.compilerNode && region === body) paramsRead.add(node.getText()); - if (ts.isImportSpecifier(decl) || ts.isImportClause(decl) || ts.isNamespaceImport(decl)) { - const local = node.getText(); - const prev = imports.get(local); - imports.set(local, { value: Boolean(prev && prev.value) || !typePos }); - continue; - } - if (!isModuleScopeDecl(ts, decl)) continue; - // Lever (a) of ruling 6591753301: a module-scope name is imported — - // from the leaf when it moved there, from `registry.ts` (exported - // for it) when it stayed. Two things a module function cannot do: - // reach a non-public static of the class, and assign an import. - const declName = decl.name && ts.isIdentifier(decl.name) ? decl.name.text : node.getText(); - if (ts.isClassDeclaration(decl) && declName === (ctx.className ?? CLASS_NAME)) { - const access = node.getParent(); - const info = !typePos && Node.isPropertyAccessExpression(access) && access.getExpression() === node - ? ctx.statics.get(access.getName()) : null; - if (info) { - const member = access.getName(); - if (info.scope !== 'public') readsNonPublicStatic = true; - const outer = access.getParent(); - const called = Node.isCallExpression(outer) && outer.getExpression() === access; - const block = (tag) => { if (!routeBlockers.includes(tag)) routeBlockers.push(tag); }; - if (isWritten(tm, access) || info.kind === 'setter') block(`static-write: ${member}`); - if (info.kind === 'method' && !called) block(`static-as-value: ${member}`); - if (info.kind === 'method' && info.overloaded) block(`overloaded-static: ${member}`); - if (info.kind !== 'method' && called) block(`static-field-call: ${member}`); - classStatics.set(member, info.kind === 'method' ? 'method' : 'getter'); - } else { - classOther = true; - } - } - if (!typePos && isWritten(tm, node)) add('module-write', declName); - const prev = moduleRefs.get(declName); - moduleRefs.set(declName, { value: Boolean(prev && prev.value) || !typePos }); - } - } - } - - const binding = ctx.scope.bindings.get(name); - // A hand-series seam stub calls the `registry-*` import of its own name; a - // method that only shares the name (`ObjectRepository#count` beside the - // hand-series `count`) and never reads that import is not one. - if (binding && binding.kind === 'import' && SEAM_SPECIFIER.test(binding.specifier) && imports.has(name)) add('already-moved', binding.specifier); - for (const p of params) { - const pn = p.getName(); - if (pn === 'host' || /^(read|write)[A-Z]/.test(pn)) add('parameter-shadows', pn); - } - // A body that reads a non-public static takes the route; what the route - // cannot carry keeps it on the class. (A module another mover routes - // applies the same to every mover in it: `plan`.) - if (readsNonPublicStatic) { - for (const tag of routeBlockers) add(tag); - if (classOther) add('static-route-conflict'); - } - return result(); -} - -/** - * The fields of the class a strict compiler requires the constructor to - * assign (TS2564 when no constructor statement does): an instance property - * with a declared type, no initializer, no `?` and no `!`, whose type node is - * not `any`, `unknown` or a union naming `undefined`. Judged on the type - * node's text alone, so a type that admits `undefined` through an alias counts - * as required, which only ever keeps more on the class. - */ -function requiredFieldsOf(tm, cls) { - const { SyntaxKind } = tm; - const out = new Set(); - for (const p of cls.getProperties()) { - if (p.isStatic() || p.hasInitializer() || p.hasQuestionToken() || p.hasExclamationToken()) continue; - const t = p.getTypeNode(); - if (!t) continue; - const k = t.getKind(); - if (k === SyntaxKind.AnyKeyword || k === SyntaxKind.UnknownKeyword) continue; - if (k === SyntaxKind.UnionType && t.getTypeNodes().some((u) => u.getKind() === SyntaxKind.UndefinedKeyword)) continue; - out.add(p.getName()); - } - return out; -} - -/** The marker R8 leaves in the constructor's facet body where it kept a statement on the class. */ -const r8MarkerText = (field, fn) => `// ${MARK} R8: \`this.${field}\` is assigned on the class (\`${fn}\`).`; - -/** The facet member that returns the value R8 keeps a constructor assignment of `field` reading. */ -const r8FnName = (field) => `${CTOR_FACET_MEMBER}${field[0].toUpperCase()}${field.slice(1)}`; - -/** The facet member a moved constructor body becomes. */ -const CTOR_FACET_MEMBER = 'construct'; - -/** Is `name` one a moved constructor's facet member takes (`construct`, or R8's `constructF`)? */ -const isCtorFacetName = (name) => name === CTOR_FACET_MEMBER || new RegExp(`^${CTOR_FACET_MEMBER}[A-Z_$]`).test(name); - -/** - * R8 (#30020), definite-assignment wiring. A strict compiler accepts a field - * the constructor must assign (`requiredFieldsOf`) only when a constructor - * statement assigns it, so moving that statement into a facet is a TS2564 - * (and a TS2565 at every read after it). R8 keeps the assignment on the class - * and moves its right side: each LEADING statement of the body that is - * exactly `this.F = EXPR;`, F required, EXPR reading no `this` and no - * top-level name of `registry.ts` (its parameters, imports and globals only), - * stays as `this.F = Facet.constructF(params);`, and `constructF` returns - * EXPR. The facet body holds a marker line in its place, which the undo turns - * back into the statement. Any other write to a required field (after another - * statement, nested, or with a right side R8 cannot carry) keeps the - * constructor whole: `definite-assignment`. - * - * Returns `{ r8: [{ field, fn, start, end, exprText, imports, params }], reasons }`. - */ -function definiteAssignmentsOf(tm, ctx, ctor) { - const { Node, SyntaxKind } = tm; - const required = requiredFieldsOf(tm, ctx.cls); - const r8 = []; - const reasons = []; - const body = ctor.getBody(); - if (!body || required.size === 0) return { r8, reasons }; - const checker = ctx.sf.getProject().getTypeChecker().compilerObject; - const ts = tm.ts; - const carried = (stmt) => { - if (!Node.isExpressionStatement(stmt)) return null; - const e = stmt.getExpression(); - if (!Node.isBinaryExpression(e) || e.getOperatorToken().getKind() !== SyntaxKind.EqualsToken) return null; - const left = e.getLeft(); - if (!Node.isPropertyAccessExpression(left) || left.getExpression().getKind() !== SyntaxKind.ThisKeyword) return null; - const field = left.getName(); - if (!required.has(field)) return null; - const expr = e.getRight(); - if (stmt.getText() !== `this.${field} = ${expr.getText()};`) return null; - const imports = new Map(); - const params = new Set(); - for (const n of [expr, ...expr.getDescendants()]) { - const k = n.getKind(); - if (k === SyntaxKind.ThisKeyword || k === SyntaxKind.SuperKeyword) return null; - if (k !== SyntaxKind.Identifier) continue; - if (n.getText() === 'arguments') return null; - const symbol = checker.getSymbolAtLocation(n.compilerNode); - for (const decl of symbol?.declarations ?? []) { - if (decl.getSourceFile() !== ctx.sf.compilerNode) continue; - if (ts.isParameter(decl) && decl.parent === ctor.compilerNode) { params.add(n.getText()); continue; } - if (ts.isImportSpecifier(decl) || ts.isImportClause(decl) || ts.isNamespaceImport(decl)) { - const prev = imports.get(n.getText()); - imports.set(n.getText(), { value: Boolean(prev && prev.value) || !inTypePosition(tm, n, ctor) }); - continue; - } - if (isModuleScopeDecl(ts, decl)) return null; - } - } - if (ctx.members.has(r8FnName(field))) return null; - return { field, fn: r8FnName(field), start: stmt.getStart(), end: stmt.getEnd(), exprText: expr.getText(), imports, params }; - }; - const statements = body.getStatements(); - let i = 0; - for (; i < statements.length; i += 1) { - const hit = carried(statements[i]); - if (!hit) break; - r8.push(hit); - } - for (const st of statements.slice(i)) { - for (const pa of [st, ...st.getDescendants()].filter((n) => Node.isPropertyAccessExpression(n))) { - if (pa.getExpression().getKind() !== SyntaxKind.ThisKeyword || thisContainer(tm, pa.getExpression()) !== ctor) continue; - if (required.has(pa.getName()) && isWritten(tm, pa)) { - const tag = `definite-assignment: ${pa.getName()}`; - if (!reasons.includes(tag)) reasons.push(tag); - } - } - } - return { r8, reasons }; -} - -/** - * R3 (#30020): the fields a computed key `this[k]` can name, when `k`'s type - * is a string literal or a union of them and each one names a field of the - * class; null otherwise, and the body stays. - */ -function literalFieldKeys(ctx, checker, key) { - if (!key) return null; - const type = checker.getTypeAtLocation(key.compilerNode); - const parts = type.isUnion() ? type.types : [type]; - const keys = []; - for (const t of parts) { - if (!t.isStringLiteral()) return null; - const info = ctx.members.get(t.value); - if (!info || info.kind !== 'field') return null; - keys.push(t.value); - } - return keys.length > 0 ? [...new Set(keys)].sort() : null; -} - -/** The name of the class's non-public instance member `pa` reads, or null. */ -function foreignNonPublicMember(tm, ctx, checker, pa) { - const ts = tm.ts; - const nameNode = pa.getNameNode(); - if (!tm.Node.isIdentifier(nameNode)) return null; - const symbol = checker.getSymbolAtLocation(nameNode.compilerNode); - for (const decl of symbol?.declarations ?? []) { - const owner = ts.isParameter(decl) && decl.parent && ts.isConstructorDeclaration(decl.parent) ? decl.parent.parent : decl.parent; - if (owner !== ctx.cls.compilerNode) continue; - const flags = ts.getCombinedModifierFlags(decl); - if (flags & ts.ModifierFlags.Static) continue; - if (flags & (ts.ModifierFlags.Private | ts.ModifierFlags.Protected)) return nameNode.getText(); - } - return null; -} - -/** - * R9 (#30020): a method keyed by a symbol binding, `[NAME]` where NAME is a - * binding of `registry.ts`'s module scope spelled SCREAMING_SNAKE, moves into the - * facet member named NAME in camelCase (`[SEED_USER_GRANTS]` -> - * `seedUserGrants`, the name the hand move of `[APP_AUTHORED_HOOK_API]` took). - * Its stub keeps the key; only the facet member is renamed. Null for any other - * name. - */ -function symbolKeyOf(tm, ctx, m) { - const { Node } = tm; - const nameNode = m.getNameNode(); - if (!Node.isComputedPropertyName(nameNode)) return null; - const expr = nameNode.getExpression(); - if (!Node.isIdentifier(expr)) return null; - const key = expr.getText(); - if (!ctx.scope.bindings.has(key) || !/^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*$/.test(key)) return null; - return { key, facetName: key.toLowerCase().replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase()) }; -} - -function isModuleScopeDecl(ts, decl) { - if (ts.isVariableDeclaration(decl)) { - const statement = decl.parent && decl.parent.parent; - return Boolean(statement && ts.isVariableStatement(statement) && ts.isSourceFile(statement.parent)); - } - if (ts.isBindingElement(decl)) { - let n = decl; - while (n && !ts.isVariableDeclaration(n)) n = n.parent; - return Boolean(n && isModuleScopeDecl(ts, n)); - } - return Boolean(decl.parent && ts.isSourceFile(decl.parent)) && - (ts.isFunctionDeclaration(decl) || ts.isClassDeclaration(decl) || ts.isInterfaceDeclaration(decl) || - ts.isTypeAliasDeclaration(decl) || ts.isEnumDeclaration(decl) || ts.isModuleDeclaration(decl)); -} - -// ── The plan ───────────────────────────────────────────────────────────────── - -/** `registry-transactions-section.ts` -> `TransactionsSectionHost`. */ -function hostNameOf(moduleFile) { - const slug = moduleFile.replace(/^registry-/, '').replace(/\.ts$/, ''); - return `${slug.split('-').map((w) => w[0].toUpperCase() + w.slice(1)).join('')}Host`; -} - -/** `registry-transactions-section.ts` -> `TransactionsSectionFacet`: the stateless class the bodies move into. */ -function facetNameOf(moduleFile) { - return hostNameOf(moduleFile).replace(/Host$/, 'Facet'); -} - -/** `registry-transactions-section.ts` -> `transactionsSectionHost`: the module's `FacetHost` in `HOSTS_MODULE`. */ -function hostVarOf(moduleFile) { - const name = hostNameOf(moduleFile); - return `${name[0].toLowerCase()}${name.slice(1)}`; -} - -/** The object the class fills with forwarders to the statics the facets read, and its type. */ -const STATICS_NAME = `${CLASS_NAME}Statics`; -const STATICS_HOST_NAME = `${CLASS_NAME}StaticsHost`; -/** How a routed facet module names the class as a type: `SchemaRegistry` there is `SchemaRegistryStatics`. */ -const CLASS_TYPE_ALIAS = `${CLASS_NAME}Class`; - -/** The placeholder above a moved public or protected body: its docblock stays on the class. */ -const placeholderDoc = (name, className = CLASS_NAME) => `/** The body of \`${className}#${name}\`; the method's docblock there is its contract. */`; - -/** - * The statics block `registry.ts` carries for the routed facets: the type and - * the object ahead of the class, and the static block at the end of the class - * that fills the object. `kinds` maps each routed static to `method` (a - * forwarder that calls it on the class) or `getter` (read when read). Pure: - * `verify` rebuilds both texts from the base class and compares them. - */ -function staticsTexts(kinds) { - const members = [...kinds.keys()].sort(); - const ahead = [ - `// ${MARK} The statics of \`${CLASS_NAME}\` its facet modules read, private ones included, as`, - `// the class declares them (an indexed access on the class). The static block at the end of`, - `// the class fills \`${STATICS_NAME}\` with forwarders: a method is called on the class at`, - '// call time, a field or accessor is read when the body reads it. A facet module imports', - `// \`${STATICS_NAME}\` AS \`${CLASS_NAME}\`, so each moved body reads \`${CLASS_NAME}.x\` unedited;`, - '// `index.ts` re-exports neither, so neither reaches the package.', - `export interface ${STATICS_HOST_NAME} {`, - ...members.map((n) => ` readonly ${n}: (typeof ${CLASS_NAME})['${n}'];`), - '}', - `export const ${STATICS_NAME} = {} as ${STATICS_HOST_NAME};`, - ].join('\n'); - const block = [ - ` // ${MARK} Fills \`${STATICS_NAME}\` (declared above the class) for the facet modules.`, - ' static {', - ` Object.defineProperties(${STATICS_NAME}, {`, - ...members.map((n) => (kinds.get(n) === 'method' - ? ` ${n}: { value: (...args: Parameters) => ${CLASS_NAME}.${n}(...args) },` - : ` ${n}: { get: () => ${CLASS_NAME}.${n} },`)), - ' });', - ' }', - ].join('\n'); - return { ahead, block }; -} - -/** - * The facet hosts (#30051): `HOSTS_MODULE`'s text, and the static block at the - * end of the class that installs one host factory per facet into it. `facets` - * lists each facet module whose bodies read the class, with `members` (member - * -> `method` | `getter`, the union over its bodies) and `written` (the fields - * a body assigns). The host is the module's whole `
Host`, so every - * stub of the facet hands its body the same object, which carries every - * member any body of the facet reads (a `Pick` of it is what each body's - * `this` names). Each member forwards as the per-call literal did: a method - * as a closure that calls it on the registry at call time, a field or accessor - * as a getter read when the body reads it, a written field with a setter that - * assigns it on the registry. So a spy or subclass override installed after the - * host was built is still the one reached, and the only thing a body can tell - * apart is that two calls see the same host object, which no moved body can - * observe: a body that reads `this` bare stays on the class (`bare-this`). - * Pure: `verify` rebuilds both texts from the head's host interfaces and the - * base class, and compares them. - */ -function hostsTexts(facets) { - const sorted = [...facets].sort((a, b) => (a.moduleFile < b.moduleFile ? -1 : a.moduleFile > b.moduleFile ? 1 : 0)); - const module = [ - '// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.', - '', - '/**', - ` * ${MARK} The host of each facet of \`${CLASS_NAME}\`, built ONCE per registry (#30051):`, - ` * what every stub of a facet hands that facet's bodies as \`this\`, in place of`, - ' * a host literal built on every call. Each `FacetHost` below is filled by the', - ` * factory \`${CLASS_NAME}\`'s static block installs, the one place that can read`, - ' * the class\'s private members; the host is built on the first call that needs', - ' * it and kept for the registry\'s life. A method goes as a closure that calls it', - ' * on the registry at call time, a field or accessor as a getter (and a setter', - ' * when a body assigns it), so a body reads and writes the registry exactly as', - ' * the per-call literal let it.', - ' *', - ` * Written by \`${SCRIPT}\`; it imports types only, so it forms no`, - ' * evaluation cycle and is evaluated before the class whichever module loads', - ' * first. `index.ts` re-exports nothing from this module.', - ' */', - '', - `import type { ${CLASS_NAME} } from './registry.js';`, - ...sorted.map((f) => `import type { ${hostNameOf(f.moduleFile)} } from './${f.moduleFile.replace(/\.ts$/, '.js')}';`), - '', - `/** One facet's host for each registry: built on first use, kept for the registry's life. */`, - `export class ${HOSTS_CLASS} {`, - ` private readonly hosts = new WeakMap<${CLASS_NAME}, H>();`, - ` private build: ((registry: ${CLASS_NAME}) => H) | undefined;`, - '', - ' constructor(private readonly facet: string) {}', - '', - ` /** Called once, from \`${CLASS_NAME}\`'s static block. */`, - ` install(build: (registry: ${CLASS_NAME}) => H): void {`, - ' this.build = build;', - ' }', - '', - ' /** The host of `registry`, built on first use. */', - ` of(registry: ${CLASS_NAME}): H {`, - ' let host = this.hosts.get(registry);', - ' if (host === undefined) {', - ' if (this.build === undefined) {', - ` throw new Error(\`\${this.facet}: ${CLASS_NAME} has not installed its host factory\`);`, - ' }', - ' host = this.build(registry);', - ' this.hosts.set(registry, host);', - ' }', - ' return host;', - ' }', - '}', - '', - ...sorted.map((f) => `export const ${hostVarOf(f.moduleFile)} = new ${HOSTS_CLASS}<${hostNameOf(f.moduleFile)}>('${facetNameOf(f.moduleFile)}');`), - '', - ].join('\n'); - const block = [ - ` // ${MARK} Installs each facet's host factory (\`${HOSTS_MODULE}\`): a static block adds`, - ' // no member to the class, so the published `.d.ts` does not move.', - ' static {', - ...sorted.flatMap((f) => [ - ` ${hostVarOf(f.moduleFile)}.install((registry) => ({`, - ...hostInstallLines(f.members, f.written), - ' }));', - ]), - ' }', - ].join('\n'); - return { module, block }; -} - -/** The member lines of one facet's host factory, in its install call: sorted, a getter's setter after it. */ -function hostInstallLines(members, written) { - return [...members.keys()].sort().flatMap((member) => { - if (members.get(member) === 'facade') return [FACADE_INSTALL_LINE]; - if (members.get(member) === 'getter') { - return [` get ${member}() { return registry.${member}; },`, - ...(written.has(member) ? [` set ${member}(v) { registry.${member} = v; },`] : [])]; - } - if (BOUND_HOST_METHODS.has(member)) return [` get ${member}() { return registry.${member}.bind(registry); },`]; - return [` ${member}: (...a) => registry.${member}(...a),`]; - }); -} - -/** Is `node` inside a JSDoc comment? A `{@link X}` there is prose, not a use. */ -function inJsDoc(tm, node) { - for (let n = node.compilerNode; n; n = n.parent) { - if (n.kind >= tm.SyntaxKind.FirstJSDocNode && n.kind <= tm.SyntaxKind.LastJSDocNode) return true; - } - return false; -} - -/** The statement of `sf` (a direct child of the source file) a declaration belongs to. */ -function topStatementOf(ts, decl) { - let n = decl; - while (n.parent && !ts.isSourceFile(n.parent)) n = n.parent; - return n.parent ? n : null; -} - -/** The names a top-level statement declares, or null when one is not a plain identifier. */ -function declaredNames(tm, st) { - const { Node } = tm; - if (Node.isVariableStatement(st)) { - const names = []; - for (const d of st.getDeclarations()) { - if (!Node.isIdentifier(d.getNameNode())) return null; - names.push(d.getName()); - } - return names; - } - if (Node.isFunctionDeclaration(st) || Node.isInterfaceDeclaration(st) || Node.isTypeAliasDeclaration(st)) { - return st.getName() ? [st.getName()] : null; - } - return null; -} - -/** - * Lever (b): read every top-level statement between the last import and the - * class, and decide which move whole into the leaf. A mover never names the - * class, an `registry-*` module's import, or a statement that stays — so the - * leaf it lands in never imports `registry.ts` back. Pure. - */ -function planTopLevel(tm, ctx) { - const { Node, SyntaxKind } = tm; - const ts = tm.ts; - const stmts = ctx.sf.getStatements(); - const clsIndex = stmts.indexOf(ctx.cls); - let lastImport = -1; - stmts.forEach((st, i) => { if (Node.isImportDeclaration(st)) lastImport = i; }); - const checker = ctx.sf.getProject().getTypeChecker().compilerObject; - const nameCount = new Map(); - for (const st of stmts) { - for (const n of declaredNames(tm, st) ?? []) nameCount.set(n, (nameCount.get(n) ?? 0) + 1); - } - const records = []; - const byStatement = new Map(); - for (let i = lastImport + 1; i < clsIndex; i += 1) { - const st = stmts[i]; - const names = declaredNames(tm, st); - const record = { - node: st, names: names ?? [], label: names ? names.join(', ') : `(${st.getKindName()})`, - exported: Node.isExportable(st) && st.isExported(), reasons: [], - deps: new Set(), imports: new Map(), - startLine: st.getStartLineNumber(true), lines: st.getEndLineNumber() - st.getStartLineNumber(true) + 1, - typeOnly: Node.isInterfaceDeclaration(st) || Node.isTypeAliasDeclaration(st), - }; - const add = (code, detail) => { - const tag = detail ? `${code}: ${detail}` : code; - if (!record.reasons.includes(tag)) record.reasons.push(tag); - }; - if (!names || (Node.isVariableStatement(st) && st.hasDeclareKeyword()) || - (Node.isFunctionDeclaration(st) && (!st.getBody() || st.hasDeclareKeyword()))) add('top-kind'); - for (const n of record.names) if ((nameCount.get(n) ?? 0) > 1) add('merged-declaration', n); - for (const n of record.names) if (Object.prototype.hasOwnProperty.call(ctx.pins, n)) add('path-reader', n); - const fullText = st.getFullText(); - for (const needle of Object.keys(ctx.needles)) if (fullText.includes(needle)) add('path-reader', JSON.stringify(needle)); - for (const node of st.getDescendantsOfKind(SyntaxKind.Identifier)) { - if (inJsDoc(tm, node)) continue; - const parent = node.getParent(); - const symbol = Node.isShorthandPropertyAssignment(parent) && parent.getNameNode() === node - ? checker.getShorthandAssignmentValueSymbol(parent.compilerNode) - : checker.getSymbolAtLocation(node.compilerNode); - if (!symbol || !symbol.declarations) continue; - for (const decl of symbol.declarations) { - if (decl.getSourceFile() !== ctx.sf.compilerNode) continue; - const typePos = inTypePosition(tm, node, st); - if (ts.isImportSpecifier(decl) || ts.isImportClause(decl) || ts.isNamespaceImport(decl)) { - const local = node.getText(); - const binding = ctx.scope.bindings.get(local); - if (binding && (SEAM_SPECIFIER.test(binding.specifier) || binding.specifier === './registry.js')) add('seam-import', local); - const prev = record.imports.get(local); - record.imports.set(local, { value: Boolean(prev && prev.value) || !typePos }); - continue; - } - if (!isModuleScopeDecl(ts, decl)) continue; - const owner = topStatementOf(ts, decl); - if (!owner || owner === st.compilerNode) continue; - if (owner === ctx.cls.compilerNode) { add('class-reference'); continue; } - record.deps.add(owner); - } - } - records.push(record); - byStatement.set(st.compilerNode, record); - } - // A mover may not name a statement that stays: close over the stayers until stable. - for (let changed = true; changed;) { - changed = false; - for (const r of records) { - if (r.reasons.length > 0) continue; - for (const owner of r.deps) { - const dep = byStatement.get(owner); - if (!dep || dep.reasons.length > 0) { - const depName = dep ? dep.label : (declaredNames(tm, ctx.sf.getStatements().find((s) => s.compilerNode === owner)) ?? ['?']).join(', '); - r.reasons.push(`needs-stayer: ${depName}`); - changed = true; - break; - } - } - } - } - const movers = records.filter((r) => r.reasons.length === 0); - const moverNames = new Map(); - for (const r of movers) for (const n of r.names) moverNames.set(n, r); - return { records, movers, moverNames }; -} - -/** - * Which moved top-level names the text that STAYS in `registry.ts` still - * reads, value or type: everything outside the moved statements and the moved - * method bodies, JSDoc excepted. Those are what `registry.ts` imports back - * from the leaf. - */ -function hostUsesOfMovers(tm, ctx, top, methodRecords) { - const { SyntaxKind } = tm; - const ts = tm.ts; - const checker = ctx.sf.getProject().getTypeChecker().compilerObject; - const skip = []; - for (const r of top.movers) skip.push([r.node.getFullStart(), r.node.getEnd()]); - for (const r of methodRecords) if (r.reasons.length === 0) skip.push([r.node.getBody().getStart(), r.node.getBody().getEnd()]); - const uses = new Map(); - for (const node of ctx.sf.getDescendantsOfKind(SyntaxKind.Identifier)) { - const at = node.getStart(); - if (skip.some(([a, b]) => at >= a && at < b)) continue; - if (inJsDoc(tm, node)) continue; - const name = node.getText(); - if (!top.moverNames.has(name)) continue; - const symbol = checker.getSymbolAtLocation(node.compilerNode); - if (!symbol || !symbol.declarations) continue; - const mover = top.moverNames.get(name); - if (!symbol.declarations.some((d) => topStatementOf(ts, d) === mover.node.compilerNode)) continue; - const typePos = inTypePosition(tm, node, ctx.sf); - const prev = uses.get(name); - uses.set(name, { value: Boolean(prev && prev.value) || !typePos }); - } - return uses; -} - -/** - * The class's statics: scope, and what a forwarder in `SchemaRegistryStatics` would - * be (a method forwards as a function; a field or accessor as a getter). - */ -function classStaticsOf(tm, cls) { - const { Node } = tm; - const statics = new Map(); - for (const m of cls.getMembers()) { - if (typeof m.isStatic === 'function' && m.isStatic() && typeof m.getName === 'function') { - const prev = statics.get(m.getName()); - const kind = Node.isMethodDeclaration(m) ? 'method' : Node.isSetAccessorDeclaration(m) ? (prev ? prev.kind : 'setter') - : Node.isGetAccessorDeclaration(m) ? 'getter' : 'field'; - statics.set(m.getName(), { - scope: typeof m.getScope === 'function' ? m.getScope() : 'public', kind, - overloaded: Node.isMethodDeclaration(m) && m.getOverloads().length > 0, - }); - } - } - return statics; -} - -/** - * Read `text` (an `registry.ts`) and decide, per method, where its body goes, - * and per top-level declaration ahead of the class, whether it moves to the - * leaf. Pure: the same text gives the same plan. - */ -function plan(tm, text, { - anchors = SECTION_ANCHORS, pins = PATH_READER_PINS, needles = PATH_READER_NEEDLES, classNeedles = CLASS_PATH_READER_NEEDLES, -} = {}) { - const sf = parse(tm, HOST_PATH, text); - const cls = sf.getClass(CLASS_NAME); - if (!cls) refuse(`no class ${CLASS_NAME} in ${HOST_BASENAME}`); - const sectionOf = readSections(cls, anchors); - const ctx = { text, sf, cls, members: instanceMembers(tm, cls), scope: moduleScope(tm, sf), statics: classStaticsOf(tm, cls), pins, needles, classNeedles }; - for (const n of [STATICS_NAME, STATICS_HOST_NAME, ...SECTION_ANCHORS.filter((row) => row[2]).map((row) => facetNameOf(row[2]))]) { - if (ctx.scope.bindings.has(n)) refuse(`${HOST_BASENAME} already binds ${n}, a name this script generates`); - } - const methodNames = new Set(cls.getMethods().map((m) => m.getName())); - const stalePins = Object.keys(pins).filter((n) => !methodNames.has(n) && !ctx.scope.bindings.has(n)); - if (stalePins.length > 0) refuse(`PATH_READER_PINS names what ${HOST_BASENAME} no longer declares: ${stalePins.join(', ')}`); - const staleNeedles = Object.keys(needles).filter((n) => !text.includes(n)); - if (staleNeedles.length > 0) refuse(`PATH_READER_NEEDLES names text ${HOST_BASENAME} no longer carries: ${staleNeedles.map((n) => JSON.stringify(n)).join(', ')}`); - const staleClassNeedles = Object.keys(classNeedles).filter((n) => !text.includes(n)); - if (staleClassNeedles.length > 0) refuse(`CLASS_PATH_READER_NEEDLES names text ${HOST_BASENAME} no longer carries: ${staleClassNeedles.map((n) => JSON.stringify(n)).join(', ')}`); - - const top = planTopLevel(tm, ctx); - - const records = []; - for (const m of cls.getMethods()) { - const startLine = m.getStartLineNumber(true); - const section = sectionOf(m); - const read = readMethod(tm, ctx, m); - const name = m.getName(); - if (!section) read.reasons.unshift('no-section'); - else if (section[2] === null) read.reasons.unshift('section-kept'); - const lineCount = m.getEndLineNumber() - startLine + 1; - const body = m.getBody(); - const bodyLines = body ? body.getEndLineNumber() - body.getStartLineNumber() + 1 : 0; - // Docblock ownership (ruling 6624345766 item 1): a PRIVATE method's - // docblock moves with its body; a public or protected one stays on the - // facade, where the published surface prints it. - const docStart = m.getStart(true); - const doc = m.getScope() === 'private' && docStart < m.getStart() ? text.slice(docStart, m.getStart()) : null; - const record = { - name, node: m, section: section ? section[1] : null, module: section ? section[2] : null, - startLine, lineCount, bodyLines, doc, docLines: doc ? doc.split('\n').length - 1 : 0, ...read, - }; - if (section && section[2]) { - for (const p of m.getParameters()) if (p.getName() === facetNameOf(section[2])) read.reasons.push(`parameter-shadows: ${p.getName()}`); - } - if (record.reasons.length === 0) record.stubLines = stubBody(record, record.module, ' ').split('\n').length; - records.push(record); - } - - // The private-static-through-host rule, per facet module: once one mover - // reads the statics through `SchemaRegistryStatics` (imported AS `SchemaRegistry`), - // every mover in that module does, so what the route cannot carry keeps a - // mover on the class. A router itself never carries a blocker (readMethod - // already kept it), so the set of routed modules is stable. - for (let changed = true; changed;) { - changed = false; - for (const movers of groupMovers(records).values()) { - if (!movers.some((r) => r.readsNonPublicStatic)) continue; - for (const r of movers) { - const extra = [...r.routeBlockers, ...(r.classOther ? ['static-route-conflict'] : [])].filter((t) => !r.reasons.includes(t)); - if (extra.length > 0) { r.reasons.push(...extra); changed = true; } - } - } - } - const modules = groupMovers(records); - const routedModules = new Set(); - const routedStatics = new Map(); - for (const [moduleFile, movers] of modules) { - if (!movers.some((r) => r.readsNonPublicStatic)) continue; - routedModules.add(moduleFile); - for (const r of movers) for (const [member, kind] of r.classStatics) routedStatics.set(member, kind); - } - for (const [moduleFile, movers] of modules) { - const facet = facetNameOf(moduleFile); - for (const r of movers) { - for (const local of [...r.imports.keys(), ...r.moduleRefs.keys()]) { - if (local === facet || local === hostNameOf(moduleFile) || local === CLASS_TYPE_ALIAS) { - refuse(`${moduleFile}: ${r.name} reads ${local}, a name this script generates in that module`); - } - } - } - } - const hostUses = hostUsesOfMovers(tm, ctx, top, records); - - // What each side imports across the cut: the section modules read moved - // names from the leaf and staying names from registry.ts (lever (a)); - // registry.ts reads moved names from the leaf. - const sectionLeafNames = new Set(); - const hostExportsForSections = new Map(); // staying name -> { value } - for (const movers of modules.values()) { - for (const r of movers) { - for (const [n, use] of r.moduleRefs) { - if (top.moverNames.has(n)) { sectionLeafNames.add(n); continue; } - if (n === CLASS_NAME || ctx.scope.exported.has(n)) continue; - const prev = hostExportsForSections.get(n); - hostExportsForSections.set(n, { value: Boolean(prev && prev.value) || use.value }); - } - } - } - const leafExports = []; - for (const r of top.movers) { - if (r.exported) continue; - for (const n of r.names) if (hostUses.has(n) || sectionLeafNames.has(n)) leafExports.push(n); - } - return { sf, cls, ctx, records, modules, top, hostUses, leafExports, hostExportsForSections, routedModules, routedStatics }; -} - -/** Movers grouped by module, modules in the order their first member appears. */ -function groupMovers(records) { - const byModule = new Map(); - for (const r of records) { - if (r.reasons.length > 0 || !r.module) continue; - if (!byModule.has(r.module)) byModule.set(r.module, []); - byModule.get(r.module).push(r); - } - return byModule; -} - -// ── Generation ─────────────────────────────────────────────────────────────── - - -const byName = ([a], [b]) => (a < b ? -1 : a > b ? 1 : 0); -const leafSpecifier = () => `./${LEAF_MODULE.replace(/\.ts$/, '.js')}`; - -/** - * One `import` / `export` statement over `names`: on one line when it fits in - * 120 columns, else one name per wrapped run, two-space indented. - */ -function namedStatement(head, names, tail) { - const one = `${head} { ${names.join(', ')} }${tail}`; - if (one.length <= 120) return one; - const out = [`${head} {`]; - let line = ' '; - for (const n of names) { - if (line.length + n.length + 2 > 118 && line !== ' ') { out.push(line); line = ' '; } - line += ` ${n},`; - } - out.push(line); - out.push(`}${tail}`); - return out.join('\n'); -} - -/** - * `import { … }` and `import type { … }` from one specifier, values first. - * `styles` keeps a statement a hand edit left on one line (`'line'`) on one - * line, however long; the first pass's own form (`'wrap'`, the default) is - * `namedStatement`'s. - */ -function importPair(specifier, uses, styles = {}) { - const out = []; - const entries = [...uses.entries()].sort(byName); - const values = entries.filter(([, u]) => u.value).map(([n]) => n); - const types = entries.filter(([, u]) => !u.value).map(([n]) => n); - const stmt = (head, names, style) => (style === 'line' - ? `${head} { ${names.join(', ')} } from '${specifier}';` : namedStatement(head, names, ` from '${specifier}';`)); - if (values.length > 0) out.push(stmt('import', values, styles.value)); - if (types.length > 0) out.push(stmt('import type', types, styles.type)); - return out; -} - -/** - * The form each statement of a leaf or `./registry.js` import run is in: - * `'wrap'` when it is `namedStatement`'s, `'line'` when a hand edit grew it on - * one line past the wrap width (#30227 left one so), each with its names - * sorted; null when a statement is in neither form. A move renders the run - * back in the forms it read, and so does the undo, so neither rewraps a line - * the hand wrote. - */ -function runStyles(text, decls, specifier) { - const styles = {}; - if (specifier !== leafSpecifier() && specifier !== './registry.js') return styles; - for (const d of decls) { - const stmt = text.slice(d.start, d.end); - const type = stmt.startsWith('import type '); - const head = type ? 'import type' : 'import'; - const names = [...d.bindings.keys()].sort(); - const tail = ` from '${specifier}';`; - let style; - if (stmt === namedStatement(head, names, tail)) style = 'wrap'; - else if (stmt === `${head} { ${names.join(', ')} }${tail}`) style = 'line'; - else return null; - const key = type ? 'type' : 'value'; - if (styles[key]) return null; - styles[key] = style; - } - return styles; -} - -/** - * The import lines a module needs: registry.ts's own imports it uses, in - * registry.ts's import order; then the moved top-level names, from the leaf; - * then what stayed in registry.ts (lever (a)), including the class. - */ -function importLines(scope, users, { needsHost = false, moduleRefs = new Map(), leafNames = new Map(), routed = false, className = CLASS_NAME } = {}) { - const bySpecifier = new Map(); - for (const r of users) { - for (const [local, use] of r.imports) { - const b = scope.bindings.get(local); - if (!b || b.kind !== 'import') refuse(`import binding ${local} not found`); - if (!bySpecifier.has(b.specifier)) bySpecifier.set(b.specifier, new Map()); - const slot = bySpecifier.get(b.specifier); - const prev = slot.get(local); - slot.set(local, { ...b, value: Boolean(prev && prev.value) || (use.value && !b.typeOnly) }); - } - } - const out = []; - const specifiers = [...bySpecifier.keys()].sort((a, b) => scope.importOrder.get(a) - scope.importOrder.get(b)); - for (const specifier of specifiers) out.push(...specifierImportLines(specifier, bySpecifier.get(specifier))); - const fromLeaf = new Map(); - const fromHost = new Map(); - for (const [n, use] of moduleRefs) (leafNames.has(n) ? fromLeaf : fromHost).set(n, use); - if (routed) { - // The private-static-through-host rule: `SchemaRegistry.x` in a moved body reads - // the forwarder object; the host interface names the class by an alias. - fromHost.delete(CLASS_NAME); - fromHost.set(`${STATICS_NAME} as ${CLASS_NAME}`, { value: true }); - if (needsHost) fromHost.set(`${CLASS_NAME} as ${CLASS_TYPE_ALIAS}`, { value: false }); - } else if (needsHost && !fromHost.has(className)) fromHost.set(className, { value: false }); - out.push(...importPair(leafSpecifier(), fromLeaf)); - out.push(...importPair('./registry.js', fromHost)); - return out; -} - -/** - * The import lines of one package specifier in a facet module, from its - * bindings (local -> { form, imported, value }): the value statement, then a - * type-only default, a namespace, then the type-only names, each list sorted. - */ -function specifierImportLines(specifier, bindings) { - const out = []; - const entries = [...bindings.entries()].sort(byName); - const named = (value) => entries - .filter(([, e]) => e.form === 'named' && e.value === value) - .map(([local, e]) => (e.imported === local ? local : `${e.imported} as ${local}`)); - const def = entries.find(([, e]) => e.form === 'default'); - const ns = entries.find(([, e]) => e.form === 'namespace'); - const valueNamed = named(true); - const defValue = def && def[1].value ? def[0] : null; - if (defValue || valueNamed.length > 0) { - const parts = [defValue, valueNamed.length > 0 ? `{ ${valueNamed.join(', ')} }` : null].filter(Boolean); - out.push(`import ${parts.join(', ')} from '${specifier}';`); - } - if (def && !def[1].value) out.push(`import type ${def[0]} from '${specifier}';`); - if (ns) out.push(`import ${ns[1].value ? '' : 'type '}* as ${ns[0]} from '${specifier}';`); - const typeNamed = named(false); - if (typeNamed.length > 0) out.push(`import type { ${typeNamed.join(', ')} } from '${specifier}';`); - return out; -} - -/** `text` wrapped into ` * ` comment lines no wider than `width`. */ -function wrapComment(words, width = 80) { - const lines = []; - let line = ' *'; - for (const w of words) { - if (line.length + 1 + w.length > width && line !== ' *') { lines.push(line); line = ' *'; } - line += ` ${w}`; - } - if (line !== ' *') lines.push(line); - return lines; -} - -function paramsText(m) { - return m.getParameters().map((p) => p.getText()); -} - -function forwardList(m) { - return m.getParameters().map((p) => (p.isRestParameter() ? `...${p.getName()}` : p.getName())); -} - -/** A facet module header's import paragraph: values imported from `registry.ts` (lever (a)), or types only. */ -const VALUES_CYCLE_PARA = [ - ' * Values that stayed in `registry.ts` are imported from it (lever (a) of', - ' * ruling 6591753301); every use is inside a function body, so the import', - ' * cycle is never read at evaluation time. `index.ts` re-exports nothing from', - ' * this module.', -].join('\n'); -const TYPES_CYCLE_PARA = [ - ' * The module imports `registry.ts` for types only, so the two modules form no', - ' * evaluation cycle. `index.ts` re-exports nothing from this module.', -].join('\n'); - -/** - * A routed facet module's header paragraph on the statics route: what - * `SchemaRegistry` names there. The first pass writes it for a module it routes, and - * the fourth pass's R10 (#30020) for one it reroutes. - */ -const STATICS_PARA = [ - ` * \`${CLASS_NAME}\` here is \`${STATICS_NAME}\` (imported AS \`${CLASS_NAME}\`): forwarders to the`, - ' * statics the bodies read, private ones included, which the class fills in a', - ` * static block; the class as a type is \`${CLASS_TYPE_ALIAS}\`.`, -].join('\n'); - -/** Does a facet module import a VALUE from `./registry.js`? */ -const importsEngineValues = (text) => /^import \{[^}]*\} from '\.\/registry\.js';$/m.test(text); - -/** - * A facet module's header paragraph made to say what its `./registry.js` lines - * import (R4 of #30020: a move that brings the first value import switches - * it, and the undo switches it back). Refuses a header carrying neither form. - */ -function withCyclePara(text, file) { - const values = importsEngineValues(text); - const [from, to] = values ? [TYPES_CYCLE_PARA, VALUES_CYCLE_PARA] : [VALUES_CYCLE_PARA, TYPES_CYCLE_PARA]; - if (text.includes(to)) return text; - const at = text.indexOf(from); - if (at < 0 || text.indexOf(from, at + 1) >= 0) refuse(`${file}: its header carries no single import paragraph as the first pass writes one`); - return text.slice(0, at) + to + text.slice(at + from.length); -} - -function moduleText(moduleFile, title, movers, scope, leafNames, routed = false) { - const hostName = hostNameOf(moduleFile); - const facet = facetNameOf(moduleFile); - const classType = routed ? CLASS_TYPE_ALIAS : CLASS_NAME; - const hostMembers = new Map(); - const written = new Set(); - for (const r of movers) for (const [member, kind] of r.host) hostMembers.set(member, kind); - for (const r of movers) for (const member of r.writes) written.add(member); - const needsHost = hostMembers.size > 0; - const moduleRefs = new Map(); - for (const r of movers) { - for (const [n, use] of r.moduleRefs) { - const prev = moduleRefs.get(n); - moduleRefs.set(n, { value: Boolean(prev && prev.value) || use.value }); - } - } - const readsHostValues = [...moduleRefs].some(([n, use]) => use.value && !leafNames.has(n)); - const out = []; - out.push('// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.'); - out.push(''); - out.push('/**'); - out.push(...moduleHeaderLines(title, movers.map((r) => r.name))); - out.push(' *'); - out.push(` * A MOVE, not a redesign. {@link ${facet}} is a stateless facet of \`SchemaRegistry\``); - out.push(' * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry`'); - out.push(' * method of the same name exactly as it stood: not re-indented, not'); - out.push(' * formatted, not edited. The method stays on the class as a one-call stub with'); - out.push(' * its signature, so the published surface, an instance spy and a subclass'); - out.push(' * override are what they were. A public or protected method keeps its'); - out.push(' * docblock on the class, the contract; a private method\'s docblock moved here'); - out.push(' * with its body. The state stays on `SchemaRegistry`.'); - out.push(' *'); - if (needsHost) { - out.push(...wrapComment((`The bodies reach the class through {@link ${hostName}}: exactly the \`SchemaRegistry\` ` + - 'members they read, private ones included. Every stub hands its body, by a `.call`, the same host: ' + - `\`${hostVarOf(moduleFile)}\` in \`${HOSTS_MODULE}\`, built once per registry by the class's static block. ` + - 'Methods go as closures that call them on the registry at call time, so a subclass override or an ' + - 'instance spy is still the one reached; fields and accessors go as GETTERS, read when the body reads ' + - 'them, as they were on the class, and a field a body assigns goes with a SETTER that assigns it on ' + - 'the registry.').split(' '))); - } else { - out.push(' * No body reads an `SchemaRegistry` member, so each stub calls its body directly.'); - } - if (routed) { - out.push(' *'); - out.push(STATICS_PARA); - } - out.push(' *'); - out.push(readsHostValues ? VALUES_CYCLE_PARA : TYPES_CYCLE_PARA); - out.push(' */'); - out.push(''); - out.push(...importLines(scope, movers, { needsHost, moduleRefs, leafNames, routed })); - if (needsHost) { - out.push(''); - out.push('/**'); - out.push(` * The \`SchemaRegistry\` members the bodies of {@link ${facet}} read — every one of`); - out.push(' * them, and no other. Each is typed AS the class member (an indexed access on'); - out.push(' * the class), so a change to a member\'s signature on the class is a change'); - out.push(' * here too. `readonly` members are the getters; the others are fields a body'); - out.push(' * assigns.'); - out.push(' */'); - out.push(`export interface ${hostName} {`); - for (const member of [...hostMembers.keys()].sort()) { - // R1's identity port: only a fourth-pass mover reads it (R11 starts its facet with this text). - if (hostMembers.get(member) === 'facade') { out.push(facadeIfaceLine(classType)); continue; } - const ro = hostMembers.get(member) === 'getter' && !written.has(member); - out.push(` ${ro ? 'readonly ' : ''}${member}: ${classType}['${member}'];`); - } - out.push('}'); - } - out.push(''); - out.push(`/** The bodies of the "${title}" section of \`SchemaRegistry\`: a stateless facet, called by the class's stubs. */`); - out.push(`export class ${facet} {`); - movers.forEach((r, i) => { - if (i > 0) out.push(''); - out.push(facetMethodText(r, hostName)); - }); - out.push('}'); - out.push(''); - return out.join('\n'); -} - -/** The opening lines of a facet module's header docblock: its section's title and the names of its bodies. */ -/** - * A facet module's header sentence, wrapped. `tail` is the words a hand edit - * wrote after that sentence in the same paragraph (the fourth pass reads them - * back with `headerNamesOf` and carries them over unedited); the first pass - * writes none. - */ -function moduleHeaderLines(title, names, tail = []) { - const listed = names.map((n, i) => `\`${n}\`${i === names.length - 1 ? '' : ','}`); - return wrapComment([MARK, 'The', `"${title}"`, 'section', 'of', '`SchemaRegistry`:', 'the', 'bodies', 'of', - ...listed, ...HEADER_SENTENCE_END.trim().split(' '), ...tail]); -} - -/** The words that close a facet module's header sentence, after its list of names. */ -const HEADER_SENTENCE_END = ` moved out of \`registry.ts\` whole by \`${SCRIPT}\`.`; - -/** - * One facet member, as its module holds it: the method's docblock (a private - * one) or the placeholder, `static`, and the method's own parameters, return - * type and body text, with `this` a `Pick` of the host when the body reads it. - */ -function facetMethodText(r, hostName, className = CLASS_NAME) { - const m = r.node; - const picks = [...r.host.keys()].sort().map((k) => `'${k}'`).join(' | '); - const params = [...(r.host.size > 0 ? [`this: Pick<${hostName}, ${picks}>`] : []), ...paramsText(m)]; - const ret = m.getReturnTypeNode() ? `: ${m.getReturnTypeNode().getText()}` : ''; - const typeParams = m.getTypeParameters().length > 0 ? `<${m.getTypeParameters().map((t) => t.getText()).join(', ')}>` : ''; - return ` ${r.doc ?? `${placeholderDoc(r.name, className)}\n `}static ${m.isAsync() ? 'async ' : ''}${r.facetName ?? r.name}${typeParams}(${params.join(', ')})${ret} ${identityBodyText(m.getBody(), r.identity)}`; -} - -/** - * A method's body text with R1 applied (#30020): each bare `this` at the - * starts `identity` lists reads `this.facade`. No other byte changes, so the - * undo (`identityUndoText`) is the one inverse rewrite. - */ -function identityBodyText(body, identity = []) { - const start = body.getStart(); - let text = body.getText(); - for (const at of [...identity].sort((a, b) => b - a)) { - if (text.slice(at - start, at - start + 4) !== 'this') refuse(`identity port: no \`this\` at offset ${at}`); - text = `${text.slice(0, at - start)}this.${FACADE_MEMBER}${text.slice(at - start + 4)}`; - } - return text; -} - -/** - * R1's inverse, read off a facet member at head: its body text with each - * `this.facade` its own `this` reads back to a bare `this`. - */ -function identityUndoText(tm, fm) { - const { Node, SyntaxKind } = tm; - const body = fm.getBody(); - const start = body.getStart(); - let text = body.getText(); - const sites = body.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression).filter((pa) => - pa.getExpression().getKind() === SyntaxKind.ThisKeyword && Node.isIdentifier(pa.getNameNode()) && - pa.getName() === FACADE_MEMBER && thisContainer(tm, pa.getExpression()) === fm); - for (const pa of sites.sort((a, b) => b.getStart() - a.getStart())) { - text = `${text.slice(0, pa.getStart() - start)}this${text.slice(pa.getEnd() - start)}`; - } - return text; -} - -/** - * The leaf: the moved top-level statements, each its base text exactly (its - * leading comments and blank lines included), in base order, after the - * imports they use; then one `export { … }` of the moved names base did not - * export and something outside the leaf now imports. - */ -function leafText(scope, top, leafExports) { - // The header is `//` lines, not a `/** */` docblock: the bundled `.d.ts` - // keeps a JSDoc block that leads a module's first statement, and a leaf - // bundled first would publish text the base did not carry (the pilot's - // measurement: 14 added lines in dist/index.d.ts). - const out = []; - out.push('// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.'); - out.push(''); - out.push(...wrapComment([MARK, 'The', 'top-level', 'declarations', 'ahead', 'of', '`SchemaRegistry`', 'in', '`registry.ts`', - 'that', 'moved', 'out', 'whole', 'by', `\`${SCRIPT}\``, '(lever', '(b)', 'of', 'ruling', - '6591753301).']).map((l) => l.replace(/^ \*/, '//'))); - out.push('//'); - out.push('// A MOVE, not a redesign: each declaration below is its `registry.ts` text'); - out.push('// exactly, with the comments above it, in the order it stood there. A LEAF:'); - out.push('// it imports neither `registry.ts` nor any `registry-*` module, so it is'); - out.push('// evaluated before the class and forms no cycle with it. `registry.ts`'); - out.push('// re-exports, by name, every declaration here it exported before, so every'); - out.push('// importer of `./registry.js` reads what it read.'); - out.push(''); - out.push(...importLines(scope, top.movers)); - let text = out.join('\n'); - for (const r of top.movers) text += r.node.getFullText(); - if (leafExports.length > 0) { - text += `\n\n// ${MARK} Declared above without \`export\`, and read by \`registry.ts\` or a section module.\n`; - text += `${namedStatement('export', [...leafExports].sort(), ';')}`; - } - return `${text}\n`; -} - -const DOC_MOVED = 'its docblock with it'; - -/** - * A stub's body: the marker line, then ONE call of the facet member of the - * same name. A body that reads the class is called with its facet's host, the - * one object per registry `HOSTS_MODULE` keeps (#30051), not a literal built per - * call. `node` is the method (base or head: the stub keeps its signature). - * Pure: `verify` rebuilds every stub with it and compares. - */ -function stubBody({ node, name, usesHost, docMoved }, moduleFile, closeIndent, mark = MARK) { - const ind = `${closeIndent} `; - const hostName = hostNameOf(moduleFile); - // A generic method calls its body INSTANTIATED with its own type - // parameters (`Function#call` alone would erase them). - const typeArgs = node.getTypeParameters().map((t) => t.getName()); - const fn = typeArgs.length > 0 ? `(${facetNameOf(moduleFile)}.${name}<${typeArgs.join(', ')}>)` : `${facetNameOf(moduleFile)}.${name}`; - const out = ['{']; - const args = forwardList(node).join(', '); - const doc = docMoved ? `, ${DOC_MOVED}` : ''; - if (!usesHost) { - out.push(`${ind}// ${mark} The body moved to \`${moduleFile}\`${doc}.`); - out.push(`${ind}return ${fn}(${args});`); - } else { - out.push(`${ind}// ${mark} The body moved to \`${moduleFile}\`${doc} (see \`${hostName}\`).`); - out.push(`${ind}return ${fn}.call(${hostVarOf(moduleFile)}.of(this)${args ? `, ${args}` : ''});`); - } - out.push(`${closeIndent}}`); - return out.join('\n'); -} - -/** The constructor's forwarded arguments: the parameters a moved part reads, in declaration order. */ -const ctorArgs = (node, read) => forwardList(node).filter((_, i) => read.has(node.getParameters()[i].getName())); - -/** - * A moved constructor's body (#30020 R8): the marker line, each statement R8 - * keeps (`this.F = Facet.constructF(…)`), then ONE call of the facet's - * `construct`. Gate wiring only: `this.x = ` and a - * forwarding call. Pure: `verify` rebuilds it and compares. - */ -function ctorStubBody({ node, usesHost, r8, paramsRead }, moduleFile, closeIndent, mark = MARK) { - const ind = `${closeIndent} `; - const facet = facetNameOf(moduleFile); - const out = ['{']; - out.push(usesHost - ? `${ind}// ${mark} The body moved to \`${moduleFile}\` (see \`${hostNameOf(moduleFile)}\`).` - : `${ind}// ${mark} The body moved to \`${moduleFile}\`.`); - for (const a of r8) out.push(`${ind}this.${a.field} = ${facet}.${a.fn}(${ctorArgs(node, a.params).join(', ')});`); - const args = ctorArgs(node, paramsRead).join(', '); - out.push(usesHost - ? `${ind}${facet}.${CTOR_FACET_MEMBER}.call(${hostVarOf(moduleFile)}.of(this)${args ? `, ${args}` : ''});` - : `${ind}${facet}.${CTOR_FACET_MEMBER}(${args});`); - out.push(`${closeIndent}}`); - return out.join('\n'); -} - -/** R8's facet member: the value the constructor statement R8 keeps on the class assigns. */ -function r8FnText(node, a, classType) { - const params = node.getParameters().filter((p) => a.params.has(p.getName())).map((p) => p.getText()); - return ` /** R8 (#30020): the value \`${CLASS_NAME}\`'s constructor assigns to \`${a.field}\`, a field only a constructor may assign. */\n` + - ` static ${a.fn}(${params.join(', ')}): ${classType}['${a.field}'] {\n return ${a.exprText};\n }`; -} - -/** - * The constructor's facet member: its parameters the body reads, `this` a - * `Pick` of the host when it reads the class, and its body with R1 applied and - * each statement R8 kept on the class replaced by a marker line. - */ -function ctorFacetText(r, hostName) { - const m = r.node; - const picks = [...r.host.keys()].sort().map((k) => `'${k}'`).join(' | '); - const params = [...(r.host.size > 0 ? [`this: Pick<${hostName}, ${picks}>`] : []), - ...m.getParameters().filter((p) => r.paramsRead.has(p.getName())).map((p) => p.getText())]; - const body = m.getBody(); - const start = body.getStart(); - const text = body.getText(); - const edits = []; - for (const at of r.identity) { - if (text.slice(at - start, at - start + 4) !== 'this') refuse(`identity port: no \`this\` at offset ${at}`); - edits.push({ start: at - start, end: at - start + 4, text: `this.${FACADE_MEMBER}` }); - } - for (const a of r.r8) edits.push({ start: a.start - start, end: a.end - start, text: r8MarkerText(a.field, a.fn) }); - return ` /** The body of \`${CLASS_NAME}\`'s constructor; the constructor there keeps its signature and docblock. */\n` + - ` static ${CTOR_FACET_MEMBER}(${params.join(', ')}): void ${applyOrderedEdits(text, edits)}`; -} - -/** The facet member texts a moved constructor becomes, in module order: R8's, then `construct`. */ -function ctorFacetTexts(r, hostName, classType) { - return [...r.r8.map((a) => r8FnText(r.node, a, classType)), ctorFacetText(r, hostName)]; -} - -/** The facets whose bodies read the class, as `hostsTexts` takes them, from the movers of each module. */ -function facetsOf(modules) { - const facets = []; - for (const [moduleFile, movers] of modules) { - const members = new Map(); - const written = new Set(); - for (const r of movers) for (const [member, kind] of r.host) members.set(member, kind); - for (const r of movers) for (const member of r.writes) written.add(member); - if (members.size > 0) facets.push({ moduleFile, members, written }); - } - return facets; -} - -/** - * Apply a plan to `text`. Returns `{ host, modules, pruned }`: the new - * `registry.ts`, a Map of module basename -> text (the leaf among them), and the - * import bindings the prune dropped. Pure. - */ -function generate(tm, text, planned) { - const { sf, ctx, modules, top, hostUses, leafExports, hostExportsForSections, routedModules, routedStatics } = planned; - const edits = []; - const importBlock = []; - if (top.movers.length > 0) { - const leafUses = new Map([...hostUses].filter(([n]) => top.moverNames.has(n))); - const lines = importPair(leafSpecifier(), leafUses); - if (lines.length > 0) { - importBlock.push(`\n// ${MARK} The top-level declarations that moved whole to \`${LEAF_MODULE}\`, as this file still reads them.`); - for (const l of lines) importBlock.push(`\n${l}`); - } - const reexports = top.movers.filter((r) => r.exported) - .flatMap((r) => r.names.map((n) => [n, r.typeOnly ? `type ${n}` : n])) - .sort(byName).map(([, spelled]) => spelled); - if (reexports.length > 0) { - importBlock.push(`\n// ${MARK} Re-exported by name, so every importer of \`./registry.js\` reads what it read.`); - importBlock.push(`\n${namedStatement('export', reexports, ` from '${leafSpecifier()}';`)}`); - } - for (const r of top.movers) edits.push({ start: r.node.getFullStart(), end: r.node.getEnd(), text: '' }); - } - for (const [moduleFile, movers] of modules) { - const title = movers[0].section; - const hostName = hostNameOf(moduleFile); - const anyHost = movers.some((r) => r.host.size > 0); - importBlock.push(`\n// ${MARK} ${title}: the methods of the same names below hand the bodies` + - `${anyHost ? ` what they read (\`${hostName}\`)` : ' their arguments'}.`); - importBlock.push(`\nimport { ${facetNameOf(moduleFile)} } from './${moduleFile.replace(/\.ts$/, '.js')}';`); - for (const r of movers) { - const body = r.node.getBody(); - const lineStart = text.lastIndexOf('\n', body.getEnd() - 1) + 1; - const closeIndent = text.slice(lineStart, body.getEnd() - 1); - if (!/^ *$/.test(closeIndent)) refuse(`${r.name}: the body's closing brace does not open its own line`); - if (r.host.size > 0 && r.node.getParameters().some((p) => p.getName() === hostVarOf(moduleFile))) { - refuse(`${r.name}: a parameter is named ${hostVarOf(moduleFile)}, which its stub reads as the facet's host`); - } - const stub = stubBody({ node: r.node, name: r.name, usesHost: r.host.size > 0, docMoved: Boolean(r.doc) }, moduleFile, closeIndent); - edits.push({ start: body.getStart(), end: body.getEnd(), text: stub }); - const asyncKw = r.node.getModifiers().find((x) => x.getKind() === tm.SyntaxKind.AsyncKeyword); - if (asyncKw) edits.push({ start: asyncKw.getStart(), end: r.node.getNameNode().getStart(), text: '' }); - if (r.doc) edits.push({ start: r.node.getStart(true), end: r.node.getStart(), text: '' }); - } - } - // After the class's last member: the static block that fills the statics - // forwarders, then the one that installs the facet hosts (one edit, so the - // two keep this order). - const tail = []; - if (routedStatics.size > 0) { - // The private-static-through-host rule: the forwarder object ahead of the - // class (ahead of its leading comments), the static block that fills it - // after the class's last member. - const { ahead, block } = staticsTexts(routedStatics); - edits.push({ start: ctx.cls.getFullStart(), end: ctx.cls.getFullStart(), text: `\n\n${ahead}` }); - tail.push(`\n\n${block}`); - } - const facets = facetsOf(modules); - const hosts = facets.length > 0 ? hostsTexts(facets) : null; - if (hosts) { - for (const f of facets) { - if (ctx.scope.bindings.has(hostVarOf(f.moduleFile))) refuse(`registry.ts already binds ${hostVarOf(f.moduleFile)}, the name of ${f.moduleFile}'s host`); - } - tail.push(`\n\n${hosts.block}`); - importBlock.push(`\n// ${MARK} The facets' hosts, one per registry, which the static block at the end of the class builds.`); - importBlock.push(`\n${namedStatement('import', facets.map((f) => hostVarOf(f.moduleFile)).sort(), ` from './${HOSTS_MODULE.replace(/\.ts$/, '.js')}';`)}`); - } - if (tail.length > 0) { - const last = ctx.cls.getMembers().at(-1); - edits.push({ start: last.getEnd(), end: last.getEnd(), text: tail.join('') }); - } - if (hostExportsForSections.size > 0) { - const spelled = [...hostExportsForSections.keys()].sort().map((n) => { - const st = ctx.sf.getStatements().find((s) => (declaredNames(tm, s) ?? []).includes(n)); - return st && (tm.Node.isInterfaceDeclaration(st) || tm.Node.isTypeAliasDeclaration(st)) ? `type ${n}` : n; - }); - importBlock.push(`\n// ${MARK} Exported for the section modules above, which import them (lever (a)); \`index.ts\` re-exports by name, so none reaches the package.`); - importBlock.push(`\n${namedStatement('export', spelled, ';')}`); - } - const lastImport = sf.getImportDeclarations().at(-1); - if (!lastImport) refuse('registry.ts has no import declaration to place the generated imports after'); - if (importBlock.length > 0) edits.push({ start: lastImport.getEnd(), end: lastImport.getEnd(), text: importBlock.join('') }); - edits.sort((a, b) => b.start - a.start || b.end - a.end); - let out = text; - let floor = Infinity; - for (const e of edits) { - if (e.end > floor) refuse('overlapping edits'); - out = out.slice(0, e.start) + e.text + out.slice(e.end); - floor = e.start; - } - // The imports only moved text read are dropped (the package compiles with - // `noUnusedLocals`); `verify` recomputes the same prune from the head. - const pruned = pruneImports(tm, out, usedImportNames(tm, out)); - const moduleTexts = new Map(); - const leafNames = top.moverNames; - if (top.movers.length > 0) moduleTexts.set(LEAF_MODULE, leafText(ctx.scope, top, leafExports)); - for (const [moduleFile, movers] of modules) { - moduleTexts.set(moduleFile, moduleText(moduleFile, movers[0].section, movers, ctx.scope, leafNames, routedModules.has(moduleFile))); - } - if (hosts) moduleTexts.set(HOSTS_MODULE, hosts.module); - return { host: pruned.text, modules: moduleTexts, pruned: pruned.removed }; -} - -// ── The import prune ───────────────────────────────────────────────────────── - -/** Is `node` (a compiler node) inside a JSDoc comment? */ -function inJsDocNode(ts, node) { - for (let n = node; n; n = n.parent) { - if (n.kind >= ts.SyntaxKind.FirstJSDocNode && n.kind <= ts.SyntaxKind.LastJSDocNode) return true; - } - return false; -} - -/** Is the identifier a property NAME (`a.NAME`, `{ NAME: … }`, a member name), not a reference? */ -function isPropertyNameIdentifier(ts, node) { - const p = node.parent; - if (!p) return false; - if (ts.isPropertyAccessExpression(p) && p.name === node) return true; - if ((ts.isPropertyAssignment(p) || ts.isPropertyDeclaration(p) || ts.isPropertySignature(p) || - ts.isMethodDeclaration(p) || ts.isMethodSignature(p) || ts.isGetAccessorDeclaration(p) || - ts.isSetAccessorDeclaration(p) || ts.isEnumMember(p)) && p.name === node) return true; - if (ts.isQualifiedName(p) && p.right === node) return true; - return false; -} - -/** - * The import bindings of an `registry.ts` that something outside its import - * declarations reads: a reference in code or a type, a local `export { … }`, - * or a JSDoc `{@link X}` / `@see X` name. Those JSDoc names count because the - * compiler's `noUnusedLocals` counts them; other JSDoc text and `//` comments - * do not. - */ -function usedImportNames(tm, text) { - const ts = tm.ts; - const sfm = parse(tm, HOST_PATH, text); - const sf = sfm.compilerNode; - const checker = sfm.getProject().getTypeChecker().compilerObject; - const used = new Set(); - const isImportDecl = (d) => d.getSourceFile() === sf && (ts.isImportSpecifier(d) || ts.isImportClause(d) || ts.isNamespaceImport(d)); - const leftmost = (name) => { - let n = name; - while (n && (ts.isQualifiedName(n) || ts.isPropertyAccessExpression(n) || n.kind === ts.SyntaxKind.JSDocMemberName)) { - n = ts.isQualifiedName(n) || n.kind === ts.SyntaxKind.JSDocMemberName ? n.left : n.expression; - } - return n && ts.isIdentifier(n) ? n : undefined; - }; - const visitDoc = (node) => { - const linkish = node.kind === ts.SyntaxKind.JSDocLink || node.kind === ts.SyntaxKind.JSDocLinkCode || - node.kind === ts.SyntaxKind.JSDocLinkPlain || node.kind === ts.SyntaxKind.JSDocNameReference; - if (linkish && node.name) { - const id = leftmost(node.name); - const symbol = id && checker.getSymbolAtLocation(id); - if (id && (symbol?.declarations ?? []).some(isImportDecl)) used.add(id.text); - } - if (Array.isArray(node.comment)) node.comment.forEach(visitDoc); - ts.forEachChild(node, visitDoc); - }; - const visit = (node) => { - if (ts.isImportDeclaration(node)) return; - for (const doc of node.jsDoc ?? []) visitDoc(doc); - if (ts.isExportDeclaration(node) && node.moduleSpecifier) return; - if (ts.isIdentifier(node) && !inJsDocNode(ts, node)) { - const parent = node.parent; - let symbol; - if (ts.isExportSpecifier(parent)) symbol = checker.getExportSpecifierLocalTargetSymbol(parent); - else if (ts.isShorthandPropertyAssignment(parent) && parent.name === node) symbol = checker.getShorthandAssignmentValueSymbol(parent); - else if (!isPropertyNameIdentifier(ts, node)) symbol = checker.getSymbolAtLocation(node); - if ((symbol?.declarations ?? []).some(isImportDecl)) used.add(node.text); - } - ts.forEachChild(node, visit); - }; - visit(sf); - return used; -} - -/** - * Drop every import binding `used` lacks. A declaration left with no binding - * goes whole (its own text and the line break after it; the comments above it - * stay, unless `dropAttachedComments`: then the `//` lines directly above it, - * with no blank line between, go with it); one left with some keeps them, a - * one-line `{ … }` on one line, a multi-line one as one binding per line. - * Pure. Returns `{ text, removed }`. - */ -function pruneImports(tm, text, used, { dropAttachedComments = false } = {}) { - const ts = tm.ts; - const sf = parseSourceFile(HOST_PATH, text); - const edits = []; - const removed = []; - for (const st of sf.statements) { - if (!ts.isImportDeclaration(st) || !st.importClause) continue; - const clause = st.importClause; - const nb = clause.namedBindings; - const named = nb && ts.isNamedImports(nb) ? [...nb.elements] : []; - const dropDefault = Boolean(clause.name && !used.has(clause.name.text)); - const dropNs = Boolean(nb && ts.isNamespaceImport(nb) && !used.has(nb.name.text)); - const dropNamed = named.filter((e) => !used.has(e.name.text)); - if (!dropDefault && !dropNs && dropNamed.length === 0) continue; - if (dropDefault) removed.push(clause.name.text); - if (dropNs) removed.push(nb.name.text); - for (const e of dropNamed) removed.push(e.name.text); - const keepDefault = Boolean(clause.name && !dropDefault); - const keepNs = Boolean(nb && ts.isNamespaceImport(nb) && !dropNs); - const keepNamed = named.filter((e) => used.has(e.name.text)); - if (!keepDefault && !keepNs && keepNamed.length === 0) { - const end = text[st.getEnd()] === '\n' ? st.getEnd() + 1 : st.getEnd(); - let start = st.getStart(sf); - if (dropAttachedComments) { - // Walk up the `//` lines that touch the declaration (no blank line between). - for (;;) { - const prevEnd = start - 1; - if (prevEnd <= 0 || text[prevEnd] !== '\n') break; - const lineStart = text.lastIndexOf('\n', prevEnd - 1) + 1; - if (!/^\s*\/\/.*$/.test(text.slice(lineStart, prevEnd))) break; - start = lineStart; - } - } - edits.push({ start, end, text: '' }); - continue; - } - if (nb && ts.isNamedImports(nb) && dropNamed.length > 0) { - if (keepNamed.length === 0) { - edits.push({ start: clause.name.getEnd(), end: nb.getEnd(), text: '' }); - } else { - const one = !nb.getText(sf).includes('\n'); - const parts = keepNamed.map((e) => e.getText(sf)); - edits.push({ start: nb.getStart(sf), end: nb.getEnd(), text: one ? `{ ${parts.join(', ')} }` : `{\n${parts.map((x) => ` ${x},`).join('\n')}\n}` }); - } - } - if (dropNs) edits.push({ start: clause.name.getEnd(), end: nb.getEnd(), text: '' }); - if (dropDefault) edits.push({ start: clause.name.getStart(sf), end: nb.getStart(sf), text: '' }); - } - edits.sort((a, b) => b.start - a.start); - let out = text; - for (const e of edits) out = out.slice(0, e.start) + e.text + out.slice(e.end); - return { text: out, removed: removed.sort() }; -} - -// ── Proof 1: byte-identity ─────────────────────────────────────────────────── - -const sha256 = (text) => createHash('sha256').update(text).digest('hex'); - -/** - * Prove a split against its base, from the texts alone (it shares no state with - * `generate`): every generated function is its base method's text, every leaf - * declaration is its base statement's text, and undoing the split rebuilds the - * base `registry.ts` byte-for-byte, once the base's import bindings the head no - * longer reads are pruned from it. - * - * Undoing is: put each body back on its stub (and `async` back), drop the - * generated import / export statements, then splice the leaf's statements back - * between the statements that stayed — the interleaving read off the base by - * NAME only, so every byte of the result comes from the head or the leaf. - * - * The one edit the undo does not reverse by reading the head is the import - * prune: it is recomputed from the head (which import bindings does the head - * still read?) and applied to the base, and every binding it drops must occur - * in a moved body or a moved declaration, so a binding the head lost for any - * other reason fails. - * - * Returns `{ failures, moved, movedTop, pruned, linesBefore, linesAfter }`; no failure = proved. - */ -function verify(tm, baseText, headText, moduleTexts, { readSibling = null } = {}) { - const { Node } = tm; - const failures = []; - const moved = []; - const movedTop = []; - const baseSf = parse(tm, HOST_PATH, baseText); - const baseCls = baseSf.getClass(CLASS_NAME); - const headSf = parse(tm, HOST_PATH, headText); - const headCls = headSf.getClass(CLASS_NAME); - const none = { failures: [`no class ${CLASS_NAME} at base or head`], moved, movedTop, pruned: [], linesBefore: 0, linesAfter: 0 }; - if (!baseCls || !headCls) return none; - const prune = pruneImports(tm, baseText, usedImportNames(tm, headText)); - const target = prune.text; - - const modules = new Map(); - let leaf = null; - let hostsOnDisk = null; - for (const [file, text] of moduleTexts) { - if (!text.includes(MARK)) continue; - if (file === HOSTS_MODULE) { hostsOnDisk = text; continue; } - const sf = parse(tm, join(SRC_DIR, file), text); - if (file === LEAF_MODULE) { - const statements = []; - const stmts = sf.getStatements(); - // The generated header is leading trivia of the first statement when no - // import precedes it; a moved statement's text starts after it. - const headerEnd = text.indexOf('\n\n', text.indexOf(MARK)) + 1; - stmts.forEach((st, i) => { - if (Node.isImportDeclaration(st)) { - if (st.getModuleSpecifierValue() === './registry.js' || SEAM_SPECIFIER.test(st.getModuleSpecifierValue())) { - failures.push(`${file}: imports ${st.getModuleSpecifierValue()} — a leaf never imports registry.ts or a registry-* module`); - } - return; - } - if (i === stmts.length - 1 && Node.isExportDeclaration(st) && !st.getModuleSpecifier() && st.getFullText().includes(MARK)) return; - const names = declaredNames(tm, st); - if (!names) { failures.push(`${file}: a statement that is not a moved declaration: ${st.getText().slice(0, 60)}`); return; } - statements.push({ names, text: text.slice(Math.max(st.getFullStart(), headerEnd), st.getEnd()), node: st }); - }); - leaf = { file, sf, statements }; - if (readSibling) { - // The leaf's relative VALUE imports, followed on disk: none may reach - // registry.ts or a registry-* module. A type-only import is erased, so it - // forms no evaluation cycle and is not followed. - const seen = new Set(); - const queue = valueSpecifiers(text).map((spec) => [file, spec]); - while (queue.length > 0) { - const [from, spec] = queue.shift(); - let rel = relative(SRC_DIR, resolve(dirname(join(SRC_DIR, from)), spec.replace(/\.js$/, '.ts'))); - if (seen.has(rel)) continue; - seen.add(rel); - if (rel === HOST_BASENAME || /^registry-[a-z0-9-]+\.ts$/.test(rel)) { - failures.push(`${file}: reaches ${rel} through its value imports (from ${from}) — not a leaf`); - continue; - } - let sibling = readSibling(rel); - if (sibling === null) { - const index = rel.replace(/\.ts$/, '/index.ts'); - sibling = readSibling(index); - if (sibling !== null) rel = index; - } - if (sibling === null) continue; - for (const next of valueSpecifiers(sibling)) queue.push([rel, next]); - } - } - continue; - } - // A facet module: imports, the host interface, and ONE class, the facet, - // whose every member is a static method that is a moved body. - const fns = new Map(); - const facet = facetNameOf(file); - for (const st of sf.getStatements()) { - if (Node.isImportDeclaration(st)) continue; - if (Node.isInterfaceDeclaration(st) && st.getName() === hostNameOf(file)) continue; - if (Node.isClassDeclaration(st) && st.getName() === facet && !fns.size) { - for (const member of st.getMembers()) { - if (Node.isMethodDeclaration(member) && member.isStatic() && member.getBody() && !fns.has(member.getName())) fns.set(member.getName(), member); - else failures.push(`${file}: ${facet} has a member that is not a moved body: ${member.getText().slice(0, 60)}`); - } - continue; - } - failures.push(`${file}: a top-level statement that is neither an import, the host interface nor the facet class ${facet}: ${st.getText().slice(0, 60)}`); - } - modules.set(file, { sf, text, fns, claimed: new Set() }); - } - - const marker = new RegExp(`^\\{\\s*// ${MARK.replace(/[[\]]/g, '\\$&')} The body moved to \`(registry-[a-z0-9-]+\\.ts)\`(, ${DOC_MOVED})?`); - const movedDocs = []; - const edits = []; - for (const m of headCls.getMethods()) { - const body = m.getBody(); - if (!body) continue; - const hit = marker.exec(body.getText()); - if (!hit) continue; - const name = m.getName(); - const mod = modules.get(hit[1]); - const fn = mod && mod.fns.get(name); - if (!fn) { failures.push(`${name}: its stub names ${hit[1]}, which has no function ${name}`); continue; } - mod.claimed.add(name); - // The stub itself is exactly what `stubBody` writes for this method: one - // call of the facet member of the same name, with the facet's own host - // when the body reads the class (#30051). - { - const lineStart = headText.lastIndexOf('\n', body.getEnd() - 1) + 1; - const closeIndent = headText.slice(lineStart, body.getEnd() - 1); - const usesHost = fn.getParameters().some((p) => p.getName() === 'this'); - const want = stubBody({ node: m, name, usesHost, docMoved: Boolean(hit[2]) }, hit[1], closeIndent); - if (body.getText() !== want) failures.push(`${name}: its stub is not the generated one-call stub (want ${JSON.stringify(want.split('\n').at(-2).trim())})`); - } - const base = baseCls.getMethods().find((x) => x.getName() === name && x.isStatic() === m.isStatic()); - if (!base || !base.getBody()) { failures.push(`${name}: no method of that name with a body at base`); continue; } - const fnParams = fn.getParameters().filter((p) => p.getName() !== 'this').map((p) => p.getText()); - const baseParams = base.getParameters().map((p) => p.getText()); - const same = { - body: fn.getBody().getText() === base.getBody().getText(), - params: JSON.stringify(fnParams) === JSON.stringify(baseParams), - returnType: (fn.getReturnTypeNode()?.getText() ?? null) === (base.getReturnTypeNode()?.getText() ?? null), - async: fn.isAsync() === base.isAsync(), - typeParameters: JSON.stringify(fn.getTypeParameters().map((t) => t.getText())) === JSON.stringify(base.getTypeParameters().map((t) => t.getText())), - static: m.isStatic() === base.isStatic(), - }; - // Docblock ownership: the facet member's leading text is the base - // method's docblock (the stub says so, and only a private method's), else - // exactly the placeholder; the undo puts a moved docblock back. - const lead = mod.text.slice(fn.getStart(true), fn.getStart()); - const docMoved = Boolean(hit[2]); - if (docMoved) { - if (m.getStart(true) !== m.getStart()) failures.push(`${name}: its stub says the docblock moved, but the class still carries one`); - if (m.getScope() !== 'private') failures.push(`${name}: a ${m.getScope()} method's docblock moved off the class (only a private one may)`); - edits.push({ start: m.getStart(), end: m.getStart(), text: lead }); - movedDocs.push(lead); - } else if (lead !== `${placeholderDoc(name)}\n `) { - failures.push(`${name}: the facet member's leading text is neither its moved docblock nor the placeholder`); - } - for (const [what, ok] of Object.entries(same)) if (!ok) failures.push(`${name}: the generated function's ${what} is not the base method's`); - moved.push({ - name, module: hit[1], bodySha: sha256(fn.getBody().getText()), baseBodySha: sha256(base.getBody().getText()), - lines: base.getBody().getEndLineNumber() - base.getBody().getStartLineNumber() + 1, text: fn.getText(), docMoved, ...same, - }); - edits.push({ start: body.getStart(), end: body.getEnd(), text: fn.getBody().getText() }); - if (fn.isAsync()) edits.push({ start: m.getNameNode().getStart(), end: m.getNameNode().getStart(), text: 'async ' }); - } - - // The statics route: the forwarder type and object ahead of the class and - // the static block that fills them must be exactly what `staticsTexts` - // writes for the statics the type names, each forwarded as the BASE class - // declares it (a method called, anything else read). Then the undo drops - // them. - const staticsHost = headSf.getInterface(STATICS_HOST_NAME); - const staticsVar = headSf.getVariableStatement(STATICS_NAME); - const markedBlocks = headCls.getMembers().filter((x) => Node.isClassStaticBlockDeclaration(x) && x.getFullText().includes(MARK)); - const hostsBlocks = markedBlocks.filter((x) => x.getFullText().includes(`// ${MARK} Installs`)); - const staticBlocks = markedBlocks.filter((x) => !hostsBlocks.includes(x)); - if (staticsHost || staticsVar || staticBlocks.length > 0) { - const kinds = new Map(); - for (const member of staticsHost ? staticsHost.getProperties() : []) { - const n = member.getName(); - const declared = baseCls.getMembers().filter((x) => typeof x.isStatic === 'function' && x.isStatic() && typeof x.getName === 'function' && x.getName() === n); - if (declared.length === 0) failures.push(`${STATICS_HOST_NAME}.${n}: the base class declares no static of that name`); - kinds.set(n, declared.some((x) => Node.isMethodDeclaration(x)) ? 'method' : 'getter'); - } - const { ahead, block } = staticsTexts(kinds); - const aheadAt = staticsHost ? headText.lastIndexOf(`// ${MARK} The statics of`, staticsHost.getStart()) : -1; - const aheadEnd = staticsVar ? staticsVar.getEnd() : -1; - if (!staticsHost || !staticsVar || aheadAt < 0 || headText.slice(aheadAt, aheadEnd) !== ahead) { - failures.push(`${STATICS_HOST_NAME} / ${STATICS_NAME} are not the generated forwarder declarations for the statics they name`); - } else { - edits.push({ start: headText.lastIndexOf('\n\n', aheadAt), end: aheadEnd, text: '' }); - } - const blockAt = staticBlocks.length === 1 ? headText.lastIndexOf(` // ${MARK} Fills`, staticBlocks[0].getStart()) : -1; - if (staticBlocks.length !== 1 || blockAt < 0 || headText.slice(blockAt, staticBlocks[0].getEnd()) !== block) { - failures.push(`the class's static block is not the generated filler of ${STATICS_NAME} for the statics it names`); - } else { - edits.push({ start: headText.lastIndexOf('\n\n', blockAt), end: staticBlocks[0].getEnd(), text: '' }); - } - } - for (const [file, mod] of modules) { - for (const name of mod.fns.keys()) if (!mod.claimed.has(name)) failures.push(`${file}: function ${name} has no stub on the class`); - } - - // The facet hosts (#30051): `HOSTS_MODULE` and the static block that - // installs the factories must be exactly what `hostsTexts` writes for the - // facets on disk, each member forwarded as the BASE class declares it (a - // method as a closure, anything else as a getter, a member the interface - // does not mark `readonly` with a setter too). Then the undo drops both. - const baseMembers = instanceMembers(tm, baseCls); - const facets = []; - for (const [file, mod] of modules) { - const iface = mod.sf.getInterface(hostNameOf(file)); - if (!iface) continue; - const members = new Map(); - const written = new Set(); - for (const p of iface.getProperties()) { - const n = p.getName(); - const info = baseMembers.get(n); - if (!info) { failures.push(`${file}: ${hostNameOf(file)}.${n} names no instance member of the base class`); continue; } - members.set(n, info.kind === 'method' ? 'method' : 'getter'); - if (info.kind !== 'method' && !p.isReadonly()) written.add(n); - } - facets.push({ moduleFile: file, members, written }); - } - if (facets.length > 0 || hostsOnDisk !== null || hostsBlocks.length > 0) { - const want = hostsTexts(facets); - if (hostsOnDisk !== want.module) failures.push(`${HOSTS_MODULE} is not the generated host module for the facets on disk`); - const blockAt = hostsBlocks.length === 1 ? headText.lastIndexOf(` // ${MARK} Installs`, hostsBlocks[0].getStart()) : -1; - if (hostsBlocks.length !== 1 || blockAt < 0 || headText.slice(blockAt, hostsBlocks[0].getEnd()) !== want.block) { - failures.push('the class\'s host static block is not the generated installer of the facet hosts'); - } else { - edits.push({ start: headText.lastIndexOf('\n\n', blockAt), end: hostsBlocks[0].getEnd(), text: '' }); - } - } - const generatedSpecifier = (spec) => { - const hit = /^\.\/(registry-[a-z0-9-]+)\.js$/.exec(spec ?? ''); - return Boolean(hit && (modules.has(`${hit[1]}.ts`) || `${hit[1]}.ts` === HOSTS_MODULE || (leaf && `${hit[1]}.ts` === leaf.file))); - }; - for (const st of headSf.getStatements()) { - const drop = (Node.isImportDeclaration(st) && generatedSpecifier(st.getModuleSpecifierValue())) || - (Node.isExportDeclaration(st) && (st.getModuleSpecifier() - ? generatedSpecifier(st.getModuleSpecifierValue()) - : st.getFullText().includes(MARK))); - if (drop) edits.push({ start: st.getFullStart(), end: st.getEnd(), text: '' }); - } - edits.sort((a, b) => b.start - a.start || b.end - a.end); - let rebuilt = headText; - for (const e of edits) rebuilt = rebuilt.slice(0, e.start) + e.text + rebuilt.slice(e.end); - - if (leaf) { - // Splice the leaf back, in the order the base interleaves it by NAME. - const restSf = parse(tm, HOST_PATH, rebuilt); - const rest = restSf.getStatements(); - const queue = [...leaf.statements]; - const baseStmts = parse(tm, HOST_PATH, target).getStatements(); - let j = 0; - let assembled = rest.length > 0 ? rebuilt.slice(0, rest[0].getFullStart()) : ''; - for (const b of baseStmts) { - const names = declaredNames(tm, b); - const key = names ? names.join(',') : null; - if (queue.length > 0 && key !== null && queue[0].names.join(',') === key) { - const l = queue.shift(); - assembled += l.text; - const same = l.text === b.getFullText(); - if (!same) failures.push(`${leaf.file}: ${key} is not its base text (leading comments included)`); - movedTop.push({ - name: key, lines: b.getEndLineNumber() - b.getStartLineNumber(true) + 1, - sha: sha256(l.text), baseSha: sha256(b.getFullText()), same, exported: Node.isExportable(b) && b.isExported(), - }); - continue; - } - if (j >= rest.length) { failures.push(`undoing the split runs out of head statements at base ${key ?? b.getKindName()}`); break; } - assembled += rest[j].getFullText(); - j += 1; - } - if (queue.length > 0) failures.push(`${leaf.file}: ${queue.map((q) => q.names.join(',')).join('; ')} not placed — out of base order or not in the base`); - if (rest.length > 0) assembled += rebuilt.slice(rest[rest.length - 1].getEnd()); - rebuilt = assembled; - } - - const movedText = [...moved.map((m) => m.text), ...movedDocs, ...(leaf ? leaf.statements.map((s) => s.text) : [])].join('\n'); - for (const n of prune.removed) { - if (!new RegExp(`(^|[^\\w$])${n.replace(/\$/g, '\\$')}($|[^\\w$])`).test(movedText)) { - failures.push(`the head dropped the import \`${n}\`, which no moved body or declaration reads`); - } - } - if (rebuilt !== target) { - const a = rebuilt.split('\n'); - const b = target.split('\n'); - let i = 0; - while (i < Math.max(a.length, b.length) && a[i] === b[i]) i += 1; - failures.push(`undoing the split does not rebuild the base registry.ts (its unread imports pruned): first difference at line ${i + 1}` + - `\n rebuilt: ${JSON.stringify(a[i] ?? '')}\n base: ${JSON.stringify(b[i] ?? '')}`); - } - if (moved.length === 0 && movedTop.length === 0) failures.push('no moved method or declaration found: a proof over nothing is not a proof'); - return { failures, moved, movedTop, pruned: prune.removed, linesBefore: lineCount(baseText), linesAfter: lineCount(headText), - routedStatics: staticsHost ? staticsHost.getProperties().length : 0 }; -} - -/** Lines as `wc -l` counts them: newline characters. */ -function lineCount(text) { - return text.split('\n').length - 1; -} - -// ── Reports ────────────────────────────────────────────────────────────────── - -function planReport(planned) { - const out = []; - const bySection = new Map(); - for (const r of planned.records) { - const key = r.section ?? '(above the first anchor)'; - if (!bySection.has(key)) bySection.set(key, []); - bySection.get(key).push(r); - } - let movedMethods = 0; - let movedLines = 0; - let movedSaved = 0; - const reasonCounts = new Map(); - for (const [section, rs] of bySection) { - const movers = rs.filter((r) => r.reasons.length === 0); - const lines = movers.reduce((n, r) => n + r.bodyLines, 0); - const saved = movers.reduce((n, r) => n + r.bodyLines - r.stubLines, 0); - movedMethods += movers.length; - movedLines += lines; - movedSaved += saved; - out.push(`§ ${section} -> ${rs[0].module ?? '(stays)'}: ${movers.length}/${rs.length} methods move ` + - `(${lines} body lines out, ${lines - saved} stub lines in)`); - for (const r of rs) { - if (r.reasons.length === 0) out.push(` move ${r.name} (body ${r.bodyLines} -> stub ${r.stubLines})`); - else out.push(` stay ${r.name} (${r.lineCount}) — ${r.reasons.join('; ')}`); - for (const reason of r.reasons) { - const code = reason.split(':')[0]; - reasonCounts.set(code, (reasonCounts.get(code) ?? 0) + 1); - } - } - } - out.push(''); - out.push(`total: ${movedMethods}/${planned.records.length} methods move into ${planned.modules.size} module(s): ` + - `${movedLines} body lines out, ${movedLines - movedSaved} stub lines in, plus ${2 * planned.modules.size} import lines`); - out.push(`stay reasons (a method may carry several): ${[...reasonCounts].sort((a, b) => b[1] - a[1]).map(([c, n]) => `${c} ${n}`).join(', ')}`); - const top = planned.top; - out.push(''); - out.push(`§ top-level declarations ahead of the class -> ${LEAF_MODULE}: ${top.movers.length}/${top.records.length} move`); - const topReasons = new Map(); - for (const r of top.records) { - if (r.reasons.length === 0) out.push(` move ${r.label} (${r.lines})${r.exported ? ' [exported, re-exported]' : ''}`); - else out.push(` stay ${r.label} (${r.lines}) — ${r.reasons.join('; ')}`); - for (const reason of r.reasons) { - const code = reason.split(':')[0]; - topReasons.set(code, (topReasons.get(code) ?? 0) + 1); - } - } - const topLines = top.movers.reduce((n, r) => n + r.lines, 0); - out.push(`top-level total: ${top.movers.length}/${top.records.length} declarations move (${topLines} lines with their docblocks); ` + - `stay reasons: ${[...topReasons].sort((a, b) => b[1] - a[1]).map(([c, n]) => `${c} ${n}`).join(', ') || 'none'}`); - out.push(`lever (a): ${planned.hostExportsForSections.size} staying name(s) exported for the section modules` + - `${planned.hostExportsForSections.size ? ` (${[...planned.hostExportsForSections.keys()].sort().join(', ')})` : ''}; ` + - `${planned.leafExports.length} leaf name(s) exported beside the base exports`); - return out.join('\n'); -} - -function treeHash(files) { - const h = createHash('sha256'); - for (const [name, text] of [...files].sort(([a], [b]) => (a < b ? -1 : 1))) h.update(`${name}\0${sha256(text)}\n`); - return h.digest('hex'); -} - -function printVerify(v) { - for (const f of v.failures) console.error(` ✗ ${f}`); - const identical = v.moved.filter((m) => m.body && m.params && m.returnType && m.async).length; - const identicalTop = v.movedTop.filter((m) => m.same).length; - console.log(`byte-identity: ${identical}/${v.moved.length} moved bodies identical to base (body, parameters, return type, async); ` + - `${identicalTop}/${v.movedTop.length} moved top-level declarations identical to base (leading comments included); ` + - `registry.ts ${v.linesBefore} -> ${v.linesAfter} lines; undoing the split ${v.failures.some((f) => f.startsWith('undoing')) ? 'does NOT' : 'does'} rebuild the base byte-for-byte ` + - `once the ${v.pruned.length} import binding(s) only moved text reads are pruned from the base (${v.pruned.join(', ') || 'none'})`); -} - -function gitShow(ref, path) { - return execFileSync('git', ['show', `${ref}:${path}`], { cwd: ROOT, encoding: 'utf8', maxBuffer: 1 << 28 }); -} - -/** A file under `registry.ts`'s directory by its relative path, or null when there is none. */ -function readSibling(rel) { - const p = join(SRC_DIR, rel); - return existsSync(p) ? readFileSync(p, 'utf8') : null; -} - -function generatedModulesOnDisk() { - const out = new Map(); - for (const file of [LEAF_MODULE, HOSTS_MODULE, ...SECTION_ANCHORS.map((row) => row[2])]) { - if (!file) continue; - const p = join(SRC_DIR, file); - if (existsSync(p)) out.set(file, readFileSync(p, 'utf8')); - } - return out; -} - -// ── Proof 2: the module's exports are unchanged ────────────────────────────── - -/** - * The names `registry.ts` exports, value and type alike, each with whether it - * carries a value, as the TypeScript checker resolves them: a name the leaf - * now declares and `registry.ts` re-exports by name counts as the same export. - * The files are compiled in one in-memory project (no lib, no `node_modules`), - * so a package import resolves to nothing and only the module graph under - * `SRC_REL` is read. - * @param {Map} files basename -> text, `registry.ts` among them - * @returns {Map} - */ -function exportedNames(tm, files) { - const project = new tm.Project({ - useInMemoryFileSystem: true, - compilerOptions: { - target: tm.ts.ScriptTarget.ES2020, module: tm.ts.ModuleKind.ES2020, - moduleResolution: tm.ts.ModuleResolutionKind.Node10, strict: true, noLib: true, allowJs: false, - }, - }); - const fs = project.getFileSystem(); - for (const [file, text] of files) fs.writeFileSync(join(SRC_DIR, file), text); - for (const [file] of files) project.addSourceFileAtPath(join(SRC_DIR, file)); - const sf = project.getSourceFileOrThrow(HOST_PATH); - const checker = project.getTypeChecker().compilerObject; - const moduleSymbol = checker.getSymbolAtLocation(sf.compilerNode); - const out = new Map(); - if (!moduleSymbol) return out; - for (const sym of checker.getExportsOfModule(moduleSymbol)) { - const target = sym.flags & tm.ts.SymbolFlags.Alias ? checker.getAliasedSymbol(sym) : sym; - // A package import resolves to nothing here (no `node_modules`): the - // checker's unknown symbol, kind `unresolved` on both sides alike. - // A name exported `type`-only carries no value whatever it aliases. - const typeOnly = (sym.declarations ?? []).some((d) => - tm.ts.isExportSpecifier(d) && (d.isTypeOnly || d.parent.parent.isTypeOnly)); - const kind = typeOnly ? 'type' - : target.flags & tm.ts.SymbolFlags.Value ? 'value' : target.flags & tm.ts.SymbolFlags.Type ? 'type' : 'unresolved'; - out.set(sym.getName(), kind); - } - return out; -} - -/** The names the head's generated lever-(a) statement exports for the section modules (`Exported for the section modules`). */ -function leverExportsOf(tm, headText) { - const names = new Set(); - for (const st of parse(tm, HOST_PATH, headText).getStatements()) { - if (!tm.Node.isExportDeclaration(st) || st.getModuleSpecifier() || !st.getFullText().includes(`${MARK} Exported for the section modules`)) continue; - for (const spec of st.getNamedExports()) names.add(spec.getNameNode().getText()); - } - return names; -} - -/** - * Every module under `SRC_DIR` that re-exports `registry.ts` with `export *`: - * through one, a name `registry.ts` newly exports would reach the package. The - * package entries (`index.ts`, `core.ts`) name what they take from it today. - */ -function starReexportersOnDisk() { - const out = []; - const walk = (dir) => { - for (const entry of readdirSync(dir, { withFileTypes: true })) { - const p = join(dir, entry.name); - if (entry.isDirectory()) { if (entry.name !== 'node_modules' && entry.name !== 'dist') walk(p); continue; } - if (!/\.ts$/.test(entry.name) || /\.(test|spec)\.ts$/.test(entry.name)) continue; - for (const m of readFileSync(p, 'utf8').matchAll(/^\s*export\s+\*\s+(?:as\s+\w+\s+)?from\s+'(\.{1,2}\/[^']+)'/gm)) { - if (resolve(dirname(p), m[1].replace(/\.js$/, '.ts')) === HOST_PATH) out.push(relative(SRC_DIR, p)); - } - } - }; - walk(SRC_DIR); - return out; -} - -/** - * Exports-equality (Proof 2): the split `registry.ts` (with every generated - * module) exports every name the base exports, each a value or a type as it - * was; the only names it adds are the staying declarations lever (a) exports - * for the section modules, and no module re-exports `registry.ts` with - * `export *`, so none of those reaches the package. Returns the failures; - * none = proved. - */ -function verifyExports(tm, baseText, headText, moduleTexts, { starReexporters = [] } = {}) { - const before = exportedNames(tm, new Map([[HOST_BASENAME, baseText]])); - const after = exportedNames(tm, new Map([[HOST_BASENAME, headText], ...moduleTexts])); - // The generated additions: lever (a)'s names, and the statics route's - // forwarder type and object when a moved body reads a non-public static. - const lever = new Set([...leverExportsOf(tm, headText), ...(headText.includes(`// ${MARK} The statics of`) ? [STATICS_NAME, STATICS_HOST_NAME] : [])]); - const failures = []; - if (before.size === 0) failures.push(`the base ${HOST_BASENAME} exports nothing the checker can read: an equality over nothing is not a proof`); - for (const [name, kind] of before) { - if (!after.has(name)) failures.push(`exports: ${name} (${kind}) is exported at base and not by the split ${HOST_BASENAME}`); - else if (after.get(name) !== kind) failures.push(`exports: ${name} is a ${kind} at base and a ${after.get(name)} after the split`); - } - const added = [...after.keys()].filter((name) => !before.has(name)); - for (const name of added) { - if (!lever.has(name)) failures.push(`exports: ${name} (${after.get(name)}) is exported by the split ${HOST_BASENAME} and not at base, and is not a generated export (lever (a) or the statics route)`); - } - for (const file of starReexporters) { - failures.push(`exports: ${file} re-exports ${HOST_BASENAME} with \`export *\`, so its generated names would reach the package; name what it takes instead`); - } - return { failures, count: before.size, values: [...before.values()].filter((k) => k === 'value').length, added }; -} - -function printExports(e) { - for (const f of e.failures) console.error(` ✗ ${f}`); - console.log(`exports-equality: ${e.failures.length === 0 ? 'equal' : 'NOT equal'} — ${HOST_BASENAME} exports ${e.count} name(s) at base ` + - `(${e.values} value(s), ${e.count - e.values} type(s)); the split exports ${e.failures.length === 0 ? 'every one, each the same kind' : 'a different set'}, ` + - `plus ${e.added.length} generated name(s) (lever (a), the statics route) (${e.added.join(', ') || 'none'}), which no module re-exports with \`export *\``); -} - -// ── Entry ──────────────────────────────────────────────────────────────────── - -const USAGE = [ - 'usage: node scripts/codemod-split-registry.mjs [--write] plan (or apply) the split of an UNSPLIT registry.ts', - ' node scripts/codemod-split-registry.mjs --verify --base REF re-prove a split on disk against registry.ts at REF', - ' node scripts/codemod-split-registry.mjs --self-test', - ' node scripts/codemod-split-registry.mjs --help', -].join('\n'); - -async function main(argv) { - // Answered before any guard: `--help` reads no tree, so the "already split" - // refusal below must never be what a reader asking for usage gets. - if (argv.includes('--help') || argv.includes('-h')) { - console.log(USAGE); - return 0; - } - if (argv.includes('--self-test')) { - await selfTest(); - if (process.exitCode === 1) return 1; - if (!selfTestReachedVerdict) { - console.error('\n✗ codemod-split-registry self-test: selfTest() returned without reaching its verdict,\n' + - 'so no success line was printed. Exiting 0 here would report a self-test that never\n' + - 'finished as a self-test that passed.'); - return 1; - } - return 0; - } - const known = new Set(['--write', '--verify', '--base']); - for (let i = 0; i < argv.length; i += 1) { - if (argv[i] === '--base') { i += 1; continue; } - if (!known.has(argv[i])) refuse(`unknown argument ${JSON.stringify(argv[i])}\n${USAGE}`); - } - const tm = await loadTsMorph(); - if (argv.includes('--verify')) { - const i = argv.indexOf('--base'); - const ref = i >= 0 ? argv[i + 1] : null; - if (!ref) refuse('--verify needs --base '); - const baseText = gitShow(ref, `${SRC_REL}/${HOST_BASENAME}`); - const headText = readFileSync(HOST_PATH, 'utf8'); - const modules = generatedModulesOnDisk(); - const v = verify(tm, baseText, headText, modules, { readSibling }); - printVerify(v); - const e = verifyExports(tm, baseText, headText, modules, { starReexporters: starReexportersOnDisk() }); - printExports(e); - return v.failures.length === 0 && e.failures.length === 0 ? 0 : 1; - } - const input = readFileSync(HOST_PATH, 'utf8'); - if (input.includes(MARK)) refuse(`${HOST_BASENAME} already carries ${MARK} stubs: run the script on an unsplit file (re-run on the latest main, never on its own output)`); - const planned = plan(tm, input); - console.log(planReport(planned)); - const out = generate(tm, input, planned); - const files = new Map([[HOST_BASENAME, out.host], ...out.modules]); - console.log(`output tree sha256: ${treeHash(files)} (input sha256 ${sha256(input)})`); - const v = verify(tm, input, out.host, out.modules, { readSibling }); - printVerify(v); - const e = verifyExports(tm, input, out.host, out.modules, { starReexporters: starReexportersOnDisk() }); - printExports(e); - if (v.failures.length > 0 || e.failures.length > 0) { - console.error('✗ the generated split fails its proof: nothing written. Report the fork; never hand-edit the split.'); - return 1; - } - if (!argv.includes('--write')) { - console.log('plan only: nothing written (pass --write to apply).'); - return 0; - } - for (const [file] of out.modules) { - const p = join(SRC_DIR, file); - if (existsSync(p) && !readFileSync(p, 'utf8').includes(MARK)) refuse(`${file} exists and was not written by this script`); - } - writeFileSync(HOST_PATH, out.host); - for (const [file, text] of out.modules) writeFileSync(join(SRC_DIR, file), text); - console.log(`✓ wrote ${HOST_BASENAME} and ${out.modules.size} module(s) under ${SRC_REL}`); - return 0; -} - -function valueSpecifiers(src) { - const out = []; - for (const m of src.matchAll(/^[ \t]*(?:import|export)\s+(type\s+)?([^;'"]*?)\s*from\s*'(\.{1,2}\/[^']+)'/gm)) { - if (m[1]) continue; - const inner = /^\{([^}]*)\}$/.exec(m[2].trim()); - if (inner && inner[1].split(',').map((s) => s.trim()).filter(Boolean).every((s) => s.startsWith('type '))) continue; - out.push(m[3]); - } - for (const m of src.matchAll(/^[ \t]*import\s*'(\.{1,2}\/[^']+)'/gm)) out.push(m[1]); - return out; -} - -// ── Self-test ──────────────────────────────────────────────────────────────── -// -// The proofs above are this script's whole value, and a proof that cannot fail -// proves nothing, so the self-test feeds each one an input it must REFUSE. The -// roster pins battery NAMES with a floor each, the roster's own size is pinned, -// and the verdict sets a flag the dispatch reads (AGENTS.md, "Writing a -// `--self-test`"), so a battery that stops running, or a `return` above the -// verdict, cannot read as a pass. - -let selfTestReachedVerdict = false; - -const SELF_TEST_BATTERIES = Object.freeze({ - 'stay rules': 20, - 'facets, statics and docblocks': 17, - 'top-level moves': 11, - 'byte-identity proof': 10, - 'import prune': 4, - 'refusals': 6, - 'exports-equality (Proof 2)': 6, -}) -const SELF_TEST_BATTERY_FLOOR = 7; - -const FIXTURE_ANCHORS = Object.freeze([ - ['count', 'Lifecycle', 'registry-lifecycle.ts'], - ['keptSection', 'Kept', null], -]); -const FIXTURE_NEEDLES = Object.freeze({ 'PINNED_TEXT': 'a fixture reader', 'needleInMethod(': 'a fixture reader' }); -const FIXTURE_CLASS_NEEDLES = Object.freeze({ 'classReaderText(': 'a fixture reader that also reads the leaf' }); -const FIXTURE_PINS = Object.freeze({ pinnedMethod: 'a fixture reader' }); -const fixturePlan = (tm, text) => plan(tm, text, - { anchors: FIXTURE_ANCHORS, pins: FIXTURE_PINS, needles: FIXTURE_NEEDLES, classNeedles: FIXTURE_CLASS_NEEDLES }); - -function fixtureSource(extraMethods = '') { - const pad = Array.from({ length: 12 }, (_, i) => ` const p${i} = ${i};`).join('\n'); - return `import type { Knex } from 'knex'; -import { helperImported } from './other.js'; -import { onlyMovedReads } from './moved-only.js'; -import { seamHelper } from './registry-seam.js'; - -/** A value the leaf takes, read by a moved body. */ -const LOCAL_VALUE = 1; -type LocalType = { n: number }; -export interface ExportedShape { a: number } - -// A reader takes this one out of the file by path. -const PINNED = 'PINNED_TEXT'; -function dependsOnPinned(): string { - return PINNED; -} -function namesClass(d: SchemaRegistry): SchemaRegistry { - return d; -} -const VIA_SEAM = seamHelper(); -export const DERIVED = LOCAL_VALUE + 1; -function classReaderText(n: number): number { - return n; -} - -export class SchemaRegistry { - private count = 0; - private knex: any = null; - protected logger: { warn(m: string): void } = console; - private readonly fn = (x: string): string => x; - private static readonly SECRET = 1; - private static bump = 0; - - /** A private docblock: it moves with the body. */ - private async movable(object: string, trx?: Knex.Transaction): Promise { -${pad} - const a = helperImported(object) as ExportedShape | string; - this.logger.warn(String(a) + onlyMovedReads()); - return this.other(String(a)) + String(trx ? this.count : 0); - } - - /** A protected docblock: it stays on the facade. */ - protected other(x: string): string { - return x; - } - - protected writes(): void { -${pad} - this.count = 1; - } - - protected asValue(): string[] { -${pad} - return ['a'].map(this.other); - } - - protected readsLocal(): number { -${pad} - return LOCAL_VALUE + dependsOnPinned().length; - } - - protected namesLocalType(): LocalType | null { -${pad} - return null; - } - - public inferred() { -${pad} - return 1; - } - - protected scoped(object: string): string { -${pad} - return this.getBuilder(object); - } - - protected getBuilder(object: string): string { - return object; - } - - protected defaulted(x: string = 'a'): string { -${pad} - return x; - } - - protected callsField(): string { -${pad} - return this.fn('a'); - } - - protected readsStatic(): number { -${pad} - return SchemaRegistry.SECRET; - } - - protected bumpsStatic(): number { -${pad} - SchemaRegistry.bump += 1; - return SchemaRegistry.SECRET; - } - - protected namesClassType(): SchemaRegistry | null { -${pad} - return null; - } - - protected delete(): number { -${pad} - return 4; - } - - protected deletesField(): void { -${pad} - delete this.knex; - } - - protected destructured({ a }: { a: number }): number { -${pad} - return a; - } - - protected generic(x: T): T { -${pad} - return this.other(x) as T; - } - - static staticHelper(n: number): number { -${pad} - return n + SchemaRegistry.SECRET; - } - - static usesThis(): number { -${pad} - return this.SECRET; - } - - protected pinnedMethod(): number { -${pad} - return 3; - } - - protected carriesNeedle(): number { -${pad} - return needleInMethod(1); - } - - protected carriesClassNeedle(): number { -${pad} - return classReaderText(1); - } -${extraMethods} - protected keptSection(): number { -${pad} - return 2 + VIA_SEAM + namesClass(this).count; - } -} - -function needleInMethod(n: number): number { - return n; -} -`; -} - -async function selfTest() { - const tm = await loadTsMorph(); - const batterySeen = new Map(); - let battery = null; - let failed = 0; - let cases = 0; - const open = (name) => { battery = name; }; - const check = (label, ok, detail = '') => { - batterySeen.set(battery, (batterySeen.get(battery) ?? 0) + 1); - cases += 1; - if (ok) console.log(` ✓ [${battery}] ${label}`); - else { failed += 1; console.error(` ✗ [${battery}] ${label}${detail ? ` — ${detail}` : ''}`); } - }; - const refused = (fn) => { try { fn(); return false; } catch (e) { return e instanceof CodemodRefusal; } }; - const refusalOf = (fn) => { try { fn(); return ''; } catch (e) { if (e instanceof CodemodRefusal) return e.message; throw e; } }; - - open('stay rules'); - { - const planned = fixturePlan(tm, fixtureSource()); - const why = (name) => (planned.records.find((r) => r.name === name)?.reasons ?? ['']).join('; '); - const stays = (name, code) => check(`${name} stays (${code})`, why(name).split('; ').some((r) => r.startsWith(code)), why(name)); - const moves = (name, because) => check(`${name} moves (${because})`, why(name) === '', why(name)); - moves('movable', 'the canonical body, private members read through the host'); - moves('readsLocal', 'lever (a)/(b): it imports the leaf value and the staying function'); - moves('namesLocalType', 'a non-exported type it names moves to the leaf'); - moves('scoped', 'a method it calls goes as a closure'); - moves('other', 'a body no longer than its stub moves too: the facade gate counts bodies, not lines'); - moves('writes', 'a field it assigns goes through a host setter'); - stays('deletesField', 'this-write'); - moves('delete', 'a reserved word is a valid facet member name'); - stays('asValue', 'method-as-value'); - stays('inferred', 'inferred-return-type'); - moves('defaulted', 'the stub keeps the default and forwards the value'); - stays('destructured', 'parameter-shape'); - moves('generic', 'the stub calls the body instantiated with its type parameters'); - moves('staticHelper', 'a static whose body reads no `this`'); - stays('usesThis', 'static-this'); - moves('readsStatic', 'a private static it reads goes through SchemaRegistryStatics'); - stays('bumpsStatic', 'static-write'); - stays('namesClassType', 'static-route-conflict'); - check('a pinned method and a method carrying a needle stay (path-reader)', - why('pinnedMethod').startsWith('path-reader') && why('carriesNeedle').startsWith('path-reader'), - `${why('pinnedMethod')} | ${why('carriesNeedle')}`); - check('a method in a section that maps to no module stays (section-kept)', why('keptSection').startsWith('section-kept'), why('keptSection')); - } - - open('top-level moves'); - { - const input = fixtureSource(); - const planned = fixturePlan(tm, input); - const top = (label) => planned.top.records.find((r) => r.label === label); - const why = (label) => (top(label)?.reasons ?? ['']).join('; '); - check('a value, a local type and an exported interface move', ['LOCAL_VALUE', 'LocalType', 'ExportedShape', 'DERIVED'] - .every((n) => why(n) === ''), ['LOCAL_VALUE', 'LocalType', 'ExportedShape', 'DERIVED'].map(why).join(' | ')); - check('a declaration carrying a reader\'s needle stays (path-reader)', why('PINNED').startsWith('path-reader'), why('PINNED')); - check('a declaration carrying a class-scoped needle moves: its reader reads the leaf', why('classReaderText') === '', why('classReaderText')); - check('a declaration naming a stayer stays (needs-stayer)', why('dependsOnPinned').startsWith('needs-stayer'), why('dependsOnPinned')); - check('a declaration naming the class stays (class-reference)', why('namesClass').startsWith('class-reference'), why('namesClass')); - check('a declaration using a registry-* import stays (seam-import)', why('VIA_SEAM').startsWith('seam-import'), why('VIA_SEAM')); - check('a declaration after the class is not in the region', !top('needleInMethod')); - const out = generate(tm, input, planned); - const leaf = out.modules.get(LEAF_MODULE) ?? ''; - check('the leaf header is line comments, never a docblock the bundled .d.ts would publish', - leaf.includes(MARK) && !leaf.slice(0, leaf.indexOf('\n\n', leaf.indexOf(MARK))).includes('/*')); - check('the leaf imports neither registry.ts nor a registry-* module', leaf.length > 0 && !/from '\.\/registry[.-]/.test(leaf), leaf.slice(0, 200)); - check('registry.ts re-exports the exported movers by name, the interface as a type', - /export \{ DERIVED, type ExportedShape \} from '\.\/registry-top-level\.js';/.test(out.host)); - check('lever (a): the staying function a moved body reads is exported for its module, and the module imports it', - /export \{ dependsOnPinned \};/.test(out.host) && - /import \{ SchemaRegistryStatics as SchemaRegistry, dependsOnPinned \} from '\.\/registry\.js';/.test(out.modules.get('registry-lifecycle.ts') ?? '')); - } - - open('byte-identity proof'); - { - const input = fixtureSource(); - const planned = fixturePlan(tm, input); - const out = generate(tm, input, planned); - const again = generate(tm, input, fixturePlan(tm, input)); - const v = verify(tm, input, out.host, out.modules); - check('the generated split proves against its base', v.failures.length === 0 && v.moved.length === 12 && v.movedTop.length === 5, - [...v.failures, `moved: ${v.moved.map((m) => m.name).join()}`, `top: ${v.movedTop.map((m) => m.name).join()}`].join(' | ')); - check('two runs on one input write byte-identical trees', - treeHash(new Map([['h', out.host], ...out.modules])) === treeHash(new Map([['h', again.host], ...again.modules]))); - const mod = out.modules.get('registry-lifecycle.ts'); - const edited = new Map([...out.modules, ['registry-lifecycle.ts', mod.replace('const p3 = 3;', 'const p3 = 4;')]]); - check('one edited character in a moved body fails', verify(tm, input, out.host, edited).failures.length > 0); - check('an edited stub signature fails', - verify(tm, input, out.host.replace('trx?: Knex.Transaction): Promise', 'trx?: Knex.Transaction): Promise'), out.modules).failures.length > 0); - check('an extra top-level statement in a module fails', - verify(tm, input, out.host, new Map([...out.modules, ['registry-lifecycle.ts', `${mod}\nexport const extra = 1;\n`]])).failures.length > 0); - check('a proof over no moved method fails', verify(tm, input, input, new Map()).failures.length > 0); - const leaf = out.modules.get(LEAF_MODULE); - const leafWith = (text) => new Map([...out.modules, [LEAF_MODULE, text]]); - check('one edited character in a moved declaration fails', - verify(tm, input, out.host, leafWith(leaf.replace('const LOCAL_VALUE = 1;', 'const LOCAL_VALUE = 2;'))).failures.length > 0); - check('an edited comment above a moved declaration fails', - verify(tm, input, out.host, leafWith(leaf.replace('/** A value the leaf takes', '/** A value the leaf holds'))).failures.length > 0); - check('a leaf that imports registry.ts back fails', - verify(tm, input, out.host, leafWith(leaf.replace('\nconst LOCAL_VALUE', "\nimport { SchemaRegistry } from './registry.js';\nconst LOCAL_VALUE"))).failures.length > 0); - const swapped = leaf.replace('type LocalType = { n: number };\nexport interface ExportedShape { a: number }', - 'export interface ExportedShape { a: number }\ntype LocalType = { n: number };'); - check('two moved declarations out of base order fail', swapped !== leaf && verify(tm, input, out.host, leafWith(swapped)).failures.length > 0); - } - - open('facets, statics and docblocks'); - { - const input = fixtureSource(); - const out = generate(tm, input, fixturePlan(tm, input)); - const mod = out.modules.get('registry-lifecycle.ts') ?? ''; - check('the bodies move into one stateless facet class of static methods', /\nexport class LifecycleFacet \{\n/.test(mod) && - /\n static async movable\(this: Pick SchemaRegistry.SECRET },\n });\n }\n\n // ${MARK} Installs`) && !out.host.includes('bump:')); - check('a written field gets a host setter that assigns it on the registry', - out.host.includes(' get count() { return registry.count; },\n set count(v) { registry.count = v; },')); - check('a generic method calls its body instantiated with its own type parameters', - out.host.includes('return (LifecycleFacet.generic).call(lifecycleHost.of(this), x);') && mod.includes(' static generic(this: Pick= 3 && - out.host.includes("import { lifecycleHost } from './registry-facet-hosts.js';")); - check('the host module keeps one FacetHost per facet and imports types only', - hostsMod.includes("export const lifecycleHost = new FacetHost('LifecycleFacet');") && - !/^import (?!type )/m.test(hostsMod) && hostsMod.includes("import type { LifecycleHost } from './registry-lifecycle.js';")); - check('a method goes into the host as a closure called on the registry at call time', - /\n [a-z]\w*: \(\.\.\.a\) => registry\.[a-z]\w*\(\.\.\.a\),\n/.test(out.host), out.host.slice(out.host.indexOf('Installs'))); - check('a stub handing another facet\'s host fails', verify(tm, input, - out.host.replace('.call(lifecycleHost.of(this), x)', '.call(otherHost.of(this), x)'), out.modules).failures.length > 0); - check('an edited host module fails', verify(tm, input, out.host, - new Map([...out.modules, [HOSTS_MODULE, hostsMod.replace('this.hosts.set(registry, host);', '')]])).failures.length > 0); - check('a written field\'s setter left out of the host fails', verify(tm, input, - out.host.replace(' set count(v) { registry.count = v; },\n', ''), out.modules).failures.length > 0); - check('a static method keeps its static stub and calls its facet body directly', - out.host.includes(' static staticHelper(n: number): number {\n // [#33949 codemod] The body moved to `registry-lifecycle.ts`.\n return LifecycleFacet.staticHelper(n);\n }')); - const withMod = (text) => new Map([...out.modules, ['registry-lifecycle.ts', text]]); - check('an edited moved docblock fails', verify(tm, input, out.host, - withMod(mod.replace('it moves with the body', 'it moved with the body'))).failures.length > 0); - check('an edited placeholder fails', verify(tm, input, out.host, - withMod(mod.replace(placeholderDoc('other'), placeholderDoc('other').replace('contract', 'promise')))).failures.length > 0); - check('a static forwarded as the wrong kind fails', verify(tm, input, - out.host.replace('SECRET: { get: () => SchemaRegistry.SECRET },', 'SECRET: { value: SchemaRegistry.SECRET },'), out.modules).failures.length > 0); - } - - open('import prune'); - { - const input = fixtureSource(); - const out = generate(tm, input, fixturePlan(tm, input)); - check('an import only a moved body read is pruned from registry.ts', out.pruned.includes('onlyMovedReads') && - !out.host.includes('onlyMovedReads'), out.pruned.join()); - check('an import the staying text still reads is kept', out.host.includes("import { seamHelper } from './registry-seam.js';")); - const v = verify(tm, input, out.host, out.modules); - check('the proof recomputes the prune from the head and names it', v.failures.length === 0 && v.pruned.includes('onlyMovedReads'), v.failures.join(' | ')); - const lost = out.host.replace("import { seamHelper } from './registry-seam.js';\n", ''); - check('an import the head lost that no moved text reads fails', verify(tm, input, lost, out.modules).failures.length > 0); - } - - open('refusals'); - { - check('an anchor the class does not declare refuses', - refused(() => plan(tm, fixtureSource(), { anchors: [['gone', 'x', 'registry-x.ts']], pins: {}, needles: {}, classNeedles: {} }))); - check('anchors out of class order refuse', - refused(() => plan(tm, fixtureSource(), { anchors: [['keptSection', 'x', null], ['count', 'y', 'registry-y.ts']], pins: {}, needles: {}, classNeedles: {} }))); - check('a pin naming what the file lacks refuses', refused(() => plan(tm, fixtureSource(), { anchors: FIXTURE_ANCHORS, pins: { gone: 'x' }, needles: {}, classNeedles: {} }))); - check('a class-scoped needle the file no longer carries refuses', - refused(() => plan(tm, fixtureSource(), { anchors: FIXTURE_ANCHORS, pins: {}, needles: {}, classNeedles: { 'gone text': 'x' } }))); - check('a needle the file no longer carries refuses', refused(() => plan(tm, fixtureSource(), { anchors: FIXTURE_ANCHORS, pins: {}, needles: { 'gone text': 'x' }, classNeedles: {} }))); - // `--help` reads no tree: it answers before the "already split" guard, so - // it exits 0 whether registry.ts on disk is split (as on a branch) or not. - check('--help is answered, never refused, whatever registry.ts holds', (await main(['--help']).catch(() => 2)) === 0); - } - - open('exports-equality (Proof 2)'); - { - const input = fixtureSource(); - const out = generate(tm, input, fixturePlan(tm, input)); - const e = verifyExports(tm, input, out.host, out.modules); - check('the generated split exports every base name, each the same kind, plus only its generated names (lever (a), the statics route)', - e.failures.length === 0 && e.count > 0 && e.added.includes('dependsOnPinned') && e.added.includes('SchemaRegistryStatics'), - [...e.failures, `added: ${e.added.join()}`].join(' | ')); - check('a base export the split no longer exports fails', - verifyExports(tm, input, out.host.replace(/export \{ DERIVED, type ExportedShape \} from '\.\/registry-top-level\.js';/, "export { type ExportedShape } from './registry-top-level.js';"), out.modules) - .failures.some((f) => /DERIVED \(value\) is exported at base and not by the split/.test(f))); - check('an export that changed kind (a value re-exported as a type only) fails', - verifyExports(tm, input, out.host.replace('export { DERIVED, type ExportedShape }', 'export { type DERIVED, type ExportedShape }'), out.modules) - .failures.some((f) => /DERIVED is a value at base and a type after the split/.test(f))); - check('a new export outside the lever-(a) statement fails', - verifyExports(tm, input, `${out.host}\nexport const SNUCK_IN = 1;\n`, out.modules) - .failures.some((f) => /SNUCK_IN \(value\) is exported by the split registry\.ts and not at base, and is not a generated export/.test(f))); - check('a module re-exporting registry.ts with export * fails', - verifyExports(tm, input, out.host, out.modules, { starReexporters: ['index.ts'] }) - .failures.some((f) => /index\.ts re-exports registry\.ts with `export \*`/.test(f))); - check('a base that exports nothing the checker reads fails: an equality over nothing is not a proof', - verifyExports(tm, 'export {};\n', 'export {};\n', new Map()).failures.some((f) => /an equality over nothing/.test(f))); - } - - const declared = Object.keys(SELF_TEST_BATTERIES); - if (declared.length < SELF_TEST_BATTERY_FLOOR) { - failed += 1; - console.error(`✗ self-test floor: ${declared.length} batteries declared, below the pinned ${SELF_TEST_BATTERY_FLOOR}`); - } - for (const [name, count] of batterySeen) { - if (!declared.includes(name)) { failed += 1; console.error(`✗ self-test floor: battery "${name}" ran ${count} case(s) but is not declared`); } - } - for (const name of declared) { - const count = batterySeen.get(name) ?? 0; - if (count < SELF_TEST_BATTERIES[name]) { - failed += 1; - console.error(`✗ self-test floor: battery "${name}" ran ${count} case(s), below its pinned ${SELF_TEST_BATTERIES[name]}`); - } - } - if (failed > 0) { - console.error(`\n✗ codemod-split-registry self-test: ${failed} failure(s) (cases and floor).`); - process.exitCode = 1; - selfTestReachedVerdict = true; - return; - } - console.log(`\n✓ codemod-split-registry self-test: ${cases} cases in ${declared.length} batteries pass.`); - selfTestReachedVerdict = true; -} - -if (isEntrypoint(import.meta.url)) { - main(process.argv.slice(2)).then( - (code) => process.exit(code), - (error) => { - if (error instanceof CodemodRefusal) { - console.error(`✗ codemod-split-registry: ${error.message}`); - process.exit(2); - } - console.error(error); - process.exit(2); - }, - ); -} From e06e1a9d536524d73485a1c916f14f8db52c7c4a Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 08:34:47 +0000 Subject: [PATCH 12/12] docs(objectql): the registry split's headers, changeset and exception reasons cite the split's PR instead of a script path Comments and text only; no code moves. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ --- .changeset/33949-registry-split.md | 2 +- packages/objectql/src/registry-apps.ts | 2 +- packages/objectql/src/registry-bare-names.ts | 3 +- packages/objectql/src/registry-facet-hosts.ts | 7 +++-- packages/objectql/src/registry-items.ts | 2 +- .../src/registry-namespaces-and-claims.ts | 2 +- .../src/registry-object-registration.ts | 2 +- .../src/registry-package-addressing.ts | 2 +- packages/objectql/src/registry-packages.ts | 3 +- packages/objectql/src/registry-picklists.ts | 2 +- .../objectql/src/registry-platform-global.ts | 3 +- packages/objectql/src/registry-restore.ts | 3 +- packages/objectql/src/registry-storage.ts | 2 +- packages/objectql/src/registry-top-level.ts | 3 +- scripts/facade-delegation-registry.json | 30 +++++++++---------- 15 files changed, 37 insertions(+), 31 deletions(-) diff --git a/.changeset/33949-registry-split.md b/.changeset/33949-registry-split.md index 19285c2e17c..857844fcf85 100644 --- a/.changeset/33949-registry-split.md +++ b/.changeset/33949-registry-split.md @@ -7,4 +7,4 @@ refactor(objectql): `registry.ts` is split into section modules by codemod; a mo Clause-②: no Metadata-protocol: none -`packages/objectql/src/registry.ts` (12,577 lines) is split by `scripts/codemod-split-registry.mjs`, the first pass of the `engine.ts` codemod, as ruled on #33949 (item 1, split 1 of 8). `SchemaRegistry` keeps every member's signature and docblock, and each moved method forwards in one call to a facet module (`registry-SECTION.ts`). The top-level declarations ahead of the class move whole into `registry-top-level.ts`, and `registry.ts` re-exports every one it exported by name. The script proves that every moved body and declaration is byte-identical to the base, and that `registry.ts` still exports the same names, each a value or a type as before. No behaviour changes, and nothing an importer of `@objectstack/objectql` reads changes. +`packages/objectql/src/registry.ts` (12,577 lines) is split by codemod (the first pass of the `engine.ts` codemod; the script is kept in PR #34285's branch history and is not committed), as ruled on #33949 (item 1, split 1 of 8). `SchemaRegistry` keeps every member's signature and docblock, and each moved method forwards in one call to a facet module (`registry-SECTION.ts`). The top-level declarations ahead of the class move whole into `registry-top-level.ts`, and `registry.ts` re-exports every one it exported by name. The codemod's proof, recorded in PR #34285, shows that every moved body and declaration is byte-identical to the base, and that `registry.ts` still exports the same names, each a value or a type as before. No behaviour changes, and nothing an importer of `@objectstack/objectql` reads changes. diff --git a/packages/objectql/src/registry-apps.ts b/packages/objectql/src/registry-apps.ts index 16b443a6f22..d1be00c257b 100644 --- a/packages/objectql/src/registry-apps.ts +++ b/packages/objectql/src/registry-apps.ts @@ -6,7 +6,7 @@ * `registerAppNavContribution`, `setPackageNavContributions`, * `getAppNavContributions`, `applyNavContributions`, `findNavGroup`, * `recordNavGroupMiss`, `getAppNavDiagnostics` moved out of `registry.ts` whole - * by `scripts/codemod-split-registry.mjs`. + * by the #33949 split codemod (PR #34285, kept in its branch history, not committed). * * A MOVE, not a redesign. {@link AppsFacet} is a stateless facet of `SchemaRegistry` * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` diff --git a/packages/objectql/src/registry-bare-names.ts b/packages/objectql/src/registry-bare-names.ts index 0c1b4a1cecd..7ef7826047f 100644 --- a/packages/objectql/src/registry-bare-names.ts +++ b/packages/objectql/src/registry-bare-names.ts @@ -7,7 +7,8 @@ * `bareNameHoldersOtherThan`, `bareNameBundleAdditionMeets`, * `refuseBareNameConflicts`, `recordBareNameClaims`, `forgetBareNameClaims`, * `claimBareNameFunctions`, `releaseBareNameFunctions`, `sealBareNameHolders` - * moved out of `registry.ts` whole by `scripts/codemod-split-registry.mjs`. + * moved out of `registry.ts` whole by + * the #33949 split codemod (PR #34285, kept in its branch history, not committed). * * A MOVE, not a redesign. {@link BareNamesFacet} is a stateless facet of `SchemaRegistry` * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` diff --git a/packages/objectql/src/registry-facet-hosts.ts b/packages/objectql/src/registry-facet-hosts.ts index 9b780d10604..8cca5ac8804 100644 --- a/packages/objectql/src/registry-facet-hosts.ts +++ b/packages/objectql/src/registry-facet-hosts.ts @@ -11,9 +11,10 @@ * when a body assigns it), so a body reads and writes the registry exactly as * the per-call literal let it. * - * Written by `scripts/codemod-split-registry.mjs`; it imports types only, so it forms no - * evaluation cycle and is evaluated before the class whichever module loads - * first. `index.ts` re-exports nothing from this module. + * Written by the #33949 split codemod (PR #34285, kept in its branch history, + * not committed); it imports types only, so it forms no evaluation cycle + * and is evaluated before the class whichever module loads first. + * `index.ts` re-exports nothing from this module. */ import type { SchemaRegistry } from './registry.js'; diff --git a/packages/objectql/src/registry-items.ts b/packages/objectql/src/registry-items.ts index 1847151020c..cf0545e7c87 100644 --- a/packages/objectql/src/registry-items.ts +++ b/packages/objectql/src/registry-items.ts @@ -7,7 +7,7 @@ * `unregisterItemsByPackage`, `replacingPackageBody`, * `withdrawPackageBodyItemsNotShipped`, `withdrawPackageClaimsNotDeclared`, * `getItem` moved out of `registry.ts` whole by - * `scripts/codemod-split-registry.mjs`. + * the #33949 split codemod (PR #34285, kept in its branch history, not committed). * * A MOVE, not a redesign. {@link ItemsFacet} is a stateless facet of `SchemaRegistry` * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` diff --git a/packages/objectql/src/registry-namespaces-and-claims.ts b/packages/objectql/src/registry-namespaces-and-claims.ts index a38a928cf1a..00de042b803 100644 --- a/packages/objectql/src/registry-namespaces-and-claims.ts +++ b/packages/objectql/src/registry-namespaces-and-claims.ts @@ -12,7 +12,7 @@ * `forgetSecurityCatalogClaims`, `securityCatalogNamesHeldBy`, * `environmentHeldSecurityCatalogConflicts`, `packageHeldSecurityCatalogNames`, * `securityCatalogPackageHolders` moved out of `registry.ts` whole by - * `scripts/codemod-split-registry.mjs`. + * the #33949 split codemod (PR #34285, kept in its branch history, not committed). * * A MOVE, not a redesign. {@link NamespacesAndClaimsFacet} is a stateless facet of `SchemaRegistry` * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` diff --git a/packages/objectql/src/registry-object-registration.ts b/packages/objectql/src/registry-object-registration.ts index 45501631cd4..3da9d6a4ad1 100644 --- a/packages/objectql/src/registry-object-registration.ts +++ b/packages/objectql/src/registry-object-registration.ts @@ -18,7 +18,7 @@ * `materializeServedObjectOnto`, `declinedBaseLayerStamps`, * `provisionSearchCompanionOnto`, `stripProvisionedSearchCompanionFrom`, * `stripMaterializedStampsFrom` moved out of `registry.ts` whole by - * `scripts/codemod-split-registry.mjs`. + * the #33949 split codemod (PR #34285, kept in its branch history, not committed). * * A MOVE, not a redesign. {@link ObjectRegistrationFacet} is a stateless facet of `SchemaRegistry` * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` diff --git a/packages/objectql/src/registry-package-addressing.ts b/packages/objectql/src/registry-package-addressing.ts index eb272dc3615..2e58e110d58 100644 --- a/packages/objectql/src/registry-package-addressing.ts +++ b/packages/objectql/src/registry-package-addressing.ts @@ -12,7 +12,7 @@ * `isPackageDisabled`, `isObjectPackageDisabled`, `isFlowOfDisabledPackage`, * `isActionWithheldByDisabledPackage`, `hasDisabledPackage`, * `getRegisteredTypes` moved out of `registry.ts` whole by - * `scripts/codemod-split-registry.mjs`. + * the #33949 split codemod (PR #34285, kept in its branch history, not committed). * * A MOVE, not a redesign. {@link PackageAddressingFacet} is a stateless facet of `SchemaRegistry` * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` diff --git a/packages/objectql/src/registry-packages.ts b/packages/objectql/src/registry-packages.ts index 2a533b9339b..b958dd52a67 100644 --- a/packages/objectql/src/registry-packages.ts +++ b/packages/objectql/src/registry-packages.ts @@ -8,7 +8,8 @@ * `refuseNamespaceHolderConflicts`, `uninstallPackage`, `getPackage`, * `recordResidualOwnerDependencies`, `residualOwnerDependenciesOf`, * `getAllPackages`, `enablePackage`, `disablePackage`, `updatePackageManifest` - * moved out of `registry.ts` whole by `scripts/codemod-split-registry.mjs`. + * moved out of `registry.ts` whole by + * the #33949 split codemod (PR #34285, kept in its branch history, not committed). * * A MOVE, not a redesign. {@link PackagesFacet} is a stateless facet of `SchemaRegistry` * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` diff --git a/packages/objectql/src/registry-picklists.ts b/packages/objectql/src/registry-picklists.ts index 26cda79a1bc..1c9043b187b 100644 --- a/packages/objectql/src/registry-picklists.ts +++ b/packages/objectql/src/registry-picklists.ts @@ -14,7 +14,7 @@ * `assertPicklistValuesDistinct`, `resolvePicklistFields`, * `resolveFieldPicklistOptions`, `unregisterPicklistExtensionsByPackage`, * `invalidate`, `invalidateAll`, `reset` moved out of `registry.ts` whole by - * `scripts/codemod-split-registry.mjs`. + * the #33949 split codemod (PR #34285, kept in its branch history, not committed). * * A MOVE, not a redesign. {@link PicklistsFacet} is a stateless facet of `SchemaRegistry` * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` diff --git a/packages/objectql/src/registry-platform-global.ts b/packages/objectql/src/registry-platform-global.ts index f3f02a15ece..308734de8cd 100644 --- a/packages/objectql/src/registry-platform-global.ts +++ b/packages/objectql/src/registry-platform-global.ts @@ -13,7 +13,8 @@ * `registerObjectExtension`, `extensionTargetKey`, `withdrawObjectExtension`, * `removeObjectOverlay`, `assertSingleOwnerPerObject`, * `unregisterObjectsByPackage`, `unregisterObject` moved out of `registry.ts` - * whole by `scripts/codemod-split-registry.mjs`. + * whole by + * the #33949 split codemod (PR #34285, kept in its branch history, not committed). * * A MOVE, not a redesign. {@link PlatformGlobalFacet} is a stateless facet of `SchemaRegistry` * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` diff --git a/packages/objectql/src/registry-restore.ts b/packages/objectql/src/registry-restore.ts index 1a93b882a9b..498233a2e48 100644 --- a/packages/objectql/src/registry-restore.ts +++ b/packages/objectql/src/registry-restore.ts @@ -7,7 +7,8 @@ * `noteObjectEntry`, `noteNamespace`, `noteItemType`, `noteItem`, * `noteItemOrder`, `notePicklistExtension`, `noteSecurityCatalogClaim`, * `noteBareNameClaim`, `noteDefaultRecordPageClaims` moved out of `registry.ts` - * whole by `scripts/codemod-split-registry.mjs`. + * whole by + * the #33949 split codemod (PR #34285, kept in its branch history, not committed). * * A MOVE, not a redesign. {@link RestoreFacet} is a stateless facet of `SchemaRegistry` * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` diff --git a/packages/objectql/src/registry-storage.ts b/packages/objectql/src/registry-storage.ts index 08b97811d47..a2f6f4b28d4 100644 --- a/packages/objectql/src/registry-storage.ts +++ b/packages/objectql/src/registry-storage.ts @@ -7,7 +7,7 @@ * `closeObjectEntry`, `setInitialDisabledPackageIds`, `onPackageEnabled`, * `announcePackageEnabled`, `onPackageUninstalled`, * `announcePackageUninstalled` moved out of `registry.ts` whole by - * `scripts/codemod-split-registry.mjs`. + * the #33949 split codemod (PR #34285, kept in its branch history, not committed). * * A MOVE, not a redesign. {@link StorageFacet} is a stateless facet of `SchemaRegistry` * (ruling 6624345766): each static method of it is the body of the `SchemaRegistry` diff --git a/packages/objectql/src/registry-top-level.ts b/packages/objectql/src/registry-top-level.ts index ec9b2130f67..5b7a47ccfe4 100644 --- a/packages/objectql/src/registry-top-level.ts +++ b/packages/objectql/src/registry-top-level.ts @@ -1,7 +1,8 @@ // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. // [#33949 codemod] The top-level declarations ahead of `SchemaRegistry` in -// `registry.ts` that moved out whole by `scripts/codemod-split-registry.mjs` +// `registry.ts` that moved out whole by +// the #33949 split codemod (PR #34285, kept in its branch history, not committed) // (lever (b) of ruling 6591753301). // // A MOVE, not a redesign: each declaration below is its `registry.ts` text diff --git a/scripts/facade-delegation-registry.json b/scripts/facade-delegation-registry.json index f0108946729..1d263a10567 100644 --- a/scripts/facade-delegation-registry.json +++ b/scripts/facade-delegation-registry.json @@ -61,105 +61,105 @@ "class": "SchemaRegistry", "member": "constructor", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): the first pass never moves a constructor (the engine moved its own in its fourth pass, #30020 R8); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): the first pass never moves a constructor (the engine moved its own in its fourth pass, #30020 R8); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "registeredBundleHolding", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "declaredBundleHolding", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "recordCurrencyFollowRefusalOf", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): its stub keeps the base signature byte-for-byte, and two parameter defaults are closures over `this` (`() => this.getAllObjects()`), which the gate reads as a computed default; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): its stub keeps the base signature byte-for-byte, and two parameter defaults are closures over `this` (`() => this.getAllObjects()`), which the gate reads as a computed default; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "registerObject", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): its stub keeps the base signature byte-for-byte, and the `priority` default is a conditional over `ownership`, which the gate reads as a computed default; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): its stub keeps the base signature byte-for-byte, and the `priority` default is a conditional over `ownership`, which the gate reads as a computed default; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "[FIND_BOUND_ACTION_COLLISIONS]", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): not-identifier-name: a symbol-keyed method (the engine moved its own under R9, #30020); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): not-identifier-name: a symbol-keyed method (the engine moved its own under R9, #30020); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "assertPackageUninstallable", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "registerItem", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): inferred-return-type: a public method whose return type is inferred, so moving it would change the published .d.ts; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): inferred-return-type: a public method whose return type is inferred, so moving it would change the published .d.ts; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "unregisterItem", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): inferred-return-type: a public method whose return type is inferred, so moving it would change the published .d.ts; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): inferred-return-type: a public method whose return type is inferred, so moving it would change the published .d.ts; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "settledPairRowBinding", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "isBundleAddition", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "resolveItem", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "refuseAmbiguousObjectLessAction", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): parameter-shadows: a parameter (`readStoredRows`) shadows a stub local; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): parameter-shadows: a parameter (`readStoredRows`) shadows a stub local; a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "picklistReferenceVerdict", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" }, { "file": "packages/objectql/src/registry.ts", "class": "SchemaRegistry", "member": "judgePicklistArrival", "card": "#33949", - "reason": "first-pass stay (scripts/codemod-split-registry.mjs, the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" + "reason": "first-pass stay (the #33949 split codemod of PR #34285, kept in its branch history, not committed; the first pass of the engine codemod): bare-this: its body uses `this` other than as `this.member`, which needs the identity port (#30020 R1); a later pass on #33949 moves it, as the engine's fourth pass (#30055, #30020) moved the engine's" } ], "ceilings": [