Skip to content

Commit f783577

Browse files
committed
feat(metadata-protocol)!: the save door refuses a tenant's copy of a packaged validation rule that changes what the package owns
The runtime authoring gate asks the registry's own predicate, SchemaRegistry.tenantRuleCopyRefusal, the one the load report reads, and folds its verdict into the gate's one 422 as the gate-local rule validation-rule-copy-changed, located at the copy's first changed key, with the ruling's prescription. A reworded or switched-off copy and a rule under a new name still save. Claude-Session: https://claude.ai/code/session_01L9qibx3A7AeHSnSr39oxaV Co-authored-by: Claude <noreply@anthropic.com>
1 parent 90ddb98 commit f783577

3 files changed

Lines changed: 97 additions & 4 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'@objectstack/metadata-protocol': minor
3+
---
4+
5+
feat(metadata-protocol)!: the save door refuses a tenant's copy of a packaged validation rule that changes the rule's condition, type or any other key the package owns (#28448)
6+
7+
Clause-②: no (narrowing) — an `object` publish that carries a same-named copy of a validation rule a code layer ships (the packaged owner's or an extension's), and changes anything but the copy's `message` and `active`, is now refused with `422 INVALID_METADATA`. The same publish was stored before, and the fold ran the package's rule instead of the copy's.
8+
Metadata-protocol: narrowed
9+
10+
<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no stored row needs rewriting: a copy stored before this change keeps loading, reported once at load, and the package's rule runs for it, as the registry fold already did. Only a new write of such a copy is refused, and its refusal carries the prescription. -->
11+
12+
**BREAKING** for a tenant save that changes a packaged validation rule (ADR-0029 D9.2a, 2026-10-30 note; ruling of record 6524229156, letter C′, and ruling of record 6545641964, letter B, which puts this refusal at the save door).
13+
14+
- **Before.** The registry fold ran the package's rule with the tenant copy's `message` and `active`, and said once at load (`validation_rule_copy_changed`) that the copy's condition does not run. The save that stored the copy answered success, so the admin who changed the condition was never told.
15+
- **Now.** The runtime authoring gate asks the registry's own predicate, `SchemaRegistry.tenantRuleCopyRefusal`, the one the load report reads. A publish of a copy that changes a package-owned key is refused with one issue, rule `validation-rule-copy-changed`, located at the copy's first changed key (for example `validations[1].condition`), with the prescription: "To change the condition, add a rule under a new name, or change it in the package." Nothing is written.
16+
- **Still saved.** A copy that changes only `message` (a reword) or `active` (`active: false` switches the rule off). A rule under a new name, which stays additive. A draft save, which no gate rule refuses; publishing it is judged.
17+
- **Who sees it.** An admin who edits a packaged or extension rule's condition or type in Studio, or an author doing the same through the metadata API of a runtime: the save now answers with the refusal instead of storing a copy that never runs. To keep a different condition, add it as a rule under a new name.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#28448] The gate-local `validation-rule-copy-changed` rule, on the pure gate:
5+
* the registry's verdict on a tenant object's copies of packaged validation
6+
* rules arrives as a host fact (`packagedRuleCopyRefusal`) and is folded into
7+
* the gate's one 422 envelope, located on the body. The end-to-end pins (a real
8+
* `SchemaRegistry` behind a real save door) live in `@objectstack/objectql`'s
9+
* `protocol-validation-rule-copy-save-refusal.test.ts`, the one package that
10+
* has both.
11+
*/
12+
13+
import { describe, expect, it } from 'vitest';
14+
import { evaluateRuntimeAuthoringGate, VALIDATION_RULE_COPY_CHANGED } from './runtime-authoring-gate.js';
15+
16+
const PRESCRIPTION = 'To change the condition, add a rule under a new name, or change it in the package.';
17+
18+
const body = {
19+
name: 'vr_duty',
20+
label: 'Duty',
21+
sharingModel: 'private',
22+
fields: { status: { name: 'status', label: 'Status', type: 'text' } },
23+
validations: [
24+
{ type: 'script', name: 'own_rule', message: 'Own.', condition: 'false' },
25+
{ type: 'script', name: 'ext_rule', message: 'Ext.', condition: "record.status == 'x'" },
26+
],
27+
};
28+
29+
const refusal = {
30+
ruleName: 'ext_rule',
31+
changedKeys: ['condition'],
32+
message: `Object "vr_duty": validation rule "ext_rule" is a packaged rule. ${PRESCRIPTION}`,
33+
};
34+
35+
describe('[#28448] the gate folds the registry\'s rule-copy verdict into its one refusal', () => {
36+
it('a refusing verdict is one located error, and the rule is disclosed as run', () => {
37+
const verdict = evaluateRuntimeAuthoringGate({
38+
type: 'object', name: 'vr_duty', state: 'active', body, packagedRuleCopyRefusal: refusal,
39+
});
40+
expect(verdict.error).toMatchObject({ code: 'INVALID_METADATA', status: 422 });
41+
const err = verdict.error as Error & { issues: any[]; rulesRun: string[] };
42+
expect(err.issues).toEqual([{
43+
severity: 'error',
44+
rule: VALIDATION_RULE_COPY_CHANGED,
45+
where: 'object "vr_duty" · validation rule "ext_rule"',
46+
path: 'validations[1].condition',
47+
message: refusal.message,
48+
hint: PRESCRIPTION,
49+
}]);
50+
expect(err.rulesRun).toContain(VALIDATION_RULE_COPY_CHANGED);
51+
});
52+
53+
it('a clean verdict refuses nothing', () => {
54+
const verdict = evaluateRuntimeAuthoringGate({
55+
type: 'object', name: 'vr_duty', state: 'active', body, packagedRuleCopyRefusal: null,
56+
});
57+
expect(verdict.error).toBeNull();
58+
});
59+
60+
it('no verdict (a host without the registry predicate) judges nothing', () => {
61+
const verdict = evaluateRuntimeAuthoringGate({ type: 'object', name: 'vr_duty', state: 'active', body });
62+
expect(verdict.error).toBeNull();
63+
});
64+
65+
it('a draft is not gated (D1), whatever the verdict', () => {
66+
const verdict = evaluateRuntimeAuthoringGate({
67+
type: 'object', name: 'vr_duty', state: 'draft', body, packagedRuleCopyRefusal: refusal,
68+
});
69+
expect(verdict.error).toBeNull();
70+
});
71+
});

