|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25467 cut 17] The object-owner verdicts of |
| 5 | + * `ObjectStackProtocolImplementation`: which contributor kind a stored object |
| 6 | + * row registers as against the registry's owner of that name |
| 7 | + * (`classifyObjectContribution`), and handing an adopted object's name from the |
| 8 | + * `'sys_metadata'` sentinel owner to its rebound row |
| 9 | + * (`releaseAdoptedObjectOwner`), moved out of `protocol.ts` whole. |
| 10 | + * |
| 11 | + * A MOVE, not a redesign. Each function below is the body of the |
| 12 | + * `ObjectStackProtocolImplementation` method of the same name as it stood, |
| 13 | + * re-indented one level and otherwise unedited: the same registry reads in the |
| 14 | + * same order, the same verdicts, and the same logged line. |
| 15 | + * |
| 16 | + * ## How the moved bodies reach the class |
| 17 | + * |
| 18 | + * The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts` |
| 19 | + * header carries the full reasoning), as `protocol-batch-data.ts` applies it. |
| 20 | + * The class keeps every method — the published `dist/*.d.ts` lists each one's |
| 21 | + * name, docblock and declared signature — and each body is one literal of |
| 22 | + * exactly the {@link ObjectOwnerHost} members that method reads, plus a |
| 23 | + * `.call`. The engine is handed over as a getter, so the body reads the |
| 24 | + * protocol's engine (and so its registry) at call time. Every other reader of |
| 25 | + * these two methods keeps calling the class method. |
| 26 | + * |
| 27 | + * Neither body reads a module-level declaration of `protocol.ts`, and neither |
| 28 | + * imports anything, so this module imports only the class type. Nothing |
| 29 | + * re-exports this module, so the published surface does not move. |
| 30 | + */ |
| 31 | + |
| 32 | +import type { ObjectStackProtocolImplementation } from './protocol.js'; |
| 33 | + |
| 34 | +/** |
| 35 | + * The `ObjectStackProtocolImplementation` members the functions below read — |
| 36 | + * every one of them, and no other; each function's `this` is the `Pick` of the |
| 37 | + * members IT reads. Each is typed AS the class member (an indexed access on |
| 38 | + * the class), so a change to a member's signature on the class is a change |
| 39 | + * here too, and naming a member the class does not have is a compile error. |
| 40 | + */ |
| 41 | +export interface ObjectOwnerHost { |
| 42 | + readonly engine: ObjectStackProtocolImplementation['engine']; |
| 43 | +} |
| 44 | + |
| 45 | +/** The body of `ObjectStackProtocolImplementation#classifyObjectContribution`; the method's docblock there is its contract. */ |
| 46 | +export function classifyObjectContribution( |
| 47 | + this: Pick<ObjectOwnerHost, 'engine'>, |
| 48 | + name: string, |
| 49 | + rowPackageId: string | null | undefined, |
| 50 | +): |
| 51 | + | { kind: 'own' | 'overlay'; packageId: string } |
| 52 | + | { kind: 'mismatch'; packageId: string; ownerPackageId: string } { |
| 53 | + // `||`, not `??`: an empty-string binding is "no package", the same |
| 54 | + // normalisation both hydration seams apply to `package_id`. |
| 55 | + const binding = rowPackageId || ''; |
| 56 | + const registry: any = (this.engine as any)?.registry; |
| 57 | + const owner = typeof registry?.getPackagedObjectOwner === 'function' |
| 58 | + ? registry.getPackagedObjectOwner(name) |
| 59 | + : undefined; |
| 60 | + const ownerPackageId: unknown = owner?.packageId; |
| 61 | + if (typeof ownerPackageId !== 'string' || ownerPackageId === '') { |
| 62 | + // [#23416] No packaged owner: the runtime-authored one, if any. |
| 63 | + const runtimeOwner = typeof registry?.getObjectOwner === 'function' |
| 64 | + ? registry.getObjectOwner(name) |
| 65 | + : undefined; |
| 66 | + const runtimeOwnerPackageId: unknown = runtimeOwner?.packageId; |
| 67 | + // [#23514] The `'sys_metadata'` sentinel owner is judged too: see the header. |
| 68 | + if ( |
| 69 | + binding !== '' |
| 70 | + && typeof runtimeOwnerPackageId === 'string' |
| 71 | + && runtimeOwnerPackageId !== '' |
| 72 | + && binding !== runtimeOwnerPackageId |
| 73 | + ) { |
| 74 | + return { kind: 'mismatch', packageId: binding, ownerPackageId: runtimeOwnerPackageId }; |
| 75 | + } |
| 76 | + return { kind: 'own', packageId: binding || 'sys_metadata' }; |
| 77 | + } |
| 78 | + if (binding !== '' && binding !== ownerPackageId) { |
| 79 | + return { kind: 'mismatch', packageId: binding, ownerPackageId }; |
| 80 | + } |
| 81 | + return { kind: 'overlay', packageId: binding || 'sys_metadata' }; |
| 82 | +} |
| 83 | + |
| 84 | +/** The body of `ObjectStackProtocolImplementation#releaseAdoptedObjectOwner`; the method's docblock there is its contract. */ |
| 85 | +export function releaseAdoptedObjectOwner( |
| 86 | + this: Pick<ObjectOwnerHost, 'engine'>, |
| 87 | + name: string, item: unknown, targetPackageId: string, |
| 88 | +): boolean { |
| 89 | + const registry: any = (this.engine as any)?.registry; |
| 90 | + if (typeof registry?.registerObject !== 'function') return false; |
| 91 | + if (!item || typeof item !== 'object' || typeof (item as { name?: unknown }).name !== 'string') return false; |
| 92 | + const owner: { packageId?: unknown } | undefined = typeof registry.getObjectOwner === 'function' |
| 93 | + ? registry.getObjectOwner(name) |
| 94 | + : undefined; |
| 95 | + if (owner === undefined) return true; |
| 96 | + if (owner.packageId !== 'sys_metadata' || typeof registry.unregisterObject !== 'function') return false; |
| 97 | + try { |
| 98 | + registry.unregisterObject(name); |
| 99 | + return true; |
| 100 | + } catch (err: any) { |
| 101 | + console.error( |
| 102 | + `[Protocol] reassignOrphanedMetadata: object '${name}' was adopted into package ` |
| 103 | + + `'${targetPackageId}' in sys_metadata, but the registry keeps its package-less definition ` |
| 104 | + + `(owner 'sys_metadata') until the next restart: ${err?.message ?? err}. Until then the data ` |
| 105 | + + `plane serves the pre-adoption definition, and a save of the object naming '${targetPackageId}' ` |
| 106 | + + 'answers success without going live. Fix: restart the server; the boot registers the object ' |
| 107 | + + `under '${targetPackageId}'.`, |
| 108 | + ); |
| 109 | + return false; |
| 110 | + } |
| 111 | +} |
0 commit comments