Skip to content

Commit c5f1351

Browse files
committed
refactor(metadata-protocol): move the write package scope resolution out of protocol.ts (#25467 cut 19)
Move the body of resolveWritePackageScope unedited into protocol-write-package-scope.ts, behind a WritePackageScopeHost that hands over the engine as a getter. The class keeps the method, its docblock and declared signature; its body becomes a host literal plus a .call. The authoring gate's host closure keeps calling the class method. Claude-Session: https://claude.ai/code/session_016SP1spj613tyGCgxoD1An6 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 77dc706 commit c5f1351

2 files changed

Lines changed: 96 additions & 38 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#25467 cut 19] A package write's validation scope, as
5+
* `ObjectStackProtocolImplementation` resolves it
6+
* (`resolveWritePackageScope`): the written package and the closure of the
7+
* dependencies its manifest declares, read from the registry, or `undefined`
8+
* wherever that cannot be read. Moved out of `protocol.ts` whole.
9+
*
10+
* A MOVE, not a redesign. The function below is the body of the
11+
* `ObjectStackProtocolImplementation` method of the same name as it stood,
12+
* re-indented one level and otherwise unedited: the same guards, the same
13+
* dependency walk, and the same silent `catch` answering `undefined`.
14+
*
15+
* ## How the moved body reaches the class
16+
*
17+
* The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts`
18+
* header carries the full reasoning), as `protocol-batch-data.ts` applies it.
19+
* The class keeps the method — the published `dist/*.d.ts` lists its name,
20+
* docblock and declared signature — and its body is one literal of exactly
21+
* the {@link WritePackageScopeHost} members it reads, plus a `.call`. The
22+
* engine is handed over as a getter, so the body reads the protocol's engine
23+
* (and so its registry) at call time. Every other reader of the method keeps
24+
* calling the class method: `tenantAuthoredWriteRefusal`, and the authoring
25+
* gate's host (cut 8, `protocol-authoring-gate.ts`), whose closure calls it.
26+
*
27+
* The body reads no module-level declaration of `protocol.ts` and no import,
28+
* so this module imports only the class type. Nothing re-exports this module,
29+
* so the published surface does not move.
30+
*/
31+
32+
import type { ObjectStackProtocolImplementation } from './protocol.js';
33+
34+
/**
35+
* The `ObjectStackProtocolImplementation` members the function below reads —
36+
* every one of them, and no other; the 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 WritePackageScopeHost {
42+
readonly engine: ObjectStackProtocolImplementation['engine'];
43+
}
44+
45+
/** The body of `ObjectStackProtocolImplementation#resolveWritePackageScope`; the method's docblock there is its contract. */
46+
export function resolveWritePackageScope(
47+
this: Pick<WritePackageScopeHost, 'engine'>,
48+
packageId: string | null | undefined,
49+
): { packageId: string; dependencies: string[] } | undefined {
50+
if (typeof packageId !== 'string' || packageId === '' || packageId === 'sys_metadata') return undefined;
51+
try {
52+
const registry = this.engine.registry as
53+
| { getPackage?: (id: string) => { manifest?: unknown } | undefined }
54+
| undefined;
55+
if (typeof registry?.getPackage !== 'function') return undefined;
56+
// The written package itself must be readable: its manifest is the
57+
// ONLY declaration of what it may reference, and narrowing without
58+
// it would be narrowing on nothing.
59+
if (!registry.getPackage(packageId)) return undefined;
60+
61+
const dependencies = new Set<string>();
62+
const frontier = [packageId];
63+
while (frontier.length > 0) {
64+
const current = frontier.pop() as string;
65+
const manifest = registry.getPackage(current)?.manifest as
66+
| { dependencies?: Record<string, unknown> }
67+
| undefined;
68+
const declared = manifest?.dependencies;
69+
if (!declared || typeof declared !== 'object') continue;
70+
for (const dep of Object.keys(declared)) {
71+
// A dependency the registry cannot resolve is still IN the
72+
// closure: the package declared it, so an object stamped
73+
// with it is reachable by declaration. Only the transitive
74+
// walk stops there — `getPackage` returns undefined and the
75+
// loop above simply contributes nothing further.
76+
if (dep === packageId || dependencies.has(dep)) continue;
77+
dependencies.add(dep);
78+
frontier.push(dep);
79+
}
80+
}
81+
return { packageId, dependencies: [...dependencies] };
82+
} catch {
83+
// Context gathering must never fail a write (the same contract
84+
// `listCollection` above holds). An unreadable registry means an
85+
// unnarrowed gate, not a refused publish.
86+
return undefined;
87+
}
88+
}

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

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ import { namesWithOwnStoredRow, readActiveOverlayRows, readItemActiveRows, store
402402
import { classifyObjectContribution, releaseAdoptedObjectOwner } from './protocol-object-owner.js';
403403
// [#25467 cut 18] The flow save's retired-spelling replay verdict (#23903 ruling B′): does a save newly introduce a retired spelling no stored row carries.
404404
import { flowSaveReplaysRetired } from './protocol-flow-save-replay.js';
405+
// [#25467 cut 19] A package write's validation scope: the written package and its declared dependency closure, from the registry.
406+
import { resolveWritePackageScope } from './protocol-write-package-scope.js';
405407

406408
/**
407409
* Canonical Zod schema per metadata type lives in
@@ -7361,44 +7363,12 @@ export class ObjectStackProtocolImplementation implements
73617363
private resolveWritePackageScope(
73627364
packageId: string | null | undefined,
73637365
): { packageId: string; dependencies: string[] } | undefined {
7364-
if (typeof packageId !== 'string' || packageId === '' || packageId === 'sys_metadata') return undefined;
7365-
try {
7366-
const registry = this.engine.registry as
7367-
| { getPackage?: (id: string) => { manifest?: unknown } | undefined }
7368-
| undefined;
7369-
if (typeof registry?.getPackage !== 'function') return undefined;
7370-
// The written package itself must be readable: its manifest is the
7371-
// ONLY declaration of what it may reference, and narrowing without
7372-
// it would be narrowing on nothing.
7373-
if (!registry.getPackage(packageId)) return undefined;
7374-
7375-
const dependencies = new Set<string>();
7376-
const frontier = [packageId];
7377-
while (frontier.length > 0) {
7378-
const current = frontier.pop() as string;
7379-
const manifest = registry.getPackage(current)?.manifest as
7380-
| { dependencies?: Record<string, unknown> }
7381-
| undefined;
7382-
const declared = manifest?.dependencies;
7383-
if (!declared || typeof declared !== 'object') continue;
7384-
for (const dep of Object.keys(declared)) {
7385-
// A dependency the registry cannot resolve is still IN the
7386-
// closure: the package declared it, so an object stamped
7387-
// with it is reachable by declaration. Only the transitive
7388-
// walk stops there — `getPackage` returns undefined and the
7389-
// loop above simply contributes nothing further.
7390-
if (dep === packageId || dependencies.has(dep)) continue;
7391-
dependencies.add(dep);
7392-
frontier.push(dep);
7393-
}
7394-
}
7395-
return { packageId, dependencies: [...dependencies] };
7396-
} catch {
7397-
// Context gathering must never fail a write (the same contract
7398-
// `listCollection` above holds). An unreadable registry means an
7399-
// unnarrowed gate, not a refused publish.
7400-
return undefined;
7401-
}
7366+
// [#25467 cut 19] The body moved to `protocol-write-package-scope.ts` (see `WritePackageScopeHost`).
7367+
const readEngine = () => this.engine;
7368+
const host: ThisParameterType<typeof resolveWritePackageScope> = {
7369+
get engine() { return readEngine(); },
7370+
};
7371+
return resolveWritePackageScope.call(host, packageId);
74027372
}
74037373

74047374
/**

0 commit comments

Comments
 (0)