‎packages/objectql/src/protocol-validation-rule-copy-save-refusal.test.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ const extRule = () => ({
6262
name: 'ext_blocked_needs_reason',
6363
message: 'Say what is blocking the duty.',
6464
condition: BLOCKED,
65-
fields: ['note'],
6665
});
6766

6867
const clone = <T>(v: T): T => JSON.parse(JSON.stringify(v)) as T;
@@ -140,9 +139,13 @@ async function seeded() {
140139
return host;
141140
}
142141

143-
/** The served object, as Studio holds it before the PUT. */
142+
/**
143+
* The served object, as Studio holds it before the PUT: a copy, as the wire
144+
* gives it. An in-process read hands back the registry's own rule objects, so
145+
* editing it in place would edit the extension's rule too.
146+
*/
144147
async function served(host: { protocol: ObjectStackProtocolImplementation }) {
145-
return (await host.protocol.getMetaItem({ type: 'object', name: OBJ } as never)).item as any;
148+
return clone((await host.protocol.getMetaItem({ type: 'object', name: OBJ } as never)).item) as any;
146149
}
147150

148151
/** The served object with the extension's rule copy edited by `edit`. */
@@ -185,7 +188,9 @@ describe('[#28448] the save door judges a tenant copy of a packaged validation r
185188

186189
it('a copy that changes the type is refused, `type` named', async () => {
187190
const host = await seeded();
188-
const item = await withCopy(host, (rule) => { rule.type = 'cross_field'; });
191+
// `cross_field` shares `script`'s condition and requires `fields`, so the
192+
// copy parses: what refuses it is the package owning the type.
193+
const item = await withCopy(host, (rule) => { rule.type = 'cross_field'; rule.fields = ['note']; });
189194
const err = await thrownBy(host.protocol.saveMetaItem({ type: 'object', name: OBJ, item } as never));
190195
expect(err).toMatchObject({ code: 'INVALID_METADATA', status: 422 });
191196
expect(err.issues).toEqual([expect.objectContaining({ rule: 'validation-rule-copy-changed' })]);

0 commit comments

Comments
 (0)