|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0010] The owner-stamp judge for a collection item's `_packageId`: ONE |
| 5 | + * comparison and ONE set of words, called by every door that files a |
| 6 | + * collection item under a package. |
| 7 | + * |
| 8 | + * ## The rule |
| 9 | + * |
| 10 | + * `_packageId` is the ADR-0010 envelope stamp: the package that REGISTERS the |
| 11 | + * item, written by the registration (`applyProtection`, which writes the key |
| 12 | + * only while it is unset). Every item schema that spreads |
| 13 | + * `MetadataProtectionFields` declares the key, so a body could arrive wearing |
| 14 | + * another package's id, or an id no package holds, and keep it: every reader |
| 15 | + * that asks an item's owner (whose package root a connector's file ref |
| 16 | + * resolves against, the disabled-package filter, the artifact gates) then read |
| 17 | + * a stamp the body wrote about itself. A body never names its own owner, so |
| 18 | + * the stamp is refused, with a prescription. |
| 19 | + * |
| 20 | + * | body `_packageId` | the owner | verdict | |
| 21 | + * |:--|:--|:--| |
| 22 | + * | absent (`undefined`) | anything | accepted: an absent key declares nothing | |
| 23 | + * | `'P'` | `'P'` | accepted: what an item re-registered under its own package carries | |
| 24 | + * | `'Q'` | `'P'` | refused | |
| 25 | + * | any value, `null` and `''` included | none | refused | |
| 26 | + * | `null` or `''` | `'P'` | refused: `applyProtection` overwrites neither, so either would survive as an owner no row answers | |
| 27 | + * |
| 28 | + * The comparison is strict equality against the owner as the door passes it: |
| 29 | + * the registration door's own normalisation (`id || name`) is the door's, and |
| 30 | + * a stamp of another type than string is judged like any other foreign value |
| 31 | + * (the build door's item schemas refuse it first, with their own located |
| 32 | + * issue, since `_packageId` is declared `z.string()`). |
| 33 | + * |
| 34 | + * ## The doors, and why the judge lives in this package |
| 35 | + * |
| 36 | + * | door | caller | the owner it passes | |
| 37 | + * |:--|:--|:--| |
| 38 | + * | `build` | the stack schema's refinement (`ObjectStackDefinitionSchema`, `stack.zod.ts`): `defineStack`, `os validate`, `os build`, the artifact door's parse | the stack's `manifest.id` (none without a manifest); each `packages[]` body's own `id` | |
| 39 | + * | `register` | `ObjectQL.registerApp`'s collection loop, its seed datasets and its `viewItems:` channel, and the `manifest` service's residual registration (`@objectstack/objectql`) | the package id it registers under | |
| 40 | + * |
| 41 | + * `@objectstack/spec` is the one layer both callers already depend on, so the |
| 42 | + * judge lives here and nowhere else. ⛔ Do not write a second comparison at a |
| 43 | + * door: two doors spelling one rule differently is how one comes to accept |
| 44 | + * what the other refuses. What stays at the registration door is process |
| 45 | + * state, not a body rule: a stamp a registration of this process wrote onto |
| 46 | + * the caller's own object is exempt there, because no body wrote it. |
| 47 | + * |
| 48 | + * ## Which collections |
| 49 | + * |
| 50 | + * {@link OWNER_STAMP_JUDGED_COLLECTIONS}: the stack collections whose items the |
| 51 | + * registration door files through `registerItem` and judges, the seed `data` |
| 52 | + * datasets included. The build door judges exactly these, so it refuses what |
| 53 | + * the boot refuses and nothing the boot accepts. An objectql test holds the |
| 54 | + * list equal to what `registerApp` refuses, key by key. |
| 55 | + * |
| 56 | + * ## The words |
| 57 | + * |
| 58 | + * Pure: it builds no error and throws nothing. Each door wraps |
| 59 | + * {@link CollectionItemOwnerStampRefusal} in its own envelope: the build door |
| 60 | + * as a schema issue at the item's `_packageId`, the registration door as |
| 61 | + * ADR-0112's `VALIDATION_ERROR` / 400 with `issues[]` at `_packageId`. The |
| 62 | + * message prescribes first, because a 4xx message crosses the REST boundary |
| 63 | + * bounded at 500 characters with its tail truncated. The two doors' words |
| 64 | + * differ only in the clause that names where the owner comes from. Runtime |
| 65 | + * words carry no tracker number. |
| 66 | + */ |
| 67 | + |
| 68 | +/** The door judging the item: where its owner comes from. */ |
| 69 | +export type CollectionItemOwnerStampDoor = 'build' | 'register'; |
| 70 | + |
| 71 | +/** A disagreement, in the words every door shows it with. */ |
| 72 | +export interface CollectionItemOwnerStampRefusal { |
| 73 | + /** The body's `_packageId`, as it arrived (never `undefined`). */ |
| 74 | + readonly stamped: unknown; |
| 75 | + /** The owner judged against: `undefined` is no package. */ |
| 76 | + readonly owner: string | undefined; |
| 77 | + /** The refusal sentence: the prescription first, then both values, then the rule. */ |
| 78 | + readonly message: string; |
| 79 | + /** The located sentence a door's issue at `_packageId` carries. */ |
| 80 | + readonly issueMessage: string; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * The stack collections whose items the registration door files under their |
| 85 | + * package and judges by {@link judgeCollectionItemOwnerStamp}, in the stack |
| 86 | + * schema's spelling. |
| 87 | + * |
| 88 | + * Not judged, because the registration door files them through a path of its |
| 89 | + * own and does not judge their stamp: `objects` and `objectExtensions` (the |
| 90 | + * object-merge path), `apps` (registered by their own verb), |
| 91 | + * `picklistExtensions` (merged into the list they name, not filed as items), |
| 92 | + * and `datasources`, `datasourceMapping` and `translations` (configuration, |
| 93 | + * not registry items). |
| 94 | + */ |
| 95 | +export const OWNER_STAMP_JUDGED_COLLECTIONS = Object.freeze([ |
| 96 | + 'picklists', |
| 97 | + 'actions', 'views', 'pages', 'dashboards', 'reports', 'datasets', |
| 98 | + 'flows', 'webhooks', 'jobs', |
| 99 | + 'positions', 'permissions', 'capabilities', 'sharingRules', |
| 100 | + 'queues', 'assignmentRules', |
| 101 | + 'agents', 'tools', 'skills', |
| 102 | + 'apis', |
| 103 | + 'hooks', 'mappings', 'analyticsCubes', |
| 104 | + 'connectors', |
| 105 | + 'emailTemplates', 'docs', 'books', |
| 106 | + 'data', |
| 107 | +] as const); |
| 108 | + |
| 109 | +/** One member of {@link OWNER_STAMP_JUDGED_COLLECTIONS}. */ |
| 110 | +export type OwnerStampJudgedCollection = (typeof OWNER_STAMP_JUDGED_COLLECTIONS)[number]; |
| 111 | + |
| 112 | +/** |
| 113 | + * Judge one collection item's body `_packageId` against the owner of the door |
| 114 | + * that files it. |
| 115 | + * |
| 116 | + * @param item The item's body, as it arrived (anything: a non-object is not judged). |
| 117 | + * @param type The item's metadata type, as the words show it (`connector`, `data`, …). |
| 118 | + * @param name The item's name, as the words show it. |
| 119 | + * @param owner The package the door files the item under: `undefined` for none. |
| 120 | + * @param door Which door is asking (see the module header). |
| 121 | + * @returns The refusal when the stamp disagrees with the owner, else `undefined`. |
| 122 | + */ |
| 123 | +export function judgeCollectionItemOwnerStamp( |
| 124 | + item: unknown, |
| 125 | + type: string, |
| 126 | + name: string, |
| 127 | + owner: string | undefined, |
| 128 | + door: CollectionItemOwnerStampDoor, |
| 129 | +): CollectionItemOwnerStampRefusal | undefined { |
| 130 | + if (!item || typeof item !== 'object') return undefined; |
| 131 | + const stamped = (item as { _packageId?: unknown })._packageId; |
| 132 | + if (stamped === undefined || stamped === owner) return undefined; |
| 133 | + const where = door === 'register' |
| 134 | + ? `it is registered under ${owner === undefined ? 'no package' : `package '${owner}'`}` |
| 135 | + : `this stack ships it from ${owner === undefined ? 'no package (the stack declares no `manifest`)' : `package '${owner}'`}`; |
| 136 | + const issueMessage = `carries _packageId ${JSON.stringify(stamped)}, but ${where}`; |
| 137 | + return { |
| 138 | + stamped, |
| 139 | + owner, |
| 140 | + message: `Drop \`_packageId\` from ${type} '${name}': it ${issueMessage}. The package an item belongs to is the one ` |
| 141 | + + 'that registers it, stamped by the registration; a body never names its own owner.', |
| 142 | + issueMessage, |
| 143 | + }; |
| 144 | +} |
0 commit comments