Skip to content

Commit 90ddb98

Browse files
committed
wip(metadata-protocol): the save door asks the registry's rule-copy predicate
Claude-Session: https://claude.ai/code/session_01L9qibx3A7AeHSnSr39oxaV Co-authored-by: Claude <noreply@anthropic.com>
1 parent 52306d4 commit 90ddb98

3 files changed

Lines changed: 364 additions & 1 deletion

File tree

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import type { ObjectStackProtocolImplementation } from './protocol.js';
5050
import { permissionSetOwnerBindingRefusal, runtimeHookWithoutBodyRefusal } from './protocol.js';
5151
import { datasourceCredentialsRefSaveRefusal } from './protocol-datasource-credentials-ref.js';
5252
import { evaluateRuntimeAuthoringGate } from './runtime-authoring-gate.js';
53-
import type { RuntimeAuthoringIssue, RuntimeAuthoringVerdict, RuntimePendingDeclarations } from './runtime-authoring-gate.js';
53+
import type { PackagedRuleCopyRefusal, RuntimeAuthoringIssue, RuntimeAuthoringVerdict, RuntimePendingDeclarations } from './runtime-authoring-gate.js';
5454
import { savedItemNameRefusal } from '@objectstack/metadata/view-container-name';
5555
import { PLURAL_TO_SINGULAR, SINGULAR_TO_PLURAL } from '@objectstack/spec/shared';
5656
import type { IObjectQLEngine } from '@objectstack/spec/contracts';
@@ -435,6 +435,13 @@ export async function judgeRuntimeAuthoringRules(
435435
// read here per publish and passed in so the gate stays pure.
436436
const hierarchyResolverMounted = this.hierarchyResolverMounted();
437437

438+
// [#28448] The registry's verdict on an object body's copies of packaged
439+
// validation rules — a host fact of the same kind, read here per publish
440+
// and passed in so the gate stays pure.
441+
const packagedRuleCopyRefusal = singular === 'object'
442+
? packagedRuleCopyRefusalOf(this.engine, evt.name, evt.body)
443+
: undefined;
444+
438445
const verdict = evaluateRuntimeAuthoringGate({
439446
type: singular,
440447
name: evt.name,
@@ -463,10 +470,42 @@ export async function judgeRuntimeAuthoringRules(
463470
...(rlsMembershipResolverKeys !== undefined ? { rlsMembershipResolverKeys } : {}),
464471
// [#26188] With it the gate refuses a hierarchy scope no resolver enforces.
465472
...(hierarchyResolverMounted !== undefined ? { hierarchyResolverMounted } : {}),
473+
// [#28448] With it the gate refuses a rule copy that changes what the package owns.
474+
...(packagedRuleCopyRefusal !== undefined ? { packagedRuleCopyRefusal } : {}),
466475
});
467476
return verdict;
468477
}
469478

479+
/**
480+
* [#28448 · ADR-0029 D9.2a, 2026-10-30 note; rulings of record 6524229156
481+
* (letter C′) and 6545641964 (letter B)] Ask the live registry whether a tenant
482+
* `object` body carries a copy of a packaged validation rule that changes what
483+
* the package owns — `SchemaRegistry.tenantRuleCopyRefusal`, the one predicate
484+
* the registry's load report reads too, so the save door and the load judge
485+
* one way. Its refusal is reduced to the fields the gate locates it with.
486+
*
487+
* `undefined` when the engine's registry carries no such predicate (a
488+
* metadata-only host, a test double): nothing is judged, as on the CLI.
489+
*/
490+
function packagedRuleCopyRefusalOf(
491+
engine: { registry?: unknown } | undefined,
492+
name: string,
493+
body: unknown,
494+
): PackagedRuleCopyRefusal | null | undefined {
495+
const registry = engine?.registry as { tenantRuleCopyRefusal?: unknown } | null | undefined;
496+
if (!registry || typeof registry.tenantRuleCopyRefusal !== 'function') return undefined;
497+
const refusal = (registry.tenantRuleCopyRefusal as (name: string, body: unknown) => unknown)
498+
.call(registry, name, body) as { ruleName?: unknown; changedKeys?: unknown; message?: unknown } | null;
499+
if (!refusal) return null;
500+
return {
501+
ruleName: typeof refusal.ruleName === 'string' ? refusal.ruleName : '',
502+
changedKeys: Array.isArray(refusal.changedKeys)
503+
? refusal.changedKeys.filter((key): key is string => typeof key === 'string')
504+
: [],
505+
message: typeof refusal.message === 'string' ? refusal.message : String(refusal),
506+
};
507+
}
508+
470509
/** The body of `ObjectStackProtocolImplementation#draftPublishVerdict`; the method's docblock there is its contract. */
471510
export async function draftPublishVerdict(
472511
this: RuntimeRulesHost & Pick<AuthoringGateHost, 'collectBatchPendingDeclarations' | 'credentialChannelFor' | 'getOverlayRepo' | 'objectNamespacePrefixViolation'>,

‎packages/metadata-protocol/src/runtime-authoring-gate.ts‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,84 @@ export function findPublicFormIntakeGaps(args: {
453453
return issues;
454454
}
455455

456+
// ─────────────────────────────────────────────────────────────────────────────
457+
// #28448 — a tenant's copy of a packaged validation rule that changes what the
458+
// package owns.
459+
// ─────────────────────────────────────────────────────────────────────────────
460+
461+
/**
462+
* The rule id of the finding below. Refused on publish, inside the gate's one
463+
* 422 `INVALID_METADATA` envelope.
464+
*
465+
* [ADR-0029 D9.2a, 2026-10-30 note; ruling of record 6524229156, letter C′;
466+
* ruling of record 6545641964, letter B] A validation rule's identity within an
467+
* object is its `name`. A tenant's copy of a rule a code layer ships (the
468+
* packaged owner's or an extension's) may change its `message` and `active`;
469+
* its condition, its `type` and every other key belong to the package. The
470+
* registry fold already runs the package's rule and reports such a copy at load
471+
* (`validation_rule_copy_changed`), never refusing it there, so an upgrade never
472+
* fails on stored data. This is the other half: the save door refuses the copy
473+
* when the admin writes it, with the prescription.
474+
*
475+
* ## Why it is gate-local
476+
*
477+
* Its input is a fact about the DEPLOYMENT: which rules the code packages
478+
* registered here ship, by name. A build machine cannot know what a tenant's
479+
* copy is a copy of, so `AUTHORING_RULES` must not judge it — the reason the
480+
* #6285 and #26188 rules live here. The judgement itself is the registry's
481+
* (`SchemaRegistry.tenantRuleCopyRefusal`), the same predicate its load report
482+
* reads, so the save and the load judge one way; the host asks it and hands
483+
* the verdict in ({@link PackagedRuleCopyRefusal}), and this function stays
484+
* pure.
485+
*/
486+
export const VALIDATION_RULE_COPY_CHANGED = 'validation-rule-copy-changed';
487+
488+
/** The prescription ruling C′ names, word for word. */
489+
const VALIDATION_RULE_COPY_PRESCRIPTION =
490+
'To change the condition, add a rule under a new name, or change it in the package.';
491+
492+
/**
493+
* The registry's verdict on a tenant `object` body's rule copies, as the host
494+
* reads it from `SchemaRegistry.tenantRuleCopyRefusal`: the first copy that
495+
* changes a key the package owns, the keys it changes (`type` first) and the
496+
* registry's sentence. `null` when every copy changes at most `message` and
497+
* `active`, or no code layer ships a rule of any name the body carries.
498+
*/
499+
export interface PackagedRuleCopyRefusal {
500+
ruleName: string;
501+
changedKeys: readonly string[];
502+
message: string;
503+
}
504+
505+
/**
506+
* Judge one about-to-be-published `object` body against the registry's verdict
507+
* on its rule copies: one `error` finding when the verdict refuses, located at
508+
* the copy's first changed key.
509+
*
510+
* PURE — the verdict arrives as an argument. Absent (a host whose engine has no
511+
* registry predicate, such as the CLI) nothing is judged.
512+
*/
513+
export function findPackagedRuleCopyGaps(args: {
514+
type: string;
515+
name: string;
516+
body: unknown;
517+
packagedRuleCopyRefusal?: PackagedRuleCopyRefusal | null;
518+
}): RuntimeAuthoringIssue[] {
519+
if (args.type !== 'object' || !args.packagedRuleCopyRefusal) return [];
520+
const { ruleName, changedKeys, message } = args.packagedRuleCopyRefusal;
521+
const rules = isRec(args.body) && Array.isArray(args.body.validations) ? args.body.validations : [];
522+
const index = rules.findIndex((rule) => isRec(rule) && rule.name === ruleName);
523+
const rulePath = index >= 0 ? `validations[${index}]` : 'validations';
524+
return [{
525+
severity: 'error',
526+
rule: VALIDATION_RULE_COPY_CHANGED,
527+
where: `object "${args.name}" · validation rule "${ruleName}"`,
528+
path: changedKeys.length > 0 && index >= 0 ? `${rulePath}.${changedKeys[0]}` : rulePath,
529+
message,
530+
hint: VALIDATION_RULE_COPY_PRESCRIPTION,
531+
}];
532+
}
533+
456534
// ─────────────────────────────────────────────────────────────────────────────
457535
// #26188 — a HIERARCHY scope on a runtime with no hierarchy resolver.
458536
// ─────────────────────────────────────────────────────────────────────────────
@@ -1118,6 +1196,15 @@ export function evaluateRuntimeAuthoringGate(args: {
11181196
* service registry), nothing is judged, as on the CLI.
11191197
*/
11201198
hierarchyResolverMounted?: boolean;
1199+
/**
1200+
* [#28448] The registry's verdict on an `object` body's copies of packaged
1201+
* validation rules ({@link PackagedRuleCopyRefusal}). A fact only the host
1202+
* holds, of the #6285 kind: gathered by the impure caller and passed in so
1203+
* this function stays pure. A verdict arms {@link findPackagedRuleCopyGaps};
1204+
* `null` is a clean verdict; absent (a host whose engine has no registry
1205+
* predicate), nothing is judged.
1206+
*/
1207+
packagedRuleCopyRefusal?: PackagedRuleCopyRefusal | null;
11211208
}): RuntimeAuthoringVerdict {
11221209
// [#26188] The one finding a draft hears: a hierarchy scope this runtime
11231210
// cannot enforce. Same rule as the publish refusal below, `warning` here.
@@ -1204,8 +1291,19 @@ export function evaluateRuntimeAuthoringGate(args: {
12041291
? { tenancyPostureInForce: args.tenancyPostureInForce }
12051292
: {}),
12061293
});
1294+
// [#28448] The gate-local refusal of a tenant's rule copy that changes what
1295+
// the package owns — the registry's verdict, located on the body.
1296+
const packagedRuleCopyGaps = findPackagedRuleCopyGaps({
1297+
type: args.type,
1298+
name: args.name,
1299+
body: args.body,
1300+
...(args.packagedRuleCopyRefusal !== undefined
1301+
? { packagedRuleCopyRefusal: args.packagedRuleCopyRefusal }
1302+
: {}),
1303+
});
12071304
const localIssues = [
12081305
...scheduleOrgGaps,
1306+
...packagedRuleCopyGaps,
12091307
...hierarchyScopeGaps,
12101308
...(pageSourceFindings ?? []).filter((f) => f.severity === 'error').map(toIssue),
12111309
];
@@ -1272,6 +1370,7 @@ export function evaluateRuntimeAuthoringGate(args: {
12721370
? [HIERARCHY_SCOPE_CAPABILITY_REQUIRED]
12731371
: []),
12741372
...(pageSourceFindings !== null ? [HTML_PAGE_SOURCE_COMPILE, PAGE_REQUIRES_DISAGREES_WITH_SOURCE] : []),
1373+
...(args.type === 'object' && args.packagedRuleCopyRefusal !== undefined ? [VALIDATION_RULE_COPY_CHANGED] : []),
12751374
];
12761375

12771376
if (unlintedWritesAllowed()) {

0 commit comments

Comments
 (0)