Skip to content

Commit 4d5121b

Browse files
committed
refactor(metadata-protocol): move the object-owner verdicts out of protocol.ts (#25467 cut 17)
Move the bodies of classifyObjectContribution and releaseAdoptedObjectOwner unedited into protocol-object-owner.ts, behind an ObjectOwnerHost that hands over the engine as a getter. The class keeps both methods, their docblocks and declared signatures; each body becomes a host literal plus a .call. Claude-Session: https://claude.ai/code/session_016SP1spj613tyGCgxoD1An6 Co-authored-by: Claude <noreply@anthropic.com>
1 parent df8aac4 commit 4d5121b

2 files changed

Lines changed: 125 additions & 51 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

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

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ import { applySeedBodies, collectBatchPendingDeclarations } from './protocol-pub
397397
import { artifactLockLayerAt, lookupArtifactItem, shippedArtifactsOf, shippingPackagesOf } from './protocol-artifact-lookup.js';
398398
// [#25467 cut 16] The stored-overlay row reads: a type's active rows, their parsed entries, the names with a row of their own, and one item's rows.
399399
import { namesWithOwnStoredRow, readActiveOverlayRows, readItemActiveRows, storedOverlayEntries } from './protocol-stored-overlay-reads.js';
400+
// [#25467 cut 17] The object-owner verdicts: which contributor kind a stored object row registers as, and handing an adopted object's name to its rebound row.
401+
import { classifyObjectContribution, releaseAdoptedObjectOwner } from './protocol-object-owner.js';
400402

401403
/**
402404
* Canonical Zod schema per metadata type lives in
@@ -17515,35 +17517,12 @@ export class ObjectStackProtocolImplementation implements
1751517517
):
1751617518
| { kind: 'own' | 'overlay'; packageId: string }
1751717519
| { kind: 'mismatch'; packageId: string; ownerPackageId: string } {
17518-
// `||`, not `??`: an empty-string binding is "no package", the same
17519-
// normalisation both hydration seams apply to `package_id`.
17520-
const binding = rowPackageId || '';
17521-
const registry: any = (this.engine as any)?.registry;
17522-
const owner = typeof registry?.getPackagedObjectOwner === 'function'
17523-
? registry.getPackagedObjectOwner(name)
17524-
: undefined;
17525-
const ownerPackageId: unknown = owner?.packageId;
17526-
if (typeof ownerPackageId !== 'string' || ownerPackageId === '') {
17527-
// [#23416] No packaged owner: the runtime-authored one, if any.
17528-
const runtimeOwner = typeof registry?.getObjectOwner === 'function'
17529-
? registry.getObjectOwner(name)
17530-
: undefined;
17531-
const runtimeOwnerPackageId: unknown = runtimeOwner?.packageId;
17532-
// [#23514] The `'sys_metadata'` sentinel owner is judged too: see the header.
17533-
if (
17534-
binding !== ''
17535-
&& typeof runtimeOwnerPackageId === 'string'
17536-
&& runtimeOwnerPackageId !== ''
17537-
&& binding !== runtimeOwnerPackageId
17538-
) {
17539-
return { kind: 'mismatch', packageId: binding, ownerPackageId: runtimeOwnerPackageId };
17540-
}
17541-
return { kind: 'own', packageId: binding || 'sys_metadata' };
17542-
}
17543-
if (binding !== '' && binding !== ownerPackageId) {
17544-
return { kind: 'mismatch', packageId: binding, ownerPackageId };
17545-
}
17546-
return { kind: 'overlay', packageId: binding || 'sys_metadata' };
17520+
// [#25467 cut 17] The body moved to `protocol-object-owner.ts` (see `ObjectOwnerHost`).
17521+
const readEngine = () => this.engine;
17522+
const host: ThisParameterType<typeof classifyObjectContribution> = {
17523+
get engine() { return readEngine(); },
17524+
};
17525+
return classifyObjectContribution.call(host, name, rowPackageId);
1754717526
}
1754817527

1754917528
/**
@@ -26123,28 +26102,12 @@ export class ObjectStackProtocolImplementation implements
2612326102
* for a body with no name, which register nothing.
2612426103
*/
2612526104
private releaseAdoptedObjectOwner(name: string, item: unknown, targetPackageId: string): boolean {
26126-
const registry: any = (this.engine as any)?.registry;
26127-
if (typeof registry?.registerObject !== 'function') return false;
26128-
if (!item || typeof item !== 'object' || typeof (item as { name?: unknown }).name !== 'string') return false;
26129-
const owner: { packageId?: unknown } | undefined = typeof registry.getObjectOwner === 'function'
26130-
? registry.getObjectOwner(name)
26131-
: undefined;
26132-
if (owner === undefined) return true;
26133-
if (owner.packageId !== 'sys_metadata' || typeof registry.unregisterObject !== 'function') return false;
26134-
try {
26135-
registry.unregisterObject(name);
26136-
return true;
26137-
} catch (err: any) {
26138-
console.error(
26139-
`[Protocol] reassignOrphanedMetadata: object '${name}' was adopted into package `
26140-
+ `'${targetPackageId}' in sys_metadata, but the registry keeps its package-less definition `
26141-
+ `(owner 'sys_metadata') until the next restart: ${err?.message ?? err}. Until then the data `
26142-
+ `plane serves the pre-adoption definition, and a save of the object naming '${targetPackageId}' `
26143-
+ 'answers success without going live. Fix: restart the server; the boot registers the object '
26144-
+ `under '${targetPackageId}'.`,
26145-
);
26146-
return false;
26147-
}
26105+
// [#25467 cut 17] The body moved to `protocol-object-owner.ts` (see `ObjectOwnerHost`).
26106+
const readEngine = () => this.engine;
26107+
const host: ThisParameterType<typeof releaseAdoptedObjectOwner> = {
26108+
get engine() { return readEngine(); },
26109+
};
26110+
return releaseAdoptedObjectOwner.call(host, name, item, targetPackageId);
2614826111
}
2614926112

2615026113
// ─────────────────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)